]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-framefilter.py
Update year range in copyright notice of all files owned by the GDB project.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-framefilter.py
1 # Copyright (C) 2013-2015 Free Software Foundation, Inc.
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.
18 import gdb
19 import itertools
20 from gdb.FrameDecorator import FrameDecorator
21 import copy
22
23 class Reverse_Function (FrameDecorator):
24
25 def __init__(self, fobj):
26 super(Reverse_Function, self).__init__(fobj)
27 self.fobj = fobj
28
29 def function (self):
30 fname = str (self.fobj.function())
31 if (fname == None or fname == ""):
32 return None
33 if fname == 'end_func':
34 extra = self.fobj.inferior_frame().read_var('str').string()
35 else:
36 extra = ''
37 fname = fname[::-1] + extra
38 return fname
39
40 class Dummy (FrameDecorator):
41
42 def __init__(self, fobj):
43 super(Dummy, self).__init__(fobj)
44 self.fobj = fobj
45
46 def function (self):
47 return "Dummy function"
48
49 def address (self):
50 return 0x123
51
52 def filename (self):
53 return "Dummy filename"
54
55 def frame_args (self):
56 return [("Foo",gdb.Value(12)),("Bar","Stuff"), ("FooBar",42)]
57
58 def frame_locals (self):
59 return []
60
61 def line (self):
62 return 0
63
64 def elided (self):
65 return None
66
67 class FrameFilter ():
68
69 def __init__ (self):
70 self.name = "Reverse"
71 self.priority = 100
72 self.enabled = True
73 gdb.frame_filters [self.name] = self
74
75 def filter (self, frame_iter):
76 # Python 3.x moved the itertools.imap functionality to map(),
77 # so check if it is available.
78 if hasattr(itertools, "imap"):
79 frame_iter = itertools.imap (Reverse_Function,
80 frame_iter)
81 else:
82 frame_iter = map(Reverse_Function, frame_iter)
83
84 return frame_iter
85
86 class ElidingFrameDecorator(FrameDecorator):
87
88 def __init__(self, frame, elided_frames):
89 super(ElidingFrameDecorator, self).__init__(frame)
90 self.elided_frames = elided_frames
91
92 def elided(self):
93 return iter(self.elided_frames)
94
95 class ElidingIterator:
96 def __init__(self, ii):
97 self.input_iterator = ii
98
99 def __iter__(self):
100 return self
101
102 def next(self):
103 frame = next(self.input_iterator)
104 if str(frame.function()) != 'func1':
105 return frame
106
107 # Suppose we want to return the 'func1' frame but elide the
108 # next frame. E.g., if call in our interpreter language takes
109 # two C frames to implement, and the first one we see is the
110 # "sentinel".
111 elided = next(self.input_iterator)
112 return ElidingFrameDecorator(frame, [elided])
113
114 # Python 3.x requires __next__(self) while Python 2.x requires
115 # next(self). Define next(self), and for Python 3.x create this
116 # wrapper.
117 def __next__(self):
118 return self.next()
119
120 class FrameElider ():
121
122 def __init__ (self):
123 self.name = "Elider"
124 self.priority = 900
125 self.enabled = True
126 gdb.frame_filters [self.name] = self
127
128 def filter (self, frame_iter):
129 return ElidingIterator (frame_iter)
130
131 # A simple decorator that gives an error when computing the function.
132 class ErrorInName(FrameDecorator):
133 def __init__(self, frame):
134 FrameDecorator.__init__(self, frame)
135
136 def function(self):
137 raise RuntimeError('whoops')
138
139 # A filter that supplies buggy frames. Disabled by default.
140 class ErrorFilter():
141 def __init__ (self):
142 self.name = "Error"
143 self.priority = 1
144 self.enabled = False
145 gdb.frame_filters [self.name] = self
146
147 def filter(self, frame_iter):
148 return itertools.imap(ErrorInName, frame_iter)
149
150 FrameFilter()
151 FrameElider()
152 ErrorFilter()