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