1 /* Python interface to lazy strings.
3 Copyright (C) 2010-2014 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"
27 #include "gdb_assert.h"
31 /* Holds the address of the lazy string. */
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. */
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. */
45 /* This attribute holds the type that is represented by the lazy
50 static PyTypeObject lazy_string_object_type
51 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("lazy_string_object");
54 stpy_get_address (PyObject *self, void *closure)
56 lazy_string_object *self_string = (lazy_string_object *) self;
58 return gdb_py_long_from_ulongest (self_string->address);
62 stpy_get_encoding (PyObject *self, void *closure)
64 lazy_string_object *self_string = (lazy_string_object *) self;
67 /* An encoding can be set to NULL by the user, so check before
68 attempting a Python FromString call. If NULL return Py_None. */
69 if (self_string->encoding)
70 result = PyString_FromString (self_string->encoding);
81 stpy_get_length (PyObject *self, void *closure)
83 lazy_string_object *self_string = (lazy_string_object *) self;
85 return PyLong_FromLong (self_string->length);
89 stpy_get_type (PyObject *self, void *closure)
91 lazy_string_object *str_obj = (lazy_string_object *) self;
93 return type_to_type_object (str_obj->type);
97 stpy_convert_to_value (PyObject *self, PyObject *args)
99 lazy_string_object *self_string = (lazy_string_object *) self;
100 struct value *val = NULL;
101 volatile struct gdb_exception except;
103 if (self_string->address == 0)
105 PyErr_SetString (PyExc_MemoryError,
106 _("Cannot create a value from NULL."));
110 TRY_CATCH (except, RETURN_MASK_ALL)
112 val = value_at_lazy (self_string->type, self_string->address);
114 GDB_PY_HANDLE_EXCEPTION (except);
116 return value_to_value_object (val);
120 stpy_dealloc (PyObject *self)
122 lazy_string_object *self_string = (lazy_string_object *) self;
124 xfree (self_string->encoding);
128 gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
129 const char *encoding, struct type *type)
131 lazy_string_object *str_obj = NULL;
133 if (address == 0 && length != 0)
135 PyErr_SetString (PyExc_MemoryError,
136 _("Cannot create a lazy string with address 0x0, " \
137 "and a non-zero length."));
143 PyErr_SetString (PyExc_RuntimeError,
144 _("A lazy string's type cannot be NULL."));
148 str_obj = PyObject_New (lazy_string_object, &lazy_string_object_type);
152 str_obj->address = address;
153 str_obj->length = length;
154 if (encoding == NULL || !strcmp (encoding, ""))
155 str_obj->encoding = NULL;
157 str_obj->encoding = xstrdup (encoding);
158 str_obj->type = type;
160 return (PyObject *) str_obj;
164 gdbpy_initialize_lazy_string (void)
166 if (PyType_Ready (&lazy_string_object_type) < 0)
169 Py_INCREF (&lazy_string_object_type);
173 /* Determine whether the printer object pointed to by OBJ is a
174 Python lazy string. */
176 gdbpy_is_lazy_string (PyObject *result)
178 return PyObject_TypeCheck (result, &lazy_string_object_type);
181 /* Extract the parameters from the lazy string object STRING.
182 ENCODING will either be set to NULL, or will be allocated with
183 xmalloc, in which case the callers is responsible for freeing
187 gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
188 struct type **str_type,
189 long *length, char **encoding)
191 lazy_string_object *lazy;
193 gdb_assert (gdbpy_is_lazy_string (string));
195 lazy = (lazy_string_object *) string;
197 *addr = lazy->address;
198 *str_type = lazy->type;
199 *length = lazy->length;
200 *encoding = lazy->encoding ? xstrdup (lazy->encoding) : NULL;
205 static PyMethodDef lazy_string_object_methods[] = {
206 { "value", stpy_convert_to_value, METH_NOARGS,
207 "Create a (lazy) value that contains a pointer to the string." },
208 {NULL} /* Sentinel */
212 static PyGetSetDef lazy_string_object_getset[] = {
213 { "address", stpy_get_address, NULL, "Address of the string.", NULL },
214 { "encoding", stpy_get_encoding, NULL, "Encoding of the string.", NULL },
215 { "length", stpy_get_length, NULL, "Length of the string.", NULL },
216 { "type", stpy_get_type, NULL, "Type associated with the string.", NULL },
217 { NULL } /* Sentinel */
220 static PyTypeObject lazy_string_object_type = {
221 PyVarObject_HEAD_INIT (NULL, 0)
222 "gdb.LazyString", /*tp_name*/
223 sizeof (lazy_string_object), /*tp_basicsize*/
225 stpy_dealloc, /*tp_dealloc*/
232 0, /*tp_as_sequence*/
240 Py_TPFLAGS_DEFAULT, /*tp_flags*/
241 "GDB lazy string object", /* tp_doc */
244 0, /* tp_richcompare */
245 0, /* tp_weaklistoffset */
248 lazy_string_object_methods, /* tp_methods */
250 lazy_string_object_getset /* tp_getset */