Gdb: GNU Debugger Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition, Prentice-Hall,

Slides:



Advertisements
Similar presentations
Introduction to the Omega Server CSE Overview Intro to Omega Basic Unix Command Files Directories Printing C and C++ compilers GNU Debugger.
Advertisements

Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization.
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
DEBUGGING IN THE REAL WORLD : Recitation 4.
C Programming Tools Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition,
Ar: Unix Archiver Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
CSE 303 Lecture 13a Debugging C programs
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Awk challanges Prof. Andrzej (AJ) Bieszczad Phone: awk Challenges Find out what the following awk scripts do: END.
C Slides and captured lecture (video and sound) are available at:
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
Debugger Presented by 李明璋 2012/05/08. The Definition of Bug –Part of the code which would result in an error, fault or malfunctioning of the program.
Gdb is the GNU debugger on our CS machines. gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With gcc and.
Homework Reading Programming Assignments
Debugging Cluster Programs using symbolic debuggers.
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
1 SEEM3460 Tutorial Unix Introduction. 2 Introduction Unix-like system is everywhere Linux Android for smartphones Google Chrome OS for Chromebook Web.
1 ENERGY 211 / CME 211 Lecture 13 October 20, 2008.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Lecture 5 Software Development. Types of Development Tools Archiving: tar, cpio, pax, RPM Configuration: autoconf Compilation and building: make Managing.
Some Basics && GDB overview Ram Sheshadri –
Programmable Text Processing with awk Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users”
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
Adv. UNIX: debug/141 Advanced UNIX v Objectives of these slides: –tools and techniques for debugging C code Special Topics in Comp. Eng.
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
Debugging Xin Tong. GDB GNU Project debugger Allows you to see what is going on `inside' another program while it executes or crashed. (Faster than printing.
1 SEEM3460 Tutorial Compiling and Debugging C programs.
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
Debugging Computer Networks Sep. 26, 2007 Seunghwan Hong.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
S OME USEFUL D EBUG C OMMANDS FOR C LEAR -S PEED S OFTWARE D EVELOPMENT K IT -- COMMANDS FROM CHAP.7 By: Pallav Laskar.
CVS: Concurrent Version System Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
CSI605 Introduction to ddd. ddd ddd stands for the Data Display Debugger ddd is a graphical environment that resides on top of gdb We recall that gdb.
Make: File Dependency System Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third.
cs33201 Chapter 12 C Programming Tools Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003.
COP 3530 Spring 12 Discussion Session 1. Agenda 1.Introduction 2.Remote programming 3.Separate code 4.Compile -- g++,makefile 5.Debug -- gdb 6.Questions?
CMSC 202 Lesson 15 Debugging. Warmup  What is the bug in the following code? int* foo(int a) { return &a; }
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
17/02/2016S. Ponce / EP-LBC1 Debugging Under Linux Sebastien Ponce Friday, 8 March 2002.
Lab 9 Department of Computer Science and Information Engineering National Taiwan University Lab9 - Debugging I 2014/11/4/ 28 1.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
HP-SEE Debugging with GDB Vladimir Slavnic Research Assistant SCL, Institute of Physics Belgrade The HP-SEE initiative.
C 프로그래밍 개발 도구. 컴파일러 gnu c compiler 컴파일 gcc sample.c 실행 a.out 실행 파일 지정 컴파일 gcc –o sample sample.c 실행 sample.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 4, 2010 CSCE 212Honors Computer Organization.
Lecture 6 UNIX Development Tools. Software Development Tools.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Using the GNU Debugger (GDB)‏ Techzemplary Pvt.Ltd February 24 th 2008 Pranav Peshwe.
Institute of Radio Physics and Electronics ILug-Cal Introduction to GDB Institute of Radio Physics and Electronics and Indian GNU/Linux Users Group Kolkata.
Ar: Unix Archiver Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
DEBUG.
Debugger Ritu Chaturvedi
CSCE 212Honors Computer Organization
Debugging with gdb gdb is the GNU debugger on our CS machines.
gdb gdb is the GNU debugger on our CS machines.
Debuggers.
Common C Programming Errors, GDB Debugging
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
GNU DEBUGGER TOOL. What is the GDB ? GNU Debugger It Works for several languages – including C/C++ [Assembly, Fortran,Go,Objective-C,Pascal]
When your program crashes
Homework Reading Programming Assignments Finish K&R Chapter 1
ETE 132 Lecture 6 By Munirul Haque.
CSE 303 Concepts and Tools for Software Development
Chapter 15 Debugging.
CSCE 212Honors Computer Organization
Debugging.
Presentation transcript:

gdb: GNU Debugger Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “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

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: 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)

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: gdb Example Get reversefirst.c from: $ cc -g reversefirst.c -o reverse $ gdb reverse HP gdb 3.2 for PA-RISC 1.1 or 2.0 (narrow), HP-UX (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)

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: 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

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: 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

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: 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

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: 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 { 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) _

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: 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=0x "cat", after=0x7f7f0958 "") at reversefirst.c:33 33len = strlen (before); (gdb) _

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: gdb - Example (gdb) backtrace---> show the execution stack #0 reverse (before=0x "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) _

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: 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 = 0x "cat" (gdb) c Continuing. Breakpoint 2, main () at reversefirst.c:16 16printf ("reverse (\"cat\") = %s\n", str); /* Display */ 17(gdb) _

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: gdb - Example (gdb) n reverse ("cat") = tac 17reverse ("noon", str); /* Reverse the string "noon" */ (gdb) s Breakpoint 3, reverse (before=0x "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 $ _

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: 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.

Ch. 12. C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: 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); }