summaryrefslogtreecommitdiff
path: root/share/gdb/python/gdb/function/caller_is.py
blob: 5b8919171f7d1022ee6d63e5caf7ea4e68fdea69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Caller-is functions.
# Copyright (C) 2008-2016 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gdb
import re

class CallerIs(gdb.Function):
    """Check the calling function's name.

Usage:
  $_caller_is(name [, number_of_frames])

Arguments:

  name: The name of the function to search for.

  number_of_frames: How many stack frames to traverse back from the currently
    selected frame to compare with.  If the value is greater than the depth of
    the stack from that point then the result is False.
    The default is 1.

Returns:
  True if the function's name at the specified frame is equal to name.
"""

    def __init__(self):
        super(CallerIs, self).__init__("_caller_is")

    def invoke(self, name, nframes = 1):
        if nframes < 0:
            raise ValueError("nframes must be >= 0")
        frame = gdb.selected_frame()
        while nframes > 0:
            frame = frame.older()
            if frame is None:
                return False
            nframes = nframes - 1
        return frame.name() == name.string()

class CallerMatches(gdb.Function):
    """Compare the calling function's name with a regexp.

Usage:
  $_caller_matches(regex [, number_of_frames])

Arguments:

  regex: The regular expression to compare the function's name with.

  number_of_frames: How many stack frames to traverse back from the currently
    selected frame to compare with.  If the value is greater than the depth of
    the stack from that point then the result is False.
    The default is 1.

Returns:
  True if the function's name at the specified frame matches regex.
"""

    def __init__(self):
        super(CallerMatches, self).__init__("_caller_matches")

    def invoke(self, name, nframes = 1):
        if nframes < 0:
            raise ValueError("nframes must be >= 0")
        frame = gdb.selected_frame()
        while nframes > 0:
            frame = frame.older()
            if frame is None:
                return False
            nframes = nframes - 1
        return re.match(name.string(), frame.name()) is not None

class AnyCallerIs(gdb.Function):
    """Check all calling function's names.

Usage:
  $_any_caller_is(name [, number_of_frames])

Arguments:

  name: The name of the function to search for.

  number_of_frames: How many stack frames to traverse back from the currently
    selected frame to compare with.  If the value is greater than the depth of
    the stack from that point then the result is False.
    The default is 1.

Returns:
  True if any function's name is equal to name.
"""

    def __init__(self):
        super(AnyCallerIs, self).__init__("_any_caller_is")

    def invoke(self, name, nframes = 1):
        if nframes < 0:
            raise ValueError("nframes must be >= 0")
        frame = gdb.selected_frame()
        while nframes >= 0:
            if frame.name() == name.string():
                return True 
            frame = frame.older()
            if frame is None:
                return False
            nframes = nframes - 1
        return False

class AnyCallerMatches(gdb.Function):
    """Compare all calling function's names with a regexp.

Usage:
  $_any_caller_matches(regex [, number_of_frames])

Arguments:

  regex: The regular expression to compare the function's name with.

  number_of_frames: How many stack frames to traverse back from the currently
    selected frame to compare with.  If the value is greater than the depth of
    the stack from that point then the result is False.
    The default is 1.

Returns:
  True if any function's name matches regex.
"""

    def __init__(self):
        super(AnyCallerMatches, self).__init__("_any_caller_matches")

    def invoke(self, name, nframes = 1):
        if nframes < 0:
            raise ValueError("nframes must be >= 0")
        frame = gdb.selected_frame()
        name_re = re.compile(name.string())
        while nframes >= 0:
            if name_re.match(frame.name()) is not None:
                return True
            frame = frame.older()
            if frame is None:
                return False
            nframes = nframes - 1
        return False

CallerIs()
CallerMatches()
AnyCallerIs()
AnyCallerMatches()