Debugging C programs in Unix and Windows Environments - Part One ICT106 Fundamentals of Computer Systems Eric Li Debugging and Debugger The process of finding bugs in code called Debugging; A must-have skill for every programmer; Debugger is a utility tool to assist a programmer to debug code; A compulsory component included in Integrated Development Environment (IDE), such as Visual C, Borland Turbo C etc. ICT106_Pract_Week 3 1 ICT106_Pract_Week 3 2 Terminology Breakpoints Stop the execution of the program at a specific source line; Enable to monitor internal changes at runtime; Stepping Walk-through lines in code; Functions: Step Over, Step Into, Step Out; Variable watches Examine variable contents during program execution; GNU gcc and gdb gdb is a command line Unix debugger; Use debugging information generated by gcc: gcc -g source.c -o executable.exe gdb executable.exe ICT106_Pract_Week 3 3 ICT106_Pract_Week 3 4 1
Essential Commands Installing gdb under Cygwin break [file:]function run [arglist] bt print expr c next, step watch info locals help - set breakpoint - run program - print function stack - print the result of expr - continue execution - next instruction/line - watch an expr - local variables - built-in help Go to Cygwin homepage: www.cygwin.com Click on one of the install Cygwin now icons and download setup.exe ICT106_Pract_Week 3 5 ICT106_Pract_Week 3 6 Installation (2) Click Run button Installation (3) Run the program as a local user ICT106_Pract_Week 3 7 ICT106_Pract_Week 3 8 2
Installation (4) Choose Install from Internet Installation (5) Choose Install for Just Me ICT106_Pract_Week 3 9 ICT106_Pract_Week 3 10 Installation (6) - IMPORTANT Select the local package directory (H:\ in Murdoch network) Installation (7) Choose Use IE5 Settings for Internet Connection ICT106_Pract_Week 3 11 ICT106_Pract_Week 3 12 3
Installation (8) Add ICT106 Murdoch Download Site http://ftp.it.murdoch.edu.au/ict106/ict106%20utility%20programs/cygwi Installation (9) Check gdb package under Devel Category nrelease/ ICT106_Pract_Week 3 13 ICT106_Pract_Week 3 14 Using gdb /* File: test2-d.c ICT106 test program Purpose: To demonstrate passing array arguments to a function */ #include <stdio.h> int Sum (int[], int); // prototype int main ( ) { int coins[6] = { 5, 10, 20, 50, 100, 200 }; int sumofcoins; sumofcoins = Sum(coins, 6); printf("\nthe sum of coins is %d\n", sumofcoins); return 0; } // end main int Sum(int A[ ], int size ) { int i = 0; int total = 0; while (i < size) { total = total + A[i]; i = i+1; } /* end while */ return total; } /* end Sum */ ICT106_Pract_Week 3 15 ICT106_Pract_Week 3 16 4
Compile with -g option Note the size difference between a normal executable file and a debug-ready one; Use strip command to remove debug information; The new output file is 3.5KB (even smaller!) Read strip s manual to find out why. ICT106_Pract_Week 3 17 ICT106_Pract_Week 3 18 Using gdb Type gdb lect_3.exe to start Choosing a breakpoint #include <stdio.h> int Sum (int[], int); // prototype int main ( ) { int coins[6] = { 5, 10, 20, 50, 100, 200 }; int sumofcoins; sumofcoins = Sum(coins, 6); // Add a breakpoint HERE printf("\nthe sum of coins is %d\n", sumofcoins); return 0; } // end main ICT106_Pract_Week 3 19 ICT106_Pract_Week 3 20 5
Setting breakpoints Command to add a breakpoint break lect_3.c:13 Execute run lect_3.exe causing the program stops at line 13: Monitoring local variables Option 1: Use info locals to display ALL locally defined variables Or, Option 2: Use print command to show a specified variable 13 sumofcoins = Sum(coins, 6); ICT106_Pract_Week 3 21 ICT106_Pract_Week 3 22 Printing variables Step & Next commands next" - causes the debugger to execute the current line, and stop again. It steps over a function call. "step" - similar to next, but it steps into a function call - break at the beginning of that function. step is useful for debugging nested code. ICT106_Pract_Week 3 23 ICT106_Pract_Week 3 24 6
finish command To step out from a function call. If it is main function, it causes termination of the program ICT106_Pract_Week 3 25 ICT106_Pract_Week 3 26 ICT106_Pract_Week 3 27 7