Why does my debugger sometimes get worried and do something like not in my code?

When I use my debugger (in my particular case it was QT Creator along with GDB that inspired it) to my C ++ code, sometimes even after calling make clean followed by make , the debugger seems to worry.

Sometimes it seems to line up with a different line number of code and will jump. Sometimes it is disconnected in one line, sometimes it is completely disconnected, and it will jump in steps.

In other cases, this will worry, entering into something I didn’t ask him to enter, for example, when passing a function call, he can go into the initialization procedure of the string, which is part of it.

When I get seg errors, sometimes he can tell me where this happened, and in other cases he can’t even display question marks for which functions are called code and where, and all I see is an assembly, even time as repeating the same code.

It seems I can’t understand the pattern that causes these crashes, and sometimes my debugger behaves fine.

What are the theoretical causes of these debugger debugger errors, and what are the specific steps that I can take to prevent them?

+4
source share
3 answers

There are 3 very common reasons

  • You are debugging optimized code. This rarely works - optimized code can be reordered / inserted / precalculated / etc. to this extent there is no way to map it back to the source code.

  • You are not debugging, for some reason, the binary code corresponding to the current source code.

  • You triggered undefined behavior somewhere - if everything your code did, it got messed up with forests whose debugger needs to be kept reasonable. This is what usually happens when you get segfault and you cannot get a normal stack trace, you overwritten / ruined information (e.g. stack pointers), the debugger should do its job.

And probably hundreds more - from what I personally encounter: debugging multithreaded code; depending on gcc / gdb versions and other things - there were quite a lot of debugger errors.

+10
source

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

But the most common reason that the debugger does not show the correct source location is because the compiler somehow optimized the code, so there is no simple correspondence between the source code and the executable code. The general optimization that confuses debuggers is inlining, and C ++ is very prone to it.

For example, your line initialization initiative was probably included in a function call, as there was only one function with respect to the debugger that started with some line initialization code.

If you track an algorithm error (as opposed to a coding error that causes undefined behavior or concurrency errors), then turning the optimization level down will help you track the error, since the debugger will have a simpler code view.

+2
source

I have the same question as you, and I still can’t solve it. But I chose one solution to the problem, which is to install a virtual machine and install a Unix system into it. And debug it on a Linux system. Maybe it will work.

I figured out the reason, you have to rebuild the project every time you change your code, or Qt just runs the old version of the code.

0
source

All Articles