ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!!
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.
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
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:
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
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:
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)
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!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.