是否可以使用gdb以C99语法打印结构?
例如:
UpdateStocks::storeSitemapData(UpdateStocks::collectAPIData));
该结构的实例可以写为:
struct ApplicationState {
struct {
bool use_crash_handler;
bool use_abort_handler;
} signal;
struct {
unsigned char python;
} exit_code_on_error;
};
是否可以使用gdb从结构实例中获取像这样的文字字符串?
答案 0 :(得分:1)
我在python中编写了一个新的gdb
CLI命令,以C99样式打印结构的内容。通过此命令,我可以获得:
(gdb) print_struct_c99 as
struct ApplicationState as = {
.x = 0,
.signal = {
.use_crash_handler = true,
.use_abort_handler = false
},
.exit_code_on_error = {
.python = 88
}
}
在运行print_struct_c99
之前,您必须获取python脚本的源代码。例如:
(gdb) source gdb_script.py
Python脚本:
class PrintStructC99(gdb.Command):
def __init__(self):
super(PrintStructC99, self).__init__(
"print_struct_c99",
gdb.COMMAND_USER,
)
def get_count_heading(self, string):
for i, s in enumerate(string):
if s != ' ':
break
return i
def extract_typename(self, string):
first_line = string.split('\n')[0]
return first_line.split('=')[1][:-1].strip()
def invoke(self, arg, from_tty):
ret_ptype = gdb.execute('ptype {}'.format(arg), to_string=True)
tname = self.extract_typename(ret_ptype)
print('{} {} = {{'.format(tname, arg))
r = gdb.execute('p {}'.format(arg), to_string=True)
r = r.split('\n')
for rr in r[1:]:
if '=' not in rr:
print(rr)
continue
hs = self.get_count_heading(rr)
rr_s = rr.strip().split('=')
rr_rval = rr_s[1].strip().split(' ')[0]
print(' ' * hs + '.' + rr_s[0] + '= ' + rr_rval)
print('Running GDB from: %s\n' % (gdb.PYTHONDIR))
gdb.execute("set print pretty")
# instantiate
PrintStructC99()