]> Git Repo - binutils.git/blob - gdb/python/lib/gdb/function/caller_is.py
922e2f4b6aa4287b69611bb9fad632d6116e1441
[binutils.git] / gdb / python / lib / gdb / function / caller_is.py
1 # Caller-is functions.
2 # Copyright (C) 2008-2021 Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 import gdb
18 import re
19
20
21 class CallerIs(gdb.Function):
22     """Check the calling function's name.
23
24     Usage: $_caller_is (NAME [, NUMBER-OF-FRAMES])
25
26     Arguments:
27
28       NAME: The name of the function to search for.
29
30       NUMBER-OF-FRAMES: How many stack frames to traverse back from the currently
31         selected frame to compare with.  If the value is greater than the depth of
32         the stack from that point then the result is False.
33         The default is 1.
34
35     Returns:
36       True if the function's name at the specified frame is equal to NAME."""
37
38     def __init__(self):
39         super(CallerIs, self).__init__("_caller_is")
40
41     def invoke(self, name, nframes=1):
42         if nframes < 0:
43             raise ValueError("nframes must be >= 0")
44         frame = gdb.selected_frame()
45         while nframes > 0:
46             frame = frame.older()
47             if frame is None:
48                 return False
49             nframes = nframes - 1
50         return frame.name() == name.string()
51
52
53 class CallerMatches(gdb.Function):
54     """Compare the calling function's name with a regexp.
55
56     Usage: $_caller_matches (REGEX [, NUMBER-OF-FRAMES])
57
58     Arguments:
59
60       REGEX: The regular expression to compare the function's name with.
61
62       NUMBER-OF-FRAMES: How many stack frames to traverse back from the currently
63         selected frame to compare with.  If the value is greater than the depth of
64         the stack from that point then the result is False.
65         The default is 1.
66
67     Returns:
68       True if the function's name at the specified frame matches REGEX."""
69
70     def __init__(self):
71         super(CallerMatches, self).__init__("_caller_matches")
72
73     def invoke(self, name, nframes=1):
74         if nframes < 0:
75             raise ValueError("nframes must be >= 0")
76         frame = gdb.selected_frame()
77         while nframes > 0:
78             frame = frame.older()
79             if frame is None:
80                 return False
81             nframes = nframes - 1
82         return re.match(name.string(), frame.name()) is not None
83
84
85 class AnyCallerIs(gdb.Function):
86     """Check all calling function's names.
87
88     Usage: $_any_caller_is (NAME [, NUMBER-OF-FRAMES])
89
90     Arguments:
91
92       NAME: The name of the function to search for.
93
94       NUMBER-OF-FRAMES: How many stack frames to traverse back from the currently
95         selected frame to compare with.  If the value is greater than the depth of
96         the stack from that point then the result is False.
97         The default is 1.
98
99     Returns:
100       True if any function's name is equal to NAME."""
101
102     def __init__(self):
103         super(AnyCallerIs, self).__init__("_any_caller_is")
104
105     def invoke(self, name, nframes=1):
106         if nframes < 0:
107             raise ValueError("nframes must be >= 0")
108         frame = gdb.selected_frame()
109         while nframes >= 0:
110             if frame.name() == name.string():
111                 return True
112             frame = frame.older()
113             if frame is None:
114                 return False
115             nframes = nframes - 1
116         return False
117
118
119 class AnyCallerMatches(gdb.Function):
120     """Compare all calling function's names with a regexp.
121
122     Usage: $_any_caller_matches (REGEX [, NUMBER-OF-FRAMES])
123
124     Arguments:
125
126       REGEX: The regular expression to compare the function's name with.
127
128       NUMBER-OF-FRAMES: How many stack frames to traverse back from the currently
129         selected frame to compare with.  If the value is greater than the depth of
130         the stack from that point then the result is False.
131         The default is 1.
132
133     Returns:
134       True if any function's name matches REGEX."""
135
136     def __init__(self):
137         super(AnyCallerMatches, self).__init__("_any_caller_matches")
138
139     def invoke(self, name, nframes=1):
140         if nframes < 0:
141             raise ValueError("nframes must be >= 0")
142         frame = gdb.selected_frame()
143         name_re = re.compile(name.string())
144         while nframes >= 0:
145             if name_re.match(frame.name()) is not None:
146                 return True
147             frame = frame.older()
148             if frame is None:
149                 return False
150             nframes = nframes - 1
151         return False
152
153
154 CallerIs()
155 CallerMatches()
156 AnyCallerIs()
157 AnyCallerMatches()
This page took 0.025103 seconds and 2 git commands to generate.