| 1 | /* Python interface to stack frames |
| 2 | |
| 3 | Copyright (C) 2008-2020 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "charset.h" |
| 22 | #include "block.h" |
| 23 | #include "frame.h" |
| 24 | #include "symtab.h" |
| 25 | #include "stack.h" |
| 26 | #include "value.h" |
| 27 | #include "python-internal.h" |
| 28 | #include "symfile.h" |
| 29 | #include "objfiles.h" |
| 30 | #include "user-regs.h" |
| 31 | |
| 32 | typedef struct { |
| 33 | PyObject_HEAD |
| 34 | struct frame_id frame_id; |
| 35 | struct gdbarch *gdbarch; |
| 36 | |
| 37 | /* Marks that the FRAME_ID member actually holds the ID of the frame next |
| 38 | to this, and not this frames' ID itself. This is a hack to permit Python |
| 39 | frame objects which represent invalid frames (i.e., the last frame_info |
| 40 | in a corrupt stack). The problem arises from the fact that this code |
| 41 | relies on FRAME_ID to uniquely identify a frame, which is not always true |
| 42 | for the last "frame" in a corrupt stack (it can have a null ID, or the same |
| 43 | ID as the previous frame). Whenever get_prev_frame returns NULL, we |
| 44 | record the frame_id of the next frame and set FRAME_ID_IS_NEXT to 1. */ |
| 45 | int frame_id_is_next; |
| 46 | } frame_object; |
| 47 | |
| 48 | /* Require a valid frame. This must be called inside a TRY_CATCH, or |
| 49 | another context in which a gdb exception is allowed. */ |
| 50 | #define FRAPY_REQUIRE_VALID(frame_obj, frame) \ |
| 51 | do { \ |
| 52 | frame = frame_object_to_frame_info (frame_obj); \ |
| 53 | if (frame == NULL) \ |
| 54 | error (_("Frame is invalid.")); \ |
| 55 | } while (0) |
| 56 | |
| 57 | /* Returns the frame_info object corresponding to the given Python Frame |
| 58 | object. If the frame doesn't exist anymore (the frame id doesn't |
| 59 | correspond to any frame in the inferior), returns NULL. */ |
| 60 | |
| 61 | struct frame_info * |
| 62 | frame_object_to_frame_info (PyObject *obj) |
| 63 | { |
| 64 | frame_object *frame_obj = (frame_object *) obj; |
| 65 | struct frame_info *frame; |
| 66 | |
| 67 | frame = frame_find_by_id (frame_obj->frame_id); |
| 68 | if (frame == NULL) |
| 69 | return NULL; |
| 70 | |
| 71 | if (frame_obj->frame_id_is_next) |
| 72 | frame = get_prev_frame (frame); |
| 73 | |
| 74 | return frame; |
| 75 | } |
| 76 | |
| 77 | /* Called by the Python interpreter to obtain string representation |
| 78 | of the object. */ |
| 79 | |
| 80 | static PyObject * |
| 81 | frapy_str (PyObject *self) |
| 82 | { |
| 83 | string_file strfile; |
| 84 | |
| 85 | fprint_frame_id (&strfile, ((frame_object *) self)->frame_id); |
| 86 | return PyString_FromString (strfile.c_str ()); |
| 87 | } |
| 88 | |
| 89 | /* Implementation of gdb.Frame.is_valid (self) -> Boolean. |
| 90 | Returns True if the frame corresponding to the frame_id of this |
| 91 | object still exists in the inferior. */ |
| 92 | |
| 93 | static PyObject * |
| 94 | frapy_is_valid (PyObject *self, PyObject *args) |
| 95 | { |
| 96 | struct frame_info *frame = NULL; |
| 97 | |
| 98 | try |
| 99 | { |
| 100 | frame = frame_object_to_frame_info (self); |
| 101 | } |
| 102 | catch (const gdb_exception &except) |
| 103 | { |
| 104 | GDB_PY_HANDLE_EXCEPTION (except); |
| 105 | } |
| 106 | |
| 107 | if (frame == NULL) |
| 108 | Py_RETURN_FALSE; |
| 109 | |
| 110 | Py_RETURN_TRUE; |
| 111 | } |
| 112 | |
| 113 | /* Implementation of gdb.Frame.name (self) -> String. |
| 114 | Returns the name of the function corresponding to this frame. */ |
| 115 | |
| 116 | static PyObject * |
| 117 | frapy_name (PyObject *self, PyObject *args) |
| 118 | { |
| 119 | struct frame_info *frame; |
| 120 | gdb::unique_xmalloc_ptr<char> name; |
| 121 | enum language lang; |
| 122 | PyObject *result; |
| 123 | |
| 124 | try |
| 125 | { |
| 126 | FRAPY_REQUIRE_VALID (self, frame); |
| 127 | |
| 128 | name = find_frame_funname (frame, &lang, NULL); |
| 129 | } |
| 130 | catch (const gdb_exception &except) |
| 131 | { |
| 132 | GDB_PY_HANDLE_EXCEPTION (except); |
| 133 | } |
| 134 | |
| 135 | if (name) |
| 136 | { |
| 137 | result = PyUnicode_Decode (name.get (), strlen (name.get ()), |
| 138 | host_charset (), NULL); |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | result = Py_None; |
| 143 | Py_INCREF (Py_None); |
| 144 | } |
| 145 | |
| 146 | return result; |
| 147 | } |
| 148 | |
| 149 | /* Implementation of gdb.Frame.type (self) -> Integer. |
| 150 | Returns the frame type, namely one of the gdb.*_FRAME constants. */ |
| 151 | |
| 152 | static PyObject * |
| 153 | frapy_type (PyObject *self, PyObject *args) |
| 154 | { |
| 155 | struct frame_info *frame; |
| 156 | enum frame_type type = NORMAL_FRAME;/* Initialize to appease gcc warning. */ |
| 157 | |
| 158 | try |
| 159 | { |
| 160 | FRAPY_REQUIRE_VALID (self, frame); |
| 161 | |
| 162 | type = get_frame_type (frame); |
| 163 | } |
| 164 | catch (const gdb_exception &except) |
| 165 | { |
| 166 | GDB_PY_HANDLE_EXCEPTION (except); |
| 167 | } |
| 168 | |
| 169 | return PyInt_FromLong (type); |
| 170 | } |
| 171 | |
| 172 | /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture. |
| 173 | Returns the frame's architecture as a gdb.Architecture object. */ |
| 174 | |
| 175 | static PyObject * |
| 176 | frapy_arch (PyObject *self, PyObject *args) |
| 177 | { |
| 178 | struct frame_info *frame = NULL; /* Initialize to appease gcc warning. */ |
| 179 | frame_object *obj = (frame_object *) self; |
| 180 | |
| 181 | try |
| 182 | { |
| 183 | FRAPY_REQUIRE_VALID (self, frame); |
| 184 | } |
| 185 | catch (const gdb_exception &except) |
| 186 | { |
| 187 | GDB_PY_HANDLE_EXCEPTION (except); |
| 188 | } |
| 189 | |
| 190 | return gdbarch_to_arch_object (obj->gdbarch); |
| 191 | } |
| 192 | |
| 193 | /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer. |
| 194 | Returns one of the gdb.FRAME_UNWIND_* constants. */ |
| 195 | |
| 196 | static PyObject * |
| 197 | frapy_unwind_stop_reason (PyObject *self, PyObject *args) |
| 198 | { |
| 199 | struct frame_info *frame = NULL; /* Initialize to appease gcc warning. */ |
| 200 | enum unwind_stop_reason stop_reason; |
| 201 | |
| 202 | try |
| 203 | { |
| 204 | FRAPY_REQUIRE_VALID (self, frame); |
| 205 | } |
| 206 | catch (const gdb_exception &except) |
| 207 | { |
| 208 | GDB_PY_HANDLE_EXCEPTION (except); |
| 209 | } |
| 210 | |
| 211 | stop_reason = get_frame_unwind_stop_reason (frame); |
| 212 | |
| 213 | return PyInt_FromLong (stop_reason); |
| 214 | } |
| 215 | |
| 216 | /* Implementation of gdb.Frame.pc (self) -> Long. |
| 217 | Returns the frame's resume address. */ |
| 218 | |
| 219 | static PyObject * |
| 220 | frapy_pc (PyObject *self, PyObject *args) |
| 221 | { |
| 222 | CORE_ADDR pc = 0; /* Initialize to appease gcc warning. */ |
| 223 | struct frame_info *frame; |
| 224 | |
| 225 | try |
| 226 | { |
| 227 | FRAPY_REQUIRE_VALID (self, frame); |
| 228 | |
| 229 | pc = get_frame_pc (frame); |
| 230 | } |
| 231 | catch (const gdb_exception &except) |
| 232 | { |
| 233 | GDB_PY_HANDLE_EXCEPTION (except); |
| 234 | } |
| 235 | |
| 236 | return gdb_py_long_from_ulongest (pc); |
| 237 | } |
| 238 | |
| 239 | /* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value. |
| 240 | Returns the value of a register in this frame. */ |
| 241 | |
| 242 | static PyObject * |
| 243 | frapy_read_register (PyObject *self, PyObject *args) |
| 244 | { |
| 245 | const char *regnum_str; |
| 246 | struct value *val = NULL; |
| 247 | |
| 248 | if (!PyArg_ParseTuple (args, "s", ®num_str)) |
| 249 | return NULL; |
| 250 | |
| 251 | try |
| 252 | { |
| 253 | struct frame_info *frame; |
| 254 | int regnum; |
| 255 | |
| 256 | FRAPY_REQUIRE_VALID (self, frame); |
| 257 | |
| 258 | regnum = user_reg_map_name_to_regnum (get_frame_arch (frame), |
| 259 | regnum_str, |
| 260 | strlen (regnum_str)); |
| 261 | if (regnum >= 0) |
| 262 | val = value_of_register (regnum, frame); |
| 263 | |
| 264 | if (val == NULL) |
| 265 | PyErr_SetString (PyExc_ValueError, _("Unknown register.")); |
| 266 | } |
| 267 | catch (const gdb_exception &except) |
| 268 | { |
| 269 | GDB_PY_HANDLE_EXCEPTION (except); |
| 270 | } |
| 271 | |
| 272 | return val == NULL ? NULL : value_to_value_object (val); |
| 273 | } |
| 274 | |
| 275 | /* Implementation of gdb.Frame.block (self) -> gdb.Block. |
| 276 | Returns the frame's code block. */ |
| 277 | |
| 278 | static PyObject * |
| 279 | frapy_block (PyObject *self, PyObject *args) |
| 280 | { |
| 281 | struct frame_info *frame; |
| 282 | const struct block *block = NULL, *fn_block; |
| 283 | |
| 284 | try |
| 285 | { |
| 286 | FRAPY_REQUIRE_VALID (self, frame); |
| 287 | block = get_frame_block (frame, NULL); |
| 288 | } |
| 289 | catch (const gdb_exception &except) |
| 290 | { |
| 291 | GDB_PY_HANDLE_EXCEPTION (except); |
| 292 | } |
| 293 | |
| 294 | for (fn_block = block; |
| 295 | fn_block != NULL && BLOCK_FUNCTION (fn_block) == NULL; |
| 296 | fn_block = BLOCK_SUPERBLOCK (fn_block)) |
| 297 | ; |
| 298 | |
| 299 | if (block == NULL || fn_block == NULL || BLOCK_FUNCTION (fn_block) == NULL) |
| 300 | { |
| 301 | PyErr_SetString (PyExc_RuntimeError, |
| 302 | _("Cannot locate block for frame.")); |
| 303 | return NULL; |
| 304 | } |
| 305 | |
| 306 | if (block) |
| 307 | { |
| 308 | return block_to_block_object |
| 309 | (block, symbol_objfile (BLOCK_FUNCTION (fn_block))); |
| 310 | } |
| 311 | |
| 312 | Py_RETURN_NONE; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /* Implementation of gdb.Frame.function (self) -> gdb.Symbol. |
| 317 | Returns the symbol for the function corresponding to this frame. */ |
| 318 | |
| 319 | static PyObject * |
| 320 | frapy_function (PyObject *self, PyObject *args) |
| 321 | { |
| 322 | struct symbol *sym = NULL; |
| 323 | struct frame_info *frame; |
| 324 | |
| 325 | try |
| 326 | { |
| 327 | enum language funlang; |
| 328 | |
| 329 | FRAPY_REQUIRE_VALID (self, frame); |
| 330 | |
| 331 | gdb::unique_xmalloc_ptr<char> funname |
| 332 | = find_frame_funname (frame, &funlang, &sym); |
| 333 | } |
| 334 | catch (const gdb_exception &except) |
| 335 | { |
| 336 | GDB_PY_HANDLE_EXCEPTION (except); |
| 337 | } |
| 338 | |
| 339 | if (sym) |
| 340 | return symbol_to_symbol_object (sym); |
| 341 | |
| 342 | Py_RETURN_NONE; |
| 343 | } |
| 344 | |
| 345 | /* Convert a frame_info struct to a Python Frame object. |
| 346 | Sets a Python exception and returns NULL on error. */ |
| 347 | |
| 348 | PyObject * |
| 349 | frame_info_to_frame_object (struct frame_info *frame) |
| 350 | { |
| 351 | gdbpy_ref<frame_object> frame_obj (PyObject_New (frame_object, |
| 352 | &frame_object_type)); |
| 353 | if (frame_obj == NULL) |
| 354 | return NULL; |
| 355 | |
| 356 | try |
| 357 | { |
| 358 | |
| 359 | /* Try to get the previous frame, to determine if this is the last frame |
| 360 | in a corrupt stack. If so, we need to store the frame_id of the next |
| 361 | frame and not of this one (which is possibly invalid). */ |
| 362 | if (get_prev_frame (frame) == NULL |
| 363 | && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON |
| 364 | && get_next_frame (frame) != NULL) |
| 365 | { |
| 366 | frame_obj->frame_id = get_frame_id (get_next_frame (frame)); |
| 367 | frame_obj->frame_id_is_next = 1; |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | frame_obj->frame_id = get_frame_id (frame); |
| 372 | frame_obj->frame_id_is_next = 0; |
| 373 | } |
| 374 | frame_obj->gdbarch = get_frame_arch (frame); |
| 375 | } |
| 376 | catch (const gdb_exception &except) |
| 377 | { |
| 378 | gdbpy_convert_exception (except); |
| 379 | return NULL; |
| 380 | } |
| 381 | |
| 382 | return (PyObject *) frame_obj.release (); |
| 383 | } |
| 384 | |
| 385 | /* Implementation of gdb.Frame.older (self) -> gdb.Frame. |
| 386 | Returns the frame immediately older (outer) to this frame, or None if |
| 387 | there isn't one. */ |
| 388 | |
| 389 | static PyObject * |
| 390 | frapy_older (PyObject *self, PyObject *args) |
| 391 | { |
| 392 | struct frame_info *frame, *prev = NULL; |
| 393 | PyObject *prev_obj = NULL; /* Initialize to appease gcc warning. */ |
| 394 | |
| 395 | try |
| 396 | { |
| 397 | FRAPY_REQUIRE_VALID (self, frame); |
| 398 | |
| 399 | prev = get_prev_frame (frame); |
| 400 | } |
| 401 | catch (const gdb_exception &except) |
| 402 | { |
| 403 | GDB_PY_HANDLE_EXCEPTION (except); |
| 404 | } |
| 405 | |
| 406 | if (prev) |
| 407 | prev_obj = frame_info_to_frame_object (prev); |
| 408 | else |
| 409 | { |
| 410 | Py_INCREF (Py_None); |
| 411 | prev_obj = Py_None; |
| 412 | } |
| 413 | |
| 414 | return prev_obj; |
| 415 | } |
| 416 | |
| 417 | /* Implementation of gdb.Frame.newer (self) -> gdb.Frame. |
| 418 | Returns the frame immediately newer (inner) to this frame, or None if |
| 419 | there isn't one. */ |
| 420 | |
| 421 | static PyObject * |
| 422 | frapy_newer (PyObject *self, PyObject *args) |
| 423 | { |
| 424 | struct frame_info *frame, *next = NULL; |
| 425 | PyObject *next_obj = NULL; /* Initialize to appease gcc warning. */ |
| 426 | |
| 427 | try |
| 428 | { |
| 429 | FRAPY_REQUIRE_VALID (self, frame); |
| 430 | |
| 431 | next = get_next_frame (frame); |
| 432 | } |
| 433 | catch (const gdb_exception &except) |
| 434 | { |
| 435 | GDB_PY_HANDLE_EXCEPTION (except); |
| 436 | } |
| 437 | |
| 438 | if (next) |
| 439 | next_obj = frame_info_to_frame_object (next); |
| 440 | else |
| 441 | { |
| 442 | Py_INCREF (Py_None); |
| 443 | next_obj = Py_None; |
| 444 | } |
| 445 | |
| 446 | return next_obj; |
| 447 | } |
| 448 | |
| 449 | /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line. |
| 450 | Returns the frame's symtab and line. */ |
| 451 | |
| 452 | static PyObject * |
| 453 | frapy_find_sal (PyObject *self, PyObject *args) |
| 454 | { |
| 455 | struct frame_info *frame; |
| 456 | PyObject *sal_obj = NULL; /* Initialize to appease gcc warning. */ |
| 457 | |
| 458 | try |
| 459 | { |
| 460 | FRAPY_REQUIRE_VALID (self, frame); |
| 461 | |
| 462 | symtab_and_line sal = find_frame_sal (frame); |
| 463 | sal_obj = symtab_and_line_to_sal_object (sal); |
| 464 | } |
| 465 | catch (const gdb_exception &except) |
| 466 | { |
| 467 | GDB_PY_HANDLE_EXCEPTION (except); |
| 468 | } |
| 469 | |
| 470 | return sal_obj; |
| 471 | } |
| 472 | |
| 473 | /* Implementation of gdb.Frame.read_var_value (self, variable, |
| 474 | [block]) -> gdb.Value. If the optional block argument is provided |
| 475 | start the search from that block, otherwise search from the frame's |
| 476 | current block (determined by examining the resume address of the |
| 477 | frame). The variable argument must be a string or an instance of a |
| 478 | gdb.Symbol. The block argument must be an instance of gdb.Block. Returns |
| 479 | NULL on error, with a python exception set. */ |
| 480 | static PyObject * |
| 481 | frapy_read_var (PyObject *self, PyObject *args) |
| 482 | { |
| 483 | struct frame_info *frame; |
| 484 | PyObject *sym_obj, *block_obj = NULL; |
| 485 | struct symbol *var = NULL; /* gcc-4.3.2 false warning. */ |
| 486 | const struct block *block = NULL; |
| 487 | struct value *val = NULL; |
| 488 | |
| 489 | if (!PyArg_ParseTuple (args, "O|O", &sym_obj, &block_obj)) |
| 490 | return NULL; |
| 491 | |
| 492 | if (PyObject_TypeCheck (sym_obj, &symbol_object_type)) |
| 493 | var = symbol_object_to_symbol (sym_obj); |
| 494 | else if (gdbpy_is_string (sym_obj)) |
| 495 | { |
| 496 | gdb::unique_xmalloc_ptr<char> |
| 497 | var_name (python_string_to_target_string (sym_obj)); |
| 498 | |
| 499 | if (!var_name) |
| 500 | return NULL; |
| 501 | |
| 502 | if (block_obj) |
| 503 | { |
| 504 | block = block_object_to_block (block_obj); |
| 505 | if (!block) |
| 506 | { |
| 507 | PyErr_SetString (PyExc_RuntimeError, |
| 508 | _("Second argument must be block.")); |
| 509 | return NULL; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | try |
| 514 | { |
| 515 | struct block_symbol lookup_sym; |
| 516 | FRAPY_REQUIRE_VALID (self, frame); |
| 517 | |
| 518 | if (!block) |
| 519 | block = get_frame_block (frame, NULL); |
| 520 | lookup_sym = lookup_symbol (var_name.get (), block, VAR_DOMAIN, NULL); |
| 521 | var = lookup_sym.symbol; |
| 522 | block = lookup_sym.block; |
| 523 | } |
| 524 | catch (const gdb_exception &except) |
| 525 | { |
| 526 | gdbpy_convert_exception (except); |
| 527 | return NULL; |
| 528 | } |
| 529 | |
| 530 | if (!var) |
| 531 | { |
| 532 | PyErr_Format (PyExc_ValueError, |
| 533 | _("Variable '%s' not found."), var_name.get ()); |
| 534 | |
| 535 | return NULL; |
| 536 | } |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | PyErr_SetString (PyExc_TypeError, |
| 541 | _("Argument must be a symbol or string.")); |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | try |
| 546 | { |
| 547 | FRAPY_REQUIRE_VALID (self, frame); |
| 548 | |
| 549 | val = read_var_value (var, block, frame); |
| 550 | } |
| 551 | catch (const gdb_exception &except) |
| 552 | { |
| 553 | GDB_PY_HANDLE_EXCEPTION (except); |
| 554 | } |
| 555 | |
| 556 | return value_to_value_object (val); |
| 557 | } |
| 558 | |
| 559 | /* Select this frame. */ |
| 560 | |
| 561 | static PyObject * |
| 562 | frapy_select (PyObject *self, PyObject *args) |
| 563 | { |
| 564 | struct frame_info *fi; |
| 565 | |
| 566 | try |
| 567 | { |
| 568 | FRAPY_REQUIRE_VALID (self, fi); |
| 569 | |
| 570 | select_frame (fi); |
| 571 | } |
| 572 | catch (const gdb_exception &except) |
| 573 | { |
| 574 | GDB_PY_HANDLE_EXCEPTION (except); |
| 575 | } |
| 576 | |
| 577 | Py_RETURN_NONE; |
| 578 | } |
| 579 | |
| 580 | /* Implementation of gdb.newest_frame () -> gdb.Frame. |
| 581 | Returns the newest frame object. */ |
| 582 | |
| 583 | PyObject * |
| 584 | gdbpy_newest_frame (PyObject *self, PyObject *args) |
| 585 | { |
| 586 | struct frame_info *frame = NULL; |
| 587 | |
| 588 | try |
| 589 | { |
| 590 | frame = get_current_frame (); |
| 591 | } |
| 592 | catch (const gdb_exception &except) |
| 593 | { |
| 594 | GDB_PY_HANDLE_EXCEPTION (except); |
| 595 | } |
| 596 | |
| 597 | return frame_info_to_frame_object (frame); |
| 598 | } |
| 599 | |
| 600 | /* Implementation of gdb.selected_frame () -> gdb.Frame. |
| 601 | Returns the selected frame object. */ |
| 602 | |
| 603 | PyObject * |
| 604 | gdbpy_selected_frame (PyObject *self, PyObject *args) |
| 605 | { |
| 606 | struct frame_info *frame = NULL; |
| 607 | |
| 608 | try |
| 609 | { |
| 610 | frame = get_selected_frame ("No frame is currently selected."); |
| 611 | } |
| 612 | catch (const gdb_exception &except) |
| 613 | { |
| 614 | GDB_PY_HANDLE_EXCEPTION (except); |
| 615 | } |
| 616 | |
| 617 | return frame_info_to_frame_object (frame); |
| 618 | } |
| 619 | |
| 620 | /* Implementation of gdb.stop_reason_string (Integer) -> String. |
| 621 | Return a string explaining the unwind stop reason. */ |
| 622 | |
| 623 | PyObject * |
| 624 | gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args) |
| 625 | { |
| 626 | int reason; |
| 627 | const char *str; |
| 628 | |
| 629 | if (!PyArg_ParseTuple (args, "i", &reason)) |
| 630 | return NULL; |
| 631 | |
| 632 | if (reason < UNWIND_FIRST || reason > UNWIND_LAST) |
| 633 | { |
| 634 | PyErr_SetString (PyExc_ValueError, |
| 635 | _("Invalid frame stop reason.")); |
| 636 | return NULL; |
| 637 | } |
| 638 | |
| 639 | str = unwind_stop_reason_to_string ((enum unwind_stop_reason) reason); |
| 640 | return PyUnicode_Decode (str, strlen (str), host_charset (), NULL); |
| 641 | } |
| 642 | |
| 643 | /* Implements the equality comparison for Frame objects. |
| 644 | All other comparison operators will throw a TypeError Python exception, |
| 645 | as they aren't valid for frames. */ |
| 646 | |
| 647 | static PyObject * |
| 648 | frapy_richcompare (PyObject *self, PyObject *other, int op) |
| 649 | { |
| 650 | int result; |
| 651 | |
| 652 | if (!PyObject_TypeCheck (other, &frame_object_type) |
| 653 | || (op != Py_EQ && op != Py_NE)) |
| 654 | { |
| 655 | Py_INCREF (Py_NotImplemented); |
| 656 | return Py_NotImplemented; |
| 657 | } |
| 658 | |
| 659 | if (frame_id_eq (((frame_object *) self)->frame_id, |
| 660 | ((frame_object *) other)->frame_id)) |
| 661 | result = Py_EQ; |
| 662 | else |
| 663 | result = Py_NE; |
| 664 | |
| 665 | if (op == result) |
| 666 | Py_RETURN_TRUE; |
| 667 | Py_RETURN_FALSE; |
| 668 | } |
| 669 | |
| 670 | /* Sets up the Frame API in the gdb module. */ |
| 671 | |
| 672 | int |
| 673 | gdbpy_initialize_frames (void) |
| 674 | { |
| 675 | frame_object_type.tp_new = PyType_GenericNew; |
| 676 | if (PyType_Ready (&frame_object_type) < 0) |
| 677 | return -1; |
| 678 | |
| 679 | /* Note: These would probably be best exposed as class attributes of |
| 680 | Frame, but I don't know how to do it except by messing with the |
| 681 | type's dictionary. That seems too messy. */ |
| 682 | if (PyModule_AddIntConstant (gdb_module, "NORMAL_FRAME", NORMAL_FRAME) < 0 |
| 683 | || PyModule_AddIntConstant (gdb_module, "DUMMY_FRAME", DUMMY_FRAME) < 0 |
| 684 | || PyModule_AddIntConstant (gdb_module, "INLINE_FRAME", INLINE_FRAME) < 0 |
| 685 | || PyModule_AddIntConstant (gdb_module, "TAILCALL_FRAME", |
| 686 | TAILCALL_FRAME) < 0 |
| 687 | || PyModule_AddIntConstant (gdb_module, "SIGTRAMP_FRAME", |
| 688 | SIGTRAMP_FRAME) < 0 |
| 689 | || PyModule_AddIntConstant (gdb_module, "ARCH_FRAME", ARCH_FRAME) < 0 |
| 690 | || PyModule_AddIntConstant (gdb_module, "SENTINEL_FRAME", |
| 691 | SENTINEL_FRAME) < 0) |
| 692 | return -1; |
| 693 | |
| 694 | #define SET(name, description) \ |
| 695 | if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \ |
| 696 | return -1; |
| 697 | #include "unwind_stop_reasons.def" |
| 698 | #undef SET |
| 699 | |
| 700 | return gdb_pymodule_addobject (gdb_module, "Frame", |
| 701 | (PyObject *) &frame_object_type); |
| 702 | } |
| 703 | |
| 704 | \f |
| 705 | |
| 706 | static PyMethodDef frame_object_methods[] = { |
| 707 | { "is_valid", frapy_is_valid, METH_NOARGS, |
| 708 | "is_valid () -> Boolean.\n\ |
| 709 | Return true if this frame is valid, false if not." }, |
| 710 | { "name", frapy_name, METH_NOARGS, |
| 711 | "name () -> String.\n\ |
| 712 | Return the function name of the frame, or None if it can't be determined." }, |
| 713 | { "type", frapy_type, METH_NOARGS, |
| 714 | "type () -> Integer.\n\ |
| 715 | Return the type of the frame." }, |
| 716 | { "architecture", frapy_arch, METH_NOARGS, |
| 717 | "architecture () -> gdb.Architecture.\n\ |
| 718 | Return the architecture of the frame." }, |
| 719 | { "unwind_stop_reason", frapy_unwind_stop_reason, METH_NOARGS, |
| 720 | "unwind_stop_reason () -> Integer.\n\ |
| 721 | Return the reason why it's not possible to find frames older than this." }, |
| 722 | { "pc", frapy_pc, METH_NOARGS, |
| 723 | "pc () -> Long.\n\ |
| 724 | Return the frame's resume address." }, |
| 725 | { "read_register", frapy_read_register, METH_VARARGS, |
| 726 | "read_register (register_name) -> gdb.Value\n\ |
| 727 | Return the value of the register in the frame." }, |
| 728 | { "block", frapy_block, METH_NOARGS, |
| 729 | "block () -> gdb.Block.\n\ |
| 730 | Return the frame's code block." }, |
| 731 | { "function", frapy_function, METH_NOARGS, |
| 732 | "function () -> gdb.Symbol.\n\ |
| 733 | Returns the symbol for the function corresponding to this frame." }, |
| 734 | { "older", frapy_older, METH_NOARGS, |
| 735 | "older () -> gdb.Frame.\n\ |
| 736 | Return the frame that called this frame." }, |
| 737 | { "newer", frapy_newer, METH_NOARGS, |
| 738 | "newer () -> gdb.Frame.\n\ |
| 739 | Return the frame called by this frame." }, |
| 740 | { "find_sal", frapy_find_sal, METH_NOARGS, |
| 741 | "find_sal () -> gdb.Symtab_and_line.\n\ |
| 742 | Return the frame's symtab and line." }, |
| 743 | { "read_var", frapy_read_var, METH_VARARGS, |
| 744 | "read_var (variable) -> gdb.Value.\n\ |
| 745 | Return the value of the variable in this frame." }, |
| 746 | { "select", frapy_select, METH_NOARGS, |
| 747 | "Select this frame as the user's current frame." }, |
| 748 | {NULL} /* Sentinel */ |
| 749 | }; |
| 750 | |
| 751 | PyTypeObject frame_object_type = { |
| 752 | PyVarObject_HEAD_INIT (NULL, 0) |
| 753 | "gdb.Frame", /* tp_name */ |
| 754 | sizeof (frame_object), /* tp_basicsize */ |
| 755 | 0, /* tp_itemsize */ |
| 756 | 0, /* tp_dealloc */ |
| 757 | 0, /* tp_print */ |
| 758 | 0, /* tp_getattr */ |
| 759 | 0, /* tp_setattr */ |
| 760 | 0, /* tp_compare */ |
| 761 | 0, /* tp_repr */ |
| 762 | 0, /* tp_as_number */ |
| 763 | 0, /* tp_as_sequence */ |
| 764 | 0, /* tp_as_mapping */ |
| 765 | 0, /* tp_hash */ |
| 766 | 0, /* tp_call */ |
| 767 | frapy_str, /* tp_str */ |
| 768 | 0, /* tp_getattro */ |
| 769 | 0, /* tp_setattro */ |
| 770 | 0, /* tp_as_buffer */ |
| 771 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 772 | "GDB frame object", /* tp_doc */ |
| 773 | 0, /* tp_traverse */ |
| 774 | 0, /* tp_clear */ |
| 775 | frapy_richcompare, /* tp_richcompare */ |
| 776 | 0, /* tp_weaklistoffset */ |
| 777 | 0, /* tp_iter */ |
| 778 | 0, /* tp_iternext */ |
| 779 | frame_object_methods, /* tp_methods */ |
| 780 | 0, /* tp_members */ |
| 781 | 0, /* tp_getset */ |
| 782 | 0, /* tp_base */ |
| 783 | 0, /* tp_dict */ |
| 784 | 0, /* tp_descr_get */ |
| 785 | 0, /* tp_descr_set */ |
| 786 | 0, /* tp_dictoffset */ |
| 787 | 0, /* tp_init */ |
| 788 | 0, /* tp_alloc */ |
| 789 | }; |