LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2013, 07:35 AM   #1
sisrnb
LQ Newbie
 
Registered: Dec 2013
Distribution: debian wheezy
Posts: 23

Rep: Reputation: Disabled
Question 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?
 
Old 12-07-2013, 11:12 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
for a start, add fflush(stdout); after printf
 
Old 12-07-2013, 08:03 PM   #3
sisrnb
LQ Newbie
 
Registered: Dec 2013
Distribution: debian wheezy
Posts: 23

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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);
 
Old 12-08-2013, 12:58 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Then try command 'next' instead of 'continue'. Also, use command 'display i'.
 
Old 12-08-2013, 04:22 AM   #5
sisrnb
LQ Newbie
 
Registered: Dec 2013
Distribution: debian wheezy
Posts: 23

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by NevemTeve View Post
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] GDB Breakpoint Internals rupeshkp728 Programming 5 07-18-2012 06:31 AM
Problem setting breakpoint on function with GDB Ganahim Programming 4 02-24-2011 08:03 AM
[SOLVED] How to cancel a breakpoint in gdb? PJYang2009 Linux - Software 3 06-02-2010 01:43 AM
[gdb]how to exec cmd when breakpoint was hit jackandking Linux - Software 1 12-08-2008 11:49 PM
gdb breakpoint doesnt work? Thinking Programming 3 09-29-2005 06:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration