py-value.c revision 1.1.1.8 1 1.1 christos /* Python interface to values.
2 1.1 christos
3 1.1.1.8 christos Copyright (C) 2008-2023 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "charset.h"
22 1.1 christos #include "value.h"
23 1.1 christos #include "language.h"
24 1.1.1.6 christos #include "target-float.h"
25 1.1 christos #include "valprint.h"
26 1.1 christos #include "infcall.h"
27 1.1 christos #include "expression.h"
28 1.1 christos #include "cp-abi.h"
29 1.1 christos #include "python.h"
30 1.1 christos
31 1.1 christos #include "python-internal.h"
32 1.1 christos
33 1.1 christos /* Even though Python scalar types directly map to host types, we use
34 1.1 christos target types here to remain consistent with the values system in
35 1.1 christos GDB (which uses target arithmetic). */
36 1.1 christos
37 1.1 christos /* Python's integer type corresponds to C's long type. */
38 1.1.1.8 christos #define builtin_type_pyint \
39 1.1.1.8 christos builtin_type (gdbpy_enter::get_gdbarch ())->builtin_long
40 1.1 christos
41 1.1 christos /* Python's float type corresponds to C's double type. */
42 1.1.1.8 christos #define builtin_type_pyfloat \
43 1.1.1.8 christos builtin_type (gdbpy_enter::get_gdbarch ())->builtin_double
44 1.1 christos
45 1.1 christos /* Python's long type corresponds to C's long long type. */
46 1.1.1.8 christos #define builtin_type_pylong \
47 1.1.1.8 christos builtin_type (gdbpy_enter::get_gdbarch ())->builtin_long_long
48 1.1 christos
49 1.1 christos /* Python's long type corresponds to C's long long type. Unsigned version. */
50 1.1 christos #define builtin_type_upylong builtin_type \
51 1.1.1.8 christos (gdbpy_enter::get_gdbarch ())->builtin_unsigned_long_long
52 1.1 christos
53 1.1 christos #define builtin_type_pybool \
54 1.1.1.8 christos language_bool_type (current_language, gdbpy_enter::get_gdbarch ())
55 1.1 christos
56 1.1 christos #define builtin_type_pychar \
57 1.1.1.8 christos language_string_char_type (current_language, gdbpy_enter::get_gdbarch ())
58 1.1 christos
59 1.1.1.8 christos struct value_object {
60 1.1 christos PyObject_HEAD
61 1.1 christos struct value_object *next;
62 1.1 christos struct value_object *prev;
63 1.1 christos struct value *value;
64 1.1 christos PyObject *address;
65 1.1 christos PyObject *type;
66 1.1 christos PyObject *dynamic_type;
67 1.1.1.8 christos };
68 1.1 christos
69 1.1 christos /* List of all values which are currently exposed to Python. It is
70 1.1 christos maintained so that when an objfile is discarded, preserve_values
71 1.1 christos can copy the values' types if needed. */
72 1.1 christos /* This variable is unnecessarily initialized to NULL in order to
73 1.1 christos work around a linker bug on MacOS. */
74 1.1 christos static value_object *values_in_python = NULL;
75 1.1 christos
76 1.1.1.8 christos /* Clear out an old GDB value stored within SELF, and reset the fields to
77 1.1.1.8 christos nullptr. This should be called when a gdb.Value is deallocated, and
78 1.1.1.8 christos also if a gdb.Value is reinitialized with a new value. */
79 1.1.1.8 christos
80 1.1.1.8 christos static void
81 1.1.1.8 christos valpy_clear_value (value_object *self)
82 1.1.1.8 christos {
83 1.1.1.8 christos /* Indicate we are no longer interested in the value object. */
84 1.1.1.8 christos value_decref (self->value);
85 1.1.1.8 christos self->value = nullptr;
86 1.1.1.8 christos
87 1.1.1.8 christos Py_CLEAR (self->address);
88 1.1.1.8 christos Py_CLEAR (self->type);
89 1.1.1.8 christos Py_CLEAR (self->dynamic_type);
90 1.1.1.8 christos }
91 1.1.1.8 christos
92 1.1 christos /* Called by the Python interpreter when deallocating a value object. */
93 1.1 christos static void
94 1.1 christos valpy_dealloc (PyObject *obj)
95 1.1 christos {
96 1.1 christos value_object *self = (value_object *) obj;
97 1.1 christos
98 1.1.1.8 christos /* If SELF failed to initialize correctly then it may not have a value
99 1.1.1.8 christos contained within it. */
100 1.1.1.8 christos if (self->value != nullptr)
101 1.1.1.8 christos {
102 1.1.1.8 christos /* Remove SELF from the global list of values. */
103 1.1.1.8 christos if (self->prev != nullptr)
104 1.1.1.8 christos self->prev->next = self->next;
105 1.1.1.8 christos else
106 1.1.1.8 christos {
107 1.1.1.8 christos gdb_assert (values_in_python == self);
108 1.1.1.8 christos values_in_python = self->next;
109 1.1.1.8 christos }
110 1.1.1.8 christos if (self->next != nullptr)
111 1.1.1.8 christos self->next->prev = self->prev;
112 1.1 christos
113 1.1.1.8 christos /* Release the value object and any cached Python objects. */
114 1.1.1.8 christos valpy_clear_value (self);
115 1.1.1.8 christos }
116 1.1 christos
117 1.1 christos Py_TYPE (self)->tp_free (self);
118 1.1 christos }
119 1.1 christos
120 1.1.1.8 christos /* Helper to push a gdb.Value object on to the global list of values. If
121 1.1.1.8 christos VALUE_OBJ is already on the lit then this does nothing. */
122 1.1.1.8 christos
123 1.1 christos static void
124 1.1 christos note_value (value_object *value_obj)
125 1.1 christos {
126 1.1.1.8 christos if (value_obj->next == nullptr)
127 1.1.1.8 christos {
128 1.1.1.8 christos gdb_assert (value_obj->prev == nullptr);
129 1.1.1.8 christos value_obj->next = values_in_python;
130 1.1.1.8 christos if (value_obj->next != nullptr)
131 1.1.1.8 christos value_obj->next->prev = value_obj;
132 1.1.1.8 christos values_in_python = value_obj;
133 1.1.1.8 christos }
134 1.1 christos }
135 1.1 christos
136 1.1.1.6 christos /* Convert a python object OBJ with type TYPE to a gdb value. The
137 1.1.1.6 christos python object in question must conform to the python buffer
138 1.1.1.6 christos protocol. On success, return the converted value, otherwise
139 1.1.1.6 christos nullptr. */
140 1.1.1.6 christos
141 1.1.1.6 christos static struct value *
142 1.1.1.6 christos convert_buffer_and_type_to_value (PyObject *obj, struct type *type)
143 1.1.1.6 christos {
144 1.1.1.6 christos Py_buffer_up buffer_up;
145 1.1.1.6 christos Py_buffer py_buf;
146 1.1.1.6 christos
147 1.1.1.6 christos if (PyObject_CheckBuffer (obj)
148 1.1.1.6 christos && PyObject_GetBuffer (obj, &py_buf, PyBUF_SIMPLE) == 0)
149 1.1.1.6 christos {
150 1.1.1.6 christos /* Got a buffer, py_buf, out of obj. Cause it to be released
151 1.1.1.8 christos when it goes out of scope. */
152 1.1.1.6 christos buffer_up.reset (&py_buf);
153 1.1.1.6 christos }
154 1.1.1.6 christos else
155 1.1.1.6 christos {
156 1.1.1.6 christos PyErr_SetString (PyExc_TypeError,
157 1.1.1.6 christos _("Object must support the python buffer protocol."));
158 1.1.1.6 christos return nullptr;
159 1.1.1.6 christos }
160 1.1.1.6 christos
161 1.1.1.8 christos if (type->length () > py_buf.len)
162 1.1.1.6 christos {
163 1.1.1.6 christos PyErr_SetString (PyExc_ValueError,
164 1.1.1.6 christos _("Size of type is larger than that of buffer object."));
165 1.1.1.6 christos return nullptr;
166 1.1.1.6 christos }
167 1.1.1.6 christos
168 1.1.1.6 christos return value_from_contents (type, (const gdb_byte *) py_buf.buf);
169 1.1.1.6 christos }
170 1.1.1.6 christos
171 1.1.1.8 christos /* Implement gdb.Value.__init__. */
172 1.1.1.8 christos
173 1.1.1.8 christos static int
174 1.1.1.8 christos valpy_init (PyObject *self, PyObject *args, PyObject *kwds)
175 1.1 christos {
176 1.1.1.6 christos static const char *keywords[] = { "val", "type", NULL };
177 1.1.1.6 christos PyObject *val_obj = nullptr;
178 1.1.1.6 christos PyObject *type_obj = nullptr;
179 1.1.1.6 christos
180 1.1.1.8 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kwds, "O|O", keywords,
181 1.1.1.6 christos &val_obj, &type_obj))
182 1.1.1.8 christos return -1;
183 1.1.1.6 christos
184 1.1.1.6 christos struct type *type = nullptr;
185 1.1.1.8 christos if (type_obj != nullptr && type_obj != Py_None)
186 1.1 christos {
187 1.1.1.6 christos type = type_object_to_type (type_obj);
188 1.1.1.6 christos if (type == nullptr)
189 1.1.1.8 christos {
190 1.1.1.6 christos PyErr_SetString (PyExc_TypeError,
191 1.1.1.6 christos _("type argument must be a gdb.Type."));
192 1.1.1.8 christos return -1;
193 1.1.1.6 christos }
194 1.1 christos }
195 1.1 christos
196 1.1.1.6 christos struct value *value;
197 1.1.1.6 christos if (type == nullptr)
198 1.1.1.6 christos value = convert_value_from_python (val_obj);
199 1.1.1.6 christos else
200 1.1.1.6 christos value = convert_buffer_and_type_to_value (val_obj, type);
201 1.1.1.6 christos if (value == nullptr)
202 1.1 christos {
203 1.1.1.8 christos gdb_assert (PyErr_Occurred ());
204 1.1.1.8 christos return -1;
205 1.1 christos }
206 1.1 christos
207 1.1.1.8 christos /* There might be a previous value here. */
208 1.1.1.8 christos value_object *value_obj = (value_object *) self;
209 1.1.1.8 christos if (value_obj->value != nullptr)
210 1.1.1.8 christos valpy_clear_value (value_obj);
211 1.1.1.8 christos
212 1.1.1.8 christos /* Store the value into this Python object. */
213 1.1.1.6 christos value_obj->value = release_value (value).release ();
214 1.1.1.8 christos
215 1.1.1.8 christos /* Ensure that this gdb.Value is in the set of all gdb.Value objects. If
216 1.1.1.8 christos we are already in the set then this is call does nothing. */
217 1.1 christos note_value (value_obj);
218 1.1 christos
219 1.1.1.8 christos return 0;
220 1.1 christos }
221 1.1 christos
222 1.1 christos /* Iterate over all the Value objects, calling preserve_one_value on
223 1.1 christos each. */
224 1.1 christos void
225 1.1.1.2 christos gdbpy_preserve_values (const struct extension_language_defn *extlang,
226 1.1.1.2 christos struct objfile *objfile, htab_t copied_types)
227 1.1 christos {
228 1.1 christos value_object *iter;
229 1.1 christos
230 1.1 christos for (iter = values_in_python; iter; iter = iter->next)
231 1.1 christos preserve_one_value (iter->value, objfile, copied_types);
232 1.1 christos }
233 1.1 christos
234 1.1 christos /* Given a value of a pointer type, apply the C unary * operator to it. */
235 1.1 christos static PyObject *
236 1.1 christos valpy_dereference (PyObject *self, PyObject *args)
237 1.1 christos {
238 1.1 christos PyObject *result = NULL;
239 1.1 christos
240 1.1.1.7 christos try
241 1.1 christos {
242 1.1 christos struct value *res_val;
243 1.1.1.5 christos scoped_value_mark free_values;
244 1.1 christos
245 1.1 christos res_val = value_ind (((value_object *) self)->value);
246 1.1 christos result = value_to_value_object (res_val);
247 1.1 christos }
248 1.1.1.7 christos catch (const gdb_exception &except)
249 1.1.1.3 christos {
250 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
251 1.1.1.3 christos }
252 1.1 christos
253 1.1 christos return result;
254 1.1 christos }
255 1.1 christos
256 1.1 christos /* Given a value of a pointer type or a reference type, return the value
257 1.1 christos referenced. The difference between this function and valpy_dereference is
258 1.1 christos that the latter applies * unary operator to a value, which need not always
259 1.1 christos result in the value referenced. For example, for a value which is a reference
260 1.1 christos to an 'int' pointer ('int *'), valpy_dereference will result in a value of
261 1.1 christos type 'int' while valpy_referenced_value will result in a value of type
262 1.1 christos 'int *'. */
263 1.1 christos
264 1.1 christos static PyObject *
265 1.1 christos valpy_referenced_value (PyObject *self, PyObject *args)
266 1.1 christos {
267 1.1 christos PyObject *result = NULL;
268 1.1 christos
269 1.1.1.7 christos try
270 1.1 christos {
271 1.1 christos struct value *self_val, *res_val;
272 1.1.1.5 christos scoped_value_mark free_values;
273 1.1 christos
274 1.1 christos self_val = ((value_object *) self)->value;
275 1.1.1.7 christos switch (check_typedef (value_type (self_val))->code ())
276 1.1.1.8 christos {
277 1.1.1.8 christos case TYPE_CODE_PTR:
278 1.1.1.8 christos res_val = value_ind (self_val);
279 1.1.1.8 christos break;
280 1.1.1.8 christos case TYPE_CODE_REF:
281 1.1.1.8 christos case TYPE_CODE_RVALUE_REF:
282 1.1.1.8 christos res_val = coerce_ref (self_val);
283 1.1.1.8 christos break;
284 1.1.1.8 christos default:
285 1.1.1.8 christos error(_("Trying to get the referenced value from a value which is "
286 1.1.1.8 christos "neither a pointer nor a reference."));
287 1.1.1.8 christos }
288 1.1 christos
289 1.1 christos result = value_to_value_object (res_val);
290 1.1 christos }
291 1.1.1.7 christos catch (const gdb_exception &except)
292 1.1.1.3 christos {
293 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
294 1.1.1.3 christos }
295 1.1.1.3 christos
296 1.1.1.3 christos return result;
297 1.1.1.3 christos }
298 1.1.1.3 christos
299 1.1.1.3 christos /* Return a value which is a reference to the value. */
300 1.1.1.3 christos
301 1.1.1.3 christos static PyObject *
302 1.1.1.5 christos valpy_reference_value (PyObject *self, PyObject *args, enum type_code refcode)
303 1.1.1.3 christos {
304 1.1.1.3 christos PyObject *result = NULL;
305 1.1.1.3 christos
306 1.1.1.7 christos try
307 1.1.1.3 christos {
308 1.1.1.3 christos struct value *self_val;
309 1.1.1.5 christos scoped_value_mark free_values;
310 1.1.1.3 christos
311 1.1.1.3 christos self_val = ((value_object *) self)->value;
312 1.1.1.5 christos result = value_to_value_object (value_ref (self_val, refcode));
313 1.1.1.3 christos }
314 1.1.1.7 christos catch (const gdb_exception &except)
315 1.1.1.3 christos {
316 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
317 1.1.1.3 christos }
318 1.1.1.3 christos
319 1.1.1.3 christos return result;
320 1.1.1.3 christos }
321 1.1.1.3 christos
322 1.1.1.5 christos static PyObject *
323 1.1.1.5 christos valpy_lvalue_reference_value (PyObject *self, PyObject *args)
324 1.1.1.5 christos {
325 1.1.1.5 christos return valpy_reference_value (self, args, TYPE_CODE_REF);
326 1.1.1.5 christos }
327 1.1.1.5 christos
328 1.1.1.5 christos static PyObject *
329 1.1.1.5 christos valpy_rvalue_reference_value (PyObject *self, PyObject *args)
330 1.1.1.5 christos {
331 1.1.1.5 christos return valpy_reference_value (self, args, TYPE_CODE_RVALUE_REF);
332 1.1.1.5 christos }
333 1.1.1.5 christos
334 1.1.1.3 christos /* Return a "const" qualified version of the value. */
335 1.1.1.3 christos
336 1.1.1.3 christos static PyObject *
337 1.1.1.3 christos valpy_const_value (PyObject *self, PyObject *args)
338 1.1.1.3 christos {
339 1.1.1.3 christos PyObject *result = NULL;
340 1.1.1.3 christos
341 1.1.1.7 christos try
342 1.1.1.3 christos {
343 1.1.1.3 christos struct value *self_val, *res_val;
344 1.1.1.5 christos scoped_value_mark free_values;
345 1.1.1.3 christos
346 1.1.1.3 christos self_val = ((value_object *) self)->value;
347 1.1.1.3 christos res_val = make_cv_value (1, 0, self_val);
348 1.1.1.3 christos result = value_to_value_object (res_val);
349 1.1.1.3 christos }
350 1.1.1.7 christos catch (const gdb_exception &except)
351 1.1.1.3 christos {
352 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
353 1.1.1.3 christos }
354 1.1 christos
355 1.1 christos return result;
356 1.1 christos }
357 1.1 christos
358 1.1 christos /* Return "&value". */
359 1.1 christos static PyObject *
360 1.1 christos valpy_get_address (PyObject *self, void *closure)
361 1.1 christos {
362 1.1 christos value_object *val_obj = (value_object *) self;
363 1.1 christos
364 1.1 christos if (!val_obj->address)
365 1.1 christos {
366 1.1.1.7 christos try
367 1.1 christos {
368 1.1 christos struct value *res_val;
369 1.1.1.5 christos scoped_value_mark free_values;
370 1.1 christos
371 1.1 christos res_val = value_addr (val_obj->value);
372 1.1 christos val_obj->address = value_to_value_object (res_val);
373 1.1 christos }
374 1.1.1.7 christos catch (const gdb_exception &except)
375 1.1 christos {
376 1.1 christos val_obj->address = Py_None;
377 1.1 christos Py_INCREF (Py_None);
378 1.1 christos }
379 1.1 christos }
380 1.1 christos
381 1.1 christos Py_XINCREF (val_obj->address);
382 1.1 christos
383 1.1 christos return val_obj->address;
384 1.1 christos }
385 1.1 christos
386 1.1 christos /* Return type of the value. */
387 1.1 christos static PyObject *
388 1.1 christos valpy_get_type (PyObject *self, void *closure)
389 1.1 christos {
390 1.1 christos value_object *obj = (value_object *) self;
391 1.1 christos
392 1.1 christos if (!obj->type)
393 1.1 christos {
394 1.1 christos obj->type = type_to_type_object (value_type (obj->value));
395 1.1 christos if (!obj->type)
396 1.1 christos return NULL;
397 1.1 christos }
398 1.1 christos Py_INCREF (obj->type);
399 1.1 christos return obj->type;
400 1.1 christos }
401 1.1 christos
402 1.1 christos /* Return dynamic type of the value. */
403 1.1 christos
404 1.1 christos static PyObject *
405 1.1 christos valpy_get_dynamic_type (PyObject *self, void *closure)
406 1.1 christos {
407 1.1 christos value_object *obj = (value_object *) self;
408 1.1 christos struct type *type = NULL;
409 1.1 christos
410 1.1 christos if (obj->dynamic_type != NULL)
411 1.1 christos {
412 1.1 christos Py_INCREF (obj->dynamic_type);
413 1.1 christos return obj->dynamic_type;
414 1.1 christos }
415 1.1 christos
416 1.1.1.7 christos try
417 1.1 christos {
418 1.1 christos struct value *val = obj->value;
419 1.1.1.5 christos scoped_value_mark free_values;
420 1.1 christos
421 1.1 christos type = value_type (val);
422 1.1.1.4 christos type = check_typedef (type);
423 1.1 christos
424 1.1.1.8 christos if (type->is_pointer_or_reference ()
425 1.1.1.8 christos && (type->target_type ()->code () == TYPE_CODE_STRUCT))
426 1.1 christos {
427 1.1 christos struct value *target;
428 1.1.1.7 christos int was_pointer = type->code () == TYPE_CODE_PTR;
429 1.1 christos
430 1.1.1.2 christos if (was_pointer)
431 1.1.1.2 christos target = value_ind (val);
432 1.1.1.2 christos else
433 1.1.1.2 christos target = coerce_ref (val);
434 1.1 christos type = value_rtti_type (target, NULL, NULL, NULL);
435 1.1 christos
436 1.1 christos if (type)
437 1.1 christos {
438 1.1 christos if (was_pointer)
439 1.1 christos type = lookup_pointer_type (type);
440 1.1 christos else
441 1.1.1.5 christos type = lookup_lvalue_reference_type (type);
442 1.1 christos }
443 1.1 christos }
444 1.1.1.7 christos else if (type->code () == TYPE_CODE_STRUCT)
445 1.1 christos type = value_rtti_type (val, NULL, NULL, NULL);
446 1.1 christos else
447 1.1 christos {
448 1.1 christos /* Re-use object's static type. */
449 1.1 christos type = NULL;
450 1.1 christos }
451 1.1 christos }
452 1.1.1.7 christos catch (const gdb_exception &except)
453 1.1.1.3 christos {
454 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
455 1.1.1.3 christos }
456 1.1 christos
457 1.1 christos if (type == NULL)
458 1.1 christos obj->dynamic_type = valpy_get_type (self, NULL);
459 1.1 christos else
460 1.1 christos obj->dynamic_type = type_to_type_object (type);
461 1.1 christos
462 1.1 christos Py_XINCREF (obj->dynamic_type);
463 1.1 christos return obj->dynamic_type;
464 1.1 christos }
465 1.1 christos
466 1.1 christos /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
467 1.1 christos string. Return a PyObject representing a lazy_string_object type.
468 1.1 christos A lazy string is a pointer to a string with an optional encoding and
469 1.1 christos length. If ENCODING is not given, encoding is set to None. If an
470 1.1 christos ENCODING is provided the encoding parameter is set to ENCODING, but
471 1.1.1.5 christos the string is not encoded.
472 1.1.1.5 christos If LENGTH is provided then the length parameter is set to LENGTH.
473 1.1.1.5 christos Otherwise if the value is an array of known length then the array's length
474 1.1.1.5 christos is used. Otherwise the length will be set to -1 (meaning first null of
475 1.1.1.5 christos appropriate with).
476 1.1.1.5 christos
477 1.1.1.5 christos Note: In order to not break any existing uses this allows creating
478 1.1.1.5 christos lazy strings from anything. PR 20769. E.g.,
479 1.1.1.5 christos gdb.parse_and_eval("my_int_variable").lazy_string().
480 1.1.1.5 christos "It's easier to relax restrictions than it is to impose them after the
481 1.1.1.5 christos fact." So we should be flagging any unintended uses as errors, but it's
482 1.1.1.5 christos perhaps too late for that. */
483 1.1.1.5 christos
484 1.1 christos static PyObject *
485 1.1 christos valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
486 1.1 christos {
487 1.1 christos gdb_py_longest length = -1;
488 1.1 christos struct value *value = ((value_object *) self)->value;
489 1.1 christos const char *user_encoding = NULL;
490 1.1.1.5 christos static const char *keywords[] = { "encoding", "length", NULL };
491 1.1 christos PyObject *str_obj = NULL;
492 1.1 christos
493 1.1.1.5 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG,
494 1.1.1.5 christos keywords, &user_encoding, &length))
495 1.1 christos return NULL;
496 1.1 christos
497 1.1.1.5 christos if (length < -1)
498 1.1.1.5 christos {
499 1.1.1.5 christos PyErr_SetString (PyExc_ValueError, _("Invalid length."));
500 1.1.1.5 christos return NULL;
501 1.1.1.5 christos }
502 1.1.1.5 christos
503 1.1.1.7 christos try
504 1.1 christos {
505 1.1.1.5 christos scoped_value_mark free_values;
506 1.1.1.5 christos struct type *type, *realtype;
507 1.1.1.5 christos CORE_ADDR addr;
508 1.1 christos
509 1.1.1.5 christos type = value_type (value);
510 1.1.1.5 christos realtype = check_typedef (type);
511 1.1.1.5 christos
512 1.1.1.7 christos switch (realtype->code ())
513 1.1.1.5 christos {
514 1.1.1.5 christos case TYPE_CODE_ARRAY:
515 1.1.1.5 christos {
516 1.1.1.5 christos LONGEST array_length = -1;
517 1.1.1.5 christos LONGEST low_bound, high_bound;
518 1.1 christos
519 1.1.1.5 christos /* PR 20786: There's no way to specify an array of length zero.
520 1.1.1.5 christos Record a length of [0,-1] which is how Ada does it. Anything
521 1.1.1.5 christos we do is broken, but this one possible solution. */
522 1.1.1.5 christos if (get_array_bounds (realtype, &low_bound, &high_bound))
523 1.1.1.5 christos array_length = high_bound - low_bound + 1;
524 1.1.1.5 christos if (length == -1)
525 1.1.1.5 christos length = array_length;
526 1.1.1.5 christos else if (array_length == -1)
527 1.1.1.5 christos {
528 1.1.1.8 christos type = lookup_array_range_type (realtype->target_type (),
529 1.1.1.5 christos 0, length - 1);
530 1.1.1.5 christos }
531 1.1.1.5 christos else if (length != array_length)
532 1.1.1.5 christos {
533 1.1.1.5 christos /* We need to create a new array type with the
534 1.1.1.5 christos specified length. */
535 1.1.1.5 christos if (length > array_length)
536 1.1.1.5 christos error (_("Length is larger than array size."));
537 1.1.1.8 christos type = lookup_array_range_type (realtype->target_type (),
538 1.1.1.5 christos low_bound,
539 1.1.1.5 christos low_bound + length - 1);
540 1.1.1.5 christos }
541 1.1.1.5 christos addr = value_address (value);
542 1.1.1.5 christos break;
543 1.1.1.5 christos }
544 1.1.1.5 christos case TYPE_CODE_PTR:
545 1.1.1.5 christos /* If a length is specified we defer creating an array of the
546 1.1.1.5 christos specified width until we need to. */
547 1.1.1.5 christos addr = value_as_address (value);
548 1.1.1.5 christos break;
549 1.1.1.5 christos default:
550 1.1.1.5 christos /* Should flag an error here. PR 20769. */
551 1.1.1.5 christos addr = value_address (value);
552 1.1.1.5 christos break;
553 1.1.1.5 christos }
554 1.1 christos
555 1.1.1.5 christos str_obj = gdbpy_create_lazy_string_object (addr, length, user_encoding,
556 1.1.1.5 christos type);
557 1.1 christos }
558 1.1.1.7 christos catch (const gdb_exception &except)
559 1.1.1.3 christos {
560 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
561 1.1.1.3 christos }
562 1.1 christos
563 1.1 christos return str_obj;
564 1.1 christos }
565 1.1 christos
566 1.1 christos /* Implementation of gdb.Value.string ([encoding] [, errors]
567 1.1 christos [, length]) -> string. Return Unicode string with value contents.
568 1.1 christos If ENCODING is not given, the string is assumed to be encoded in
569 1.1 christos the target's charset. If LENGTH is provided, only fetch string to
570 1.1 christos the length provided. */
571 1.1 christos
572 1.1 christos static PyObject *
573 1.1 christos valpy_string (PyObject *self, PyObject *args, PyObject *kw)
574 1.1 christos {
575 1.1 christos int length = -1;
576 1.1.1.6 christos gdb::unique_xmalloc_ptr<gdb_byte> buffer;
577 1.1 christos struct value *value = ((value_object *) self)->value;
578 1.1 christos const char *encoding = NULL;
579 1.1 christos const char *errors = NULL;
580 1.1 christos const char *user_encoding = NULL;
581 1.1 christos const char *la_encoding = NULL;
582 1.1 christos struct type *char_type;
583 1.1.1.5 christos static const char *keywords[] = { "encoding", "errors", "length", NULL };
584 1.1 christos
585 1.1.1.5 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
586 1.1.1.5 christos &user_encoding, &errors, &length))
587 1.1 christos return NULL;
588 1.1 christos
589 1.1.1.7 christos try
590 1.1 christos {
591 1.1.1.7 christos c_get_string (value, &buffer, &length, &char_type, &la_encoding);
592 1.1 christos }
593 1.1.1.7 christos catch (const gdb_exception &except)
594 1.1.1.3 christos {
595 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
596 1.1.1.3 christos }
597 1.1 christos
598 1.1 christos encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
599 1.1.1.6 christos return PyUnicode_Decode ((const char *) buffer.get (),
600 1.1.1.8 christos length * char_type->length (),
601 1.1.1.6 christos encoding, errors);
602 1.1 christos }
603 1.1 christos
604 1.1.1.7 christos /* Given a Python object, copy its truth value to a C bool (the value
605 1.1.1.7 christos pointed by dest).
606 1.1.1.7 christos If src_obj is NULL, then *dest is not modified.
607 1.1.1.7 christos
608 1.1.1.7 christos Return true in case of success (including src_obj being NULL), false
609 1.1.1.7 christos in case of error. */
610 1.1.1.7 christos
611 1.1.1.7 christos static bool
612 1.1.1.7 christos copy_py_bool_obj (bool *dest, PyObject *src_obj)
613 1.1.1.7 christos {
614 1.1.1.7 christos if (src_obj)
615 1.1.1.7 christos {
616 1.1.1.7 christos int cmp = PyObject_IsTrue (src_obj);
617 1.1.1.7 christos if (cmp < 0)
618 1.1.1.7 christos return false;
619 1.1.1.7 christos *dest = cmp;
620 1.1.1.7 christos }
621 1.1.1.7 christos
622 1.1.1.7 christos return true;
623 1.1.1.7 christos }
624 1.1.1.7 christos
625 1.1.1.7 christos /* Implementation of gdb.Value.format_string (...) -> string.
626 1.1.1.7 christos Return Unicode string with value contents formatted using the
627 1.1.1.7 christos keyword-only arguments. */
628 1.1.1.7 christos
629 1.1.1.7 christos static PyObject *
630 1.1.1.7 christos valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
631 1.1.1.7 christos {
632 1.1.1.7 christos static const char *keywords[] =
633 1.1.1.7 christos {
634 1.1.1.7 christos /* Basic C/C++ options. */
635 1.1.1.7 christos "raw", /* See the /r option to print. */
636 1.1.1.7 christos "pretty_arrays", /* See set print array on|off. */
637 1.1.1.7 christos "pretty_structs", /* See set print pretty on|off. */
638 1.1.1.7 christos "array_indexes", /* See set print array-indexes on|off. */
639 1.1.1.7 christos "symbols", /* See set print symbol on|off. */
640 1.1.1.7 christos "unions", /* See set print union on|off. */
641 1.1.1.8 christos "address", /* See set print address on|off. */
642 1.1.1.8 christos "styling", /* Should we apply styling. */
643 1.1.1.8 christos "nibbles", /* See set print nibbles on|off. */
644 1.1.1.8 christos "summary", /* Summary mode for non-scalars. */
645 1.1.1.7 christos /* C++ options. */
646 1.1.1.7 christos "deref_refs", /* No corresponding setting. */
647 1.1.1.7 christos "actual_objects", /* See set print object on|off. */
648 1.1.1.7 christos "static_members", /* See set print static-members on|off. */
649 1.1.1.7 christos /* C non-bool options. */
650 1.1.1.7 christos "max_elements", /* See set print elements N. */
651 1.1.1.7 christos "max_depth", /* See set print max-depth N. */
652 1.1.1.7 christos "repeat_threshold", /* See set print repeats. */
653 1.1.1.7 christos "format", /* The format passed to the print command. */
654 1.1.1.7 christos NULL
655 1.1.1.7 christos };
656 1.1.1.7 christos
657 1.1.1.7 christos /* This function has too many arguments to be useful as positionals, so
658 1.1.1.7 christos the user should specify them all as keyword arguments.
659 1.1.1.7 christos Python 3.3 and later have a way to specify it (both in C and Python
660 1.1.1.7 christos itself), but we could be compiled with older versions, so we just
661 1.1.1.7 christos check that the args tuple is empty. */
662 1.1.1.7 christos Py_ssize_t positional_count = PyObject_Length (args);
663 1.1.1.7 christos if (positional_count < 0)
664 1.1.1.7 christos return NULL;
665 1.1.1.7 christos else if (positional_count > 0)
666 1.1.1.7 christos {
667 1.1.1.7 christos /* This matches the error message that Python 3.3 raises when
668 1.1.1.7 christos passing positionals to functions expecting keyword-only
669 1.1.1.7 christos arguments. */
670 1.1.1.7 christos PyErr_Format (PyExc_TypeError,
671 1.1.1.7 christos "format_string() takes 0 positional arguments but %zu were given",
672 1.1.1.7 christos positional_count);
673 1.1.1.7 christos return NULL;
674 1.1.1.7 christos }
675 1.1.1.7 christos
676 1.1.1.7 christos struct value_print_options opts;
677 1.1.1.8 christos gdbpy_get_print_options (&opts);
678 1.1.1.7 christos opts.deref_ref = 0;
679 1.1.1.7 christos
680 1.1.1.7 christos /* We need objects for booleans as the "p" flag for bools is new in
681 1.1.1.7 christos Python 3.3. */
682 1.1.1.7 christos PyObject *raw_obj = NULL;
683 1.1.1.7 christos PyObject *pretty_arrays_obj = NULL;
684 1.1.1.7 christos PyObject *pretty_structs_obj = NULL;
685 1.1.1.7 christos PyObject *array_indexes_obj = NULL;
686 1.1.1.7 christos PyObject *symbols_obj = NULL;
687 1.1.1.7 christos PyObject *unions_obj = NULL;
688 1.1.1.8 christos PyObject *address_obj = NULL;
689 1.1.1.8 christos PyObject *styling_obj = Py_False;
690 1.1.1.8 christos PyObject *nibbles_obj = NULL;
691 1.1.1.7 christos PyObject *deref_refs_obj = NULL;
692 1.1.1.7 christos PyObject *actual_objects_obj = NULL;
693 1.1.1.7 christos PyObject *static_members_obj = NULL;
694 1.1.1.8 christos PyObject *summary_obj = NULL;
695 1.1.1.7 christos char *format = NULL;
696 1.1.1.7 christos if (!gdb_PyArg_ParseTupleAndKeywords (args,
697 1.1.1.7 christos kw,
698 1.1.1.8 christos "|O!O!O!O!O!O!O!O!O!O!O!O!O!IIIs",
699 1.1.1.7 christos keywords,
700 1.1.1.7 christos &PyBool_Type, &raw_obj,
701 1.1.1.7 christos &PyBool_Type, &pretty_arrays_obj,
702 1.1.1.7 christos &PyBool_Type, &pretty_structs_obj,
703 1.1.1.7 christos &PyBool_Type, &array_indexes_obj,
704 1.1.1.7 christos &PyBool_Type, &symbols_obj,
705 1.1.1.7 christos &PyBool_Type, &unions_obj,
706 1.1.1.8 christos &PyBool_Type, &address_obj,
707 1.1.1.8 christos &PyBool_Type, &styling_obj,
708 1.1.1.8 christos &PyBool_Type, &nibbles_obj,
709 1.1.1.8 christos &PyBool_Type, &summary_obj,
710 1.1.1.7 christos &PyBool_Type, &deref_refs_obj,
711 1.1.1.7 christos &PyBool_Type, &actual_objects_obj,
712 1.1.1.7 christos &PyBool_Type, &static_members_obj,
713 1.1.1.7 christos &opts.print_max,
714 1.1.1.7 christos &opts.max_depth,
715 1.1.1.7 christos &opts.repeat_count_threshold,
716 1.1.1.7 christos &format))
717 1.1.1.7 christos return NULL;
718 1.1.1.7 christos
719 1.1.1.7 christos /* Set boolean arguments. */
720 1.1.1.7 christos if (!copy_py_bool_obj (&opts.raw, raw_obj))
721 1.1.1.7 christos return NULL;
722 1.1.1.7 christos if (!copy_py_bool_obj (&opts.prettyformat_arrays, pretty_arrays_obj))
723 1.1.1.7 christos return NULL;
724 1.1.1.7 christos if (!copy_py_bool_obj (&opts.prettyformat_structs, pretty_structs_obj))
725 1.1.1.7 christos return NULL;
726 1.1.1.7 christos if (!copy_py_bool_obj (&opts.print_array_indexes, array_indexes_obj))
727 1.1.1.7 christos return NULL;
728 1.1.1.7 christos if (!copy_py_bool_obj (&opts.symbol_print, symbols_obj))
729 1.1.1.7 christos return NULL;
730 1.1.1.7 christos if (!copy_py_bool_obj (&opts.unionprint, unions_obj))
731 1.1.1.7 christos return NULL;
732 1.1.1.8 christos if (!copy_py_bool_obj (&opts.addressprint, address_obj))
733 1.1.1.8 christos return NULL;
734 1.1.1.8 christos if (!copy_py_bool_obj (&opts.nibblesprint, nibbles_obj))
735 1.1.1.8 christos return NULL;
736 1.1.1.7 christos if (!copy_py_bool_obj (&opts.deref_ref, deref_refs_obj))
737 1.1.1.7 christos return NULL;
738 1.1.1.7 christos if (!copy_py_bool_obj (&opts.objectprint, actual_objects_obj))
739 1.1.1.7 christos return NULL;
740 1.1.1.7 christos if (!copy_py_bool_obj (&opts.static_field_print, static_members_obj))
741 1.1.1.7 christos return NULL;
742 1.1.1.8 christos if (!copy_py_bool_obj (&opts.summary, summary_obj))
743 1.1.1.8 christos return nullptr;
744 1.1.1.7 christos
745 1.1.1.7 christos /* Numeric arguments for which 0 means unlimited (which we represent as
746 1.1.1.7 christos UINT_MAX). Note that the max-depth numeric argument uses -1 as
747 1.1.1.7 christos unlimited, and 0 is a valid choice. */
748 1.1.1.7 christos if (opts.print_max == 0)
749 1.1.1.7 christos opts.print_max = UINT_MAX;
750 1.1.1.7 christos if (opts.repeat_count_threshold == 0)
751 1.1.1.7 christos opts.repeat_count_threshold = UINT_MAX;
752 1.1.1.7 christos
753 1.1.1.7 christos /* Other arguments. */
754 1.1.1.7 christos if (format != NULL)
755 1.1.1.7 christos {
756 1.1.1.7 christos if (strlen (format) == 1)
757 1.1.1.7 christos opts.format = format[0];
758 1.1.1.7 christos else
759 1.1.1.7 christos {
760 1.1.1.7 christos /* Mimic the message on standard Python ones for similar
761 1.1.1.7 christos errors. */
762 1.1.1.7 christos PyErr_SetString (PyExc_ValueError,
763 1.1.1.7 christos "a single character is required");
764 1.1.1.7 christos return NULL;
765 1.1.1.7 christos }
766 1.1.1.7 christos }
767 1.1.1.7 christos
768 1.1.1.8 christos string_file stb (PyObject_IsTrue (styling_obj));
769 1.1.1.7 christos
770 1.1.1.7 christos try
771 1.1.1.7 christos {
772 1.1.1.7 christos common_val_print (((value_object *) self)->value, &stb, 0,
773 1.1.1.8 christos &opts, current_language);
774 1.1.1.7 christos }
775 1.1.1.7 christos catch (const gdb_exception &except)
776 1.1.1.7 christos {
777 1.1.1.7 christos GDB_PY_HANDLE_EXCEPTION (except);
778 1.1.1.7 christos }
779 1.1.1.7 christos
780 1.1.1.7 christos return PyUnicode_Decode (stb.c_str (), stb.size (), host_charset (), NULL);
781 1.1.1.7 christos }
782 1.1.1.7 christos
783 1.1 christos /* A helper function that implements the various cast operators. */
784 1.1 christos
785 1.1 christos static PyObject *
786 1.1 christos valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op)
787 1.1 christos {
788 1.1 christos PyObject *type_obj, *result = NULL;
789 1.1 christos struct type *type;
790 1.1 christos
791 1.1 christos if (! PyArg_ParseTuple (args, "O", &type_obj))
792 1.1 christos return NULL;
793 1.1 christos
794 1.1 christos type = type_object_to_type (type_obj);
795 1.1 christos if (! type)
796 1.1 christos {
797 1.1 christos PyErr_SetString (PyExc_RuntimeError,
798 1.1 christos _("Argument must be a type."));
799 1.1 christos return NULL;
800 1.1 christos }
801 1.1 christos
802 1.1.1.7 christos try
803 1.1 christos {
804 1.1 christos struct value *val = ((value_object *) self)->value;
805 1.1 christos struct value *res_val;
806 1.1.1.5 christos scoped_value_mark free_values;
807 1.1 christos
808 1.1 christos if (op == UNOP_DYNAMIC_CAST)
809 1.1 christos res_val = value_dynamic_cast (type, val);
810 1.1 christos else if (op == UNOP_REINTERPRET_CAST)
811 1.1 christos res_val = value_reinterpret_cast (type, val);
812 1.1 christos else
813 1.1 christos {
814 1.1 christos gdb_assert (op == UNOP_CAST);
815 1.1 christos res_val = value_cast (type, val);
816 1.1 christos }
817 1.1 christos
818 1.1 christos result = value_to_value_object (res_val);
819 1.1 christos }
820 1.1.1.7 christos catch (const gdb_exception &except)
821 1.1.1.3 christos {
822 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
823 1.1.1.3 christos }
824 1.1 christos
825 1.1 christos return result;
826 1.1 christos }
827 1.1 christos
828 1.1 christos /* Implementation of the "cast" method. */
829 1.1 christos
830 1.1 christos static PyObject *
831 1.1 christos valpy_cast (PyObject *self, PyObject *args)
832 1.1 christos {
833 1.1 christos return valpy_do_cast (self, args, UNOP_CAST);
834 1.1 christos }
835 1.1 christos
836 1.1 christos /* Implementation of the "dynamic_cast" method. */
837 1.1 christos
838 1.1 christos static PyObject *
839 1.1 christos valpy_dynamic_cast (PyObject *self, PyObject *args)
840 1.1 christos {
841 1.1 christos return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST);
842 1.1 christos }
843 1.1 christos
844 1.1 christos /* Implementation of the "reinterpret_cast" method. */
845 1.1 christos
846 1.1 christos static PyObject *
847 1.1 christos valpy_reinterpret_cast (PyObject *self, PyObject *args)
848 1.1 christos {
849 1.1 christos return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
850 1.1 christos }
851 1.1 christos
852 1.1 christos static Py_ssize_t
853 1.1 christos valpy_length (PyObject *self)
854 1.1 christos {
855 1.1 christos /* We don't support getting the number of elements in a struct / class. */
856 1.1 christos PyErr_SetString (PyExc_NotImplementedError,
857 1.1 christos _("Invalid operation on gdb.Value."));
858 1.1 christos return -1;
859 1.1 christos }
860 1.1 christos
861 1.1 christos /* Return 1 if the gdb.Field object FIELD is present in the value V.
862 1.1 christos Returns 0 otherwise. If any Python error occurs, -1 is returned. */
863 1.1 christos
864 1.1 christos static int
865 1.1 christos value_has_field (struct value *v, PyObject *field)
866 1.1 christos {
867 1.1 christos struct type *parent_type, *val_type;
868 1.1 christos enum type_code type_code;
869 1.1.1.5 christos gdbpy_ref<> type_object (PyObject_GetAttrString (field, "parent_type"));
870 1.1 christos int has_field = 0;
871 1.1 christos
872 1.1 christos if (type_object == NULL)
873 1.1 christos return -1;
874 1.1 christos
875 1.1.1.5 christos parent_type = type_object_to_type (type_object.get ());
876 1.1 christos if (parent_type == NULL)
877 1.1 christos {
878 1.1 christos PyErr_SetString (PyExc_TypeError,
879 1.1 christos _("'parent_type' attribute of gdb.Field object is not a"
880 1.1 christos "gdb.Type object."));
881 1.1 christos return -1;
882 1.1 christos }
883 1.1 christos
884 1.1.1.7 christos try
885 1.1 christos {
886 1.1 christos val_type = value_type (v);
887 1.1 christos val_type = check_typedef (val_type);
888 1.1.1.8 christos if (val_type->is_pointer_or_reference ())
889 1.1.1.8 christos val_type = check_typedef (val_type->target_type ());
890 1.1 christos
891 1.1.1.7 christos type_code = val_type->code ();
892 1.1 christos if ((type_code == TYPE_CODE_STRUCT || type_code == TYPE_CODE_UNION)
893 1.1 christos && types_equal (val_type, parent_type))
894 1.1 christos has_field = 1;
895 1.1 christos else
896 1.1 christos has_field = 0;
897 1.1 christos }
898 1.1.1.7 christos catch (const gdb_exception &except)
899 1.1.1.3 christos {
900 1.1.1.3 christos GDB_PY_SET_HANDLE_EXCEPTION (except);
901 1.1.1.3 christos }
902 1.1 christos
903 1.1 christos return has_field;
904 1.1 christos }
905 1.1 christos
906 1.1 christos /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
907 1.1 christos Returns 1 if the flag value is true, 0 if it is false, and -1 if
908 1.1 christos a Python error occurs. */
909 1.1 christos
910 1.1 christos static int
911 1.1 christos get_field_flag (PyObject *field, const char *flag_name)
912 1.1 christos {
913 1.1.1.5 christos gdbpy_ref<> flag_object (PyObject_GetAttrString (field, flag_name));
914 1.1 christos
915 1.1 christos if (flag_object == NULL)
916 1.1 christos return -1;
917 1.1 christos
918 1.1.1.5 christos return PyObject_IsTrue (flag_object.get ());
919 1.1 christos }
920 1.1 christos
921 1.1 christos /* Return the "type" attribute of a gdb.Field object.
922 1.1 christos Returns NULL on error, with a Python exception set. */
923 1.1 christos
924 1.1 christos static struct type *
925 1.1 christos get_field_type (PyObject *field)
926 1.1 christos {
927 1.1.1.5 christos gdbpy_ref<> ftype_obj (PyObject_GetAttrString (field, "type"));
928 1.1 christos struct type *ftype;
929 1.1 christos
930 1.1 christos if (ftype_obj == NULL)
931 1.1 christos return NULL;
932 1.1.1.5 christos ftype = type_object_to_type (ftype_obj.get ());
933 1.1 christos if (ftype == NULL)
934 1.1 christos PyErr_SetString (PyExc_TypeError,
935 1.1 christos _("'type' attribute of gdb.Field object is not a "
936 1.1 christos "gdb.Type object."));
937 1.1 christos
938 1.1 christos return ftype;
939 1.1 christos }
940 1.1 christos
941 1.1 christos /* Given string name or a gdb.Field object corresponding to an element inside
942 1.1 christos a structure, return its value object. Returns NULL on error, with a python
943 1.1 christos exception set. */
944 1.1 christos
945 1.1 christos static PyObject *
946 1.1 christos valpy_getitem (PyObject *self, PyObject *key)
947 1.1 christos {
948 1.1.1.7 christos struct gdb_exception except;
949 1.1 christos value_object *self_value = (value_object *) self;
950 1.1.1.5 christos gdb::unique_xmalloc_ptr<char> field;
951 1.1 christos struct type *base_class_type = NULL, *field_type = NULL;
952 1.1 christos long bitpos = -1;
953 1.1 christos PyObject *result = NULL;
954 1.1 christos
955 1.1 christos if (gdbpy_is_string (key))
956 1.1 christos {
957 1.1 christos field = python_string_to_host_string (key);
958 1.1 christos if (field == NULL)
959 1.1 christos return NULL;
960 1.1 christos }
961 1.1 christos else if (gdbpy_is_field (key))
962 1.1 christos {
963 1.1 christos int is_base_class, valid_field;
964 1.1 christos
965 1.1 christos valid_field = value_has_field (self_value->value, key);
966 1.1 christos if (valid_field < 0)
967 1.1 christos return NULL;
968 1.1 christos else if (valid_field == 0)
969 1.1 christos {
970 1.1 christos PyErr_SetString (PyExc_TypeError,
971 1.1 christos _("Invalid lookup for a field not contained in "
972 1.1 christos "the value."));
973 1.1 christos
974 1.1 christos return NULL;
975 1.1 christos }
976 1.1 christos
977 1.1 christos is_base_class = get_field_flag (key, "is_base_class");
978 1.1 christos if (is_base_class < 0)
979 1.1 christos return NULL;
980 1.1 christos else if (is_base_class > 0)
981 1.1 christos {
982 1.1 christos base_class_type = get_field_type (key);
983 1.1 christos if (base_class_type == NULL)
984 1.1 christos return NULL;
985 1.1 christos }
986 1.1 christos else
987 1.1 christos {
988 1.1.1.5 christos gdbpy_ref<> name_obj (PyObject_GetAttrString (key, "name"));
989 1.1 christos
990 1.1 christos if (name_obj == NULL)
991 1.1 christos return NULL;
992 1.1 christos
993 1.1 christos if (name_obj != Py_None)
994 1.1 christos {
995 1.1.1.5 christos field = python_string_to_host_string (name_obj.get ());
996 1.1 christos if (field == NULL)
997 1.1 christos return NULL;
998 1.1 christos }
999 1.1 christos else
1000 1.1 christos {
1001 1.1 christos if (!PyObject_HasAttrString (key, "bitpos"))
1002 1.1 christos {
1003 1.1 christos PyErr_SetString (PyExc_AttributeError,
1004 1.1 christos _("gdb.Field object has no name and no "
1005 1.1.1.8 christos "'bitpos' attribute."));
1006 1.1 christos
1007 1.1 christos return NULL;
1008 1.1 christos }
1009 1.1.1.5 christos gdbpy_ref<> bitpos_obj (PyObject_GetAttrString (key, "bitpos"));
1010 1.1 christos if (bitpos_obj == NULL)
1011 1.1 christos return NULL;
1012 1.1.1.5 christos if (!gdb_py_int_as_long (bitpos_obj.get (), &bitpos))
1013 1.1 christos return NULL;
1014 1.1 christos
1015 1.1 christos field_type = get_field_type (key);
1016 1.1 christos if (field_type == NULL)
1017 1.1 christos return NULL;
1018 1.1 christos }
1019 1.1 christos }
1020 1.1 christos }
1021 1.1 christos
1022 1.1.1.7 christos try
1023 1.1 christos {
1024 1.1 christos struct value *tmp = self_value->value;
1025 1.1 christos struct value *res_val = NULL;
1026 1.1.1.5 christos scoped_value_mark free_values;
1027 1.1 christos
1028 1.1 christos if (field)
1029 1.1.1.8 christos res_val = value_struct_elt (&tmp, {}, field.get (), NULL,
1030 1.1.1.5 christos "struct/class/union");
1031 1.1 christos else if (bitpos >= 0)
1032 1.1 christos res_val = value_struct_elt_bitpos (&tmp, bitpos, field_type,
1033 1.1 christos "struct/class/union");
1034 1.1 christos else if (base_class_type != NULL)
1035 1.1 christos {
1036 1.1 christos struct type *val_type;
1037 1.1 christos
1038 1.1 christos val_type = check_typedef (value_type (tmp));
1039 1.1.1.7 christos if (val_type->code () == TYPE_CODE_PTR)
1040 1.1 christos res_val = value_cast (lookup_pointer_type (base_class_type), tmp);
1041 1.1.1.7 christos else if (val_type->code () == TYPE_CODE_REF)
1042 1.1.1.5 christos res_val = value_cast (lookup_lvalue_reference_type (base_class_type),
1043 1.1.1.8 christos tmp);
1044 1.1.1.7 christos else if (val_type->code () == TYPE_CODE_RVALUE_REF)
1045 1.1.1.5 christos res_val = value_cast (lookup_rvalue_reference_type (base_class_type),
1046 1.1.1.8 christos tmp);
1047 1.1 christos else
1048 1.1 christos res_val = value_cast (base_class_type, tmp);
1049 1.1 christos }
1050 1.1 christos else
1051 1.1 christos {
1052 1.1 christos /* Assume we are attempting an array access, and let the
1053 1.1 christos value code throw an exception if the index has an invalid
1054 1.1 christos type. */
1055 1.1 christos struct value *idx = convert_value_from_python (key);
1056 1.1 christos
1057 1.1 christos if (idx != NULL)
1058 1.1 christos {
1059 1.1 christos /* Check the value's type is something that can be accessed via
1060 1.1 christos a subscript. */
1061 1.1 christos struct type *type;
1062 1.1 christos
1063 1.1 christos tmp = coerce_ref (tmp);
1064 1.1 christos type = check_typedef (value_type (tmp));
1065 1.1.1.7 christos if (type->code () != TYPE_CODE_ARRAY
1066 1.1.1.7 christos && type->code () != TYPE_CODE_PTR)
1067 1.1 christos error (_("Cannot subscript requested type."));
1068 1.1 christos else
1069 1.1 christos res_val = value_subscript (tmp, value_as_long (idx));
1070 1.1 christos }
1071 1.1 christos }
1072 1.1 christos
1073 1.1 christos if (res_val)
1074 1.1 christos result = value_to_value_object (res_val);
1075 1.1 christos }
1076 1.1.1.7 christos catch (gdb_exception &ex)
1077 1.1.1.3 christos {
1078 1.1.1.7 christos except = std::move (ex);
1079 1.1.1.3 christos }
1080 1.1 christos
1081 1.1 christos GDB_PY_HANDLE_EXCEPTION (except);
1082 1.1 christos
1083 1.1 christos return result;
1084 1.1 christos }
1085 1.1 christos
1086 1.1 christos static int
1087 1.1 christos valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
1088 1.1 christos {
1089 1.1 christos PyErr_Format (PyExc_NotImplementedError,
1090 1.1 christos _("Setting of struct elements is not currently supported."));
1091 1.1 christos return -1;
1092 1.1 christos }
1093 1.1 christos
1094 1.1 christos /* Called by the Python interpreter to perform an inferior function
1095 1.1 christos call on the value. Returns NULL on error, with a python exception set. */
1096 1.1 christos static PyObject *
1097 1.1 christos valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
1098 1.1 christos {
1099 1.1 christos Py_ssize_t args_count;
1100 1.1 christos struct value *function = ((value_object *) self)->value;
1101 1.1 christos struct value **vargs = NULL;
1102 1.1 christos struct type *ftype = NULL;
1103 1.1 christos PyObject *result = NULL;
1104 1.1 christos
1105 1.1.1.7 christos try
1106 1.1 christos {
1107 1.1 christos ftype = check_typedef (value_type (function));
1108 1.1 christos }
1109 1.1.1.7 christos catch (const gdb_exception &except)
1110 1.1.1.3 christos {
1111 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1112 1.1.1.3 christos }
1113 1.1 christos
1114 1.1.1.7 christos if (ftype->code () != TYPE_CODE_FUNC)
1115 1.1 christos {
1116 1.1 christos PyErr_SetString (PyExc_RuntimeError,
1117 1.1 christos _("Value is not callable (not TYPE_CODE_FUNC)."));
1118 1.1 christos return NULL;
1119 1.1 christos }
1120 1.1 christos
1121 1.1 christos if (! PyTuple_Check (args))
1122 1.1 christos {
1123 1.1 christos PyErr_SetString (PyExc_TypeError,
1124 1.1 christos _("Inferior arguments must be provided in a tuple."));
1125 1.1 christos return NULL;
1126 1.1 christos }
1127 1.1 christos
1128 1.1 christos args_count = PyTuple_Size (args);
1129 1.1 christos if (args_count > 0)
1130 1.1 christos {
1131 1.1 christos int i;
1132 1.1 christos
1133 1.1.1.4 christos vargs = XALLOCAVEC (struct value *, args_count);
1134 1.1 christos for (i = 0; i < args_count; i++)
1135 1.1 christos {
1136 1.1 christos PyObject *item = PyTuple_GetItem (args, i);
1137 1.1 christos
1138 1.1 christos if (item == NULL)
1139 1.1 christos return NULL;
1140 1.1 christos
1141 1.1 christos vargs[i] = convert_value_from_python (item);
1142 1.1 christos if (vargs[i] == NULL)
1143 1.1 christos return NULL;
1144 1.1 christos }
1145 1.1 christos }
1146 1.1 christos
1147 1.1.1.7 christos try
1148 1.1 christos {
1149 1.1.1.5 christos scoped_value_mark free_values;
1150 1.1 christos
1151 1.1.1.6 christos value *return_value
1152 1.1.1.6 christos = call_function_by_hand (function, NULL,
1153 1.1.1.6 christos gdb::make_array_view (vargs, args_count));
1154 1.1 christos result = value_to_value_object (return_value);
1155 1.1 christos }
1156 1.1.1.7 christos catch (const gdb_exception &except)
1157 1.1.1.3 christos {
1158 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1159 1.1.1.3 christos }
1160 1.1 christos
1161 1.1 christos return result;
1162 1.1 christos }
1163 1.1 christos
1164 1.1 christos /* Called by the Python interpreter to obtain string representation
1165 1.1 christos of the object. */
1166 1.1 christos static PyObject *
1167 1.1 christos valpy_str (PyObject *self)
1168 1.1 christos {
1169 1.1 christos struct value_print_options opts;
1170 1.1 christos
1171 1.1.1.8 christos gdbpy_get_print_options (&opts);
1172 1.1 christos opts.deref_ref = 0;
1173 1.1 christos
1174 1.1.1.5 christos string_file stb;
1175 1.1.1.5 christos
1176 1.1.1.7 christos try
1177 1.1 christos {
1178 1.1.1.5 christos common_val_print (((value_object *) self)->value, &stb, 0,
1179 1.1.1.8 christos &opts, current_language);
1180 1.1 christos }
1181 1.1.1.7 christos catch (const gdb_exception &except)
1182 1.1.1.3 christos {
1183 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1184 1.1.1.3 christos }
1185 1.1 christos
1186 1.1.1.5 christos return PyUnicode_Decode (stb.c_str (), stb.size (), host_charset (), NULL);
1187 1.1 christos }
1188 1.1 christos
1189 1.1 christos /* Implements gdb.Value.is_optimized_out. */
1190 1.1 christos static PyObject *
1191 1.1 christos valpy_get_is_optimized_out (PyObject *self, void *closure)
1192 1.1 christos {
1193 1.1 christos struct value *value = ((value_object *) self)->value;
1194 1.1 christos int opt = 0;
1195 1.1 christos
1196 1.1.1.7 christos try
1197 1.1 christos {
1198 1.1 christos opt = value_optimized_out (value);
1199 1.1 christos }
1200 1.1.1.7 christos catch (const gdb_exception &except)
1201 1.1.1.3 christos {
1202 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1203 1.1.1.3 christos }
1204 1.1 christos
1205 1.1 christos if (opt)
1206 1.1 christos Py_RETURN_TRUE;
1207 1.1 christos
1208 1.1 christos Py_RETURN_FALSE;
1209 1.1 christos }
1210 1.1 christos
1211 1.1 christos /* Implements gdb.Value.is_lazy. */
1212 1.1 christos static PyObject *
1213 1.1 christos valpy_get_is_lazy (PyObject *self, void *closure)
1214 1.1 christos {
1215 1.1 christos struct value *value = ((value_object *) self)->value;
1216 1.1 christos int opt = 0;
1217 1.1 christos
1218 1.1.1.7 christos try
1219 1.1 christos {
1220 1.1 christos opt = value_lazy (value);
1221 1.1 christos }
1222 1.1.1.7 christos catch (const gdb_exception &except)
1223 1.1.1.3 christos {
1224 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1225 1.1.1.3 christos }
1226 1.1 christos
1227 1.1 christos if (opt)
1228 1.1 christos Py_RETURN_TRUE;
1229 1.1 christos
1230 1.1 christos Py_RETURN_FALSE;
1231 1.1 christos }
1232 1.1 christos
1233 1.1 christos /* Implements gdb.Value.fetch_lazy (). */
1234 1.1 christos static PyObject *
1235 1.1 christos valpy_fetch_lazy (PyObject *self, PyObject *args)
1236 1.1 christos {
1237 1.1 christos struct value *value = ((value_object *) self)->value;
1238 1.1 christos
1239 1.1.1.7 christos try
1240 1.1 christos {
1241 1.1 christos if (value_lazy (value))
1242 1.1 christos value_fetch_lazy (value);
1243 1.1 christos }
1244 1.1.1.7 christos catch (const gdb_exception &except)
1245 1.1.1.3 christos {
1246 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1247 1.1.1.3 christos }
1248 1.1 christos
1249 1.1 christos Py_RETURN_NONE;
1250 1.1 christos }
1251 1.1 christos
1252 1.1 christos /* Calculate and return the address of the PyObject as the value of
1253 1.1 christos the builtin __hash__ call. */
1254 1.1.1.3 christos static Py_hash_t
1255 1.1 christos valpy_hash (PyObject *self)
1256 1.1 christos {
1257 1.1.1.3 christos return (intptr_t) self;
1258 1.1 christos }
1259 1.1 christos
1260 1.1 christos enum valpy_opcode
1261 1.1 christos {
1262 1.1 christos VALPY_ADD,
1263 1.1 christos VALPY_SUB,
1264 1.1 christos VALPY_MUL,
1265 1.1 christos VALPY_DIV,
1266 1.1 christos VALPY_REM,
1267 1.1 christos VALPY_POW,
1268 1.1 christos VALPY_LSH,
1269 1.1 christos VALPY_RSH,
1270 1.1 christos VALPY_BITAND,
1271 1.1 christos VALPY_BITOR,
1272 1.1 christos VALPY_BITXOR
1273 1.1 christos };
1274 1.1 christos
1275 1.1 christos /* If TYPE is a reference, return the target; otherwise return TYPE. */
1276 1.1 christos #define STRIP_REFERENCE(TYPE) \
1277 1.1.1.8 christos (TYPE_IS_REFERENCE (TYPE) ? ((TYPE)->target_type ()) : (TYPE))
1278 1.1 christos
1279 1.1.1.4 christos /* Helper for valpy_binop. Returns a value object which is the result
1280 1.1.1.4 christos of applying the operation specified by OPCODE to the given
1281 1.1.1.4 christos arguments. Throws a GDB exception on error. */
1282 1.1.1.4 christos
1283 1.1 christos static PyObject *
1284 1.1.1.4 christos valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other)
1285 1.1 christos {
1286 1.1 christos PyObject *result = NULL;
1287 1.1 christos
1288 1.1.1.4 christos struct value *arg1, *arg2;
1289 1.1.1.4 christos struct value *res_val = NULL;
1290 1.1.1.4 christos enum exp_opcode op = OP_NULL;
1291 1.1.1.4 christos int handled = 0;
1292 1.1.1.4 christos
1293 1.1.1.5 christos scoped_value_mark free_values;
1294 1.1.1.5 christos
1295 1.1.1.4 christos /* If the gdb.Value object is the second operand, then it will be
1296 1.1.1.4 christos passed to us as the OTHER argument, and SELF will be an entirely
1297 1.1.1.4 christos different kind of object, altogether. Because of this, we can't
1298 1.1.1.4 christos assume self is a gdb.Value object and need to convert it from
1299 1.1.1.4 christos python as well. */
1300 1.1.1.4 christos arg1 = convert_value_from_python (self);
1301 1.1.1.4 christos if (arg1 == NULL)
1302 1.1.1.5 christos return NULL;
1303 1.1 christos
1304 1.1.1.4 christos arg2 = convert_value_from_python (other);
1305 1.1.1.4 christos if (arg2 == NULL)
1306 1.1.1.5 christos return NULL;
1307 1.1 christos
1308 1.1.1.4 christos switch (opcode)
1309 1.1.1.4 christos {
1310 1.1.1.4 christos case VALPY_ADD:
1311 1.1.1.4 christos {
1312 1.1.1.4 christos struct type *ltype = value_type (arg1);
1313 1.1.1.4 christos struct type *rtype = value_type (arg2);
1314 1.1.1.4 christos
1315 1.1.1.4 christos ltype = check_typedef (ltype);
1316 1.1.1.4 christos ltype = STRIP_REFERENCE (ltype);
1317 1.1.1.4 christos rtype = check_typedef (rtype);
1318 1.1.1.4 christos rtype = STRIP_REFERENCE (rtype);
1319 1.1.1.4 christos
1320 1.1.1.4 christos handled = 1;
1321 1.1.1.7 christos if (ltype->code () == TYPE_CODE_PTR
1322 1.1.1.4 christos && is_integral_type (rtype))
1323 1.1.1.4 christos res_val = value_ptradd (arg1, value_as_long (arg2));
1324 1.1.1.7 christos else if (rtype->code () == TYPE_CODE_PTR
1325 1.1.1.4 christos && is_integral_type (ltype))
1326 1.1.1.4 christos res_val = value_ptradd (arg2, value_as_long (arg1));
1327 1.1.1.4 christos else
1328 1.1 christos {
1329 1.1.1.4 christos handled = 0;
1330 1.1.1.4 christos op = BINOP_ADD;
1331 1.1 christos }
1332 1.1.1.4 christos }
1333 1.1.1.4 christos break;
1334 1.1.1.4 christos case VALPY_SUB:
1335 1.1.1.4 christos {
1336 1.1.1.4 christos struct type *ltype = value_type (arg1);
1337 1.1.1.4 christos struct type *rtype = value_type (arg2);
1338 1.1.1.4 christos
1339 1.1.1.4 christos ltype = check_typedef (ltype);
1340 1.1.1.4 christos ltype = STRIP_REFERENCE (ltype);
1341 1.1.1.4 christos rtype = check_typedef (rtype);
1342 1.1.1.4 christos rtype = STRIP_REFERENCE (rtype);
1343 1.1.1.4 christos
1344 1.1.1.4 christos handled = 1;
1345 1.1.1.7 christos if (ltype->code () == TYPE_CODE_PTR
1346 1.1.1.7 christos && rtype->code () == TYPE_CODE_PTR)
1347 1.1.1.4 christos /* A ptrdiff_t for the target would be preferable here. */
1348 1.1.1.4 christos res_val = value_from_longest (builtin_type_pyint,
1349 1.1.1.4 christos value_ptrdiff (arg1, arg2));
1350 1.1.1.7 christos else if (ltype->code () == TYPE_CODE_PTR
1351 1.1.1.4 christos && is_integral_type (rtype))
1352 1.1.1.4 christos res_val = value_ptradd (arg1, - value_as_long (arg2));
1353 1.1.1.4 christos else
1354 1.1 christos {
1355 1.1.1.4 christos handled = 0;
1356 1.1.1.4 christos op = BINOP_SUB;
1357 1.1 christos }
1358 1.1.1.4 christos }
1359 1.1.1.4 christos break;
1360 1.1.1.4 christos case VALPY_MUL:
1361 1.1.1.4 christos op = BINOP_MUL;
1362 1.1.1.4 christos break;
1363 1.1.1.4 christos case VALPY_DIV:
1364 1.1.1.4 christos op = BINOP_DIV;
1365 1.1.1.4 christos break;
1366 1.1.1.4 christos case VALPY_REM:
1367 1.1.1.4 christos op = BINOP_REM;
1368 1.1.1.4 christos break;
1369 1.1.1.4 christos case VALPY_POW:
1370 1.1.1.4 christos op = BINOP_EXP;
1371 1.1.1.4 christos break;
1372 1.1.1.4 christos case VALPY_LSH:
1373 1.1.1.4 christos op = BINOP_LSH;
1374 1.1.1.4 christos break;
1375 1.1.1.4 christos case VALPY_RSH:
1376 1.1.1.4 christos op = BINOP_RSH;
1377 1.1.1.4 christos break;
1378 1.1.1.4 christos case VALPY_BITAND:
1379 1.1.1.4 christos op = BINOP_BITWISE_AND;
1380 1.1.1.4 christos break;
1381 1.1.1.4 christos case VALPY_BITOR:
1382 1.1.1.4 christos op = BINOP_BITWISE_IOR;
1383 1.1.1.4 christos break;
1384 1.1.1.4 christos case VALPY_BITXOR:
1385 1.1.1.4 christos op = BINOP_BITWISE_XOR;
1386 1.1.1.4 christos break;
1387 1.1.1.4 christos }
1388 1.1 christos
1389 1.1.1.4 christos if (!handled)
1390 1.1.1.4 christos {
1391 1.1.1.4 christos if (binop_user_defined_p (op, arg1, arg2))
1392 1.1.1.4 christos res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
1393 1.1.1.4 christos else
1394 1.1.1.4 christos res_val = value_binop (arg1, arg2, op);
1395 1.1.1.4 christos }
1396 1.1.1.2 christos
1397 1.1.1.4 christos if (res_val)
1398 1.1.1.4 christos result = value_to_value_object (res_val);
1399 1.1 christos
1400 1.1.1.4 christos return result;
1401 1.1.1.4 christos }
1402 1.1.1.4 christos
1403 1.1.1.4 christos /* Returns a value object which is the result of applying the operation
1404 1.1.1.4 christos specified by OPCODE to the given arguments. Returns NULL on error, with
1405 1.1.1.4 christos a python exception set. */
1406 1.1.1.4 christos static PyObject *
1407 1.1.1.4 christos valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
1408 1.1.1.4 christos {
1409 1.1.1.4 christos PyObject *result = NULL;
1410 1.1.1.4 christos
1411 1.1.1.7 christos try
1412 1.1.1.4 christos {
1413 1.1.1.4 christos result = valpy_binop_throw (opcode, self, other);
1414 1.1 christos }
1415 1.1.1.7 christos catch (const gdb_exception &except)
1416 1.1.1.3 christos {
1417 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1418 1.1.1.3 christos }
1419 1.1 christos
1420 1.1 christos return result;
1421 1.1 christos }
1422 1.1 christos
1423 1.1 christos static PyObject *
1424 1.1 christos valpy_add (PyObject *self, PyObject *other)
1425 1.1 christos {
1426 1.1 christos return valpy_binop (VALPY_ADD, self, other);
1427 1.1 christos }
1428 1.1 christos
1429 1.1 christos static PyObject *
1430 1.1 christos valpy_subtract (PyObject *self, PyObject *other)
1431 1.1 christos {
1432 1.1 christos return valpy_binop (VALPY_SUB, self, other);
1433 1.1 christos }
1434 1.1 christos
1435 1.1 christos static PyObject *
1436 1.1 christos valpy_multiply (PyObject *self, PyObject *other)
1437 1.1 christos {
1438 1.1 christos return valpy_binop (VALPY_MUL, self, other);
1439 1.1 christos }
1440 1.1 christos
1441 1.1 christos static PyObject *
1442 1.1 christos valpy_divide (PyObject *self, PyObject *other)
1443 1.1 christos {
1444 1.1 christos return valpy_binop (VALPY_DIV, self, other);
1445 1.1 christos }
1446 1.1 christos
1447 1.1 christos static PyObject *
1448 1.1 christos valpy_remainder (PyObject *self, PyObject *other)
1449 1.1 christos {
1450 1.1 christos return valpy_binop (VALPY_REM, self, other);
1451 1.1 christos }
1452 1.1 christos
1453 1.1 christos static PyObject *
1454 1.1 christos valpy_power (PyObject *self, PyObject *other, PyObject *unused)
1455 1.1 christos {
1456 1.1 christos /* We don't support the ternary form of pow. I don't know how to express
1457 1.1 christos that, so let's just throw NotImplementedError to at least do something
1458 1.1 christos about it. */
1459 1.1 christos if (unused != Py_None)
1460 1.1 christos {
1461 1.1 christos PyErr_SetString (PyExc_NotImplementedError,
1462 1.1 christos "Invalid operation on gdb.Value.");
1463 1.1 christos return NULL;
1464 1.1 christos }
1465 1.1 christos
1466 1.1 christos return valpy_binop (VALPY_POW, self, other);
1467 1.1 christos }
1468 1.1 christos
1469 1.1 christos static PyObject *
1470 1.1 christos valpy_negative (PyObject *self)
1471 1.1 christos {
1472 1.1 christos PyObject *result = NULL;
1473 1.1 christos
1474 1.1.1.7 christos try
1475 1.1 christos {
1476 1.1 christos /* Perhaps overkill, but consistency has some virtue. */
1477 1.1.1.5 christos scoped_value_mark free_values;
1478 1.1 christos struct value *val;
1479 1.1 christos
1480 1.1 christos val = value_neg (((value_object *) self)->value);
1481 1.1 christos result = value_to_value_object (val);
1482 1.1 christos }
1483 1.1.1.7 christos catch (const gdb_exception &except)
1484 1.1.1.3 christos {
1485 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1486 1.1.1.3 christos }
1487 1.1 christos
1488 1.1 christos return result;
1489 1.1 christos }
1490 1.1 christos
1491 1.1 christos static PyObject *
1492 1.1 christos valpy_positive (PyObject *self)
1493 1.1 christos {
1494 1.1 christos return value_to_value_object (((value_object *) self)->value);
1495 1.1 christos }
1496 1.1 christos
1497 1.1 christos static PyObject *
1498 1.1 christos valpy_absolute (PyObject *self)
1499 1.1 christos {
1500 1.1 christos struct value *value = ((value_object *) self)->value;
1501 1.1 christos int isabs = 1;
1502 1.1 christos
1503 1.1.1.7 christos try
1504 1.1 christos {
1505 1.1.1.5 christos scoped_value_mark free_values;
1506 1.1 christos
1507 1.1 christos if (value_less (value, value_zero (value_type (value), not_lval)))
1508 1.1 christos isabs = 0;
1509 1.1 christos }
1510 1.1.1.7 christos catch (const gdb_exception &except)
1511 1.1.1.3 christos {
1512 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1513 1.1.1.3 christos }
1514 1.1 christos
1515 1.1 christos if (isabs)
1516 1.1 christos return valpy_positive (self);
1517 1.1 christos else
1518 1.1 christos return valpy_negative (self);
1519 1.1 christos }
1520 1.1 christos
1521 1.1 christos /* Implements boolean evaluation of gdb.Value. */
1522 1.1 christos static int
1523 1.1 christos valpy_nonzero (PyObject *self)
1524 1.1 christos {
1525 1.1.1.7 christos struct gdb_exception except;
1526 1.1 christos value_object *self_value = (value_object *) self;
1527 1.1 christos struct type *type;
1528 1.1 christos int nonzero = 0; /* Appease GCC warning. */
1529 1.1 christos
1530 1.1.1.7 christos try
1531 1.1 christos {
1532 1.1 christos type = check_typedef (value_type (self_value->value));
1533 1.1 christos
1534 1.1.1.7 christos if (is_integral_type (type) || type->code () == TYPE_CODE_PTR)
1535 1.1 christos nonzero = !!value_as_long (self_value->value);
1536 1.1.1.6 christos else if (is_floating_value (self_value->value))
1537 1.1.1.8 christos nonzero = !target_float_is_zero
1538 1.1.1.8 christos (value_contents (self_value->value).data (), type);
1539 1.1 christos else
1540 1.1 christos /* All other values are True. */
1541 1.1 christos nonzero = 1;
1542 1.1 christos }
1543 1.1.1.7 christos catch (gdb_exception &ex)
1544 1.1.1.3 christos {
1545 1.1.1.7 christos except = std::move (ex);
1546 1.1.1.3 christos }
1547 1.1.1.3 christos
1548 1.1 christos /* This is not documented in the Python documentation, but if this
1549 1.1 christos function fails, return -1 as slot_nb_nonzero does (the default
1550 1.1 christos Python nonzero function). */
1551 1.1 christos GDB_PY_SET_HANDLE_EXCEPTION (except);
1552 1.1 christos
1553 1.1 christos return nonzero;
1554 1.1 christos }
1555 1.1 christos
1556 1.1 christos /* Implements ~ for value objects. */
1557 1.1 christos static PyObject *
1558 1.1 christos valpy_invert (PyObject *self)
1559 1.1 christos {
1560 1.1 christos struct value *val = NULL;
1561 1.1 christos
1562 1.1.1.7 christos try
1563 1.1 christos {
1564 1.1 christos val = value_complement (((value_object *) self)->value);
1565 1.1 christos }
1566 1.1.1.7 christos catch (const gdb_exception &except)
1567 1.1.1.3 christos {
1568 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1569 1.1.1.3 christos }
1570 1.1 christos
1571 1.1 christos return value_to_value_object (val);
1572 1.1 christos }
1573 1.1 christos
1574 1.1 christos /* Implements left shift for value objects. */
1575 1.1 christos static PyObject *
1576 1.1 christos valpy_lsh (PyObject *self, PyObject *other)
1577 1.1 christos {
1578 1.1 christos return valpy_binop (VALPY_LSH, self, other);
1579 1.1 christos }
1580 1.1 christos
1581 1.1 christos /* Implements right shift for value objects. */
1582 1.1 christos static PyObject *
1583 1.1 christos valpy_rsh (PyObject *self, PyObject *other)
1584 1.1 christos {
1585 1.1 christos return valpy_binop (VALPY_RSH, self, other);
1586 1.1 christos }
1587 1.1 christos
1588 1.1 christos /* Implements bitwise and for value objects. */
1589 1.1 christos static PyObject *
1590 1.1 christos valpy_and (PyObject *self, PyObject *other)
1591 1.1 christos {
1592 1.1 christos return valpy_binop (VALPY_BITAND, self, other);
1593 1.1 christos }
1594 1.1 christos
1595 1.1 christos /* Implements bitwise or for value objects. */
1596 1.1 christos static PyObject *
1597 1.1 christos valpy_or (PyObject *self, PyObject *other)
1598 1.1 christos {
1599 1.1 christos return valpy_binop (VALPY_BITOR, self, other);
1600 1.1 christos }
1601 1.1 christos
1602 1.1 christos /* Implements bitwise xor for value objects. */
1603 1.1 christos static PyObject *
1604 1.1 christos valpy_xor (PyObject *self, PyObject *other)
1605 1.1 christos {
1606 1.1 christos return valpy_binop (VALPY_BITXOR, self, other);
1607 1.1 christos }
1608 1.1 christos
1609 1.1.1.4 christos /* Helper for valpy_richcompare. Implements comparison operations for
1610 1.1.1.4 christos value objects. Returns true/false on success. Returns -1 with a
1611 1.1.1.4 christos Python exception set if a Python error is detected. Throws a GDB
1612 1.1.1.4 christos exception on other errors (memory error, etc.). */
1613 1.1.1.4 christos
1614 1.1.1.4 christos static int
1615 1.1.1.4 christos valpy_richcompare_throw (PyObject *self, PyObject *other, int op)
1616 1.1.1.4 christos {
1617 1.1.1.4 christos int result;
1618 1.1.1.4 christos struct value *value_other;
1619 1.1.1.4 christos struct value *value_self;
1620 1.1.1.5 christos
1621 1.1.1.5 christos scoped_value_mark free_values;
1622 1.1.1.4 christos
1623 1.1.1.4 christos value_other = convert_value_from_python (other);
1624 1.1.1.4 christos if (value_other == NULL)
1625 1.1.1.4 christos return -1;
1626 1.1.1.4 christos
1627 1.1.1.4 christos value_self = ((value_object *) self)->value;
1628 1.1.1.4 christos
1629 1.1.1.4 christos switch (op)
1630 1.1.1.4 christos {
1631 1.1.1.4 christos case Py_LT:
1632 1.1.1.4 christos result = value_less (value_self, value_other);
1633 1.1.1.4 christos break;
1634 1.1.1.4 christos case Py_LE:
1635 1.1.1.4 christos result = value_less (value_self, value_other)
1636 1.1.1.4 christos || value_equal (value_self, value_other);
1637 1.1.1.4 christos break;
1638 1.1.1.4 christos case Py_EQ:
1639 1.1.1.4 christos result = value_equal (value_self, value_other);
1640 1.1.1.4 christos break;
1641 1.1.1.4 christos case Py_NE:
1642 1.1.1.4 christos result = !value_equal (value_self, value_other);
1643 1.1.1.4 christos break;
1644 1.1.1.4 christos case Py_GT:
1645 1.1.1.4 christos result = value_less (value_other, value_self);
1646 1.1.1.4 christos break;
1647 1.1.1.4 christos case Py_GE:
1648 1.1.1.4 christos result = (value_less (value_other, value_self)
1649 1.1.1.4 christos || value_equal (value_self, value_other));
1650 1.1.1.4 christos break;
1651 1.1.1.4 christos default:
1652 1.1.1.4 christos /* Can't happen. */
1653 1.1.1.4 christos PyErr_SetString (PyExc_NotImplementedError,
1654 1.1.1.4 christos _("Invalid operation on gdb.Value."));
1655 1.1.1.4 christos result = -1;
1656 1.1.1.4 christos break;
1657 1.1.1.4 christos }
1658 1.1.1.4 christos
1659 1.1.1.4 christos return result;
1660 1.1.1.4 christos }
1661 1.1.1.4 christos
1662 1.1.1.4 christos
1663 1.1 christos /* Implements comparison operations for value objects. Returns NULL on error,
1664 1.1 christos with a python exception set. */
1665 1.1 christos static PyObject *
1666 1.1 christos valpy_richcompare (PyObject *self, PyObject *other, int op)
1667 1.1 christos {
1668 1.1 christos int result = 0;
1669 1.1 christos
1670 1.1 christos if (other == Py_None)
1671 1.1 christos /* Comparing with None is special. From what I can tell, in Python
1672 1.1 christos None is smaller than anything else. */
1673 1.1 christos switch (op) {
1674 1.1 christos case Py_LT:
1675 1.1 christos case Py_LE:
1676 1.1 christos case Py_EQ:
1677 1.1 christos Py_RETURN_FALSE;
1678 1.1 christos case Py_NE:
1679 1.1 christos case Py_GT:
1680 1.1 christos case Py_GE:
1681 1.1 christos Py_RETURN_TRUE;
1682 1.1 christos default:
1683 1.1 christos /* Can't happen. */
1684 1.1 christos PyErr_SetString (PyExc_NotImplementedError,
1685 1.1 christos _("Invalid operation on gdb.Value."));
1686 1.1 christos return NULL;
1687 1.1 christos }
1688 1.1 christos
1689 1.1.1.7 christos try
1690 1.1 christos {
1691 1.1.1.4 christos result = valpy_richcompare_throw (self, other, op);
1692 1.1 christos }
1693 1.1.1.7 christos catch (const gdb_exception &except)
1694 1.1.1.3 christos {
1695 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1696 1.1.1.3 christos }
1697 1.1 christos
1698 1.1 christos /* In this case, the Python exception has already been set. */
1699 1.1 christos if (result < 0)
1700 1.1 christos return NULL;
1701 1.1 christos
1702 1.1 christos if (result == 1)
1703 1.1 christos Py_RETURN_TRUE;
1704 1.1 christos
1705 1.1 christos Py_RETURN_FALSE;
1706 1.1 christos }
1707 1.1 christos
1708 1.1 christos /* Implements conversion to long. */
1709 1.1 christos static PyObject *
1710 1.1 christos valpy_long (PyObject *self)
1711 1.1 christos {
1712 1.1 christos struct value *value = ((value_object *) self)->value;
1713 1.1 christos struct type *type = value_type (value);
1714 1.1 christos LONGEST l = 0;
1715 1.1 christos
1716 1.1.1.7 christos try
1717 1.1 christos {
1718 1.1.1.6 christos if (is_floating_value (value))
1719 1.1.1.6 christos {
1720 1.1.1.6 christos type = builtin_type_pylong;
1721 1.1.1.6 christos value = value_cast (type, value);
1722 1.1.1.6 christos }
1723 1.1.1.6 christos
1724 1.1.1.4 christos type = check_typedef (type);
1725 1.1 christos
1726 1.1 christos if (!is_integral_type (type)
1727 1.1.1.7 christos && type->code () != TYPE_CODE_PTR)
1728 1.1 christos error (_("Cannot convert value to long."));
1729 1.1 christos
1730 1.1 christos l = value_as_long (value);
1731 1.1 christos }
1732 1.1.1.7 christos catch (const gdb_exception &except)
1733 1.1.1.3 christos {
1734 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1735 1.1.1.3 christos }
1736 1.1 christos
1737 1.1.1.8 christos if (type->is_unsigned ())
1738 1.1.1.8 christos return gdb_py_object_from_ulongest (l).release ();
1739 1.1.1.5 christos else
1740 1.1.1.8 christos return gdb_py_object_from_longest (l).release ();
1741 1.1 christos }
1742 1.1 christos
1743 1.1 christos /* Implements conversion to float. */
1744 1.1 christos static PyObject *
1745 1.1 christos valpy_float (PyObject *self)
1746 1.1 christos {
1747 1.1 christos struct value *value = ((value_object *) self)->value;
1748 1.1 christos struct type *type = value_type (value);
1749 1.1 christos double d = 0;
1750 1.1 christos
1751 1.1.1.7 christos try
1752 1.1 christos {
1753 1.1.1.4 christos type = check_typedef (type);
1754 1.1 christos
1755 1.1.1.7 christos if (type->code () == TYPE_CODE_FLT && is_floating_value (value))
1756 1.1.1.8 christos d = target_float_to_host_double (value_contents (value).data (), type);
1757 1.1.1.7 christos else if (type->code () == TYPE_CODE_INT)
1758 1.1.1.6 christos {
1759 1.1.1.6 christos /* Note that valpy_long accepts TYPE_CODE_PTR and some
1760 1.1.1.6 christos others here here -- but casting a pointer or bool to a
1761 1.1.1.6 christos float seems wrong. */
1762 1.1.1.6 christos d = value_as_long (value);
1763 1.1.1.6 christos }
1764 1.1.1.6 christos else
1765 1.1 christos error (_("Cannot convert value to float."));
1766 1.1 christos }
1767 1.1.1.7 christos catch (const gdb_exception &except)
1768 1.1.1.3 christos {
1769 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1770 1.1.1.3 christos }
1771 1.1 christos
1772 1.1 christos return PyFloat_FromDouble (d);
1773 1.1 christos }
1774 1.1 christos
1775 1.1 christos /* Returns an object for a value which is released from the all_values chain,
1776 1.1 christos so its lifetime is not bound to the execution of a command. */
1777 1.1 christos PyObject *
1778 1.1 christos value_to_value_object (struct value *val)
1779 1.1 christos {
1780 1.1 christos value_object *val_obj;
1781 1.1 christos
1782 1.1 christos val_obj = PyObject_New (value_object, &value_object_type);
1783 1.1 christos if (val_obj != NULL)
1784 1.1 christos {
1785 1.1.1.6 christos val_obj->value = release_value (val).release ();
1786 1.1.1.8 christos val_obj->next = nullptr;
1787 1.1.1.8 christos val_obj->prev = nullptr;
1788 1.1 christos val_obj->address = NULL;
1789 1.1 christos val_obj->type = NULL;
1790 1.1 christos val_obj->dynamic_type = NULL;
1791 1.1 christos note_value (val_obj);
1792 1.1 christos }
1793 1.1 christos
1794 1.1 christos return (PyObject *) val_obj;
1795 1.1 christos }
1796 1.1 christos
1797 1.1.1.7 christos /* Returns an object for a value, but without releasing it from the
1798 1.1.1.7 christos all_values chain. */
1799 1.1.1.7 christos PyObject *
1800 1.1.1.7 christos value_to_value_object_no_release (struct value *val)
1801 1.1.1.7 christos {
1802 1.1.1.7 christos value_object *val_obj;
1803 1.1.1.7 christos
1804 1.1.1.7 christos val_obj = PyObject_New (value_object, &value_object_type);
1805 1.1.1.7 christos if (val_obj != NULL)
1806 1.1.1.7 christos {
1807 1.1.1.7 christos value_incref (val);
1808 1.1.1.7 christos val_obj->value = val;
1809 1.1.1.8 christos val_obj->next = nullptr;
1810 1.1.1.8 christos val_obj->prev = nullptr;
1811 1.1.1.7 christos val_obj->address = NULL;
1812 1.1.1.7 christos val_obj->type = NULL;
1813 1.1.1.7 christos val_obj->dynamic_type = NULL;
1814 1.1.1.7 christos note_value (val_obj);
1815 1.1.1.7 christos }
1816 1.1.1.7 christos
1817 1.1.1.7 christos return (PyObject *) val_obj;
1818 1.1.1.7 christos }
1819 1.1.1.7 christos
1820 1.1 christos /* Returns a borrowed reference to the struct value corresponding to
1821 1.1 christos the given value object. */
1822 1.1 christos struct value *
1823 1.1 christos value_object_to_value (PyObject *self)
1824 1.1 christos {
1825 1.1 christos value_object *real;
1826 1.1 christos
1827 1.1 christos if (! PyObject_TypeCheck (self, &value_object_type))
1828 1.1 christos return NULL;
1829 1.1 christos real = (value_object *) self;
1830 1.1 christos return real->value;
1831 1.1 christos }
1832 1.1 christos
1833 1.1 christos /* Try to convert a Python value to a gdb value. If the value cannot
1834 1.1 christos be converted, set a Python exception and return NULL. Returns a
1835 1.1 christos reference to a new value on the all_values chain. */
1836 1.1 christos
1837 1.1 christos struct value *
1838 1.1 christos convert_value_from_python (PyObject *obj)
1839 1.1 christos {
1840 1.1 christos struct value *value = NULL; /* -Wall */
1841 1.1 christos int cmp;
1842 1.1 christos
1843 1.1 christos gdb_assert (obj != NULL);
1844 1.1 christos
1845 1.1.1.7 christos try
1846 1.1 christos {
1847 1.1 christos if (PyBool_Check (obj))
1848 1.1 christos {
1849 1.1 christos cmp = PyObject_IsTrue (obj);
1850 1.1 christos if (cmp >= 0)
1851 1.1 christos value = value_from_longest (builtin_type_pybool, cmp);
1852 1.1 christos }
1853 1.1 christos else if (PyLong_Check (obj))
1854 1.1 christos {
1855 1.1 christos LONGEST l = PyLong_AsLongLong (obj);
1856 1.1 christos
1857 1.1 christos if (PyErr_Occurred ())
1858 1.1 christos {
1859 1.1 christos /* If the error was an overflow, we can try converting to
1860 1.1.1.8 christos ULONGEST instead. */
1861 1.1 christos if (PyErr_ExceptionMatches (PyExc_OverflowError))
1862 1.1 christos {
1863 1.1.1.6 christos gdbpy_err_fetch fetched_error;
1864 1.1.1.8 christos gdbpy_ref<> zero = gdb_py_object_from_longest (0);
1865 1.1 christos
1866 1.1 christos /* Check whether obj is positive. */
1867 1.1.1.5 christos if (PyObject_RichCompareBool (obj, zero.get (), Py_GT) > 0)
1868 1.1 christos {
1869 1.1 christos ULONGEST ul;
1870 1.1 christos
1871 1.1 christos ul = PyLong_AsUnsignedLongLong (obj);
1872 1.1 christos if (! PyErr_Occurred ())
1873 1.1 christos value = value_from_ulongest (builtin_type_upylong, ul);
1874 1.1 christos }
1875 1.1 christos else
1876 1.1.1.6 christos {
1877 1.1.1.6 christos /* There's nothing we can do. */
1878 1.1.1.6 christos fetched_error.restore ();
1879 1.1.1.6 christos }
1880 1.1 christos }
1881 1.1 christos }
1882 1.1 christos else
1883 1.1 christos value = value_from_longest (builtin_type_pylong, l);
1884 1.1 christos }
1885 1.1 christos else if (PyFloat_Check (obj))
1886 1.1 christos {
1887 1.1 christos double d = PyFloat_AsDouble (obj);
1888 1.1 christos
1889 1.1 christos if (! PyErr_Occurred ())
1890 1.1.1.7 christos value = value_from_host_double (builtin_type_pyfloat, d);
1891 1.1 christos }
1892 1.1 christos else if (gdbpy_is_string (obj))
1893 1.1 christos {
1894 1.1.1.5 christos gdb::unique_xmalloc_ptr<char> s
1895 1.1.1.5 christos = python_string_to_target_string (obj);
1896 1.1 christos if (s != NULL)
1897 1.1.1.5 christos value = value_cstring (s.get (), strlen (s.get ()),
1898 1.1.1.5 christos builtin_type_pychar);
1899 1.1 christos }
1900 1.1 christos else if (PyObject_TypeCheck (obj, &value_object_type))
1901 1.1 christos value = value_copy (((value_object *) obj)->value);
1902 1.1 christos else if (gdbpy_is_lazy_string (obj))
1903 1.1 christos {
1904 1.1 christos PyObject *result;
1905 1.1 christos
1906 1.1 christos result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst, NULL);
1907 1.1 christos value = value_copy (((value_object *) result)->value);
1908 1.1 christos }
1909 1.1 christos else
1910 1.1 christos PyErr_Format (PyExc_TypeError,
1911 1.1 christos _("Could not convert Python object: %S."), obj);
1912 1.1 christos }
1913 1.1.1.7 christos catch (const gdb_exception &except)
1914 1.1 christos {
1915 1.1.1.6 christos gdbpy_convert_exception (except);
1916 1.1 christos return NULL;
1917 1.1 christos }
1918 1.1 christos
1919 1.1 christos return value;
1920 1.1 christos }
1921 1.1 christos
1922 1.1 christos /* Returns value object in the ARGth position in GDB's history. */
1923 1.1 christos PyObject *
1924 1.1 christos gdbpy_history (PyObject *self, PyObject *args)
1925 1.1 christos {
1926 1.1 christos int i;
1927 1.1 christos struct value *res_val = NULL; /* Initialize to appease gcc warning. */
1928 1.1 christos
1929 1.1 christos if (!PyArg_ParseTuple (args, "i", &i))
1930 1.1 christos return NULL;
1931 1.1 christos
1932 1.1.1.7 christos try
1933 1.1 christos {
1934 1.1 christos res_val = access_value_history (i);
1935 1.1 christos }
1936 1.1.1.7 christos catch (const gdb_exception &except)
1937 1.1.1.3 christos {
1938 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
1939 1.1.1.3 christos }
1940 1.1 christos
1941 1.1 christos return value_to_value_object (res_val);
1942 1.1 christos }
1943 1.1 christos
1944 1.1.1.8 christos /* Add a gdb.Value into GDB's history, and return (as an integer) the
1945 1.1.1.8 christos position of the newly added value. */
1946 1.1.1.8 christos PyObject *
1947 1.1.1.8 christos gdbpy_add_history (PyObject *self, PyObject *args)
1948 1.1.1.8 christos {
1949 1.1.1.8 christos PyObject *value_obj;
1950 1.1.1.8 christos
1951 1.1.1.8 christos if (!PyArg_ParseTuple (args, "O", &value_obj))
1952 1.1.1.8 christos return nullptr;
1953 1.1.1.8 christos
1954 1.1.1.8 christos struct value *value = convert_value_from_python (value_obj);
1955 1.1.1.8 christos if (value == nullptr)
1956 1.1.1.8 christos return nullptr;
1957 1.1.1.8 christos
1958 1.1.1.8 christos try
1959 1.1.1.8 christos {
1960 1.1.1.8 christos int idx = record_latest_value (value);
1961 1.1.1.8 christos return gdb_py_object_from_longest (idx).release ();
1962 1.1.1.8 christos }
1963 1.1.1.8 christos catch (const gdb_exception &except)
1964 1.1.1.8 christos {
1965 1.1.1.8 christos GDB_PY_HANDLE_EXCEPTION (except);
1966 1.1.1.8 christos }
1967 1.1.1.8 christos
1968 1.1.1.8 christos return nullptr;
1969 1.1.1.8 christos }
1970 1.1.1.8 christos
1971 1.1.1.8 christos /* Return an integer, the number of items in GDB's history. */
1972 1.1.1.8 christos
1973 1.1.1.8 christos PyObject *
1974 1.1.1.8 christos gdbpy_history_count (PyObject *self, PyObject *args)
1975 1.1.1.8 christos {
1976 1.1.1.8 christos return gdb_py_object_from_ulongest (value_history_count ()).release ();
1977 1.1.1.8 christos }
1978 1.1.1.8 christos
1979 1.1.1.6 christos /* Return the value of a convenience variable. */
1980 1.1.1.6 christos PyObject *
1981 1.1.1.6 christos gdbpy_convenience_variable (PyObject *self, PyObject *args)
1982 1.1.1.6 christos {
1983 1.1.1.6 christos const char *varname;
1984 1.1.1.6 christos struct value *res_val = NULL;
1985 1.1.1.6 christos
1986 1.1.1.6 christos if (!PyArg_ParseTuple (args, "s", &varname))
1987 1.1.1.6 christos return NULL;
1988 1.1.1.6 christos
1989 1.1.1.7 christos try
1990 1.1.1.6 christos {
1991 1.1.1.6 christos struct internalvar *var = lookup_only_internalvar (varname);
1992 1.1.1.6 christos
1993 1.1.1.6 christos if (var != NULL)
1994 1.1.1.6 christos {
1995 1.1.1.8 christos res_val = value_of_internalvar (gdbpy_enter::get_gdbarch (), var);
1996 1.1.1.7 christos if (value_type (res_val)->code () == TYPE_CODE_VOID)
1997 1.1.1.6 christos res_val = NULL;
1998 1.1.1.6 christos }
1999 1.1.1.6 christos }
2000 1.1.1.7 christos catch (const gdb_exception &except)
2001 1.1.1.6 christos {
2002 1.1.1.6 christos GDB_PY_HANDLE_EXCEPTION (except);
2003 1.1.1.6 christos }
2004 1.1.1.6 christos
2005 1.1.1.6 christos if (res_val == NULL)
2006 1.1.1.6 christos Py_RETURN_NONE;
2007 1.1.1.6 christos
2008 1.1.1.6 christos return value_to_value_object (res_val);
2009 1.1.1.6 christos }
2010 1.1.1.6 christos
2011 1.1.1.6 christos /* Set the value of a convenience variable. */
2012 1.1.1.6 christos PyObject *
2013 1.1.1.6 christos gdbpy_set_convenience_variable (PyObject *self, PyObject *args)
2014 1.1.1.6 christos {
2015 1.1.1.6 christos const char *varname;
2016 1.1.1.6 christos PyObject *value_obj;
2017 1.1.1.6 christos struct value *value = NULL;
2018 1.1.1.6 christos
2019 1.1.1.6 christos if (!PyArg_ParseTuple (args, "sO", &varname, &value_obj))
2020 1.1.1.6 christos return NULL;
2021 1.1.1.6 christos
2022 1.1.1.6 christos /* None means to clear the variable. */
2023 1.1.1.6 christos if (value_obj != Py_None)
2024 1.1.1.6 christos {
2025 1.1.1.6 christos value = convert_value_from_python (value_obj);
2026 1.1.1.6 christos if (value == NULL)
2027 1.1.1.6 christos return NULL;
2028 1.1.1.6 christos }
2029 1.1.1.6 christos
2030 1.1.1.7 christos try
2031 1.1.1.6 christos {
2032 1.1.1.6 christos if (value == NULL)
2033 1.1.1.6 christos {
2034 1.1.1.6 christos struct internalvar *var = lookup_only_internalvar (varname);
2035 1.1.1.6 christos
2036 1.1.1.6 christos if (var != NULL)
2037 1.1.1.6 christos clear_internalvar (var);
2038 1.1.1.6 christos }
2039 1.1.1.6 christos else
2040 1.1.1.6 christos {
2041 1.1.1.6 christos struct internalvar *var = lookup_internalvar (varname);
2042 1.1.1.6 christos
2043 1.1.1.6 christos set_internalvar (var, value);
2044 1.1.1.6 christos }
2045 1.1.1.6 christos }
2046 1.1.1.7 christos catch (const gdb_exception &except)
2047 1.1.1.6 christos {
2048 1.1.1.6 christos GDB_PY_HANDLE_EXCEPTION (except);
2049 1.1.1.6 christos }
2050 1.1.1.6 christos
2051 1.1.1.6 christos Py_RETURN_NONE;
2052 1.1.1.6 christos }
2053 1.1.1.6 christos
2054 1.1 christos /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
2055 1.1 christos
2056 1.1 christos int
2057 1.1 christos gdbpy_is_value_object (PyObject *obj)
2058 1.1 christos {
2059 1.1 christos return PyObject_TypeCheck (obj, &value_object_type);
2060 1.1 christos }
2061 1.1 christos
2062 1.1 christos int
2063 1.1 christos gdbpy_initialize_values (void)
2064 1.1 christos {
2065 1.1 christos if (PyType_Ready (&value_object_type) < 0)
2066 1.1 christos return -1;
2067 1.1 christos
2068 1.1 christos return gdb_pymodule_addobject (gdb_module, "Value",
2069 1.1 christos (PyObject *) &value_object_type);
2070 1.1 christos }
2071 1.1 christos
2072 1.1 christos
2073 1.1 christos
2075 1.1 christos static gdb_PyGetSetDef value_object_getset[] = {
2076 1.1 christos { "address", valpy_get_address, NULL, "The address of the value.",
2077 1.1 christos NULL },
2078 1.1 christos { "is_optimized_out", valpy_get_is_optimized_out, NULL,
2079 1.1 christos "Boolean telling whether the value is optimized "
2080 1.1 christos "out (i.e., not available).",
2081 1.1 christos NULL },
2082 1.1 christos { "type", valpy_get_type, NULL, "Type of the value.", NULL },
2083 1.1 christos { "dynamic_type", valpy_get_dynamic_type, NULL,
2084 1.1 christos "Dynamic type of the value.", NULL },
2085 1.1 christos { "is_lazy", valpy_get_is_lazy, NULL,
2086 1.1 christos "Boolean telling whether the value is lazy (not fetched yet\n\
2087 1.1 christos from the inferior). A lazy value is fetched when needed, or when\n\
2088 1.1 christos the \"fetch_lazy()\" method is called.", NULL },
2089 1.1 christos {NULL} /* Sentinel */
2090 1.1 christos };
2091 1.1 christos
2092 1.1 christos static PyMethodDef value_object_methods[] = {
2093 1.1 christos { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
2094 1.1 christos { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS,
2095 1.1 christos "dynamic_cast (gdb.Type) -> gdb.Value\n\
2096 1.1 christos Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
2097 1.1 christos },
2098 1.1 christos { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS,
2099 1.1 christos "reinterpret_cast (gdb.Type) -> gdb.Value\n\
2100 1.1 christos Cast the value to the supplied type, as if by the C++\n\
2101 1.1 christos reinterpret_cast operator."
2102 1.1 christos },
2103 1.1 christos { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
2104 1.1 christos { "referenced_value", valpy_referenced_value, METH_NOARGS,
2105 1.1.1.5 christos "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
2106 1.1.1.3 christos { "reference_value", valpy_lvalue_reference_value, METH_NOARGS,
2107 1.1.1.5 christos "Return a value of type TYPE_CODE_REF referencing this value." },
2108 1.1.1.5 christos { "rvalue_reference_value", valpy_rvalue_reference_value, METH_NOARGS,
2109 1.1.1.3 christos "Return a value of type TYPE_CODE_RVALUE_REF referencing this value." },
2110 1.1.1.3 christos { "const_value", valpy_const_value, METH_NOARGS,
2111 1.1 christos "Return a 'const' qualied version of the same value." },
2112 1.1 christos { "lazy_string", (PyCFunction) valpy_lazy_string,
2113 1.1 christos METH_VARARGS | METH_KEYWORDS,
2114 1.1 christos "lazy_string ([encoding] [, length]) -> lazy_string\n\
2115 1.1 christos Return a lazy string representation of the value." },
2116 1.1 christos { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
2117 1.1 christos "string ([encoding] [, errors] [, length]) -> string\n\
2118 1.1 christos Return Unicode string representation of the value." },
2119 1.1 christos { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
2120 1.1.1.7 christos "Fetches the value from the inferior, if it was lazy." },
2121 1.1.1.7 christos { "format_string", (PyCFunction) valpy_format_string,
2122 1.1.1.7 christos METH_VARARGS | METH_KEYWORDS,
2123 1.1.1.7 christos "format_string (...) -> string\n\
2124 1.1.1.7 christos Return a string representation of the value using the specified\n\
2125 1.1 christos formatting options" },
2126 1.1 christos {NULL} /* Sentinel */
2127 1.1 christos };
2128 1.1 christos
2129 1.1 christos static PyNumberMethods value_object_as_number = {
2130 1.1 christos valpy_add,
2131 1.1 christos valpy_subtract,
2132 1.1 christos valpy_multiply,
2133 1.1 christos valpy_remainder,
2134 1.1 christos NULL, /* nb_divmod */
2135 1.1 christos valpy_power, /* nb_power */
2136 1.1 christos valpy_negative, /* nb_negative */
2137 1.1 christos valpy_positive, /* nb_positive */
2138 1.1 christos valpy_absolute, /* nb_absolute */
2139 1.1 christos valpy_nonzero, /* nb_nonzero */
2140 1.1 christos valpy_invert, /* nb_invert */
2141 1.1 christos valpy_lsh, /* nb_lshift */
2142 1.1 christos valpy_rsh, /* nb_rshift */
2143 1.1 christos valpy_and, /* nb_and */
2144 1.1 christos valpy_xor, /* nb_xor */
2145 1.1 christos valpy_or, /* nb_or */
2146 1.1 christos valpy_long, /* nb_int */
2147 1.1 christos NULL, /* reserved */
2148 1.1 christos valpy_float, /* nb_float */
2149 1.1 christos NULL, /* nb_inplace_add */
2150 1.1 christos NULL, /* nb_inplace_subtract */
2151 1.1 christos NULL, /* nb_inplace_multiply */
2152 1.1 christos NULL, /* nb_inplace_remainder */
2153 1.1 christos NULL, /* nb_inplace_power */
2154 1.1 christos NULL, /* nb_inplace_lshift */
2155 1.1 christos NULL, /* nb_inplace_rshift */
2156 1.1 christos NULL, /* nb_inplace_and */
2157 1.1 christos NULL, /* nb_inplace_xor */
2158 1.1 christos NULL, /* nb_inplace_or */
2159 1.1.1.4 christos NULL, /* nb_floor_divide */
2160 1.1.1.4 christos valpy_divide, /* nb_true_divide */
2161 1.1.1.4 christos NULL, /* nb_inplace_floor_divide */
2162 1.1.1.4 christos NULL, /* nb_inplace_true_divide */
2163 1.1 christos valpy_long, /* nb_index */
2164 1.1 christos };
2165 1.1 christos
2166 1.1 christos static PyMappingMethods value_object_as_mapping = {
2167 1.1 christos valpy_length,
2168 1.1 christos valpy_getitem,
2169 1.1 christos valpy_setitem
2170 1.1 christos };
2171 1.1 christos
2172 1.1 christos PyTypeObject value_object_type = {
2173 1.1 christos PyVarObject_HEAD_INIT (NULL, 0)
2174 1.1 christos "gdb.Value", /*tp_name*/
2175 1.1 christos sizeof (value_object), /*tp_basicsize*/
2176 1.1 christos 0, /*tp_itemsize*/
2177 1.1 christos valpy_dealloc, /*tp_dealloc*/
2178 1.1 christos 0, /*tp_print*/
2179 1.1 christos 0, /*tp_getattr*/
2180 1.1 christos 0, /*tp_setattr*/
2181 1.1 christos 0, /*tp_compare*/
2182 1.1 christos 0, /*tp_repr*/
2183 1.1 christos &value_object_as_number, /*tp_as_number*/
2184 1.1 christos 0, /*tp_as_sequence*/
2185 1.1 christos &value_object_as_mapping, /*tp_as_mapping*/
2186 1.1 christos valpy_hash, /*tp_hash*/
2187 1.1 christos valpy_call, /*tp_call*/
2188 1.1 christos valpy_str, /*tp_str*/
2189 1.1 christos 0, /*tp_getattro*/
2190 1.1 christos 0, /*tp_setattro*/
2191 1.1 christos 0, /*tp_as_buffer*/
2192 1.1 christos Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES
2193 1.1 christos | Py_TPFLAGS_BASETYPE, /*tp_flags*/
2194 1.1 christos "GDB value object", /* tp_doc */
2195 1.1 christos 0, /* tp_traverse */
2196 1.1 christos 0, /* tp_clear */
2197 1.1 christos valpy_richcompare, /* tp_richcompare */
2198 1.1 christos 0, /* tp_weaklistoffset */
2199 1.1 christos 0, /* tp_iter */
2200 1.1 christos 0, /* tp_iternext */
2201 1.1 christos value_object_methods, /* tp_methods */
2202 1.1 christos 0, /* tp_members */
2203 1.1 christos value_object_getset, /* tp_getset */
2204 1.1 christos 0, /* tp_base */
2205 1.1 christos 0, /* tp_dict */
2206 1.1 christos 0, /* tp_descr_get */
2207 1.1 christos 0, /* tp_descr_set */
2208 1.1.1.8 christos 0, /* tp_dictoffset */
2209 1.1 christos valpy_init, /* tp_init */
2210 1.1.1.8 christos 0, /* tp_alloc */
2211 1.1 christos PyType_GenericNew, /* tp_new */
2212 };
2213