0

I have a binary that creates multiple threads during execution. I am trying to debug a flow for which I have inserted printf statements as milestones in code. But strange they are not getting printed on console and via GDB - I am not sure what wrong am I doing and how to correct the same:

./mycore.exe

When I start the flow I do not see my printf strings printed on console - but have a look at the GDB trace:

gdb  ./mycore.exe
break filename:linenum
run
[New Thread 11533.0x282c]
[New Thread 11533.0x283c]
[New Thread 11533.0x21d4]
[New Thread 11533.0x24f8]
[New Thread 11533.0x580]
[New Thread 11533.0x10e0]

[Switching to Thread 11533.0x283c]

Breakpoint 1, fuction ()
    at file.c:623
warning: Source file is more recent than executable.
623     {
(gdb) s
629         .........
(gdb) n
631         printf("reached here\n");
(gdb) n
640             ....................
(gdb) n
641             ....................
(gdb) quit
A debugging session is active.

        Inferior 1 [process 11592] will be killed.

As you can see GDB step flow of the execution did lead to code - print statement introduced by me.

How to resolve the issue - make print the printf on the console?

===================UPDATE=================================

I re-compiled the code and ran again and same problem - still the printf string did not got printed:

[Switching to Thread 5868.0x2c0c]

    Breakpoint 1, fuction ()
        at file.c:623
    623     {
    (gdb) s
    629         .........
    (gdb) n
    631         printf("reached here\n");
    (gdb) n
    640             ....................
    (gdb) n
    641             ....................
    (gdb) quit
Programmer
  • 8,303
  • 23
  • 78
  • 162
  • "warning: Source file is more recent than executable" tells that you are not running latest version of your code, did you recompile executable after introducing print statement? – user7860670 Jan 17 '18 at 08:22
  • 1
    You see that warning message in GDB about "Source file is more recent than executable"? That is quite significant and means the lines shown when debugging might not be what is really being executed. You need to rebuild your program with the updated sources. – Some programmer dude Jan 17 '18 at 08:22
  • Based on "file.c" I would guess that this is C code, not C++. – molbdnilo Jan 17 '18 at 08:30
  • Yes it is a C not C++ and I re-compiled the code and ran again and same problem - still the printf string did not got printed – Programmer Jan 17 '18 at 08:50
  • fflush(stdout) after printf resolved the issue - is there a way to make it by default rather than setting after each printf – Programmer Jan 17 '18 at 08:56
  • You can `fprintf( stderr, ...)` to disable buffering. – ks1322 Jan 17 '18 at 09:05
  • 1
    `setbuf(stdout, NULL);` would disable stdout buffering completely, `setlinebuf(stdout);` would change bufferring mode to per-line. And you indeed maybe can write to `stderr` instead - it is unbuffered by default. – dewaffled Jan 17 '18 at 09:07
  • Possible duplicate of [printf not printing to screen](https://stackoverflow.com/questions/16870059/printf-not-printing-to-screen) – ks1322 Jan 17 '18 at 09:13

2 Answers2

0

Two things.

  1. Because of the message "warning: Source file is more recent than executable." are you sure that you have recompile ?

  2. In the past I got the same behavior. There was a memory leak in the program and using the debugger displace the leak to other place in the code, no output to the console, so I could not use the debugger to find the problem. Use the debugger to check all the vars that could cause a leak, like pointers.

  • I re-compiled the code and ran again and same problem - still the printf string did not got printed - I am sure there is no memory leak curently – Programmer Jan 17 '18 at 08:51
  • 1
    Seems like fflush(stdout) resolved the issue - is there a way to make it by default rather than setting after each printf – Programmer Jan 17 '18 at 08:56
0

You can follow the article we do we have to use fflush(stdout).

https://www.quora.com/Why-do-we-use-the-functions-fflush-stdin-and-fflush-stdout-in-c

You can use the puts instead of printf() for logging on the console on run time, you don't have to add an extra statement for that. alternative.

puts("we are testing fputs if can be used for debugging logs");
Arpan Saini
  • 4,623
  • 1
  • 42
  • 50