0

I'm using Valgrind and GDB to debug a memory leak. I've the following call trace that shows where the memory leak occurs:

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

I want to print the value of the string argument that's passed to Program::add at:

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

How do I do that in GDB?

jeffreyveon
  • 13,400
  • 18
  • 79
  • 129
  • 3
    Put a breakpoint on the first line inside the function, run the program and wait for it to hit the breakpoint, then `print ` should do it. – user4581301 Nov 21 '17 at 07:18
  • Possible duplicate of [How to inspect std::string in GDB with no source code?](https://stackoverflow.com/questions/6776961/how-to-inspect-stdstring-in-gdb-with-no-source-code) – Paul Floyd Nov 21 '17 at 10:17

1 Answers1

1

You can follow step by step Valgrind manual:

If you want to debug a program with GDB when using the Memcheck tool, start Valgrind like this:

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

In another shell, start GDB:

gdb prog

Then give the following command to GDB:

(gdb) target remote | vgdb

Now you can go to frame Program::add(std::string const&) in gdb and print the value of argument.

ks1322
  • 33,961
  • 14
  • 109
  • 164