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