gdb - pretty printer help: object has no attribute -
i writing first pretty printer, have trouble. gdb 7.7.
here c++ code debug:
enum country { china, usa }; class foo { public: foo(int _v, enum country c) {v = _v; m_country = c;} ~foo() { v = -1; } int v; enum country m_country; }; int main() { foo f(10, china); return 0; }
here printer:
import gdb class fooprinter: def __init__(self, val): self.v = val def to_string(self): # because v integer, simpler way is: # return self.v['v'] v = "v=" + str(self.v['v']) f = self.m_country['m_country'] if (f == 0): c = "china" elif (f == 1): c = "usa" else: c = "unknown" c = "c=" + c ret = v + '\n' + c return ret def lookup_type (val): if str(val.type) == 'foo': return fooprinter(val) return none gdb.pretty_printers.append (lookup_type)
when run printer in gdb, error:
(gdb) source printer.py (gdb) p f python exception <class 'attributeerror'> 'fooprinter' object has no attribute 'm_country':
i guess need somehow modify __init__
take country argument, if so, how should update rest of code?
the problem line:
f = self.m_country['m_country']
here think want examine self.v
instead:
f = self.v['m_country']
note can enumeration constant names enum type. so, can replace chained if
simpler lookup. nice if enum larger couple of constants.