]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/py-framefilter.py
Automatic Copyright Year update after running gdb/copyright.py
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-framefilter.py
CommitLineData
4a94e368 1# Copyright (C) 2013-2022 Free Software Foundation, Inc.
1e611234
PM
2
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.
7#
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.
12#
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/>.
15
16# This file is part of the GDB testsuite. It tests Python-based
17# frame-filters.
18import gdb
19import itertools
20from gdb.FrameDecorator import FrameDecorator
21import copy
22
1e611234 23
13123da8 24class Reverse_Function(FrameDecorator):
1e611234
PM
25 def __init__(self, fobj):
26 super(Reverse_Function, self).__init__(fobj)
27 self.fobj = fobj
28
13123da8
SM
29 def function(self):
30 fname = str(self.fobj.function())
f9e59d06 31 if not fname:
1e611234 32 return None
13123da8
SM
33 if fname == "end_func":
34 extra = self.fobj.inferior_frame().read_var("str").string()
1e611234 35 else:
13123da8 36 extra = ""
21909fa1 37 fname = fname[::-1] + extra
1e611234
PM
38 return fname
39
1e611234 40
13123da8 41class Dummy(FrameDecorator):
1e611234
PM
42 def __init__(self, fobj):
43 super(Dummy, self).__init__(fobj)
44 self.fobj = fobj
45
13123da8 46 def function(self):
1e611234
PM
47 return "Dummy function"
48
13123da8 49 def address(self):
1e611234
PM
50 return 0x123
51
13123da8 52 def filename(self):
1e611234
PM
53 return "Dummy filename"
54
13123da8
SM
55 def frame_args(self):
56 return [("Foo", gdb.Value(12)), ("Bar", "Stuff"), ("FooBar", 42)]
1e611234 57
13123da8 58 def frame_locals(self):
1e611234
PM
59 return []
60
13123da8 61 def line(self):
1e611234
PM
62 return 0
63
13123da8 64 def elided(self):
1e611234
PM
65 return None
66
1e611234 67
13123da8
SM
68class FrameFilter:
69 def __init__(self):
1e611234
PM
70 self.name = "Reverse"
71 self.priority = 100
72 self.enabled = True
13123da8 73 gdb.frame_filters[self.name] = self
1e611234 74
13123da8 75 def filter(self, frame_iter):
8f28f522
PM
76 # Python 3.x moved the itertools.imap functionality to map(),
77 # so check if it is available.
78 if hasattr(itertools, "imap"):
13123da8 79 frame_iter = itertools.imap(Reverse_Function, frame_iter)
8f28f522
PM
80 else:
81 frame_iter = map(Reverse_Function, frame_iter)
82
1e611234
PM
83 return frame_iter
84
1e611234 85
13123da8 86class ElidingFrameDecorator(FrameDecorator):
1e611234
PM
87 def __init__(self, frame, elided_frames):
88 super(ElidingFrameDecorator, self).__init__(frame)
89 self.elided_frames = elided_frames
90
91 def elided(self):
92 return iter(self.elided_frames)
93
13123da8 94 def address(self):
30a7bb83 95 # Regression test for an overflow in the python layer.
13123da8 96 bitsize = 8 * gdb.lookup_type("void").pointer().sizeof
30a7bb83 97 mask = (1 << bitsize) - 1
13123da8
SM
98 return 0xFFFFFFFFFFFFFFFF & mask
99
30a7bb83 100
1e611234
PM
101class ElidingIterator:
102 def __init__(self, ii):
103 self.input_iterator = ii
104
105 def __iter__(self):
106 return self
107
108 def next(self):
109 frame = next(self.input_iterator)
13123da8 110 if str(frame.function()) != "func1":
1e611234
PM
111 return frame
112
113 # Suppose we want to return the 'func1' frame but elide the
114 # next frame. E.g., if call in our interpreter language takes
115 # two C frames to implement, and the first one we see is the
116 # "sentinel".
117 elided = next(self.input_iterator)
118 return ElidingFrameDecorator(frame, [elided])
119
8f28f522
PM
120 # Python 3.x requires __next__(self) while Python 2.x requires
121 # next(self). Define next(self), and for Python 3.x create this
122 # wrapper.
123 def __next__(self):
124 return self.next()
125
1e611234 126
13123da8
SM
127class FrameElider:
128 def __init__(self):
1e611234
PM
129 self.name = "Elider"
130 self.priority = 900
131 self.enabled = True
13123da8
SM
132 gdb.frame_filters[self.name] = self
133
134 def filter(self, frame_iter):
135 return ElidingIterator(frame_iter)
1e611234 136
1e611234 137
4ca59a9f
TT
138# This is here so the test can change the kind of error that is
139# thrown.
140name_error = RuntimeError
141
0740f8d8
TT
142# A simple decorator that gives an error when computing the function.
143class ErrorInName(FrameDecorator):
144 def __init__(self, frame):
145 FrameDecorator.__init__(self, frame)
146
147 def function(self):
13123da8
SM
148 raise name_error("whoops")
149
0740f8d8
TT
150
151# A filter that supplies buggy frames. Disabled by default.
13123da8
SM
152class ErrorFilter:
153 def __init__(self):
0740f8d8
TT
154 self.name = "Error"
155 self.priority = 1
156 self.enabled = False
13123da8 157 gdb.frame_filters[self.name] = self
0740f8d8
TT
158
159 def filter(self, frame_iter):
40d2f8d6
SM
160 # Python 3.x moved the itertools.imap functionality to map(),
161 # so check if it is available.
162 if hasattr(itertools, "imap"):
13123da8 163 return itertools.imap(ErrorInName, frame_iter)
40d2f8d6
SM
164 else:
165 return map(ErrorInName, frame_iter)
0740f8d8 166
13123da8 167
1e611234
PM
168FrameFilter()
169FrameElider()
0740f8d8 170ErrorFilter()