Download presentation
Presentation is loading. Please wait.
Published byLaurence Gallagher Modified over 9 years ago
1
gdb: GNU Debugger Lecturer: Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall, GRAHAM GLASS, KING ABLES Slides partially adapted from Kumoh National University of Technology (Korea) and NYU
2
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49542 GDB, the GNU Debugger Text-based, invoked with: gdb [ [ | ]] Argument descriptions: executable program file core dump of program (crash records) process id of already running program Compile with –g for debug info (cc -g ) Use -xdb for compatibility with xdb corefiles very useful in post mortem analysis (figuring out what went wrong; why did it crash?) Starting with pid very useful for programs that misbehave non-deterministically (scenario difficult to repeat; problem exhibited sporadically; field troubleshooting)
3
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49543 gdb Example Get reversefirst.c from: http://www.csun.edu/~andrzej/COMP421-F04/source/ch12/reversefirst.c $ cc -g reversefirst.c -o reverse $ gdb reverse HP gdb 3.2 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00. (gdb) help Type "help" followed by a class name for a list of commands in that class. Type "help" followed by command name for full documentation. Command name abbreviations are allowed if unambiguous. (gdb)
4
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49544 Basic GDB Commands General Commands: file [ ]selects as the program to debug run [ ]runs selected program with arguments attach attach gdb to a running process killkills the process being debugged quitquits the gdb program help [ ]accesses the internal help documentation Stepping and Continuing: c[ontinue]continue execution (after a stop) s[tep]step one line, entering called functions n[ext]step one line, without entering functions finishfinish the function and print the return value
5
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49545 GDB Breakpoints Useful breakpoint commands: b[reak] [ ]sets breakpoints. can be a number of things, including a hex address, a function name, a line number, or a relative line offset [r]watch sets a watchpoint, which will break when is written to [or read] info break[points]prints out a listing of all breakpoints clear [ ]clears a breakpoint at d[elete] [ ]deletes breakpoints by number
6
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49546 Playing with Data in GDB Commands for looking around: list [ ] prints out source code at search searches source code for backtrace [ ] prints a backtrace levels deep info [ ] prints out info on (like local variables or function args) p[rint] [ ] prints out the evaluation of Commands for altering data and control path: set sets variables or arguments return [ ] returns from current function jump jumps execution to
7
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49547 gdb - Example (gdb) list 1---> can use “l” for “list” 1 /* REVERSE.C */ 2 3 #include 4 5 /* Function Prototype */ 6 void reverse (); 7 8 /****************************************************************/ 9 10 main () (gdb) l---> same as “list”; continues from the previous 11 12 { 13char str [100]; /* Buffer to hold reversed string */ 14 15reverse ("cat", str); /* Reverse the string "cat" */ 16printf ("reverse (\"cat\") = %s\n", str); /* Display */ 17reverse ("noon", str); /* Reverse the string "noon" */ 18printf ("reverse (\"noon\") = %s\n", str); /* Display */ 19 } 20 (gdb) _
8
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49548 gdb - Example (gdb) break main---> set a breakpoint in function main Breakpoint 1 at 0x29f4: file reversefirst.c, line 15. (gdb) break 16---> set a breakpoint in line 16 (current source) Breakpoint 2 at 0x2a0c: file reversefirst.c, line 16. (gdb) break reverse---> set a breakpoint in function reverse Breakpoint 3 at 0x2a80: file reversefirst.c, line 33. (gdb) info break---> display information on breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x000029f4 in main at reversefirst.c:15 2 breakpoint keep y 0x00002a0c in main at reversefirst.c:16 3 breakpoint keep y 0x00002a80 in reverse at reversefirst.c:33 (gdb) run Starting program: /home/usersNN/userID/reverse/reverse Breakpoint 1, main () at reversefirst.c:15 15reverse ("cat", str); /* Reverse the string "cat" */ (gdb) ontinue---> you can use “c” as well Breakpoint 3, reverse (before=0x40001028 "cat", after=0x7f7f0958 "") at reversefirst.c:33 33len = strlen (before); (gdb) _
9
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-49549 gdb - Example (gdb) backtrace---> show the execution stack #0 reverse (before=0x40001028 "cat", after=0x7f7f0958 "") at reversefirst.c:33 #1 0x2a0c in main () at reversefirst.c:15 (gdb) l 28 { 29 int i; 30int j; 31int len; 32 33len = strlen (before); 34 35for (j = len - 1, i = 0; j >= 0; j--, i++) /* Reverse loop */ 36after[i] = before[j]; 37 (gdb) next---> execute next line 35for (j = len - 1, i = 0; j >= 0; j--, i++) /* Reverse loop */ (gdb) n---> same as “next” 36after[i] = before[j]; (gdb) _
10
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-495410 gdb - Example (gdb) print after[i]---> display data (expression) $1 = 0 '\000' (gdb) p before[j]---> same as “print” $2 = 116 't' (gdb) _ (gdb) n 35for (j = len - 1, i = 0; j >= 0; j--, i++) /* Reverse loop */ (gdb) p after---> print $4 = 0x7f7f0958 "t" (gdb) p before $5 = 0x40001028 "cat" (gdb) c Continuing. Breakpoint 2, main () at reversefirst.c:16 16printf ("reverse (\"cat\") = %s\n", str); /* Display */ 17(gdb) _
11
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-495411 gdb - Example (gdb) n reverse ("cat") = tac 17reverse ("noon", str); /* Reverse the string "noon" */ (gdb) s Breakpoint 3, reverse (before=0x40001030 "noon", after=0x7f7f0958 "tac") at reversefirst.c:33 33len = strlen (before); (gdb) return 0 Make reverse return now? (y or n) y #0 main () at reversefirst.c:18 18printf ("reverse (\"noon\") = %s\n", str); /* Display */ (gdb) p str $4 = "tac", '\000' (gdb) n reverse ("noon") = tac---> this is the output from the program 19 } (gdb) quit $ _
12
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-495412 gdb/C Challenge The reverse program from the next slide is buggy. Copy the files (reverse1.c and reverse1.h) to your local file system; then, compile and run them. Do you see the problem? Can you fix it easily? gdb will help Use gdb to examine program behavior, fix the bug and call me to show the working version.
13
Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-495413 gdb/C Challenge /* REVERSE1.C */ #include #include "reverse1.h" /****************************************************************/ int reverse (str) char *str; { int i; int len; char c; len = strlen (str); for (i = 0; i < len/2; i++) /* Reverse loop */ { c = *str+i; *(str+I) = *str+len-i-1; *(str+len-i-1) = c; } int main() { char str[1024]; printf("Give me a word to reverse: "); scanf("%s", &str); reverse(str); printf("REVERSED: %s\n", str); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.