]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-unwind.py
1 # Copyright (C) 2015-2020 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from gdb
.unwinder
import Unwinder
19 class FrameId(object):
21 def __init__(self
, sp
, pc
):
33 class TestUnwinder(Unwinder
):
39 Unwinder
.__init
__(self
, "test unwinder")
40 self
.char_ptr_t
= gdb
.lookup_type("unsigned char").pointer()
41 self
.char_ptr_ptr_t
= self
.char_ptr_t
.pointer()
43 def _read_word(self
, address
):
44 return address
.cast(self
.char_ptr_ptr_t
).dereference()
46 def __call__(self
, pending_frame
):
47 """Test unwinder written in Python.
49 This unwinder can unwind the frames that have been deliberately
50 corrupted in a specific way (functions in the accompanying
51 py-unwind.c file do that.)
52 This code is only on AMD64.
53 On AMD64 $RBP points to the innermost frame (unless the code
54 was compiled with -fomit-frame-pointer), which contains the
55 address of the previous frame at offset 0. The functions
56 deliberately corrupt their frames as follows:
58 Corruption: Corruption:
59 +--------------+ +--------------+
60 RBP-8 | | | Previous RBP |
61 +--------------+ +--------------+
62 RBP + Previous RBP | | RBP |
63 +--------------+ +--------------+
64 RBP+8 | Return RIP | | Return RIP |
65 +--------------+ +--------------+
68 This unwinder recognizes the corrupt frames by checking that
69 *RBP == RBP, and restores previous RBP from the word above it.
72 # Check that we can access the architecture of the pending
73 # frame, and that this is the same architecture as for the
74 # currently selected inferior.
75 inf_arch
= gdb
.selected_inferior ().architecture ()
76 frame_arch
= pending_frame
.architecture ()
77 if (inf_arch
!= frame_arch
):
78 raise gdb
.GdbError ("architecture mismatch")
81 # NOTE: the registers in Unwinder API can be referenced
82 # either by name or by number. The code below uses both
83 # to achieve more coverage.
84 bp
= pending_frame
.read_register("rbp").cast(self
.char_ptr_t
)
85 if self
._read
_word
(bp
) != bp
:
87 # Found the frame that the test program has corrupted for us.
88 # The correct BP for the outer frame has been saved one word
89 # above, previous IP and SP are at the expected places.
90 previous_bp
= self
._read
_word
(bp
- 8)
91 previous_ip
= self
._read
_word
(bp
+ 8)
95 pending_frame
.read_register(TestUnwinder
.AMD64_RSP
),
96 pending_frame
.read_register(TestUnwinder
.AMD64_RIP
))
97 unwind_info
= pending_frame
.create_unwind_info(frame_id
)
98 unwind_info
.add_saved_register(TestUnwinder
.AMD64_RBP
,
100 unwind_info
.add_saved_register("rip", previous_ip
)
101 unwind_info
.add_saved_register("rsp", previous_sp
)
103 except (gdb
.error
, RuntimeError):
106 gdb
.unwinder
.register_unwinder(None, TestUnwinder(), True)
107 print("Python script imported")