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