Debugging

This is a discussion on Debugging within the C Programming forums, part of the General Programming Boards category; What flags do I compile my program with so I can debug it? And how do I run the debugger ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    15

    Debugging

    What flags do I compile my program with so I can debug it? And how do I run the debugger from a unix terminal? Can anyone direct me to a site with the basic commands once running the debugger? Need to trace a segmentation fault, thanks!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by cecil_rabmere View Post
    What flags do I compile my program with so I can debug it?
    With gcc, add -g:
    Code:
    gcc -g program.c -o program
    You can also get useful warnings like so:
    Code:
    gcc -W -Wall -ansi -pedantic -g program.c -o program
    And how do I run the debugger from a unix terminal?
    You can run gdb like this.
    Code:
    gdb program
    After compiling "program", of course.

    Can anyone direct me to a site with the basic commands once running the debugger?
    http://www.bgsu.edu/departments/compsci/docs/gdb.html
    Just search for "gdb commands" or some such.

    Need to trace a segmentation fault, thanks!
    Good luck . . . you can post the code, if you want to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    A quick example:
    Code:
    $ cat seg.c
    #include <stdio.h>
    
    int get_num(int array[], int x) {
        return 2 * array[x];
    }
    
    int main() {
        int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int x, sum = 0;
    
        for(x = 0; x < (int)(sizeof array); x --) {  /* oops */
            sum += get_num(array, x);
        }
    
        return 0;
    }
    $ gcc -g seg.c -o seg
    $ ./seg
    Segmentation fault
    $ gdb ./seg
    GNU gdb 6.7.1-debian
    Copyright (C) 2007 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-linux-gnu"...
    Using host libthread_db library "/lib/libthread_db.so.1".
    (gdb) run
    Starting program: seg 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000400460 in get_num (array=0x7fffb25329a0, x=-19049) at seg.c:4
    4	    return 2 * array[x];
    (gdb) backtrace
    #0  0x0000000000400460 in get_num (array=0x7fffb25329a0, x=-19049) at seg.c:4
    #1  0x00000000004004d0 in main () at seg.c:12
    (gdb) list
    1	#include <stdio.h>
    2	
    3	int get_num(int array[], int x) {
    4	    return 2 * array[x];
    5	}
    6	
    7	int main() {
    8	    int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    9	    int x, sum = 0;
    10	
    (gdb) frame 1
    #1  0x00000000004004d0 in main () at seg.c:12
    12	        sum += get_num(array, x);
    (gdb) list
    7	int main() {
    8	    int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    9	    int x, sum = 0;
    10	
    11	    for(x = 0; x < (int)(sizeof array); x --) {  /* oops */
    12	        sum += get_num(array, x);
    13	    }
    14	
    15	    return 0;
    16	}
    (gdb) p array
    $1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    (gdb) p x
    $2 = -19049
    (gdb) quit
    The program is running.  Exit anyway? (y or n) y
    $
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 09:48 AM
  2. Problem in debugging in Eclipse
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 08-21-2007, 09:53 AM
  3. Debugging Dev C++
    By tuurb046 in forum Tech Board
    Replies: 10
    Last Post: 08-16-2007, 12:51 PM
  4. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  5. debugging directx apps
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 08-16-2003, 08:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21