Eclipse CDT setup for valgrind + gdb

2020-07-14 09:46发布

问题:

How can I debug a valgrind run with gdb in Eclipse?

I start the program like this:

Terminal A:

valgrind vgdb=yes --vgdb-error=0 ./a.out

It can debug it from another terminal like this:

Terminal B:

gdb ./a.out
target remote |vgdb

But I can't get this to work in Eclipse... So, I want to do graphically in Eclipse what I can do via command line in terminal B.

I tried many ways to set up debug configurations in Eclipse, but I can't get it to work.

The closest I got was via "C++ Attach to Application", in which I loaded a customized .gdbinit according to this post at the end: https://www.eclipse.org/forums/index.php/t/681832/

After starting that configuration, the valgrind run continues (to the next error), but Eclipse then reports that the application terminated, and it also terminates the gdb session.

Two more links that might be helpful: https://bugs.eclipse.org/bugs/show_bug.cgi?id=269687 http://www.eclipse.org/forums/index.php/t/354700/,

I am using Eclipse 4.5.1 (Mars.1)

EDIT:

I made some progress- I found two ways that almost work as I'd wish:

1) I use the "C/C++ Application" setting with this in .gdbinit:

file /path/to/a.out
target remote | /usr/bin/vgdb
set sysroot /
define run

Note that I had to specifically add the "file" statement to gdbinit. The drawback in this case is that Eclipse is waiting for the program to terminate forever when I try to quit/kill (because it never actually started running it...). I have to click "Terminate and Remove" manually afterwards.

Would be nice if you can tell me a trick/hack how to prevent that.

2) I use "C++ Attach to Application" with the same .gdbinit from above. Eclipse will ask me for the process to attach to; I search for and select valgrind. I can now step through the code, but I cannot set breakpoints within Eclipse. I can do that only in the gdb shell via "break file.cpp:line".

Any ideas how to resolve that?

回答1:

Not my favorite solution, but its working on Ubuntu/Eclipse:

Theses are my steps:

Create an "External Tool":

  • Name: Example Starter
  • Location: /usr/bin/valgrind
  • Arguments: --vgdb=full --vgdb-error=0 --leak-check=full -v ./MyProg.elf argument1 ...

Create a customized gdb init file

Content of File gdb_vgdb_init:

target remote | /usr/bin/vgdb
set sysroot /
define run

Create a new Debug Configuration

  • Type: C/C++ Application
  • Name: Example Debugger
  • C/C++ Application: MyProg.elf
  • Arguments -> Programm Arguments: No Arguments needed
  • Debugger -> GDB command file: /path/to/gdb_vgdb_init

Create a new Launch Group

The new Launch Group should contain:

  1. Created External Tool Example Starter (in run mode)
  2. Created C/C++ Application Example Debugger (in debug mode)

Afterwards the Launch Group can be started. the Launchgroup will start a valgrind incl. gdb connected to it.