What are the best methods for finding a bug in a C program that only appears in an optimized build

My program uses the library of the third part, which causes some segmentation error. I tried to compile the library with debugging symbols and without compiler optimization, and the crash disappeared. My suspect is that compiler optimization has detected this error. What are the best methods for debugging such cases?

EDIT - (the above statement is corrected: "detected" instead of "called")

I think they misunderstood me. I had no intention of blaming the compiler or anything like that. I just asked the best practices to find an error in a situation where I do not have debugging symbols in a third-party library (a reverse trace of failure leads to a third-party library).

+5
source share
7 answers

What you describe is pretty common. And that was almost never a mistake in optimizing the compiler. Optimization does a lot for your code. Variables are reordered / optimized, etc. If you have a buffer overflow, this can lead to a memory overflow, which is not a big deal in the debug assembly, but this memory is very important in the optimization assembly.

valgrind - , .

+8

, . , , Undefined , , Undefined . . UB ... . :

  • OutOfBounds
+8

, , , . (ulimit -c unlimited, ). gdb, , .

valgrind, valgrind --db-attatch=yes, , . / Segfault, , .

,

+4

, , , . , , .

, . , . , , , .

, , .

, , . , , , , - .

+2

, gdb . , , , (.. , , ..)

strace , ? printf - , strace.

, , , . , ? gdb - .

+2

, .

, , , segfault. . , , . , printf , , .

;)

0

, , undefined -. , UB , segfault ( ).

Every time this happened to me (which is not so common), the reason was a buffer overflow somewhere else in the code. However, I never developed a repeatable, generally applicable technique for finding a problem (unless you want to name the clock passing through the debugger and swear in the generally applicable technique).

0
source

All Articles