如何从 gdb 打印 C99 结构?

How to print a C99 struct from gdb?

是否可以使用 gdb 以 C99 语法打印结构?

例如:

struct ApplicationState {
    struct {
        bool use_crash_handler;
        bool use_abort_handler;
    } signal;

    struct {
        unsigned char python;
    } exit_code_on_error;
};

这个结构的实例可以写成:

struct ApplicationState app_state = {
    .signal = {
        .use_crash_handler = true,
        .use_abort_handler = true,
    },
    .exit_code_on_error = {
        .python = 0,
    },
};

是否可以使用 gdb 从结构的实例中获取类似这样的文字字符串?

我在 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('=', 1)
            rr_rval = rr_s[1].strip()
            print(' ' * hs + '.' + rr_s[0] + '= ' + rr_rval)


print('Running GDB from: %s\n' % (gdb.PYTHONDIR))
gdb.execute("set print pretty")
gdb.execute('set pagination off')
gdb.execute('set print repeats 0')
gdb.execute('set print elements unlimited')
# instantiate
PrintStructC99()