Docsity

Debugging - Networking III-Unix Based Network | CSCI 553, Lab Reports of Computer Systems Networking and Telecommunications

Partial preview of the text

Download Debugging - Networking III-Unix Based Network | CSCI 553 and more Lab Reports Computer Systems Networking and Telecommunications in PDF only on Docsity!

CSCI 414 / 553: Networking III

Unix Network Programming

Spring 2008

Debugging

Today's Agenda

A word about Lab 01 & tasks for class.

Lecture on Debugging

Lab 02 (@ 10:15 till 10:45)

Whats Wrong with Print

Statements

 (^) 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

Symbolic Debuggers

 (^) 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

Kinds of Debuggers

 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

Integrated Development

Environments

 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

Inspecting Values

Use the debugger to set breakpoints in the target

program

 Tells the target program to pause when it reaches particular lines of code 

When the target program is paused, the debugger can

display the contents of its memory

Most debuggers can also evaluate expressions using

the current values of variables

 E.g., type in 2*x<0, debugger displays False

Controlling Execution

The debugger can also:

 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 

Allows you to see:

 How values are changing  Which branches the program is actually taking  Which functions are actually being called 

Debuggers really should be called “inspectors”

Implementing Breakpoints

 (^) 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

Implementing Breakpoints

When the target program reaches the HALT

instruction, it signals the debugger

 (^) Which can then inspect the target program's memory 

When the user wants the program to resume, the

debugger:

 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

Conditional Breakpoints and

Watchpoints

 (^) 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

Logging

 (^) 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

Agan's Rule

Many people make debugging harder than it needs

to be by:

 (^) Using inadequate tools  (^) Not going about it systematically  (^) Becoming impatient 

Agans' Rules [Agans 2002] describe how to apply

the scientific method to debugging

 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

Rule 0: Get it Right the First

Time

 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)