Fix "maint print" error messages
[binutils-gdb.git] / gdb / testsuite / gdb.python / py-framefilter.py
blobfb679f18b4f22b3be1df3454d774908b8eabf078
1 # Copyright (C) 2013-2024 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/>.
16 import copy
17 import itertools
19 # This file is part of the GDB testsuite. It tests Python-based
20 # frame-filters.
21 import gdb
22 from gdb.FrameDecorator import FrameDecorator
25 class Reverse_Function(FrameDecorator):
26 def __init__(self, fobj):
27 super(Reverse_Function, self).__init__(fobj)
28 self.fobj = fobj
30 def function(self):
31 fname = str(self.fobj.function())
32 if not fname:
33 return None
34 if fname == "end_func":
35 extra = self.fobj.inferior_frame().read_var("str").string()
36 else:
37 extra = ""
38 fname = fname[::-1] + extra
39 return fname
42 class Dummy(FrameDecorator):
43 def __init__(self, fobj):
44 super(Dummy, self).__init__(fobj)
45 self.fobj = fobj
47 def function(self):
48 return "Dummy function"
50 def address(self):
51 return 0x123
53 def filename(self):
54 return "Dummy filename"
56 def frame_args(self):
57 return [("Foo", gdb.Value(12)), ("Bar", "Stuff"), ("FooBar", 42)]
59 def frame_locals(self):
60 return []
62 def line(self):
63 return 0
65 def elided(self):
66 return None
69 class FrameFilter:
70 def __init__(self):
71 self.name = "Reverse"
72 self.priority = 100
73 self.enabled = True
74 gdb.frame_filters[self.name] = self
76 def filter(self, frame_iter):
77 # Python 3.x moved the itertools.imap functionality to map(),
78 # so check if it is available.
79 if hasattr(itertools, "imap"):
80 frame_iter = itertools.imap(Reverse_Function, frame_iter)
81 else:
82 frame_iter = map(Reverse_Function, frame_iter)
84 return frame_iter
87 class ElidingFrameDecorator(FrameDecorator):
88 def __init__(self, frame, elided_frames):
89 super(ElidingFrameDecorator, self).__init__(frame)
90 self.elided_frames = elided_frames
92 def elided(self):
93 return iter(self.elided_frames)
95 def address(self):
96 # Regression test for an overflow in the python layer.
97 bitsize = 8 * gdb.lookup_type("void").pointer().sizeof
98 mask = (1 << bitsize) - 1
99 return 0xFFFFFFFFFFFFFFFF & mask
102 class ElidingIterator:
103 def __init__(self, ii):
104 self.input_iterator = ii
106 def __iter__(self):
107 return self
109 def next(self):
110 frame = next(self.input_iterator)
111 if str(frame.function()) != "func1":
112 return frame
114 # Suppose we want to return the 'func1' frame but elide the
115 # next frame. E.g., if call in our interpreter language takes
116 # two C frames to implement, and the first one we see is the
117 # "sentinel".
118 elided = next(self.input_iterator)
119 return ElidingFrameDecorator(frame, [elided])
121 # Python 3.x requires __next__(self) while Python 2.x requires
122 # next(self). Define next(self), and for Python 3.x create this
123 # wrapper.
124 def __next__(self):
125 return self.next()
128 class FrameElider:
129 def __init__(self):
130 self.name = "Elider"
131 self.priority = 900
132 self.enabled = True
133 gdb.frame_filters[self.name] = self
135 def filter(self, frame_iter):
136 return ElidingIterator(frame_iter)
139 # This is here so the test can change the kind of error that is
140 # thrown.
141 name_error = RuntimeError
144 # A simple decorator that gives an error when computing the function.
145 class ErrorInName(FrameDecorator):
146 def __init__(self, frame):
147 FrameDecorator.__init__(self, frame)
149 def function(self):
150 raise name_error("whoops")
153 # A filter that supplies buggy frames. Disabled by default.
154 class ErrorFilter:
155 def __init__(self):
156 self.name = "Error"
157 self.priority = 1
158 self.enabled = False
159 gdb.frame_filters[self.name] = self
161 def filter(self, frame_iter):
162 # Python 3.x moved the itertools.imap functionality to map(),
163 # so check if it is available.
164 if hasattr(itertools, "imap"):
165 return itertools.imap(ErrorInName, frame_iter)
166 else:
167 return map(ErrorInName, frame_iter)
170 FrameFilter()
171 FrameElider()
172 ErrorFilter()