17 """Utilities for defining xmethods""" 24 if sys.version_info[0] > 2:
31 """Base class (or a template) for an xmethod description. 33 Currently, the description requires only the 'name' and 'enabled' 34 attributes. Description objects are managed by 'XMethodMatcher' 35 objects (see below). Note that this is only a template for the 36 interface of the XMethodMatcher.methods objects. One could use 37 this class or choose to use an object which supports this exact same 38 interface. Also, an XMethodMatcher can choose not use it 'methods' 39 attribute. In such cases this class (or an equivalent) is not used. 42 name: The name of the xmethod. 43 enabled: A boolean indicating if the xmethod is enabled. 52 """Abstract base class for matching an xmethod. 54 When looking for xmethods, GDB invokes the `match' method of a 55 registered xmethod matcher to match the object type and method name. 56 The `match' method in concrete classes derived from this class should 57 return an `XMethodWorker' object, or a list of `XMethodWorker' 58 objects if there is a match (see below for 'XMethodWorker' class). 61 name: The name of the matcher. 62 enabled: A boolean indicating if the matcher is enabled. 63 methods: A sequence of objects of type 'XMethod', or objects 64 which have at least the attributes of an 'XMethod' object. 65 This list is used by the 'enable'/'disable'/'info' commands to 66 enable/disable/list the xmethods registered with GDB. See 67 the 'match' method below to know how this sequence is used. 68 This attribute is None if the matcher chooses not have any 69 xmethods managed by it. 75 name: An identifying name for the xmethod or the group of 76 xmethods returned by the `match' method. 82 def match(self, class_type, method_name):
83 """Match class type and method name. 85 In derived classes, it should return an XMethodWorker object, or a 86 sequence of 'XMethodWorker' objects. Only those xmethod workers 87 whose corresponding 'XMethod' descriptor object is enabled should be 91 class_type: The class type (gdb.Type object) to match. 92 method_name: The name (string) of the method to match. 94 raise NotImplementedError(
"XMethodMatcher match")
98 """Base class for all xmethod workers defined in Python. 100 An xmethod worker is an object which matches the method arguments, and 101 invokes the method when GDB wants it to. Internally, GDB first invokes the 102 'get_arg_types' method to perform overload resolution. If GDB selects to 103 invoke this Python xmethod, then it invokes it via the overridden 104 '__call__' method. The 'get_result_type' method is used to implement 105 'ptype' on the xmethod. 107 Derived classes should override the 'get_arg_types', 'get_result_type' 108 and '__call__' methods. 112 """Return arguments types of an xmethod. 114 A sequence of gdb.Type objects corresponding to the arguments of the 115 xmethod are returned. If the xmethod takes no arguments, then 'None' 116 or an empty sequence is returned. If the xmethod takes only a single 117 argument, then a gdb.Type object or a sequence with a single gdb.Type 120 raise NotImplementedError(
"XMethodWorker get_arg_types")
123 """Return the type of the result of the xmethod. 126 args: Arguments to the method. Each element of the tuple is a 127 gdb.Value object. The first element is the 'this' pointer 128 value. These are the same arguments passed to '__call__'. 131 A gdb.Type object representing the type of the result of the 134 raise NotImplementedError(
"XMethodWorker get_result_type")
137 """Invoke the xmethod. 140 args: Arguments to the method. Each element of the tuple is a 141 gdb.Value object. The first element is the 'this' pointer 145 A gdb.Value corresponding to the value returned by the xmethod. 146 Returns 'None' if the method does not return anything. 148 raise NotImplementedError(
"XMethodWorker __call__")
152 """A utility class to implement simple xmethod mathers and workers. 154 See the __init__ method below for information on how instances of this 157 For simple classes and methods, one can choose to use this class. For 158 complex xmethods, which need to replace/implement template methods on 159 possibly template classes, one should implement their own xmethod 160 matchers and workers. See py-xmethods.py in testsuite/gdb.python 161 directory of the GDB source tree for examples. 176 def __init__(self, name, class_matcher, method_matcher, method_function,
180 name: Name of the xmethod matcher. 181 class_matcher: A regular expression used to match the name of the 182 class whose method this xmethod is implementing/replacing. 183 method_matcher: A regular expression used to match the name of the 184 method this xmethod is implementing/replacing. 185 method_function: A Python callable which would be called via the 186 'invoke' method of the worker returned by the objects of this 187 class. This callable should accept the object (*this) as the 188 first argument followed by the rest of the arguments to the 189 method. All arguments to this function should be gdb.Value 191 arg_types: The gdb.Type objects corresponding to the arguments that 192 this xmethod takes. It can be None, or an empty sequence, 193 or a single gdb.Type object, or a sequence of gdb.Type objects. 195 XMethodMatcher.__init__(self, name)
196 assert callable(method_function), (
197 "The 'method_function' argument to 'SimpleXMethodMatcher' " 198 "__init__ method should be a callable.")
204 def match(self, class_type, method_name):
205 cm = re.match(self.
_class_matcher, str(class_type.unqualified().tag))
217 if not hasattr(matcher,
"match"):
218 return TypeError(
"Xmethod matcher is missing method: match")
219 if not hasattr(matcher,
"name"):
220 return TypeError(
"Xmethod matcher is missing attribute: name")
221 if not hasattr(matcher,
"enabled"):
222 return TypeError(
"Xmethod matcher is missing attribute: enabled")
223 if not isinstance(matcher.name, basestring):
224 return TypeError(
"Attribute 'name' of xmethod matcher is not a " 226 if matcher.name.find(
";") >= 0:
227 return ValueError(
"Xmethod matcher name cannot contain ';' in it")
236 for i
in range(0, len(locus.xmethods)):
237 if locus.xmethods[i].name == name:
243 """Registers a xmethod matcher MATCHER with a LOCUS. 246 locus: The locus in which the xmethods should be registered. 247 It can be 'None' to indicate that the xmethods should be 248 registered globally. Or, it could be a gdb.Objfile or a 249 gdb.Progspace object in which the xmethods should be 251 matcher: The xmethod matcher to register with the LOCUS. It 252 should be an instance of 'XMethodMatcher' class. 253 replace: If True, replace any existing xmethod matcher with the 254 same name in the locus. Otherwise, if a matcher with the same name 255 exists in the locus, raise an exception. 263 locus_name =
"global" 265 locus_name = locus.filename
269 del locus.xmethods[index]
271 raise RuntimeError(
"Xmethod matcher already registered with " 272 "%s: %s" % (locus_name, matcher.name))
273 if gdb.parameter(
"verbose"):
274 gdb.write(
"Registering xmethod matcher '%s' with %s' ...\n")
275 locus.xmethods.insert(0, matcher)
def match(self, class_type, method_name)
def get_result_type(self, args)
def __init__(self, method_function, arg_types)
def match(self, class_type, method_name)
def register_xmethod_matcher(locus, matcher, replace=False)
def _validate_xmethod_matcher(matcher)
def _lookup_xmethod_matcher(locus, name)