Gnu Debugger: Gdb, Page 1
Total Page:16
File Type:pdf, Size:1020Kb
CS 310 Gnu Debugger: gdb, Page 1 ' $ GNU Debugger: gdb • A text-based symbolic debugger • Make your program stop on specified conditions. • Examine what has happened, when your program has stopped. • Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another. & % CS 310 Gnu Debugger: gdb, Page 2 ' $ An Example Program: main.cc 1 #include<iostream.h> 2 class Queue { 3 class ListNode { 4 public: 5 int data; 6 ListNode* next; 7 }; 8 ListNode *front, *rear; 9 public: 10 Queue () {front = rear = NULL;}; ~Queue() {}; 11 void add (int new_item); 12 void remove (int& old_front, bool& empty); 13 }; & % CS 310 Gnu Debugger: gdb, Page 3 ' $ 16 void Queue::add (int new_item) 17 { 18 ListNode* p = new ListNode; 19 p->data = new_item; 20 p->next = NULL; 21 22 if (rear==NULL) 23 rear = front = p; 24 else 25 { 26 rear->next = p; 27 rear = p; 28 } 29 } & % CS 310 Gnu Debugger: gdb, Page 4 ' $ 32 void Queue::remove (int& old_front, bool& empty) 33 { 34 empty = (front == NULL); 35 if (empty) 36 return; 37 38 ListNode *p = front; 39 front = front->next; 40 old_front = p->data; 41 delete p; 42 } & % CS 310 Gnu Debugger: gdb, Page 5 ' $ 46 int main () 47 { 48 Queue q; 49 int i; 50 bool empty; 51 52 q.add (1); 53 q.add (2); 54 q.remove (i,empty); 55 q.remove (i,empty); 56 q.add (3); 57 } & % CS 310 Gnu Debugger: gdb, Page 6 ' $ A Debugging Scenario • g++ -g main.cc • gdb a.out ... We now see the gdb prompt (gdb) ... • list command: see program lines list 46,57 (see lines 46 { 57) • break command: set breakpoints, the points at which we would like the program to pause break Queue::add (stop when entering Queue::add) break Queue::remove (stop when entering Queue::remove) break 48 (stop when reaching line 48) & % CS 310 Gnu Debugger: gdb, Page 7 '• run (start executing program) $ ... Program stops at line 18, the first line of Queue::add ... • print command: examine data print front =) 0x0 (whose front?) print rear =) 0x0 • cont (continue) command: resume program execution ... Program execution resumes and stops again at line 18 ... • print front =) 0x20e88 print rear =) 0x20e88 print front->data =) 1 print rear->data =) ? print front->next =) 0x0 cont ... Program execution resumes and stops again at line 34 ... • print front =) 0x20e88 & % CS 310 Gnu Debugger: gdb, Page 8 ' $ print rear =) 0x20e98 print front->data =) 1 print front->next =) 0x20e98 print rear->data =) 2 print rear->next =) 0x0 cont ... Program execution resumes and stops at line 34 ... • print front =) 0x20e98 print rear =) 0x20e98 print front->next =) 0x0 print rear->data =) 2 cont & % CS 310 Gnu Debugger: gdb, Page 9 ' $ ... Program execution resumes and stops at line 18 ... • print q.front =) 0x0 print q.rear =) 0x20e98 cont ... Program crashes at line 26 ... • Other useful command: 1. where { show the calling stack 2. display { automatically print some variables when program paused 3. next, step { single step; execute the next statement and pause & % CS 310 Gnu Debugger: gdb, Page 10 ' $ • More complete information available at: http://www.delorie.com/gnu/docs/gdb/gdb toc.html • When encountering a segmentation fault, you probably screwed up pointers. Run your program within gdb, which will show at which line your program crashes. ☞ do not assume that line is the origin of the problem; keep your mind open • In general, when encountering bugs in your programs, use gdb; the instructor and the TA reserve the right not to answer your questions if you have not done so. & %.