Hi,
Anyone who can help me to debug a program in Linux environment?
I have heard about gdb but I need some getting started type of help on debugging.
Zahid
Printable View
Hi,
Anyone who can help me to debug a program in Linux environment?
I have heard about gdb but I need some getting started type of help on debugging.
Zahid
gdb is a debugger!
so what do you want to know? debugging techniques? how to use gdb? or something else?
Hi Shade,Quote:
Originally posted by Shade
gdb is a debugger!
so what do you want to know? debugging techniques? how to use gdb? or something else?
Thanks for reply. Yeah.. first I need to know the basic of debugging and then techniques. Then I have to select the tool to debug. Which one is best in linux for c and/or C++?
Hi,
that's a Linux programming topic, isn't it?
Give it a try there.
Have a nice code!
Hi!
I assume you know how to compile programs. To be able to debug your program you have to include the option -g, and debugging is easiest if you do not use optimization options... If you have a (simple) program that crashes, a simple gdb session would look like this:
Conclusion: "buffer" was not initialized (0x0 pointer, because I set all unused pointers to NULL). Happens all the time!Code:$ gcc myprog.c
$ a.out <arguments>
Segmentation fault
$ gcc -g myprog.c
$ gdb a.out
...
(gdb) run <arguments>
Starting program: a.out <arguments>
Program received signal SIGSEGV, Segmentation fault.
0x40083323 in _IO_getline_info () from /lib/libc.so.6
(gdb) bt
#0 0x40083323 in _IO_getline_info () from /lib/libc.so.6
#1 0x4008327c in _IO_getline () from /lib/libc.so.6
#2 0x40082671 in fgets () from /lib/libc.so.6
#3 0x08048605 in main (argc=2, argv=0xbffffb84) at y.c:25
#4 0x4003a65f in __libc_start_main () from /lib/libc.so.6
(gdb) list 25
...
25 fgets( buffer, 256, fp );
...
(gdb) break 25
Breakpoint 1 at 0x80485ef: file y.c, line 25.
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: a.out <arguments>
Breakpoint 1, main (argc=2, argv=0xbffffb84) at y.c:25
25 fgets( buffer, 256, fp );
(gdb) print buffer
$1 = 0x0
(gdb) q
The program is running. Exit anyway? (y or n) y
PS1: type "help" at the gdb prompt
PS2: I assumed that you have the current directory (i.e. ".") in your PATH environment variable (most programmers do this, because in the end the "command not found" messages become very irritating, although it is NOT recommended :) )
PS3: I usually stick with the default "a.out" output... Maybe this is bad practice too, but I have spent a few hours on a project where my program "test" :o would not output anything if ran directly from the command line, but worked perfectly from within gdb...
Thanks alex,
I guess it is helping me to understand debugging.