Commit | Line | Data |
---|---|---|
ecd75fc8 | 1 | # Copyright (C) 2013-2014 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. | |
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 | else: | |
34 | fname = fname[::-1] | |
35 | return fname | |
36 | ||
37 | class Dummy (FrameDecorator): | |
38 | ||
39 | def __init__(self, fobj): | |
40 | super(Dummy, self).__init__(fobj) | |
41 | self.fobj = fobj | |
42 | ||
43 | def function (self): | |
44 | return "Dummy function" | |
45 | ||
46 | def address (self): | |
47 | return 0x123 | |
48 | ||
49 | def filename (self): | |
50 | return "Dummy filename" | |
51 | ||
52 | def frame_args (self): | |
53 | return [("Foo",gdb.Value(12)),("Bar","Stuff"), ("FooBar",42)] | |
54 | ||
55 | def frame_locals (self): | |
56 | return [] | |
57 | ||
58 | def line (self): | |
59 | return 0 | |
60 | ||
61 | def elided (self): | |
62 | return None | |
63 | ||
64 | class FrameFilter (): | |
65 | ||
66 | def __init__ (self): | |
67 | self.name = "Reverse" | |
68 | self.priority = 100 | |
69 | self.enabled = True | |
70 | gdb.frame_filters [self.name] = self | |
71 | ||
72 | def filter (self, frame_iter): | |
8f28f522 PM |
73 | # Python 3.x moved the itertools.imap functionality to map(), |
74 | # so check if it is available. | |
75 | if hasattr(itertools, "imap"): | |
76 | frame_iter = itertools.imap (Reverse_Function, | |
77 | frame_iter) | |
78 | else: | |
79 | frame_iter = map(Reverse_Function, frame_iter) | |
80 | ||
1e611234 PM |
81 | return frame_iter |
82 | ||
83 | class ElidingFrameDecorator(FrameDecorator): | |
84 | ||
85 | def __init__(self, frame, elided_frames): | |
86 | super(ElidingFrameDecorator, self).__init__(frame) | |
87 | self.elided_frames = elided_frames | |
88 | ||
89 | def elided(self): | |
90 | return iter(self.elided_frames) | |
91 | ||
92 | class ElidingIterator: | |
93 | def __init__(self, ii): | |
94 | self.input_iterator = ii | |
95 | ||
96 | def __iter__(self): | |
97 | return self | |
98 | ||
99 | def next(self): | |
100 | frame = next(self.input_iterator) | |
101 | if str(frame.function()) != 'func1': | |
102 | return frame | |
103 | ||
104 | # Suppose we want to return the 'func1' frame but elide the | |
105 | # next frame. E.g., if call in our interpreter language takes | |
106 | # two C frames to implement, and the first one we see is the | |
107 | # "sentinel". | |
108 | elided = next(self.input_iterator) | |
109 | return ElidingFrameDecorator(frame, [elided]) | |
110 | ||
8f28f522 PM |
111 | # Python 3.x requires __next__(self) while Python 2.x requires |
112 | # next(self). Define next(self), and for Python 3.x create this | |
113 | # wrapper. | |
114 | def __next__(self): | |
115 | return self.next() | |
116 | ||
1e611234 PM |
117 | class FrameElider (): |
118 | ||
119 | def __init__ (self): | |
120 | self.name = "Elider" | |
121 | self.priority = 900 | |
122 | self.enabled = True | |
123 | gdb.frame_filters [self.name] = self | |
124 | ||
125 | def filter (self, frame_iter): | |
126 | return ElidingIterator (frame_iter) | |
127 | ||
128 | FrameFilter() | |
129 | FrameElider() |