Presentation is loading. Please wait.

Presentation is loading. Please wait.

Debugging and Profiling With some help from Software Carpentry resources.

Similar presentations


Presentation on theme: "Debugging and Profiling With some help from Software Carpentry resources."— Presentation transcript:

1 Debugging and Profiling With some help from Software Carpentry resources

2 Introduction You're going to spend half your coding life debugging So you should learn how to do it systematically Talk about tools first They'll make everything else less painful Techniques discussed in handouts

3 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-consuming And (if you're using Java, C++, or Fortran) all that recompiling…

4 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

5 Symbolic Debuggers A debugger is a program that runs another program on your behalf why symbolic? While the target program (or debuggee) is running, the debugger can:target programdebuggee 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

6 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

7 Sample

8 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

9 Command-Line Debuggers Many of today's debuggers are GUIs wrapped around older command-line debuggers Most widely used of these is GDB GDB Supports many languages, on many platforms But no one ever said it was easy to learn

10 Debugger Use Cases Cases: 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 debuggingpost mortem debugging When a program fails badly, it creates a core dumpcore dump Load that dump into the debugger, and see where the program was when it terminated Not as good as watching it run…

11 Debugger Uses Setting breakpoints Setting conditional breakpoints Stepping through one line at a time Stepping into functions Stepping over functions

12 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”

13 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 waylogging Separate 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

14 Logging Levels Every system is different, but the following are fairly standard DEBUG : only want to see it when debugging a problem INFO : information about normal operations WARNING : something that a human being should pay attention to ERROR : something has gone wrong inside the software CRITICAL : something has gone very wrong inside the software System is about to crash, reactor is about to melt down, etc.

15 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

16 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

17 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

18 Example

19 Profiling limitations Profiler itself eats up time Slows 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


Download ppt "Debugging and Profiling With some help from Software Carpentry resources."

Similar presentations


Ads by Google