9

GDB is not showing me the line source after next/stop , and displays only line number and source file , like this :

(gdb) n
7   in test/test.c

whereas I expect it to display the current line , like this :

(gdb) next
17        char * good_message = "Hello, world.";

any settings in .gdbinit that might help me do this ?

KernelMonk
  • 311
  • 1
  • 5
  • 9
  • Possibly a duplicate of http://stackoverflow.com/questions/4362581/get-gdb-to-do-a-list-after-every-step – rturrado Sep 05 '12 at 17:38

2 Answers2

5

whereas I expect it to display the current line , like this

On many platforms, such as ELF, the compiler records both the path to the source (test/test.c in your case), and the compilation directory, allowing GDB to display source regardless of which directory you invoke it in.

But many platforms are less flexible, and don't have a place to record compilation directory. On such platforms (e.g. AIX), you must either start GDB in the compilation directory, or tell it where to look for sources with directory command.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • I was building debian package `exim4` from source (`dpkg-buildpackage`), and it knew path to the source, compilation directory. But somehow compilation directory didn't exist, and I had to descend into directory where path to the source resolved successfully (where executable was put) for it to find the source. By the way, upon reaching the breakpoint my `gdb` said, "exim.c: No such file or directory." Not just "1415 in exim.c" – x-yuri Apr 28 '19 at 22:24
  • Well, it appears the issue was with me trying to take a shortcut. Running `dpkg-buildpackage -T clean && dpkg-buildpackage -T build` in place of just `dpkg-buildpackage`. Because in the latter case compilation directory exists. – x-yuri Apr 28 '19 at 23:09
2

Probably my answer may not be a perfect solution but the way you compile your source program matters. For example in my case if you do g++ fib.cpp -o fib and then try to run gdb fib it won't print the source code with list. Using debug flag g++ -g fib.cpp -o fib and then running with gdb solved my problem.

owsmmj
  • 168
  • 7