1 /* Python interface to lazy strings.
2 
3    Copyright (C) 2010-2013 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "defs.h"
21 #include "python-internal.h"
22 #include "charset.h"
23 #include "value.h"
24 #include "exceptions.h"
25 #include "valprint.h"
26 #include "language.h"
27 #include "gdb_assert.h"
28 
29 typedef struct {
30   PyObject_HEAD
31   /*  Holds the address of the lazy string.  */
32   CORE_ADDR address;
33 
34   /*  Holds the encoding that will be applied to the string
35       when the string is printed by GDB.  If the encoding is set
36       to None then GDB will select the most appropriate
37       encoding when the sting is printed.  */
38   char *encoding;
39 
40   /* Holds the length of the string in characters.  If the
41      length is -1, then the string will be fetched and encoded up to
42      the first null of appropriate width.  */
43   long length;
44 
45   /*  This attribute holds the type that is represented by the lazy
46       string's type.  */
47   struct type *type;
48 } lazy_string_object;
49 
50 static PyTypeObject lazy_string_object_type;
51 
52 static PyObject *
53 stpy_get_address (PyObject *self, void *closure)
54 {
55   lazy_string_object *self_string = (lazy_string_object *) self;
56 
57   return gdb_py_long_from_ulongest (self_string->address);
58 }
59 
60 static PyObject *
61 stpy_get_encoding (PyObject *self, void *closure)
62 {
63   lazy_string_object *self_string = (lazy_string_object *) self;
64   PyObject *result;
65 
66   /* An encoding can be set to NULL by the user, so check before
67      attempting a Python FromString call.  If NULL return Py_None.  */
68   if (self_string->encoding)
69     result = PyString_FromString (self_string->encoding);
70   else
71     {
72       result = Py_None;
73       Py_INCREF (result);
74     }
75 
76   return result;
77 }
78 
79 static PyObject *
80 stpy_get_length (PyObject *self, void *closure)
81 {
82   lazy_string_object *self_string = (lazy_string_object *) self;
83 
84   return PyLong_FromLong (self_string->length);
85 }
86 
87 static PyObject *
88 stpy_get_type (PyObject *self, void *closure)
89 {
90   lazy_string_object *str_obj = (lazy_string_object *) self;
91 
92   return type_to_type_object (str_obj->type);
93 }
94 
95 static PyObject *
96 stpy_convert_to_value  (PyObject *self, PyObject *args)
97 {
98   lazy_string_object *self_string = (lazy_string_object *) self;
99   struct value *val = NULL;
100   volatile struct gdb_exception except;
101 
102   if (self_string->address == 0)
103     {
104       PyErr_SetString (PyExc_MemoryError,
105 		       _("Cannot create a value from NULL."));
106       return NULL;
107     }
108 
109   TRY_CATCH (except, RETURN_MASK_ALL)
110     {
111       val = value_at_lazy (self_string->type, self_string->address);
112     }
113   GDB_PY_HANDLE_EXCEPTION (except);
114 
115   return value_to_value_object (val);
116 }
117 
118 static void
119 stpy_dealloc (PyObject *self)
120 {
121   lazy_string_object *self_string = (lazy_string_object *) self;
122 
123   xfree (self_string->encoding);
124 }
125 
126 PyObject *
127 gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
128 			   const char *encoding, struct type *type)
129 {
130   lazy_string_object *str_obj = NULL;
131 
132   if (address == 0 && length != 0)
133     {
134       PyErr_SetString (PyExc_MemoryError,
135 		       _("Cannot create a lazy string with address 0x0, " \
136 			 "and a non-zero length."));
137       return NULL;
138     }
139 
140   if (!type)
141     {
142       PyErr_SetString (PyExc_RuntimeError,
143 		       _("A lazy string's type cannot be NULL."));
144       return NULL;
145     }
146 
147   str_obj = PyObject_New (lazy_string_object, &lazy_string_object_type);
148   if (!str_obj)
149     return NULL;
150 
151   str_obj->address = address;
152   str_obj->length = length;
153   if (encoding == NULL || !strcmp (encoding, ""))
154     str_obj->encoding = NULL;
155   else
156     str_obj->encoding = xstrdup (encoding);
157   str_obj->type = type;
158 
159   return (PyObject *) str_obj;
160 }
161 
162 void
163 gdbpy_initialize_lazy_string (void)
164 {
165   if (PyType_Ready (&lazy_string_object_type) < 0)
166     return;
167 
168   Py_INCREF (&lazy_string_object_type);
169 }
170 
171 /* Determine whether the printer object pointed to by OBJ is a
172    Python lazy string.  */
173 int
174 gdbpy_is_lazy_string (PyObject *result)
175 {
176   return PyObject_TypeCheck (result, &lazy_string_object_type);
177 }
178 
179 /* Extract the parameters from the lazy string object STRING.
180    ENCODING will either be set to NULL, or will be allocated with
181    xmalloc, in which case the callers is responsible for freeing
182    it.  */
183 
184 void
185 gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
186 			   struct type **str_type,
187 			   long *length, char **encoding)
188 {
189   lazy_string_object *lazy;
190 
191   gdb_assert (gdbpy_is_lazy_string (string));
192 
193   lazy = (lazy_string_object *) string;
194 
195   *addr = lazy->address;
196   *str_type = lazy->type;
197   *length = lazy->length;
198   *encoding = lazy->encoding ? xstrdup (lazy->encoding) : NULL;
199 }
200 
201 
202 
203 static PyMethodDef lazy_string_object_methods[] = {
204   { "value", stpy_convert_to_value, METH_NOARGS,
205     "Create a (lazy) value that contains a pointer to the string." },
206   {NULL}  /* Sentinel */
207 };
208 
209 
210 static PyGetSetDef lazy_string_object_getset[] = {
211   { "address", stpy_get_address, NULL, "Address of the string.", NULL },
212   { "encoding", stpy_get_encoding, NULL, "Encoding of the string.", NULL },
213   { "length", stpy_get_length, NULL, "Length of the string.", NULL },
214   { "type", stpy_get_type, NULL, "Type associated with the string.", NULL },
215   { NULL }  /* Sentinel */
216 };
217 
218 static PyTypeObject lazy_string_object_type = {
219   PyVarObject_HEAD_INIT (NULL, 0)
220   "gdb.LazyString",	          /*tp_name*/
221   sizeof (lazy_string_object),	  /*tp_basicsize*/
222   0,				  /*tp_itemsize*/
223   stpy_dealloc,                   /*tp_dealloc*/
224   0,				  /*tp_print*/
225   0,				  /*tp_getattr*/
226   0,				  /*tp_setattr*/
227   0,				  /*tp_compare*/
228   0,				  /*tp_repr*/
229   0,				  /*tp_as_number*/
230   0,				  /*tp_as_sequence*/
231   0,				  /*tp_as_mapping*/
232   0,				  /*tp_hash */
233   0,				  /*tp_call*/
234   0,				  /*tp_str*/
235   0,				  /*tp_getattro*/
236   0,				  /*tp_setattro*/
237   0,				  /*tp_as_buffer*/
238   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
239   "GDB lazy string object",	  /* tp_doc */
240   0,				  /* tp_traverse */
241   0,				  /* tp_clear */
242   0,				  /* tp_richcompare */
243   0,				  /* tp_weaklistoffset */
244   0,			          /* tp_iter */
245   0,				  /* tp_iternext */
246   lazy_string_object_methods,	  /* tp_methods */
247   0,				  /* tp_members */
248   lazy_string_object_getset	  /* tp_getset */
249 };
250