Update copyright year range in all GDB files.
[deliverable/binutils-gdb.git] / gdb / python / lib / gdb / function / caller_is.py
1 # Caller-is functions.
2 # Copyright (C) 2008-2019 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 class CallerIs(gdb.Function):
21 """Check the calling function's name.
22
23 Usage: $_caller_is (NAME [, NUMBER-OF-FRAMES])
24
25 Arguments:
26
27 NAME: The name of the function to search for.
28
29 NUMBER-OF-FRAMES: How many stack frames to traverse back from the currently
30 selected frame to compare with. If the value is greater than the depth of
31 the stack from that point then the result is False.
32 The default is 1.
33
34 Returns:
35 True if the function's name at the specified frame is equal to NAME."""
36
37 def __init__(self):
38 super(CallerIs, self).__init__("_caller_is")
39
40 def invoke(self, name, nframes = 1):
41 if nframes < 0:
42 raise ValueError("nframes must be >= 0")
43 frame = gdb.selected_frame()
44 while nframes > 0:
45 frame = frame.older()
46 if frame is None:
47 return False
48 nframes = nframes - 1
49 return frame.name() == name.string()
50
51 class CallerMatches(gdb.Function):
52 """Compare the calling function's name with a regexp.
53
54 Usage: $_caller_matches (REGEX [, NUMBER-OF-FRAMES])
55
56 Arguments:
57
58 REGEX: The regular expression to compare the function's name with.
59
60 NUMBER-OF-FRAMES: How many stack frames to traverse back from the currently
61 selected frame to compare with. If the value is greater than the depth of
62 the stack from that point then the result is False.
63 The default is 1.
64
65 Returns:
66 True if the function's name at the specified frame matches REGEX."""
67
68 def __init__(self):
69 super(CallerMatches, self).__init__("_caller_matches")
70
71 def invoke(self, name, nframes = 1):
72 if nframes < 0:
73 raise ValueError("nframes must be >= 0")
74 frame = gdb.selected_frame()
75 while nframes > 0:
76 frame = frame.older()
77 if frame is None:
78 return False
79 nframes = nframes - 1
80 return re.match(name.string(), frame.name()) is not None
81
82 class AnyCallerIs(gdb.Function):
83 """Check all calling function's names.
84
85 Usage: $_any_caller_is (NAME [, NUMBER-OF-FRAMES])
86
87 Arguments:
88
89 NAME: The name of the function to search for.
90
91 NUMBER-OF-FRAMES: How many stack frames to traverse back from the currently
92 selected frame to compare with. If the value is greater than the depth of
93 the stack from that point then the result is False.
94 The default is 1.
95
96 Returns:
97 True if any function's name is equal to NAME."""
98
99 def __init__(self):
100 super(AnyCallerIs, self).__init__("_any_caller_is")
101
102 def invoke(self, name, nframes = 1):
103 if nframes < 0:
104 raise ValueError("nframes must be >= 0")
105 frame = gdb.selected_frame()
106 while nframes >= 0:
107 if frame.name() == name.string():
108 return True
109 frame = frame.older()
110 if frame is None:
111 return False
112 nframes = nframes - 1
113 return False
114
115 class AnyCallerMatches(gdb.Function):
116 """Compare all calling function's names with a regexp.
117
118 Usage: $_any_caller_matches (REGEX [, NUMBER-OF-FRAMES])
119
120 Arguments:
121
122 REGEX: The regular expression to compare the function's name with.
123
124 NUMBER-OF-FRAMES: How many stack frames to traverse back from the currently
125 selected frame to compare with. If the value is greater than the depth of
126 the stack from that point then the result is False.
127 The default is 1.
128
129 Returns:
130 True if any function's name matches REGEX."""
131
132 def __init__(self):
133 super(AnyCallerMatches, self).__init__("_any_caller_matches")
134
135 def invoke(self, name, nframes = 1):
136 if nframes < 0:
137 raise ValueError("nframes must be >= 0")
138 frame = gdb.selected_frame()
139 name_re = re.compile(name.string())
140 while nframes >= 0:
141 if name_re.match(frame.name()) is not None:
142 return True
143 frame = frame.older()
144 if frame is None:
145 return False
146 nframes = nframes - 1
147 return False
148
149 CallerIs()
150 CallerMatches()
151 AnyCallerIs()
152 AnyCallerMatches()
This page took 0.050463 seconds and 4 git commands to generate.