1 1.1 christos /* Python frame unwinder interface. 2 1.1 christos 3 1.1.1.7 christos Copyright (C) 2015-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos #include "arch-utils.h" 21 1.1 christos #include "frame-unwind.h" 22 1.1.1.6 christos #include "gdbsupport/gdb_obstack.h" 23 1.1.1.7 christos #include "cli/cli-cmds.h" 24 1.1 christos #include "language.h" 25 1.1.1.4 christos #include "observable.h" 26 1.1 christos #include "python-internal.h" 27 1.1 christos #include "regcache.h" 28 1.1 christos #include "valprint.h" 29 1.1.1.6 christos #include "user-regs.h" 30 1.1.1.7 christos #include "stack.h" 31 1.1.1.7 christos #include "charset.h" 32 1.1.1.7 christos #include "block.h" 33 1.1.1.7 christos 34 1.1 christos 35 1.1.1.6 christos /* Debugging of Python unwinders. */ 36 1.1 christos 37 1.1.1.6 christos static bool pyuw_debug; 38 1.1.1.6 christos 39 1.1.1.6 christos /* Implementation of "show debug py-unwind". */ 40 1.1.1.6 christos 41 1.1.1.6 christos static void 42 1.1.1.6 christos show_pyuw_debug (struct ui_file *file, int from_tty, 43 1.1.1.6 christos struct cmd_list_element *c, const char *value) 44 1.1.1.6 christos { 45 1.1.1.6 christos gdb_printf (file, _("Python unwinder debugging is %s.\n"), value); 46 1.1.1.6 christos } 47 1.1.1.6 christos 48 1.1.1.6 christos /* Print a "py-unwind" debug statement. */ 49 1.1.1.6 christos 50 1.1.1.6 christos #define pyuw_debug_printf(fmt, ...) \ 51 1.1.1.6 christos debug_prefixed_printf_cond (pyuw_debug, "py-unwind", fmt, ##__VA_ARGS__) 52 1.1.1.6 christos 53 1.1.1.6 christos /* Print "py-unwind" enter/exit debug statements. */ 54 1.1.1.6 christos 55 1.1.1.6 christos #define PYUW_SCOPED_DEBUG_ENTER_EXIT \ 56 1.1.1.6 christos scoped_debug_enter_exit (pyuw_debug, "py-unwind") 57 1.1.1.6 christos 58 1.1.1.7 christos /* Require a valid pending frame. */ 59 1.1.1.7 christos #define PENDING_FRAMEPY_REQUIRE_VALID(pending_frame) \ 60 1.1.1.7 christos do { \ 61 1.1.1.7 christos if ((pending_frame)->frame_info == nullptr) \ 62 1.1.1.7 christos { \ 63 1.1.1.7 christos PyErr_SetString (PyExc_ValueError, \ 64 1.1.1.7 christos _("gdb.PendingFrame is invalid.")); \ 65 1.1.1.7 christos return nullptr; \ 66 1.1.1.7 christos } \ 67 1.1.1.7 christos } while (0) 68 1.1.1.7 christos 69 1.1.1.6 christos struct pending_frame_object 70 1.1 christos { 71 1.1 christos PyObject_HEAD 72 1.1 christos 73 1.1 christos /* Frame we are unwinding. */ 74 1.1.1.6 christos frame_info_ptr frame_info; 75 1.1 christos 76 1.1 christos /* Its architecture, passed by the sniffer caller. */ 77 1.1 christos struct gdbarch *gdbarch; 78 1.1.1.6 christos }; 79 1.1 christos 80 1.1 christos /* Saved registers array item. */ 81 1.1 christos 82 1.1.1.4 christos struct saved_reg 83 1.1 christos { 84 1.1.1.4 christos saved_reg (int n, gdbpy_ref<> &&v) 85 1.1.1.4 christos : number (n), 86 1.1.1.4 christos value (std::move (v)) 87 1.1.1.4 christos { 88 1.1.1.4 christos } 89 1.1.1.4 christos 90 1.1 christos int number; 91 1.1.1.4 christos gdbpy_ref<> value; 92 1.1.1.4 christos }; 93 1.1 christos 94 1.1 christos /* The data we keep for the PyUnwindInfo: pending_frame, saved registers 95 1.1 christos and frame ID. */ 96 1.1 christos 97 1.1.1.6 christos struct unwind_info_object 98 1.1 christos { 99 1.1 christos PyObject_HEAD 100 1.1 christos 101 1.1 christos /* gdb.PendingFrame for the frame we are unwinding. */ 102 1.1 christos PyObject *pending_frame; 103 1.1 christos 104 1.1 christos /* Its ID. */ 105 1.1 christos struct frame_id frame_id; 106 1.1 christos 107 1.1 christos /* Saved registers array. */ 108 1.1.1.4 christos std::vector<saved_reg> *saved_regs; 109 1.1.1.6 christos }; 110 1.1 christos 111 1.1 christos /* The data we keep for a frame we can unwind: frame ID and an array of 112 1.1 christos (register_number, register_value) pairs. */ 113 1.1 christos 114 1.1.1.6 christos struct cached_frame_info 115 1.1 christos { 116 1.1 christos /* Frame ID. */ 117 1.1 christos struct frame_id frame_id; 118 1.1 christos 119 1.1 christos /* GDB Architecture. */ 120 1.1 christos struct gdbarch *gdbarch; 121 1.1 christos 122 1.1 christos /* Length of the `reg' array below. */ 123 1.1 christos int reg_count; 124 1.1 christos 125 1.1.1.7 christos /* Flexible array member. Note: use a zero-sized array rather than 126 1.1.1.7 christos an actual C99-style flexible array member (unsized array), 127 1.1.1.7 christos because the latter would cause an error with Clang: 128 1.1.1.7 christos 129 1.1.1.7 christos error: flexible array member 'reg' of type 'cached_reg_t[]' with non-trivial destruction 130 1.1.1.7 christos 131 1.1.1.7 christos Note we manually call the destructor of each array element in 132 1.1.1.7 christos pyuw_dealloc_cache. */ 133 1.1.1.7 christos cached_reg_t reg[0]; 134 1.1.1.6 christos }; 135 1.1 christos 136 1.1 christos extern PyTypeObject pending_frame_object_type 137 1.1 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pending_frame_object"); 138 1.1 christos 139 1.1 christos extern PyTypeObject unwind_info_object_type 140 1.1 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("unwind_info_object"); 141 1.1 christos 142 1.1.1.7 christos /* An enum returned by pyuw_object_attribute_to_pointer, a function which 143 1.1.1.7 christos is used to extract an attribute from a Python object. */ 144 1.1 christos 145 1.1.1.7 christos enum class pyuw_get_attr_code 146 1.1 christos { 147 1.1.1.7 christos /* The attribute was present, and its value was successfully extracted. */ 148 1.1.1.7 christos ATTR_OK, 149 1.1 christos 150 1.1.1.7 christos /* The attribute was not present, or was present and its value was None. 151 1.1.1.7 christos No Python error has been set. */ 152 1.1.1.7 christos ATTR_MISSING, 153 1.1.1.7 christos 154 1.1.1.7 christos /* The attribute was present, but there was some error while trying to 155 1.1.1.7 christos get the value from the attribute. A Python error will be set when 156 1.1.1.7 christos this is returned. */ 157 1.1.1.7 christos ATTR_ERROR, 158 1.1.1.7 christos }; 159 1.1 christos 160 1.1.1.7 christos /* Get the attribute named ATTR_NAME from the object PYO and convert it to 161 1.1.1.7 christos an inferior pointer value, placing the pointer in *ADDR. 162 1.1 christos 163 1.1.1.7 christos Return pyuw_get_attr_code::ATTR_OK if the attribute was present and its 164 1.1.1.7 christos value was successfully written into *ADDR. For any other return value 165 1.1.1.7 christos the contents of *ADDR are undefined. 166 1.1.1.7 christos 167 1.1.1.7 christos Return pyuw_get_attr_code::ATTR_MISSING if the attribute was not 168 1.1.1.7 christos present, or it was present but its value was None. The contents of 169 1.1.1.7 christos *ADDR are undefined in this case. No Python error will be set in this 170 1.1.1.7 christos case. 171 1.1.1.7 christos 172 1.1.1.7 christos Return pyuw_get_attr_code::ATTR_ERROR if the attribute was present, but 173 1.1.1.7 christos there was some error while extracting the attribute's value. A Python 174 1.1.1.7 christos error will be set in this case. The contents of *ADDR are undefined. */ 175 1.1.1.7 christos 176 1.1.1.7 christos static pyuw_get_attr_code 177 1.1 christos pyuw_object_attribute_to_pointer (PyObject *pyo, const char *attr_name, 178 1.1.1.6 christos CORE_ADDR *addr) 179 1.1 christos { 180 1.1.1.7 christos if (!PyObject_HasAttrString (pyo, attr_name)) 181 1.1.1.7 christos return pyuw_get_attr_code::ATTR_MISSING; 182 1.1 christos 183 1.1.1.7 christos gdbpy_ref<> pyo_value (PyObject_GetAttrString (pyo, attr_name)); 184 1.1.1.7 christos if (pyo_value == nullptr) 185 1.1 christos { 186 1.1.1.7 christos gdb_assert (PyErr_Occurred ()); 187 1.1.1.7 christos return pyuw_get_attr_code::ATTR_ERROR; 188 1.1.1.7 christos } 189 1.1.1.7 christos if (pyo_value == Py_None) 190 1.1.1.7 christos return pyuw_get_attr_code::ATTR_MISSING; 191 1.1 christos 192 1.1.1.7 christos if (get_addr_from_python (pyo_value.get (), addr) < 0) 193 1.1.1.7 christos { 194 1.1.1.7 christos gdb_assert (PyErr_Occurred ()); 195 1.1.1.7 christos return pyuw_get_attr_code::ATTR_ERROR; 196 1.1 christos } 197 1.1.1.7 christos 198 1.1.1.7 christos return pyuw_get_attr_code::ATTR_OK; 199 1.1 christos } 200 1.1 christos 201 1.1 christos /* Called by the Python interpreter to obtain string representation 202 1.1 christos of the UnwindInfo object. */ 203 1.1 christos 204 1.1 christos static PyObject * 205 1.1 christos unwind_infopy_str (PyObject *self) 206 1.1 christos { 207 1.1 christos unwind_info_object *unwind_info = (unwind_info_object *) self; 208 1.1.1.3 christos string_file stb; 209 1.1 christos 210 1.1.1.6 christos stb.printf ("Frame ID: %s", unwind_info->frame_id.to_string ().c_str ()); 211 1.1 christos { 212 1.1.1.3 christos const char *sep = ""; 213 1.1 christos struct value_print_options opts; 214 1.1 christos 215 1.1 christos get_user_print_options (&opts); 216 1.1.1.3 christos stb.printf ("\nSaved registers: ("); 217 1.1.1.4 christos for (const saved_reg ® : *unwind_info->saved_regs) 218 1.1 christos { 219 1.1.1.6 christos struct value *value = value_object_to_value (reg.value.get ()); 220 1.1 christos 221 1.1.1.6 christos stb.printf ("%s(%d, ", sep, reg.number); 222 1.1.1.6 christos if (value != NULL) 223 1.1.1.6 christos { 224 1.1.1.6 christos try 225 1.1.1.6 christos { 226 1.1.1.6 christos value_print (value, &stb, &opts); 227 1.1.1.6 christos stb.puts (")"); 228 1.1.1.6 christos } 229 1.1.1.6 christos catch (const gdb_exception &except) 230 1.1.1.6 christos { 231 1.1.1.6 christos GDB_PY_HANDLE_EXCEPTION (except); 232 1.1.1.6 christos } 233 1.1.1.6 christos } 234 1.1.1.6 christos else 235 1.1.1.6 christos stb.puts ("<BAD>)"); 236 1.1.1.6 christos sep = ", "; 237 1.1 christos } 238 1.1.1.3 christos stb.puts (")"); 239 1.1 christos } 240 1.1 christos 241 1.1.1.6 christos return PyUnicode_FromString (stb.c_str ()); 242 1.1 christos } 243 1.1 christos 244 1.1.1.7 christos /* Implement UnwindInfo.__repr__(). */ 245 1.1.1.7 christos 246 1.1.1.7 christos static PyObject * 247 1.1.1.7 christos unwind_infopy_repr (PyObject *self) 248 1.1.1.7 christos { 249 1.1.1.7 christos unwind_info_object *unwind_info = (unwind_info_object *) self; 250 1.1.1.7 christos pending_frame_object *pending_frame 251 1.1.1.7 christos = (pending_frame_object *) (unwind_info->pending_frame); 252 1.1.1.7 christos frame_info_ptr frame = pending_frame->frame_info; 253 1.1.1.7 christos 254 1.1.1.7 christos if (frame == nullptr) 255 1.1.1.7 christos return PyUnicode_FromFormat ("<%s for an invalid frame>", 256 1.1.1.7 christos Py_TYPE (self)->tp_name); 257 1.1.1.7 christos 258 1.1.1.7 christos std::string saved_reg_names; 259 1.1.1.7 christos struct gdbarch *gdbarch = pending_frame->gdbarch; 260 1.1.1.7 christos 261 1.1.1.7 christos for (const saved_reg ® : *unwind_info->saved_regs) 262 1.1.1.7 christos { 263 1.1.1.7 christos const char *name = gdbarch_register_name (gdbarch, reg.number); 264 1.1.1.7 christos if (saved_reg_names.empty ()) 265 1.1.1.7 christos saved_reg_names = name; 266 1.1.1.7 christos else 267 1.1.1.7 christos saved_reg_names = (saved_reg_names + ", ") + name; 268 1.1.1.7 christos } 269 1.1.1.7 christos 270 1.1.1.7 christos return PyUnicode_FromFormat ("<%s frame #%d, saved_regs=(%s)>", 271 1.1.1.7 christos Py_TYPE (self)->tp_name, 272 1.1.1.7 christos frame_relative_level (frame), 273 1.1.1.7 christos saved_reg_names.c_str ()); 274 1.1.1.7 christos } 275 1.1.1.7 christos 276 1.1 christos /* Create UnwindInfo instance for given PendingFrame and frame ID. 277 1.1.1.7 christos Sets Python error and returns NULL on error. 278 1.1.1.7 christos 279 1.1.1.7 christos The PYO_PENDING_FRAME object must be valid. */ 280 1.1 christos 281 1.1 christos static PyObject * 282 1.1 christos pyuw_create_unwind_info (PyObject *pyo_pending_frame, 283 1.1.1.6 christos struct frame_id frame_id) 284 1.1 christos { 285 1.1.1.7 christos gdb_assert (((pending_frame_object *) pyo_pending_frame)->frame_info 286 1.1.1.7 christos != nullptr); 287 1.1.1.7 christos 288 1.1 christos unwind_info_object *unwind_info 289 1.1.1.7 christos = PyObject_New (unwind_info_object, &unwind_info_object_type); 290 1.1 christos 291 1.1 christos unwind_info->frame_id = frame_id; 292 1.1 christos Py_INCREF (pyo_pending_frame); 293 1.1 christos unwind_info->pending_frame = pyo_pending_frame; 294 1.1.1.4 christos unwind_info->saved_regs = new std::vector<saved_reg>; 295 1.1 christos return (PyObject *) unwind_info; 296 1.1 christos } 297 1.1 christos 298 1.1 christos /* The implementation of 299 1.1 christos gdb.UnwindInfo.add_saved_register (REG, VALUE) -> None. */ 300 1.1 christos 301 1.1 christos static PyObject * 302 1.1.1.7 christos unwind_infopy_add_saved_register (PyObject *self, PyObject *args, PyObject *kw) 303 1.1 christos { 304 1.1 christos unwind_info_object *unwind_info = (unwind_info_object *) self; 305 1.1 christos pending_frame_object *pending_frame 306 1.1 christos = (pending_frame_object *) (unwind_info->pending_frame); 307 1.1 christos PyObject *pyo_reg_id; 308 1.1 christos PyObject *pyo_reg_value; 309 1.1 christos int regnum; 310 1.1 christos 311 1.1 christos if (pending_frame->frame_info == NULL) 312 1.1 christos { 313 1.1 christos PyErr_SetString (PyExc_ValueError, 314 1.1.1.6 christos "UnwindInfo instance refers to a stale PendingFrame"); 315 1.1.1.7 christos return nullptr; 316 1.1 christos } 317 1.1.1.7 christos 318 1.1.1.7 christos static const char *keywords[] = { "register", "value", nullptr }; 319 1.1.1.7 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OO!", keywords, 320 1.1.1.7 christos &pyo_reg_id, &value_object_type, 321 1.1.1.7 christos &pyo_reg_value)) 322 1.1.1.7 christos return nullptr; 323 1.1.1.7 christos 324 1.1.1.5 christos if (!gdbpy_parse_register_id (pending_frame->gdbarch, pyo_reg_id, ®num)) 325 1.1.1.6 christos return nullptr; 326 1.1.1.6 christos 327 1.1.1.6 christos /* If REGNUM identifies a user register then *maybe* we can convert this 328 1.1.1.6 christos to a real (i.e. non-user) register. The maybe qualifier is because we 329 1.1.1.6 christos don't know what user registers each target might add, however, the 330 1.1.1.6 christos following logic should work for the usual style of user registers, 331 1.1.1.6 christos where the read function just forwards the register read on to some 332 1.1.1.6 christos other register with no adjusting the value. */ 333 1.1.1.6 christos if (regnum >= gdbarch_num_cooked_regs (pending_frame->gdbarch)) 334 1.1.1.6 christos { 335 1.1.1.6 christos struct value *user_reg_value 336 1.1.1.6 christos = value_of_user_reg (regnum, pending_frame->frame_info); 337 1.1.1.7 christos if (user_reg_value->lval () == lval_register) 338 1.1.1.7 christos regnum = user_reg_value->regnum (); 339 1.1.1.6 christos if (regnum >= gdbarch_num_cooked_regs (pending_frame->gdbarch)) 340 1.1.1.6 christos { 341 1.1.1.6 christos PyErr_SetString (PyExc_ValueError, "Bad register"); 342 1.1.1.6 christos return NULL; 343 1.1.1.6 christos } 344 1.1 christos } 345 1.1.1.6 christos 346 1.1.1.7 christos /* The argument parsing above guarantees that PYO_REG_VALUE will be a 347 1.1.1.7 christos gdb.Value object, as a result the value_object_to_value call should 348 1.1.1.7 christos succeed. */ 349 1.1.1.7 christos gdb_assert (pyo_reg_value != nullptr); 350 1.1.1.7 christos struct value *value = value_object_to_value (pyo_reg_value); 351 1.1.1.7 christos gdb_assert (value != nullptr); 352 1.1.1.7 christos 353 1.1.1.7 christos ULONGEST reg_size = register_size (pending_frame->gdbarch, regnum); 354 1.1.1.7 christos if (reg_size != value->type ()->length ()) 355 1.1.1.7 christos { 356 1.1.1.7 christos PyErr_Format (PyExc_ValueError, 357 1.1.1.7 christos "The value of the register returned by the Python " 358 1.1.1.7 christos "sniffer has unexpected size: %s instead of %s.", 359 1.1.1.7 christos pulongest (value->type ()->length ()), 360 1.1.1.7 christos pulongest (reg_size)); 361 1.1.1.7 christos return nullptr; 362 1.1.1.7 christos } 363 1.1.1.7 christos 364 1.1.1.7 christos 365 1.1.1.7 christos try 366 1.1.1.7 christos { 367 1.1.1.7 christos if (value->optimized_out () || !value->entirely_available ()) 368 1.1.1.7 christos { 369 1.1.1.7 christos /* If we allow this value to be registered here, pyuw_sniffer is going 370 1.1.1.7 christos to run into an exception when trying to access its contents. 371 1.1.1.7 christos Throwing an exception here just puts a burden on the user to 372 1.1.1.7 christos implement the same checks on the user side. We could return False 373 1.1.1.7 christos here and True otherwise, but again that might require changes in 374 1.1.1.7 christos user code. So, handle this with minimal impact for the user, while 375 1.1.1.7 christos improving robustness: silently ignore the register/value pair. */ 376 1.1.1.7 christos Py_RETURN_NONE; 377 1.1.1.7 christos } 378 1.1.1.7 christos } 379 1.1.1.7 christos catch (const gdb_exception &except) 380 1.1.1.7 christos { 381 1.1.1.7 christos GDB_PY_HANDLE_EXCEPTION (except); 382 1.1.1.7 christos } 383 1.1.1.7 christos 384 1.1.1.7 christos gdbpy_ref<> new_value = gdbpy_ref<>::new_reference (pyo_reg_value); 385 1.1.1.7 christos bool found = false; 386 1.1.1.7 christos for (saved_reg ® : *unwind_info->saved_regs) 387 1.1.1.7 christos { 388 1.1.1.7 christos if (regnum == reg.number) 389 1.1.1.7 christos { 390 1.1.1.7 christos found = true; 391 1.1.1.7 christos reg.value = std::move (new_value); 392 1.1.1.7 christos break; 393 1.1.1.7 christos } 394 1.1.1.7 christos } 395 1.1.1.7 christos if (!found) 396 1.1.1.7 christos unwind_info->saved_regs->emplace_back (regnum, std::move (new_value)); 397 1.1 christos 398 1.1 christos Py_RETURN_NONE; 399 1.1 christos } 400 1.1 christos 401 1.1 christos /* UnwindInfo cleanup. */ 402 1.1 christos 403 1.1 christos static void 404 1.1 christos unwind_infopy_dealloc (PyObject *self) 405 1.1 christos { 406 1.1 christos unwind_info_object *unwind_info = (unwind_info_object *) self; 407 1.1 christos 408 1.1 christos Py_XDECREF (unwind_info->pending_frame); 409 1.1.1.4 christos delete unwind_info->saved_regs; 410 1.1 christos Py_TYPE (self)->tp_free (self); 411 1.1 christos } 412 1.1 christos 413 1.1 christos /* Called by the Python interpreter to obtain string representation 414 1.1 christos of the PendingFrame object. */ 415 1.1 christos 416 1.1 christos static PyObject * 417 1.1 christos pending_framepy_str (PyObject *self) 418 1.1 christos { 419 1.1.1.6 christos frame_info_ptr frame = ((pending_frame_object *) self)->frame_info; 420 1.1 christos const char *sp_str = NULL; 421 1.1 christos const char *pc_str = NULL; 422 1.1 christos 423 1.1 christos if (frame == NULL) 424 1.1.1.6 christos return PyUnicode_FromString ("Stale PendingFrame instance"); 425 1.1.1.5 christos try 426 1.1 christos { 427 1.1 christos sp_str = core_addr_to_string_nz (get_frame_sp (frame)); 428 1.1 christos pc_str = core_addr_to_string_nz (get_frame_pc (frame)); 429 1.1 christos } 430 1.1.1.5 christos catch (const gdb_exception &except) 431 1.1 christos { 432 1.1 christos GDB_PY_HANDLE_EXCEPTION (except); 433 1.1 christos } 434 1.1 christos 435 1.1.1.6 christos return PyUnicode_FromFormat ("SP=%s,PC=%s", sp_str, pc_str); 436 1.1 christos } 437 1.1 christos 438 1.1.1.7 christos /* Implement PendingFrame.__repr__(). */ 439 1.1.1.7 christos 440 1.1.1.7 christos static PyObject * 441 1.1.1.7 christos pending_framepy_repr (PyObject *self) 442 1.1.1.7 christos { 443 1.1.1.7 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 444 1.1.1.7 christos frame_info_ptr frame = pending_frame->frame_info; 445 1.1.1.7 christos 446 1.1.1.7 christos if (frame == nullptr) 447 1.1.1.7 christos return gdb_py_invalid_object_repr (self); 448 1.1.1.7 christos 449 1.1.1.7 christos const char *sp_str = nullptr; 450 1.1.1.7 christos const char *pc_str = nullptr; 451 1.1.1.7 christos 452 1.1.1.7 christos try 453 1.1.1.7 christos { 454 1.1.1.7 christos sp_str = core_addr_to_string_nz (get_frame_sp (frame)); 455 1.1.1.7 christos pc_str = core_addr_to_string_nz (get_frame_pc (frame)); 456 1.1.1.7 christos } 457 1.1.1.7 christos catch (const gdb_exception &except) 458 1.1.1.7 christos { 459 1.1.1.7 christos GDB_PY_HANDLE_EXCEPTION (except); 460 1.1.1.7 christos } 461 1.1.1.7 christos 462 1.1.1.7 christos return PyUnicode_FromFormat ("<%s level=%d, sp=%s, pc=%s>", 463 1.1.1.7 christos Py_TYPE (self)->tp_name, 464 1.1.1.7 christos frame_relative_level (frame), 465 1.1.1.7 christos sp_str, 466 1.1.1.7 christos pc_str); 467 1.1.1.7 christos } 468 1.1.1.7 christos 469 1.1 christos /* Implementation of gdb.PendingFrame.read_register (self, reg) -> gdb.Value. 470 1.1 christos Returns the value of register REG as gdb.Value instance. */ 471 1.1 christos 472 1.1 christos static PyObject * 473 1.1.1.7 christos pending_framepy_read_register (PyObject *self, PyObject *args, PyObject *kw) 474 1.1 christos { 475 1.1 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 476 1.1.1.7 christos PENDING_FRAMEPY_REQUIRE_VALID (pending_frame); 477 1.1.1.7 christos 478 1.1 christos PyObject *pyo_reg_id; 479 1.1.1.7 christos static const char *keywords[] = { "register", nullptr }; 480 1.1.1.7 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O", keywords, &pyo_reg_id)) 481 1.1.1.7 christos return nullptr; 482 1.1 christos 483 1.1.1.7 christos int regnum; 484 1.1.1.5 christos if (!gdbpy_parse_register_id (pending_frame->gdbarch, pyo_reg_id, ®num)) 485 1.1.1.6 christos return nullptr; 486 1.1 christos 487 1.1.1.7 christos PyObject *result = nullptr; 488 1.1.1.5 christos try 489 1.1 christos { 490 1.1.1.7 christos scoped_value_mark free_values; 491 1.1.1.7 christos 492 1.1.1.3 christos /* Fetch the value associated with a register, whether it's 493 1.1.1.3 christos a real register or a so called "user" register, like "pc", 494 1.1.1.3 christos which maps to a real register. In the past, 495 1.1.1.3 christos get_frame_register_value() was used here, which did not 496 1.1.1.3 christos handle the user register case. */ 497 1.1.1.7 christos value *val = value_of_register 498 1.1.1.7 christos (regnum, get_next_frame_sentinel_okay (pending_frame->frame_info)); 499 1.1 christos if (val == NULL) 500 1.1.1.6 christos PyErr_Format (PyExc_ValueError, 501 1.1.1.6 christos "Cannot read register %d from frame.", 502 1.1.1.6 christos regnum); 503 1.1.1.7 christos else 504 1.1.1.7 christos result = value_to_value_object (val); 505 1.1.1.7 christos } 506 1.1.1.7 christos catch (const gdb_exception &except) 507 1.1.1.7 christos { 508 1.1.1.7 christos GDB_PY_HANDLE_EXCEPTION (except); 509 1.1.1.7 christos } 510 1.1.1.7 christos 511 1.1.1.7 christos return result; 512 1.1.1.7 christos } 513 1.1.1.7 christos 514 1.1.1.7 christos /* Implement PendingFrame.is_valid(). Return True if this pending frame 515 1.1.1.7 christos object is still valid. */ 516 1.1.1.7 christos 517 1.1.1.7 christos static PyObject * 518 1.1.1.7 christos pending_framepy_is_valid (PyObject *self, PyObject *args) 519 1.1.1.7 christos { 520 1.1.1.7 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 521 1.1.1.7 christos 522 1.1.1.7 christos if (pending_frame->frame_info == nullptr) 523 1.1.1.7 christos Py_RETURN_FALSE; 524 1.1.1.7 christos 525 1.1.1.7 christos Py_RETURN_TRUE; 526 1.1.1.7 christos } 527 1.1.1.7 christos 528 1.1.1.7 christos /* Implement PendingFrame.name(). Return a string that is the name of the 529 1.1.1.7 christos function for this frame, or None if the name can't be found. */ 530 1.1.1.7 christos 531 1.1.1.7 christos static PyObject * 532 1.1.1.7 christos pending_framepy_name (PyObject *self, PyObject *args) 533 1.1.1.7 christos { 534 1.1.1.7 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 535 1.1.1.7 christos 536 1.1.1.7 christos PENDING_FRAMEPY_REQUIRE_VALID (pending_frame); 537 1.1.1.7 christos 538 1.1.1.7 christos gdb::unique_xmalloc_ptr<char> name; 539 1.1.1.7 christos 540 1.1.1.7 christos try 541 1.1.1.7 christos { 542 1.1.1.7 christos enum language lang; 543 1.1.1.7 christos frame_info_ptr frame = pending_frame->frame_info; 544 1.1.1.7 christos 545 1.1.1.7 christos name = find_frame_funname (frame, &lang, nullptr); 546 1.1 christos } 547 1.1.1.5 christos catch (const gdb_exception &except) 548 1.1 christos { 549 1.1 christos GDB_PY_HANDLE_EXCEPTION (except); 550 1.1 christos } 551 1.1 christos 552 1.1.1.7 christos if (name != nullptr) 553 1.1.1.7 christos return PyUnicode_Decode (name.get (), strlen (name.get ()), 554 1.1.1.7 christos host_charset (), nullptr); 555 1.1.1.7 christos 556 1.1.1.7 christos Py_RETURN_NONE; 557 1.1.1.7 christos } 558 1.1.1.7 christos 559 1.1.1.7 christos /* Implement gdb.PendingFrame.pc(). Returns an integer containing the 560 1.1.1.7 christos frame's current $pc value. */ 561 1.1.1.7 christos 562 1.1.1.7 christos static PyObject * 563 1.1.1.7 christos pending_framepy_pc (PyObject *self, PyObject *args) 564 1.1.1.7 christos { 565 1.1.1.7 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 566 1.1.1.7 christos 567 1.1.1.7 christos PENDING_FRAMEPY_REQUIRE_VALID (pending_frame); 568 1.1.1.7 christos 569 1.1.1.7 christos CORE_ADDR pc = 0; 570 1.1.1.7 christos 571 1.1.1.7 christos try 572 1.1.1.7 christos { 573 1.1.1.7 christos pc = get_frame_pc (pending_frame->frame_info); 574 1.1.1.7 christos } 575 1.1.1.7 christos catch (const gdb_exception &except) 576 1.1.1.7 christos { 577 1.1.1.7 christos GDB_PY_HANDLE_EXCEPTION (except); 578 1.1.1.7 christos } 579 1.1.1.7 christos 580 1.1.1.7 christos return gdb_py_object_from_ulongest (pc).release (); 581 1.1.1.7 christos } 582 1.1.1.7 christos 583 1.1.1.7 christos /* Implement gdb.PendingFrame.language(). Return the name of the language 584 1.1.1.7 christos for this frame. */ 585 1.1.1.7 christos 586 1.1.1.7 christos static PyObject * 587 1.1.1.7 christos pending_framepy_language (PyObject *self, PyObject *args) 588 1.1.1.7 christos { 589 1.1.1.7 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 590 1.1.1.7 christos 591 1.1.1.7 christos PENDING_FRAMEPY_REQUIRE_VALID (pending_frame); 592 1.1.1.7 christos 593 1.1.1.7 christos try 594 1.1.1.7 christos { 595 1.1.1.7 christos frame_info_ptr fi = pending_frame->frame_info; 596 1.1.1.7 christos 597 1.1.1.7 christos enum language lang = get_frame_language (fi); 598 1.1.1.7 christos const language_defn *lang_def = language_def (lang); 599 1.1.1.7 christos 600 1.1.1.7 christos return host_string_to_python_string (lang_def->name ()).release (); 601 1.1.1.7 christos } 602 1.1.1.7 christos catch (const gdb_exception &except) 603 1.1.1.7 christos { 604 1.1.1.7 christos GDB_PY_HANDLE_EXCEPTION (except); 605 1.1.1.7 christos } 606 1.1.1.7 christos 607 1.1.1.7 christos Py_RETURN_NONE; 608 1.1.1.7 christos } 609 1.1.1.7 christos 610 1.1.1.7 christos /* Implement PendingFrame.find_sal(). Return the PendingFrame's symtab and 611 1.1.1.7 christos line. */ 612 1.1.1.7 christos 613 1.1.1.7 christos static PyObject * 614 1.1.1.7 christos pending_framepy_find_sal (PyObject *self, PyObject *args) 615 1.1.1.7 christos { 616 1.1.1.7 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 617 1.1.1.7 christos 618 1.1.1.7 christos PENDING_FRAMEPY_REQUIRE_VALID (pending_frame); 619 1.1.1.7 christos 620 1.1.1.7 christos PyObject *sal_obj = nullptr; 621 1.1.1.7 christos 622 1.1.1.7 christos try 623 1.1.1.7 christos { 624 1.1.1.7 christos frame_info_ptr frame = pending_frame->frame_info; 625 1.1.1.7 christos 626 1.1.1.7 christos symtab_and_line sal = find_frame_sal (frame); 627 1.1.1.7 christos sal_obj = symtab_and_line_to_sal_object (sal); 628 1.1.1.7 christos } 629 1.1.1.7 christos catch (const gdb_exception &except) 630 1.1.1.7 christos { 631 1.1.1.7 christos GDB_PY_HANDLE_EXCEPTION (except); 632 1.1.1.7 christos } 633 1.1.1.7 christos 634 1.1.1.7 christos return sal_obj; 635 1.1.1.7 christos } 636 1.1.1.7 christos 637 1.1.1.7 christos /* Implement PendingFrame.block(). Return a gdb.Block for the pending 638 1.1.1.7 christos frame's code, or raise RuntimeError if the block can't be found. */ 639 1.1.1.7 christos 640 1.1.1.7 christos static PyObject * 641 1.1.1.7 christos pending_framepy_block (PyObject *self, PyObject *args) 642 1.1.1.7 christos { 643 1.1.1.7 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 644 1.1.1.7 christos 645 1.1.1.7 christos PENDING_FRAMEPY_REQUIRE_VALID (pending_frame); 646 1.1.1.7 christos 647 1.1.1.7 christos frame_info_ptr frame = pending_frame->frame_info; 648 1.1.1.7 christos const struct block *block = nullptr, *fn_block; 649 1.1.1.7 christos 650 1.1.1.7 christos try 651 1.1.1.7 christos { 652 1.1.1.7 christos block = get_frame_block (frame, nullptr); 653 1.1.1.7 christos } 654 1.1.1.7 christos catch (const gdb_exception &except) 655 1.1.1.7 christos { 656 1.1.1.7 christos GDB_PY_HANDLE_EXCEPTION (except); 657 1.1.1.7 christos } 658 1.1.1.7 christos 659 1.1.1.7 christos for (fn_block = block; 660 1.1.1.7 christos fn_block != nullptr && fn_block->function () == nullptr; 661 1.1.1.7 christos fn_block = fn_block->superblock ()) 662 1.1.1.7 christos ; 663 1.1.1.7 christos 664 1.1.1.7 christos if (block == nullptr 665 1.1.1.7 christos || fn_block == nullptr 666 1.1.1.7 christos || fn_block->function () == nullptr) 667 1.1.1.7 christos { 668 1.1.1.7 christos PyErr_SetString (PyExc_RuntimeError, 669 1.1.1.7 christos _("Cannot locate block for frame.")); 670 1.1.1.7 christos return nullptr; 671 1.1.1.7 christos } 672 1.1.1.7 christos 673 1.1.1.7 christos return block_to_block_object (block, fn_block->function ()->objfile ()); 674 1.1.1.7 christos } 675 1.1.1.7 christos 676 1.1.1.7 christos /* Implement gdb.PendingFrame.function(). Return a gdb.Symbol 677 1.1.1.7 christos representing the function of this frame, or None if no suitable symbol 678 1.1.1.7 christos can be found. */ 679 1.1.1.7 christos 680 1.1.1.7 christos static PyObject * 681 1.1.1.7 christos pending_framepy_function (PyObject *self, PyObject *args) 682 1.1.1.7 christos { 683 1.1.1.7 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 684 1.1.1.7 christos 685 1.1.1.7 christos PENDING_FRAMEPY_REQUIRE_VALID (pending_frame); 686 1.1.1.7 christos 687 1.1.1.7 christos struct symbol *sym = nullptr; 688 1.1.1.7 christos 689 1.1.1.7 christos try 690 1.1.1.7 christos { 691 1.1.1.7 christos enum language funlang; 692 1.1.1.7 christos frame_info_ptr frame = pending_frame->frame_info; 693 1.1.1.7 christos 694 1.1.1.7 christos gdb::unique_xmalloc_ptr<char> funname 695 1.1.1.7 christos = find_frame_funname (frame, &funlang, &sym); 696 1.1.1.7 christos } 697 1.1.1.7 christos catch (const gdb_exception &except) 698 1.1.1.7 christos { 699 1.1.1.7 christos GDB_PY_HANDLE_EXCEPTION (except); 700 1.1.1.7 christos } 701 1.1.1.7 christos 702 1.1.1.7 christos if (sym != nullptr) 703 1.1.1.7 christos return symbol_to_symbol_object (sym); 704 1.1.1.7 christos 705 1.1.1.7 christos Py_RETURN_NONE; 706 1.1 christos } 707 1.1 christos 708 1.1 christos /* Implementation of 709 1.1 christos PendingFrame.create_unwind_info (self, frameId) -> UnwindInfo. */ 710 1.1 christos 711 1.1 christos static PyObject * 712 1.1.1.7 christos pending_framepy_create_unwind_info (PyObject *self, PyObject *args, 713 1.1.1.7 christos PyObject *kw) 714 1.1 christos { 715 1.1 christos PyObject *pyo_frame_id; 716 1.1 christos CORE_ADDR sp; 717 1.1 christos CORE_ADDR pc; 718 1.1 christos CORE_ADDR special; 719 1.1 christos 720 1.1.1.7 christos PENDING_FRAMEPY_REQUIRE_VALID ((pending_frame_object *) self); 721 1.1.1.7 christos 722 1.1.1.7 christos static const char *keywords[] = { "frame_id", nullptr }; 723 1.1.1.7 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O", keywords, 724 1.1.1.7 christos &pyo_frame_id)) 725 1.1.1.7 christos return nullptr; 726 1.1.1.7 christos 727 1.1.1.7 christos pyuw_get_attr_code code 728 1.1.1.7 christos = pyuw_object_attribute_to_pointer (pyo_frame_id, "sp", &sp); 729 1.1.1.7 christos if (code == pyuw_get_attr_code::ATTR_MISSING) 730 1.1 christos { 731 1.1 christos PyErr_SetString (PyExc_ValueError, 732 1.1.1.6 christos _("frame_id should have 'sp' attribute.")); 733 1.1.1.7 christos return nullptr; 734 1.1 christos } 735 1.1.1.7 christos else if (code == pyuw_get_attr_code::ATTR_ERROR) 736 1.1.1.7 christos return nullptr; 737 1.1 christos 738 1.1 christos /* The logic of building frame_id depending on the attributes of 739 1.1 christos the frame_id object: 740 1.1 christos Has Has Has Function to call 741 1.1 christos 'sp'? 'pc'? 'special'? 742 1.1 christos ------|------|--------------|------------------------- 743 1.1 christos Y N * frame_id_build_wild (sp) 744 1.1 christos Y Y N frame_id_build (sp, pc) 745 1.1 christos Y Y Y frame_id_build_special (sp, pc, special) 746 1.1 christos */ 747 1.1.1.7 christos code = pyuw_object_attribute_to_pointer (pyo_frame_id, "pc", &pc); 748 1.1.1.7 christos if (code == pyuw_get_attr_code::ATTR_ERROR) 749 1.1.1.7 christos return nullptr; 750 1.1.1.7 christos else if (code == pyuw_get_attr_code::ATTR_MISSING) 751 1.1 christos return pyuw_create_unwind_info (self, frame_id_build_wild (sp)); 752 1.1.1.7 christos 753 1.1.1.7 christos code = pyuw_object_attribute_to_pointer (pyo_frame_id, "special", &special); 754 1.1.1.7 christos if (code == pyuw_get_attr_code::ATTR_ERROR) 755 1.1.1.7 christos return nullptr; 756 1.1.1.7 christos else if (code == pyuw_get_attr_code::ATTR_MISSING) 757 1.1 christos return pyuw_create_unwind_info (self, frame_id_build (sp, pc)); 758 1.1.1.7 christos 759 1.1.1.7 christos return pyuw_create_unwind_info (self, 760 1.1.1.7 christos frame_id_build_special (sp, pc, special)); 761 1.1 christos } 762 1.1 christos 763 1.1.1.5 christos /* Implementation of PendingFrame.architecture (self) -> gdb.Architecture. */ 764 1.1.1.5 christos 765 1.1.1.5 christos static PyObject * 766 1.1.1.5 christos pending_framepy_architecture (PyObject *self, PyObject *args) 767 1.1.1.5 christos { 768 1.1.1.5 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 769 1.1.1.5 christos 770 1.1.1.7 christos PENDING_FRAMEPY_REQUIRE_VALID (pending_frame); 771 1.1.1.7 christos 772 1.1.1.5 christos return gdbarch_to_arch_object (pending_frame->gdbarch); 773 1.1.1.5 christos } 774 1.1.1.5 christos 775 1.1.1.6 christos /* Implementation of PendingFrame.level (self) -> Integer. */ 776 1.1.1.6 christos 777 1.1.1.6 christos static PyObject * 778 1.1.1.6 christos pending_framepy_level (PyObject *self, PyObject *args) 779 1.1.1.6 christos { 780 1.1.1.6 christos pending_frame_object *pending_frame = (pending_frame_object *) self; 781 1.1.1.6 christos 782 1.1.1.7 christos PENDING_FRAMEPY_REQUIRE_VALID (pending_frame); 783 1.1.1.7 christos 784 1.1.1.6 christos int level = frame_relative_level (pending_frame->frame_info); 785 1.1.1.6 christos return gdb_py_object_from_longest (level).release (); 786 1.1.1.6 christos } 787 1.1.1.6 christos 788 1.1 christos /* frame_unwind.this_id method. */ 789 1.1 christos 790 1.1 christos static void 791 1.1.1.7 christos pyuw_this_id (const frame_info_ptr &this_frame, void **cache_ptr, 792 1.1.1.6 christos struct frame_id *this_id) 793 1.1 christos { 794 1.1 christos *this_id = ((cached_frame_info *) *cache_ptr)->frame_id; 795 1.1.1.6 christos pyuw_debug_printf ("frame_id: %s", this_id->to_string ().c_str ()); 796 1.1 christos } 797 1.1 christos 798 1.1 christos /* frame_unwind.prev_register. */ 799 1.1 christos 800 1.1 christos static struct value * 801 1.1.1.7 christos pyuw_prev_register (const frame_info_ptr &this_frame, void **cache_ptr, 802 1.1.1.6 christos int regnum) 803 1.1 christos { 804 1.1.1.6 christos PYUW_SCOPED_DEBUG_ENTER_EXIT; 805 1.1.1.6 christos 806 1.1.1.2 christos cached_frame_info *cached_frame = (cached_frame_info *) *cache_ptr; 807 1.1.1.4 christos cached_reg_t *reg_info = cached_frame->reg; 808 1.1.1.4 christos cached_reg_t *reg_info_end = reg_info + cached_frame->reg_count; 809 1.1 christos 810 1.1.1.6 christos pyuw_debug_printf ("frame=%d, reg=%d", 811 1.1.1.6 christos frame_relative_level (this_frame), regnum); 812 1.1 christos for (; reg_info < reg_info_end; ++reg_info) 813 1.1 christos { 814 1.1.1.4 christos if (regnum == reg_info->num) 815 1.1.1.7 christos return frame_unwind_got_bytes (this_frame, regnum, reg_info->data.get ()); 816 1.1 christos } 817 1.1 christos 818 1.1 christos return frame_unwind_got_optimized (this_frame, regnum); 819 1.1 christos } 820 1.1 christos 821 1.1 christos /* Frame sniffer dispatch. */ 822 1.1 christos 823 1.1 christos static int 824 1.1.1.7 christos pyuw_sniffer (const struct frame_unwind *self, const frame_info_ptr &this_frame, 825 1.1.1.6 christos void **cache_ptr) 826 1.1 christos { 827 1.1.1.6 christos PYUW_SCOPED_DEBUG_ENTER_EXIT; 828 1.1.1.6 christos 829 1.1 christos struct gdbarch *gdbarch = (struct gdbarch *) (self->unwind_data); 830 1.1 christos cached_frame_info *cached_frame; 831 1.1 christos 832 1.1.1.6 christos gdbpy_enter enter_py (gdbarch); 833 1.1.1.3 christos 834 1.1.1.6 christos pyuw_debug_printf ("frame=%d, sp=%s, pc=%s", 835 1.1.1.6 christos frame_relative_level (this_frame), 836 1.1.1.6 christos paddress (gdbarch, get_frame_sp (this_frame)), 837 1.1.1.6 christos paddress (gdbarch, get_frame_pc (this_frame))); 838 1.1 christos 839 1.1 christos /* Create PendingFrame instance to pass to sniffers. */ 840 1.1.1.3 christos pending_frame_object *pfo = PyObject_New (pending_frame_object, 841 1.1.1.3 christos &pending_frame_object_type); 842 1.1.1.3 christos gdbpy_ref<> pyo_pending_frame ((PyObject *) pfo); 843 1.1 christos if (pyo_pending_frame == NULL) 844 1.1.1.3 christos { 845 1.1.1.3 christos gdbpy_print_stack (); 846 1.1.1.3 christos return 0; 847 1.1.1.3 christos } 848 1.1.1.3 christos pfo->gdbarch = gdbarch; 849 1.1.1.7 christos pfo->frame_info = nullptr; 850 1.1.1.3 christos scoped_restore invalidate_frame = make_scoped_restore (&pfo->frame_info, 851 1.1.1.3 christos this_frame); 852 1.1 christos 853 1.1 christos /* Run unwinders. */ 854 1.1 christos if (gdb_python_module == NULL 855 1.1.1.5 christos || ! PyObject_HasAttrString (gdb_python_module, "_execute_unwinders")) 856 1.1 christos { 857 1.1 christos PyErr_SetString (PyExc_NameError, 858 1.1.1.6 christos "Installation error: gdb._execute_unwinders function " 859 1.1.1.6 christos "is missing"); 860 1.1.1.3 christos gdbpy_print_stack (); 861 1.1.1.3 christos return 0; 862 1.1 christos } 863 1.1.1.3 christos gdbpy_ref<> pyo_execute (PyObject_GetAttrString (gdb_python_module, 864 1.1.1.5 christos "_execute_unwinders")); 865 1.1.1.6 christos if (pyo_execute == nullptr) 866 1.1.1.3 christos { 867 1.1.1.3 christos gdbpy_print_stack (); 868 1.1.1.3 christos return 0; 869 1.1.1.3 christos } 870 1.1.1.3 christos 871 1.1.1.6 christos /* A (gdb.UnwindInfo, str) tuple, or None. */ 872 1.1.1.6 christos gdbpy_ref<> pyo_execute_ret 873 1.1.1.3 christos (PyObject_CallFunctionObjArgs (pyo_execute.get (), 874 1.1.1.3 christos pyo_pending_frame.get (), NULL)); 875 1.1.1.6 christos if (pyo_execute_ret == nullptr) 876 1.1.1.3 christos { 877 1.1.1.4 christos /* If the unwinder is cancelled due to a Ctrl-C, then propagate 878 1.1.1.4 christos the Ctrl-C as a GDB exception instead of swallowing it. */ 879 1.1.1.4 christos gdbpy_print_stack_or_quit (); 880 1.1.1.3 christos return 0; 881 1.1.1.3 christos } 882 1.1.1.6 christos if (pyo_execute_ret == Py_None) 883 1.1.1.3 christos return 0; 884 1.1 christos 885 1.1.1.6 christos /* Verify the return value of _execute_unwinders is a tuple of size 2. */ 886 1.1.1.6 christos gdb_assert (PyTuple_Check (pyo_execute_ret.get ())); 887 1.1.1.6 christos gdb_assert (PyTuple_GET_SIZE (pyo_execute_ret.get ()) == 2); 888 1.1.1.6 christos 889 1.1.1.6 christos if (pyuw_debug) 890 1.1.1.6 christos { 891 1.1.1.6 christos PyObject *pyo_unwinder_name = PyTuple_GET_ITEM (pyo_execute_ret.get (), 1); 892 1.1.1.6 christos gdb::unique_xmalloc_ptr<char> name 893 1.1.1.6 christos = python_string_to_host_string (pyo_unwinder_name); 894 1.1.1.6 christos 895 1.1.1.6 christos /* This could happen if the user passed something else than a string 896 1.1.1.6 christos as the unwinder's name. */ 897 1.1.1.6 christos if (name == nullptr) 898 1.1.1.6 christos { 899 1.1.1.6 christos gdbpy_print_stack (); 900 1.1.1.6 christos name = make_unique_xstrdup ("<failed to get unwinder name>"); 901 1.1.1.6 christos } 902 1.1.1.6 christos 903 1.1.1.6 christos pyuw_debug_printf ("frame claimed by unwinder %s", name.get ()); 904 1.1.1.6 christos } 905 1.1.1.6 christos 906 1.1 christos /* Received UnwindInfo, cache data. */ 907 1.1.1.6 christos PyObject *pyo_unwind_info = PyTuple_GET_ITEM (pyo_execute_ret.get (), 0); 908 1.1.1.6 christos if (PyObject_IsInstance (pyo_unwind_info, 909 1.1.1.6 christos (PyObject *) &unwind_info_object_type) <= 0) 910 1.1 christos error (_("A Unwinder should return gdb.UnwindInfo instance.")); 911 1.1 christos 912 1.1 christos { 913 1.1.1.3 christos unwind_info_object *unwind_info = 914 1.1.1.6 christos (unwind_info_object *) pyo_unwind_info; 915 1.1.1.4 christos int reg_count = unwind_info->saved_regs->size (); 916 1.1 christos 917 1.1.1.2 christos cached_frame 918 1.1.1.2 christos = ((cached_frame_info *) 919 1.1.1.2 christos xmalloc (sizeof (*cached_frame) 920 1.1.1.2 christos + reg_count * sizeof (cached_frame->reg[0]))); 921 1.1 christos cached_frame->gdbarch = gdbarch; 922 1.1 christos cached_frame->frame_id = unwind_info->frame_id; 923 1.1 christos cached_frame->reg_count = reg_count; 924 1.1 christos 925 1.1 christos /* Populate registers array. */ 926 1.1.1.4 christos for (int i = 0; i < unwind_info->saved_regs->size (); ++i) 927 1.1 christos { 928 1.1.1.4 christos saved_reg *reg = &(*unwind_info->saved_regs)[i]; 929 1.1.1.4 christos 930 1.1.1.6 christos struct value *value = value_object_to_value (reg->value.get ()); 931 1.1.1.6 christos size_t data_size = register_size (gdbarch, reg->number); 932 1.1 christos 933 1.1.1.6 christos /* `value' validation was done before, just assert. */ 934 1.1.1.6 christos gdb_assert (value != NULL); 935 1.1.1.7 christos gdb_assert (data_size == value->type ()->length ()); 936 1.1 christos 937 1.1.1.7 christos cached_reg_t *cached = new (&cached_frame->reg[i]) cached_reg_t (); 938 1.1.1.7 christos cached->num = reg->number; 939 1.1.1.7 christos cached->data.reset ((gdb_byte *) xmalloc (data_size)); 940 1.1.1.7 christos memcpy (cached->data.get (), value->contents ().data (), data_size); 941 1.1 christos } 942 1.1 christos } 943 1.1 christos 944 1.1 christos *cache_ptr = cached_frame; 945 1.1 christos return 1; 946 1.1 christos } 947 1.1 christos 948 1.1 christos /* Frame cache release shim. */ 949 1.1 christos 950 1.1 christos static void 951 1.1.1.6 christos pyuw_dealloc_cache (frame_info *this_frame, void *cache) 952 1.1 christos { 953 1.1.1.6 christos PYUW_SCOPED_DEBUG_ENTER_EXIT; 954 1.1.1.4 christos cached_frame_info *cached_frame = (cached_frame_info *) cache; 955 1.1.1.4 christos 956 1.1.1.4 christos for (int i = 0; i < cached_frame->reg_count; i++) 957 1.1.1.7 christos cached_frame->reg[i].~cached_reg_t (); 958 1.1.1.4 christos 959 1.1 christos xfree (cache); 960 1.1 christos } 961 1.1 christos 962 1.1 christos struct pyuw_gdbarch_data_type 963 1.1 christos { 964 1.1 christos /* Has the unwinder shim been prepended? */ 965 1.1.1.6 christos int unwinder_registered = 0; 966 1.1 christos }; 967 1.1 christos 968 1.1.1.6 christos static const registry<gdbarch>::key<pyuw_gdbarch_data_type> pyuw_gdbarch_data; 969 1.1 christos 970 1.1 christos /* New inferior architecture callback: register the Python unwinders 971 1.1 christos intermediary. */ 972 1.1 christos 973 1.1 christos static void 974 1.1.1.7 christos pyuw_on_new_gdbarch (gdbarch *newarch) 975 1.1 christos { 976 1.1.1.6 christos struct pyuw_gdbarch_data_type *data = pyuw_gdbarch_data.get (newarch); 977 1.1.1.6 christos if (data == nullptr) 978 1.1.1.6 christos data= pyuw_gdbarch_data.emplace (newarch); 979 1.1 christos 980 1.1 christos if (!data->unwinder_registered) 981 1.1 christos { 982 1.1 christos struct frame_unwind *unwinder 983 1.1.1.6 christos = GDBARCH_OBSTACK_ZALLOC (newarch, struct frame_unwind); 984 1.1 christos 985 1.1.1.6 christos unwinder->name = "python"; 986 1.1 christos unwinder->type = NORMAL_FRAME; 987 1.1 christos unwinder->stop_reason = default_frame_unwind_stop_reason; 988 1.1 christos unwinder->this_id = pyuw_this_id; 989 1.1 christos unwinder->prev_register = pyuw_prev_register; 990 1.1.1.2 christos unwinder->unwind_data = (const struct frame_data *) newarch; 991 1.1 christos unwinder->sniffer = pyuw_sniffer; 992 1.1 christos unwinder->dealloc_cache = pyuw_dealloc_cache; 993 1.1 christos frame_unwind_prepend_unwinder (newarch, unwinder); 994 1.1 christos data->unwinder_registered = 1; 995 1.1 christos } 996 1.1 christos } 997 1.1 christos 998 1.1 christos /* Initialize unwind machinery. */ 999 1.1 christos 1000 1.1.1.7 christos static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION 1001 1.1 christos gdbpy_initialize_unwind (void) 1002 1.1 christos { 1003 1.1.1.7 christos gdb::observers::new_architecture.attach (pyuw_on_new_gdbarch, "py-unwind"); 1004 1.1 christos 1005 1.1 christos if (PyType_Ready (&pending_frame_object_type) < 0) 1006 1.1 christos return -1; 1007 1.1.1.6 christos int rc = gdb_pymodule_addobject (gdb_module, "PendingFrame", 1008 1.1.1.6 christos (PyObject *) &pending_frame_object_type); 1009 1.1.1.6 christos if (rc != 0) 1010 1.1 christos return rc; 1011 1.1 christos 1012 1.1 christos if (PyType_Ready (&unwind_info_object_type) < 0) 1013 1.1 christos return -1; 1014 1.1 christos return gdb_pymodule_addobject (gdb_module, "UnwindInfo", 1015 1.1 christos (PyObject *) &unwind_info_object_type); 1016 1.1 christos } 1017 1.1 christos 1018 1.1.1.7 christos void _initialize_py_unwind (); 1019 1.1.1.7 christos void 1020 1.1.1.7 christos _initialize_py_unwind () 1021 1.1.1.7 christos { 1022 1.1.1.7 christos add_setshow_boolean_cmd 1023 1.1.1.7 christos ("py-unwind", class_maintenance, &pyuw_debug, 1024 1.1.1.7 christos _("Set Python unwinder debugging."), 1025 1.1.1.7 christos _("Show Python unwinder debugging."), 1026 1.1.1.7 christos _("When on, Python unwinder debugging is enabled."), 1027 1.1.1.7 christos NULL, 1028 1.1.1.7 christos show_pyuw_debug, 1029 1.1.1.7 christos &setdebuglist, &showdebuglist); 1030 1.1.1.7 christos } 1031 1.1.1.7 christos 1032 1.1.1.7 christos GDBPY_INITIALIZE_FILE (gdbpy_initialize_unwind); 1033 1.1.1.7 christos 1034 1.1.1.7 christos 1035 1.1.1.7 christos 1037 1.1 christos static PyMethodDef pending_frame_object_methods[] = 1038 1.1.1.7 christos { 1039 1.1.1.7 christos { "read_register", (PyCFunction) pending_framepy_read_register, 1040 1.1 christos METH_VARARGS | METH_KEYWORDS, 1041 1.1 christos "read_register (REG) -> gdb.Value\n" 1042 1.1.1.7 christos "Return the value of the REG in the frame." }, 1043 1.1.1.7 christos { "create_unwind_info", (PyCFunction) pending_framepy_create_unwind_info, 1044 1.1 christos METH_VARARGS | METH_KEYWORDS, 1045 1.1 christos "create_unwind_info (FRAME_ID) -> gdb.UnwindInfo\n" 1046 1.1 christos "Construct UnwindInfo for this PendingFrame, using FRAME_ID\n" 1047 1.1.1.5 christos "to identify it." }, 1048 1.1.1.5 christos { "architecture", 1049 1.1.1.5 christos pending_framepy_architecture, METH_NOARGS, 1050 1.1.1.5 christos "architecture () -> gdb.Architecture\n" 1051 1.1.1.7 christos "The architecture for this PendingFrame." }, 1052 1.1.1.7 christos { "name", 1053 1.1.1.7 christos pending_framepy_name, METH_NOARGS, 1054 1.1.1.7 christos "name() -> String.\n\ 1055 1.1.1.7 christos Return the function name of the frame, or None if it can't be determined." }, 1056 1.1.1.7 christos { "is_valid", 1057 1.1.1.7 christos pending_framepy_is_valid, METH_NOARGS, 1058 1.1.1.7 christos "is_valid () -> Boolean.\n\ 1059 1.1.1.7 christos Return true if this PendingFrame is valid, false if not." }, 1060 1.1.1.7 christos { "pc", 1061 1.1.1.7 christos pending_framepy_pc, METH_NOARGS, 1062 1.1.1.7 christos "pc () -> Long.\n\ 1063 1.1.1.7 christos Return the frame's resume address." }, 1064 1.1.1.7 christos { "language", pending_framepy_language, METH_NOARGS, 1065 1.1.1.7 christos "The language of this frame." }, 1066 1.1.1.7 christos { "find_sal", pending_framepy_find_sal, METH_NOARGS, 1067 1.1.1.7 christos "find_sal () -> gdb.Symtab_and_line.\n\ 1068 1.1.1.7 christos Return the frame's symtab and line." }, 1069 1.1.1.7 christos { "block", pending_framepy_block, METH_NOARGS, 1070 1.1.1.7 christos "block () -> gdb.Block.\n\ 1071 1.1.1.7 christos Return the frame's code block." }, 1072 1.1.1.7 christos { "function", pending_framepy_function, METH_NOARGS, 1073 1.1.1.7 christos "function () -> gdb.Symbol.\n\ 1074 1.1.1.6 christos Returns the symbol for the function corresponding to this frame." }, 1075 1.1.1.6 christos { "level", pending_framepy_level, METH_NOARGS, 1076 1.1 christos "The stack level of this frame." }, 1077 1.1 christos {NULL} /* Sentinel */ 1078 1.1 christos }; 1079 1.1 christos 1080 1.1 christos PyTypeObject pending_frame_object_type = 1081 1.1 christos { 1082 1.1 christos PyVarObject_HEAD_INIT (NULL, 0) 1083 1.1 christos "gdb.PendingFrame", /* tp_name */ 1084 1.1 christos sizeof (pending_frame_object), /* tp_basicsize */ 1085 1.1 christos 0, /* tp_itemsize */ 1086 1.1 christos 0, /* tp_dealloc */ 1087 1.1 christos 0, /* tp_print */ 1088 1.1 christos 0, /* tp_getattr */ 1089 1.1 christos 0, /* tp_setattr */ 1090 1.1.1.7 christos 0, /* tp_compare */ 1091 1.1 christos pending_framepy_repr, /* tp_repr */ 1092 1.1 christos 0, /* tp_as_number */ 1093 1.1 christos 0, /* tp_as_sequence */ 1094 1.1 christos 0, /* tp_as_mapping */ 1095 1.1 christos 0, /* tp_hash */ 1096 1.1 christos 0, /* tp_call */ 1097 1.1 christos pending_framepy_str, /* tp_str */ 1098 1.1 christos 0, /* tp_getattro */ 1099 1.1 christos 0, /* tp_setattro */ 1100 1.1 christos 0, /* tp_as_buffer */ 1101 1.1 christos Py_TPFLAGS_DEFAULT, /* tp_flags */ 1102 1.1 christos "GDB PendingFrame object", /* tp_doc */ 1103 1.1 christos 0, /* tp_traverse */ 1104 1.1 christos 0, /* tp_clear */ 1105 1.1 christos 0, /* tp_richcompare */ 1106 1.1 christos 0, /* tp_weaklistoffset */ 1107 1.1 christos 0, /* tp_iter */ 1108 1.1 christos 0, /* tp_iternext */ 1109 1.1 christos pending_frame_object_methods, /* tp_methods */ 1110 1.1 christos 0, /* tp_members */ 1111 1.1 christos 0, /* tp_getset */ 1112 1.1 christos 0, /* tp_base */ 1113 1.1 christos 0, /* tp_dict */ 1114 1.1 christos 0, /* tp_descr_get */ 1115 1.1 christos 0, /* tp_descr_set */ 1116 1.1 christos 0, /* tp_dictoffset */ 1117 1.1 christos 0, /* tp_init */ 1118 1.1 christos 0, /* tp_alloc */ 1119 1.1 christos }; 1120 1.1 christos 1121 1.1 christos static PyMethodDef unwind_info_object_methods[] = 1122 1.1 christos { 1123 1.1.1.7 christos { "add_saved_register", 1124 1.1.1.7 christos (PyCFunction) unwind_infopy_add_saved_register, 1125 1.1 christos METH_VARARGS | METH_KEYWORDS, 1126 1.1 christos "add_saved_register (REG, VALUE) -> None\n" 1127 1.1 christos "Set the value of the REG in the previous frame to VALUE." }, 1128 1.1 christos { NULL } /* Sentinel */ 1129 1.1 christos }; 1130 1.1 christos 1131 1.1 christos PyTypeObject unwind_info_object_type = 1132 1.1 christos { 1133 1.1 christos PyVarObject_HEAD_INIT (NULL, 0) 1134 1.1 christos "gdb.UnwindInfo", /* tp_name */ 1135 1.1 christos sizeof (unwind_info_object), /* tp_basicsize */ 1136 1.1 christos 0, /* tp_itemsize */ 1137 1.1 christos unwind_infopy_dealloc, /* tp_dealloc */ 1138 1.1 christos 0, /* tp_print */ 1139 1.1 christos 0, /* tp_getattr */ 1140 1.1 christos 0, /* tp_setattr */ 1141 1.1.1.7 christos 0, /* tp_compare */ 1142 1.1 christos unwind_infopy_repr, /* tp_repr */ 1143 1.1 christos 0, /* tp_as_number */ 1144 1.1 christos 0, /* tp_as_sequence */ 1145 1.1 christos 0, /* tp_as_mapping */ 1146 1.1 christos 0, /* tp_hash */ 1147 1.1 christos 0, /* tp_call */ 1148 1.1 christos unwind_infopy_str, /* tp_str */ 1149 1.1 christos 0, /* tp_getattro */ 1150 1.1 christos 0, /* tp_setattro */ 1151 1.1.1.7 christos 0, /* tp_as_buffer */ 1152 1.1 christos Py_TPFLAGS_DEFAULT, /* tp_flags */ 1153 1.1 christos "GDB UnwindInfo object", /* tp_doc */ 1154 1.1 christos 0, /* tp_traverse */ 1155 1.1 christos 0, /* tp_clear */ 1156 1.1 christos 0, /* tp_richcompare */ 1157 1.1 christos 0, /* tp_weaklistoffset */ 1158 1.1 christos 0, /* tp_iter */ 1159 1.1 christos 0, /* tp_iternext */ 1160 1.1 christos unwind_info_object_methods, /* tp_methods */ 1161 1.1 christos 0, /* tp_members */ 1162 1.1 christos 0, /* tp_getset */ 1163 1.1 christos 0, /* tp_base */ 1164 1.1 christos 0, /* tp_dict */ 1165 1.1 christos 0, /* tp_descr_get */ 1166 1.1 christos 0, /* tp_descr_set */ 1167 1.1 christos 0, /* tp_dictoffset */ 1168 1.1 christos 0, /* tp_init */ 1169 1.1 christos 0, /* tp_alloc */ 1170 }; 1171