1 /* Python interface to lazy strings.
3 Copyright (C) 2010 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "python-internal.h"
24 #include "exceptions.h"
30 /* Holds the address of the lazy string. */
33 /* Holds the encoding that will be applied to the string
34 when the string is printed by GDB. If the encoding is set
35 to None then GDB will select the most appropriate
36 encoding when the sting is printed. */
39 /* Holds the length of the string in characters. If the
40 length is -1, then the string will be fetched and encoded up to
41 the first null of appropriate width. */
44 /* This attribute holds the type that is represented by the lazy
49 static PyTypeObject lazy_string_object_type
;
52 stpy_get_address (PyObject
*self
, void *closure
)
54 lazy_string_object
*self_string
= (lazy_string_object
*) self
;
55 return PyLong_FromUnsignedLongLong (self_string
->address
);
59 stpy_get_encoding (PyObject
*self
, void *closure
)
61 lazy_string_object
*self_string
= (lazy_string_object
*) self
;
64 /* An encoding can be set to NULL by the user, so check before
65 attempting a Python FromString call. If NULL return Py_None. */
66 if (self_string
->encoding
)
67 result
= PyString_FromString (self_string
->encoding
);
78 stpy_get_length (PyObject
*self
, void *closure
)
80 lazy_string_object
*self_string
= (lazy_string_object
*) self
;
81 return PyLong_FromLong (self_string
->length
);
85 stpy_get_type (PyObject
*self
, void *closure
)
87 lazy_string_object
*str_obj
= (lazy_string_object
*) self
;
88 return type_to_type_object (str_obj
->type
);
92 stpy_convert_to_value (PyObject
*self
, PyObject
*args
)
94 lazy_string_object
*self_string
= (lazy_string_object
*) self
;
97 if (self_string
->address
== 0)
99 PyErr_SetString (PyExc_MemoryError
,
100 _("Cannot create a value from NULL."));
104 val
= value_at_lazy (self_string
->type
, self_string
->address
);
105 return value_to_value_object (val
);
109 stpy_dealloc (PyObject
*self
)
111 lazy_string_object
*self_string
= (lazy_string_object
*) self
;
112 xfree (self_string
->encoding
);
116 gdbpy_create_lazy_string_object (CORE_ADDR address
, long length
,
117 const char *encoding
, struct type
*type
)
119 lazy_string_object
*str_obj
= NULL
;
121 if (address
== 0 && length
!= 0)
123 PyErr_SetString (PyExc_MemoryError
,
124 _("Cannot create a lazy string with address 0x0, " \
125 "and a non-zero length."));
131 PyErr_SetString (PyExc_RuntimeError
,
132 _("A lazy string's type cannot be NULL."));
136 str_obj
= PyObject_New (lazy_string_object
, &lazy_string_object_type
);
140 str_obj
->address
= address
;
141 str_obj
->length
= length
;
142 if (encoding
== NULL
|| !strcmp (encoding
, ""))
143 str_obj
->encoding
= NULL
;
145 str_obj
->encoding
= xstrdup (encoding
);
146 str_obj
->type
= type
;
148 return (PyObject
*) str_obj
;
152 gdbpy_initialize_lazy_string (void)
154 if (PyType_Ready (&lazy_string_object_type
) < 0)
157 Py_INCREF (&lazy_string_object_type
);
160 /* Determine whether the printer object pointed to by OBJ is a
161 Python lazy string. */
163 gdbpy_is_lazy_string (PyObject
*result
)
165 return PyObject_TypeCheck (result
, &lazy_string_object_type
);
168 /* Extract and return the actual string from the lazy string object
169 STRING. Addtionally, the string type is written to *STR_TYPE, the
170 string length is written to *LENGTH, and the string encoding is
171 written to *ENCODING. On error, NULL is returned. The caller is
172 responsible for freeing the returned buffer. */
174 gdbpy_extract_lazy_string (PyObject
*string
, struct type
**str_type
,
175 long *length
, char **encoding
)
179 gdb_byte
*buffer
= NULL
;
182 struct gdbarch
*gdbarch
;
183 enum bfd_endian byte_order
;
184 PyObject
*py_len
= NULL
, *py_encoding
= NULL
;
185 PyObject
*py_addr
= NULL
, *py_type
= NULL
;
186 volatile struct gdb_exception except
;
188 py_len
= PyObject_GetAttrString (string
, "length");
189 py_encoding
= PyObject_GetAttrString (string
, "encoding");
190 py_addr
= PyObject_GetAttrString (string
, "address");
191 py_type
= PyObject_GetAttrString (string
, "type");
193 /* A NULL encoding, length, address or type is not ok. */
194 if (!py_len
|| !py_encoding
|| !py_addr
|| !py_type
)
197 *length
= PyLong_AsLong (py_len
);
198 addr
= PyLong_AsUnsignedLongLong (py_addr
);
200 /* If the user supplies Py_None an encoding, set encoding to NULL.
201 This will trigger the resulting LA_PRINT_CALL to automatically
202 select an encoding. */
203 if (py_encoding
== Py_None
)
206 *encoding
= xstrdup (PyString_AsString (py_encoding
));
208 *str_type
= type_object_to_type (py_type
);
209 gdbarch
= get_type_arch (*str_type
);
210 byte_order
= gdbarch_byte_order (gdbarch
);
211 width
= TYPE_LENGTH (*str_type
);
213 TRY_CATCH (except
, RETURN_MASK_ALL
)
215 errcode
= read_string (addr
, *length
, width
,
216 *length
, byte_order
, &buffer
,
219 if (except
.reason
< 0)
221 PyErr_Format (except
.reason
== RETURN_QUIT \
222 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
, \
223 "%s", except
.message
); \
231 *length
= bytes_read
/ width
;
233 Py_DECREF (py_encoding
);
240 Py_XDECREF (py_encoding
);
242 Py_XDECREF (py_addr
);
243 Py_XDECREF (py_type
);
252 static PyMethodDef lazy_string_object_methods
[] = {
253 { "value", stpy_convert_to_value
, METH_NOARGS
,
254 "Create a (lazy) value that contains a pointer to the string." },
255 {NULL
} /* Sentinel */
259 static PyGetSetDef lazy_string_object_getset
[] = {
260 { "address", stpy_get_address
, NULL
, "Address of the string.", NULL
},
261 { "encoding", stpy_get_encoding
, NULL
, "Encoding of the string.", NULL
},
262 { "length", stpy_get_length
, NULL
, "Length of the string.", NULL
},
263 { "type", stpy_get_type
, NULL
, "Type associated with the string.", NULL
},
264 { NULL
} /* Sentinel */
267 static PyTypeObject lazy_string_object_type
= {
268 PyObject_HEAD_INIT (NULL
)
270 "gdb.LazyString", /*tp_name*/
271 sizeof (lazy_string_object
), /*tp_basicsize*/
273 stpy_dealloc
, /*tp_dealloc*/
280 0, /*tp_as_sequence*/
288 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
289 "GDB lazy string object", /* tp_doc */
292 0, /* tp_richcompare */
293 0, /* tp_weaklistoffset */
296 lazy_string_object_methods
, /* tp_methods */
298 lazy_string_object_getset
/* tp_getset */