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