1. 13

  2. 5

    Part of the reason why it took us awhile to debug our issue was that we assumed that the stack trace we saw was accurate.

    Recall how it was compiled:

    clang -std=c99 -O3 -g -o inline_merge inline_merge.c

    As the GCC manual says with respect to combinging O with g: “The shortcuts taken by optimized code may occasionally produce surprising results.”

    1. 4

      And relatedly from the clang man page:

      Note that Clang debug information works best at -O0.

      1. 1

        So one should recompile for the purpose of debugging?

        1. 7


          GCC has -Og, which turns on all optimizations that can’t affect debugging.

          1. 2

            Or design your software in such a way that the stacktrace is not needed for debugging. Which is hard, for sure, but the current trend of depending on stacktraces for everything (Java, Python, for example) is a bit too extreme IMO. For a comparison, in Ocaml I tend to use a result type monad for things that can fail at which point the compiler makes sure I do something with all errors.

      2. -3

        Nice article