Commit | Line | Data |
---|---|---|
7843261b TJB |
1 | /* Python interface to values. |
2 | ||
4c38e0a4 | 3 | Copyright (C) 2008, 2009, 2010 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" | |
08c637de | 21 | #include "gdb_assert.h" |
7843261b TJB |
22 | #include "charset.h" |
23 | #include "value.h" | |
24 | #include "exceptions.h" | |
25 | #include "language.h" | |
26 | #include "dfp.h" | |
79a45b7d | 27 | #include "valprint.h" |
5374244e | 28 | #include "infcall.h" |
f9ffd4bb | 29 | #include "expression.h" |
03f17ccf | 30 | #include "cp-abi.h" |
7843261b | 31 | |
7843261b TJB |
32 | #ifdef HAVE_PYTHON |
33 | ||
34 | #include "python-internal.h" | |
35 | ||
36 | /* Even though Python scalar types directly map to host types, we use | |
37 | target types here to remain consistent with the the values system in | |
38 | GDB (which uses target arithmetic). */ | |
39 | ||
40 | /* Python's integer type corresponds to C's long type. */ | |
d452c4bc | 41 | #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long |
7843261b TJB |
42 | |
43 | /* Python's float type corresponds to C's double type. */ | |
d452c4bc | 44 | #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double |
7843261b TJB |
45 | |
46 | /* Python's long type corresponds to C's long long type. */ | |
d452c4bc | 47 | #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long |
7843261b | 48 | |
595939de PM |
49 | /* Python's long type corresponds to C's long long type. Unsigned version. */ |
50 | #define builtin_type_upylong builtin_type \ | |
51 | (python_gdbarch)->builtin_unsigned_long_long | |
52 | ||
7843261b | 53 | #define builtin_type_pybool \ |
d452c4bc | 54 | language_bool_type (python_language, python_gdbarch) |
7843261b | 55 | |
3b7538c0 | 56 | #define builtin_type_pychar \ |
d452c4bc | 57 | language_string_char_type (python_language, python_gdbarch) |
3b7538c0 | 58 | |
4e7a5ef5 | 59 | typedef struct value_object { |
7843261b | 60 | PyObject_HEAD |
4e7a5ef5 TT |
61 | struct value_object *next; |
62 | struct value_object *prev; | |
7843261b | 63 | struct value *value; |
c0c6f777 | 64 | PyObject *address; |
2c74e833 | 65 | PyObject *type; |
03f17ccf | 66 | PyObject *dynamic_type; |
7843261b TJB |
67 | } value_object; |
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 | ||
7843261b TJB |
76 | /* Called by the Python interpreter when deallocating a value object. */ |
77 | static void | |
78 | valpy_dealloc (PyObject *obj) | |
79 | { | |
80 | value_object *self = (value_object *) obj; | |
81 | ||
4e7a5ef5 TT |
82 | /* Remove SELF from the global list. */ |
83 | if (self->prev) | |
84 | self->prev->next = self->next; | |
85 | else | |
86 | { | |
87 | gdb_assert (values_in_python == self); | |
88 | values_in_python = self->next; | |
89 | } | |
90 | if (self->next) | |
91 | self->next->prev = self->prev; | |
7843261b | 92 | |
77316f4c | 93 | value_free (self->value); |
c0c6f777 TJB |
94 | |
95 | if (self->address) | |
96 | /* Use braces to appease gcc warning. *sigh* */ | |
97 | { | |
98 | Py_DECREF (self->address); | |
99 | } | |
100 | ||
2c74e833 TT |
101 | if (self->type) |
102 | { | |
103 | Py_DECREF (self->type); | |
104 | } | |
105 | ||
03f17ccf TT |
106 | Py_XDECREF (self->dynamic_type); |
107 | ||
7843261b TJB |
108 | self->ob_type->tp_free (self); |
109 | } | |
110 | ||
4e7a5ef5 TT |
111 | /* Helper to push a Value object on the global list. */ |
112 | static void | |
113 | note_value (value_object *value_obj) | |
114 | { | |
115 | value_obj->next = values_in_python; | |
116 | if (value_obj->next) | |
117 | value_obj->next->prev = value_obj; | |
118 | value_obj->prev = NULL; | |
119 | values_in_python = value_obj; | |
120 | } | |
121 | ||
8dc78533 JK |
122 | /* Called when a new gdb.Value object needs to be allocated. Returns NULL on |
123 | error, with a python exception set. */ | |
7843261b TJB |
124 | static PyObject * |
125 | valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords) | |
126 | { | |
127 | struct value *value = NULL; /* Initialize to appease gcc warning. */ | |
128 | value_object *value_obj; | |
7843261b TJB |
129 | |
130 | if (PyTuple_Size (args) != 1) | |
131 | { | |
132 | PyErr_SetString (PyExc_TypeError, _("Value object creation takes only " | |
133 | "1 argument")); | |
134 | return NULL; | |
135 | } | |
136 | ||
137 | value_obj = (value_object *) subtype->tp_alloc (subtype, 1); | |
138 | if (value_obj == NULL) | |
139 | { | |
140 | PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to " | |
141 | "create Value object.")); | |
142 | return NULL; | |
143 | } | |
144 | ||
08c637de TJB |
145 | value = convert_value_from_python (PyTuple_GetItem (args, 0)); |
146 | if (value == NULL) | |
7843261b TJB |
147 | { |
148 | subtype->tp_free (value_obj); | |
08c637de | 149 | return NULL; |
7843261b TJB |
150 | } |
151 | ||
152 | value_obj->value = value; | |
4e7a5ef5 | 153 | value_incref (value); |
c0c6f777 | 154 | value_obj->address = NULL; |
2c74e833 | 155 | value_obj->type = NULL; |
03f17ccf | 156 | value_obj->dynamic_type = NULL; |
4e7a5ef5 | 157 | note_value (value_obj); |
7843261b TJB |
158 | |
159 | return (PyObject *) value_obj; | |
160 | } | |
161 | ||
4e7a5ef5 TT |
162 | /* Iterate over all the Value objects, calling preserve_one_value on |
163 | each. */ | |
164 | void | |
165 | preserve_python_values (struct objfile *objfile, htab_t copied_types) | |
166 | { | |
167 | value_object *iter; | |
168 | ||
169 | for (iter = values_in_python; iter; iter = iter->next) | |
170 | preserve_one_value (iter->value, objfile, copied_types); | |
171 | } | |
172 | ||
7843261b TJB |
173 | /* Given a value of a pointer type, apply the C unary * operator to it. */ |
174 | static PyObject * | |
175 | valpy_dereference (PyObject *self, PyObject *args) | |
176 | { | |
177 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
178 | volatile struct gdb_exception except; | |
179 | ||
180 | TRY_CATCH (except, RETURN_MASK_ALL) | |
181 | { | |
182 | res_val = value_ind (((value_object *) self)->value); | |
183 | } | |
184 | GDB_PY_HANDLE_EXCEPTION (except); | |
185 | ||
186 | return value_to_value_object (res_val); | |
187 | } | |
188 | ||
08c637de TJB |
189 | /* Return "&value". */ |
190 | static PyObject * | |
c0c6f777 | 191 | valpy_get_address (PyObject *self, void *closure) |
08c637de TJB |
192 | { |
193 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
c0c6f777 | 194 | value_object *val_obj = (value_object *) self; |
08c637de TJB |
195 | volatile struct gdb_exception except; |
196 | ||
c0c6f777 | 197 | if (!val_obj->address) |
08c637de | 198 | { |
c0c6f777 TJB |
199 | TRY_CATCH (except, RETURN_MASK_ALL) |
200 | { | |
201 | res_val = value_addr (val_obj->value); | |
202 | } | |
203 | if (except.reason < 0) | |
204 | { | |
205 | val_obj->address = Py_None; | |
206 | Py_INCREF (Py_None); | |
207 | } | |
208 | else | |
209 | val_obj->address = value_to_value_object (res_val); | |
08c637de | 210 | } |
08c637de | 211 | |
c0c6f777 TJB |
212 | Py_INCREF (val_obj->address); |
213 | ||
214 | return val_obj->address; | |
08c637de TJB |
215 | } |
216 | ||
2c74e833 TT |
217 | /* Return type of the value. */ |
218 | static PyObject * | |
219 | valpy_get_type (PyObject *self, void *closure) | |
220 | { | |
221 | value_object *obj = (value_object *) self; | |
d59b6f6c | 222 | |
2c74e833 TT |
223 | if (!obj->type) |
224 | { | |
225 | obj->type = type_to_type_object (value_type (obj->value)); | |
226 | if (!obj->type) | |
03f17ccf | 227 | return NULL; |
2c74e833 TT |
228 | } |
229 | Py_INCREF (obj->type); | |
230 | return obj->type; | |
231 | } | |
232 | ||
03f17ccf TT |
233 | /* Return dynamic type of the value. */ |
234 | ||
235 | static PyObject * | |
236 | valpy_get_dynamic_type (PyObject *self, void *closure) | |
237 | { | |
238 | value_object *obj = (value_object *) self; | |
239 | volatile struct gdb_exception except; | |
240 | struct type *type = NULL; | |
241 | ||
242 | if (obj->dynamic_type != NULL) | |
243 | { | |
244 | Py_INCREF (obj->dynamic_type); | |
245 | return obj->dynamic_type; | |
246 | } | |
247 | ||
248 | TRY_CATCH (except, RETURN_MASK_ALL) | |
249 | { | |
250 | struct value *val = obj->value; | |
251 | ||
252 | type = value_type (val); | |
253 | CHECK_TYPEDEF (type); | |
254 | ||
255 | if (((TYPE_CODE (type) == TYPE_CODE_PTR) | |
256 | || (TYPE_CODE (type) == TYPE_CODE_REF)) | |
257 | && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) | |
258 | { | |
259 | struct value *target; | |
260 | int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR; | |
261 | ||
262 | target = value_ind (val); | |
263 | type = value_rtti_type (target, NULL, NULL, NULL); | |
264 | ||
265 | if (type) | |
266 | { | |
267 | if (was_pointer) | |
268 | type = lookup_pointer_type (type); | |
269 | else | |
270 | type = lookup_reference_type (type); | |
271 | } | |
272 | } | |
273 | else if (TYPE_CODE (type) == TYPE_CODE_CLASS) | |
274 | type = value_rtti_type (val, NULL, NULL, NULL); | |
275 | else | |
276 | { | |
277 | /* Re-use object's static type. */ | |
278 | type = NULL; | |
279 | } | |
280 | } | |
281 | GDB_PY_HANDLE_EXCEPTION (except); | |
282 | ||
283 | if (type == NULL) | |
284 | { | |
285 | /* Ensure that the TYPE field is ready. */ | |
286 | if (!valpy_get_type (self, NULL)) | |
287 | return NULL; | |
288 | /* We don't need to incref here, because valpy_get_type already | |
289 | did it for us. */ | |
290 | obj->dynamic_type = obj->type; | |
291 | } | |
292 | else | |
293 | obj->dynamic_type = type_to_type_object (type); | |
294 | ||
295 | Py_INCREF (obj->dynamic_type); | |
296 | return obj->dynamic_type; | |
297 | } | |
298 | ||
be759fcf PM |
299 | /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) -> |
300 | string. Return a PyObject representing a lazy_string_object type. | |
301 | A lazy string is a pointer to a string with an optional encoding and | |
302 | length. If ENCODING is not given, encoding is set to None. If an | |
303 | ENCODING is provided the encoding parameter is set to ENCODING, but | |
304 | the string is not encoded. If LENGTH is provided then the length | |
305 | parameter is set to LENGTH, otherwise length will be set to -1 (first | |
306 | null of appropriate with). */ | |
307 | static PyObject * | |
308 | valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) | |
309 | { | |
310 | int length = -1; | |
311 | struct value *value = ((value_object *) self)->value; | |
312 | const char *user_encoding = NULL; | |
313 | static char *keywords[] = { "encoding", "length", NULL }; | |
314 | PyObject *str_obj; | |
315 | ||
316 | if (!PyArg_ParseTupleAndKeywords (args, kw, "|si", keywords, | |
317 | &user_encoding, &length)) | |
318 | return NULL; | |
319 | ||
320 | if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR) | |
321 | value = value_ind (value); | |
322 | ||
323 | str_obj = gdbpy_create_lazy_string_object (value_address (value), length, | |
324 | user_encoding, value_type (value)); | |
325 | ||
326 | return (PyObject *) str_obj; | |
327 | } | |
328 | ||
fbb8f299 PM |
329 | /* Implementation of gdb.Value.string ([encoding] [, errors] |
330 | [, length]) -> string. Return Unicode string with value contents. | |
331 | If ENCODING is not given, the string is assumed to be encoded in | |
332 | the target's charset. If LENGTH is provided, only fetch string to | |
333 | the length provided. */ | |
334 | ||
b6cb8e7d | 335 | static PyObject * |
cc924cad | 336 | valpy_string (PyObject *self, PyObject *args, PyObject *kw) |
b6cb8e7d | 337 | { |
f92adf3c | 338 | int length = -1; |
b6cb8e7d TJB |
339 | gdb_byte *buffer; |
340 | struct value *value = ((value_object *) self)->value; | |
341 | volatile struct gdb_exception except; | |
342 | PyObject *unicode; | |
343 | const char *encoding = NULL; | |
344 | const char *errors = NULL; | |
345 | const char *user_encoding = NULL; | |
346 | const char *la_encoding = NULL; | |
96c07c5b | 347 | struct type *char_type; |
31158f0e | 348 | static char *keywords[] = { "encoding", "errors", "length", NULL }; |
b6cb8e7d | 349 | |
fbb8f299 PM |
350 | if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords, |
351 | &user_encoding, &errors, &length)) | |
b6cb8e7d TJB |
352 | return NULL; |
353 | ||
354 | TRY_CATCH (except, RETURN_MASK_ALL) | |
355 | { | |
96c07c5b | 356 | LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding); |
b6cb8e7d TJB |
357 | } |
358 | GDB_PY_HANDLE_EXCEPTION (except); | |
359 | ||
360 | encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding; | |
96c07c5b TT |
361 | unicode = PyUnicode_Decode (buffer, length * TYPE_LENGTH (char_type), |
362 | encoding, errors); | |
b6cb8e7d TJB |
363 | xfree (buffer); |
364 | ||
365 | return unicode; | |
366 | } | |
367 | ||
f9ffd4bb TT |
368 | /* A helper function that implements the various cast operators. */ |
369 | ||
2c74e833 | 370 | static PyObject * |
f9ffd4bb | 371 | valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) |
2c74e833 TT |
372 | { |
373 | PyObject *type_obj; | |
374 | struct type *type; | |
375 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
376 | volatile struct gdb_exception except; | |
377 | ||
378 | if (! PyArg_ParseTuple (args, "O", &type_obj)) | |
379 | return NULL; | |
380 | ||
381 | type = type_object_to_type (type_obj); | |
382 | if (! type) | |
383 | { | |
044c0f87 PM |
384 | PyErr_SetString (PyExc_RuntimeError, |
385 | _("Argument must be a type.")); | |
2c74e833 TT |
386 | return NULL; |
387 | } | |
388 | ||
389 | TRY_CATCH (except, RETURN_MASK_ALL) | |
390 | { | |
f9ffd4bb TT |
391 | struct value *val = ((value_object *) self)->value; |
392 | ||
393 | if (op == UNOP_DYNAMIC_CAST) | |
394 | res_val = value_dynamic_cast (type, val); | |
395 | else if (op == UNOP_REINTERPRET_CAST) | |
396 | res_val = value_reinterpret_cast (type, val); | |
397 | else | |
398 | { | |
399 | gdb_assert (op == UNOP_CAST); | |
400 | res_val = value_cast (type, val); | |
401 | } | |
2c74e833 TT |
402 | } |
403 | GDB_PY_HANDLE_EXCEPTION (except); | |
404 | ||
405 | return value_to_value_object (res_val); | |
406 | } | |
407 | ||
f9ffd4bb TT |
408 | /* Implementation of the "cast" method. */ |
409 | ||
410 | static PyObject * | |
411 | valpy_cast (PyObject *self, PyObject *args) | |
412 | { | |
413 | return valpy_do_cast (self, args, UNOP_CAST); | |
414 | } | |
415 | ||
416 | /* Implementation of the "dynamic_cast" method. */ | |
417 | ||
418 | static PyObject * | |
419 | valpy_dynamic_cast (PyObject *self, PyObject *args) | |
420 | { | |
421 | return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST); | |
422 | } | |
423 | ||
424 | /* Implementation of the "reinterpret_cast" method. */ | |
425 | ||
426 | static PyObject * | |
427 | valpy_reinterpret_cast (PyObject *self, PyObject *args) | |
428 | { | |
429 | return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST); | |
430 | } | |
431 | ||
7843261b TJB |
432 | static Py_ssize_t |
433 | valpy_length (PyObject *self) | |
434 | { | |
435 | /* We don't support getting the number of elements in a struct / class. */ | |
436 | PyErr_SetString (PyExc_NotImplementedError, | |
044c0f87 | 437 | _("Invalid operation on gdb.Value.")); |
7843261b TJB |
438 | return -1; |
439 | } | |
440 | ||
441 | /* Given string name of an element inside structure, return its value | |
8dc78533 | 442 | object. Returns NULL on error, with a python exception set. */ |
7843261b TJB |
443 | static PyObject * |
444 | valpy_getitem (PyObject *self, PyObject *key) | |
445 | { | |
446 | value_object *self_value = (value_object *) self; | |
08c637de | 447 | char *field = NULL; |
570e2b1a | 448 | struct value *res_val = NULL; |
7843261b TJB |
449 | volatile struct gdb_exception except; |
450 | ||
08c637de TJB |
451 | if (gdbpy_is_string (key)) |
452 | { | |
453 | field = python_string_to_host_string (key); | |
454 | if (field == NULL) | |
455 | return NULL; | |
456 | } | |
7843261b TJB |
457 | |
458 | TRY_CATCH (except, RETURN_MASK_ALL) | |
459 | { | |
3c0ed299 | 460 | struct value *tmp = self_value->value; |
08c637de TJB |
461 | |
462 | if (field) | |
463 | res_val = value_struct_elt (&tmp, NULL, field, 0, NULL); | |
464 | else | |
465 | { | |
466 | /* Assume we are attempting an array access, and let the | |
467 | value code throw an exception if the index has an invalid | |
468 | type. */ | |
469 | struct value *idx = convert_value_from_python (key); | |
d59b6f6c | 470 | |
570e2b1a | 471 | if (idx != NULL) |
2e4d963f PM |
472 | { |
473 | /* Check the value's type is something that can be accessed via | |
474 | a subscript. */ | |
475 | struct type *type; | |
d59b6f6c | 476 | |
2e4d963f PM |
477 | tmp = coerce_ref (tmp); |
478 | type = check_typedef (value_type (tmp)); | |
479 | if (TYPE_CODE (type) != TYPE_CODE_ARRAY | |
480 | && TYPE_CODE (type) != TYPE_CODE_PTR) | |
044c0f87 | 481 | error( _("Cannot subscript requested type.")); |
2e4d963f PM |
482 | else |
483 | res_val = value_subscript (tmp, value_as_long (idx)); | |
484 | } | |
08c637de | 485 | } |
7843261b | 486 | } |
570e2b1a | 487 | |
06878dd2 | 488 | xfree (field); |
7843261b TJB |
489 | GDB_PY_HANDLE_EXCEPTION (except); |
490 | ||
06878dd2 | 491 | return res_val ? value_to_value_object (res_val) : NULL; |
7843261b TJB |
492 | } |
493 | ||
494 | static int | |
495 | valpy_setitem (PyObject *self, PyObject *key, PyObject *value) | |
496 | { | |
497 | PyErr_Format (PyExc_NotImplementedError, | |
498 | _("Setting of struct elements is not currently supported.")); | |
499 | return -1; | |
500 | } | |
501 | ||
5374244e | 502 | /* Called by the Python interpreter to perform an inferior function |
8dc78533 | 503 | call on the value. Returns NULL on error, with a python exception set. */ |
5374244e PM |
504 | static PyObject * |
505 | valpy_call (PyObject *self, PyObject *args, PyObject *keywords) | |
506 | { | |
507 | struct value *return_value = NULL; | |
508 | Py_ssize_t args_count; | |
509 | volatile struct gdb_exception except; | |
510 | struct value *function = ((value_object *) self)->value; | |
511 | struct value **vargs = NULL; | |
512 | struct type *ftype = check_typedef (value_type (function)); | |
513 | ||
514 | if (TYPE_CODE (ftype) != TYPE_CODE_FUNC) | |
515 | { | |
516 | PyErr_SetString (PyExc_RuntimeError, | |
517 | _("Value is not callable (not TYPE_CODE_FUNC).")); | |
518 | return NULL; | |
519 | } | |
520 | ||
521 | args_count = PyTuple_Size (args); | |
522 | if (args_count > 0) | |
523 | { | |
524 | int i; | |
525 | ||
526 | vargs = alloca (sizeof (struct value *) * args_count); | |
527 | for (i = 0; i < args_count; i++) | |
528 | { | |
529 | PyObject *item = PyTuple_GetItem (args, i); | |
530 | ||
531 | if (item == NULL) | |
532 | return NULL; | |
533 | ||
534 | vargs[i] = convert_value_from_python (item); | |
535 | if (vargs[i] == NULL) | |
536 | return NULL; | |
537 | } | |
538 | } | |
539 | ||
540 | TRY_CATCH (except, RETURN_MASK_ALL) | |
541 | { | |
542 | return_value = call_function_by_hand (function, args_count, vargs); | |
543 | } | |
544 | GDB_PY_HANDLE_EXCEPTION (except); | |
545 | ||
546 | return value_to_value_object (return_value); | |
547 | } | |
548 | ||
7843261b TJB |
549 | /* Called by the Python interpreter to obtain string representation |
550 | of the object. */ | |
551 | static PyObject * | |
552 | valpy_str (PyObject *self) | |
553 | { | |
554 | char *s = NULL; | |
7843261b TJB |
555 | struct ui_file *stb; |
556 | struct cleanup *old_chain; | |
557 | PyObject *result; | |
79a45b7d | 558 | struct value_print_options opts; |
7843261b TJB |
559 | volatile struct gdb_exception except; |
560 | ||
79a45b7d TT |
561 | get_user_print_options (&opts); |
562 | opts.deref_ref = 0; | |
563 | ||
7843261b TJB |
564 | stb = mem_fileopen (); |
565 | old_chain = make_cleanup_ui_file_delete (stb); | |
566 | ||
567 | TRY_CATCH (except, RETURN_MASK_ALL) | |
568 | { | |
79a45b7d | 569 | common_val_print (((value_object *) self)->value, stb, 0, |
d452c4bc | 570 | &opts, python_language); |
759ef836 | 571 | s = ui_file_xstrdup (stb, NULL); |
7843261b TJB |
572 | } |
573 | GDB_PY_HANDLE_EXCEPTION (except); | |
574 | ||
575 | do_cleanups (old_chain); | |
576 | ||
577 | result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL); | |
578 | xfree (s); | |
579 | ||
580 | return result; | |
581 | } | |
582 | ||
def2b000 TJB |
583 | /* Implements gdb.Value.is_optimized_out. */ |
584 | static PyObject * | |
585 | valpy_get_is_optimized_out (PyObject *self, void *closure) | |
586 | { | |
587 | struct value *value = ((value_object *) self)->value; | |
588 | ||
589 | if (value_optimized_out (value)) | |
590 | Py_RETURN_TRUE; | |
591 | ||
592 | Py_RETURN_FALSE; | |
593 | } | |
594 | ||
88d4aea7 PM |
595 | /* Calculate and return the address of the PyObject as the value of |
596 | the builtin __hash__ call. */ | |
597 | static long | |
598 | valpy_hash (PyObject *self) | |
599 | { | |
600 | return (long) (intptr_t) self; | |
601 | } | |
602 | ||
7843261b TJB |
603 | enum valpy_opcode |
604 | { | |
605 | VALPY_ADD, | |
606 | VALPY_SUB, | |
607 | VALPY_MUL, | |
608 | VALPY_DIV, | |
609 | VALPY_REM, | |
08c637de TJB |
610 | VALPY_POW, |
611 | VALPY_LSH, | |
612 | VALPY_RSH, | |
613 | VALPY_BITAND, | |
614 | VALPY_BITOR, | |
615 | VALPY_BITXOR | |
7843261b TJB |
616 | }; |
617 | ||
618 | /* If TYPE is a reference, return the target; otherwise return TYPE. */ | |
619 | #define STRIP_REFERENCE(TYPE) \ | |
620 | ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE)) | |
621 | ||
622 | /* Returns a value object which is the result of applying the operation | |
8dc78533 JK |
623 | specified by OPCODE to the given arguments. Returns NULL on error, with |
624 | a python exception set. */ | |
7843261b TJB |
625 | static PyObject * |
626 | valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) | |
627 | { | |
628 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
629 | volatile struct gdb_exception except; | |
630 | ||
631 | TRY_CATCH (except, RETURN_MASK_ALL) | |
632 | { | |
633 | struct value *arg1, *arg2; | |
634 | ||
635 | /* If the gdb.Value object is the second operand, then it will be passed | |
636 | to us as the OTHER argument, and SELF will be an entirely different | |
637 | kind of object, altogether. Because of this, we can't assume self is | |
638 | a gdb.Value object and need to convert it from python as well. */ | |
639 | arg1 = convert_value_from_python (self); | |
08c637de | 640 | if (arg1 == NULL) |
cc924cad | 641 | break; |
08c637de | 642 | |
7843261b | 643 | arg2 = convert_value_from_python (other); |
08c637de | 644 | if (arg2 == NULL) |
cc924cad | 645 | break; |
7843261b TJB |
646 | |
647 | switch (opcode) | |
648 | { | |
649 | case VALPY_ADD: | |
650 | { | |
651 | struct type *ltype = value_type (arg1); | |
652 | struct type *rtype = value_type (arg2); | |
653 | ||
654 | CHECK_TYPEDEF (ltype); | |
655 | ltype = STRIP_REFERENCE (ltype); | |
656 | CHECK_TYPEDEF (rtype); | |
657 | rtype = STRIP_REFERENCE (rtype); | |
658 | ||
2497b498 UW |
659 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR |
660 | && is_integral_type (rtype)) | |
661 | res_val = value_ptradd (arg1, value_as_long (arg2)); | |
662 | else if (TYPE_CODE (rtype) == TYPE_CODE_PTR | |
663 | && is_integral_type (ltype)) | |
664 | res_val = value_ptradd (arg2, value_as_long (arg1)); | |
7843261b TJB |
665 | else |
666 | res_val = value_binop (arg1, arg2, BINOP_ADD); | |
667 | } | |
668 | break; | |
669 | case VALPY_SUB: | |
670 | { | |
671 | struct type *ltype = value_type (arg1); | |
672 | struct type *rtype = value_type (arg2); | |
673 | ||
674 | CHECK_TYPEDEF (ltype); | |
675 | ltype = STRIP_REFERENCE (ltype); | |
676 | CHECK_TYPEDEF (rtype); | |
677 | rtype = STRIP_REFERENCE (rtype); | |
678 | ||
2497b498 UW |
679 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR |
680 | && TYPE_CODE (rtype) == TYPE_CODE_PTR) | |
681 | /* A ptrdiff_t for the target would be preferable here. */ | |
682 | res_val = value_from_longest (builtin_type_pyint, | |
683 | value_ptrdiff (arg1, arg2)); | |
684 | else if (TYPE_CODE (ltype) == TYPE_CODE_PTR | |
685 | && is_integral_type (rtype)) | |
686 | res_val = value_ptradd (arg1, - value_as_long (arg2)); | |
7843261b TJB |
687 | else |
688 | res_val = value_binop (arg1, arg2, BINOP_SUB); | |
689 | } | |
690 | break; | |
691 | case VALPY_MUL: | |
692 | res_val = value_binop (arg1, arg2, BINOP_MUL); | |
693 | break; | |
694 | case VALPY_DIV: | |
695 | res_val = value_binop (arg1, arg2, BINOP_DIV); | |
696 | break; | |
697 | case VALPY_REM: | |
698 | res_val = value_binop (arg1, arg2, BINOP_REM); | |
699 | break; | |
700 | case VALPY_POW: | |
701 | res_val = value_binop (arg1, arg2, BINOP_EXP); | |
702 | break; | |
08c637de TJB |
703 | case VALPY_LSH: |
704 | res_val = value_binop (arg1, arg2, BINOP_LSH); | |
705 | break; | |
706 | case VALPY_RSH: | |
707 | res_val = value_binop (arg1, arg2, BINOP_RSH); | |
708 | break; | |
709 | case VALPY_BITAND: | |
710 | res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND); | |
711 | break; | |
712 | case VALPY_BITOR: | |
713 | res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR); | |
714 | break; | |
715 | case VALPY_BITXOR: | |
716 | res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR); | |
717 | break; | |
7843261b TJB |
718 | } |
719 | } | |
720 | GDB_PY_HANDLE_EXCEPTION (except); | |
721 | ||
cc924cad | 722 | return res_val ? value_to_value_object (res_val) : NULL; |
7843261b TJB |
723 | } |
724 | ||
725 | static PyObject * | |
726 | valpy_add (PyObject *self, PyObject *other) | |
727 | { | |
728 | return valpy_binop (VALPY_ADD, self, other); | |
729 | } | |
730 | ||
731 | static PyObject * | |
732 | valpy_subtract (PyObject *self, PyObject *other) | |
733 | { | |
734 | return valpy_binop (VALPY_SUB, self, other); | |
735 | } | |
736 | ||
737 | static PyObject * | |
738 | valpy_multiply (PyObject *self, PyObject *other) | |
739 | { | |
740 | return valpy_binop (VALPY_MUL, self, other); | |
741 | } | |
742 | ||
743 | static PyObject * | |
744 | valpy_divide (PyObject *self, PyObject *other) | |
745 | { | |
746 | return valpy_binop (VALPY_DIV, self, other); | |
747 | } | |
748 | ||
749 | static PyObject * | |
750 | valpy_remainder (PyObject *self, PyObject *other) | |
751 | { | |
752 | return valpy_binop (VALPY_REM, self, other); | |
753 | } | |
754 | ||
755 | static PyObject * | |
756 | valpy_power (PyObject *self, PyObject *other, PyObject *unused) | |
757 | { | |
758 | /* We don't support the ternary form of pow. I don't know how to express | |
759 | that, so let's just throw NotImplementedError to at least do something | |
760 | about it. */ | |
761 | if (unused != Py_None) | |
762 | { | |
763 | PyErr_SetString (PyExc_NotImplementedError, | |
764 | "Invalid operation on gdb.Value."); | |
765 | return NULL; | |
766 | } | |
767 | ||
768 | return valpy_binop (VALPY_POW, self, other); | |
769 | } | |
770 | ||
771 | static PyObject * | |
772 | valpy_negative (PyObject *self) | |
773 | { | |
774 | struct value *val = NULL; | |
775 | volatile struct gdb_exception except; | |
776 | ||
777 | TRY_CATCH (except, RETURN_MASK_ALL) | |
778 | { | |
779 | val = value_neg (((value_object *) self)->value); | |
780 | } | |
781 | GDB_PY_HANDLE_EXCEPTION (except); | |
782 | ||
783 | return value_to_value_object (val); | |
784 | } | |
785 | ||
786 | static PyObject * | |
787 | valpy_positive (PyObject *self) | |
788 | { | |
4e7a5ef5 | 789 | return value_to_value_object (((value_object *) self)->value); |
7843261b TJB |
790 | } |
791 | ||
792 | static PyObject * | |
793 | valpy_absolute (PyObject *self) | |
794 | { | |
22601c15 | 795 | struct value *value = ((value_object *) self)->value; |
d59b6f6c | 796 | |
22601c15 | 797 | if (value_less (value, value_zero (value_type (value), not_lval))) |
7843261b TJB |
798 | return valpy_negative (self); |
799 | else | |
800 | return valpy_positive (self); | |
801 | } | |
802 | ||
803 | /* Implements boolean evaluation of gdb.Value. */ | |
804 | static int | |
805 | valpy_nonzero (PyObject *self) | |
806 | { | |
807 | value_object *self_value = (value_object *) self; | |
808 | struct type *type; | |
809 | ||
810 | type = check_typedef (value_type (self_value->value)); | |
811 | ||
812 | if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR) | |
813 | return !!value_as_long (self_value->value); | |
814 | else if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
815 | return value_as_double (self_value->value) != 0; | |
816 | else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
817 | return !decimal_is_zero (value_contents (self_value->value), | |
e17a4113 UW |
818 | TYPE_LENGTH (type), |
819 | gdbarch_byte_order (get_type_arch (type))); | |
7843261b | 820 | else |
96f73ba5 TT |
821 | /* All other values are True. */ |
822 | return 1; | |
7843261b TJB |
823 | } |
824 | ||
08c637de | 825 | /* Implements ~ for value objects. */ |
7843261b | 826 | static PyObject * |
08c637de | 827 | valpy_invert (PyObject *self) |
7843261b | 828 | { |
08c637de | 829 | struct value *val = NULL; |
7843261b TJB |
830 | volatile struct gdb_exception except; |
831 | ||
08c637de | 832 | TRY_CATCH (except, RETURN_MASK_ALL) |
7843261b | 833 | { |
08c637de TJB |
834 | val = value_complement (((value_object *) self)->value); |
835 | } | |
836 | GDB_PY_HANDLE_EXCEPTION (except); | |
7843261b | 837 | |
08c637de TJB |
838 | return value_to_value_object (val); |
839 | } | |
7843261b | 840 | |
08c637de TJB |
841 | /* Implements left shift for value objects. */ |
842 | static PyObject * | |
843 | valpy_lsh (PyObject *self, PyObject *other) | |
844 | { | |
845 | return valpy_binop (VALPY_LSH, self, other); | |
846 | } | |
7843261b | 847 | |
08c637de TJB |
848 | /* Implements right shift for value objects. */ |
849 | static PyObject * | |
850 | valpy_rsh (PyObject *self, PyObject *other) | |
851 | { | |
852 | return valpy_binop (VALPY_RSH, self, other); | |
853 | } | |
7843261b | 854 | |
08c637de TJB |
855 | /* Implements bitwise and for value objects. */ |
856 | static PyObject * | |
857 | valpy_and (PyObject *self, PyObject *other) | |
858 | { | |
859 | return valpy_binop (VALPY_BITAND, self, other); | |
860 | } | |
7843261b | 861 | |
08c637de TJB |
862 | /* Implements bitwise or for value objects. */ |
863 | static PyObject * | |
864 | valpy_or (PyObject *self, PyObject *other) | |
865 | { | |
866 | return valpy_binop (VALPY_BITOR, self, other); | |
867 | } | |
868 | ||
869 | /* Implements bitwise xor for value objects. */ | |
870 | static PyObject * | |
871 | valpy_xor (PyObject *self, PyObject *other) | |
872 | { | |
873 | return valpy_binop (VALPY_BITXOR, self, other); | |
874 | } | |
875 | ||
8dc78533 JK |
876 | /* Implements comparison operations for value objects. Returns NULL on error, |
877 | with a python exception set. */ | |
08c637de TJB |
878 | static PyObject * |
879 | valpy_richcompare (PyObject *self, PyObject *other, int op) | |
880 | { | |
881 | int result = 0; | |
882 | struct value *value_other; | |
883 | volatile struct gdb_exception except; | |
884 | ||
885 | if (other == Py_None) | |
7843261b TJB |
886 | /* Comparing with None is special. From what I can tell, in Python |
887 | None is smaller than anything else. */ | |
888 | switch (op) { | |
889 | case Py_LT: | |
890 | case Py_LE: | |
891 | case Py_EQ: | |
892 | Py_RETURN_FALSE; | |
893 | case Py_NE: | |
894 | case Py_GT: | |
895 | case Py_GE: | |
896 | Py_RETURN_TRUE; | |
897 | default: | |
898 | /* Can't happen. */ | |
899 | PyErr_SetString (PyExc_NotImplementedError, | |
044c0f87 | 900 | _("Invalid operation on gdb.Value.")); |
7843261b TJB |
901 | return NULL; |
902 | } | |
7843261b TJB |
903 | |
904 | TRY_CATCH (except, RETURN_MASK_ALL) | |
905 | { | |
08c637de TJB |
906 | value_other = convert_value_from_python (other); |
907 | if (value_other == NULL) | |
f02779d8 TT |
908 | { |
909 | result = -1; | |
910 | break; | |
911 | } | |
08c637de | 912 | |
7843261b TJB |
913 | switch (op) { |
914 | case Py_LT: | |
915 | result = value_less (((value_object *) self)->value, value_other); | |
916 | break; | |
917 | case Py_LE: | |
918 | result = value_less (((value_object *) self)->value, value_other) | |
919 | || value_equal (((value_object *) self)->value, value_other); | |
920 | break; | |
921 | case Py_EQ: | |
922 | result = value_equal (((value_object *) self)->value, value_other); | |
923 | break; | |
924 | case Py_NE: | |
925 | result = !value_equal (((value_object *) self)->value, value_other); | |
926 | break; | |
927 | case Py_GT: | |
928 | result = value_less (value_other, ((value_object *) self)->value); | |
929 | break; | |
930 | case Py_GE: | |
931 | result = value_less (value_other, ((value_object *) self)->value) | |
932 | || value_equal (((value_object *) self)->value, value_other); | |
933 | break; | |
934 | default: | |
935 | /* Can't happen. */ | |
936 | PyErr_SetString (PyExc_NotImplementedError, | |
044c0f87 | 937 | _("Invalid operation on gdb.Value.")); |
f02779d8 TT |
938 | result = -1; |
939 | break; | |
7843261b TJB |
940 | } |
941 | } | |
942 | GDB_PY_HANDLE_EXCEPTION (except); | |
943 | ||
f02779d8 TT |
944 | /* In this case, the Python exception has already been set. */ |
945 | if (result < 0) | |
946 | return NULL; | |
947 | ||
7843261b TJB |
948 | if (result == 1) |
949 | Py_RETURN_TRUE; | |
950 | ||
951 | Py_RETURN_FALSE; | |
952 | } | |
953 | ||
08c637de TJB |
954 | /* Helper function to determine if a type is "int-like". */ |
955 | static int | |
956 | is_intlike (struct type *type, int ptr_ok) | |
957 | { | |
958 | CHECK_TYPEDEF (type); | |
959 | return (TYPE_CODE (type) == TYPE_CODE_INT | |
960 | || TYPE_CODE (type) == TYPE_CODE_ENUM | |
961 | || TYPE_CODE (type) == TYPE_CODE_BOOL | |
962 | || TYPE_CODE (type) == TYPE_CODE_CHAR | |
963 | || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR)); | |
964 | } | |
965 | ||
966 | /* Implements conversion to int. */ | |
967 | static PyObject * | |
968 | valpy_int (PyObject *self) | |
969 | { | |
970 | struct value *value = ((value_object *) self)->value; | |
971 | struct type *type = value_type (value); | |
972 | LONGEST l = 0; | |
973 | volatile struct gdb_exception except; | |
974 | ||
975 | CHECK_TYPEDEF (type); | |
976 | if (!is_intlike (type, 0)) | |
977 | { | |
044c0f87 PM |
978 | PyErr_SetString (PyExc_RuntimeError, |
979 | _("Cannot convert value to int.")); | |
08c637de TJB |
980 | return NULL; |
981 | } | |
982 | ||
983 | TRY_CATCH (except, RETURN_MASK_ALL) | |
984 | { | |
985 | l = value_as_long (value); | |
986 | } | |
987 | GDB_PY_HANDLE_EXCEPTION (except); | |
988 | ||
89fa5381 TT |
989 | #ifdef HAVE_LONG_LONG /* Defined by Python. */ |
990 | /* If we have 'long long', and the value overflows a 'long', use a | |
991 | Python Long; otherwise use a Python Int. */ | |
992 | if (sizeof (l) > sizeof (long) && (l > PyInt_GetMax () | |
993 | || l < (- (LONGEST) PyInt_GetMax ()) - 1)) | |
994 | return PyLong_FromLongLong (l); | |
995 | #endif | |
08c637de TJB |
996 | return PyInt_FromLong (l); |
997 | } | |
998 | ||
999 | /* Implements conversion to long. */ | |
1000 | static PyObject * | |
1001 | valpy_long (PyObject *self) | |
1002 | { | |
1003 | struct value *value = ((value_object *) self)->value; | |
1004 | struct type *type = value_type (value); | |
1005 | LONGEST l = 0; | |
1006 | volatile struct gdb_exception except; | |
1007 | ||
1008 | if (!is_intlike (type, 1)) | |
1009 | { | |
044c0f87 PM |
1010 | PyErr_SetString (PyExc_RuntimeError, |
1011 | _("Cannot convert value to long.")); | |
08c637de TJB |
1012 | return NULL; |
1013 | } | |
1014 | ||
1015 | TRY_CATCH (except, RETURN_MASK_ALL) | |
1016 | { | |
1017 | l = value_as_long (value); | |
1018 | } | |
1019 | GDB_PY_HANDLE_EXCEPTION (except); | |
1020 | ||
89fa5381 TT |
1021 | #ifdef HAVE_LONG_LONG /* Defined by Python. */ |
1022 | return PyLong_FromLongLong (l); | |
1023 | #else | |
08c637de | 1024 | return PyLong_FromLong (l); |
89fa5381 | 1025 | #endif |
08c637de TJB |
1026 | } |
1027 | ||
1028 | /* Implements conversion to float. */ | |
1029 | static PyObject * | |
1030 | valpy_float (PyObject *self) | |
1031 | { | |
1032 | struct value *value = ((value_object *) self)->value; | |
1033 | struct type *type = value_type (value); | |
1034 | double d = 0; | |
1035 | volatile struct gdb_exception except; | |
1036 | ||
1037 | CHECK_TYPEDEF (type); | |
1038 | if (TYPE_CODE (type) != TYPE_CODE_FLT) | |
1039 | { | |
044c0f87 PM |
1040 | PyErr_SetString (PyExc_RuntimeError, |
1041 | _("Cannot convert value to float.")); | |
08c637de TJB |
1042 | return NULL; |
1043 | } | |
1044 | ||
1045 | TRY_CATCH (except, RETURN_MASK_ALL) | |
1046 | { | |
1047 | d = value_as_double (value); | |
1048 | } | |
1049 | GDB_PY_HANDLE_EXCEPTION (except); | |
1050 | ||
1051 | return PyFloat_FromDouble (d); | |
1052 | } | |
1053 | ||
7843261b TJB |
1054 | /* Returns an object for a value which is released from the all_values chain, |
1055 | so its lifetime is not bound to the execution of a command. */ | |
1056 | PyObject * | |
1057 | value_to_value_object (struct value *val) | |
1058 | { | |
1059 | value_object *val_obj; | |
1060 | ||
1061 | val_obj = PyObject_New (value_object, &value_object_type); | |
1062 | if (val_obj != NULL) | |
1063 | { | |
1064 | val_obj->value = val; | |
4e7a5ef5 | 1065 | value_incref (val); |
c0c6f777 | 1066 | val_obj->address = NULL; |
2c74e833 | 1067 | val_obj->type = NULL; |
03f17ccf | 1068 | val_obj->dynamic_type = NULL; |
4e7a5ef5 | 1069 | note_value (val_obj); |
7843261b TJB |
1070 | } |
1071 | ||
1072 | return (PyObject *) val_obj; | |
1073 | } | |
1074 | ||
4e7a5ef5 TT |
1075 | /* Returns a borrowed reference to the struct value corresponding to |
1076 | the given value object. */ | |
a6bac58e TT |
1077 | struct value * |
1078 | value_object_to_value (PyObject *self) | |
1079 | { | |
1080 | value_object *real; | |
d59b6f6c | 1081 | |
a6bac58e TT |
1082 | if (! PyObject_TypeCheck (self, &value_object_type)) |
1083 | return NULL; | |
1084 | real = (value_object *) self; | |
1085 | return real->value; | |
1086 | } | |
1087 | ||
7843261b | 1088 | /* Try to convert a Python value to a gdb value. If the value cannot |
4e7a5ef5 TT |
1089 | be converted, set a Python exception and return NULL. Returns a |
1090 | reference to a new value on the all_values chain. */ | |
7843261b TJB |
1091 | |
1092 | struct value * | |
1093 | convert_value_from_python (PyObject *obj) | |
1094 | { | |
1095 | struct value *value = NULL; /* -Wall */ | |
7843261b | 1096 | struct cleanup *old; |
08c637de TJB |
1097 | volatile struct gdb_exception except; |
1098 | int cmp; | |
7843261b | 1099 | |
08c637de | 1100 | gdb_assert (obj != NULL); |
7843261b | 1101 | |
08c637de | 1102 | TRY_CATCH (except, RETURN_MASK_ALL) |
7843261b | 1103 | { |
08c637de TJB |
1104 | if (PyBool_Check (obj)) |
1105 | { | |
1106 | cmp = PyObject_IsTrue (obj); | |
1107 | if (cmp >= 0) | |
1108 | value = value_from_longest (builtin_type_pybool, cmp); | |
1109 | } | |
1110 | else if (PyInt_Check (obj)) | |
1111 | { | |
1112 | long l = PyInt_AsLong (obj); | |
7843261b | 1113 | |
08c637de TJB |
1114 | if (! PyErr_Occurred ()) |
1115 | value = value_from_longest (builtin_type_pyint, l); | |
1116 | } | |
1117 | else if (PyLong_Check (obj)) | |
1118 | { | |
1119 | LONGEST l = PyLong_AsLongLong (obj); | |
7843261b | 1120 | |
595939de PM |
1121 | if (PyErr_Occurred ()) |
1122 | { | |
1123 | /* If the error was an overflow, we can try converting to | |
1124 | ULONGEST instead. */ | |
1125 | if (PyErr_ExceptionMatches (PyExc_OverflowError)) | |
1126 | { | |
1127 | PyObject *etype, *evalue, *etraceback, *zero; | |
1128 | ||
1129 | PyErr_Fetch (&etype, &evalue, &etraceback); | |
1130 | zero = PyInt_FromLong (0); | |
1131 | ||
1132 | /* Check whether obj is positive. */ | |
1133 | if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0) | |
1134 | { | |
1135 | ULONGEST ul; | |
1136 | ||
1137 | ul = PyLong_AsUnsignedLongLong (obj); | |
1138 | if (! PyErr_Occurred ()) | |
1139 | value = value_from_ulongest (builtin_type_upylong, ul); | |
1140 | } | |
1141 | else | |
1142 | /* There's nothing we can do. */ | |
1143 | PyErr_Restore (etype, evalue, etraceback); | |
1144 | ||
1145 | Py_DECREF (zero); | |
1146 | } | |
1147 | } | |
1148 | else | |
08c637de TJB |
1149 | value = value_from_longest (builtin_type_pylong, l); |
1150 | } | |
1151 | else if (PyFloat_Check (obj)) | |
1152 | { | |
1153 | double d = PyFloat_AsDouble (obj); | |
7843261b | 1154 | |
08c637de TJB |
1155 | if (! PyErr_Occurred ()) |
1156 | value = value_from_double (builtin_type_pyfloat, d); | |
1157 | } | |
1158 | else if (gdbpy_is_string (obj)) | |
1159 | { | |
1160 | char *s; | |
1161 | ||
1162 | s = python_string_to_target_string (obj); | |
1163 | if (s != NULL) | |
1164 | { | |
1165 | old = make_cleanup (xfree, s); | |
3b7538c0 | 1166 | value = value_cstring (s, strlen (s), builtin_type_pychar); |
08c637de TJB |
1167 | do_cleanups (old); |
1168 | } | |
1169 | } | |
1170 | else if (PyObject_TypeCheck (obj, &value_object_type)) | |
cc924cad | 1171 | value = value_copy (((value_object *) obj)->value); |
be759fcf PM |
1172 | else if (gdbpy_is_lazy_string (obj)) |
1173 | { | |
1174 | PyObject *result; | |
1175 | PyObject *function = PyString_FromString ("value"); | |
d59b6f6c | 1176 | |
be759fcf PM |
1177 | result = PyObject_CallMethodObjArgs (obj, function, NULL); |
1178 | value = value_copy (((value_object *) result)->value); | |
1179 | } | |
08c637de | 1180 | else |
044c0f87 | 1181 | PyErr_Format (PyExc_TypeError, _("Could not convert Python object: %s."), |
08c637de TJB |
1182 | PyString_AsString (PyObject_Str (obj))); |
1183 | } | |
1184 | if (except.reason < 0) | |
1185 | { | |
1186 | PyErr_Format (except.reason == RETURN_QUIT | |
1187 | ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, | |
1188 | "%s", except.message); | |
1189 | return NULL; | |
1190 | } | |
7843261b TJB |
1191 | |
1192 | return value; | |
1193 | } | |
1194 | ||
1195 | /* Returns value object in the ARGth position in GDB's history. */ | |
1196 | PyObject * | |
08c637de | 1197 | gdbpy_history (PyObject *self, PyObject *args) |
7843261b TJB |
1198 | { |
1199 | int i; | |
1200 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
1201 | volatile struct gdb_exception except; | |
1202 | ||
1203 | if (!PyArg_ParseTuple (args, "i", &i)) | |
1204 | return NULL; | |
1205 | ||
1206 | TRY_CATCH (except, RETURN_MASK_ALL) | |
1207 | { | |
1208 | res_val = access_value_history (i); | |
1209 | } | |
1210 | GDB_PY_HANDLE_EXCEPTION (except); | |
1211 | ||
1212 | return value_to_value_object (res_val); | |
1213 | } | |
1214 | ||
595939de PM |
1215 | /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */ |
1216 | ||
1217 | int | |
1218 | gdbpy_is_value_object (PyObject *obj) | |
1219 | { | |
1220 | return PyObject_TypeCheck (obj, &value_object_type); | |
1221 | } | |
1222 | ||
7843261b TJB |
1223 | void |
1224 | gdbpy_initialize_values (void) | |
1225 | { | |
7843261b TJB |
1226 | if (PyType_Ready (&value_object_type) < 0) |
1227 | return; | |
1228 | ||
1229 | Py_INCREF (&value_object_type); | |
1230 | PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type); | |
1231 | ||
1232 | values_in_python = NULL; | |
1233 | } | |
1234 | ||
2c74e833 TT |
1235 | \f |
1236 | ||
def2b000 | 1237 | static PyGetSetDef value_object_getset[] = { |
c0c6f777 TJB |
1238 | { "address", valpy_get_address, NULL, "The address of the value.", |
1239 | NULL }, | |
def2b000 TJB |
1240 | { "is_optimized_out", valpy_get_is_optimized_out, NULL, |
1241 | "Boolean telling whether the value is optimized out (i.e., not available).", | |
1242 | NULL }, | |
2c74e833 | 1243 | { "type", valpy_get_type, NULL, "Type of the value.", NULL }, |
03f17ccf TT |
1244 | { "dynamic_type", valpy_get_dynamic_type, NULL, |
1245 | "Dynamic type of the value.", NULL }, | |
def2b000 TJB |
1246 | {NULL} /* Sentinel */ |
1247 | }; | |
1248 | ||
f9176c46 | 1249 | static PyMethodDef value_object_methods[] = { |
2c74e833 | 1250 | { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." }, |
f9ffd4bb TT |
1251 | { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS, |
1252 | "dynamic_cast (gdb.Type) -> gdb.Value\n\ | |
1253 | Cast the value to the supplied type, as if by the C++ dynamic_cast operator." | |
1254 | }, | |
1255 | { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS, | |
1256 | "reinterpret_cast (gdb.Type) -> gdb.Value\n\ | |
1257 | Cast the value to the supplied type, as if by the C++\n\ | |
1258 | reinterpret_cast operator." | |
1259 | }, | |
f9176c46 | 1260 | { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, |
be759fcf PM |
1261 | { "lazy_string", (PyCFunction) valpy_lazy_string, METH_VARARGS | METH_KEYWORDS, |
1262 | "lazy_string ([encoding] [, length]) -> lazy_string\n\ | |
1263 | Return a lazy string representation of the value." }, | |
cc924cad | 1264 | { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS, |
fbb8f299 | 1265 | "string ([encoding] [, errors] [, length]) -> string\n\ |
cc924cad | 1266 | Return Unicode string representation of the value." }, |
f9176c46 PA |
1267 | {NULL} /* Sentinel */ |
1268 | }; | |
1269 | ||
1270 | static PyNumberMethods value_object_as_number = { | |
1271 | valpy_add, | |
1272 | valpy_subtract, | |
1273 | valpy_multiply, | |
1274 | valpy_divide, | |
1275 | valpy_remainder, | |
1276 | NULL, /* nb_divmod */ | |
1277 | valpy_power, /* nb_power */ | |
1278 | valpy_negative, /* nb_negative */ | |
1279 | valpy_positive, /* nb_positive */ | |
1280 | valpy_absolute, /* nb_absolute */ | |
08c637de TJB |
1281 | valpy_nonzero, /* nb_nonzero */ |
1282 | valpy_invert, /* nb_invert */ | |
1283 | valpy_lsh, /* nb_lshift */ | |
1284 | valpy_rsh, /* nb_rshift */ | |
1285 | valpy_and, /* nb_and */ | |
1286 | valpy_xor, /* nb_xor */ | |
1287 | valpy_or, /* nb_or */ | |
1288 | NULL, /* nb_coerce */ | |
1289 | valpy_int, /* nb_int */ | |
1290 | valpy_long, /* nb_long */ | |
1291 | valpy_float, /* nb_float */ | |
1292 | NULL, /* nb_oct */ | |
1293 | NULL /* nb_hex */ | |
f9176c46 PA |
1294 | }; |
1295 | ||
1296 | static PyMappingMethods value_object_as_mapping = { | |
1297 | valpy_length, | |
1298 | valpy_getitem, | |
1299 | valpy_setitem | |
1300 | }; | |
1301 | ||
1302 | PyTypeObject value_object_type = { | |
1303 | PyObject_HEAD_INIT (NULL) | |
1304 | 0, /*ob_size*/ | |
1305 | "gdb.Value", /*tp_name*/ | |
1306 | sizeof (value_object), /*tp_basicsize*/ | |
1307 | 0, /*tp_itemsize*/ | |
1308 | valpy_dealloc, /*tp_dealloc*/ | |
1309 | 0, /*tp_print*/ | |
1310 | 0, /*tp_getattr*/ | |
1311 | 0, /*tp_setattr*/ | |
1312 | 0, /*tp_compare*/ | |
1313 | 0, /*tp_repr*/ | |
1314 | &value_object_as_number, /*tp_as_number*/ | |
1315 | 0, /*tp_as_sequence*/ | |
1316 | &value_object_as_mapping, /*tp_as_mapping*/ | |
88d4aea7 | 1317 | valpy_hash, /*tp_hash*/ |
5374244e | 1318 | valpy_call, /*tp_call*/ |
f9176c46 PA |
1319 | valpy_str, /*tp_str*/ |
1320 | 0, /*tp_getattro*/ | |
1321 | 0, /*tp_setattro*/ | |
1322 | 0, /*tp_as_buffer*/ | |
fa595c27 | 1323 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
f9176c46 PA |
1324 | "GDB value object", /* tp_doc */ |
1325 | 0, /* tp_traverse */ | |
1326 | 0, /* tp_clear */ | |
1327 | valpy_richcompare, /* tp_richcompare */ | |
1328 | 0, /* tp_weaklistoffset */ | |
1329 | 0, /* tp_iter */ | |
1330 | 0, /* tp_iternext */ | |
08c637de TJB |
1331 | value_object_methods, /* tp_methods */ |
1332 | 0, /* tp_members */ | |
def2b000 | 1333 | value_object_getset, /* tp_getset */ |
08c637de TJB |
1334 | 0, /* tp_base */ |
1335 | 0, /* tp_dict */ | |
1336 | 0, /* tp_descr_get */ | |
1337 | 0, /* tp_descr_set */ | |
1338 | 0, /* tp_dictoffset */ | |
1339 | 0, /* tp_init */ | |
1340 | 0, /* tp_alloc */ | |
1341 | valpy_new /* tp_new */ | |
f9176c46 PA |
1342 | }; |
1343 | ||
4e7a5ef5 TT |
1344 | #else |
1345 | ||
1346 | void | |
1347 | preserve_python_values (struct objfile *objfile, htab_t copied_types) | |
1348 | { | |
1349 | /* Nothing. */ | |
1350 | } | |
1351 | ||
7843261b | 1352 | #endif /* HAVE_PYTHON */ |