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