OLD | NEW |
1 /* Python interface to values. | 1 /* Python interface to values. |
2 | 2 |
3 Copyright (C) 2008-2012 Free Software Foundation, Inc. | 3 Copyright (C) 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. |
4 | 4 |
5 This file is part of GDB. | 5 This file is part of GDB. |
6 | 6 |
7 This program is free software; you can redistribute it and/or modify | 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 | 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 | 9 the Free Software Foundation; either version 3 of the License, or |
10 (at your option) any later version. | 10 (at your option) any later version. |
11 | 11 |
12 This program is distributed in the hope that it will be useful, | 12 This program is distributed in the hope that it will be useful, |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 GNU General Public License for more details. | 15 GNU General Public License for more details. |
16 | 16 |
17 You should have received a copy of the GNU General Public License | 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/>. */ | 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
19 | 19 |
20 #include "defs.h" | 20 #include "defs.h" |
21 #include "gdb_assert.h" | 21 #include "gdb_assert.h" |
22 #include "charset.h" | 22 #include "charset.h" |
23 #include "value.h" | 23 #include "value.h" |
24 #include "exceptions.h" | 24 #include "exceptions.h" |
25 #include "language.h" | 25 #include "language.h" |
26 #include "dfp.h" | 26 #include "dfp.h" |
27 #include "valprint.h" | 27 #include "valprint.h" |
28 #include "infcall.h" | 28 #include "infcall.h" |
29 #include "expression.h" | 29 #include "expression.h" |
30 #include "cp-abi.h" | 30 #include "cp-abi.h" |
| 31 #include "python.h" |
31 | 32 |
32 #ifdef HAVE_PYTHON | 33 #ifdef HAVE_PYTHON |
33 | 34 |
34 #include "python-internal.h" | 35 #include "python-internal.h" |
35 | 36 |
36 /* Even though Python scalar types directly map to host types, we use | 37 /* Even though Python scalar types directly map to host types, we use |
37 target types here to remain consistent with the values system in | 38 target types here to remain consistent with the values system in |
38 GDB (which uses target arithmetic). */ | 39 GDB (which uses target arithmetic). */ |
39 | 40 |
40 /* Python's integer type corresponds to C's long type. */ | 41 /* Python's integer type corresponds to C's long type. */ |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 } | 144 } |
144 | 145 |
145 value = convert_value_from_python (PyTuple_GetItem (args, 0)); | 146 value = convert_value_from_python (PyTuple_GetItem (args, 0)); |
146 if (value == NULL) | 147 if (value == NULL) |
147 { | 148 { |
148 subtype->tp_free (value_obj); | 149 subtype->tp_free (value_obj); |
149 return NULL; | 150 return NULL; |
150 } | 151 } |
151 | 152 |
152 value_obj->value = value; | 153 value_obj->value = value; |
153 value_incref (value); | 154 release_value_or_incref (value); |
154 value_obj->address = NULL; | 155 value_obj->address = NULL; |
155 value_obj->type = NULL; | 156 value_obj->type = NULL; |
156 value_obj->dynamic_type = NULL; | 157 value_obj->dynamic_type = NULL; |
157 note_value (value_obj); | 158 note_value (value_obj); |
158 | 159 |
159 return (PyObject *) value_obj; | 160 return (PyObject *) value_obj; |
160 } | 161 } |
161 | 162 |
162 /* Iterate over all the Value objects, calling preserve_one_value on | 163 /* Iterate over all the Value objects, calling preserve_one_value on |
163 each. */ | 164 each. */ |
164 void | 165 void |
165 preserve_python_values (struct objfile *objfile, htab_t copied_types) | 166 preserve_python_values (struct objfile *objfile, htab_t copied_types) |
166 { | 167 { |
167 value_object *iter; | 168 value_object *iter; |
168 | 169 |
169 for (iter = values_in_python; iter; iter = iter->next) | 170 for (iter = values_in_python; iter; iter = iter->next) |
170 preserve_one_value (iter->value, objfile, copied_types); | 171 preserve_one_value (iter->value, objfile, copied_types); |
171 } | 172 } |
172 | 173 |
173 /* Given a value of a pointer type, apply the C unary * operator to it. */ | 174 /* Given a value of a pointer type, apply the C unary * operator to it. */ |
174 static PyObject * | 175 static PyObject * |
175 valpy_dereference (PyObject *self, PyObject *args) | 176 valpy_dereference (PyObject *self, PyObject *args) |
176 { | 177 { |
177 struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
178 volatile struct gdb_exception except; | 178 volatile struct gdb_exception except; |
| 179 PyObject *result = NULL; |
179 | 180 |
180 TRY_CATCH (except, RETURN_MASK_ALL) | 181 TRY_CATCH (except, RETURN_MASK_ALL) |
181 { | 182 { |
| 183 struct value *res_val; |
| 184 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 185 |
182 res_val = value_ind (((value_object *) self)->value); | 186 res_val = value_ind (((value_object *) self)->value); |
| 187 result = value_to_value_object (res_val); |
| 188 do_cleanups (cleanup); |
183 } | 189 } |
184 GDB_PY_HANDLE_EXCEPTION (except); | 190 GDB_PY_HANDLE_EXCEPTION (except); |
185 | 191 |
186 return value_to_value_object (res_val); | 192 return result; |
| 193 } |
| 194 |
| 195 /* Given a value of a pointer type or a reference type, return the value |
| 196 referenced. The difference between this function and valpy_dereference is |
| 197 that the latter applies * unary operator to a value, which need not always |
| 198 result in the value referenced. For example, for a value which is a reference |
| 199 to an 'int' pointer ('int *'), valpy_dereference will result in a value of |
| 200 type 'int' while valpy_referenced_value will result in a value of type |
| 201 'int *'. */ |
| 202 |
| 203 static PyObject * |
| 204 valpy_referenced_value (PyObject *self, PyObject *args) |
| 205 { |
| 206 volatile struct gdb_exception except; |
| 207 PyObject *result = NULL; |
| 208 |
| 209 TRY_CATCH (except, RETURN_MASK_ALL) |
| 210 { |
| 211 struct value *self_val, *res_val; |
| 212 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 213 |
| 214 self_val = ((value_object *) self)->value; |
| 215 switch (TYPE_CODE (check_typedef (value_type (self_val)))) |
| 216 { |
| 217 case TYPE_CODE_PTR: |
| 218 res_val = value_ind (self_val); |
| 219 break; |
| 220 case TYPE_CODE_REF: |
| 221 res_val = coerce_ref (self_val); |
| 222 break; |
| 223 default: |
| 224 error(_("Trying to get the referenced value from a value which is " |
| 225 "neither a pointer nor a reference.")); |
| 226 } |
| 227 |
| 228 result = value_to_value_object (res_val); |
| 229 do_cleanups (cleanup); |
| 230 } |
| 231 GDB_PY_HANDLE_EXCEPTION (except); |
| 232 |
| 233 return result; |
187 } | 234 } |
188 | 235 |
189 /* Return "&value". */ | 236 /* Return "&value". */ |
190 static PyObject * | 237 static PyObject * |
191 valpy_get_address (PyObject *self, void *closure) | 238 valpy_get_address (PyObject *self, void *closure) |
192 { | 239 { |
193 struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
194 value_object *val_obj = (value_object *) self; | 240 value_object *val_obj = (value_object *) self; |
195 volatile struct gdb_exception except; | 241 volatile struct gdb_exception except; |
196 | 242 |
197 if (!val_obj->address) | 243 if (!val_obj->address) |
198 { | 244 { |
199 TRY_CATCH (except, RETURN_MASK_ALL) | 245 TRY_CATCH (except, RETURN_MASK_ALL) |
200 { | 246 { |
| 247 struct value *res_val; |
| 248 struct cleanup *cleanup |
| 249 = make_cleanup_value_free_to_mark (value_mark ()); |
| 250 |
201 res_val = value_addr (val_obj->value); | 251 res_val = value_addr (val_obj->value); |
| 252 val_obj->address = value_to_value_object (res_val); |
| 253 do_cleanups (cleanup); |
202 } | 254 } |
203 if (except.reason < 0) | 255 if (except.reason < 0) |
204 { | 256 { |
205 val_obj->address = Py_None; | 257 val_obj->address = Py_None; |
206 Py_INCREF (Py_None); | 258 Py_INCREF (Py_None); |
207 } | 259 } |
208 else | |
209 val_obj->address = value_to_value_object (res_val); | |
210 } | 260 } |
211 | 261 |
212 Py_XINCREF (val_obj->address); | 262 Py_XINCREF (val_obj->address); |
213 | 263 |
214 return val_obj->address; | 264 return val_obj->address; |
215 } | 265 } |
216 | 266 |
217 /* Return type of the value. */ | 267 /* Return type of the value. */ |
218 static PyObject * | 268 static PyObject * |
219 valpy_get_type (PyObject *self, void *closure) | 269 valpy_get_type (PyObject *self, void *closure) |
(...skipping 21 matching lines...) Expand all Loading... |
241 | 291 |
242 if (obj->dynamic_type != NULL) | 292 if (obj->dynamic_type != NULL) |
243 { | 293 { |
244 Py_INCREF (obj->dynamic_type); | 294 Py_INCREF (obj->dynamic_type); |
245 return obj->dynamic_type; | 295 return obj->dynamic_type; |
246 } | 296 } |
247 | 297 |
248 TRY_CATCH (except, RETURN_MASK_ALL) | 298 TRY_CATCH (except, RETURN_MASK_ALL) |
249 { | 299 { |
250 struct value *val = obj->value; | 300 struct value *val = obj->value; |
| 301 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
251 | 302 |
252 type = value_type (val); | 303 type = value_type (val); |
253 CHECK_TYPEDEF (type); | 304 CHECK_TYPEDEF (type); |
254 | 305 |
255 if (((TYPE_CODE (type) == TYPE_CODE_PTR) | 306 if (((TYPE_CODE (type) == TYPE_CODE_PTR) |
256 || (TYPE_CODE (type) == TYPE_CODE_REF)) | 307 || (TYPE_CODE (type) == TYPE_CODE_REF)) |
257 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) | 308 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) |
258 { | 309 { |
259 struct value *target; | 310 struct value *target; |
260 int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR; | 311 int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR; |
261 | 312 |
262 target = value_ind (val); | 313 target = value_ind (val); |
263 type = value_rtti_type (target, NULL, NULL, NULL); | 314 type = value_rtti_type (target, NULL, NULL, NULL); |
264 | 315 |
265 if (type) | 316 if (type) |
266 { | 317 { |
267 if (was_pointer) | 318 if (was_pointer) |
268 type = lookup_pointer_type (type); | 319 type = lookup_pointer_type (type); |
269 else | 320 else |
270 type = lookup_reference_type (type); | 321 type = lookup_reference_type (type); |
271 } | 322 } |
272 } | 323 } |
273 else if (TYPE_CODE (type) == TYPE_CODE_CLASS) | 324 else if (TYPE_CODE (type) == TYPE_CODE_CLASS) |
274 type = value_rtti_type (val, NULL, NULL, NULL); | 325 type = value_rtti_type (val, NULL, NULL, NULL); |
275 else | 326 else |
276 { | 327 { |
277 /* Re-use object's static type. */ | 328 /* Re-use object's static type. */ |
278 type = NULL; | 329 type = NULL; |
279 } | 330 } |
| 331 |
| 332 do_cleanups (cleanup); |
280 } | 333 } |
281 GDB_PY_HANDLE_EXCEPTION (except); | 334 GDB_PY_HANDLE_EXCEPTION (except); |
282 | 335 |
283 if (type == NULL) | 336 if (type == NULL) |
284 { | 337 { |
285 /* Ensure that the TYPE field is ready. */ | 338 /* Ensure that the TYPE field is ready. */ |
286 if (!valpy_get_type (self, NULL)) | 339 if (!valpy_get_type (self, NULL)) |
287 return NULL; | 340 return NULL; |
288 /* We don't need to incref here, because valpy_get_type already | 341 /* We don't need to incref here, because valpy_get_type already |
289 did it for us. */ | 342 did it for us. */ |
(...skipping 14 matching lines...) Expand all Loading... |
304 the string is not encoded. If LENGTH is provided then the length | 357 the string is not encoded. If LENGTH is provided then the length |
305 parameter is set to LENGTH, otherwise length will be set to -1 (first | 358 parameter is set to LENGTH, otherwise length will be set to -1 (first |
306 null of appropriate with). */ | 359 null of appropriate with). */ |
307 static PyObject * | 360 static PyObject * |
308 valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) | 361 valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) |
309 { | 362 { |
310 gdb_py_longest length = -1; | 363 gdb_py_longest length = -1; |
311 struct value *value = ((value_object *) self)->value; | 364 struct value *value = ((value_object *) self)->value; |
312 const char *user_encoding = NULL; | 365 const char *user_encoding = NULL; |
313 static char *keywords[] = { "encoding", "length", NULL }; | 366 static char *keywords[] = { "encoding", "length", NULL }; |
314 PyObject *str_obj; | 367 PyObject *str_obj = NULL; |
315 volatile struct gdb_exception except; | 368 volatile struct gdb_exception except; |
316 | 369 |
317 if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords, | 370 if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords, |
318 &user_encoding, &length)) | 371 &user_encoding, &length)) |
319 return NULL; | 372 return NULL; |
320 | 373 |
321 TRY_CATCH (except, RETURN_MASK_ALL) | 374 TRY_CATCH (except, RETURN_MASK_ALL) |
322 { | 375 { |
| 376 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 377 |
323 if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR) | 378 if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR) |
324 value = value_ind (value); | 379 value = value_ind (value); |
| 380 |
| 381 str_obj = gdbpy_create_lazy_string_object (value_address (value), length, |
| 382 user_encoding, |
| 383 value_type (value)); |
| 384 |
| 385 do_cleanups (cleanup); |
325 } | 386 } |
326 GDB_PY_HANDLE_EXCEPTION (except); | 387 GDB_PY_HANDLE_EXCEPTION (except); |
327 | 388 |
328 str_obj = gdbpy_create_lazy_string_object (value_address (value), length, | 389 return str_obj; |
329 » » » » » user_encoding, | |
330 » » » » » value_type (value)); | |
331 | |
332 return (PyObject *) str_obj; | |
333 } | 390 } |
334 | 391 |
335 /* Implementation of gdb.Value.string ([encoding] [, errors] | 392 /* Implementation of gdb.Value.string ([encoding] [, errors] |
336 [, length]) -> string. Return Unicode string with value contents. | 393 [, length]) -> string. Return Unicode string with value contents. |
337 If ENCODING is not given, the string is assumed to be encoded in | 394 If ENCODING is not given, the string is assumed to be encoded in |
338 the target's charset. If LENGTH is provided, only fetch string to | 395 the target's charset. If LENGTH is provided, only fetch string to |
339 the length provided. */ | 396 the length provided. */ |
340 | 397 |
341 static PyObject * | 398 static PyObject * |
342 valpy_string (PyObject *self, PyObject *args, PyObject *kw) | 399 valpy_string (PyObject *self, PyObject *args, PyObject *kw) |
(...skipping 26 matching lines...) Expand all Loading... |
369 xfree (buffer); | 426 xfree (buffer); |
370 | 427 |
371 return unicode; | 428 return unicode; |
372 } | 429 } |
373 | 430 |
374 /* A helper function that implements the various cast operators. */ | 431 /* A helper function that implements the various cast operators. */ |
375 | 432 |
376 static PyObject * | 433 static PyObject * |
377 valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) | 434 valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) |
378 { | 435 { |
379 PyObject *type_obj; | 436 PyObject *type_obj, *result = NULL; |
380 struct type *type; | 437 struct type *type; |
381 struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
382 volatile struct gdb_exception except; | 438 volatile struct gdb_exception except; |
383 | 439 |
384 if (! PyArg_ParseTuple (args, "O", &type_obj)) | 440 if (! PyArg_ParseTuple (args, "O", &type_obj)) |
385 return NULL; | 441 return NULL; |
386 | 442 |
387 type = type_object_to_type (type_obj); | 443 type = type_object_to_type (type_obj); |
388 if (! type) | 444 if (! type) |
389 { | 445 { |
390 PyErr_SetString (PyExc_RuntimeError, | 446 PyErr_SetString (PyExc_RuntimeError, |
391 _("Argument must be a type.")); | 447 _("Argument must be a type.")); |
392 return NULL; | 448 return NULL; |
393 } | 449 } |
394 | 450 |
395 TRY_CATCH (except, RETURN_MASK_ALL) | 451 TRY_CATCH (except, RETURN_MASK_ALL) |
396 { | 452 { |
397 struct value *val = ((value_object *) self)->value; | 453 struct value *val = ((value_object *) self)->value; |
| 454 struct value *res_val; |
| 455 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
398 | 456 |
399 if (op == UNOP_DYNAMIC_CAST) | 457 if (op == UNOP_DYNAMIC_CAST) |
400 res_val = value_dynamic_cast (type, val); | 458 res_val = value_dynamic_cast (type, val); |
401 else if (op == UNOP_REINTERPRET_CAST) | 459 else if (op == UNOP_REINTERPRET_CAST) |
402 res_val = value_reinterpret_cast (type, val); | 460 res_val = value_reinterpret_cast (type, val); |
403 else | 461 else |
404 { | 462 { |
405 gdb_assert (op == UNOP_CAST); | 463 gdb_assert (op == UNOP_CAST); |
406 res_val = value_cast (type, val); | 464 res_val = value_cast (type, val); |
407 } | 465 } |
| 466 |
| 467 result = value_to_value_object (res_val); |
| 468 do_cleanups (cleanup); |
408 } | 469 } |
409 GDB_PY_HANDLE_EXCEPTION (except); | 470 GDB_PY_HANDLE_EXCEPTION (except); |
410 | 471 |
411 return value_to_value_object (res_val); | 472 return result; |
412 } | 473 } |
413 | 474 |
414 /* Implementation of the "cast" method. */ | 475 /* Implementation of the "cast" method. */ |
415 | 476 |
416 static PyObject * | 477 static PyObject * |
417 valpy_cast (PyObject *self, PyObject *args) | 478 valpy_cast (PyObject *self, PyObject *args) |
418 { | 479 { |
419 return valpy_do_cast (self, args, UNOP_CAST); | 480 return valpy_do_cast (self, args, UNOP_CAST); |
420 } | 481 } |
421 | 482 |
(...skipping 22 matching lines...) Expand all Loading... |
444 return -1; | 505 return -1; |
445 } | 506 } |
446 | 507 |
447 /* Given string name of an element inside structure, return its value | 508 /* Given string name of an element inside structure, return its value |
448 object. Returns NULL on error, with a python exception set. */ | 509 object. Returns NULL on error, with a python exception set. */ |
449 static PyObject * | 510 static PyObject * |
450 valpy_getitem (PyObject *self, PyObject *key) | 511 valpy_getitem (PyObject *self, PyObject *key) |
451 { | 512 { |
452 value_object *self_value = (value_object *) self; | 513 value_object *self_value = (value_object *) self; |
453 char *field = NULL; | 514 char *field = NULL; |
454 struct value *res_val = NULL; | |
455 volatile struct gdb_exception except; | 515 volatile struct gdb_exception except; |
| 516 PyObject *result = NULL; |
456 | 517 |
457 if (gdbpy_is_string (key)) | 518 if (gdbpy_is_string (key)) |
458 { | 519 { |
459 field = python_string_to_host_string (key); | 520 field = python_string_to_host_string (key); |
460 if (field == NULL) | 521 if (field == NULL) |
461 return NULL; | 522 return NULL; |
462 } | 523 } |
463 | 524 |
464 TRY_CATCH (except, RETURN_MASK_ALL) | 525 TRY_CATCH (except, RETURN_MASK_ALL) |
465 { | 526 { |
466 struct value *tmp = self_value->value; | 527 struct value *tmp = self_value->value; |
| 528 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 529 struct value *res_val = NULL; |
467 | 530 |
468 if (field) | 531 if (field) |
469 res_val = value_struct_elt (&tmp, NULL, field, 0, NULL); | 532 res_val = value_struct_elt (&tmp, NULL, field, 0, NULL); |
470 else | 533 else |
471 { | 534 { |
472 /* Assume we are attempting an array access, and let the | 535 /* Assume we are attempting an array access, and let the |
473 value code throw an exception if the index has an invalid | 536 value code throw an exception if the index has an invalid |
474 type. */ | 537 type. */ |
475 struct value *idx = convert_value_from_python (key); | 538 struct value *idx = convert_value_from_python (key); |
476 | 539 |
477 if (idx != NULL) | 540 if (idx != NULL) |
478 { | 541 { |
479 /* Check the value's type is something that can be accessed via | 542 /* Check the value's type is something that can be accessed via |
480 a subscript. */ | 543 a subscript. */ |
481 struct type *type; | 544 struct type *type; |
482 | 545 |
483 tmp = coerce_ref (tmp); | 546 tmp = coerce_ref (tmp); |
484 type = check_typedef (value_type (tmp)); | 547 type = check_typedef (value_type (tmp)); |
485 if (TYPE_CODE (type) != TYPE_CODE_ARRAY | 548 if (TYPE_CODE (type) != TYPE_CODE_ARRAY |
486 && TYPE_CODE (type) != TYPE_CODE_PTR) | 549 && TYPE_CODE (type) != TYPE_CODE_PTR) |
487 error (_("Cannot subscript requested type.")); | 550 error (_("Cannot subscript requested type.")); |
488 else | 551 else |
489 res_val = value_subscript (tmp, value_as_long (idx)); | 552 res_val = value_subscript (tmp, value_as_long (idx)); |
490 } | 553 } |
491 } | 554 } |
| 555 |
| 556 if (res_val) |
| 557 result = value_to_value_object (res_val); |
| 558 do_cleanups (cleanup); |
492 } | 559 } |
493 | 560 |
494 xfree (field); | 561 xfree (field); |
495 GDB_PY_HANDLE_EXCEPTION (except); | 562 GDB_PY_HANDLE_EXCEPTION (except); |
496 | 563 |
497 return res_val ? value_to_value_object (res_val) : NULL; | 564 return result; |
498 } | 565 } |
499 | 566 |
500 static int | 567 static int |
501 valpy_setitem (PyObject *self, PyObject *key, PyObject *value) | 568 valpy_setitem (PyObject *self, PyObject *key, PyObject *value) |
502 { | 569 { |
503 PyErr_Format (PyExc_NotImplementedError, | 570 PyErr_Format (PyExc_NotImplementedError, |
504 _("Setting of struct elements is not currently supported.")); | 571 _("Setting of struct elements is not currently supported.")); |
505 return -1; | 572 return -1; |
506 } | 573 } |
507 | 574 |
508 /* Called by the Python interpreter to perform an inferior function | 575 /* Called by the Python interpreter to perform an inferior function |
509 call on the value. Returns NULL on error, with a python exception set. */ | 576 call on the value. Returns NULL on error, with a python exception set. */ |
510 static PyObject * | 577 static PyObject * |
511 valpy_call (PyObject *self, PyObject *args, PyObject *keywords) | 578 valpy_call (PyObject *self, PyObject *args, PyObject *keywords) |
512 { | 579 { |
513 struct value *return_value = NULL; | |
514 Py_ssize_t args_count; | 580 Py_ssize_t args_count; |
515 volatile struct gdb_exception except; | 581 volatile struct gdb_exception except; |
516 struct value *function = ((value_object *) self)->value; | 582 struct value *function = ((value_object *) self)->value; |
517 struct value **vargs = NULL; | 583 struct value **vargs = NULL; |
518 struct type *ftype = NULL; | 584 struct type *ftype = NULL; |
| 585 struct value *mark = value_mark (); |
| 586 PyObject *result = NULL; |
519 | 587 |
520 TRY_CATCH (except, RETURN_MASK_ALL) | 588 TRY_CATCH (except, RETURN_MASK_ALL) |
521 { | 589 { |
522 ftype = check_typedef (value_type (function)); | 590 ftype = check_typedef (value_type (function)); |
523 } | 591 } |
524 GDB_PY_HANDLE_EXCEPTION (except); | 592 GDB_PY_HANDLE_EXCEPTION (except); |
525 | 593 |
526 if (TYPE_CODE (ftype) != TYPE_CODE_FUNC) | 594 if (TYPE_CODE (ftype) != TYPE_CODE_FUNC) |
527 { | 595 { |
528 PyErr_SetString (PyExc_RuntimeError, | 596 PyErr_SetString (PyExc_RuntimeError, |
(...skipping 22 matching lines...) Expand all Loading... |
551 return NULL; | 619 return NULL; |
552 | 620 |
553 vargs[i] = convert_value_from_python (item); | 621 vargs[i] = convert_value_from_python (item); |
554 if (vargs[i] == NULL) | 622 if (vargs[i] == NULL) |
555 return NULL; | 623 return NULL; |
556 } | 624 } |
557 } | 625 } |
558 | 626 |
559 TRY_CATCH (except, RETURN_MASK_ALL) | 627 TRY_CATCH (except, RETURN_MASK_ALL) |
560 { | 628 { |
| 629 struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark); |
| 630 struct value *return_value; |
| 631 |
561 return_value = call_function_by_hand (function, args_count, vargs); | 632 return_value = call_function_by_hand (function, args_count, vargs); |
| 633 result = value_to_value_object (return_value); |
| 634 do_cleanups (cleanup); |
562 } | 635 } |
563 GDB_PY_HANDLE_EXCEPTION (except); | 636 GDB_PY_HANDLE_EXCEPTION (except); |
564 | 637 |
565 return value_to_value_object (return_value); | 638 return result; |
566 } | 639 } |
567 | 640 |
568 /* Called by the Python interpreter to obtain string representation | 641 /* Called by the Python interpreter to obtain string representation |
569 of the object. */ | 642 of the object. */ |
570 static PyObject * | 643 static PyObject * |
571 valpy_str (PyObject *self) | 644 valpy_str (PyObject *self) |
572 { | 645 { |
573 char *s = NULL; | 646 char *s = NULL; |
574 PyObject *result; | 647 PyObject *result; |
575 struct value_print_options opts; | 648 struct value_print_options opts; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 /* If TYPE is a reference, return the target; otherwise return TYPE. */ | 753 /* If TYPE is a reference, return the target; otherwise return TYPE. */ |
681 #define STRIP_REFERENCE(TYPE) \ | 754 #define STRIP_REFERENCE(TYPE) \ |
682 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE)) | 755 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE)) |
683 | 756 |
684 /* Returns a value object which is the result of applying the operation | 757 /* Returns a value object which is the result of applying the operation |
685 specified by OPCODE to the given arguments. Returns NULL on error, with | 758 specified by OPCODE to the given arguments. Returns NULL on error, with |
686 a python exception set. */ | 759 a python exception set. */ |
687 static PyObject * | 760 static PyObject * |
688 valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) | 761 valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) |
689 { | 762 { |
690 struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
691 volatile struct gdb_exception except; | 763 volatile struct gdb_exception except; |
| 764 PyObject *result = NULL; |
692 | 765 |
693 TRY_CATCH (except, RETURN_MASK_ALL) | 766 TRY_CATCH (except, RETURN_MASK_ALL) |
694 { | 767 { |
695 struct value *arg1, *arg2; | 768 struct value *arg1, *arg2; |
| 769 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 770 struct value *res_val = NULL; |
696 | 771 |
697 /* If the gdb.Value object is the second operand, then it will be passed | 772 /* If the gdb.Value object is the second operand, then it will be passed |
698 to us as the OTHER argument, and SELF will be an entirely different | 773 to us as the OTHER argument, and SELF will be an entirely different |
699 kind of object, altogether. Because of this, we can't assume self is | 774 kind of object, altogether. Because of this, we can't assume self is |
700 a gdb.Value object and need to convert it from python as well. */ | 775 a gdb.Value object and need to convert it from python as well. */ |
701 arg1 = convert_value_from_python (self); | 776 arg1 = convert_value_from_python (self); |
702 if (arg1 == NULL) | 777 if (arg1 == NULL) |
703 break; | 778 break; |
704 | 779 |
705 arg2 = convert_value_from_python (other); | 780 arg2 = convert_value_from_python (other); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
771 case VALPY_BITAND: | 846 case VALPY_BITAND: |
772 res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND); | 847 res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND); |
773 break; | 848 break; |
774 case VALPY_BITOR: | 849 case VALPY_BITOR: |
775 res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR); | 850 res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR); |
776 break; | 851 break; |
777 case VALPY_BITXOR: | 852 case VALPY_BITXOR: |
778 res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR); | 853 res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR); |
779 break; | 854 break; |
780 } | 855 } |
| 856 |
| 857 if (res_val) |
| 858 result = value_to_value_object (res_val); |
| 859 |
| 860 do_cleanups (cleanup); |
781 } | 861 } |
782 GDB_PY_HANDLE_EXCEPTION (except); | 862 GDB_PY_HANDLE_EXCEPTION (except); |
783 | 863 |
784 return res_val ? value_to_value_object (res_val) : NULL; | 864 return result; |
785 } | 865 } |
786 | 866 |
787 static PyObject * | 867 static PyObject * |
788 valpy_add (PyObject *self, PyObject *other) | 868 valpy_add (PyObject *self, PyObject *other) |
789 { | 869 { |
790 return valpy_binop (VALPY_ADD, self, other); | 870 return valpy_binop (VALPY_ADD, self, other); |
791 } | 871 } |
792 | 872 |
793 static PyObject * | 873 static PyObject * |
794 valpy_subtract (PyObject *self, PyObject *other) | 874 valpy_subtract (PyObject *self, PyObject *other) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
826 "Invalid operation on gdb.Value."); | 906 "Invalid operation on gdb.Value."); |
827 return NULL; | 907 return NULL; |
828 } | 908 } |
829 | 909 |
830 return valpy_binop (VALPY_POW, self, other); | 910 return valpy_binop (VALPY_POW, self, other); |
831 } | 911 } |
832 | 912 |
833 static PyObject * | 913 static PyObject * |
834 valpy_negative (PyObject *self) | 914 valpy_negative (PyObject *self) |
835 { | 915 { |
836 struct value *val = NULL; | |
837 volatile struct gdb_exception except; | 916 volatile struct gdb_exception except; |
| 917 PyObject *result = NULL; |
838 | 918 |
839 TRY_CATCH (except, RETURN_MASK_ALL) | 919 TRY_CATCH (except, RETURN_MASK_ALL) |
840 { | 920 { |
| 921 /* Perhaps overkill, but consistency has some virtue. */ |
| 922 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 923 struct value *val; |
| 924 |
841 val = value_neg (((value_object *) self)->value); | 925 val = value_neg (((value_object *) self)->value); |
| 926 result = value_to_value_object (val); |
| 927 do_cleanups (cleanup); |
842 } | 928 } |
843 GDB_PY_HANDLE_EXCEPTION (except); | 929 GDB_PY_HANDLE_EXCEPTION (except); |
844 | 930 |
845 return value_to_value_object (val); | 931 return result; |
846 } | 932 } |
847 | 933 |
848 static PyObject * | 934 static PyObject * |
849 valpy_positive (PyObject *self) | 935 valpy_positive (PyObject *self) |
850 { | 936 { |
851 return value_to_value_object (((value_object *) self)->value); | 937 return value_to_value_object (((value_object *) self)->value); |
852 } | 938 } |
853 | 939 |
854 static PyObject * | 940 static PyObject * |
855 valpy_absolute (PyObject *self) | 941 valpy_absolute (PyObject *self) |
856 { | 942 { |
857 struct value *value = ((value_object *) self)->value; | 943 struct value *value = ((value_object *) self)->value; |
858 volatile struct gdb_exception except; | 944 volatile struct gdb_exception except; |
859 int isabs = 1; | 945 int isabs = 1; |
860 | 946 |
861 TRY_CATCH (except, RETURN_MASK_ALL) | 947 TRY_CATCH (except, RETURN_MASK_ALL) |
862 { | 948 { |
| 949 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 950 |
863 if (value_less (value, value_zero (value_type (value), not_lval))) | 951 if (value_less (value, value_zero (value_type (value), not_lval))) |
864 isabs = 0; | 952 isabs = 0; |
| 953 |
| 954 do_cleanups (cleanup); |
865 } | 955 } |
866 GDB_PY_HANDLE_EXCEPTION (except); | 956 GDB_PY_HANDLE_EXCEPTION (except); |
867 | 957 |
868 if (isabs) | 958 if (isabs) |
869 return valpy_positive (self); | 959 return valpy_positive (self); |
870 else | 960 else |
871 return valpy_negative (self); | 961 return valpy_negative (self); |
872 } | 962 } |
873 | 963 |
874 /* Implements boolean evaluation of gdb.Value. */ | 964 /* Implements boolean evaluation of gdb.Value. */ |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
954 { | 1044 { |
955 return valpy_binop (VALPY_BITXOR, self, other); | 1045 return valpy_binop (VALPY_BITXOR, self, other); |
956 } | 1046 } |
957 | 1047 |
958 /* Implements comparison operations for value objects. Returns NULL on error, | 1048 /* Implements comparison operations for value objects. Returns NULL on error, |
959 with a python exception set. */ | 1049 with a python exception set. */ |
960 static PyObject * | 1050 static PyObject * |
961 valpy_richcompare (PyObject *self, PyObject *other, int op) | 1051 valpy_richcompare (PyObject *self, PyObject *other, int op) |
962 { | 1052 { |
963 int result = 0; | 1053 int result = 0; |
964 struct value *value_other; | |
965 volatile struct gdb_exception except; | 1054 volatile struct gdb_exception except; |
966 | 1055 |
967 if (other == Py_None) | 1056 if (other == Py_None) |
968 /* Comparing with None is special. From what I can tell, in Python | 1057 /* Comparing with None is special. From what I can tell, in Python |
969 None is smaller than anything else. */ | 1058 None is smaller than anything else. */ |
970 switch (op) { | 1059 switch (op) { |
971 case Py_LT: | 1060 case Py_LT: |
972 case Py_LE: | 1061 case Py_LE: |
973 case Py_EQ: | 1062 case Py_EQ: |
974 Py_RETURN_FALSE; | 1063 Py_RETURN_FALSE; |
975 case Py_NE: | 1064 case Py_NE: |
976 case Py_GT: | 1065 case Py_GT: |
977 case Py_GE: | 1066 case Py_GE: |
978 Py_RETURN_TRUE; | 1067 Py_RETURN_TRUE; |
979 default: | 1068 default: |
980 /* Can't happen. */ | 1069 /* Can't happen. */ |
981 PyErr_SetString (PyExc_NotImplementedError, | 1070 PyErr_SetString (PyExc_NotImplementedError, |
982 _("Invalid operation on gdb.Value.")); | 1071 _("Invalid operation on gdb.Value.")); |
983 return NULL; | 1072 return NULL; |
984 } | 1073 } |
985 | 1074 |
986 TRY_CATCH (except, RETURN_MASK_ALL) | 1075 TRY_CATCH (except, RETURN_MASK_ALL) |
987 { | 1076 { |
| 1077 struct value *value_other, *mark = value_mark (); |
| 1078 struct cleanup *cleanup; |
| 1079 |
988 value_other = convert_value_from_python (other); | 1080 value_other = convert_value_from_python (other); |
989 if (value_other == NULL) | 1081 if (value_other == NULL) |
990 { | 1082 { |
991 result = -1; | 1083 result = -1; |
992 break; | 1084 break; |
993 } | 1085 } |
994 | 1086 |
| 1087 cleanup = make_cleanup_value_free_to_mark (mark); |
| 1088 |
995 switch (op) { | 1089 switch (op) { |
996 case Py_LT: | 1090 case Py_LT: |
997 result = value_less (((value_object *) self)->value, value_other); | 1091 result = value_less (((value_object *) self)->value, value_other); |
998 break; | 1092 break; |
999 case Py_LE: | 1093 case Py_LE: |
1000 result = value_less (((value_object *) self)->value, value_other) | 1094 result = value_less (((value_object *) self)->value, value_other) |
1001 || value_equal (((value_object *) self)->value, value_other); | 1095 || value_equal (((value_object *) self)->value, value_other); |
1002 break; | 1096 break; |
1003 case Py_EQ: | 1097 case Py_EQ: |
1004 result = value_equal (((value_object *) self)->value, value_other); | 1098 result = value_equal (((value_object *) self)->value, value_other); |
1005 break; | 1099 break; |
1006 case Py_NE: | 1100 case Py_NE: |
1007 result = !value_equal (((value_object *) self)->value, value_other); | 1101 result = !value_equal (((value_object *) self)->value, value_other); |
1008 break; | 1102 break; |
1009 case Py_GT: | 1103 case Py_GT: |
1010 result = value_less (value_other, ((value_object *) self)->value); | 1104 result = value_less (value_other, ((value_object *) self)->value); |
1011 break; | 1105 break; |
1012 case Py_GE: | 1106 case Py_GE: |
1013 result = value_less (value_other, ((value_object *) self)->value) | 1107 result = value_less (value_other, ((value_object *) self)->value) |
1014 || value_equal (((value_object *) self)->value, value_other); | 1108 || value_equal (((value_object *) self)->value, value_other); |
1015 break; | 1109 break; |
1016 default: | 1110 default: |
1017 /* Can't happen. */ | 1111 /* Can't happen. */ |
1018 PyErr_SetString (PyExc_NotImplementedError, | 1112 PyErr_SetString (PyExc_NotImplementedError, |
1019 _("Invalid operation on gdb.Value.")); | 1113 _("Invalid operation on gdb.Value.")); |
1020 result = -1; | 1114 result = -1; |
1021 break; | 1115 break; |
1022 } | 1116 } |
| 1117 |
| 1118 do_cleanups (cleanup); |
1023 } | 1119 } |
1024 GDB_PY_HANDLE_EXCEPTION (except); | 1120 GDB_PY_HANDLE_EXCEPTION (except); |
1025 | 1121 |
1026 /* In this case, the Python exception has already been set. */ | 1122 /* In this case, the Python exception has already been set. */ |
1027 if (result < 0) | 1123 if (result < 0) |
1028 return NULL; | 1124 return NULL; |
1029 | 1125 |
1030 if (result == 1) | 1126 if (result == 1) |
1031 Py_RETURN_TRUE; | 1127 Py_RETURN_TRUE; |
1032 | 1128 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1116 so its lifetime is not bound to the execution of a command. */ | 1212 so its lifetime is not bound to the execution of a command. */ |
1117 PyObject * | 1213 PyObject * |
1118 value_to_value_object (struct value *val) | 1214 value_to_value_object (struct value *val) |
1119 { | 1215 { |
1120 value_object *val_obj; | 1216 value_object *val_obj; |
1121 | 1217 |
1122 val_obj = PyObject_New (value_object, &value_object_type); | 1218 val_obj = PyObject_New (value_object, &value_object_type); |
1123 if (val_obj != NULL) | 1219 if (val_obj != NULL) |
1124 { | 1220 { |
1125 val_obj->value = val; | 1221 val_obj->value = val; |
1126 value_incref (val); | 1222 release_value_or_incref (val); |
1127 val_obj->address = NULL; | 1223 val_obj->address = NULL; |
1128 val_obj->type = NULL; | 1224 val_obj->type = NULL; |
1129 val_obj->dynamic_type = NULL; | 1225 val_obj->dynamic_type = NULL; |
1130 note_value (val_obj); | 1226 note_value (val_obj); |
1131 } | 1227 } |
1132 | 1228 |
1133 return (PyObject *) val_obj; | 1229 return (PyObject *) val_obj; |
1134 } | 1230 } |
1135 | 1231 |
1136 /* Returns a borrowed reference to the struct value corresponding to | 1232 /* Returns a borrowed reference to the struct value corresponding to |
(...skipping 10 matching lines...) Expand all Loading... |
1147 } | 1243 } |
1148 | 1244 |
1149 /* Try to convert a Python value to a gdb value. If the value cannot | 1245 /* Try to convert a Python value to a gdb value. If the value cannot |
1150 be converted, set a Python exception and return NULL. Returns a | 1246 be converted, set a Python exception and return NULL. Returns a |
1151 reference to a new value on the all_values chain. */ | 1247 reference to a new value on the all_values chain. */ |
1152 | 1248 |
1153 struct value * | 1249 struct value * |
1154 convert_value_from_python (PyObject *obj) | 1250 convert_value_from_python (PyObject *obj) |
1155 { | 1251 { |
1156 struct value *value = NULL; /* -Wall */ | 1252 struct value *value = NULL; /* -Wall */ |
1157 struct cleanup *old; | |
1158 volatile struct gdb_exception except; | 1253 volatile struct gdb_exception except; |
1159 int cmp; | 1254 int cmp; |
1160 | 1255 |
1161 gdb_assert (obj != NULL); | 1256 gdb_assert (obj != NULL); |
1162 | 1257 |
1163 TRY_CATCH (except, RETURN_MASK_ALL) | 1258 TRY_CATCH (except, RETURN_MASK_ALL) |
1164 { | 1259 { |
1165 if (PyBool_Check (obj)) | 1260 if (PyBool_Check (obj)) |
1166 { | 1261 { |
1167 cmp = PyObject_IsTrue (obj); | 1262 cmp = PyObject_IsTrue (obj); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1216 if (! PyErr_Occurred ()) | 1311 if (! PyErr_Occurred ()) |
1217 value = value_from_double (builtin_type_pyfloat, d); | 1312 value = value_from_double (builtin_type_pyfloat, d); |
1218 } | 1313 } |
1219 else if (gdbpy_is_string (obj)) | 1314 else if (gdbpy_is_string (obj)) |
1220 { | 1315 { |
1221 char *s; | 1316 char *s; |
1222 | 1317 |
1223 s = python_string_to_target_string (obj); | 1318 s = python_string_to_target_string (obj); |
1224 if (s != NULL) | 1319 if (s != NULL) |
1225 { | 1320 { |
| 1321 struct cleanup *old; |
| 1322 |
1226 old = make_cleanup (xfree, s); | 1323 old = make_cleanup (xfree, s); |
1227 value = value_cstring (s, strlen (s), builtin_type_pychar); | 1324 value = value_cstring (s, strlen (s), builtin_type_pychar); |
1228 do_cleanups (old); | 1325 do_cleanups (old); |
1229 } | 1326 } |
1230 } | 1327 } |
1231 else if (PyObject_TypeCheck (obj, &value_object_type)) | 1328 else if (PyObject_TypeCheck (obj, &value_object_type)) |
1232 value = value_copy (((value_object *) obj)->value); | 1329 value = value_copy (((value_object *) obj)->value); |
1233 else if (gdbpy_is_lazy_string (obj)) | 1330 else if (gdbpy_is_lazy_string (obj)) |
1234 { | 1331 { |
1235 PyObject *result; | 1332 PyObject *result; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1314 }; | 1411 }; |
1315 | 1412 |
1316 static PyMethodDef value_object_methods[] = { | 1413 static PyMethodDef value_object_methods[] = { |
1317 { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." }, | 1414 { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." }, |
1318 { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS, | 1415 { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS, |
1319 "dynamic_cast (gdb.Type) -> gdb.Value\n\ | 1416 "dynamic_cast (gdb.Type) -> gdb.Value\n\ |
1320 Cast the value to the supplied type, as if by the C++ dynamic_cast operator." | 1417 Cast the value to the supplied type, as if by the C++ dynamic_cast operator." |
1321 }, | 1418 }, |
1322 { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS, | 1419 { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS, |
1323 "reinterpret_cast (gdb.Type) -> gdb.Value\n\ | 1420 "reinterpret_cast (gdb.Type) -> gdb.Value\n\ |
error: old chunk mismatch |
None
OLD | NEW |