1

Is it possible to use gdb to print a struct in C99 syntax?

eg:

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

    struct {
        unsigned char python;
    } exit_code_on_error;
};

An instance of this struct could be written as:

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

Is it possible to use gdb to get something like this as a literal string from an instance of the struct?

2
  • By default, gdb can print: $1 = {signal = {use_crash_handler = true, use_abort_handler = false}, exit_code_on_error = {python = 88 'X'}}. Is it different with what you want to get?
    – H. Jang
    Mar 22, 2019 at 13:17
  • Could it print with the . before each variable? otherwise it's not valid C99 (I'd like to be able to use this as input - the structs are very large, while I could manipulate the output - it would be nice if I could get output which is already usable).
    – ideasman42
    Mar 22, 2019 at 13:33

1 Answer 1

2

I wrote a new gdb CLI command in python to print contents of a struct in C99 style. By this command, I can get this:

(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
  }
}

You have to source the python script, before run print_struct_c99. For example:

(gdb) source gdb_script.py

Python script:


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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.