Debugging: Catching Bugs ( II ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series
What shall we learn today? Introduction –What is debugging –Why do we need debugging –What do you need Trace your code –Debugging tools
Why? The complier will never catch all the bugs –The complier only check syntax errors –It will never check if your code would fulfill your tasks. You are on you own to –make your program correct –make your program robust –make sure what is really going on in your code
What is debugging? Always make sure you know what you are expecting from the code If your program did not output what you expected, tract it down to the exact point You will find the bug! Go ahead and fix it. your code inputoutput Debugging
!!! What is the BEST way to mastering C/C++? Viewing examples? Reading C/C++ syntax? Taking exams? NO. The BEST way is through playing with a debugger!
Debug Tools Debuggers –have full control of the execution of the program set break points check values of variables and structures modify variables dump memories … – depend on platforms UNIX debuggers: e.g., adb, dbx, gdb WIN-PC debugger: e.g., VC++ debugger Integrated development environment (IDE) –Put together editor, complier, linker, debuggers, etc. –e.g., MS Visual Studio
Observe, Guess and Trace You get bugs, but there is no need to panic! It is very normal. How to catch and fix them? –Create testing cases (normal and abnormal) –Observe the run-time error information, intermediate results, stranger behaviors, and printouts –Guess the possible reasons (which needs experience) –Trace down to the troublesome code lines which needs strategy, tool, experience and patient. –Fix the bug, which will be a piece of cake! It is full of fun doing bug-catching.
Setting breakpoints Where do you want to stop and check? How to set breakpoints –at a source-code line –at the beginning of a function –when an expression is true –when a variable changes value –when an expression changes value
Stepping Through Your Code What if you do not want to set so many breakpoints? If you want a careful trace Stepping –run to cursor –step over –step into –step out
Check and Modify Variables How can access those variable? VC++ debugger provides many windows –Output –Variables –Watch –Quick watch –Call stack –Register –Memory –disassembly
Simulating Conditions You can test your functions by simulating some “artificial” conditions Modify the value of a variable in debugger, instead of the source code!
Let’s rock & roll! // functions to check the characters bool IsCharALetter(const char& c) { return( (c>='A' && c ='a' && c<='z') || (c=='_') ); } bool IsCharADigit(const char& c) { return( (c>='0' && c<='9')); // } bool IsCharAOperator(const char& c) { return( c=='+' || c=='-' || c=='*' || c=='/' || c=='='); }
int ReadAPiece(const char* buffer, int& st, int& ed) { int err_code = 1; while(buffer[st]==' ' && buffer[st]!=0)st++; ed = st; if(buffer[st] == 0) err_code = 0; else{ if(IsCharALetter(buffer[st])){ while(IsCharALetter(buffer[ed]) || IsCharADigit(buffer[ed])) ed ++; } else if(IsCharADigit(buffer[st])){ while(IsCharADigit(buffer[ed])) ed ++; } else if(IsCharAOperator(buffer[st])){ while(IsCharAOperator(buffer[ed])) ed ++; } else{ err_code = 0; } // } return err_code; }
#define SIZE_CL_BUFF 100 void main() { char CL_Buffer[SIZE_CL_BUFF]; while(1){ cout >"; cin.getline(CL_Buffer, SIZE_CL_BUFF); if(strcmp(CL_Buffer, "quit")==0){ cout << "Thank you and Bye!" << endl; break; } else{ // main processing int st = 0, ed; char seg[5]; // a very tough bug!!! how to fix it??? (DMA) while(ReadAPiece(CL_Buffer, st, ed)==1){ for(int i=st;i<=ed;i++) seg[i-st] = CL_Buffer[i]; seg[ed-st+1]= '\0'; cout << seg << endl; // }
Testing Cases “a = b + 1” “a=3.14” “dafdafdfasdfadafds = kdfjdkfajdkfjd + 1” …