Thread: How can I debug a program in linux?

  1. #1
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531

    Thumbs up How can I debug a program in linux?

    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

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    gdb is a debugger!

    so what do you want to know? debugging techniques? how to use gdb? or something else?
    Hope you don't mind my bad english, I'm Austrian!

  3. #3
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Originally posted by Shade
    gdb is a debugger!

    so what do you want to know? debugging techniques? how to use gdb? or something else?
    Hi Shade,
    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++?

  4. #4
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Hi,
    that's a Linux programming topic, isn't it?
    Give it a try there.

    Have a nice code!

  5. #5
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    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:

    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
    Conclusion: "buffer" was not initialized (0x0 pointer, because I set all unused pointers to NULL). Happens all the time!

    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" would not output anything if ran directly from the command line, but worked perfectly from within gdb...

  6. #6
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Thanks alex,
    I guess it is helping me to understand debugging.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Can't compile an SDL program under Linux
    By dwks in forum Game Programming
    Replies: 2
    Last Post: 01-16-2006, 08:51 PM
  3. Compiling a C program in Linux
    By hern in forum C Programming
    Replies: 14
    Last Post: 06-28-2004, 08:33 PM
  4. A C++ program in Solaris that cannot be ported to Linux
    By tvenki in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2004, 12:13 PM
  5. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM