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