Commit | Line | Data |
---|---|---|
be759fcf PM |
1 | /* Python interface to lazy strings. |
2 | ||
28e7fd62 | 3 | Copyright (C) 2010-2013 Free Software Foundation, Inc. |
be759fcf PM |
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" | |
09ca9e2e | 27 | #include "gdb_assert.h" |
be759fcf PM |
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 | ||
62eec1a5 TT |
50 | static PyTypeObject lazy_string_object_type |
51 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("lazy_string_object"); | |
be759fcf PM |
52 | |
53 | static PyObject * | |
54 | stpy_get_address (PyObject *self, void *closure) | |
55 | { | |
56 | lazy_string_object *self_string = (lazy_string_object *) self; | |
d59b6f6c | 57 | |
74aedc46 | 58 | return gdb_py_long_from_ulongest (self_string->address); |
be759fcf PM |
59 | } |
60 | ||
61 | static PyObject * | |
62 | stpy_get_encoding (PyObject *self, void *closure) | |
63 | { | |
64 | lazy_string_object *self_string = (lazy_string_object *) self; | |
65 | PyObject *result; | |
66 | ||
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); | |
71 | else | |
72 | { | |
73 | result = Py_None; | |
74 | Py_INCREF (result); | |
75 | } | |
76 | ||
77 | return result; | |
78 | } | |
79 | ||
80 | static PyObject * | |
81 | stpy_get_length (PyObject *self, void *closure) | |
82 | { | |
83 | lazy_string_object *self_string = (lazy_string_object *) self; | |
d59b6f6c | 84 | |
be759fcf PM |
85 | return PyLong_FromLong (self_string->length); |
86 | } | |
87 | ||
49a8461d | 88 | static PyObject * |
be759fcf PM |
89 | stpy_get_type (PyObject *self, void *closure) |
90 | { | |
91 | lazy_string_object *str_obj = (lazy_string_object *) self; | |
d59b6f6c | 92 | |
be759fcf PM |
93 | return type_to_type_object (str_obj->type); |
94 | } | |
95 | ||
96 | static PyObject * | |
97 | stpy_convert_to_value (PyObject *self, PyObject *args) | |
98 | { | |
99 | lazy_string_object *self_string = (lazy_string_object *) self; | |
76dce0be PM |
100 | struct value *val = NULL; |
101 | volatile struct gdb_exception except; | |
be759fcf | 102 | |
fff5cc64 PM |
103 | if (self_string->address == 0) |
104 | { | |
105 | PyErr_SetString (PyExc_MemoryError, | |
044c0f87 | 106 | _("Cannot create a value from NULL.")); |
fff5cc64 PM |
107 | return NULL; |
108 | } | |
109 | ||
76dce0be PM |
110 | TRY_CATCH (except, RETURN_MASK_ALL) |
111 | { | |
112 | val = value_at_lazy (self_string->type, self_string->address); | |
113 | } | |
114 | GDB_PY_HANDLE_EXCEPTION (except); | |
115 | ||
be759fcf PM |
116 | return value_to_value_object (val); |
117 | } | |
118 | ||
119 | static void | |
120 | stpy_dealloc (PyObject *self) | |
121 | { | |
122 | lazy_string_object *self_string = (lazy_string_object *) self; | |
d59b6f6c | 123 | |
be759fcf PM |
124 | xfree (self_string->encoding); |
125 | } | |
126 | ||
127 | PyObject * | |
128 | gdbpy_create_lazy_string_object (CORE_ADDR address, long length, | |
129 | const char *encoding, struct type *type) | |
130 | { | |
131 | lazy_string_object *str_obj = NULL; | |
132 | ||
fff5cc64 | 133 | if (address == 0 && length != 0) |
be759fcf PM |
134 | { |
135 | PyErr_SetString (PyExc_MemoryError, | |
fff5cc64 PM |
136 | _("Cannot create a lazy string with address 0x0, " \ |
137 | "and a non-zero length.")); | |
be759fcf PM |
138 | return NULL; |
139 | } | |
140 | ||
141 | if (!type) | |
142 | { | |
143 | PyErr_SetString (PyExc_RuntimeError, | |
044c0f87 | 144 | _("A lazy string's type cannot be NULL.")); |
be759fcf PM |
145 | return NULL; |
146 | } | |
147 | ||
148 | str_obj = PyObject_New (lazy_string_object, &lazy_string_object_type); | |
149 | if (!str_obj) | |
150 | return NULL; | |
151 | ||
152 | str_obj->address = address; | |
153 | str_obj->length = length; | |
154 | if (encoding == NULL || !strcmp (encoding, "")) | |
155 | str_obj->encoding = NULL; | |
156 | else | |
157 | str_obj->encoding = xstrdup (encoding); | |
158 | str_obj->type = type; | |
159 | ||
160 | return (PyObject *) str_obj; | |
161 | } | |
162 | ||
163 | void | |
164 | gdbpy_initialize_lazy_string (void) | |
165 | { | |
166 | if (PyType_Ready (&lazy_string_object_type) < 0) | |
167 | return; | |
168 | ||
169 | Py_INCREF (&lazy_string_object_type); | |
170 | } | |
171 | ||
172 | /* Determine whether the printer object pointed to by OBJ is a | |
173 | Python lazy string. */ | |
174 | int | |
175 | gdbpy_is_lazy_string (PyObject *result) | |
176 | { | |
177 | return PyObject_TypeCheck (result, &lazy_string_object_type); | |
178 | } | |
179 | ||
09ca9e2e TT |
180 | /* Extract the parameters from the lazy string object STRING. |
181 | ENCODING will either be set to NULL, or will be allocated with | |
182 | xmalloc, in which case the callers is responsible for freeing | |
183 | it. */ | |
be759fcf | 184 | |
09ca9e2e TT |
185 | void |
186 | gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr, | |
187 | struct type **str_type, | |
188 | long *length, char **encoding) | |
189 | { | |
190 | lazy_string_object *lazy; | |
be759fcf | 191 | |
09ca9e2e | 192 | gdb_assert (gdbpy_is_lazy_string (string)); |
be759fcf | 193 | |
09ca9e2e | 194 | lazy = (lazy_string_object *) string; |
be759fcf | 195 | |
09ca9e2e TT |
196 | *addr = lazy->address; |
197 | *str_type = lazy->type; | |
198 | *length = lazy->length; | |
199 | *encoding = lazy->encoding ? xstrdup (lazy->encoding) : NULL; | |
be759fcf PM |
200 | } |
201 | ||
202 | \f | |
203 | ||
204 | static PyMethodDef lazy_string_object_methods[] = { | |
205 | { "value", stpy_convert_to_value, METH_NOARGS, | |
206 | "Create a (lazy) value that contains a pointer to the string." }, | |
207 | {NULL} /* Sentinel */ | |
208 | }; | |
209 | ||
210 | ||
211 | static PyGetSetDef lazy_string_object_getset[] = { | |
212 | { "address", stpy_get_address, NULL, "Address of the string.", NULL }, | |
213 | { "encoding", stpy_get_encoding, NULL, "Encoding of the string.", NULL }, | |
214 | { "length", stpy_get_length, NULL, "Length of the string.", NULL }, | |
215 | { "type", stpy_get_type, NULL, "Type associated with the string.", NULL }, | |
216 | { NULL } /* Sentinel */ | |
217 | }; | |
218 | ||
219 | static PyTypeObject lazy_string_object_type = { | |
9a27f2c6 | 220 | PyVarObject_HEAD_INIT (NULL, 0) |
be759fcf PM |
221 | "gdb.LazyString", /*tp_name*/ |
222 | sizeof (lazy_string_object), /*tp_basicsize*/ | |
223 | 0, /*tp_itemsize*/ | |
224 | stpy_dealloc, /*tp_dealloc*/ | |
225 | 0, /*tp_print*/ | |
226 | 0, /*tp_getattr*/ | |
227 | 0, /*tp_setattr*/ | |
228 | 0, /*tp_compare*/ | |
229 | 0, /*tp_repr*/ | |
230 | 0, /*tp_as_number*/ | |
231 | 0, /*tp_as_sequence*/ | |
232 | 0, /*tp_as_mapping*/ | |
233 | 0, /*tp_hash */ | |
234 | 0, /*tp_call*/ | |
235 | 0, /*tp_str*/ | |
236 | 0, /*tp_getattro*/ | |
237 | 0, /*tp_setattro*/ | |
238 | 0, /*tp_as_buffer*/ | |
239 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
240 | "GDB lazy string object", /* tp_doc */ | |
241 | 0, /* tp_traverse */ | |
242 | 0, /* tp_clear */ | |
243 | 0, /* tp_richcompare */ | |
244 | 0, /* tp_weaklistoffset */ | |
245 | 0, /* tp_iter */ | |
246 | 0, /* tp_iternext */ | |
247 | lazy_string_object_methods, /* tp_methods */ | |
248 | 0, /* tp_members */ | |
249 | lazy_string_object_getset /* tp_getset */ | |
250 | }; |