9
votes

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.

2
If a small function got inlined in many places, this could happen.Iwillnotexist Idonotexist
Just out of curiosity. Even if I don't tell the compiler to inline a method, it still can based on the analysis it has conducted. Am I correct?flashburn
Yes. The inline keyword is merely a hint; It is rarely considered seriously by the compiler.Iwillnotexist Idonotexist

2 Answers

11
votes

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.

1
votes

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