When creating an 'xmethod' how do I activate it in GDB?
source /home/me/xmethod/my-class.py
enable xmethod
The documentation does not provide an activation example.
help enable xmethod
GDB command to enable a specified (group of) xmethod(s).
Usage: enable xmethod [LOCUS-REGEXP [NAME-REGEXP]]
LOCUS-REGEXP is a regular expression matching the location of the
xmethod matchers. If it is omitted, all registered xmethods matchers
from all loci are enabled. A locus could be 'global', a regular expression
matching the current program spaces filename, or a regular expression
matching filenames of objfiles. Locus could be 'progspace' to specify that
only xmethods from the current progspace should be enabled.
NAME-REGEXP is a regular expression matching the names of xmethods
within a given locus. If this omitted for a specified locus, then all
registered xmethod matchers in the locus are enabled. To enable only
a certain xmethods managed by a single matcher, the name regexp can be
specified as matcher-name-regexp;xmethod-name-regexp.
The method does not seem to be active.
info xmethod
Returns a list of nothing.
Note: I am running GDB under CLion.
What I am wanting is a good and proper example.
The source code for xmethod.py does provide some documentation but not an example.
Any advice on the proper way to specify the locus and name?
- GDB source for managing xmethods
- Canonical Docs on xmethod
- Source Code for xmethods.py
- Source Code for xmethod.py a sample
- A discussion about xmethods
- The libstdc++ xmethod
Here is the my-class.py
I am trying to load:
import gdb
import gdb.xmethod
# https://sourceware.org/gdb/onlinedocs/gdb/Xmethods-In-Python.html#Xmethods-In-Python
class MyClass_geta(gdb.xmethod.XMethod):
def __init__(self):
gdb.xmethod.XMethod.__init__(self, 'geta')
def get_worker(self, method_name):
if method_name == 'geta':
return MyClassWorker_geta()
class MyClass_sum(gdb.xmethod.XMethod):
def __init__(self):
gdb.xmethod.XMethod.__init__(self, 'sum')
def get_worker(self, method_name):
if method_name == 'operator+':
return MyClassWorker_plus()
class MyClassMatcher(gdb.xmethod.XMethodMatcher):
def __init__(self):
gdb.xmethod.XMethodMatcher.__init__(self, 'MyClassMatcher')
# List of methods 'managed' by this matcher
self.methods = [MyClass_geta(), MyClass_sum()]
def match(self, class_type, method_name):
if class_type.tag != 'MyClass':
return None
workers = []
for method in self.methods:
if method.enabled:
worker = method.get_worker(method_name)
if worker:
workers.append(worker)
return workers
class MyClassWorker_geta(gdb.xmethod.XMethodWorker):
def get_arg_types(self):
return None
def get_result_type(self, obj):
return gdb.lookup_type('int')
def __call__(self, obj):
return obj['a_']
class MyClassWorker_plus(gdb.xmethod.XMethodWorker):
def get_arg_types(self):
return gdb.lookup_type('MyClass')
def get_result_type(self, obj):
return gdb.lookup_type('int')
def __call__(self, obj, other):
return obj['a_'] + other['a_']