Commit | Line | Data |
---|---|---|
d11916aa SS |
1 | /* Python frame unwinder interface. |
2 | ||
61baf725 | 3 | Copyright (C) 2015-2017 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" | |
c0171de6 | 31 | #include "py-ref.h" |
d11916aa SS |
32 | |
33 | #define TRACE_PY_UNWIND(level, args...) if (pyuw_debug >= level) \ | |
34 | { fprintf_unfiltered (gdb_stdlog, args); } | |
35 | ||
36 | typedef struct | |
37 | { | |
38 | PyObject_HEAD | |
39 | ||
40 | /* Frame we are unwinding. */ | |
41 | struct frame_info *frame_info; | |
42 | ||
43 | /* Its architecture, passed by the sniffer caller. */ | |
44 | struct gdbarch *gdbarch; | |
45 | } pending_frame_object; | |
46 | ||
47 | /* Saved registers array item. */ | |
48 | ||
49 | typedef struct | |
50 | { | |
51 | int number; | |
52 | PyObject *value; | |
53 | } saved_reg; | |
54 | DEF_VEC_O (saved_reg); | |
55 | ||
56 | /* The data we keep for the PyUnwindInfo: pending_frame, saved registers | |
57 | and frame ID. */ | |
58 | ||
59 | typedef struct | |
60 | { | |
61 | PyObject_HEAD | |
62 | ||
63 | /* gdb.PendingFrame for the frame we are unwinding. */ | |
64 | PyObject *pending_frame; | |
65 | ||
66 | /* Its ID. */ | |
67 | struct frame_id frame_id; | |
68 | ||
69 | /* Saved registers array. */ | |
70 | VEC (saved_reg) *saved_regs; | |
71 | } unwind_info_object; | |
72 | ||
73 | /* The data we keep for a frame we can unwind: frame ID and an array of | |
74 | (register_number, register_value) pairs. */ | |
75 | ||
13fa0398 YZ |
76 | struct reg_info |
77 | { | |
78 | /* Register number. */ | |
79 | int number; | |
80 | ||
81 | /* Register data bytes pointer. */ | |
82 | gdb_byte data[MAX_REGISTER_SIZE]; | |
83 | }; | |
84 | ||
d11916aa SS |
85 | typedef struct |
86 | { | |
87 | /* Frame ID. */ | |
88 | struct frame_id frame_id; | |
89 | ||
90 | /* GDB Architecture. */ | |
91 | struct gdbarch *gdbarch; | |
92 | ||
93 | /* Length of the `reg' array below. */ | |
94 | int reg_count; | |
95 | ||
13fa0398 | 96 | struct reg_info reg[]; |
d11916aa SS |
97 | } cached_frame_info; |
98 | ||
13fa0398 | 99 | extern PyTypeObject pending_frame_object_type |
d11916aa SS |
100 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pending_frame_object"); |
101 | ||
13fa0398 | 102 | extern PyTypeObject unwind_info_object_type |
d11916aa SS |
103 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("unwind_info_object"); |
104 | ||
105 | static unsigned int pyuw_debug = 0; | |
106 | ||
107 | static struct gdbarch_data *pyuw_gdbarch_data; | |
108 | ||
109 | /* Parses register id, which can be either a number or a name. | |
110 | Returns 1 on success, 0 otherwise. */ | |
111 | ||
112 | static int | |
113 | pyuw_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id, | |
114 | int *reg_num) | |
115 | { | |
116 | if (pyo_reg_id == NULL) | |
117 | return 0; | |
118 | if (gdbpy_is_string (pyo_reg_id)) | |
119 | { | |
9b972014 | 120 | gdb::unique_xmalloc_ptr<char> reg_name (gdbpy_obj_to_string (pyo_reg_id)); |
d11916aa SS |
121 | |
122 | if (reg_name == NULL) | |
123 | return 0; | |
9b972014 TT |
124 | *reg_num = user_reg_map_name_to_regnum (gdbarch, reg_name.get (), |
125 | strlen (reg_name.get ())); | |
d11916aa SS |
126 | return *reg_num >= 0; |
127 | } | |
128 | else if (PyInt_Check (pyo_reg_id)) | |
129 | { | |
130 | long value; | |
131 | if (gdb_py_int_as_long (pyo_reg_id, &value) && (int) value == value) | |
132 | { | |
133 | *reg_num = (int) value; | |
134 | return user_reg_map_regnum_to_name (gdbarch, *reg_num) != NULL; | |
135 | } | |
136 | } | |
137 | return 0; | |
138 | } | |
139 | ||
140 | /* Convert gdb.Value instance to inferior's pointer. Return 1 on success, | |
141 | 0 on failure. */ | |
142 | ||
143 | static int | |
144 | pyuw_value_obj_to_pointer (PyObject *pyo_value, CORE_ADDR *addr) | |
145 | { | |
146 | int rc = 0; | |
147 | struct value *value; | |
148 | ||
149 | TRY | |
150 | { | |
151 | if ((value = value_object_to_value (pyo_value)) != NULL) | |
152 | { | |
153 | *addr = unpack_pointer (value_type (value), | |
154 | value_contents (value)); | |
155 | rc = 1; | |
156 | } | |
157 | } | |
158 | CATCH (except, RETURN_MASK_ALL) | |
159 | { | |
160 | gdbpy_convert_exception (except); | |
161 | } | |
162 | END_CATCH | |
163 | return rc; | |
164 | } | |
165 | ||
166 | /* Get attribute from an object and convert it to the inferior's | |
167 | pointer value. Return 1 if attribute exists and its value can be | |
168 | converted. Otherwise, if attribute does not exist or its value is | |
169 | None, return 0. In all other cases set Python error and return | |
170 | 0. */ | |
171 | ||
172 | static int | |
173 | pyuw_object_attribute_to_pointer (PyObject *pyo, const char *attr_name, | |
174 | CORE_ADDR *addr) | |
175 | { | |
176 | int rc = 0; | |
177 | ||
178 | if (PyObject_HasAttrString (pyo, attr_name)) | |
179 | { | |
4586d543 | 180 | gdbpy_ref pyo_value (PyObject_GetAttrString (pyo, attr_name)); |
d11916aa SS |
181 | |
182 | if (pyo_value != NULL && pyo_value != Py_None) | |
183 | { | |
4586d543 | 184 | rc = pyuw_value_obj_to_pointer (pyo_value.get (), addr); |
d11916aa SS |
185 | if (!rc) |
186 | PyErr_Format ( | |
187 | PyExc_ValueError, | |
188 | _("The value of the '%s' attribute is not a pointer."), | |
189 | attr_name); | |
190 | } | |
d11916aa SS |
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 | { | |
33cc7d36 KB |
410 | /* Fetch the value associated with a register, whether it's |
411 | a real register or a so called "user" register, like "pc", | |
412 | which maps to a real register. In the past, | |
413 | get_frame_register_value() was used here, which did not | |
414 | handle the user register case. */ | |
415 | val = value_of_register (regnum, pending_frame->frame_info); | |
d11916aa SS |
416 | if (val == NULL) |
417 | PyErr_Format (PyExc_ValueError, | |
418 | "Cannot read register %d from frame.", | |
419 | regnum); | |
420 | } | |
421 | CATCH (except, RETURN_MASK_ALL) | |
422 | { | |
423 | GDB_PY_HANDLE_EXCEPTION (except); | |
424 | } | |
425 | END_CATCH | |
426 | ||
427 | return val == NULL ? NULL : value_to_value_object (val); | |
428 | } | |
429 | ||
430 | /* Implementation of | |
431 | PendingFrame.create_unwind_info (self, frameId) -> UnwindInfo. */ | |
432 | ||
433 | static PyObject * | |
434 | pending_framepy_create_unwind_info (PyObject *self, PyObject *args) | |
435 | { | |
436 | PyObject *pyo_frame_id; | |
437 | CORE_ADDR sp; | |
438 | CORE_ADDR pc; | |
439 | CORE_ADDR special; | |
440 | ||
441 | if (!PyArg_ParseTuple (args, "O:create_unwind_info", &pyo_frame_id)) | |
442 | return NULL; | |
443 | if (!pyuw_object_attribute_to_pointer (pyo_frame_id, "sp", &sp)) | |
444 | { | |
445 | PyErr_SetString (PyExc_ValueError, | |
446 | _("frame_id should have 'sp' attribute.")); | |
447 | return NULL; | |
448 | } | |
449 | ||
450 | /* The logic of building frame_id depending on the attributes of | |
451 | the frame_id object: | |
452 | Has Has Has Function to call | |
453 | 'sp'? 'pc'? 'special'? | |
454 | ------|------|--------------|------------------------- | |
455 | Y N * frame_id_build_wild (sp) | |
456 | Y Y N frame_id_build (sp, pc) | |
457 | Y Y Y frame_id_build_special (sp, pc, special) | |
458 | */ | |
459 | if (!pyuw_object_attribute_to_pointer (pyo_frame_id, "pc", &pc)) | |
460 | return pyuw_create_unwind_info (self, frame_id_build_wild (sp)); | |
461 | if (!pyuw_object_attribute_to_pointer (pyo_frame_id, "special", &special)) | |
462 | return pyuw_create_unwind_info (self, frame_id_build (sp, pc)); | |
463 | else | |
464 | return pyuw_create_unwind_info (self, | |
465 | frame_id_build_special (sp, pc, special)); | |
466 | } | |
467 | ||
d11916aa SS |
468 | /* frame_unwind.this_id method. */ |
469 | ||
470 | static void | |
471 | pyuw_this_id (struct frame_info *this_frame, void **cache_ptr, | |
472 | struct frame_id *this_id) | |
473 | { | |
474 | *this_id = ((cached_frame_info *) *cache_ptr)->frame_id; | |
475 | if (pyuw_debug >= 1) | |
476 | { | |
477 | fprintf_unfiltered (gdb_stdlog, "%s: frame_id: ", __FUNCTION__); | |
478 | fprint_frame_id (gdb_stdlog, *this_id); | |
479 | fprintf_unfiltered (gdb_stdlog, "\n"); | |
480 | } | |
481 | } | |
482 | ||
483 | /* frame_unwind.prev_register. */ | |
484 | ||
485 | static struct value * | |
486 | pyuw_prev_register (struct frame_info *this_frame, void **cache_ptr, | |
487 | int regnum) | |
488 | { | |
19ba03f4 | 489 | cached_frame_info *cached_frame = (cached_frame_info *) *cache_ptr; |
d11916aa SS |
490 | struct reg_info *reg_info = cached_frame->reg; |
491 | struct reg_info *reg_info_end = reg_info + cached_frame->reg_count; | |
492 | ||
493 | TRACE_PY_UNWIND (1, "%s (frame=%p,...,reg=%d)\n", __FUNCTION__, this_frame, | |
494 | regnum); | |
495 | for (; reg_info < reg_info_end; ++reg_info) | |
496 | { | |
497 | if (regnum == reg_info->number) | |
498 | return frame_unwind_got_bytes (this_frame, regnum, reg_info->data); | |
499 | } | |
500 | ||
501 | return frame_unwind_got_optimized (this_frame, regnum); | |
502 | } | |
503 | ||
504 | /* Frame sniffer dispatch. */ | |
505 | ||
506 | static int | |
507 | pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame, | |
508 | void **cache_ptr) | |
509 | { | |
510 | struct gdbarch *gdbarch = (struct gdbarch *) (self->unwind_data); | |
d11916aa SS |
511 | cached_frame_info *cached_frame; |
512 | ||
c0171de6 TT |
513 | gdbpy_enter enter_py (gdbarch, current_language); |
514 | ||
d11916aa SS |
515 | TRACE_PY_UNWIND (3, "%s (SP=%s, PC=%s)\n", __FUNCTION__, |
516 | paddress (gdbarch, get_frame_sp (this_frame)), | |
517 | paddress (gdbarch, get_frame_pc (this_frame))); | |
518 | ||
519 | /* Create PendingFrame instance to pass to sniffers. */ | |
c0171de6 TT |
520 | pending_frame_object *pfo = PyObject_New (pending_frame_object, |
521 | &pending_frame_object_type); | |
522 | gdbpy_ref pyo_pending_frame ((PyObject *) pfo); | |
d11916aa | 523 | if (pyo_pending_frame == NULL) |
c0171de6 TT |
524 | { |
525 | gdbpy_print_stack (); | |
526 | return 0; | |
527 | } | |
528 | pfo->gdbarch = gdbarch; | |
529 | scoped_restore invalidate_frame = make_scoped_restore (&pfo->frame_info, | |
530 | this_frame); | |
d11916aa SS |
531 | |
532 | /* Run unwinders. */ | |
533 | if (gdb_python_module == NULL | |
534 | || ! PyObject_HasAttrString (gdb_python_module, "execute_unwinders")) | |
535 | { | |
536 | PyErr_SetString (PyExc_NameError, | |
537 | "Installation error: gdb.execute_unwinders function " | |
538 | "is missing"); | |
c0171de6 TT |
539 | gdbpy_print_stack (); |
540 | return 0; | |
d11916aa | 541 | } |
c0171de6 TT |
542 | gdbpy_ref pyo_execute (PyObject_GetAttrString (gdb_python_module, |
543 | "execute_unwinders")); | |
d11916aa | 544 | if (pyo_execute == NULL) |
c0171de6 TT |
545 | { |
546 | gdbpy_print_stack (); | |
547 | return 0; | |
548 | } | |
549 | ||
550 | gdbpy_ref pyo_unwind_info | |
551 | (PyObject_CallFunctionObjArgs (pyo_execute.get (), | |
552 | pyo_pending_frame.get (), NULL)); | |
d11916aa | 553 | if (pyo_unwind_info == NULL) |
c0171de6 TT |
554 | { |
555 | gdbpy_print_stack (); | |
556 | return 0; | |
557 | } | |
d11916aa | 558 | if (pyo_unwind_info == Py_None) |
c0171de6 | 559 | return 0; |
d11916aa SS |
560 | |
561 | /* Received UnwindInfo, cache data. */ | |
c0171de6 | 562 | if (PyObject_IsInstance (pyo_unwind_info.get (), |
d11916aa SS |
563 | (PyObject *) &unwind_info_object_type) <= 0) |
564 | error (_("A Unwinder should return gdb.UnwindInfo instance.")); | |
565 | ||
566 | { | |
c0171de6 TT |
567 | unwind_info_object *unwind_info = |
568 | (unwind_info_object *) pyo_unwind_info.get (); | |
d11916aa SS |
569 | int reg_count = VEC_length (saved_reg, unwind_info->saved_regs); |
570 | saved_reg *reg; | |
571 | int i; | |
572 | ||
224c3ddb SM |
573 | cached_frame |
574 | = ((cached_frame_info *) | |
575 | xmalloc (sizeof (*cached_frame) | |
576 | + reg_count * sizeof (cached_frame->reg[0]))); | |
d11916aa SS |
577 | cached_frame->gdbarch = gdbarch; |
578 | cached_frame->frame_id = unwind_info->frame_id; | |
579 | cached_frame->reg_count = reg_count; | |
580 | ||
581 | /* Populate registers array. */ | |
582 | for (i = 0; VEC_iterate (saved_reg, unwind_info->saved_regs, i, reg); i++) | |
583 | { | |
584 | struct value *value = value_object_to_value (reg->value); | |
585 | size_t data_size = register_size (gdbarch, reg->number); | |
586 | ||
587 | cached_frame->reg[i].number = reg->number; | |
588 | ||
589 | /* `value' validation was done before, just assert. */ | |
590 | gdb_assert (value != NULL); | |
591 | gdb_assert (data_size == TYPE_LENGTH (value_type (value))); | |
592 | gdb_assert (data_size <= MAX_REGISTER_SIZE); | |
593 | ||
594 | memcpy (cached_frame->reg[i].data, value_contents (value), data_size); | |
595 | } | |
596 | } | |
597 | ||
598 | *cache_ptr = cached_frame; | |
d11916aa | 599 | return 1; |
d11916aa SS |
600 | } |
601 | ||
602 | /* Frame cache release shim. */ | |
603 | ||
604 | static void | |
605 | pyuw_dealloc_cache (struct frame_info *this_frame, void *cache) | |
606 | { | |
607 | TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__); | |
608 | xfree (cache); | |
609 | } | |
610 | ||
611 | struct pyuw_gdbarch_data_type | |
612 | { | |
613 | /* Has the unwinder shim been prepended? */ | |
614 | int unwinder_registered; | |
615 | }; | |
616 | ||
617 | static void * | |
618 | pyuw_gdbarch_data_init (struct gdbarch *gdbarch) | |
619 | { | |
620 | return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct pyuw_gdbarch_data_type); | |
621 | } | |
622 | ||
623 | /* New inferior architecture callback: register the Python unwinders | |
624 | intermediary. */ | |
625 | ||
626 | static void | |
627 | pyuw_on_new_gdbarch (struct gdbarch *newarch) | |
628 | { | |
19ba03f4 SM |
629 | struct pyuw_gdbarch_data_type *data |
630 | = (struct pyuw_gdbarch_data_type *) gdbarch_data (newarch, | |
631 | pyuw_gdbarch_data); | |
d11916aa SS |
632 | |
633 | if (!data->unwinder_registered) | |
634 | { | |
635 | struct frame_unwind *unwinder | |
636 | = GDBARCH_OBSTACK_ZALLOC (newarch, struct frame_unwind); | |
637 | ||
638 | unwinder->type = NORMAL_FRAME; | |
639 | unwinder->stop_reason = default_frame_unwind_stop_reason; | |
640 | unwinder->this_id = pyuw_this_id; | |
641 | unwinder->prev_register = pyuw_prev_register; | |
19ba03f4 | 642 | unwinder->unwind_data = (const struct frame_data *) newarch; |
d11916aa SS |
643 | unwinder->sniffer = pyuw_sniffer; |
644 | unwinder->dealloc_cache = pyuw_dealloc_cache; | |
645 | frame_unwind_prepend_unwinder (newarch, unwinder); | |
646 | data->unwinder_registered = 1; | |
647 | } | |
648 | } | |
649 | ||
650 | /* Initialize unwind machinery. */ | |
651 | ||
652 | int | |
653 | gdbpy_initialize_unwind (void) | |
654 | { | |
655 | int rc; | |
656 | add_setshow_zuinteger_cmd | |
657 | ("py-unwind", class_maintenance, &pyuw_debug, | |
658 | _("Set Python unwinder debugging."), | |
659 | _("Show Python unwinder debugging."), | |
660 | _("When non-zero, Python unwinder debugging is enabled."), | |
661 | NULL, | |
662 | NULL, | |
663 | &setdebuglist, &showdebuglist); | |
664 | pyuw_gdbarch_data | |
665 | = gdbarch_data_register_post_init (pyuw_gdbarch_data_init); | |
666 | observer_attach_architecture_changed (pyuw_on_new_gdbarch); | |
667 | ||
668 | if (PyType_Ready (&pending_frame_object_type) < 0) | |
669 | return -1; | |
670 | rc = gdb_pymodule_addobject (gdb_module, "PendingFrame", | |
671 | (PyObject *) &pending_frame_object_type); | |
672 | if (rc) | |
673 | return rc; | |
674 | ||
675 | if (PyType_Ready (&unwind_info_object_type) < 0) | |
676 | return -1; | |
677 | return gdb_pymodule_addobject (gdb_module, "UnwindInfo", | |
678 | (PyObject *) &unwind_info_object_type); | |
679 | } | |
680 | ||
681 | static PyMethodDef pending_frame_object_methods[] = | |
682 | { | |
683 | { "read_register", pending_framepy_read_register, METH_VARARGS, | |
684 | "read_register (REG) -> gdb.Value\n" | |
685 | "Return the value of the REG in the frame." }, | |
686 | { "create_unwind_info", | |
687 | pending_framepy_create_unwind_info, METH_VARARGS, | |
688 | "create_unwind_info (FRAME_ID) -> gdb.UnwindInfo\n" | |
689 | "Construct UnwindInfo for this PendingFrame, using FRAME_ID\n" | |
690 | "to identify it." }, | |
691 | {NULL} /* Sentinel */ | |
692 | }; | |
693 | ||
13fa0398 | 694 | PyTypeObject pending_frame_object_type = |
d11916aa SS |
695 | { |
696 | PyVarObject_HEAD_INIT (NULL, 0) | |
697 | "gdb.PendingFrame", /* tp_name */ | |
698 | sizeof (pending_frame_object), /* tp_basicsize */ | |
699 | 0, /* tp_itemsize */ | |
700 | 0, /* tp_dealloc */ | |
701 | 0, /* tp_print */ | |
702 | 0, /* tp_getattr */ | |
703 | 0, /* tp_setattr */ | |
704 | 0, /* tp_compare */ | |
705 | 0, /* tp_repr */ | |
706 | 0, /* tp_as_number */ | |
707 | 0, /* tp_as_sequence */ | |
708 | 0, /* tp_as_mapping */ | |
709 | 0, /* tp_hash */ | |
710 | 0, /* tp_call */ | |
711 | pending_framepy_str, /* tp_str */ | |
712 | 0, /* tp_getattro */ | |
713 | 0, /* tp_setattro */ | |
714 | 0, /* tp_as_buffer */ | |
715 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
716 | "GDB PendingFrame object", /* tp_doc */ | |
717 | 0, /* tp_traverse */ | |
718 | 0, /* tp_clear */ | |
719 | 0, /* tp_richcompare */ | |
720 | 0, /* tp_weaklistoffset */ | |
721 | 0, /* tp_iter */ | |
722 | 0, /* tp_iternext */ | |
723 | pending_frame_object_methods, /* tp_methods */ | |
724 | 0, /* tp_members */ | |
725 | 0, /* tp_getset */ | |
726 | 0, /* tp_base */ | |
727 | 0, /* tp_dict */ | |
728 | 0, /* tp_descr_get */ | |
729 | 0, /* tp_descr_set */ | |
730 | 0, /* tp_dictoffset */ | |
731 | 0, /* tp_init */ | |
732 | 0, /* tp_alloc */ | |
733 | }; | |
734 | ||
735 | static PyMethodDef unwind_info_object_methods[] = | |
736 | { | |
737 | { "add_saved_register", | |
738 | unwind_infopy_add_saved_register, METH_VARARGS, | |
739 | "add_saved_register (REG, VALUE) -> None\n" | |
740 | "Set the value of the REG in the previous frame to VALUE." }, | |
741 | { NULL } /* Sentinel */ | |
742 | }; | |
743 | ||
13fa0398 | 744 | PyTypeObject unwind_info_object_type = |
d11916aa SS |
745 | { |
746 | PyVarObject_HEAD_INIT (NULL, 0) | |
747 | "gdb.UnwindInfo", /* tp_name */ | |
748 | sizeof (unwind_info_object), /* tp_basicsize */ | |
749 | 0, /* tp_itemsize */ | |
750 | unwind_infopy_dealloc, /* tp_dealloc */ | |
751 | 0, /* tp_print */ | |
752 | 0, /* tp_getattr */ | |
753 | 0, /* tp_setattr */ | |
754 | 0, /* tp_compare */ | |
755 | 0, /* tp_repr */ | |
756 | 0, /* tp_as_number */ | |
757 | 0, /* tp_as_sequence */ | |
758 | 0, /* tp_as_mapping */ | |
759 | 0, /* tp_hash */ | |
760 | 0, /* tp_call */ | |
761 | unwind_infopy_str, /* tp_str */ | |
762 | 0, /* tp_getattro */ | |
763 | 0, /* tp_setattro */ | |
764 | 0, /* tp_as_buffer */ | |
765 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ | |
766 | "GDB UnwindInfo object", /* tp_doc */ | |
767 | 0, /* tp_traverse */ | |
768 | 0, /* tp_clear */ | |
769 | 0, /* tp_richcompare */ | |
770 | 0, /* tp_weaklistoffset */ | |
771 | 0, /* tp_iter */ | |
772 | 0, /* tp_iternext */ | |
773 | unwind_info_object_methods, /* tp_methods */ | |
774 | 0, /* tp_members */ | |
775 | 0, /* tp_getset */ | |
776 | 0, /* tp_base */ | |
777 | 0, /* tp_dict */ | |
778 | 0, /* tp_descr_get */ | |
779 | 0, /* tp_descr_set */ | |
780 | 0, /* tp_dictoffset */ | |
781 | 0, /* tp_init */ | |
782 | 0, /* tp_alloc */ | |
783 | }; |