Gdb Pretty Printer: *(char*){hex_address} equivalent in python -
i have c++ classes in following format (copying important parts):
class my_stringimpl { public: static sample_string* create(const char* str, int len) { my_stringimpl* sample = static_cast<my_stringimpl*>(malloc(sizeof(my_stringimpl) + len*sizeof(char))); char* data_ptr = reinterpret_cast<char*>(sample+1); memset(data_ptr, 0, len); memcpy(data_ptr, str, len); return new (sample) my_stringimpl(len); } private: int m_length; }; class my_string { public: my_string(const char* str, int len) : m_impl(my_stringimpl::create(str, len)) { } ~my_string() { delete m_impl; } private: my_stringimpl* m_impl; };
for my_string class adding pretty printer. added following defs in python script (which including in .gdbinit file) - func defs copied here:
def string_to_char(ptr, length=none): error_message = '' if length none: length = int(0) error_message = 'null string' else: length = int(length) string = ''.join([chr((ptr + i).dereference()) in xrange(length)]) return string + error_message class stringprinter(object): def __init__(self, val): self.val = val class stringimplprinter(stringprinter): def get_length(self): return self.val['m_length'] def get_data_address(self): return self.val.address + 1 def to_string(self): return string_to_char(self.get_data_address(), self.get_length()) class mystringprinter(stringprinter): def stringimpl_ptr(self): return self.val['m_impl'] def get_length(self): if not self.stringimpl_ptr(): return 0 return stringimplprinter(self.stringimpl_ptr().dereference()).get_length() def to_string(self): if not self.stringimpl_ptr(): return '(null)' return stringimplprinter(self.stringimpl_ptr().dereference()).to_string()
but, on usage getting below error -
python exception <class 'gdb.error'> cannot convert value int.:
if try change value in 'ptr' int , arthimetic before casting char (like in def above), gives below error:
python exception <type 'exceptions.attributeerror'> 'nonetype' object has no attribute 'cast':
can tell doing wrong? struck here. :(. in nutshell, trying achieve following c/c++ expr equivalent,
*(char*){hex_address}
in python. how can it?
it's better post full stack traces or @ least indicate lines throws exception. python provides this...
your string_to_char function can replaced value.string or value.lazy_string, designed use. imagine error coming there; in case ought remove it.
also, printers should implement "hint" methods return "string".
Comments
Post a Comment