| 1 | /* Python frame unwinder interface. |
| 2 | |
| 3 | Copyright (C) 2015-2019 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 "arch-utils.h" |
| 22 | #include "frame-unwind.h" |
| 23 | #include "gdb_obstack.h" |
| 24 | #include "gdbcmd.h" |
| 25 | #include "language.h" |
| 26 | #include "observable.h" |
| 27 | #include "python-internal.h" |
| 28 | #include "regcache.h" |
| 29 | #include "valprint.h" |
| 30 | #include "user-regs.h" |
| 31 | #include "py-ref.h" |
| 32 | |
| 33 | #define TRACE_PY_UNWIND(level, args...) if (pyuw_debug >= level) \ |
| 34 | { fprintf_unfiltered (gdb_stdlog, args); } |
| 35 | |
| 36 | typedef struct |
| 37 | { |
| 38 | PyObject_HEAD |
| 39 | |
| 40 | /* Frame we are unwinding. */ |
| 41 | struct frame_info *frame_info; |
| 42 | |
| 43 | /* Its architecture, passed by the sniffer caller. */ |
| 44 | struct gdbarch *gdbarch; |
| 45 | } pending_frame_object; |
| 46 | |
| 47 | /* Saved registers array item. */ |
| 48 | |
| 49 | struct saved_reg |
| 50 | { |
| 51 | saved_reg (int n, gdbpy_ref<> &&v) |
| 52 | : number (n), |
| 53 | value (std::move (v)) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | int number; |
| 58 | gdbpy_ref<> value; |
| 59 | }; |
| 60 | |
| 61 | /* The data we keep for the PyUnwindInfo: pending_frame, saved registers |
| 62 | and frame ID. */ |
| 63 | |
| 64 | typedef struct |
| 65 | { |
| 66 | PyObject_HEAD |
| 67 | |
| 68 | /* gdb.PendingFrame for the frame we are unwinding. */ |
| 69 | PyObject *pending_frame; |
| 70 | |
| 71 | /* Its ID. */ |
| 72 | struct frame_id frame_id; |
| 73 | |
| 74 | /* Saved registers array. */ |
| 75 | std::vector<saved_reg> *saved_regs; |
| 76 | } unwind_info_object; |
| 77 | |
| 78 | /* The data we keep for a frame we can unwind: frame ID and an array of |
| 79 | (register_number, register_value) pairs. */ |
| 80 | |
| 81 | typedef struct |
| 82 | { |
| 83 | /* Frame ID. */ |
| 84 | struct frame_id frame_id; |
| 85 | |
| 86 | /* GDB Architecture. */ |
| 87 | struct gdbarch *gdbarch; |
| 88 | |
| 89 | /* Length of the `reg' array below. */ |
| 90 | int reg_count; |
| 91 | |
| 92 | cached_reg_t reg[]; |
| 93 | } cached_frame_info; |
| 94 | |
| 95 | extern PyTypeObject pending_frame_object_type |
| 96 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pending_frame_object"); |
| 97 | |
| 98 | extern PyTypeObject unwind_info_object_type |
| 99 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("unwind_info_object"); |
| 100 | |
| 101 | static unsigned int pyuw_debug = 0; |
| 102 | |
| 103 | static struct gdbarch_data *pyuw_gdbarch_data; |
| 104 | |
| 105 | /* Parses register id, which can be either a number or a name. |
| 106 | Returns 1 on success, 0 otherwise. */ |
| 107 | |
| 108 | static int |
| 109 | pyuw_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id, |
| 110 | int *reg_num) |
| 111 | { |
| 112 | if (pyo_reg_id == NULL) |
| 113 | return 0; |
| 114 | if (gdbpy_is_string (pyo_reg_id)) |
| 115 | { |
| 116 | gdb::unique_xmalloc_ptr<char> reg_name (gdbpy_obj_to_string (pyo_reg_id)); |
| 117 | |
| 118 | if (reg_name == NULL) |
| 119 | return 0; |
| 120 | *reg_num = user_reg_map_name_to_regnum (gdbarch, reg_name.get (), |
| 121 | strlen (reg_name.get ())); |
| 122 | return *reg_num >= 0; |
| 123 | } |
| 124 | else if (PyInt_Check (pyo_reg_id)) |
| 125 | { |
| 126 | long value; |
| 127 | if (gdb_py_int_as_long (pyo_reg_id, &value) && (int) value == value) |
| 128 | { |
| 129 | *reg_num = (int) value; |
| 130 | return user_reg_map_regnum_to_name (gdbarch, *reg_num) != NULL; |
| 131 | } |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* Convert gdb.Value instance to inferior's pointer. Return 1 on success, |
| 137 | 0 on failure. */ |
| 138 | |
| 139 | static int |
| 140 | pyuw_value_obj_to_pointer (PyObject *pyo_value, CORE_ADDR *addr) |
| 141 | { |
| 142 | int rc = 0; |
| 143 | struct value *value; |
| 144 | |
| 145 | TRY |
| 146 | { |
| 147 | if ((value = value_object_to_value (pyo_value)) != NULL) |
| 148 | { |
| 149 | *addr = unpack_pointer (value_type (value), |
| 150 | value_contents (value)); |
| 151 | rc = 1; |
| 152 | } |
| 153 | } |
| 154 | CATCH (except, RETURN_MASK_ALL) |
| 155 | { |
| 156 | gdbpy_convert_exception (except); |
| 157 | } |
| 158 | END_CATCH |
| 159 | return rc; |
| 160 | } |
| 161 | |
| 162 | /* Get attribute from an object and convert it to the inferior's |
| 163 | pointer value. Return 1 if attribute exists and its value can be |
| 164 | converted. Otherwise, if attribute does not exist or its value is |
| 165 | None, return 0. In all other cases set Python error and return |
| 166 | 0. */ |
| 167 | |
| 168 | static int |
| 169 | pyuw_object_attribute_to_pointer (PyObject *pyo, const char *attr_name, |
| 170 | CORE_ADDR *addr) |
| 171 | { |
| 172 | int rc = 0; |
| 173 | |
| 174 | if (PyObject_HasAttrString (pyo, attr_name)) |
| 175 | { |
| 176 | gdbpy_ref<> pyo_value (PyObject_GetAttrString (pyo, attr_name)); |
| 177 | |
| 178 | if (pyo_value != NULL && pyo_value != Py_None) |
| 179 | { |
| 180 | rc = pyuw_value_obj_to_pointer (pyo_value.get (), addr); |
| 181 | if (!rc) |
| 182 | PyErr_Format ( |
| 183 | PyExc_ValueError, |
| 184 | _("The value of the '%s' attribute is not a pointer."), |
| 185 | attr_name); |
| 186 | } |
| 187 | } |
| 188 | return rc; |
| 189 | } |
| 190 | |
| 191 | /* Called by the Python interpreter to obtain string representation |
| 192 | of the UnwindInfo object. */ |
| 193 | |
| 194 | static PyObject * |
| 195 | unwind_infopy_str (PyObject *self) |
| 196 | { |
| 197 | unwind_info_object *unwind_info = (unwind_info_object *) self; |
| 198 | string_file stb; |
| 199 | |
| 200 | stb.puts ("Frame ID: "); |
| 201 | fprint_frame_id (&stb, unwind_info->frame_id); |
| 202 | { |
| 203 | const char *sep = ""; |
| 204 | struct value_print_options opts; |
| 205 | |
| 206 | get_user_print_options (&opts); |
| 207 | stb.printf ("\nSaved registers: ("); |
| 208 | for (const saved_reg ® : *unwind_info->saved_regs) |
| 209 | { |
| 210 | struct value *value = value_object_to_value (reg.value.get ()); |
| 211 | |
| 212 | stb.printf ("%s(%d, ", sep, reg.number); |
| 213 | if (value != NULL) |
| 214 | { |
| 215 | TRY |
| 216 | { |
| 217 | value_print (value, &stb, &opts); |
| 218 | stb.puts (")"); |
| 219 | } |
| 220 | CATCH (except, RETURN_MASK_ALL) |
| 221 | { |
| 222 | GDB_PY_HANDLE_EXCEPTION (except); |
| 223 | } |
| 224 | END_CATCH |
| 225 | } |
| 226 | else |
| 227 | stb.puts ("<BAD>)"); |
| 228 | sep = ", "; |
| 229 | } |
| 230 | stb.puts (")"); |
| 231 | } |
| 232 | |
| 233 | return PyString_FromString (stb.c_str ()); |
| 234 | } |
| 235 | |
| 236 | /* Create UnwindInfo instance for given PendingFrame and frame ID. |
| 237 | Sets Python error and returns NULL on error. */ |
| 238 | |
| 239 | static PyObject * |
| 240 | pyuw_create_unwind_info (PyObject *pyo_pending_frame, |
| 241 | struct frame_id frame_id) |
| 242 | { |
| 243 | unwind_info_object *unwind_info |
| 244 | = PyObject_New (unwind_info_object, &unwind_info_object_type); |
| 245 | |
| 246 | if (((pending_frame_object *) pyo_pending_frame)->frame_info == NULL) |
| 247 | { |
| 248 | PyErr_SetString (PyExc_ValueError, |
| 249 | "Attempting to use stale PendingFrame"); |
| 250 | return NULL; |
| 251 | } |
| 252 | unwind_info->frame_id = frame_id; |
| 253 | Py_INCREF (pyo_pending_frame); |
| 254 | unwind_info->pending_frame = pyo_pending_frame; |
| 255 | unwind_info->saved_regs = new std::vector<saved_reg>; |
| 256 | return (PyObject *) unwind_info; |
| 257 | } |
| 258 | |
| 259 | /* The implementation of |
| 260 | gdb.UnwindInfo.add_saved_register (REG, VALUE) -> None. */ |
| 261 | |
| 262 | static PyObject * |
| 263 | unwind_infopy_add_saved_register (PyObject *self, PyObject *args) |
| 264 | { |
| 265 | unwind_info_object *unwind_info = (unwind_info_object *) self; |
| 266 | pending_frame_object *pending_frame |
| 267 | = (pending_frame_object *) (unwind_info->pending_frame); |
| 268 | PyObject *pyo_reg_id; |
| 269 | PyObject *pyo_reg_value; |
| 270 | int regnum; |
| 271 | |
| 272 | if (pending_frame->frame_info == NULL) |
| 273 | { |
| 274 | PyErr_SetString (PyExc_ValueError, |
| 275 | "UnwindInfo instance refers to a stale PendingFrame"); |
| 276 | return NULL; |
| 277 | } |
| 278 | if (!PyArg_UnpackTuple (args, "previous_frame_register", 2, 2, |
| 279 | &pyo_reg_id, &pyo_reg_value)) |
| 280 | return NULL; |
| 281 | if (!pyuw_parse_register_id (pending_frame->gdbarch, pyo_reg_id, ®num)) |
| 282 | { |
| 283 | PyErr_SetString (PyExc_ValueError, "Bad register"); |
| 284 | return NULL; |
| 285 | } |
| 286 | { |
| 287 | struct value *value; |
| 288 | size_t data_size; |
| 289 | |
| 290 | if (pyo_reg_value == NULL |
| 291 | || (value = value_object_to_value (pyo_reg_value)) == NULL) |
| 292 | { |
| 293 | PyErr_SetString (PyExc_ValueError, "Bad register value"); |
| 294 | return NULL; |
| 295 | } |
| 296 | data_size = register_size (pending_frame->gdbarch, regnum); |
| 297 | if (data_size != TYPE_LENGTH (value_type (value))) |
| 298 | { |
| 299 | PyErr_Format ( |
| 300 | PyExc_ValueError, |
| 301 | "The value of the register returned by the Python " |
| 302 | "sniffer has unexpected size: %u instead of %u.", |
| 303 | (unsigned) TYPE_LENGTH (value_type (value)), |
| 304 | (unsigned) data_size); |
| 305 | return NULL; |
| 306 | } |
| 307 | } |
| 308 | { |
| 309 | gdbpy_ref<> new_value = gdbpy_ref<>::new_reference (pyo_reg_value); |
| 310 | bool found = false; |
| 311 | for (saved_reg ® : *unwind_info->saved_regs) |
| 312 | { |
| 313 | if (regnum == reg.number) |
| 314 | { |
| 315 | found = true; |
| 316 | reg.value = std::move (new_value); |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | if (!found) |
| 321 | unwind_info->saved_regs->emplace_back (regnum, std::move (new_value)); |
| 322 | } |
| 323 | Py_RETURN_NONE; |
| 324 | } |
| 325 | |
| 326 | /* UnwindInfo cleanup. */ |
| 327 | |
| 328 | static void |
| 329 | unwind_infopy_dealloc (PyObject *self) |
| 330 | { |
| 331 | unwind_info_object *unwind_info = (unwind_info_object *) self; |
| 332 | |
| 333 | Py_XDECREF (unwind_info->pending_frame); |
| 334 | delete unwind_info->saved_regs; |
| 335 | Py_TYPE (self)->tp_free (self); |
| 336 | } |
| 337 | |
| 338 | /* Called by the Python interpreter to obtain string representation |
| 339 | of the PendingFrame object. */ |
| 340 | |
| 341 | static PyObject * |
| 342 | pending_framepy_str (PyObject *self) |
| 343 | { |
| 344 | struct frame_info *frame = ((pending_frame_object *) self)->frame_info; |
| 345 | const char *sp_str = NULL; |
| 346 | const char *pc_str = NULL; |
| 347 | |
| 348 | if (frame == NULL) |
| 349 | return PyString_FromString ("Stale PendingFrame instance"); |
| 350 | TRY |
| 351 | { |
| 352 | sp_str = core_addr_to_string_nz (get_frame_sp (frame)); |
| 353 | pc_str = core_addr_to_string_nz (get_frame_pc (frame)); |
| 354 | } |
| 355 | CATCH (except, RETURN_MASK_ALL) |
| 356 | { |
| 357 | GDB_PY_HANDLE_EXCEPTION (except); |
| 358 | } |
| 359 | END_CATCH |
| 360 | |
| 361 | return PyString_FromFormat ("SP=%s,PC=%s", sp_str, pc_str); |
| 362 | } |
| 363 | |
| 364 | /* Implementation of gdb.PendingFrame.read_register (self, reg) -> gdb.Value. |
| 365 | Returns the value of register REG as gdb.Value instance. */ |
| 366 | |
| 367 | static PyObject * |
| 368 | pending_framepy_read_register (PyObject *self, PyObject *args) |
| 369 | { |
| 370 | pending_frame_object *pending_frame = (pending_frame_object *) self; |
| 371 | struct value *val = NULL; |
| 372 | int regnum; |
| 373 | PyObject *pyo_reg_id; |
| 374 | |
| 375 | if (pending_frame->frame_info == NULL) |
| 376 | { |
| 377 | PyErr_SetString (PyExc_ValueError, |
| 378 | "Attempting to read register from stale PendingFrame"); |
| 379 | return NULL; |
| 380 | } |
| 381 | if (!PyArg_UnpackTuple (args, "read_register", 1, 1, &pyo_reg_id)) |
| 382 | return NULL; |
| 383 | if (!pyuw_parse_register_id (pending_frame->gdbarch, pyo_reg_id, ®num)) |
| 384 | { |
| 385 | PyErr_SetString (PyExc_ValueError, "Bad register"); |
| 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | TRY |
| 390 | { |
| 391 | /* Fetch the value associated with a register, whether it's |
| 392 | a real register or a so called "user" register, like "pc", |
| 393 | which maps to a real register. In the past, |
| 394 | get_frame_register_value() was used here, which did not |
| 395 | handle the user register case. */ |
| 396 | val = value_of_register (regnum, pending_frame->frame_info); |
| 397 | if (val == NULL) |
| 398 | PyErr_Format (PyExc_ValueError, |
| 399 | "Cannot read register %d from frame.", |
| 400 | regnum); |
| 401 | } |
| 402 | CATCH (except, RETURN_MASK_ALL) |
| 403 | { |
| 404 | GDB_PY_HANDLE_EXCEPTION (except); |
| 405 | } |
| 406 | END_CATCH |
| 407 | |
| 408 | return val == NULL ? NULL : value_to_value_object (val); |
| 409 | } |
| 410 | |
| 411 | /* Implementation of |
| 412 | PendingFrame.create_unwind_info (self, frameId) -> UnwindInfo. */ |
| 413 | |
| 414 | static PyObject * |
| 415 | pending_framepy_create_unwind_info (PyObject *self, PyObject *args) |
| 416 | { |
| 417 | PyObject *pyo_frame_id; |
| 418 | CORE_ADDR sp; |
| 419 | CORE_ADDR pc; |
| 420 | CORE_ADDR special; |
| 421 | |
| 422 | if (!PyArg_ParseTuple (args, "O:create_unwind_info", &pyo_frame_id)) |
| 423 | return NULL; |
| 424 | if (!pyuw_object_attribute_to_pointer (pyo_frame_id, "sp", &sp)) |
| 425 | { |
| 426 | PyErr_SetString (PyExc_ValueError, |
| 427 | _("frame_id should have 'sp' attribute.")); |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | /* The logic of building frame_id depending on the attributes of |
| 432 | the frame_id object: |
| 433 | Has Has Has Function to call |
| 434 | 'sp'? 'pc'? 'special'? |
| 435 | ------|------|--------------|------------------------- |
| 436 | Y N * frame_id_build_wild (sp) |
| 437 | Y Y N frame_id_build (sp, pc) |
| 438 | Y Y Y frame_id_build_special (sp, pc, special) |
| 439 | */ |
| 440 | if (!pyuw_object_attribute_to_pointer (pyo_frame_id, "pc", &pc)) |
| 441 | return pyuw_create_unwind_info (self, frame_id_build_wild (sp)); |
| 442 | if (!pyuw_object_attribute_to_pointer (pyo_frame_id, "special", &special)) |
| 443 | return pyuw_create_unwind_info (self, frame_id_build (sp, pc)); |
| 444 | else |
| 445 | return pyuw_create_unwind_info (self, |
| 446 | frame_id_build_special (sp, pc, special)); |
| 447 | } |
| 448 | |
| 449 | /* frame_unwind.this_id method. */ |
| 450 | |
| 451 | static void |
| 452 | pyuw_this_id (struct frame_info *this_frame, void **cache_ptr, |
| 453 | struct frame_id *this_id) |
| 454 | { |
| 455 | *this_id = ((cached_frame_info *) *cache_ptr)->frame_id; |
| 456 | if (pyuw_debug >= 1) |
| 457 | { |
| 458 | fprintf_unfiltered (gdb_stdlog, "%s: frame_id: ", __FUNCTION__); |
| 459 | fprint_frame_id (gdb_stdlog, *this_id); |
| 460 | fprintf_unfiltered (gdb_stdlog, "\n"); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /* frame_unwind.prev_register. */ |
| 465 | |
| 466 | static struct value * |
| 467 | pyuw_prev_register (struct frame_info *this_frame, void **cache_ptr, |
| 468 | int regnum) |
| 469 | { |
| 470 | cached_frame_info *cached_frame = (cached_frame_info *) *cache_ptr; |
| 471 | cached_reg_t *reg_info = cached_frame->reg; |
| 472 | cached_reg_t *reg_info_end = reg_info + cached_frame->reg_count; |
| 473 | |
| 474 | TRACE_PY_UNWIND (1, "%s (frame=%p,...,reg=%d)\n", __FUNCTION__, this_frame, |
| 475 | regnum); |
| 476 | for (; reg_info < reg_info_end; ++reg_info) |
| 477 | { |
| 478 | if (regnum == reg_info->num) |
| 479 | return frame_unwind_got_bytes (this_frame, regnum, reg_info->data); |
| 480 | } |
| 481 | |
| 482 | return frame_unwind_got_optimized (this_frame, regnum); |
| 483 | } |
| 484 | |
| 485 | /* Frame sniffer dispatch. */ |
| 486 | |
| 487 | static int |
| 488 | pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame, |
| 489 | void **cache_ptr) |
| 490 | { |
| 491 | struct gdbarch *gdbarch = (struct gdbarch *) (self->unwind_data); |
| 492 | cached_frame_info *cached_frame; |
| 493 | |
| 494 | gdbpy_enter enter_py (gdbarch, current_language); |
| 495 | |
| 496 | TRACE_PY_UNWIND (3, "%s (SP=%s, PC=%s)\n", __FUNCTION__, |
| 497 | paddress (gdbarch, get_frame_sp (this_frame)), |
| 498 | paddress (gdbarch, get_frame_pc (this_frame))); |
| 499 | |
| 500 | /* Create PendingFrame instance to pass to sniffers. */ |
| 501 | pending_frame_object *pfo = PyObject_New (pending_frame_object, |
| 502 | &pending_frame_object_type); |
| 503 | gdbpy_ref<> pyo_pending_frame ((PyObject *) pfo); |
| 504 | if (pyo_pending_frame == NULL) |
| 505 | { |
| 506 | gdbpy_print_stack (); |
| 507 | return 0; |
| 508 | } |
| 509 | pfo->gdbarch = gdbarch; |
| 510 | scoped_restore invalidate_frame = make_scoped_restore (&pfo->frame_info, |
| 511 | this_frame); |
| 512 | |
| 513 | /* Run unwinders. */ |
| 514 | if (gdb_python_module == NULL |
| 515 | || ! PyObject_HasAttrString (gdb_python_module, "execute_unwinders")) |
| 516 | { |
| 517 | PyErr_SetString (PyExc_NameError, |
| 518 | "Installation error: gdb.execute_unwinders function " |
| 519 | "is missing"); |
| 520 | gdbpy_print_stack (); |
| 521 | return 0; |
| 522 | } |
| 523 | gdbpy_ref<> pyo_execute (PyObject_GetAttrString (gdb_python_module, |
| 524 | "execute_unwinders")); |
| 525 | if (pyo_execute == NULL) |
| 526 | { |
| 527 | gdbpy_print_stack (); |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | gdbpy_ref<> pyo_unwind_info |
| 532 | (PyObject_CallFunctionObjArgs (pyo_execute.get (), |
| 533 | pyo_pending_frame.get (), NULL)); |
| 534 | if (pyo_unwind_info == NULL) |
| 535 | { |
| 536 | /* If the unwinder is cancelled due to a Ctrl-C, then propagate |
| 537 | the Ctrl-C as a GDB exception instead of swallowing it. */ |
| 538 | gdbpy_print_stack_or_quit (); |
| 539 | return 0; |
| 540 | } |
| 541 | if (pyo_unwind_info == Py_None) |
| 542 | return 0; |
| 543 | |
| 544 | /* Received UnwindInfo, cache data. */ |
| 545 | if (PyObject_IsInstance (pyo_unwind_info.get (), |
| 546 | (PyObject *) &unwind_info_object_type) <= 0) |
| 547 | error (_("A Unwinder should return gdb.UnwindInfo instance.")); |
| 548 | |
| 549 | { |
| 550 | unwind_info_object *unwind_info = |
| 551 | (unwind_info_object *) pyo_unwind_info.get (); |
| 552 | int reg_count = unwind_info->saved_regs->size (); |
| 553 | |
| 554 | cached_frame |
| 555 | = ((cached_frame_info *) |
| 556 | xmalloc (sizeof (*cached_frame) |
| 557 | + reg_count * sizeof (cached_frame->reg[0]))); |
| 558 | cached_frame->gdbarch = gdbarch; |
| 559 | cached_frame->frame_id = unwind_info->frame_id; |
| 560 | cached_frame->reg_count = reg_count; |
| 561 | |
| 562 | /* Populate registers array. */ |
| 563 | for (int i = 0; i < unwind_info->saved_regs->size (); ++i) |
| 564 | { |
| 565 | saved_reg *reg = &(*unwind_info->saved_regs)[i]; |
| 566 | |
| 567 | struct value *value = value_object_to_value (reg->value.get ()); |
| 568 | size_t data_size = register_size (gdbarch, reg->number); |
| 569 | |
| 570 | cached_frame->reg[i].num = reg->number; |
| 571 | |
| 572 | /* `value' validation was done before, just assert. */ |
| 573 | gdb_assert (value != NULL); |
| 574 | gdb_assert (data_size == TYPE_LENGTH (value_type (value))); |
| 575 | |
| 576 | cached_frame->reg[i].data = (gdb_byte *) xmalloc (data_size); |
| 577 | memcpy (cached_frame->reg[i].data, value_contents (value), data_size); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | *cache_ptr = cached_frame; |
| 582 | return 1; |
| 583 | } |
| 584 | |
| 585 | /* Frame cache release shim. */ |
| 586 | |
| 587 | static void |
| 588 | pyuw_dealloc_cache (struct frame_info *this_frame, void *cache) |
| 589 | { |
| 590 | TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__); |
| 591 | cached_frame_info *cached_frame = (cached_frame_info *) cache; |
| 592 | |
| 593 | for (int i = 0; i < cached_frame->reg_count; i++) |
| 594 | xfree (cached_frame->reg[i].data); |
| 595 | |
| 596 | xfree (cache); |
| 597 | } |
| 598 | |
| 599 | struct pyuw_gdbarch_data_type |
| 600 | { |
| 601 | /* Has the unwinder shim been prepended? */ |
| 602 | int unwinder_registered; |
| 603 | }; |
| 604 | |
| 605 | static void * |
| 606 | pyuw_gdbarch_data_init (struct gdbarch *gdbarch) |
| 607 | { |
| 608 | return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct pyuw_gdbarch_data_type); |
| 609 | } |
| 610 | |
| 611 | /* New inferior architecture callback: register the Python unwinders |
| 612 | intermediary. */ |
| 613 | |
| 614 | static void |
| 615 | pyuw_on_new_gdbarch (struct gdbarch *newarch) |
| 616 | { |
| 617 | struct pyuw_gdbarch_data_type *data |
| 618 | = (struct pyuw_gdbarch_data_type *) gdbarch_data (newarch, |
| 619 | pyuw_gdbarch_data); |
| 620 | |
| 621 | if (!data->unwinder_registered) |
| 622 | { |
| 623 | struct frame_unwind *unwinder |
| 624 | = GDBARCH_OBSTACK_ZALLOC (newarch, struct frame_unwind); |
| 625 | |
| 626 | unwinder->type = NORMAL_FRAME; |
| 627 | unwinder->stop_reason = default_frame_unwind_stop_reason; |
| 628 | unwinder->this_id = pyuw_this_id; |
| 629 | unwinder->prev_register = pyuw_prev_register; |
| 630 | unwinder->unwind_data = (const struct frame_data *) newarch; |
| 631 | unwinder->sniffer = pyuw_sniffer; |
| 632 | unwinder->dealloc_cache = pyuw_dealloc_cache; |
| 633 | frame_unwind_prepend_unwinder (newarch, unwinder); |
| 634 | data->unwinder_registered = 1; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | /* Initialize unwind machinery. */ |
| 639 | |
| 640 | int |
| 641 | gdbpy_initialize_unwind (void) |
| 642 | { |
| 643 | int rc; |
| 644 | add_setshow_zuinteger_cmd |
| 645 | ("py-unwind", class_maintenance, &pyuw_debug, |
| 646 | _("Set Python unwinder debugging."), |
| 647 | _("Show Python unwinder debugging."), |
| 648 | _("When non-zero, Python unwinder debugging is enabled."), |
| 649 | NULL, |
| 650 | NULL, |
| 651 | &setdebuglist, &showdebuglist); |
| 652 | pyuw_gdbarch_data |
| 653 | = gdbarch_data_register_post_init (pyuw_gdbarch_data_init); |
| 654 | gdb::observers::architecture_changed.attach (pyuw_on_new_gdbarch); |
| 655 | |
| 656 | if (PyType_Ready (&pending_frame_object_type) < 0) |
| 657 | return -1; |
| 658 | rc = gdb_pymodule_addobject (gdb_module, "PendingFrame", |
| 659 | (PyObject *) &pending_frame_object_type); |
| 660 | if (rc) |
| 661 | return rc; |
| 662 | |
| 663 | if (PyType_Ready (&unwind_info_object_type) < 0) |
| 664 | return -1; |
| 665 | return gdb_pymodule_addobject (gdb_module, "UnwindInfo", |
| 666 | (PyObject *) &unwind_info_object_type); |
| 667 | } |
| 668 | |
| 669 | static PyMethodDef pending_frame_object_methods[] = |
| 670 | { |
| 671 | { "read_register", pending_framepy_read_register, METH_VARARGS, |
| 672 | "read_register (REG) -> gdb.Value\n" |
| 673 | "Return the value of the REG in the frame." }, |
| 674 | { "create_unwind_info", |
| 675 | pending_framepy_create_unwind_info, METH_VARARGS, |
| 676 | "create_unwind_info (FRAME_ID) -> gdb.UnwindInfo\n" |
| 677 | "Construct UnwindInfo for this PendingFrame, using FRAME_ID\n" |
| 678 | "to identify it." }, |
| 679 | {NULL} /* Sentinel */ |
| 680 | }; |
| 681 | |
| 682 | PyTypeObject pending_frame_object_type = |
| 683 | { |
| 684 | PyVarObject_HEAD_INIT (NULL, 0) |
| 685 | "gdb.PendingFrame", /* tp_name */ |
| 686 | sizeof (pending_frame_object), /* tp_basicsize */ |
| 687 | 0, /* tp_itemsize */ |
| 688 | 0, /* tp_dealloc */ |
| 689 | 0, /* tp_print */ |
| 690 | 0, /* tp_getattr */ |
| 691 | 0, /* tp_setattr */ |
| 692 | 0, /* tp_compare */ |
| 693 | 0, /* tp_repr */ |
| 694 | 0, /* tp_as_number */ |
| 695 | 0, /* tp_as_sequence */ |
| 696 | 0, /* tp_as_mapping */ |
| 697 | 0, /* tp_hash */ |
| 698 | 0, /* tp_call */ |
| 699 | pending_framepy_str, /* tp_str */ |
| 700 | 0, /* tp_getattro */ |
| 701 | 0, /* tp_setattro */ |
| 702 | 0, /* tp_as_buffer */ |
| 703 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 704 | "GDB PendingFrame object", /* tp_doc */ |
| 705 | 0, /* tp_traverse */ |
| 706 | 0, /* tp_clear */ |
| 707 | 0, /* tp_richcompare */ |
| 708 | 0, /* tp_weaklistoffset */ |
| 709 | 0, /* tp_iter */ |
| 710 | 0, /* tp_iternext */ |
| 711 | pending_frame_object_methods, /* tp_methods */ |
| 712 | 0, /* tp_members */ |
| 713 | 0, /* tp_getset */ |
| 714 | 0, /* tp_base */ |
| 715 | 0, /* tp_dict */ |
| 716 | 0, /* tp_descr_get */ |
| 717 | 0, /* tp_descr_set */ |
| 718 | 0, /* tp_dictoffset */ |
| 719 | 0, /* tp_init */ |
| 720 | 0, /* tp_alloc */ |
| 721 | }; |
| 722 | |
| 723 | static PyMethodDef unwind_info_object_methods[] = |
| 724 | { |
| 725 | { "add_saved_register", |
| 726 | unwind_infopy_add_saved_register, METH_VARARGS, |
| 727 | "add_saved_register (REG, VALUE) -> None\n" |
| 728 | "Set the value of the REG in the previous frame to VALUE." }, |
| 729 | { NULL } /* Sentinel */ |
| 730 | }; |
| 731 | |
| 732 | PyTypeObject unwind_info_object_type = |
| 733 | { |
| 734 | PyVarObject_HEAD_INIT (NULL, 0) |
| 735 | "gdb.UnwindInfo", /* tp_name */ |
| 736 | sizeof (unwind_info_object), /* tp_basicsize */ |
| 737 | 0, /* tp_itemsize */ |
| 738 | unwind_infopy_dealloc, /* tp_dealloc */ |
| 739 | 0, /* tp_print */ |
| 740 | 0, /* tp_getattr */ |
| 741 | 0, /* tp_setattr */ |
| 742 | 0, /* tp_compare */ |
| 743 | 0, /* tp_repr */ |
| 744 | 0, /* tp_as_number */ |
| 745 | 0, /* tp_as_sequence */ |
| 746 | 0, /* tp_as_mapping */ |
| 747 | 0, /* tp_hash */ |
| 748 | 0, /* tp_call */ |
| 749 | unwind_infopy_str, /* tp_str */ |
| 750 | 0, /* tp_getattro */ |
| 751 | 0, /* tp_setattro */ |
| 752 | 0, /* tp_as_buffer */ |
| 753 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 754 | "GDB UnwindInfo object", /* tp_doc */ |
| 755 | 0, /* tp_traverse */ |
| 756 | 0, /* tp_clear */ |
| 757 | 0, /* tp_richcompare */ |
| 758 | 0, /* tp_weaklistoffset */ |
| 759 | 0, /* tp_iter */ |
| 760 | 0, /* tp_iternext */ |
| 761 | unwind_info_object_methods, /* tp_methods */ |
| 762 | 0, /* tp_members */ |
| 763 | 0, /* tp_getset */ |
| 764 | 0, /* tp_base */ |
| 765 | 0, /* tp_dict */ |
| 766 | 0, /* tp_descr_get */ |
| 767 | 0, /* tp_descr_set */ |
| 768 | 0, /* tp_dictoffset */ |
| 769 | 0, /* tp_init */ |
| 770 | 0, /* tp_alloc */ |
| 771 | }; |