19
Debugging and Profiling With some help from Software Carpentry resources

Debugging and Profiling With some help from Software Carpentry resources

Embed Size (px)

Citation preview

Page 1: Debugging and Profiling With some help from Software Carpentry resources

Debugging and Profiling

With some help from Software Carpentry resources

Page 2: 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

Page 3: Debugging and Profiling With some help from Software Carpentry resources

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…

Page 4: Debugging and Profiling With some help from Software Carpentry resources

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

Page 5: Debugging and Profiling With some help from Software Carpentry resources

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

Page 6: Debugging and Profiling With some help from Software Carpentry resources

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

Page 7: Debugging and Profiling With some help from Software Carpentry resources

Sample

Page 8: Debugging and Profiling With some help from Software Carpentry resources

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

Page 9: Debugging and Profiling With some help from Software Carpentry resources

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

Page 10: Debugging and Profiling With some help from Software Carpentry resources

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…

Page 11: Debugging and Profiling With some help from Software Carpentry resources

Debugger Uses

Setting breakpoints Setting conditional breakpoints

Stepping through one line at a timeStepping into functionsStepping over functions

Page 12: Debugging and Profiling With some help from Software Carpentry resources

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”

Page 13: Debugging and Profiling With some help from Software Carpentry resources

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

Page 14: Debugging and Profiling With some help from Software Carpentry resources

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.

Page 15: Debugging and Profiling With some help from Software Carpentry resources

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

Page 16: Debugging and Profiling With some help from Software Carpentry resources

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

Page 17: Debugging and Profiling With some help from Software Carpentry resources

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

Page 18: Debugging and Profiling With some help from Software Carpentry resources

Example

Page 19: Debugging and Profiling With some help from Software Carpentry resources

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