17"""Utilities for defining xmethods"""
24 """Base class (or a template) for an xmethod description.
26 Currently, the description requires only the 'name' and 'enabled'
27 attributes. Description objects are managed by 'XMethodMatcher'
28 objects (see below). Note that this is only a template for the
29 interface of the XMethodMatcher.methods objects. One could use
30 this class or choose to use an object which supports this exact same
31 interface. Also, an XMethodMatcher can choose not use it 'methods'
32 attribute. In such cases this class (or an equivalent) is not used.
35 name: The name of the xmethod.
36 enabled: A boolean indicating if the xmethod is enabled.
45 """Abstract base class for matching an xmethod.
47 When looking for xmethods, GDB invokes the `match' method of a
48 registered xmethod matcher to match the object type and method name.
49 The `match' method in concrete classes derived from this class should
50 return an `XMethodWorker' object, or a list of `XMethodWorker'
51 objects if there is a match (see below for 'XMethodWorker' class).
54 name: The name of the matcher.
55 enabled: A boolean indicating if the matcher is enabled.
56 methods: A sequence of objects of type 'XMethod', or objects
57 which have at least the attributes of an 'XMethod' object.
58 This list is used by the 'enable'/'disable'/'info' commands to
59 enable/disable/list the xmethods registered with GDB. See
60 the 'match' method below to know how this sequence is used.
61 This attribute is None if the matcher chooses not have any
62 xmethods managed by it.
68 name: An identifying name for the xmethod or the group of
69 xmethods returned by the `match' method.
75 def match(self, class_type, method_name):
76 """Match class type and method name.
78 In derived classes, it should return an XMethodWorker object, or a
79 sequence of 'XMethodWorker' objects. Only those xmethod workers
80 whose corresponding 'XMethod' descriptor object is enabled should be
84 class_type: The class type (gdb.Type object) to match.
85 method_name: The name (string) of the method to match.
87 raise NotImplementedError(
"XMethodMatcher match")
91 """Base class for all xmethod workers defined in Python.
93 An xmethod worker is an object which matches the method arguments, and
94 invokes the method when GDB wants it to. Internally, GDB first invokes the
95 'get_arg_types' method to perform overload resolution. If GDB selects to
96 invoke this Python xmethod, then it invokes it via the overridden
97 '__call__' method. The 'get_result_type' method is used to implement
98 'ptype' on the xmethod.
100 Derived classes should override the 'get_arg_types', 'get_result_type'
101 and '__call__' methods.
105 """Return arguments types of an xmethod.
107 A sequence of gdb.Type objects corresponding to the arguments of the
108 xmethod are returned. If the xmethod takes no arguments, then 'None'
109 or an empty sequence is returned. If the xmethod takes only a single
110 argument, then a gdb.Type object or a sequence with a single gdb.Type
113 raise NotImplementedError(
"XMethodWorker get_arg_types")
116 """Return the type of the result of the xmethod.
119 args: Arguments to the method. Each element of the tuple is a
120 gdb.Value object. The first element is the 'this' pointer
121 value. These are the same arguments passed to '__call__'.
124 A gdb.Type object representing the type of the result of the
127 raise NotImplementedError(
"XMethodWorker get_result_type")
130 """Invoke the xmethod.
133 args: Arguments to the method. Each element of the tuple is a
134 gdb.Value object. The first element is the 'this' pointer
138 A gdb.Value corresponding to the value returned by the xmethod.
139 Returns 'None' if the method does not return anything.
141 raise NotImplementedError(
"XMethodWorker __call__")
145 """A utility class to implement simple xmethod mathers and workers.
147 See the __init__ method below for information on how instances of this
150 For simple classes and methods, one can choose to use this class. For
151 complex xmethods, which need to replace/implement template methods on
152 possibly template classes, one should implement their own xmethod
153 matchers and workers. See py-xmethods.py in testsuite/gdb.python
154 directory of the GDB source tree for examples.
169 self, name, class_matcher, method_matcher, method_function, *arg_types
173 name: Name of the xmethod matcher.
174 class_matcher: A regular expression used to match the name of the
175 class whose method this xmethod is implementing/replacing.
176 method_matcher: A regular expression used to match the name of the
177 method this xmethod is implementing/replacing.
178 method_function: A Python callable which would be called via the
179 'invoke' method of the worker returned by the objects of this
180 class. This callable should accept the object (*this) as the
181 first argument followed by the rest of the arguments to the
182 method. All arguments to this function should be gdb.Value
184 arg_types: The gdb.Type objects corresponding to the arguments that
185 this xmethod takes. It can be None, or an empty sequence,
186 or a single gdb.Type object, or a sequence of gdb.Type objects.
188 XMethodMatcher.__init__(self, name)
189 assert callable(method_function), (
190 "The 'method_function' argument to 'SimpleXMethodMatcher' "
191 "__init__ method should be a callable."
198 def match(self, class_type, method_name):
199 cm = re.match(self.
_class_matcher, str(class_type.unqualified().tag))
213 if not hasattr(matcher,
"match"):
214 return TypeError(
"Xmethod matcher is missing method: match")
215 if not hasattr(matcher,
"name"):
216 return TypeError(
"Xmethod matcher is missing attribute: name")
217 if not hasattr(matcher,
"enabled"):
218 return TypeError(
"Xmethod matcher is missing attribute: enabled")
219 if not isinstance(matcher.name, str):
220 return TypeError(
"Attribute 'name' of xmethod matcher is not a " "string")
221 if matcher.name.find(
";") >= 0:
222 return ValueError(
"Xmethod matcher name cannot contain ';' in it")
232 for i
in range(0, len(locus.xmethods)):
233 if locus.xmethods[i].name == name:
239 """Registers a xmethod matcher MATCHER with a LOCUS.
242 locus: The locus in which the xmethods should be registered.
243 It can be 'None' to indicate that the xmethods should be
244 registered globally. Or, it could be a gdb.Objfile or a
245 gdb.Progspace object in which the xmethods should be
247 matcher: The xmethod matcher to register with the LOCUS. It
248 should be an instance of 'XMethodMatcher' class.
249 replace: If True, replace any existing xmethod matcher with the
250 same name in the locus. Otherwise, if a matcher with the same name
251 exists in the locus, raise an exception.
259 locus_name =
"global"
261 locus_name = locus.filename
265 del locus.xmethods[index]
268 "Xmethod matcher already registered with "
269 "%s: %s" % (locus_name, matcher.name)
271 if gdb.parameter(
"verbose"):
272 gdb.write(
"Registering xmethod matcher '%s' with %s' ...\n")
273 locus.xmethods.insert(0, matcher)
__init__(self, method_function, arg_types)
__init__(self, name, class_matcher, method_matcher, method_function, *arg_types)
match(self, class_type, method_name)
match(self, class_type, method_name)
get_result_type(self, *args)
_lookup_xmethod_matcher(locus, name)
register_xmethod_matcher(locus, matcher, replace=False)
_validate_xmethod_matcher(matcher)