I've set a breakpoint in one of the .h files which has an implementation of a small method,
(gdb) break SmallVector.h:141
And here is what I got from gdb:
Breakpoint 5 at 0x416312: SmallVector.h:141. (38 locations)
Why is the breakpoint set in 38 locations instead of a single location?
I'm not new to debugging as well as C++ but unfortunately I've never worked with anything complex like I'm working now (compiler). So I've never encountered anything like this before.
Any help is appreciated.
To do this, just type "break [functionname]". gdb will stop your program just before that function is called. Breakpoints stay set when your program ends, so you do not have to reset them unless you quit gdb and restart it. Examining data When your program is stopped you can examine or set the value of any variable.
You can create a breakpoint at an offset from the current stopped position with gdb breakpoint +<offset> . You can also create a breakpoint on a specific line number using either gdb break <linenumber> (for the current source file) or gdb break <filename>:<linenumber> (for a file other than the current file).
Just press c. It will continue execution until the next breakpoint. You can also disable intermediate breakpoints by using disable #breakpointnumber as stated here.
GDB normally ignores breakpoints when it resumes execution, until at least one instruction has been executed. If it did not do this, you would be unable to proceed past a breakpoint without first disabling the breakpoint. This rule applies whether or not the breakpoint already existed when your program stopped.
There are several ways that this can happen.
One main way, as you've found, is an inline function. Some compilers (like gcc) will emit debugging information about the inlining it has done. gdb sees this information and will try to set a breakpoint at every inlined location.
Another typical way for this to happen is with templates. Each template instantiation will have the same location, so break file:line
will result in a breakpoint in every instantiation.
Yet another way for this to happen is if you use break function
and there are multiple functions of the same name. One scenario here that often confuses new users is that, under the hood, the compiler often emits multiple copies of a constructor (look up "in charge constructor" for the gory details).
One final way this can happen is if the compiler does other sorts of optimization, like partial inlining. These are more rarely seen.
This happens to me whenever I add a breakpoint in a header file with a template implementation..
The answer would be that every time there is an inline function call the breakpoint will be set at that location, this often happens with template implementations in a header file :)
To force a function to be inline you will have to specify an __attribute__
function flag for the compiler! For example
#include <iostream>
using std::cout;
using std::endl;
__attribute__ ((always_inline))
inline void function() {
cout << "Hello World" << endl;
}
int main() {
cout << "Hello World" << endl;
return 0;
}
Credits to @IwillnotexistIdonotexist
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With