LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Lost output of printf when using conditional breakpoint in gdb (https://www.linuxquestions.org/questions/programming-9/lost-output-of-printf-when-using-conditional-breakpoint-in-gdb-4175487192/)

sisrnb 12-07-2013 07:35 AM

Lost output of printf when using conditional breakpoint in gdb
 
Version of gdb is 7.4.1-debian.

Just a simple program below:
Code:

  1 #include <stdio.h>
  2
  3 int main() {
  4    int i;
  5    for (i = 0; i < 3; i++) {
  6        printf("i=%d\n", i);
  7        sleep(1);
  8        if (i == 2) i = -1;
  9    }
 10    return 0;
 11 }

Compile and debug using gdb.

When set a breakpoint using command "b 7", run, and then continue if break, there are "i=0", "i=1", "i=2" in output:
Code:

(gdb) b 7
Breakpoint 1 at 0x400571: file hello2.c, line 7.
(gdb) r
Starting program: /home/si/s/__for_vm/p/hello_app/a.out
i=0

Breakpoint 1, main () at hello2.c:7
7                sleep(1);
(gdb) c
Continuing.
i=1

Breakpoint 1, main () at hello2.c:7
7                sleep(1);
(gdb)
Continuing.
i=2

Breakpoint 1, main () at hello2.c:7
7                sleep(1);

However, when set a breakpoint using command "b 7 if i=2", run, and then continue if break, there is only "i=0" in output, no "i=1" and "i=2":
Code:

(gdb) b 7 if i=2
Breakpoint 1 at 0x400571: file hello2.c, line 7.
(gdb) r
Starting program: /home/si/s/__for_vm/p/hello_app/a.out
i=0

Breakpoint 1, main () at hello2.c:7
7                sleep(1);
(gdb) c
Continuing.
i=0

Breakpoint 1, main () at hello2.c:7
7                sleep(1);
(gdb)
Continuing.
i=0

Breakpoint 1, main () at hello2.c:7
7                sleep(1);

Why there is no "i=1" and "i=2" in the output?

NevemTeve 12-07-2013 11:12 AM

for a start, add fflush(stdout); after printf

sisrnb 12-07-2013 08:03 PM

Quote:

Originally Posted by NevemTeve (Post 5076986)
for a start, add fflush(stdout); after printf

Thanks for the reply, NevemTeve!

I tried adding fflush(stdout); after printf, but still I can't see "i=1" and "i=2" in output:
Code:

(gdb) b 8 if i=2
Breakpoint 1 at 0x400620: file hello2.c, line 8.
(gdb) r
Starting program: /home/si/s/__for_vm/p/hello_app/a.out
i=0

Breakpoint 1, main () at hello2.c:8
8                sleep(1);
(gdb) c
Continuing.
i=0

Breakpoint 1, main () at hello2.c:8
8                sleep(1);
(gdb)
Continuing.
i=0

Breakpoint 1, main () at hello2.c:8
8                sleep(1);


NevemTeve 12-08-2013 12:58 AM

Then try command 'next' instead of 'continue'. Also, use command 'display i'.

sisrnb 12-08-2013 04:22 AM

Quote:

Originally Posted by NevemTeve (Post 5077239)
Then try command 'next' instead of 'continue'. Also, use command 'display i'.

I tried, and it helped me found the reason.

Here is the output in terminal:
Code:

(gdb) b 8 if i=2
Breakpoint 1 at 0x400620: file h.c, line 8.
(gdb) r
Starting program: /home/si/s/__for_vm/p/hello_app/a.out
i=0

Breakpoint 1, main () at h.c:8
8                sleep(1);
(gdb) display i
1: i = 2
(gdb) next
9                if (i == 2) i = -1;
1: i = 2
(gdb) next
5            for (i = 0; i < 3; i++) {
1: i = -1
(gdb) next
6                printf("i=%d\n", i);
1: i = 0
(gdb) next
i=0
7                fflush(stdout);
1: i = 0
(gdb) next

Breakpoint 1, main () at h.c:8
8                sleep(1);
1: i = 2
(gdb) next
9                if (i == 2) i = -1;
1: i = 2
(gdb) next
5            for (i = 0; i < 3; i++) {
1: i = -1
(gdb) next
6                printf("i=%d\n", i);
1: i = 0
(gdb) next
i=0
7                fflush(stdout);
1: i = 0

When first stepped to line 'fflush(stdout);', and stepped again.
Something strange happened, value of 'i' became 2, and it shouldn't encounter the breakpoint.
So I was thinking what changed value of 'i', therefore, I recalled that there is 'i=2' in command 'b 8 if i=2'.
Then I tried command 'b 8 if i==2' instead of 'b 8 if i=2', the issue disappeared.
And then I found the tutorial I referenced has such mistake.

Thanks for your help, NevemTeve!


All times are GMT -5. The time now is 01:43 AM.