py-value.c revision 1.6 1 1.1 christos /* Python interface to values.
2 1.1 christos
3 1.6 christos Copyright (C) 2008-2016 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "charset.h"
22 1.1 christos #include "value.h"
23 1.1 christos #include "language.h"
24 1.1 christos #include "dfp.h"
25 1.1 christos #include "valprint.h"
26 1.1 christos #include "infcall.h"
27 1.1 christos #include "expression.h"
28 1.1 christos #include "cp-abi.h"
29 1.1 christos #include "python.h"
30 1.1 christos
31 1.1 christos #include "python-internal.h"
32 1.1 christos
33 1.1 christos /* Even though Python scalar types directly map to host types, we use
34 1.1 christos target types here to remain consistent with the values system in
35 1.1 christos GDB (which uses target arithmetic). */
36 1.1 christos
37 1.1 christos /* Python's integer type corresponds to C's long type. */
38 1.1 christos #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
39 1.1 christos
40 1.1 christos /* Python's float type corresponds to C's double type. */
41 1.1 christos #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
42 1.1 christos
43 1.1 christos /* Python's long type corresponds to C's long long type. */
44 1.1 christos #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
45 1.1 christos
46 1.1 christos /* Python's long type corresponds to C's long long type. Unsigned version. */
47 1.1 christos #define builtin_type_upylong builtin_type \
48 1.1 christos (python_gdbarch)->builtin_unsigned_long_long
49 1.1 christos
50 1.1 christos #define builtin_type_pybool \
51 1.1 christos language_bool_type (python_language, python_gdbarch)
52 1.1 christos
53 1.1 christos #define builtin_type_pychar \
54 1.1 christos language_string_char_type (python_language, python_gdbarch)
55 1.1 christos
56 1.1 christos typedef struct value_object {
57 1.1 christos PyObject_HEAD
58 1.1 christos struct value_object *next;
59 1.1 christos struct value_object *prev;
60 1.1 christos struct value *value;
61 1.1 christos PyObject *address;
62 1.1 christos PyObject *type;
63 1.1 christos PyObject *dynamic_type;
64 1.1 christos } value_object;
65 1.1 christos
66 1.1 christos /* List of all values which are currently exposed to Python. It is
67 1.1 christos maintained so that when an objfile is discarded, preserve_values
68 1.1 christos can copy the values' types if needed. */
69 1.1 christos /* This variable is unnecessarily initialized to NULL in order to
70 1.1 christos work around a linker bug on MacOS. */
71 1.1 christos static value_object *values_in_python = NULL;
72 1.1 christos
73 1.1 christos /* Called by the Python interpreter when deallocating a value object. */
74 1.1 christos static void
75 1.1 christos valpy_dealloc (PyObject *obj)
76 1.1 christos {
77 1.1 christos value_object *self = (value_object *) obj;
78 1.1 christos
79 1.1 christos /* Remove SELF from the global list. */
80 1.1 christos if (self->prev)
81 1.1 christos self->prev->next = self->next;
82 1.1 christos else
83 1.1 christos {
84 1.1 christos gdb_assert (values_in_python == self);
85 1.1 christos values_in_python = self->next;
86 1.1 christos }
87 1.1 christos if (self->next)
88 1.1 christos self->next->prev = self->prev;
89 1.1 christos
90 1.1 christos value_free (self->value);
91 1.1 christos
92 1.1 christos if (self->address)
93 1.1 christos /* Use braces to appease gcc warning. *sigh* */
94 1.1 christos {
95 1.1 christos Py_DECREF (self->address);
96 1.1 christos }
97 1.1 christos
98 1.1 christos if (self->type)
99 1.1 christos {
100 1.1 christos Py_DECREF (self->type);
101 1.1 christos }
102 1.1 christos
103 1.1 christos Py_XDECREF (self->dynamic_type);
104 1.1 christos
105 1.1 christos Py_TYPE (self)->tp_free (self);
106 1.1 christos }
107 1.1 christos
108 1.1 christos /* Helper to push a Value object on the global list. */
109 1.1 christos static void
110 1.1 christos note_value (value_object *value_obj)
111 1.1 christos {
112 1.1 christos value_obj->next = values_in_python;
113 1.1 christos if (value_obj->next)
114 1.1 christos value_obj->next->prev = value_obj;
115 1.1 christos value_obj->prev = NULL;
116 1.1 christos values_in_python = value_obj;
117 1.1 christos }
118 1.1 christos
119 1.1 christos /* Called when a new gdb.Value object needs to be allocated. Returns NULL on
120 1.1 christos error, with a python exception set. */
121 1.1 christos static PyObject *
122 1.1 christos valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
123 1.1 christos {
124 1.1 christos struct value *value = NULL; /* Initialize to appease gcc warning. */
125 1.1 christos value_object *value_obj;
126 1.1 christos
127 1.1 christos if (PyTuple_Size (args) != 1)
128 1.1 christos {
129 1.1 christos PyErr_SetString (PyExc_TypeError, _("Value object creation takes only "
130 1.1 christos "1 argument"));
131 1.1 christos return NULL;
132 1.1 christos }
133 1.1 christos
134 1.1 christos value_obj = (value_object *) subtype->tp_alloc (subtype, 1);
135 1.1 christos if (value_obj == NULL)
136 1.1 christos {
137 1.1 christos PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to "
138 1.1 christos "create Value object."));
139 1.1 christos return NULL;
140 1.1 christos }
141 1.1 christos
142 1.1 christos value = convert_value_from_python (PyTuple_GetItem (args, 0));
143 1.1 christos if (value == NULL)
144 1.1 christos {
145 1.1 christos subtype->tp_free (value_obj);
146 1.1 christos return NULL;
147 1.1 christos }
148 1.1 christos
149 1.1 christos value_obj->value = value;
150 1.1 christos release_value_or_incref (value);
151 1.1 christos value_obj->address = NULL;
152 1.1 christos value_obj->type = NULL;
153 1.1 christos value_obj->dynamic_type = NULL;
154 1.1 christos note_value (value_obj);
155 1.1 christos
156 1.1 christos return (PyObject *) value_obj;
157 1.1 christos }
158 1.1 christos
159 1.1 christos /* Iterate over all the Value objects, calling preserve_one_value on
160 1.1 christos each. */
161 1.1 christos void
162 1.3 christos gdbpy_preserve_values (const struct extension_language_defn *extlang,
163 1.3 christos struct objfile *objfile, htab_t copied_types)
164 1.1 christos {
165 1.1 christos value_object *iter;
166 1.1 christos
167 1.1 christos for (iter = values_in_python; iter; iter = iter->next)
168 1.1 christos preserve_one_value (iter->value, objfile, copied_types);
169 1.1 christos }
170 1.1 christos
171 1.1 christos /* Given a value of a pointer type, apply the C unary * operator to it. */
172 1.1 christos static PyObject *
173 1.1 christos valpy_dereference (PyObject *self, PyObject *args)
174 1.1 christos {
175 1.1 christos PyObject *result = NULL;
176 1.1 christos
177 1.5 christos TRY
178 1.1 christos {
179 1.1 christos struct value *res_val;
180 1.1 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
181 1.1 christos
182 1.1 christos res_val = value_ind (((value_object *) self)->value);
183 1.1 christos result = value_to_value_object (res_val);
184 1.1 christos do_cleanups (cleanup);
185 1.1 christos }
186 1.5 christos CATCH (except, RETURN_MASK_ALL)
187 1.5 christos {
188 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
189 1.5 christos }
190 1.5 christos END_CATCH
191 1.1 christos
192 1.1 christos return result;
193 1.1 christos }
194 1.1 christos
195 1.1 christos /* Given a value of a pointer type or a reference type, return the value
196 1.1 christos referenced. The difference between this function and valpy_dereference is
197 1.1 christos that the latter applies * unary operator to a value, which need not always
198 1.1 christos result in the value referenced. For example, for a value which is a reference
199 1.1 christos to an 'int' pointer ('int *'), valpy_dereference will result in a value of
200 1.1 christos type 'int' while valpy_referenced_value will result in a value of type
201 1.1 christos 'int *'. */
202 1.1 christos
203 1.1 christos static PyObject *
204 1.1 christos valpy_referenced_value (PyObject *self, PyObject *args)
205 1.1 christos {
206 1.1 christos PyObject *result = NULL;
207 1.1 christos
208 1.5 christos TRY
209 1.1 christos {
210 1.1 christos struct value *self_val, *res_val;
211 1.1 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
212 1.1 christos
213 1.1 christos self_val = ((value_object *) self)->value;
214 1.1 christos switch (TYPE_CODE (check_typedef (value_type (self_val))))
215 1.1 christos {
216 1.1 christos case TYPE_CODE_PTR:
217 1.1 christos res_val = value_ind (self_val);
218 1.1 christos break;
219 1.1 christos case TYPE_CODE_REF:
220 1.1 christos res_val = coerce_ref (self_val);
221 1.1 christos break;
222 1.1 christos default:
223 1.1 christos error(_("Trying to get the referenced value from a value which is "
224 1.1 christos "neither a pointer nor a reference."));
225 1.1 christos }
226 1.1 christos
227 1.1 christos result = value_to_value_object (res_val);
228 1.1 christos do_cleanups (cleanup);
229 1.1 christos }
230 1.5 christos CATCH (except, RETURN_MASK_ALL)
231 1.5 christos {
232 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
233 1.5 christos }
234 1.5 christos END_CATCH
235 1.5 christos
236 1.5 christos return result;
237 1.5 christos }
238 1.5 christos
239 1.5 christos /* Return a value which is a reference to the value. */
240 1.5 christos
241 1.5 christos static PyObject *
242 1.5 christos valpy_reference_value (PyObject *self, PyObject *args)
243 1.5 christos {
244 1.5 christos PyObject *result = NULL;
245 1.5 christos
246 1.5 christos TRY
247 1.5 christos {
248 1.5 christos struct value *self_val;
249 1.5 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
250 1.5 christos
251 1.5 christos self_val = ((value_object *) self)->value;
252 1.5 christos result = value_to_value_object (value_ref (self_val));
253 1.5 christos
254 1.5 christos do_cleanups (cleanup);
255 1.5 christos }
256 1.5 christos CATCH (except, RETURN_MASK_ALL)
257 1.5 christos {
258 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
259 1.5 christos }
260 1.5 christos END_CATCH
261 1.5 christos
262 1.5 christos return result;
263 1.5 christos }
264 1.5 christos
265 1.5 christos /* Return a "const" qualified version of the value. */
266 1.5 christos
267 1.5 christos static PyObject *
268 1.5 christos valpy_const_value (PyObject *self, PyObject *args)
269 1.5 christos {
270 1.5 christos PyObject *result = NULL;
271 1.5 christos
272 1.5 christos TRY
273 1.5 christos {
274 1.5 christos struct value *self_val, *res_val;
275 1.5 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
276 1.5 christos
277 1.5 christos self_val = ((value_object *) self)->value;
278 1.5 christos res_val = make_cv_value (1, 0, self_val);
279 1.5 christos result = value_to_value_object (res_val);
280 1.5 christos
281 1.5 christos do_cleanups (cleanup);
282 1.5 christos }
283 1.5 christos CATCH (except, RETURN_MASK_ALL)
284 1.5 christos {
285 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
286 1.5 christos }
287 1.5 christos END_CATCH
288 1.1 christos
289 1.1 christos return result;
290 1.1 christos }
291 1.1 christos
292 1.1 christos /* Return "&value". */
293 1.1 christos static PyObject *
294 1.1 christos valpy_get_address (PyObject *self, void *closure)
295 1.1 christos {
296 1.1 christos value_object *val_obj = (value_object *) self;
297 1.1 christos
298 1.1 christos if (!val_obj->address)
299 1.1 christos {
300 1.5 christos TRY
301 1.1 christos {
302 1.1 christos struct value *res_val;
303 1.1 christos struct cleanup *cleanup
304 1.1 christos = make_cleanup_value_free_to_mark (value_mark ());
305 1.1 christos
306 1.1 christos res_val = value_addr (val_obj->value);
307 1.1 christos val_obj->address = value_to_value_object (res_val);
308 1.1 christos do_cleanups (cleanup);
309 1.1 christos }
310 1.5 christos CATCH (except, RETURN_MASK_ALL)
311 1.1 christos {
312 1.1 christos val_obj->address = Py_None;
313 1.1 christos Py_INCREF (Py_None);
314 1.1 christos }
315 1.5 christos END_CATCH
316 1.1 christos }
317 1.1 christos
318 1.1 christos Py_XINCREF (val_obj->address);
319 1.1 christos
320 1.1 christos return val_obj->address;
321 1.1 christos }
322 1.1 christos
323 1.1 christos /* Return type of the value. */
324 1.1 christos static PyObject *
325 1.1 christos valpy_get_type (PyObject *self, void *closure)
326 1.1 christos {
327 1.1 christos value_object *obj = (value_object *) self;
328 1.1 christos
329 1.1 christos if (!obj->type)
330 1.1 christos {
331 1.1 christos obj->type = type_to_type_object (value_type (obj->value));
332 1.1 christos if (!obj->type)
333 1.1 christos return NULL;
334 1.1 christos }
335 1.1 christos Py_INCREF (obj->type);
336 1.1 christos return obj->type;
337 1.1 christos }
338 1.1 christos
339 1.1 christos /* Return dynamic type of the value. */
340 1.1 christos
341 1.1 christos static PyObject *
342 1.1 christos valpy_get_dynamic_type (PyObject *self, void *closure)
343 1.1 christos {
344 1.1 christos value_object *obj = (value_object *) self;
345 1.1 christos struct type *type = NULL;
346 1.1 christos
347 1.1 christos if (obj->dynamic_type != NULL)
348 1.1 christos {
349 1.1 christos Py_INCREF (obj->dynamic_type);
350 1.1 christos return obj->dynamic_type;
351 1.1 christos }
352 1.1 christos
353 1.5 christos TRY
354 1.1 christos {
355 1.1 christos struct value *val = obj->value;
356 1.1 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
357 1.1 christos
358 1.1 christos type = value_type (val);
359 1.6 christos type = check_typedef (type);
360 1.1 christos
361 1.1 christos if (((TYPE_CODE (type) == TYPE_CODE_PTR)
362 1.1 christos || (TYPE_CODE (type) == TYPE_CODE_REF))
363 1.3 christos && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
364 1.1 christos {
365 1.1 christos struct value *target;
366 1.1 christos int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
367 1.1 christos
368 1.3 christos if (was_pointer)
369 1.3 christos target = value_ind (val);
370 1.3 christos else
371 1.3 christos target = coerce_ref (val);
372 1.1 christos type = value_rtti_type (target, NULL, NULL, NULL);
373 1.1 christos
374 1.1 christos if (type)
375 1.1 christos {
376 1.1 christos if (was_pointer)
377 1.1 christos type = lookup_pointer_type (type);
378 1.1 christos else
379 1.1 christos type = lookup_reference_type (type);
380 1.1 christos }
381 1.1 christos }
382 1.3 christos else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
383 1.1 christos type = value_rtti_type (val, NULL, NULL, NULL);
384 1.1 christos else
385 1.1 christos {
386 1.1 christos /* Re-use object's static type. */
387 1.1 christos type = NULL;
388 1.1 christos }
389 1.1 christos
390 1.1 christos do_cleanups (cleanup);
391 1.1 christos }
392 1.5 christos CATCH (except, RETURN_MASK_ALL)
393 1.5 christos {
394 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
395 1.5 christos }
396 1.5 christos END_CATCH
397 1.1 christos
398 1.1 christos if (type == NULL)
399 1.1 christos obj->dynamic_type = valpy_get_type (self, NULL);
400 1.1 christos else
401 1.1 christos obj->dynamic_type = type_to_type_object (type);
402 1.1 christos
403 1.1 christos Py_XINCREF (obj->dynamic_type);
404 1.1 christos return obj->dynamic_type;
405 1.1 christos }
406 1.1 christos
407 1.1 christos /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
408 1.1 christos string. Return a PyObject representing a lazy_string_object type.
409 1.1 christos A lazy string is a pointer to a string with an optional encoding and
410 1.1 christos length. If ENCODING is not given, encoding is set to None. If an
411 1.1 christos ENCODING is provided the encoding parameter is set to ENCODING, but
412 1.1 christos the string is not encoded. If LENGTH is provided then the length
413 1.1 christos parameter is set to LENGTH, otherwise length will be set to -1 (first
414 1.1 christos null of appropriate with). */
415 1.1 christos static PyObject *
416 1.1 christos valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
417 1.1 christos {
418 1.1 christos gdb_py_longest length = -1;
419 1.1 christos struct value *value = ((value_object *) self)->value;
420 1.1 christos const char *user_encoding = NULL;
421 1.1 christos static char *keywords[] = { "encoding", "length", NULL };
422 1.1 christos PyObject *str_obj = NULL;
423 1.1 christos
424 1.1 christos if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords,
425 1.1 christos &user_encoding, &length))
426 1.1 christos return NULL;
427 1.1 christos
428 1.5 christos TRY
429 1.1 christos {
430 1.1 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
431 1.1 christos
432 1.1 christos if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
433 1.1 christos value = value_ind (value);
434 1.1 christos
435 1.1 christos str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
436 1.1 christos user_encoding,
437 1.1 christos value_type (value));
438 1.1 christos
439 1.1 christos do_cleanups (cleanup);
440 1.1 christos }
441 1.5 christos CATCH (except, RETURN_MASK_ALL)
442 1.5 christos {
443 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
444 1.5 christos }
445 1.5 christos END_CATCH
446 1.1 christos
447 1.1 christos return str_obj;
448 1.1 christos }
449 1.1 christos
450 1.1 christos /* Implementation of gdb.Value.string ([encoding] [, errors]
451 1.1 christos [, length]) -> string. Return Unicode string with value contents.
452 1.1 christos If ENCODING is not given, the string is assumed to be encoded in
453 1.1 christos the target's charset. If LENGTH is provided, only fetch string to
454 1.1 christos the length provided. */
455 1.1 christos
456 1.1 christos static PyObject *
457 1.1 christos valpy_string (PyObject *self, PyObject *args, PyObject *kw)
458 1.1 christos {
459 1.1 christos int length = -1;
460 1.1 christos gdb_byte *buffer;
461 1.1 christos struct value *value = ((value_object *) self)->value;
462 1.1 christos PyObject *unicode;
463 1.1 christos const char *encoding = NULL;
464 1.1 christos const char *errors = NULL;
465 1.1 christos const char *user_encoding = NULL;
466 1.1 christos const char *la_encoding = NULL;
467 1.1 christos struct type *char_type;
468 1.1 christos static char *keywords[] = { "encoding", "errors", "length", NULL };
469 1.1 christos
470 1.1 christos if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
471 1.1 christos &user_encoding, &errors, &length))
472 1.1 christos return NULL;
473 1.1 christos
474 1.5 christos TRY
475 1.1 christos {
476 1.1 christos LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
477 1.1 christos }
478 1.5 christos CATCH (except, RETURN_MASK_ALL)
479 1.5 christos {
480 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
481 1.5 christos }
482 1.5 christos END_CATCH
483 1.1 christos
484 1.1 christos encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
485 1.1 christos unicode = PyUnicode_Decode ((const char *) buffer,
486 1.1 christos length * TYPE_LENGTH (char_type),
487 1.1 christos encoding, errors);
488 1.1 christos xfree (buffer);
489 1.1 christos
490 1.1 christos return unicode;
491 1.1 christos }
492 1.1 christos
493 1.1 christos /* A helper function that implements the various cast operators. */
494 1.1 christos
495 1.1 christos static PyObject *
496 1.1 christos valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op)
497 1.1 christos {
498 1.1 christos PyObject *type_obj, *result = NULL;
499 1.1 christos struct type *type;
500 1.1 christos
501 1.1 christos if (! PyArg_ParseTuple (args, "O", &type_obj))
502 1.1 christos return NULL;
503 1.1 christos
504 1.1 christos type = type_object_to_type (type_obj);
505 1.1 christos if (! type)
506 1.1 christos {
507 1.1 christos PyErr_SetString (PyExc_RuntimeError,
508 1.1 christos _("Argument must be a type."));
509 1.1 christos return NULL;
510 1.1 christos }
511 1.1 christos
512 1.5 christos TRY
513 1.1 christos {
514 1.1 christos struct value *val = ((value_object *) self)->value;
515 1.1 christos struct value *res_val;
516 1.1 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
517 1.1 christos
518 1.1 christos if (op == UNOP_DYNAMIC_CAST)
519 1.1 christos res_val = value_dynamic_cast (type, val);
520 1.1 christos else if (op == UNOP_REINTERPRET_CAST)
521 1.1 christos res_val = value_reinterpret_cast (type, val);
522 1.1 christos else
523 1.1 christos {
524 1.1 christos gdb_assert (op == UNOP_CAST);
525 1.1 christos res_val = value_cast (type, val);
526 1.1 christos }
527 1.1 christos
528 1.1 christos result = value_to_value_object (res_val);
529 1.1 christos do_cleanups (cleanup);
530 1.1 christos }
531 1.5 christos CATCH (except, RETURN_MASK_ALL)
532 1.5 christos {
533 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
534 1.5 christos }
535 1.5 christos END_CATCH
536 1.1 christos
537 1.1 christos return result;
538 1.1 christos }
539 1.1 christos
540 1.1 christos /* Implementation of the "cast" method. */
541 1.1 christos
542 1.1 christos static PyObject *
543 1.1 christos valpy_cast (PyObject *self, PyObject *args)
544 1.1 christos {
545 1.1 christos return valpy_do_cast (self, args, UNOP_CAST);
546 1.1 christos }
547 1.1 christos
548 1.1 christos /* Implementation of the "dynamic_cast" method. */
549 1.1 christos
550 1.1 christos static PyObject *
551 1.1 christos valpy_dynamic_cast (PyObject *self, PyObject *args)
552 1.1 christos {
553 1.1 christos return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST);
554 1.1 christos }
555 1.1 christos
556 1.1 christos /* Implementation of the "reinterpret_cast" method. */
557 1.1 christos
558 1.1 christos static PyObject *
559 1.1 christos valpy_reinterpret_cast (PyObject *self, PyObject *args)
560 1.1 christos {
561 1.1 christos return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
562 1.1 christos }
563 1.1 christos
564 1.1 christos static Py_ssize_t
565 1.1 christos valpy_length (PyObject *self)
566 1.1 christos {
567 1.1 christos /* We don't support getting the number of elements in a struct / class. */
568 1.1 christos PyErr_SetString (PyExc_NotImplementedError,
569 1.1 christos _("Invalid operation on gdb.Value."));
570 1.1 christos return -1;
571 1.1 christos }
572 1.1 christos
573 1.1 christos /* Return 1 if the gdb.Field object FIELD is present in the value V.
574 1.1 christos Returns 0 otherwise. If any Python error occurs, -1 is returned. */
575 1.1 christos
576 1.1 christos static int
577 1.1 christos value_has_field (struct value *v, PyObject *field)
578 1.1 christos {
579 1.1 christos struct type *parent_type, *val_type;
580 1.1 christos enum type_code type_code;
581 1.1 christos PyObject *type_object = PyObject_GetAttrString (field, "parent_type");
582 1.1 christos int has_field = 0;
583 1.1 christos
584 1.1 christos if (type_object == NULL)
585 1.1 christos return -1;
586 1.1 christos
587 1.1 christos parent_type = type_object_to_type (type_object);
588 1.1 christos Py_DECREF (type_object);
589 1.1 christos if (parent_type == NULL)
590 1.1 christos {
591 1.1 christos PyErr_SetString (PyExc_TypeError,
592 1.1 christos _("'parent_type' attribute of gdb.Field object is not a"
593 1.1 christos "gdb.Type object."));
594 1.1 christos return -1;
595 1.1 christos }
596 1.1 christos
597 1.5 christos TRY
598 1.1 christos {
599 1.1 christos val_type = value_type (v);
600 1.1 christos val_type = check_typedef (val_type);
601 1.1 christos if (TYPE_CODE (val_type) == TYPE_CODE_REF
602 1.1 christos || TYPE_CODE (val_type) == TYPE_CODE_PTR)
603 1.1 christos val_type = check_typedef (TYPE_TARGET_TYPE (val_type));
604 1.1 christos
605 1.1 christos type_code = TYPE_CODE (val_type);
606 1.1 christos if ((type_code == TYPE_CODE_STRUCT || type_code == TYPE_CODE_UNION)
607 1.1 christos && types_equal (val_type, parent_type))
608 1.1 christos has_field = 1;
609 1.1 christos else
610 1.1 christos has_field = 0;
611 1.1 christos }
612 1.5 christos CATCH (except, RETURN_MASK_ALL)
613 1.5 christos {
614 1.5 christos GDB_PY_SET_HANDLE_EXCEPTION (except);
615 1.5 christos }
616 1.5 christos END_CATCH
617 1.1 christos
618 1.1 christos return has_field;
619 1.1 christos }
620 1.1 christos
621 1.1 christos /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
622 1.1 christos Returns 1 if the flag value is true, 0 if it is false, and -1 if
623 1.1 christos a Python error occurs. */
624 1.1 christos
625 1.1 christos static int
626 1.1 christos get_field_flag (PyObject *field, const char *flag_name)
627 1.1 christos {
628 1.1 christos int flag_value;
629 1.1 christos PyObject *flag_object = PyObject_GetAttrString (field, flag_name);
630 1.1 christos
631 1.1 christos if (flag_object == NULL)
632 1.1 christos return -1;
633 1.1 christos
634 1.1 christos flag_value = PyObject_IsTrue (flag_object);
635 1.1 christos Py_DECREF (flag_object);
636 1.1 christos
637 1.1 christos return flag_value;
638 1.1 christos }
639 1.1 christos
640 1.1 christos /* Return the "type" attribute of a gdb.Field object.
641 1.1 christos Returns NULL on error, with a Python exception set. */
642 1.1 christos
643 1.1 christos static struct type *
644 1.1 christos get_field_type (PyObject *field)
645 1.1 christos {
646 1.1 christos PyObject *ftype_obj = PyObject_GetAttrString (field, "type");
647 1.1 christos struct type *ftype;
648 1.1 christos
649 1.1 christos if (ftype_obj == NULL)
650 1.1 christos return NULL;
651 1.1 christos ftype = type_object_to_type (ftype_obj);
652 1.1 christos Py_DECREF (ftype_obj);
653 1.1 christos if (ftype == NULL)
654 1.1 christos PyErr_SetString (PyExc_TypeError,
655 1.1 christos _("'type' attribute of gdb.Field object is not a "
656 1.1 christos "gdb.Type object."));
657 1.1 christos
658 1.1 christos return ftype;
659 1.1 christos }
660 1.1 christos
661 1.1 christos /* Given string name or a gdb.Field object corresponding to an element inside
662 1.1 christos a structure, return its value object. Returns NULL on error, with a python
663 1.1 christos exception set. */
664 1.1 christos
665 1.1 christos static PyObject *
666 1.1 christos valpy_getitem (PyObject *self, PyObject *key)
667 1.1 christos {
668 1.5 christos struct gdb_exception except = exception_none;
669 1.1 christos value_object *self_value = (value_object *) self;
670 1.1 christos char *field = NULL;
671 1.1 christos struct type *base_class_type = NULL, *field_type = NULL;
672 1.1 christos long bitpos = -1;
673 1.1 christos PyObject *result = NULL;
674 1.1 christos
675 1.1 christos if (gdbpy_is_string (key))
676 1.1 christos {
677 1.1 christos field = python_string_to_host_string (key);
678 1.1 christos if (field == NULL)
679 1.1 christos return NULL;
680 1.1 christos }
681 1.1 christos else if (gdbpy_is_field (key))
682 1.1 christos {
683 1.1 christos int is_base_class, valid_field;
684 1.1 christos
685 1.1 christos valid_field = value_has_field (self_value->value, key);
686 1.1 christos if (valid_field < 0)
687 1.1 christos return NULL;
688 1.1 christos else if (valid_field == 0)
689 1.1 christos {
690 1.1 christos PyErr_SetString (PyExc_TypeError,
691 1.1 christos _("Invalid lookup for a field not contained in "
692 1.1 christos "the value."));
693 1.1 christos
694 1.1 christos return NULL;
695 1.1 christos }
696 1.1 christos
697 1.1 christos is_base_class = get_field_flag (key, "is_base_class");
698 1.1 christos if (is_base_class < 0)
699 1.1 christos return NULL;
700 1.1 christos else if (is_base_class > 0)
701 1.1 christos {
702 1.1 christos base_class_type = get_field_type (key);
703 1.1 christos if (base_class_type == NULL)
704 1.1 christos return NULL;
705 1.1 christos }
706 1.1 christos else
707 1.1 christos {
708 1.1 christos PyObject *name_obj = PyObject_GetAttrString (key, "name");
709 1.1 christos
710 1.1 christos if (name_obj == NULL)
711 1.1 christos return NULL;
712 1.1 christos
713 1.1 christos if (name_obj != Py_None)
714 1.1 christos {
715 1.1 christos field = python_string_to_host_string (name_obj);
716 1.1 christos Py_DECREF (name_obj);
717 1.1 christos if (field == NULL)
718 1.1 christos return NULL;
719 1.1 christos }
720 1.1 christos else
721 1.1 christos {
722 1.1 christos PyObject *bitpos_obj;
723 1.1 christos int valid;
724 1.1 christos
725 1.1 christos Py_DECREF (name_obj);
726 1.1 christos
727 1.1 christos if (!PyObject_HasAttrString (key, "bitpos"))
728 1.1 christos {
729 1.1 christos PyErr_SetString (PyExc_AttributeError,
730 1.1 christos _("gdb.Field object has no name and no "
731 1.1 christos "'bitpos' attribute."));
732 1.1 christos
733 1.1 christos return NULL;
734 1.1 christos }
735 1.1 christos bitpos_obj = PyObject_GetAttrString (key, "bitpos");
736 1.1 christos if (bitpos_obj == NULL)
737 1.1 christos return NULL;
738 1.1 christos valid = gdb_py_int_as_long (bitpos_obj, &bitpos);
739 1.1 christos Py_DECREF (bitpos_obj);
740 1.1 christos if (!valid)
741 1.1 christos return NULL;
742 1.1 christos
743 1.1 christos field_type = get_field_type (key);
744 1.1 christos if (field_type == NULL)
745 1.1 christos return NULL;
746 1.1 christos }
747 1.1 christos }
748 1.1 christos }
749 1.1 christos
750 1.5 christos TRY
751 1.1 christos {
752 1.1 christos struct value *tmp = self_value->value;
753 1.1 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
754 1.1 christos struct value *res_val = NULL;
755 1.1 christos
756 1.1 christos if (field)
757 1.1 christos res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
758 1.1 christos else if (bitpos >= 0)
759 1.1 christos res_val = value_struct_elt_bitpos (&tmp, bitpos, field_type,
760 1.1 christos "struct/class/union");
761 1.1 christos else if (base_class_type != NULL)
762 1.1 christos {
763 1.1 christos struct type *val_type;
764 1.1 christos
765 1.1 christos val_type = check_typedef (value_type (tmp));
766 1.1 christos if (TYPE_CODE (val_type) == TYPE_CODE_PTR)
767 1.1 christos res_val = value_cast (lookup_pointer_type (base_class_type), tmp);
768 1.1 christos else if (TYPE_CODE (val_type) == TYPE_CODE_REF)
769 1.1 christos res_val = value_cast (lookup_reference_type (base_class_type), tmp);
770 1.1 christos else
771 1.1 christos res_val = value_cast (base_class_type, tmp);
772 1.1 christos }
773 1.1 christos else
774 1.1 christos {
775 1.1 christos /* Assume we are attempting an array access, and let the
776 1.1 christos value code throw an exception if the index has an invalid
777 1.1 christos type. */
778 1.1 christos struct value *idx = convert_value_from_python (key);
779 1.1 christos
780 1.1 christos if (idx != NULL)
781 1.1 christos {
782 1.1 christos /* Check the value's type is something that can be accessed via
783 1.1 christos a subscript. */
784 1.1 christos struct type *type;
785 1.1 christos
786 1.1 christos tmp = coerce_ref (tmp);
787 1.1 christos type = check_typedef (value_type (tmp));
788 1.1 christos if (TYPE_CODE (type) != TYPE_CODE_ARRAY
789 1.1 christos && TYPE_CODE (type) != TYPE_CODE_PTR)
790 1.1 christos error (_("Cannot subscript requested type."));
791 1.1 christos else
792 1.1 christos res_val = value_subscript (tmp, value_as_long (idx));
793 1.1 christos }
794 1.1 christos }
795 1.1 christos
796 1.1 christos if (res_val)
797 1.1 christos result = value_to_value_object (res_val);
798 1.1 christos do_cleanups (cleanup);
799 1.1 christos }
800 1.5 christos CATCH (ex, RETURN_MASK_ALL)
801 1.5 christos {
802 1.5 christos except = ex;
803 1.5 christos }
804 1.5 christos END_CATCH
805 1.1 christos
806 1.1 christos xfree (field);
807 1.1 christos GDB_PY_HANDLE_EXCEPTION (except);
808 1.1 christos
809 1.1 christos return result;
810 1.1 christos }
811 1.1 christos
812 1.1 christos static int
813 1.1 christos valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
814 1.1 christos {
815 1.1 christos PyErr_Format (PyExc_NotImplementedError,
816 1.1 christos _("Setting of struct elements is not currently supported."));
817 1.1 christos return -1;
818 1.1 christos }
819 1.1 christos
820 1.1 christos /* Called by the Python interpreter to perform an inferior function
821 1.1 christos call on the value. Returns NULL on error, with a python exception set. */
822 1.1 christos static PyObject *
823 1.1 christos valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
824 1.1 christos {
825 1.1 christos Py_ssize_t args_count;
826 1.1 christos struct value *function = ((value_object *) self)->value;
827 1.1 christos struct value **vargs = NULL;
828 1.1 christos struct type *ftype = NULL;
829 1.1 christos struct value *mark = value_mark ();
830 1.1 christos PyObject *result = NULL;
831 1.1 christos
832 1.5 christos TRY
833 1.1 christos {
834 1.1 christos ftype = check_typedef (value_type (function));
835 1.1 christos }
836 1.5 christos CATCH (except, RETURN_MASK_ALL)
837 1.5 christos {
838 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
839 1.5 christos }
840 1.5 christos END_CATCH
841 1.1 christos
842 1.1 christos if (TYPE_CODE (ftype) != TYPE_CODE_FUNC)
843 1.1 christos {
844 1.1 christos PyErr_SetString (PyExc_RuntimeError,
845 1.1 christos _("Value is not callable (not TYPE_CODE_FUNC)."));
846 1.1 christos return NULL;
847 1.1 christos }
848 1.1 christos
849 1.1 christos if (! PyTuple_Check (args))
850 1.1 christos {
851 1.1 christos PyErr_SetString (PyExc_TypeError,
852 1.1 christos _("Inferior arguments must be provided in a tuple."));
853 1.1 christos return NULL;
854 1.1 christos }
855 1.1 christos
856 1.1 christos args_count = PyTuple_Size (args);
857 1.1 christos if (args_count > 0)
858 1.1 christos {
859 1.1 christos int i;
860 1.1 christos
861 1.6 christos vargs = XALLOCAVEC (struct value *, args_count);
862 1.1 christos for (i = 0; i < args_count; i++)
863 1.1 christos {
864 1.1 christos PyObject *item = PyTuple_GetItem (args, i);
865 1.1 christos
866 1.1 christos if (item == NULL)
867 1.1 christos return NULL;
868 1.1 christos
869 1.1 christos vargs[i] = convert_value_from_python (item);
870 1.1 christos if (vargs[i] == NULL)
871 1.1 christos return NULL;
872 1.1 christos }
873 1.1 christos }
874 1.1 christos
875 1.5 christos TRY
876 1.1 christos {
877 1.1 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark);
878 1.1 christos struct value *return_value;
879 1.1 christos
880 1.1 christos return_value = call_function_by_hand (function, args_count, vargs);
881 1.1 christos result = value_to_value_object (return_value);
882 1.1 christos do_cleanups (cleanup);
883 1.1 christos }
884 1.5 christos CATCH (except, RETURN_MASK_ALL)
885 1.5 christos {
886 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
887 1.5 christos }
888 1.5 christos END_CATCH
889 1.1 christos
890 1.1 christos return result;
891 1.1 christos }
892 1.1 christos
893 1.1 christos /* Called by the Python interpreter to obtain string representation
894 1.1 christos of the object. */
895 1.1 christos static PyObject *
896 1.1 christos valpy_str (PyObject *self)
897 1.1 christos {
898 1.1 christos char *s = NULL;
899 1.1 christos PyObject *result;
900 1.1 christos struct value_print_options opts;
901 1.1 christos
902 1.1 christos get_user_print_options (&opts);
903 1.1 christos opts.deref_ref = 0;
904 1.1 christos
905 1.5 christos TRY
906 1.1 christos {
907 1.1 christos struct ui_file *stb = mem_fileopen ();
908 1.1 christos struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
909 1.1 christos
910 1.1 christos common_val_print (((value_object *) self)->value, stb, 0,
911 1.1 christos &opts, python_language);
912 1.1 christos s = ui_file_xstrdup (stb, NULL);
913 1.1 christos
914 1.1 christos do_cleanups (old_chain);
915 1.1 christos }
916 1.5 christos CATCH (except, RETURN_MASK_ALL)
917 1.5 christos {
918 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
919 1.5 christos }
920 1.5 christos END_CATCH
921 1.1 christos
922 1.1 christos result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL);
923 1.1 christos xfree (s);
924 1.1 christos
925 1.1 christos return result;
926 1.1 christos }
927 1.1 christos
928 1.1 christos /* Implements gdb.Value.is_optimized_out. */
929 1.1 christos static PyObject *
930 1.1 christos valpy_get_is_optimized_out (PyObject *self, void *closure)
931 1.1 christos {
932 1.1 christos struct value *value = ((value_object *) self)->value;
933 1.1 christos int opt = 0;
934 1.1 christos
935 1.5 christos TRY
936 1.1 christos {
937 1.1 christos opt = value_optimized_out (value);
938 1.1 christos }
939 1.5 christos CATCH (except, RETURN_MASK_ALL)
940 1.5 christos {
941 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
942 1.5 christos }
943 1.5 christos END_CATCH
944 1.1 christos
945 1.1 christos if (opt)
946 1.1 christos Py_RETURN_TRUE;
947 1.1 christos
948 1.1 christos Py_RETURN_FALSE;
949 1.1 christos }
950 1.1 christos
951 1.1 christos /* Implements gdb.Value.is_lazy. */
952 1.1 christos static PyObject *
953 1.1 christos valpy_get_is_lazy (PyObject *self, void *closure)
954 1.1 christos {
955 1.1 christos struct value *value = ((value_object *) self)->value;
956 1.1 christos int opt = 0;
957 1.1 christos
958 1.5 christos TRY
959 1.1 christos {
960 1.1 christos opt = value_lazy (value);
961 1.1 christos }
962 1.5 christos CATCH (except, RETURN_MASK_ALL)
963 1.5 christos {
964 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
965 1.5 christos }
966 1.5 christos END_CATCH
967 1.1 christos
968 1.1 christos if (opt)
969 1.1 christos Py_RETURN_TRUE;
970 1.1 christos
971 1.1 christos Py_RETURN_FALSE;
972 1.1 christos }
973 1.1 christos
974 1.1 christos /* Implements gdb.Value.fetch_lazy (). */
975 1.1 christos static PyObject *
976 1.1 christos valpy_fetch_lazy (PyObject *self, PyObject *args)
977 1.1 christos {
978 1.1 christos struct value *value = ((value_object *) self)->value;
979 1.1 christos
980 1.5 christos TRY
981 1.1 christos {
982 1.1 christos if (value_lazy (value))
983 1.1 christos value_fetch_lazy (value);
984 1.1 christos }
985 1.5 christos CATCH (except, RETURN_MASK_ALL)
986 1.5 christos {
987 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
988 1.5 christos }
989 1.5 christos END_CATCH
990 1.1 christos
991 1.1 christos Py_RETURN_NONE;
992 1.1 christos }
993 1.1 christos
994 1.1 christos /* Calculate and return the address of the PyObject as the value of
995 1.1 christos the builtin __hash__ call. */
996 1.5 christos static Py_hash_t
997 1.1 christos valpy_hash (PyObject *self)
998 1.1 christos {
999 1.5 christos return (intptr_t) self;
1000 1.1 christos }
1001 1.1 christos
1002 1.1 christos enum valpy_opcode
1003 1.1 christos {
1004 1.1 christos VALPY_ADD,
1005 1.1 christos VALPY_SUB,
1006 1.1 christos VALPY_MUL,
1007 1.1 christos VALPY_DIV,
1008 1.1 christos VALPY_REM,
1009 1.1 christos VALPY_POW,
1010 1.1 christos VALPY_LSH,
1011 1.1 christos VALPY_RSH,
1012 1.1 christos VALPY_BITAND,
1013 1.1 christos VALPY_BITOR,
1014 1.1 christos VALPY_BITXOR
1015 1.1 christos };
1016 1.1 christos
1017 1.1 christos /* If TYPE is a reference, return the target; otherwise return TYPE. */
1018 1.1 christos #define STRIP_REFERENCE(TYPE) \
1019 1.1 christos ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
1020 1.1 christos
1021 1.6 christos /* Helper for valpy_binop. Returns a value object which is the result
1022 1.6 christos of applying the operation specified by OPCODE to the given
1023 1.6 christos arguments. Throws a GDB exception on error. */
1024 1.6 christos
1025 1.1 christos static PyObject *
1026 1.6 christos valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other)
1027 1.1 christos {
1028 1.1 christos PyObject *result = NULL;
1029 1.1 christos
1030 1.6 christos struct value *arg1, *arg2;
1031 1.6 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
1032 1.6 christos struct value *res_val = NULL;
1033 1.6 christos enum exp_opcode op = OP_NULL;
1034 1.6 christos int handled = 0;
1035 1.6 christos
1036 1.6 christos /* If the gdb.Value object is the second operand, then it will be
1037 1.6 christos passed to us as the OTHER argument, and SELF will be an entirely
1038 1.6 christos different kind of object, altogether. Because of this, we can't
1039 1.6 christos assume self is a gdb.Value object and need to convert it from
1040 1.6 christos python as well. */
1041 1.6 christos arg1 = convert_value_from_python (self);
1042 1.6 christos if (arg1 == NULL)
1043 1.1 christos {
1044 1.6 christos do_cleanups (cleanup);
1045 1.6 christos return NULL;
1046 1.6 christos }
1047 1.1 christos
1048 1.6 christos arg2 = convert_value_from_python (other);
1049 1.6 christos if (arg2 == NULL)
1050 1.6 christos {
1051 1.6 christos do_cleanups (cleanup);
1052 1.6 christos return NULL;
1053 1.6 christos }
1054 1.1 christos
1055 1.6 christos switch (opcode)
1056 1.6 christos {
1057 1.6 christos case VALPY_ADD:
1058 1.6 christos {
1059 1.6 christos struct type *ltype = value_type (arg1);
1060 1.6 christos struct type *rtype = value_type (arg2);
1061 1.6 christos
1062 1.6 christos ltype = check_typedef (ltype);
1063 1.6 christos ltype = STRIP_REFERENCE (ltype);
1064 1.6 christos rtype = check_typedef (rtype);
1065 1.6 christos rtype = STRIP_REFERENCE (rtype);
1066 1.6 christos
1067 1.6 christos handled = 1;
1068 1.6 christos if (TYPE_CODE (ltype) == TYPE_CODE_PTR
1069 1.6 christos && is_integral_type (rtype))
1070 1.6 christos res_val = value_ptradd (arg1, value_as_long (arg2));
1071 1.6 christos else if (TYPE_CODE (rtype) == TYPE_CODE_PTR
1072 1.6 christos && is_integral_type (ltype))
1073 1.6 christos res_val = value_ptradd (arg2, value_as_long (arg1));
1074 1.6 christos else
1075 1.1 christos {
1076 1.6 christos handled = 0;
1077 1.6 christos op = BINOP_ADD;
1078 1.1 christos }
1079 1.6 christos }
1080 1.6 christos break;
1081 1.6 christos case VALPY_SUB:
1082 1.6 christos {
1083 1.6 christos struct type *ltype = value_type (arg1);
1084 1.6 christos struct type *rtype = value_type (arg2);
1085 1.6 christos
1086 1.6 christos ltype = check_typedef (ltype);
1087 1.6 christos ltype = STRIP_REFERENCE (ltype);
1088 1.6 christos rtype = check_typedef (rtype);
1089 1.6 christos rtype = STRIP_REFERENCE (rtype);
1090 1.6 christos
1091 1.6 christos handled = 1;
1092 1.6 christos if (TYPE_CODE (ltype) == TYPE_CODE_PTR
1093 1.6 christos && TYPE_CODE (rtype) == TYPE_CODE_PTR)
1094 1.6 christos /* A ptrdiff_t for the target would be preferable here. */
1095 1.6 christos res_val = value_from_longest (builtin_type_pyint,
1096 1.6 christos value_ptrdiff (arg1, arg2));
1097 1.6 christos else if (TYPE_CODE (ltype) == TYPE_CODE_PTR
1098 1.6 christos && is_integral_type (rtype))
1099 1.6 christos res_val = value_ptradd (arg1, - value_as_long (arg2));
1100 1.6 christos else
1101 1.1 christos {
1102 1.6 christos handled = 0;
1103 1.6 christos op = BINOP_SUB;
1104 1.6 christos }
1105 1.6 christos }
1106 1.6 christos break;
1107 1.6 christos case VALPY_MUL:
1108 1.6 christos op = BINOP_MUL;
1109 1.6 christos break;
1110 1.6 christos case VALPY_DIV:
1111 1.6 christos op = BINOP_DIV;
1112 1.6 christos break;
1113 1.6 christos case VALPY_REM:
1114 1.6 christos op = BINOP_REM;
1115 1.6 christos break;
1116 1.6 christos case VALPY_POW:
1117 1.6 christos op = BINOP_EXP;
1118 1.6 christos break;
1119 1.6 christos case VALPY_LSH:
1120 1.6 christos op = BINOP_LSH;
1121 1.6 christos break;
1122 1.6 christos case VALPY_RSH:
1123 1.6 christos op = BINOP_RSH;
1124 1.6 christos break;
1125 1.6 christos case VALPY_BITAND:
1126 1.6 christos op = BINOP_BITWISE_AND;
1127 1.6 christos break;
1128 1.6 christos case VALPY_BITOR:
1129 1.6 christos op = BINOP_BITWISE_IOR;
1130 1.6 christos break;
1131 1.6 christos case VALPY_BITXOR:
1132 1.6 christos op = BINOP_BITWISE_XOR;
1133 1.6 christos break;
1134 1.6 christos }
1135 1.6 christos
1136 1.6 christos if (!handled)
1137 1.6 christos {
1138 1.6 christos if (binop_user_defined_p (op, arg1, arg2))
1139 1.6 christos res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
1140 1.6 christos else
1141 1.6 christos res_val = value_binop (arg1, arg2, op);
1142 1.6 christos }
1143 1.1 christos
1144 1.6 christos if (res_val)
1145 1.6 christos result = value_to_value_object (res_val);
1146 1.1 christos
1147 1.6 christos do_cleanups (cleanup);
1148 1.6 christos return result;
1149 1.6 christos }
1150 1.3 christos
1151 1.6 christos /* Returns a value object which is the result of applying the operation
1152 1.6 christos specified by OPCODE to the given arguments. Returns NULL on error, with
1153 1.6 christos a python exception set. */
1154 1.6 christos static PyObject *
1155 1.6 christos valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
1156 1.6 christos {
1157 1.6 christos PyObject *result = NULL;
1158 1.1 christos
1159 1.6 christos TRY
1160 1.6 christos {
1161 1.6 christos result = valpy_binop_throw (opcode, self, other);
1162 1.1 christos }
1163 1.5 christos CATCH (except, RETURN_MASK_ALL)
1164 1.5 christos {
1165 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
1166 1.5 christos }
1167 1.5 christos END_CATCH
1168 1.1 christos
1169 1.1 christos return result;
1170 1.1 christos }
1171 1.1 christos
1172 1.1 christos static PyObject *
1173 1.1 christos valpy_add (PyObject *self, PyObject *other)
1174 1.1 christos {
1175 1.1 christos return valpy_binop (VALPY_ADD, self, other);
1176 1.1 christos }
1177 1.1 christos
1178 1.1 christos static PyObject *
1179 1.1 christos valpy_subtract (PyObject *self, PyObject *other)
1180 1.1 christos {
1181 1.1 christos return valpy_binop (VALPY_SUB, self, other);
1182 1.1 christos }
1183 1.1 christos
1184 1.1 christos static PyObject *
1185 1.1 christos valpy_multiply (PyObject *self, PyObject *other)
1186 1.1 christos {
1187 1.1 christos return valpy_binop (VALPY_MUL, self, other);
1188 1.1 christos }
1189 1.1 christos
1190 1.1 christos static PyObject *
1191 1.1 christos valpy_divide (PyObject *self, PyObject *other)
1192 1.1 christos {
1193 1.1 christos return valpy_binop (VALPY_DIV, self, other);
1194 1.1 christos }
1195 1.1 christos
1196 1.1 christos static PyObject *
1197 1.1 christos valpy_remainder (PyObject *self, PyObject *other)
1198 1.1 christos {
1199 1.1 christos return valpy_binop (VALPY_REM, self, other);
1200 1.1 christos }
1201 1.1 christos
1202 1.1 christos static PyObject *
1203 1.1 christos valpy_power (PyObject *self, PyObject *other, PyObject *unused)
1204 1.1 christos {
1205 1.1 christos /* We don't support the ternary form of pow. I don't know how to express
1206 1.1 christos that, so let's just throw NotImplementedError to at least do something
1207 1.1 christos about it. */
1208 1.1 christos if (unused != Py_None)
1209 1.1 christos {
1210 1.1 christos PyErr_SetString (PyExc_NotImplementedError,
1211 1.1 christos "Invalid operation on gdb.Value.");
1212 1.1 christos return NULL;
1213 1.1 christos }
1214 1.1 christos
1215 1.1 christos return valpy_binop (VALPY_POW, self, other);
1216 1.1 christos }
1217 1.1 christos
1218 1.1 christos static PyObject *
1219 1.1 christos valpy_negative (PyObject *self)
1220 1.1 christos {
1221 1.1 christos PyObject *result = NULL;
1222 1.1 christos
1223 1.5 christos TRY
1224 1.1 christos {
1225 1.1 christos /* Perhaps overkill, but consistency has some virtue. */
1226 1.1 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
1227 1.1 christos struct value *val;
1228 1.1 christos
1229 1.1 christos val = value_neg (((value_object *) self)->value);
1230 1.1 christos result = value_to_value_object (val);
1231 1.1 christos do_cleanups (cleanup);
1232 1.1 christos }
1233 1.5 christos CATCH (except, RETURN_MASK_ALL)
1234 1.5 christos {
1235 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
1236 1.5 christos }
1237 1.5 christos END_CATCH
1238 1.1 christos
1239 1.1 christos return result;
1240 1.1 christos }
1241 1.1 christos
1242 1.1 christos static PyObject *
1243 1.1 christos valpy_positive (PyObject *self)
1244 1.1 christos {
1245 1.1 christos return value_to_value_object (((value_object *) self)->value);
1246 1.1 christos }
1247 1.1 christos
1248 1.1 christos static PyObject *
1249 1.1 christos valpy_absolute (PyObject *self)
1250 1.1 christos {
1251 1.1 christos struct value *value = ((value_object *) self)->value;
1252 1.1 christos int isabs = 1;
1253 1.1 christos
1254 1.5 christos TRY
1255 1.1 christos {
1256 1.1 christos struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
1257 1.1 christos
1258 1.1 christos if (value_less (value, value_zero (value_type (value), not_lval)))
1259 1.1 christos isabs = 0;
1260 1.1 christos
1261 1.1 christos do_cleanups (cleanup);
1262 1.1 christos }
1263 1.5 christos CATCH (except, RETURN_MASK_ALL)
1264 1.5 christos {
1265 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
1266 1.5 christos }
1267 1.5 christos END_CATCH
1268 1.1 christos
1269 1.1 christos if (isabs)
1270 1.1 christos return valpy_positive (self);
1271 1.1 christos else
1272 1.1 christos return valpy_negative (self);
1273 1.1 christos }
1274 1.1 christos
1275 1.1 christos /* Implements boolean evaluation of gdb.Value. */
1276 1.1 christos static int
1277 1.1 christos valpy_nonzero (PyObject *self)
1278 1.1 christos {
1279 1.5 christos struct gdb_exception except = exception_none;
1280 1.1 christos value_object *self_value = (value_object *) self;
1281 1.1 christos struct type *type;
1282 1.1 christos int nonzero = 0; /* Appease GCC warning. */
1283 1.1 christos
1284 1.5 christos TRY
1285 1.1 christos {
1286 1.1 christos type = check_typedef (value_type (self_value->value));
1287 1.1 christos
1288 1.1 christos if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
1289 1.1 christos nonzero = !!value_as_long (self_value->value);
1290 1.1 christos else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1291 1.1 christos nonzero = value_as_double (self_value->value) != 0;
1292 1.1 christos else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1293 1.1 christos nonzero = !decimal_is_zero (value_contents (self_value->value),
1294 1.1 christos TYPE_LENGTH (type),
1295 1.1 christos gdbarch_byte_order (get_type_arch (type)));
1296 1.1 christos else
1297 1.1 christos /* All other values are True. */
1298 1.1 christos nonzero = 1;
1299 1.1 christos }
1300 1.5 christos CATCH (ex, RETURN_MASK_ALL)
1301 1.5 christos {
1302 1.5 christos except = ex;
1303 1.5 christos }
1304 1.5 christos END_CATCH
1305 1.5 christos
1306 1.1 christos /* This is not documented in the Python documentation, but if this
1307 1.1 christos function fails, return -1 as slot_nb_nonzero does (the default
1308 1.1 christos Python nonzero function). */
1309 1.1 christos GDB_PY_SET_HANDLE_EXCEPTION (except);
1310 1.1 christos
1311 1.1 christos return nonzero;
1312 1.1 christos }
1313 1.1 christos
1314 1.1 christos /* Implements ~ for value objects. */
1315 1.1 christos static PyObject *
1316 1.1 christos valpy_invert (PyObject *self)
1317 1.1 christos {
1318 1.1 christos struct value *val = NULL;
1319 1.1 christos
1320 1.5 christos TRY
1321 1.1 christos {
1322 1.1 christos val = value_complement (((value_object *) self)->value);
1323 1.1 christos }
1324 1.5 christos CATCH (except, RETURN_MASK_ALL)
1325 1.5 christos {
1326 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
1327 1.5 christos }
1328 1.5 christos END_CATCH
1329 1.1 christos
1330 1.1 christos return value_to_value_object (val);
1331 1.1 christos }
1332 1.1 christos
1333 1.1 christos /* Implements left shift for value objects. */
1334 1.1 christos static PyObject *
1335 1.1 christos valpy_lsh (PyObject *self, PyObject *other)
1336 1.1 christos {
1337 1.1 christos return valpy_binop (VALPY_LSH, self, other);
1338 1.1 christos }
1339 1.1 christos
1340 1.1 christos /* Implements right shift for value objects. */
1341 1.1 christos static PyObject *
1342 1.1 christos valpy_rsh (PyObject *self, PyObject *other)
1343 1.1 christos {
1344 1.1 christos return valpy_binop (VALPY_RSH, self, other);
1345 1.1 christos }
1346 1.1 christos
1347 1.1 christos /* Implements bitwise and for value objects. */
1348 1.1 christos static PyObject *
1349 1.1 christos valpy_and (PyObject *self, PyObject *other)
1350 1.1 christos {
1351 1.1 christos return valpy_binop (VALPY_BITAND, self, other);
1352 1.1 christos }
1353 1.1 christos
1354 1.1 christos /* Implements bitwise or for value objects. */
1355 1.1 christos static PyObject *
1356 1.1 christos valpy_or (PyObject *self, PyObject *other)
1357 1.1 christos {
1358 1.1 christos return valpy_binop (VALPY_BITOR, self, other);
1359 1.1 christos }
1360 1.1 christos
1361 1.1 christos /* Implements bitwise xor for value objects. */
1362 1.1 christos static PyObject *
1363 1.1 christos valpy_xor (PyObject *self, PyObject *other)
1364 1.1 christos {
1365 1.1 christos return valpy_binop (VALPY_BITXOR, self, other);
1366 1.1 christos }
1367 1.1 christos
1368 1.6 christos /* Helper for valpy_richcompare. Implements comparison operations for
1369 1.6 christos value objects. Returns true/false on success. Returns -1 with a
1370 1.6 christos Python exception set if a Python error is detected. Throws a GDB
1371 1.6 christos exception on other errors (memory error, etc.). */
1372 1.6 christos
1373 1.6 christos static int
1374 1.6 christos valpy_richcompare_throw (PyObject *self, PyObject *other, int op)
1375 1.6 christos {
1376 1.6 christos int result;
1377 1.6 christos struct value *value_other;
1378 1.6 christos struct value *value_self;
1379 1.6 christos struct value *mark = value_mark ();
1380 1.6 christos struct cleanup *cleanup;
1381 1.6 christos
1382 1.6 christos value_other = convert_value_from_python (other);
1383 1.6 christos if (value_other == NULL)
1384 1.6 christos return -1;
1385 1.6 christos
1386 1.6 christos cleanup = make_cleanup_value_free_to_mark (mark);
1387 1.6 christos
1388 1.6 christos value_self = ((value_object *) self)->value;
1389 1.6 christos
1390 1.6 christos switch (op)
1391 1.6 christos {
1392 1.6 christos case Py_LT:
1393 1.6 christos result = value_less (value_self, value_other);
1394 1.6 christos break;
1395 1.6 christos case Py_LE:
1396 1.6 christos result = value_less (value_self, value_other)
1397 1.6 christos || value_equal (value_self, value_other);
1398 1.6 christos break;
1399 1.6 christos case Py_EQ:
1400 1.6 christos result = value_equal (value_self, value_other);
1401 1.6 christos break;
1402 1.6 christos case Py_NE:
1403 1.6 christos result = !value_equal (value_self, value_other);
1404 1.6 christos break;
1405 1.6 christos case Py_GT:
1406 1.6 christos result = value_less (value_other, value_self);
1407 1.6 christos break;
1408 1.6 christos case Py_GE:
1409 1.6 christos result = (value_less (value_other, value_self)
1410 1.6 christos || value_equal (value_self, value_other));
1411 1.6 christos break;
1412 1.6 christos default:
1413 1.6 christos /* Can't happen. */
1414 1.6 christos PyErr_SetString (PyExc_NotImplementedError,
1415 1.6 christos _("Invalid operation on gdb.Value."));
1416 1.6 christos result = -1;
1417 1.6 christos break;
1418 1.6 christos }
1419 1.6 christos
1420 1.6 christos do_cleanups (cleanup);
1421 1.6 christos return result;
1422 1.6 christos }
1423 1.6 christos
1424 1.6 christos
1425 1.1 christos /* Implements comparison operations for value objects. Returns NULL on error,
1426 1.1 christos with a python exception set. */
1427 1.1 christos static PyObject *
1428 1.1 christos valpy_richcompare (PyObject *self, PyObject *other, int op)
1429 1.1 christos {
1430 1.1 christos int result = 0;
1431 1.1 christos
1432 1.1 christos if (other == Py_None)
1433 1.1 christos /* Comparing with None is special. From what I can tell, in Python
1434 1.1 christos None is smaller than anything else. */
1435 1.1 christos switch (op) {
1436 1.1 christos case Py_LT:
1437 1.1 christos case Py_LE:
1438 1.1 christos case Py_EQ:
1439 1.1 christos Py_RETURN_FALSE;
1440 1.1 christos case Py_NE:
1441 1.1 christos case Py_GT:
1442 1.1 christos case Py_GE:
1443 1.1 christos Py_RETURN_TRUE;
1444 1.1 christos default:
1445 1.1 christos /* Can't happen. */
1446 1.1 christos PyErr_SetString (PyExc_NotImplementedError,
1447 1.1 christos _("Invalid operation on gdb.Value."));
1448 1.1 christos return NULL;
1449 1.1 christos }
1450 1.1 christos
1451 1.5 christos TRY
1452 1.1 christos {
1453 1.6 christos result = valpy_richcompare_throw (self, other, op);
1454 1.1 christos }
1455 1.5 christos CATCH (except, RETURN_MASK_ALL)
1456 1.5 christos {
1457 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
1458 1.5 christos }
1459 1.5 christos END_CATCH
1460 1.1 christos
1461 1.1 christos /* In this case, the Python exception has already been set. */
1462 1.1 christos if (result < 0)
1463 1.1 christos return NULL;
1464 1.1 christos
1465 1.1 christos if (result == 1)
1466 1.1 christos Py_RETURN_TRUE;
1467 1.1 christos
1468 1.1 christos Py_RETURN_FALSE;
1469 1.1 christos }
1470 1.1 christos
1471 1.1 christos #ifndef IS_PY3K
1472 1.1 christos /* Implements conversion to int. */
1473 1.1 christos static PyObject *
1474 1.1 christos valpy_int (PyObject *self)
1475 1.1 christos {
1476 1.1 christos struct value *value = ((value_object *) self)->value;
1477 1.1 christos struct type *type = value_type (value);
1478 1.1 christos LONGEST l = 0;
1479 1.1 christos
1480 1.5 christos TRY
1481 1.1 christos {
1482 1.1 christos if (!is_integral_type (type))
1483 1.1 christos error (_("Cannot convert value to int."));
1484 1.1 christos
1485 1.1 christos l = value_as_long (value);
1486 1.1 christos }
1487 1.5 christos CATCH (except, RETURN_MASK_ALL)
1488 1.5 christos {
1489 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
1490 1.5 christos }
1491 1.5 christos END_CATCH
1492 1.1 christos
1493 1.1 christos return gdb_py_object_from_longest (l);
1494 1.1 christos }
1495 1.1 christos #endif
1496 1.1 christos
1497 1.1 christos /* Implements conversion to long. */
1498 1.1 christos static PyObject *
1499 1.1 christos valpy_long (PyObject *self)
1500 1.1 christos {
1501 1.1 christos struct value *value = ((value_object *) self)->value;
1502 1.1 christos struct type *type = value_type (value);
1503 1.1 christos LONGEST l = 0;
1504 1.1 christos
1505 1.5 christos TRY
1506 1.1 christos {
1507 1.6 christos type = check_typedef (type);
1508 1.1 christos
1509 1.1 christos if (!is_integral_type (type)
1510 1.1 christos && TYPE_CODE (type) != TYPE_CODE_PTR)
1511 1.1 christos error (_("Cannot convert value to long."));
1512 1.1 christos
1513 1.1 christos l = value_as_long (value);
1514 1.1 christos }
1515 1.5 christos CATCH (except, RETURN_MASK_ALL)
1516 1.5 christos {
1517 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
1518 1.5 christos }
1519 1.5 christos END_CATCH
1520 1.1 christos
1521 1.1 christos return gdb_py_long_from_longest (l);
1522 1.1 christos }
1523 1.1 christos
1524 1.1 christos /* Implements conversion to float. */
1525 1.1 christos static PyObject *
1526 1.1 christos valpy_float (PyObject *self)
1527 1.1 christos {
1528 1.1 christos struct value *value = ((value_object *) self)->value;
1529 1.1 christos struct type *type = value_type (value);
1530 1.1 christos double d = 0;
1531 1.1 christos
1532 1.5 christos TRY
1533 1.1 christos {
1534 1.6 christos type = check_typedef (type);
1535 1.1 christos
1536 1.1 christos if (TYPE_CODE (type) != TYPE_CODE_FLT)
1537 1.1 christos error (_("Cannot convert value to float."));
1538 1.1 christos
1539 1.1 christos d = value_as_double (value);
1540 1.1 christos }
1541 1.5 christos CATCH (except, RETURN_MASK_ALL)
1542 1.5 christos {
1543 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
1544 1.5 christos }
1545 1.5 christos END_CATCH
1546 1.1 christos
1547 1.1 christos return PyFloat_FromDouble (d);
1548 1.1 christos }
1549 1.1 christos
1550 1.1 christos /* Returns an object for a value which is released from the all_values chain,
1551 1.1 christos so its lifetime is not bound to the execution of a command. */
1552 1.1 christos PyObject *
1553 1.1 christos value_to_value_object (struct value *val)
1554 1.1 christos {
1555 1.1 christos value_object *val_obj;
1556 1.1 christos
1557 1.1 christos val_obj = PyObject_New (value_object, &value_object_type);
1558 1.1 christos if (val_obj != NULL)
1559 1.1 christos {
1560 1.1 christos val_obj->value = val;
1561 1.1 christos release_value_or_incref (val);
1562 1.1 christos val_obj->address = NULL;
1563 1.1 christos val_obj->type = NULL;
1564 1.1 christos val_obj->dynamic_type = NULL;
1565 1.1 christos note_value (val_obj);
1566 1.1 christos }
1567 1.1 christos
1568 1.1 christos return (PyObject *) val_obj;
1569 1.1 christos }
1570 1.1 christos
1571 1.1 christos /* Returns a borrowed reference to the struct value corresponding to
1572 1.1 christos the given value object. */
1573 1.1 christos struct value *
1574 1.1 christos value_object_to_value (PyObject *self)
1575 1.1 christos {
1576 1.1 christos value_object *real;
1577 1.1 christos
1578 1.1 christos if (! PyObject_TypeCheck (self, &value_object_type))
1579 1.1 christos return NULL;
1580 1.1 christos real = (value_object *) self;
1581 1.1 christos return real->value;
1582 1.1 christos }
1583 1.1 christos
1584 1.1 christos /* Try to convert a Python value to a gdb value. If the value cannot
1585 1.1 christos be converted, set a Python exception and return NULL. Returns a
1586 1.1 christos reference to a new value on the all_values chain. */
1587 1.1 christos
1588 1.1 christos struct value *
1589 1.1 christos convert_value_from_python (PyObject *obj)
1590 1.1 christos {
1591 1.1 christos struct value *value = NULL; /* -Wall */
1592 1.1 christos int cmp;
1593 1.1 christos
1594 1.1 christos gdb_assert (obj != NULL);
1595 1.1 christos
1596 1.5 christos TRY
1597 1.1 christos {
1598 1.1 christos if (PyBool_Check (obj))
1599 1.1 christos {
1600 1.1 christos cmp = PyObject_IsTrue (obj);
1601 1.1 christos if (cmp >= 0)
1602 1.1 christos value = value_from_longest (builtin_type_pybool, cmp);
1603 1.1 christos }
1604 1.1 christos /* Make a long logic check first. In Python 3.x, internally,
1605 1.1 christos all integers are represented as longs. In Python 2.x, there
1606 1.1 christos is still a differentiation internally between a PyInt and a
1607 1.1 christos PyLong. Explicitly do this long check conversion first. In
1608 1.1 christos GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
1609 1.1 christos to be done first to ensure we do not lose information in the
1610 1.1 christos conversion process. */
1611 1.1 christos else if (PyLong_Check (obj))
1612 1.1 christos {
1613 1.1 christos LONGEST l = PyLong_AsLongLong (obj);
1614 1.1 christos
1615 1.1 christos if (PyErr_Occurred ())
1616 1.1 christos {
1617 1.1 christos /* If the error was an overflow, we can try converting to
1618 1.1 christos ULONGEST instead. */
1619 1.1 christos if (PyErr_ExceptionMatches (PyExc_OverflowError))
1620 1.1 christos {
1621 1.1 christos PyObject *etype, *evalue, *etraceback, *zero;
1622 1.1 christos
1623 1.1 christos PyErr_Fetch (&etype, &evalue, &etraceback);
1624 1.1 christos zero = PyInt_FromLong (0);
1625 1.1 christos
1626 1.1 christos /* Check whether obj is positive. */
1627 1.1 christos if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0)
1628 1.1 christos {
1629 1.1 christos ULONGEST ul;
1630 1.1 christos
1631 1.1 christos ul = PyLong_AsUnsignedLongLong (obj);
1632 1.1 christos if (! PyErr_Occurred ())
1633 1.1 christos value = value_from_ulongest (builtin_type_upylong, ul);
1634 1.1 christos }
1635 1.1 christos else
1636 1.1 christos /* There's nothing we can do. */
1637 1.1 christos PyErr_Restore (etype, evalue, etraceback);
1638 1.1 christos
1639 1.1 christos Py_DECREF (zero);
1640 1.1 christos }
1641 1.1 christos }
1642 1.1 christos else
1643 1.1 christos value = value_from_longest (builtin_type_pylong, l);
1644 1.1 christos }
1645 1.1 christos else if (PyInt_Check (obj))
1646 1.1 christos {
1647 1.1 christos long l = PyInt_AsLong (obj);
1648 1.1 christos
1649 1.1 christos if (! PyErr_Occurred ())
1650 1.1 christos value = value_from_longest (builtin_type_pyint, l);
1651 1.1 christos }
1652 1.1 christos else if (PyFloat_Check (obj))
1653 1.1 christos {
1654 1.1 christos double d = PyFloat_AsDouble (obj);
1655 1.1 christos
1656 1.1 christos if (! PyErr_Occurred ())
1657 1.1 christos value = value_from_double (builtin_type_pyfloat, d);
1658 1.1 christos }
1659 1.1 christos else if (gdbpy_is_string (obj))
1660 1.1 christos {
1661 1.1 christos char *s;
1662 1.1 christos
1663 1.1 christos s = python_string_to_target_string (obj);
1664 1.1 christos if (s != NULL)
1665 1.1 christos {
1666 1.1 christos struct cleanup *old;
1667 1.1 christos
1668 1.1 christos old = make_cleanup (xfree, s);
1669 1.1 christos value = value_cstring (s, strlen (s), builtin_type_pychar);
1670 1.1 christos do_cleanups (old);
1671 1.1 christos }
1672 1.1 christos }
1673 1.1 christos else if (PyObject_TypeCheck (obj, &value_object_type))
1674 1.1 christos value = value_copy (((value_object *) obj)->value);
1675 1.1 christos else if (gdbpy_is_lazy_string (obj))
1676 1.1 christos {
1677 1.1 christos PyObject *result;
1678 1.1 christos
1679 1.1 christos result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst, NULL);
1680 1.1 christos value = value_copy (((value_object *) result)->value);
1681 1.1 christos }
1682 1.1 christos else
1683 1.1 christos #ifdef IS_PY3K
1684 1.1 christos PyErr_Format (PyExc_TypeError,
1685 1.1 christos _("Could not convert Python object: %S."), obj);
1686 1.1 christos #else
1687 1.1 christos PyErr_Format (PyExc_TypeError,
1688 1.1 christos _("Could not convert Python object: %s."),
1689 1.1 christos PyString_AsString (PyObject_Str (obj)));
1690 1.1 christos #endif
1691 1.1 christos }
1692 1.5 christos CATCH (except, RETURN_MASK_ALL)
1693 1.1 christos {
1694 1.1 christos PyErr_Format (except.reason == RETURN_QUIT
1695 1.1 christos ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
1696 1.1 christos "%s", except.message);
1697 1.1 christos return NULL;
1698 1.1 christos }
1699 1.5 christos END_CATCH
1700 1.1 christos
1701 1.1 christos return value;
1702 1.1 christos }
1703 1.1 christos
1704 1.1 christos /* Returns value object in the ARGth position in GDB's history. */
1705 1.1 christos PyObject *
1706 1.1 christos gdbpy_history (PyObject *self, PyObject *args)
1707 1.1 christos {
1708 1.1 christos int i;
1709 1.1 christos struct value *res_val = NULL; /* Initialize to appease gcc warning. */
1710 1.1 christos
1711 1.1 christos if (!PyArg_ParseTuple (args, "i", &i))
1712 1.1 christos return NULL;
1713 1.1 christos
1714 1.5 christos TRY
1715 1.1 christos {
1716 1.1 christos res_val = access_value_history (i);
1717 1.1 christos }
1718 1.5 christos CATCH (except, RETURN_MASK_ALL)
1719 1.5 christos {
1720 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
1721 1.5 christos }
1722 1.5 christos END_CATCH
1723 1.1 christos
1724 1.1 christos return value_to_value_object (res_val);
1725 1.1 christos }
1726 1.1 christos
1727 1.1 christos /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1728 1.1 christos
1729 1.1 christos int
1730 1.1 christos gdbpy_is_value_object (PyObject *obj)
1731 1.1 christos {
1732 1.1 christos return PyObject_TypeCheck (obj, &value_object_type);
1733 1.1 christos }
1734 1.1 christos
1735 1.1 christos int
1736 1.1 christos gdbpy_initialize_values (void)
1737 1.1 christos {
1738 1.1 christos if (PyType_Ready (&value_object_type) < 0)
1739 1.1 christos return -1;
1740 1.1 christos
1741 1.1 christos return gdb_pymodule_addobject (gdb_module, "Value",
1742 1.1 christos (PyObject *) &value_object_type);
1743 1.1 christos }
1744 1.1 christos
1745 1.1 christos
1746 1.1 christos
1748 1.1 christos static PyGetSetDef value_object_getset[] = {
1749 1.1 christos { "address", valpy_get_address, NULL, "The address of the value.",
1750 1.1 christos NULL },
1751 1.1 christos { "is_optimized_out", valpy_get_is_optimized_out, NULL,
1752 1.1 christos "Boolean telling whether the value is optimized "
1753 1.1 christos "out (i.e., not available).",
1754 1.1 christos NULL },
1755 1.1 christos { "type", valpy_get_type, NULL, "Type of the value.", NULL },
1756 1.1 christos { "dynamic_type", valpy_get_dynamic_type, NULL,
1757 1.1 christos "Dynamic type of the value.", NULL },
1758 1.1 christos { "is_lazy", valpy_get_is_lazy, NULL,
1759 1.1 christos "Boolean telling whether the value is lazy (not fetched yet\n\
1760 1.1 christos from the inferior). A lazy value is fetched when needed, or when\n\
1761 1.1 christos the \"fetch_lazy()\" method is called.", NULL },
1762 1.1 christos {NULL} /* Sentinel */
1763 1.1 christos };
1764 1.1 christos
1765 1.1 christos static PyMethodDef value_object_methods[] = {
1766 1.1 christos { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
1767 1.1 christos { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS,
1768 1.1 christos "dynamic_cast (gdb.Type) -> gdb.Value\n\
1769 1.1 christos Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1770 1.1 christos },
1771 1.1 christos { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS,
1772 1.1 christos "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1773 1.1 christos Cast the value to the supplied type, as if by the C++\n\
1774 1.1 christos reinterpret_cast operator."
1775 1.1 christos },
1776 1.1 christos { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
1777 1.1 christos { "referenced_value", valpy_referenced_value, METH_NOARGS,
1778 1.5 christos "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
1779 1.5 christos { "reference_value", valpy_reference_value, METH_NOARGS,
1780 1.5 christos "Return a value of type TYPE_CODE_REF referencing this value." },
1781 1.5 christos { "const_value", valpy_const_value, METH_NOARGS,
1782 1.1 christos "Return a 'const' qualied version of the same value." },
1783 1.1 christos { "lazy_string", (PyCFunction) valpy_lazy_string,
1784 1.1 christos METH_VARARGS | METH_KEYWORDS,
1785 1.1 christos "lazy_string ([encoding] [, length]) -> lazy_string\n\
1786 1.1 christos Return a lazy string representation of the value." },
1787 1.1 christos { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
1788 1.1 christos "string ([encoding] [, errors] [, length]) -> string\n\
1789 1.1 christos Return Unicode string representation of the value." },
1790 1.1 christos { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
1791 1.1 christos "Fetches the value from the inferior, if it was lazy." },
1792 1.1 christos {NULL} /* Sentinel */
1793 1.1 christos };
1794 1.1 christos
1795 1.1 christos static PyNumberMethods value_object_as_number = {
1796 1.1 christos valpy_add,
1797 1.1 christos valpy_subtract,
1798 1.1 christos valpy_multiply,
1799 1.1 christos #ifndef IS_PY3K
1800 1.1 christos valpy_divide,
1801 1.1 christos #endif
1802 1.1 christos valpy_remainder,
1803 1.1 christos NULL, /* nb_divmod */
1804 1.1 christos valpy_power, /* nb_power */
1805 1.1 christos valpy_negative, /* nb_negative */
1806 1.1 christos valpy_positive, /* nb_positive */
1807 1.1 christos valpy_absolute, /* nb_absolute */
1808 1.1 christos valpy_nonzero, /* nb_nonzero */
1809 1.1 christos valpy_invert, /* nb_invert */
1810 1.1 christos valpy_lsh, /* nb_lshift */
1811 1.1 christos valpy_rsh, /* nb_rshift */
1812 1.1 christos valpy_and, /* nb_and */
1813 1.1 christos valpy_xor, /* nb_xor */
1814 1.1 christos valpy_or, /* nb_or */
1815 1.1 christos #ifdef IS_PY3K
1816 1.1 christos valpy_long, /* nb_int */
1817 1.1 christos NULL, /* reserved */
1818 1.1 christos #else
1819 1.1 christos NULL, /* nb_coerce */
1820 1.1 christos valpy_int, /* nb_int */
1821 1.1 christos valpy_long, /* nb_long */
1822 1.1 christos #endif
1823 1.1 christos valpy_float, /* nb_float */
1824 1.1 christos #ifndef IS_PY3K
1825 1.1 christos NULL, /* nb_oct */
1826 1.1 christos NULL, /* nb_hex */
1827 1.1 christos #endif
1828 1.1 christos NULL, /* nb_inplace_add */
1829 1.1 christos NULL, /* nb_inplace_subtract */
1830 1.6 christos NULL, /* nb_inplace_multiply */
1831 1.6 christos #ifndef IS_PY3K
1832 1.6 christos NULL, /* nb_inplace_divide */
1833 1.1 christos #endif
1834 1.1 christos NULL, /* nb_inplace_remainder */
1835 1.1 christos NULL, /* nb_inplace_power */
1836 1.1 christos NULL, /* nb_inplace_lshift */
1837 1.1 christos NULL, /* nb_inplace_rshift */
1838 1.1 christos NULL, /* nb_inplace_and */
1839 1.1 christos NULL, /* nb_inplace_xor */
1840 1.1 christos NULL, /* nb_inplace_or */
1841 1.6 christos NULL, /* nb_floor_divide */
1842 1.6 christos valpy_divide, /* nb_true_divide */
1843 1.6 christos NULL, /* nb_inplace_floor_divide */
1844 1.6 christos NULL, /* nb_inplace_true_divide */
1845 1.6 christos #ifndef HAVE_LIBPYTHON2_4
1846 1.6 christos /* This was added in Python 2.5. */
1847 1.6 christos valpy_long, /* nb_index */
1848 1.1 christos #endif /* HAVE_LIBPYTHON2_4 */
1849 1.1 christos };
1850 1.1 christos
1851 1.1 christos static PyMappingMethods value_object_as_mapping = {
1852 1.1 christos valpy_length,
1853 1.1 christos valpy_getitem,
1854 1.1 christos valpy_setitem
1855 1.1 christos };
1856 1.1 christos
1857 1.1 christos PyTypeObject value_object_type = {
1858 1.1 christos PyVarObject_HEAD_INIT (NULL, 0)
1859 1.1 christos "gdb.Value", /*tp_name*/
1860 1.1 christos sizeof (value_object), /*tp_basicsize*/
1861 1.1 christos 0, /*tp_itemsize*/
1862 1.1 christos valpy_dealloc, /*tp_dealloc*/
1863 1.1 christos 0, /*tp_print*/
1864 1.1 christos 0, /*tp_getattr*/
1865 1.1 christos 0, /*tp_setattr*/
1866 1.1 christos 0, /*tp_compare*/
1867 1.1 christos 0, /*tp_repr*/
1868 1.1 christos &value_object_as_number, /*tp_as_number*/
1869 1.1 christos 0, /*tp_as_sequence*/
1870 1.1 christos &value_object_as_mapping, /*tp_as_mapping*/
1871 1.1 christos valpy_hash, /*tp_hash*/
1872 1.1 christos valpy_call, /*tp_call*/
1873 1.1 christos valpy_str, /*tp_str*/
1874 1.1 christos 0, /*tp_getattro*/
1875 1.1 christos 0, /*tp_setattro*/
1876 1.1 christos 0, /*tp_as_buffer*/
1877 1.1 christos Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES
1878 1.1 christos | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1879 1.1 christos "GDB value object", /* tp_doc */
1880 1.1 christos 0, /* tp_traverse */
1881 1.1 christos 0, /* tp_clear */
1882 1.1 christos valpy_richcompare, /* tp_richcompare */
1883 1.1 christos 0, /* tp_weaklistoffset */
1884 1.1 christos 0, /* tp_iter */
1885 1.1 christos 0, /* tp_iternext */
1886 1.1 christos value_object_methods, /* tp_methods */
1887 1.1 christos 0, /* tp_members */
1888 1.1 christos value_object_getset, /* tp_getset */
1889 1.1 christos 0, /* tp_base */
1890 1.1 christos 0, /* tp_dict */
1891 1.1 christos 0, /* tp_descr_get */
1892 1.1 christos 0, /* tp_descr_set */
1893 1.1 christos 0, /* tp_dictoffset */
1894 1.1 christos 0, /* tp_init */
1895 1.1 christos 0, /* tp_alloc */
1896 1.1 christos valpy_new /* tp_new */
1897 };
1898