简体   繁体   中英

Setting a Watchpoint to Output a Hex List in GDB

I'm debugging a C program with GDB and I've setup a watchpoint on a variable uint8_t msg_bin_tst[64]; . When the watchpoint is triggered GDB outputs something like:

Watchpoint 4: msg_bin_tst
Old value = "@\001\000\067\335\002\033a32c85ba9dda45823be416246cf8b433baa068d7\000\000\000\000\000\000\000\000\000\017\000\000\000(\000\000"
New value = "@\001\000\067\335\002\033a32c85ba9dda45823be416246cf6cf8b433baa068d7\000\000\000\000\000\000\017\000\000\000(\000\000"

This is somewhat hard to interpret, is there a way to set the output format to be like print/x , that is, something along the lines of:

msg_bin_tst = {0x40, 0x1, 0x0, 0x37, 0xdd, 0x2, 0x1b, 0x61, 0x33, 0x32, 0x63, 0x38, 0x35, 0x62, 0x61, 0x39, 0x64, 0x64, 0x61, 0x34, 0x35, 0x38, 0x32, 0x33, 0x62, 0x65, 0x34, 0x31, 0x36, 0x32, 0x34, 0x36, 0x63, 0x66, 0x38, 0x62, 0x34, 0x33, 0x33, 0x62, 0x61, 0x61, 0x30, 0x36, 0x38, 0x64, 0x37, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0}

Use commands to set command when the watchpoint is hit, for example:

(gdb) commands 2
Type commands for breakpoint(s) 2, one per line.
End with a line saying just "end".
>print /x msg_bin_tst
>end

2 is the number of watchpoint. Then when the watchpoint is hit, the array will be dumped automatically.

You can refer the gdb manual: https://sourceware.org/gdb/onlinedocs/gdb/Break-Commands.html .

gdb prints your value according to the default format for uint8_t[64]. However gdb also allows you to change the way it prints a particual type. This is called Python Pretty-Printers . You can read about them here:

I don't have c++11 and in order to demonstrate an example I use unsigned char as a type. The printer for your type might look like:

$ cat my_priner.py
class CustomPrinter(object):
    def __init__(self, val):
        self.val = val

    def to_string(self):
        res = "{"
        for m in xrange(64):
          res += hex(int(self.val[m]))
          if m != 63:
            res += ", "
        res += " }"
        return res

def lookup_type (val):
    if str(val.type) == 'unsigned char [64]':
        return CustomPrinter(val)
    return None

gdb.pretty_printers.append (lookup_type)

And this is this printer in action for my variable unsigned char m[64]; :

$ gdb  -q -x my_priner.py  -ex "set pagination off" -ex "start" -ex "watch m" -ex "c"  ./main
Reading symbols from /home/main...done.
Temporary breakpoint 1 at 0x400590: file main.cpp, line 7.
Starting program: /home/main

Temporary breakpoint 1, main () at main.cpp:7
7         memcpy(m, "He",2);
Watchpoint 2: m
Continuing.
Watchpoint 2: m

Old value = {0x48, 0x65, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
New value = {0x48, 0x65, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
0x0000003c41288ac4 in memcpy () from /lib64/libc.so.6
(gdb)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM