Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my debugger sometimes freak out and do things like not line up with my code?

Tags:

c++

debugging

When I'm using my debugger (in my particular case, it was QT Creator together with GDB that inspired this) on my C++ code, sometimes even after calling make clean followed by make the debugger seems to freak out.

Sometimes it will seem to be lined up with another piece of code's line numbers, and will jump around. Sometimes this is is off by one line, sometimes this is totally off and it'll jump around erratically.

Other times, it'll freak out by stepping into things I didn't ask it to step into, like while stepping over a function call, it might step into the string initialization routine that is part of it.

When I get seg faults, sometimes it's able to tell me where it happened perfectly, and other times it's not even able to display question marks for which functions called the code and from where, and all I see is assembly, even while running the exact same code repeatedly.

I can't seem to figure out a pattern to what causes these failures, and sometimes my debugger is perfectly well behaved.

What are the theoretical reasons behind these debugger freak outs, and what are the concrete steps I can take to prevent them?

like image 758
Nantucket Avatar asked Dec 12 '22 20:12

Nantucket


2 Answers

There's 3 very common reasons

  • You're debugging optimized code. This rarely works - optimized code can be reordered/inlined/precomputed/etc. to the point there's no chance whatsoever to map it back to the source code.

  • You're not debugging, for whatever reason, the binary matching the current source code.

  • You've invoked undefined behavior somewhere - if whatever stuff your code did, it has messed around with the scaffolding the debugger needs to keep its sanity. This is what usually happens when you get a segfault and you can't get a sane stack trace, you've overwritten/messed with the information(e.g. stack pointers) the debugger needs to do its job.

And probably hundreds more - of the stuff I personally encounter is: debugging multithreaded code; depending on gcc/gdb versions and various other things - there's been quite a handful debugger bugs.

like image 134
nos Avatar answered May 29 '23 03:05

nos


One possible reason is that debuggers are as buggy as any other program!

But the most common reason for a debugger not showing the right source location is that the compiler optimized the code in some way, so there is no simple correspondence between the source code and the executable code. A common optimization that confuses debuggers is inlining, and C++ is very prone to it.

For example, your string initialization routine was probably inlined into the function call, so as far as the debugger was concerned, there was just one function that happened to start with some string initialization code.

If you're tracking down an algorithm bug (as opposed to a coding bug that produces undefined behavior, or a concurrency bug), turning the optimization level down will help you track the bug, because the debugger will have a simpler view of the code.

like image 21
Gilles 'SO- stop being evil' Avatar answered May 29 '23 02:05

Gilles 'SO- stop being evil'