Blame glib/glib_gdb.py

Packit ae235b
import gdb
Packit ae235b
import sys
Packit ae235b
Packit ae235b
if sys.version_info[0] >= 3:
Packit ae235b
    long = int
Packit ae235b
Packit ae235b
# This is not quite right, as local vars may override symname
Packit ae235b
def read_global_var (symname):
Packit ae235b
    return gdb.selected_frame().read_var(symname)
Packit ae235b
Packit ae235b
def g_quark_to_string (quark):
Packit ae235b
    if quark == None:
Packit ae235b
        return None
Packit ae235b
    quark = long(quark)
Packit ae235b
    if quark == 0:
Packit ae235b
        return None
Packit ae235b
    try:
Packit ae235b
        val = read_global_var ("quarks")
Packit ae235b
        max_q = long(read_global_var ("quark_seq_id"))
Packit ae235b
    except:
Packit ae235b
        try:
Packit ae235b
            val = read_global_var ("g_quarks")
Packit ae235b
            max_q = long(read_global_var ("g_quark_seq_id"))
Packit ae235b
        except:
Packit ae235b
            return None;
Packit ae235b
    if quark < max_q:
Packit ae235b
        return val[quark].string()
Packit ae235b
    return None
Packit ae235b
Packit ae235b
# We override the node printers too, so that node->next is not expanded
Packit ae235b
class GListNodePrinter:
Packit ae235b
    "Prints a GList node"
Packit ae235b
Packit ae235b
    def __init__ (self, val):
Packit ae235b
        self.val = val
Packit ae235b
Packit ae235b
    def to_string (self):
Packit ae235b
        return "{data=%s, next=0x%x, prev=0x%x}" % (str(self.val["data"]), long(self.val["next"]), long(self.val["prev"]))
Packit ae235b
Packit ae235b
class GSListNodePrinter:
Packit ae235b
    "Prints a GSList node"
Packit ae235b
Packit ae235b
    def __init__ (self, val):
Packit ae235b
        self.val = val
Packit ae235b
Packit ae235b
    def to_string (self):
Packit ae235b
        return "{data=%s, next=0x%x}" % (str(self.val["data"]), long(self.val["next"]))
Packit ae235b
Packit ae235b
class GListPrinter:
Packit ae235b
    "Prints a GList"
Packit ae235b
Packit ae235b
    class _iterator:
Packit ae235b
        def __init__(self, head, listtype):
Packit ae235b
            self.link = head
Packit ae235b
            self.listtype = listtype
Packit ae235b
            self.count = 0
Packit ae235b
Packit ae235b
        def __iter__(self):
Packit ae235b
            return self
Packit ae235b
Packit ae235b
        def next(self):
Packit ae235b
            if self.link == 0:
Packit ae235b
                raise StopIteration
Packit ae235b
            data = self.link['data']
Packit ae235b
            self.link = self.link['next']
Packit ae235b
            count = self.count
Packit ae235b
            self.count = self.count + 1
Packit ae235b
            return ('[%d]' % count, data)
Packit ae235b
Packit ae235b
        __next__ = next
Packit ae235b
Packit ae235b
    def __init__ (self, val, listtype):
Packit ae235b
        self.val = val
Packit ae235b
        self.listtype = listtype
Packit ae235b
Packit ae235b
    def children(self):
Packit ae235b
        return self._iterator(self.val, self.listtype)
Packit ae235b
Packit ae235b
    def to_string (self):
Packit ae235b
        return  "0x%x" % (long(self.val))
Packit ae235b
Packit ae235b
    def display_hint (self):
Packit ae235b
        return "array"
Packit ae235b
Packit ae235b
class GHashPrinter:
Packit ae235b
    "Prints a GHashTable"
Packit ae235b
Packit ae235b
    class _iterator:
Packit ae235b
        def __init__(self, ht, keys_are_strings):
Packit ae235b
            self.ht = ht
Packit ae235b
            if ht != 0:
Packit ae235b
                self.keys = ht["keys"]
Packit ae235b
                self.values = ht["values"]
Packit ae235b
                self.hashes = ht["hashes"]
Packit ae235b
                self.size = ht["size"]
Packit ae235b
            self.pos = 0
Packit ae235b
            self.keys_are_strings = keys_are_strings
Packit ae235b
            self.value = None
Packit ae235b
Packit ae235b
        def __iter__(self):
Packit ae235b
            return self
Packit ae235b
Packit ae235b
        def next(self):
Packit ae235b
            if self.ht == 0:
Packit ae235b
                raise StopIteration
Packit ae235b
            if self.value != None:
Packit ae235b
                v = self.value
Packit ae235b
                self.value = None
Packit ae235b
                return v
Packit ae235b
            while long(self.pos) < long(self.size):
Packit ae235b
                self.pos = self.pos + 1
Packit ae235b
                if long (self.hashes[self.pos]) >= 2:
Packit ae235b
                    key = self.keys[self.pos]
Packit ae235b
                    val = self.values[self.pos]
Packit ae235b
Packit ae235b
                    if self.keys_are_strings:
Packit ae235b
                        key = key.cast (gdb.lookup_type("char").pointer())
Packit ae235b
Packit ae235b
                    # Queue value for next result
Packit ae235b
                    self.value = ('[%dv]'% (self.pos), val)
Packit ae235b
Packit ae235b
                    # Return key
Packit ae235b
                    return ('[%dk]'% (self.pos), key)
Packit ae235b
            raise StopIteration
Packit ae235b
Packit ae235b
        __next__ = next
Packit ae235b
Packit ae235b
    def __init__ (self, val):
Packit ae235b
        self.val = val
Packit ae235b
        self.keys_are_strings = False
Packit ae235b
        try:
Packit ae235b
            string_hash = read_global_var ("g_str_hash")
Packit ae235b
        except:
Packit ae235b
            string_hash = None
Packit ae235b
        if self.val != 0 and string_hash != None and self.val["hash_func"] == string_hash:
Packit ae235b
            self.keys_are_strings = True
Packit ae235b
Packit ae235b
    def children(self):
Packit ae235b
        return self._iterator(self.val, self.keys_are_strings)
Packit ae235b
Packit ae235b
    def to_string (self):
Packit ae235b
        return  "0x%x" % (long(self.val))
Packit ae235b
Packit ae235b
    def display_hint (self):
Packit ae235b
        return "map"
Packit ae235b
Packit ae235b
def pretty_printer_lookup (val):
Packit ae235b
    # None yet, want things like hash table and list
Packit ae235b
Packit ae235b
    type = val.type.unqualified()
Packit ae235b
Packit ae235b
    # If it points to a reference, get the reference.
Packit ae235b
    if type.code == gdb.TYPE_CODE_REF:
Packit ae235b
        type = type.target ()
Packit ae235b
Packit ae235b
    if type.code == gdb.TYPE_CODE_PTR:
Packit ae235b
        type = type.target().unqualified()
Packit ae235b
        t = str(type)
Packit ae235b
        if t == "GList":
Packit ae235b
            return GListPrinter(val, "GList")
Packit ae235b
        if t == "GSList":
Packit ae235b
            return GListPrinter(val, "GSList")
Packit ae235b
        if t == "GHashTable":
Packit ae235b
            return GHashPrinter(val)
Packit ae235b
    else:
Packit ae235b
        t = str(type)
Packit ae235b
        if t == "GList":
Packit ae235b
            return GListNodePrinter(val)
Packit ae235b
        if t == "GSList *":
Packit ae235b
            return GListPrinter(val, "GSList")
Packit ae235b
    return None
Packit ae235b
Packit ae235b
def register (obj):
Packit ae235b
    if obj == None:
Packit ae235b
        obj = gdb
Packit ae235b
Packit ae235b
    obj.pretty_printers.append(pretty_printer_lookup)
Packit ae235b
Packit ae235b
class ForeachCommand (gdb.Command):
Packit ae235b
    """Foreach on list"""
Packit ae235b
Packit ae235b
    def __init__ (self):
Packit ae235b
        super (ForeachCommand, self).__init__ ("gforeach",
Packit ae235b
                                               gdb.COMMAND_DATA,
Packit ae235b
                                               gdb.COMPLETE_SYMBOL)
Packit ae235b
Packit ae235b
    def valid_name (self, name):
Packit ae235b
        if not name[0].isalpha():
Packit ae235b
            return False
Packit ae235b
        return True
Packit ae235b
Packit ae235b
    def parse_args (self, arg):
Packit ae235b
        i = arg.find(" ")
Packit ae235b
        if i <= 0:
Packit ae235b
            raise Exception ("No var specified")
Packit ae235b
        var = arg[:i]
Packit ae235b
        if not self.valid_name(var):
Packit ae235b
            raise Exception ("Invalid variable name")
Packit ae235b
Packit ae235b
        while i < len (arg) and arg[i].isspace():
Packit ae235b
            i = i + 1
Packit ae235b
Packit ae235b
        if arg[i:i+2] != "in":
Packit ae235b
            raise Exception ("Invalid syntax, missing in")
Packit ae235b
Packit ae235b
        i = i + 2
Packit ae235b
Packit ae235b
        while i < len (arg) and arg[i].isspace():
Packit ae235b
            i = i + 1
Packit ae235b
Packit ae235b
        colon = arg.find (":", i)
Packit ae235b
        if colon == -1:
Packit ae235b
            raise Exception ("Invalid syntax, missing colon")
Packit ae235b
Packit ae235b
        val = arg[i:colon]
Packit ae235b
Packit ae235b
        colon = colon + 1
Packit ae235b
        while colon < len (arg) and arg[colon].isspace():
Packit ae235b
            colon = colon + 1
Packit ae235b
Packit ae235b
        command = arg[colon:]
Packit ae235b
Packit ae235b
        return (var, val, command)
Packit ae235b
Packit ae235b
    def do_iter(self, arg, item, command):
Packit ae235b
        item = item.cast (gdb.lookup_type("void").pointer())
Packit ae235b
        item = long(item)
Packit ae235b
        to_eval = "set $%s = (void *)0x%x\n"%(arg, item)
Packit ae235b
        gdb.execute(to_eval)
Packit ae235b
        gdb.execute(command)
Packit ae235b
Packit ae235b
    def slist_iterator (self, arg, container, command):
Packit ae235b
        l = container.cast (gdb.lookup_type("GSList").pointer())
Packit ae235b
        while long(l) != 0:
Packit ae235b
            self.do_iter (arg, l["data"], command)
Packit ae235b
            l = l["next"]
Packit ae235b
Packit ae235b
    def list_iterator (self, arg, container, command):
Packit ae235b
        l = container.cast (gdb.lookup_type("GList").pointer())
Packit ae235b
        while long(l) != 0:
Packit ae235b
            self.do_iter (arg, l["data"], command)
Packit ae235b
            l = l["next"]
Packit ae235b
Packit ae235b
    def pick_iterator (self, container):
Packit ae235b
        t = container.type.unqualified()
Packit ae235b
        if t.code == gdb.TYPE_CODE_PTR:
Packit ae235b
            t = t.target().unqualified()
Packit ae235b
            t = str(t)
Packit ae235b
            if t == "GSList":
Packit ae235b
                return self.slist_iterator
Packit ae235b
            if t == "GList":
Packit ae235b
                return self.list_iterator
Packit ae235b
        raise Exception("Invalid container type %s"%(str(container.type)))
Packit ae235b
Packit ae235b
    def invoke (self, arg, from_tty):
Packit ae235b
        (var, container, command) = self.parse_args(arg)
Packit ae235b
        container = gdb.parse_and_eval (container)
Packit ae235b
        func = self.pick_iterator(container)
Packit ae235b
        func(var, container, command)
Packit ae235b
Packit ae235b
ForeachCommand ()