





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















Debugging
(^) Many people still debug by adding print statements to their programs (^) It's error-prone (^) Adding print statements is a good way to add typos (^) Particularly when you have to modify the block structure of your program (^) And time-consuming (^) All that typing… (^) And (if you're using Java, C++, or Fortran) all that recompiling… (^) And can be misleading (^) Moves things around in memory, changes execution timing, etc. (^) Common for bugs to hide when print statements are added, and reappear when they're removed
(^) A debugger is a program that runs another program on your behalf (^) Sometimes called a symbolic debugger because it shows you the source code you wrote, rather than raw machine code (^) While the target program (or debugee) is running, the debugger can: (^) Pause, resume, or restart the target (^) Display or change values (^) Watch for calls to particular functions, changes to particular variables, etc. (^) Do not need to modify the source of the target program! (^) Depending on your language, you may need to compile it with different flags (^) And yes, the debugger modifies the target's layout in memory, and execution speed… (^) …but a lot less than print statements… (^) …with a lot less effort from you
There may be several ways to get into the debugger (^) Launch the debugger, load the target program, and start work (^) Run the debugger with the target program as a command-line argument (^) Switch into debugging mode in the middle of an interactive session (^) Sometimes also do post mortem debugging (^) When a program fails badly, it creates a core dump (^) Copies all of its internal state to a file on disk (^) Load that dump into the debugger, and see where the program was when it terminated (^) Not as good as watching it run… (^) …but sometimes the best you can do
Debuggers are usually part of Integrated Development Environments (IDEs) (^) These usually contain many other tools as well, including: (^) A class browser that presents an outline of the project's modules, classes, functions, variables, etc. (^) A code assistant that presents context-sensitive help and documentation (^) Tools like this are available for every modern language Microsoft Visual Studio on Windows Eclipse
Tells the target program to pause when it reaches particular lines of code
E.g., type in 2*x<0, debugger displays False
Single-step (i.e., execute one statement at a time) (^) Step into function calls (^) Step over them, or (^) Run to the end of the current function
How values are changing Which branches the program is actually taking Which functions are actually being called
(^) To set a breakpoint on a particular line, the debugger: (^) Figures out which instructions were produced for the statement on that line (^) Copies the first of those instructions to a safe place (^) Replaces it with a HALT instruction
(^) Which can then inspect the target program's memory
Puts the instruction at the breakpoint location back in place Tells the program to execute that instruction Replaces the instruction with a HALT once again (^) Tells the program to run normally
(^) The program only stops at a conditional breakpoint if some condition is met (^) E.g., loop index greater than 100, or filename argument is None (^) Much more efficient than single-stepping from the start of the program Some debuggers also support watchpoints (^) Have the debugger watch every write to memory (^) Halt when anything, anywhere, modifies a particular variable (^) Slow the program down a lot (^) Typically a factor of 100 or more (^) But sometimes the only practical way to find out when a particular list value is being overwritten
(^) Sometimes printing is the right thing to do (^) Collecting information for later analysis (e.g., web server (^) Not expecting anything to go wrong, but want to be able to trace execution leading up to fault if one does occur Many systems use logging to record information in a structured, manageable way (^) Separate different levels of information (^) Debugging vs. warning vs. critical (^) Separate information about different things (^) Login/logout vs. backup (^) Send information to different destinations (^) Files vs. database vs. sys admin's pager
(^) Using inadequate tools (^) Not going about it systematically (^) Becoming impatient
Observe a failure Invent a hypothesis explaining the cause (^) Test the hypothesis by running an experiment (i.e., a test) (^) Repeat until the bug has been found
The simplest bugs to fix are the ones that don't exist Design, reflect, discuss, then code (^) “A week of hard work can sometimes save you an hour of thought.” Design and build your code with testing and debugging in mind (^) Minimize the amount of “spooky action at a distance” (^) Minimize the number of things programmers have to keep track of at any one time (^) Train yourself to do things right, so that you'll code well even when you're tired, stressed, and facing a deadline (^) “Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” (Brian Kernighan)