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