11

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.

3
  • 2
    If a small function got inlined in many places, this could happen. Commented Feb 5, 2016 at 23:59
  • 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? Commented Feb 6, 2016 at 0:04
  • 1
    Yes. The inline keyword is merely a hint; It is rarely considered seriously by the compiler. Commented Feb 6, 2016 at 0:05

2 Answers 2

15

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

Recommend expanding on this answer to explain why this happens.
I had the same thing to say as the comments. Should I add it in ?
Yes. Answers should be as complete as possible without getting unnecessarily pedantic. If you're borrowing from a comment, credit the commenter. Not the downvoter, by the way. Was going to give you a chance to improve the answer first.
I upvoted, but please flesh this answer out. You don't need to quote me; This was obvious enough that many people, including you and I, would have thought, said and written exactly the same thing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.