How to set break point on one file of a project which has many files with same name?

GdbBreakpointsAmbiguity

Gdb Problem Overview


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?

Gdb Solutions


Solution 1 - Gdb

Specify the full path:

gdb> break /Full/path/to/service.cpp:45

Solution 2 - Gdb

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.

Solution 3 - Gdb

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWilliamKFView Question on Stackoverflow
Solution 1 - GdbCarl NorumView Answer on Stackoverflow
Solution 2 - GdbbanmoshView Answer on Stackoverflow
Solution 3 - GdbErhanView Answer on Stackoverflow