Not able to set breakpoints in gdb

2019-07-18 14:15发布

问题:

My C++ project folder structure is as shown below.

I am trying to debug the program using gdb in my linux machine. My main function is in g2o.cpp which is inside g2o_cli folder. I am able to put breakpoints in the files in this folder by

 break g2o.cpp:<line_number>

But I am not able to put breakpoints in the files in other folders, for example, a file optimizer.cpp in the 'core' folder. I tried giving the absolute path from my home directory

break ~/HOME/g2o/core/optimizer.cpp:<line_number>

but it is giving an error

  No source file named ~/HOME/g2o/core/optimizer.cpp

I also tried ../../core/optimizer.cpp instead of the absolute path. Still it did not work.

I saw a similar question here. But none of the possible reasons mentioned in the answer is applicable in my case. The file optimizer.cpp is not a shared library and the entire project was compiled using cmake.

How does gdb take folder paths? How can I give the relative folder path?

回答1:

A dirty hack you can use on x86 is to use int3. Just use the statement asm volatile ("int 3"); in the code where you want the breakpoint.