GDB:打印内存位置中函数参数的值

时间:2017-11-21 06:50:15

标签: c++ gdb valgrind

我正在使用Valgrind和GDB来调试内存泄漏。我有以下调用跟踪,显示内存泄漏发生的位置:

(gdb)  monitor block_list 10104
==961== 153 (18 direct, 135 indirect) bytes in 1 blocks are definitely lost in loss record 10,104 of 10,317
==961==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==961==    by 0x678199: create_node(unsigned char const*, unsigned int, document*) (a.cpp:436)
==961==    by 0x67933A: insert(art_node*, art_node**, unsigned char const*, unsigned int, document*, unsigned int, int, int*) (a.cpp:704)
==961==    by 0x68327B: Program::add(std::string const&) (program.cpp:84)
==961==    by 0x7220BF: main (main.cpp:52)

我想打印传递给Program::add的字符串参数的值:

==961==    by 0x68327B: Program::add(std::string const&) (program.cpp:84)

我如何在GDB中这样做?

1 个答案:

答案 0 :(得分:0)

您可以按照步骤Valgrind manual

进行操作
  

如果您想在使用Memcheck工具时使用GDB调试程序,   像这样开始Valgrind:

valgrind --vgdb=yes --vgdb-error=0 prog
     

在另一个shell中,启动GDB:

gdb prog
     

然后将以下命令提供给GDB:

(gdb) target remote | vgdb

现在你可以转到gdb中的Program::add(std::string const&)框架并打印参数的值。