0

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?

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_']
phreed
  • 1,759
  • 1
  • 15
  • 30
  • xmethods are usually by default enabled. What does `info xmethod` tell you about your xmethod? – ssbssa Jul 28 '22 at 16:34
  • @ssbssa I updated the post with the output from `info xmethod` which is empty. – phreed Jul 28 '22 at 18:52
  • Is the LOCUS-REGEXP the path to the python script? the directory containing the script? something else? – phreed Jul 28 '22 at 19:02
  • 1
    You have your xmethod in some python file, right? Then have you loaded it in gdb, e.g. with `source /path/to/your/xmethod.py`. – ssbssa Jul 28 '22 at 19:06
  • Yes, I have it in a python file /home/me/xmethods/my-class.py. I have loaded it in gdb with `source /home/me/xmethods/my-class.py`. I updated the question with the source for `my-class.py`. Note that `import gdb` does not include `gdb.xmethod` it needed to be included in its own import. – phreed Jul 28 '22 at 20:54
  • 2
    You're missing the `gdb.xmethod.register_xmethod_matcher(None, MyClassMatcher())` call to register the xmethod in gdb. – ssbssa Jul 29 '22 at 05:21
  • @ssbssa if you add this as an answer I will accept it as a solution. – phreed Aug 19 '22 at 18:42

0 Answers0