Upload
dylan-daniels
View
216
Download
0
Embed Size (px)
Citation preview
Debugging and Profiling
With some help from Software Carpentry resources
Introduction
You're going to spend half your coding life debuggingSo you should learn how to do it systematicallyTalk about tools first They'll make everything else less
painful
Techniques discussed in handouts
What's Wrong with Print Statements
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-consumingAnd (if you're using Java, C++, or Fortran) all that recompiling…
Print Statements Can Be Misleading
Print statements may not even help you! 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 DebuggersA debugger is a program that runs another program on your behalfwhy symbolic?While the target program (or debuggee) 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
Debugger Features
Interactive debuggers typically show: The source code The call stack The values of variables that are currently in
scope I.e., global variables, parameters to the current
function call, and local variables in that function A panel displaying what your program has
printed to standard output and/or standard error
Sample
Range of GUI Debuggers
GUI debuggers are in common use for Windows C, C++ Java MATLAB Microsoft frameworks (C#, .NET) many others
Interface is usually tied to operating system
Command-Line Debuggers
Many of today's debuggers are GUIs wrapped around older command-line debuggersMost widely used of these is GDBSupports many languages, on many platformsBut no one ever said it was easy to learn
Debugger Use CasesCases: 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 Load that dump into the debugger, and see where
the program was when it terminated Not as good as watching it run…
Debugger Uses
Setting breakpoints Setting conditional breakpoints
Stepping through one line at a timeStepping into functionsStepping over functions
What it lets you do
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”
Logging
Sometimes printing is the right thing to do Collecting information for later analysis (e.g., web
server logs)
Many systems use logging to record information in a structured, manageable waySeparate different levels of information Debugging vs. warning vs. critical
Separate information about different things Login/logout vs. backups
Send information to different destinations Files vs. database vs. sys admin's pager
Logging LevelsEvery system is different, but the following are fairly standardDEBUG: only want to see it when debugging a problemINFO: information about normal operationsWARNING: something that a human being should pay attention toERROR: something has gone wrong inside the softwareCRITICAL: something has gone very wrong inside the software System is about to crash, reactor is about to melt down,
etc.
Making It Faster
Once your code is running correctly, you want it to run faster Law: get it running first, then get it
running faster "We should forget about small
efficiencies, say about 97% of the time: premature optimization is the root of all evil." – Donald Knuth
Profiling
You will guess wrong about what makes your code slow commonly true even for experts,
guaranteed for novices
Profilers let you track total run time run time per-function number of calls of each function trace of how you got to function calls
Profilers
gprof is the gnu profiler works with C, C++, Fortran
other profilers exist for Windows, other languages Since profiling is a run-then-check
output operation, command-line profiler is fine
Example
Profiling limitations
Profiler itself eats up timeSlows execution (sometimes dramatically)For C, C++, needs to be included at compile time Not done often use when you code has stabilized, and you are
ready to look for run-time savings can't use with code inside a pre-compiled
library