Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb, breakpoint in multiple locations

Tags:

c++

debugging

gdb

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.

like image 270
flashburn Avatar asked Feb 05 '16 23:02

flashburn


People also ask

How do I break an address in GDB?

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.

How do you create a breakpoint on a specific line?

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).

How do I skip to the next breakpoint in GDB?

Just press c. It will continue execution until the next breakpoint. You can also disable intermediate breakpoints by using disable #breakpointnumber as stated here.

Why is GDB not stopping at breakpoint?

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.


2 Answers

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.

like image 192
Tom Tromey Avatar answered Sep 21 '22 00:09

Tom Tromey


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

like image 36
Curious Avatar answered Sep 24 '22 00:09

Curious