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!
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!
With gcc, add -g:
You can also get useful warnings like so:Code:gcc -g program.c -o program
Code:gcc -W -Wall -ansi -pedantic -g program.c -o programYou can run gdb like this.And how do I run the debugger from a unix terminal?
After compiling "program", of course.Code:gdb program
http://www.bgsu.edu/departments/compsci/docs/gdb.htmlCan anyone direct me to a site with the basic commands once running the debugger?
Just search for "gdb commands" or some such.
Good luck . . . you can post the code, if you want to.Need to trace a segmentation fault, thanks!
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.
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.