1## 2## imported from glib: glib/glib_gdb.py 3## 4import gdb 5import sys 6 7if sys.version_info[0] >= 3: 8 long = int 9 10# This is not quite right, as local vars may override symname 11def read_global_var (symname): 12 return gdb.selected_frame().read_var(symname) 13 14def g_quark_to_string (quark): 15 if quark is None: 16 return None 17 quark = long(quark) 18 if quark == 0: 19 return None 20 try: 21 val = read_global_var ("quarks") 22 max_q = long(read_global_var ("quark_seq_id")) 23 except: 24 try: 25 val = read_global_var ("g_quarks") 26 max_q = long(read_global_var ("g_quark_seq_id")) 27 except: 28 return None; 29 if quark < max_q: 30 return val[quark].string() 31 return None 32 33## 34## imported from glib: gobject/gobject_gdb.py 35## 36 37def g_type_to_typenode (gtype): 38 def lookup_fundamental_type (typenode): 39 if typenode == 0: 40 return None 41 val = read_global_var ("static_fundamental_type_nodes") 42 if val is None: 43 return None 44 return val[typenode >> 2].address 45 46 gtype = long(gtype) 47 typenode = gtype - gtype % 4 48 if typenode > (255 << 2): 49 typenode = gdb.Value(typenode).cast (gdb.lookup_type("TypeNode").pointer()) 50 else: 51 typenode = lookup_fundamental_type (typenode) 52 return typenode 53 54def g_type_to_name (gtype): 55 typenode = g_type_to_typenode(gtype) 56 if typenode != None: 57 return g_quark_to_string (typenode["qname"]) 58 return None 59 60def g_type_name_from_instance (instance): 61 if long(instance) != 0: 62 try: 63 inst = instance.cast (gdb.lookup_type("GTypeInstance").pointer()) 64 klass = inst["g_class"] 65 gtype = klass["g_type"] 66 name = g_type_to_name (gtype) 67 return name 68 except RuntimeError: 69 pass 70 return None 71