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