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