Home | History | Annotate | Download | only in function
      1 # Caller-is functions.
      2 # Copyright (C) 2008-2016 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:
     24   $_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 
     39     def __init__(self):
     40         super(CallerIs, self).__init__("_caller_is")
     41 
     42     def invoke(self, name, nframes = 1):
     43         if nframes < 0:
     44             raise ValueError("nframes must be >= 0")
     45         frame = gdb.selected_frame()
     46         while nframes > 0:
     47             frame = frame.older()
     48             if frame is None:
     49                 return False
     50             nframes = nframes - 1
     51         return frame.name() == name.string()
     52 
     53 class CallerMatches(gdb.Function):
     54     """Compare the calling function's name with a regexp.
     55 
     56 Usage:
     57   $_caller_matches(regex [, number_of_frames])
     58 
     59 Arguments:
     60 
     61   regex: The regular expression to compare the function's name with.
     62 
     63   number_of_frames: How many stack frames to traverse back from the currently
     64     selected frame to compare with.  If the value is greater than the depth of
     65     the stack from that point then the result is False.
     66     The default is 1.
     67 
     68 Returns:
     69   True if the function's name at the specified frame matches regex.
     70 """
     71 
     72     def __init__(self):
     73         super(CallerMatches, self).__init__("_caller_matches")
     74 
     75     def invoke(self, name, nframes = 1):
     76         if nframes < 0:
     77             raise ValueError("nframes must be >= 0")
     78         frame = gdb.selected_frame()
     79         while nframes > 0:
     80             frame = frame.older()
     81             if frame is None:
     82                 return False
     83             nframes = nframes - 1
     84         return re.match(name.string(), frame.name()) is not None
     85 
     86 class AnyCallerIs(gdb.Function):
     87     """Check all calling function's names.
     88 
     89 Usage:
     90   $_any_caller_is(name [, number_of_frames])
     91 
     92 Arguments:
     93 
     94   name: The name of the function to search for.
     95 
     96   number_of_frames: How many stack frames to traverse back from the currently
     97     selected frame to compare with.  If the value is greater than the depth of
     98     the stack from that point then the result is False.
     99     The default is 1.
    100 
    101 Returns:
    102   True if any function's name is equal to name.
    103 """
    104 
    105     def __init__(self):
    106         super(AnyCallerIs, self).__init__("_any_caller_is")
    107 
    108     def invoke(self, name, nframes = 1):
    109         if nframes < 0:
    110             raise ValueError("nframes must be >= 0")
    111         frame = gdb.selected_frame()
    112         while nframes >= 0:
    113             if frame.name() == name.string():
    114                 return True 
    115             frame = frame.older()
    116             if frame is None:
    117                 return False
    118             nframes = nframes - 1
    119         return False
    120 
    121 class AnyCallerMatches(gdb.Function):
    122     """Compare all calling function's names with a regexp.
    123 
    124 Usage:
    125   $_any_caller_matches(regex [, number_of_frames])
    126 
    127 Arguments:
    128 
    129   regex: The regular expression to compare the function's name with.
    130 
    131   number_of_frames: How many stack frames to traverse back from the currently
    132     selected frame to compare with.  If the value is greater than the depth of
    133     the stack from that point then the result is False.
    134     The default is 1.
    135 
    136 Returns:
    137   True if any function's name matches regex.
    138 """
    139 
    140     def __init__(self):
    141         super(AnyCallerMatches, self).__init__("_any_caller_matches")
    142 
    143     def invoke(self, name, nframes = 1):
    144         if nframes < 0:
    145             raise ValueError("nframes must be >= 0")
    146         frame = gdb.selected_frame()
    147         name_re = re.compile(name.string())
    148         while nframes >= 0:
    149             if name_re.match(frame.name()) is not None:
    150                 return True
    151             frame = frame.older()
    152             if frame is None:
    153                 return False
    154             nframes = nframes - 1
    155         return False
    156 
    157 CallerIs()
    158 CallerMatches()
    159 AnyCallerIs()
    160 AnyCallerMatches()
    161