gcc -g filename.c -o filename
gdb filename
list
break
nset args
arg1 arg2 arg3 ... argn run
print
exprx + 1
, a[3]
, a * b
)quit
Script started on Mon 19 Oct 2009 11:36:03 AM PDT bash-3.2$ ls cmdlinedemo.c bash-3.2$ gcc -g cmdlinedemo.c -o cmdlinedemo bash-3.2$ gdb cmdlinedemo GNU gdb Fedora (6.8-32.fc10) 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 "i386-redhat-linux-gnu"... (gdb) list 7 8 #include <stdio.h> 9 10 int main(int argc, char *argv[]) 11 { 12 int i; 13 14 // echo back argc and argv 15 16 printf("argc=%d\n",argc); (gdb) break 15 Breakpoint 1 at 0x80483d8: file cmdlinedemo.c, line 15. (gdb) set args foo bar fum (gdb) run Starting program: /cs/faculty/pconrad/public_html/cs16/09F/lectures/10.19/cmdlinedemo foo bar fum Breakpoint 1, main (argc=4, argv=0xbffff624) at cmdlinedemo.c:16 16 printf("argc=%d\n",argc); (gdb) print argc $1 = 4 (gdb) print argv[0] $2 = 0xbffff771 "/cs/faculty/pconrad/public_html/cs16/09F/lectures/10.19/cmdlinedemo" (gdb) print argv[1] $3 = 0xbffff7b5 "foo" (gdb) print argv[2] $4 = 0xbffff7b9 "bar" (gdb) print argv[1][0] $5 = 102 'f' (gdb) print argv[1][1] $6 = 111 'o' (gdb) print argv[1][2] $7 = 111 'o' (gdb) print argv[1][3] $8 = 0 '\0' (gdb) print argv[2][0] $9 = 98 'b' (gdb) print argv[2][1] $10 = 97 'a' (gdb) quit The program is running. Exit anyway? (y or n) y bash-3.2$ gdb cmdlinedemo GNU gdb Fedora (6.8-32.fc10) 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 "i386-redhat-linux-gnu"... (gdb) set args foo bar fum (gdb) run Starting program: /cs/faculty/pconrad/public_html/cs16/09F/lectures/10.19/cmdlinedemo foo bar fum argc=4 argv[0]=/cs/faculty/pconrad/public_html/cs16/09F/lectures/10.19/cmdlinedemo argv[1]=foo argv[2]=bar argv[3]=fum Program exited normally. (gdb) quit bash-3.2$ exit exit Script done on Mon 19 Oct 2009 11:38:56 AM PDT
There are lots more gdb commands. The six commands covered in this page—i.e., list
, break
, set args
, run
, print
, quit
—are just the tip of the iceberg, but they are enough to make gdb a very useful tool. As we need more, we'll learn more.
To learn more about these six commands, you can use help
from inside gdb followed by a command name—for example, help list
will give information about the list
command:
(gdb) help list List specified function or line. With no argument, lists ten more lines after or around previous listing. "list -" lists the ten lines before a previous ten-line listing. One argument specifies a line, and ten lines are listed around that line. Two arguments with comma between specify starting and ending lines to list. Lines can be specified in these ways: LINENUM, to list around that line in current file, FILE:LINENUM, to list around that line in that file, FUNCTION, to list around beginning of that function, FILE:FUNCTION, to distinguish among like-named static functions. *ADDRESS, to list around the line containing that address. With two args if one is empty it stands for ten lines away from the other arg. (gdb)
If you cant wait to learn more though, here are some links you may visit. They have more information about gdb than you can possibly make use of in CS16: