Lewis Chan
Lewis Chan

Reputation: 787

gdb set breakpoints in c++ header file?

I have a future.hh header file, and I set a breakpoint at line 800, like the following:

   795      ~future() {
   796          if (_promise) {
   797              _promise->_future = nullptr;
   798          }
   799          if (failed()) {
   800              report_failed_future(state()->get_exception());
   801          }

I thought if exception occurred at future destruction, I could get the stacktrace. However I got this: enter image description here

This is not what I want. Why is that ? So many breakpoints. When I do continue, it will stop every time, not what I expected.

Upvotes: 0

Views: 2294

Answers (1)

Tom Tromey
Tom Tromey

Reputation: 22519

When you make a request like break future.hh:800, gdb attempts to set a breakpoint at every possible address corresponding to that source location.

In your case, what is most likely happening is that the destructor has been inlined many times, so you wind up with very many breakpoint locations. (Another less likely option here is a compiler bug causing it to emit incorrect line tables somehow.)

Compiling without optimization won't really help -- it may result in fewer breakpoint locations, but you will still see just as many stops, because all that is happening is a stop on each invocation of the destructor.

Instead, if you know that you only want to stop in certain destructors, then the best approach is to try to narrow the points at which the stops happen. A few ideas:

  • Put a breakpoint in the surrounding code you care about, not in this destructor

  • Disable some or most of the locations on the breakpoint. (In gdb, breakpoints can be individually disabled.)

  • Make the breakpoint conditional to try to reduce the number of undesired stops

Upvotes: 1

Related Questions