87

I want to set a break point in gdb on file service.cpp on line 45 and I do:

gdb> break service.cpp:45

The problem is that there are many service.cpp files in my application and it is not picking the one I am interested in. How can I specify the right service.cpp file?

Yuan Wen
  • 1,583
  • 3
  • 20
  • 38
WilliamKF
  • 41,123
  • 68
  • 193
  • 295

3 Answers3

126

Specify the full path:

gdb> break /Full/path/to/service.cpp:45
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 4
    If I set a function name, like `service.cpp:main` for some reason it finds the wrong one even with a full path (it finds the service.cpp thats located in the project root directory), not sure why this is happening – SSH This Feb 29 '12 at 18:32
  • @SSH This You have multiple main functions? – Mathieu Borderé Jun 07 '17 at 14:57
  • 3
    Sorry this was about 5 years ago, I can't remember :( – SSH This Jun 07 '17 at 18:11
  • Does not work for me on Windows Subsystem for Linux. Error message: `"No source file named /Full/path/to/service.cpp"` even if `!ls -l /Full/path/to/service.cpp` works fine on the gdb prompt. – Tobias Mar 06 '19 at 16:00
  • @Tobias - maybe try a windows path? Might depend on what view of the filesystem your debugger has in that context. There is probably a way to turn on some more logging, too? I'm not a gdb expert on that side of things though. – Carl Norum Mar 06 '19 at 17:48
  • 2
    By full path, do you mean, A) the full path from /, B), the path from the binary to the source file, C) the path from the current working directory to the source file, D) the path from where the gdb command was ran to the source file, E) something else? I tried A and D but it didn't seem to work, it always said the source file didn't exist. – jrh Jul 07 '20 at 14:31
  • Just FYI, I think the issue I ran into is a bug in GDB 9.1 – jrh Jul 07 '20 at 16:38
2

In addition to @Carl 's answer,
In case anyone gets No source file named /Full/path/to/service.c like @Tobias, make sure to add the debugging -g flag to main and the source file you want to access, like main.c and foo.c.

Then make sure main uses the -g compiled operable file, wich might mean you'd have to update and relink a library, for instance.

banmosh
  • 21
  • 1
  • This is nothing to do with the Q and doesn't answer anything, Hypothetical issues are not an SO thing ;) – RichieHH Mar 04 '21 at 17:29
  • I wanted to properly comment this solution to the same problem someone else had on the main answers' thread but lacked the minimal reputation, so I just added as another answer. My mistake was trying to access manually linked libraries (compiled without `-g`) on gdb and I got the same "No source file named /Full/path/to/foo.cpp" error as reported above, it was just a matter of recompiling w gdb flag and relinking files. But I couldn't comment that without 50 rep – banmosh Mar 08 '21 at 21:51
1

Just give same trailing components that differs the file from others :

filename:linenum Specifies the line linenum in the source file filename. If filename is a relative file name, then it will match any source file name with the same trailing components. For example, if filename is `gcc/expr.c', then it will match source file name of /build/trunk/gcc/expr.c, but not /build/trunk/libcpp/expr.c or /build/trunk/gcc/x-expr.c.

https://www.zeuthen.desy.de/dv/documentation/unixguide/infohtml/gdb/Specify-Location.html

Erhan
  • 55
  • 5
  • 2
    The accepted answer is much more clear and you basically saying the same, just more complicated and with more words. – FrankM Mar 31 '21 at 15:11
  • No. The accepted answer gives full path solution. What I wrote is relative path solution. – Erhan Apr 01 '21 at 19:50