gdb | view the variable argument list

前端 未结 2 815
悲&欢浪女
悲&欢浪女 2021-01-04 09:48

I as using the bt command to view the stacktrace. The output is

(gdb) bt
#0  0x001ae4cd in Debugger (message=0x1 \"???\\a\") at /SourceCache/xnu         


        
相关标签:
2条回答
  • 2021-01-04 09:55
    (gdb) frame 8
    

    will put you in the frame of the caller. Examine the arguments that are being passed.

    0 讨论(0)
  • 2021-01-04 10:02

    Looks like this is possible to do it for a simple program like this:

    #include <stdarg.h>
    #include <stdio.h>
    
    void myfunc(const char *fmt, ...)
    {
            va_list args;
            va_start(args, fmt);
            vprintf(fmt, args);
            va_end(args);
            return;
    }
    
    int main(int argc, char *argv[])
    {
            myfunc("test 1: %s %s\n", "one", "two");
            myfunc("test 2: %s %d %c\n", "apple", 222, 'y');
            return 0;
    }
    

    Here is sample gdb session:

    $ gdb testprog
    GNU gdb (GDB) 7.1-debian
    [snip]
    Reading symbols from /home/user/testprog...done.
    (gdb) break myfunc
    Breakpoint 1 at 0x400552: file testprog.c, line 7.
    (gdb) run
    Starting program: /home/user/testprog
    
    Breakpoint 1, myfunc (fmt=0x4006f4 "test 1: %s %s\n") at testprog.c:7
    7               va_start(args, fmt);
    (gdb) # initialize args to hold correct values:
    (gdb) step
    8               vprintf(fmt, args);
    (gdb) # print first argument in "..." list which we know is a char*:
    (gdb) p *(char **)(((char *)args[0].reg_save_area)+args[0].gp_offset)
    $1 = 0x4006f0 "one"
    

    I have not tested all of this, look this link for full solution. This blog will be useful also.

    0 讨论(0)
提交回复
热议问题