LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-07-2010, 12:20 AM   #1
jeremy28
Member
 
Registered: Sep 2009
Posts: 48

Rep: Reputation: 15
Problem in debugging with "GDB"?


Hi all;

I'm using ubuntu 9.04.

I've started using "gdb" to debug my project line by line and have not worked with command
line debugger yet. So, I'm not familiar with this type of debugging!

When I write "gdb ./myprogram" in terminal, I see:

Code:
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
And when I write "b 20" to set a breakpoint at 20th line of code, I see:
Code:
Breakpoint 1 at 0x8048150: file ../sysdeps/i386/elf/start.S, line 20.
Afterwards, when I run "r" command, I expect that the program runs till 20th line and then I can write "n" command line by line to see the parameters' values and memory contents, but I face with the program's complete running without any stop on line 20!!

What is the solution please?!

TIA.
 
Old 07-07-2010, 12:57 AM   #2
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
Wrong file ... your binary does not consist only of your own source file but also of some parts of the GCC runtime. When you don't specify a file for your breakpoint, the first one encountered is used, in that case start.S, which is common startup code in assembly language.

Just write "b foobar.c:20" or use a symbol (aka function name) for defining the breakpoint.
 
Old 07-07-2010, 06:19 AM   #3
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
Compile the program with the option:
-g

Prefered option not to use while debugging
-O

More information with gdb:
http://geocities.ws/murugesan/technical/gdb/
or
http://murugesan.webnode.com/technical/gdb/


If the statement where the break is given, is inside a conditional loop, it may not enter that location.
Try to give the location before that line.

To start from main use
gdb) break main
 
Old 07-18-2010, 12:07 AM   #4
jeremy28
Member
 
Registered: Sep 2009
Posts: 48

Original Poster
Rep: Reputation: 15
Hi and thanks of your help!

When I write "b myprogram.cpp:20" in gdb, I see the result:
(while, I'm in myprogram.cpp directory, of course!)

Code:
No source file named myprogram.cpp.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (myprogram.cpp:20) pending.
And then, I type the "r" command, but the program runs completely without any stop on line 20!

What is the reason please?!

THX.
 
Old 07-19-2010, 02:59 AM   #5
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
An example:

Assume that the source file myprogram.cpp is in the following directory:
/home/murugesandins/.Murugesan/src/myprogram.cpp
Compile the program using the following commands:
 
Old 07-19-2010, 03:08 AM   #6
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
An example:

Assume that the source file myprogram.cpp is in the following directory:
Code:
/home/murugesandins/.Murugesan/src/myprogram.cpp
Compile the program using the following commands:
cd /home/murugesandins/.Murugesan/src/
g++ -g myprogram.cpp
cd /home/murugesandins/.Murugesan/
(gdb) /home/murugesandins/.Murugesan/src/a.out --directory=/home/avay/.Murugesan/tcp/src
(gdb) break myprogram.cpp:20
(gdb) run
Starting program: /home/murugesandins/.Murugesan/src/a.out
line1:1
line2:1
line3:1
line4:1
line5:1
line6:1
line7:1
line8:1
line9:1
line10:1
line11:1
line20 is not set to 20:6
Program exited normally.
The reason is because of the following source code:
Code:
$ grep -n $ /home/murugesandins/.Murugesan/src/myprogram.cpp
1:#include <iostream>
2:using namespace std ;
3:int main()
4:{
5: int line1=1, line2=2, line3=3, line4=4, line5=5, line6=6, line7=7, line8=8, line9=9, line10=10, line11=11, line20=06 ;
6: cout << "line1:" << line1 << "\n" ;
7: cout << "line2:" << line1 << "\n" ;
8: cout << "line3:" << line1 << "\n" ;
9: cout << "line4:" << line1 << "\n" ;
10: cout << "line5:" << line1 << "\n" ;
11: cout << "line6:" << line1 << "\n" ;
12: cout << "line7:" << line1 << "\n" ;
13: cout << "line8:" << line1 << "\n" ;
14: cout << "line9:" << line1 << "\n" ;
15: cout << "line10:" << line1 << "\n" ;
16: cout << "line11:" << line1 << "\n" ;
17:
18: if ( 20 == line20 )
19: {
20: cout << "line20:" << line20 << "\n" ;
21: }
22: else
23: {
24: cout << "line20 is not set to 20:" << line20 << "\n" ;
25: }
26: return 0 ;
27:}

Hence set the break point at specified location.



When you get the following error:
Code:
No source file named myprogram.cpp.
It means the gdb is not able to find the location of source code. Hence it is assuming to take it while loading the shared library.


Kindly read the manual of gdb/ use the following command:
Code:
gdb --help

Last edited by murugesan; 07-19-2010 at 05:13 AM. Reason: Using BB Code List.
 
Old 07-19-2010, 03:09 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by zirias View Post
Wrong file ... your binary does not consist only of your own source file but also of some parts of the GCC runtime. When you don't specify a file for your breakpoint, the first one encountered is used, in that case start.S, which is common startup code in assembly language.

Just write "b foobar.c:20" or use a symbol (aka function name) for defining the breakpoint.
But simple b 7 works for me !
See the below code:
Code:
anisha@linux-uitj:~/junk> cat r.c
#include <stdio.h>

int main ()
{
     int i = 98;
        // HD, Figure 3-1
        i |= (i >>  1);
        i |= (i >>  2);
        i |= (i >>  4);
        i |= (i >>  8);
        i |= (i >> 16);

        printf ("%d", (i - (i >> 1)));
        //return i - (i >> 1);


}
anisha@linux-uitj:~/junk> gcc -g r.c
anisha@linux-uitj:~/junk> gdb a.out
GNU gdb (GDB) SUSE (6.8.91.20090930-2.4)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-suse-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/anisha/junk/a.out...done.
(gdb) b 7
Breakpoint 1 at 0x400543: file r.c, line 7.
(gdb) r
Starting program: /home/anisha/junk/a.out
Missing separate debuginfo for /lib64/ld-linux-x86-64.so.2
Try: zypper install -C "debuginfo(build-id)=591af1afa33f255704fb6a60859b93d00e205302"
Missing separate debuginfo for /lib64/libc.so.6
Try: zypper install -C "debuginfo(build-id)=c5a3dfd66bf61fcdec9bc22153b2fbd0d6697960"

Breakpoint 1, main () at r.c:7
7               i |= (i >>  1);
(gdb)
 
Old 07-19-2010, 03:19 AM   #8
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by jeremy28 View Post
When I write "gdb ./myprogram" in terminal, I see:

And when I write "b 20" to set a breakpoint at 20th line of code, I see:
Code:
Breakpoint 1 at 0x8048150: file ../sysdeps/i386/elf/start.S, line 20.
Afterwards, when I run "r" command, I expect that the program runs till 20th line and then I can write "n" command line by line to see the parameters' values and memory contents, but I face with the program's complete running without any stop on line 20!!

What is the solution please?!

TIA.
May be you did not compile your program like :
Code:
gcc -g myprogram.c
Notice the -g option ?
 
Old 07-19-2010, 03:26 AM   #9
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
murugesan

Read the following link to make life easier for others:
http://www.linuxquestions.org/questi....php?do=bbcode

I couldn't read you post !
 
Old 07-19-2010, 04:03 AM   #10
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
Using BB Code List.

Last edited by murugesan; 07-19-2010 at 05:23 AM. Reason: Using BB Code List.
 
Old 07-19-2010, 04:09 AM   #11
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
murugesan

One more gentle suggestion:

You could have modified your post number 6 by clicking the Edit button rather than posting it again
 
  


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
Debugging Problem in Gdb akm3 Programming 5 01-01-2009 10:03 AM
"No symbol table is loaded." when ddd is remote debugging with gdbserver powah Linux - Software 1 01-18-2008 09:54 AM
Debugging Wine - Segmentation faults during dlopen("wined3d.dll.so"). ErV Programming 0 06-27-2007 06:39 AM
"glibc detected -> program aborted" plz help in debugging.. rs_vijay Programming 6 11-22-2006 12:16 AM
gdb assembly dump "i386-slackware-linux" centr0 Linux - General 0 06-11-2003 11:44 AM

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

All times are GMT -5. The time now is 09:20 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