But you do realise that adding printf() calls to the code can also
change,
for example, the memory layout that the compiler uses, so certain
memory
allocation bugs might become more or less easily triggerable?
This is a big deal especially debugging code that fails with -O3 but
succeeds
otherwise.
My approach (shamelessly stolen from GSL) is to write a debug callable
and let your debugger perform the "printf", rather than your own code.
$ cat oopsie.c
/* define an extern iff debugging */
#ifdef DEBUG
void oopsie(char *s,...){ }
#endif
And in your main code:
...
/* define an extern iff debugging */
#ifdef DEBUG
void oopsie(char *s,...);
#else
void oopsie(char *s,...){ }
#endif
int main(){
int i, j;
printf("%s\n","this is main.c demonstrating debug printing");
i = rand();
if(i> 5) oopsie("i more than 5");
...
Compile with DEBUG on. Run your code with the debugger, and break on
oopsie.
$ gdb ./main
...
(gdb) break oopsie
(gdb) run
Starting program: /home/jal/debug/a.out
...
this is main.c demonstrating debug printing
...
Breakpoint 1, oopsie (s=0x9766035d548 "i more than 5") at oopsie.c:4
4 void oopsie(char *s,...){ }
Backtrace tells you where oopsie was called.
J