Debugging Techniques
Total Page:16
File Type:pdf, Size:1020Kb
Debugging Techniques Mike Johanson (modified by Stef Nychka) Department of Computing Science University of Alberta CMPUT 201, Fall 2005 M. Johanson Debugging Techniques Introduction Part I Introduction M. Johanson Debugging Techniques Introduction Segmentation fault; core dumped. As you have probably found through your work on Assignment 1.. ...debugging C programs can be frustrating. It is very easy to write outside of arrays bounds, use uninitialized values, use inappropriate data types, etc. gcc errors and warnings are not always helpful to beginners (or, sometimes, to experienced programmers!) M. Johanson Debugging Techniques Introduction Segmentation fault; core dumped. As you have probably found through your work on Assignment 1.. ...debugging C programs can be frustrating. It is very easy to write outside of arrays bounds, use uninitialized values, use inappropriate data types, etc. gcc errors and warnings are not always helpful to beginners (or, sometimes, to experienced programmers!) M. Johanson Debugging Techniques Introduction Segmentation fault; core dumped. As you have probably found through your work on Assignment 1.. ...debugging C programs can be frustrating. It is very easy to write outside of arrays bounds, use uninitialized values, use inappropriate data types, etc. gcc errors and warnings are not always helpful to beginners (or, sometimes, to experienced programmers!) M. Johanson Debugging Techniques Introduction Segmentation fault; core dumped. As you have probably found through your work on Assignment 1.. ...debugging C programs can be frustrating. It is very easy to write outside of arrays bounds, use uninitialized values, use inappropriate data types, etc. gcc errors and warnings are not always helpful to beginners (or, sometimes, to experienced programmers!) M. Johanson Debugging Techniques Introduction If your program has a runtime bug: If you are lucky, your program will crash and you will realize you have a problem If you are unlucky, it will run, and you may not realize for a long time that you have a problem - if at all M. Johanson Debugging Techniques Introduction If your program has a runtime bug: If you are lucky, your program will crash and you will realize you have a problem If you are unlucky, it will run, and you may not realize for a long time that you have a problem - if at all M. Johanson Debugging Techniques Introduction If your program has a runtime bug: If you are lucky, your program will crash and you will realize you have a problem If you are unlucky, it will run, and you may not realize for a long time that you have a problem - if at all M. Johanson Debugging Techniques Introduction Motivations for this talk In the first half of this talk, we will cover some simple tips: Macros to make your debugging printouts more useful #ifdef - to display debugging output only when you need it assert() - to find errors before they happen In the second half, we will explain how to use the gdb and ddd debugger programs M. Johanson Debugging Techniques Introduction Motivations for this talk In the first half of this talk, we will cover some simple tips: Macros to make your debugging printouts more useful #ifdef - to display debugging output only when you need it assert() - to find errors before they happen In the second half, we will explain how to use the gdb and ddd debugger programs M. Johanson Debugging Techniques Introduction Motivations for this talk In the first half of this talk, we will cover some simple tips: Macros to make your debugging printouts more useful #ifdef - to display debugging output only when you need it assert() - to find errors before they happen In the second half, we will explain how to use the gdb and ddd debugger programs M. Johanson Debugging Techniques Introduction Motivations for this talk In the first half of this talk, we will cover some simple tips: Macros to make your debugging printouts more useful #ifdef - to display debugging output only when you need it assert() - to find errors before they happen In the second half, we will explain how to use the gdb and ddd debugger programs M. Johanson Debugging Techniques Introduction Motivations for this talk In the first half of this talk, we will cover some simple tips: Macros to make your debugging printouts more useful #ifdef - to display debugging output only when you need it assert() - to find errors before they happen In the second half, we will explain how to use the gdb and ddd debugger programs M. Johanson Debugging Techniques Simple debugging tips Part II Simple debugging tips M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly Using macros like LINE If you do use printf() to debug, you can make your output more useful: printf(‘‘Error on line %d in file %s\n’’, __LINE__, __FILE__); M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly LINE printf(‘‘Error on line %d in file %s\n’’, __LINE__, __FILE__); printf(‘‘Program compiled at %s on %s\n’’, __TIME__, __DATE__); During compilation, the compiler replaces LINE and FILE with the line number and file name. This is called a macro (King, pg 287) Now, your printf() statement tells you where in the code it is By printing out the time and date that the program was compiled, you can be sure that you are working with the most recent copy of your program M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly LINE printf(‘‘Error on line %d in file %s\n’’, __LINE__, __FILE__); printf(‘‘Program compiled at %s on %s\n’’, __TIME__, __DATE__); During compilation, the compiler replaces LINE and FILE with the line number and file name. This is called a macro (King, pg 287) Now, your printf() statement tells you where in the code it is By printing out the time and date that the program was compiled, you can be sure that you are working with the most recent copy of your program M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly LINE printf(‘‘Error on line %d in file %s\n’’, __LINE__, __FILE__); printf(‘‘Program compiled at %s on %s\n’’, __TIME__, __DATE__); During compilation, the compiler replaces LINE and FILE with the line number and file name. This is called a macro (King, pg 287) Now, your printf() statement tells you where in the code it is By printing out the time and date that the program was compiled, you can be sure that you are working with the most recent copy of your program M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly LINE printf(‘‘Error on line %d in file %s\n’’, __LINE__, __FILE__); printf(‘‘Program compiled at %s on %s\n’’, __TIME__, __DATE__); During compilation, the compiler replaces LINE and FILE with the line number and file name. This is called a macro (King, pg 287) Now, your printf() statement tells you where in the code it is By printing out the time and date that the program was compiled, you can be sure that you are working with the most recent copy of your program M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly Using #ifdef What if you have some useful debugging printf() statements, but you don’t want to add and remove them all the time? int foo(int x, int y) { #ifdef DEBUG printf(‘‘x is %d, y is %d\n’’, x, y); #endif } M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly int foo(int x, int y) { #ifdef DEBUG printf(‘‘x is \%d, y is \%d\n’’, x, y); #endif } To include this code, compile your program with -DDEBUG (yes, two D’s) gcc -DDEBUG -o myprogram myprogram.c To turn off the output (especially when handing in the assignment), stop compiling with -DDEBUG When the compiler reaches that line, it ignores everything between #ifdef and #endif M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly int foo(int x, int y) { #ifdef DEBUG printf(‘‘x is \%d, y is \%d\n’’, x, y); #endif } To include this code, compile your program with -DDEBUG (yes, two D’s) gcc -DDEBUG -o myprogram myprogram.c To turn off the output (especially when handing in the assignment), stop compiling with -DDEBUG When the compiler reaches that line, it ignores everything between #ifdef and #endif M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly int foo(int x, int y) { #ifdef DEBUG printf(‘‘x is \%d, y is \%d\n’’, x, y); #endif } To include this code, compile your program with -DDEBUG (yes, two D’s) gcc -DDEBUG -o myprogram myprogram.c To turn off the output (especially when handing in the assignment), stop compiling with -DDEBUG When the compiler reaches that line, it ignores everything between #ifdef and #endif M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly int foo(int x, int y) { #ifdef DEBUG printf(‘‘x is \%d, y is \%d\n’’, x, y); #endif } To include this code, compile your program with -DDEBUG (yes, two D’s) gcc -DDEBUG -o myprogram myprogram.c To turn off the output (especially when handing in the assignment), stop compiling with -DDEBUG When the compiler reaches that line, it ignores everything between #ifdef and #endif M. Johanson Debugging Techniques Using macros like LINE Simple debugging tips Using #ifdef Using assert() properly assert() If your program has a bug, would you rather: Have it crash right away, so you can use a debugger Keep running until the bug causes a crash Keep running without crashing, and you may not notice ...probably the first one! There is a useful (but easy to abuse) function called assert() that can help you check for “this-should-never-happen” cases.