Download presentation
Presentation is loading. Please wait.
Published byMiranda Rice Modified over 9 years ago
1
Debugging Tips for Programmers
2
Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language debugging –GNU debugger (GDB) –DDD –Valgrind
3
Basic Programming Cycle You’re supposed to start
4
C Shell Shell provides nice user interface features Shell provides nice user interface features –Can do cool things to make command line environment work for you. –However, “serious programming in C shell” is an oxymoron Interpreter has bugs and weird quirks and can cause loss of hair, insanity, excessive weight gain, hypertension, LDL,HDL. In other words, you age faster. Interpreter has bugs and weird quirks and can cause loss of hair, insanity, excessive weight gain, hypertension, LDL,HDL. In other words, you age faster.
5
C-Shell Programming? Good for quick and small tasks Good for quick and small tasks Setting up your preferences, e.g. $HOME/.cshrc Setting up your preferences, e.g. $HOME/.cshrc See http://www.gregor.com/dgregor/csh_whynot.html for more details of why you shouldn’t do serious programming in C shell. See http://www.gregor.com/dgregor/csh_whynot.html for more details of why you shouldn’t do serious programming in C shell. http://www.gregor.com/dgregor/csh_whynot.html BASH seems to have caught-up with C shell as far as friendliness of user- interface and is much better for programming. BASH seems to have caught-up with C shell as far as friendliness of user- interface and is much better for programming.
6
Bourne/Korn/BASH shells Widely available Widely available Coding & Test can go very fast – logic & control flow generally not complex Coding & Test can go very fast – logic & control flow generally not complex Consequently, methods for debugging aren’t too sophisticated: Consequently, methods for debugging aren’t too sophisticated: echo statement echo statement -x & -v flags -x & -v flags Any others? Any others?
7
Bourne/Korn/BASH shell programming ‘-v’ flag shows each line in your script during processing ‘-v’ flag shows each line in your script during processing ‘-x’ flag shows each command as evaluated by the shell interpreter ‘-x’ flag shows each command as evaluated by the shell interpreter IMHO, most useful when both are combined, i.e. –xv IMHO, most useful when both are combined, i.e. –xv
8
Bourne/Korn/BASH shell programming Use ‘-xv’ flag at beginning of script to see everything. For example Use ‘-xv’ flag at beginning of script to see everything. For example % /bin/ksh –xv #!/bin/sh -xv
9
Bourne/Korn/BASH shell programming If output too voluminous, consider placing set –xv/set +xv pairs that bracket the code that you’re interested in debugging. If output too voluminous, consider placing set –xv/set +xv pairs that bracket the code that you’re interested in debugging.
10
Example of selective debugging % test.sh Hello MDL if [ “X$DISPLAY” = “X” ] then echo DISPLAY is not set fi + ‘[‘ Xtyr:48.0 = X ‘]’ set +xv + set +xv Goodbye #!/bin/sh echo Hello MDL set –xv if [ “X$DISPLAY” = “X” ] then echo DISPLAY not set fi set +xv echo Goodbye
11
Debugging Compiled Programs Need the “-g” flag in the compilation line Need the “-g” flag in the compilation line –For Makefiles, you can use the command line to override predefined macros, e.g., –If you know the buggy routine, you can debug just that w/o recompiling the entire program. % make myprogram CFLAG=-g
12
Debugging Compiled Programs Development cycle is longer, algorithms more complex Development cycle is longer, algorithms more complex For new programs, using the debugger to watch the code as it executes can catch a lot of errors efficiently. For new programs, using the debugger to watch the code as it executes can catch a lot of errors efficiently. For established programs, the debugger can help you find and fix problems rapidly, as long as the code was compiled with –g flag! For established programs, the debugger can help you find and fix problems rapidly, as long as the code was compiled with –g flag!
13
GDB - GNU debugger For AWIPS, it’s the tool to use for C, C++ and FORTRAN code For AWIPS, it’s the tool to use for C, C++ and FORTRAN code DDD is a wrapper around GDB and provides a “friendly” face to users. DDD is a wrapper around GDB and provides a “friendly” face to users. /usr/bin/ddd /usr/bin/ddd /usr/bin/gdb /usr/bin/gdb GDB Interpreter GUI
14
Essential GDB commands Set breakpoints “b” stop execution Set breakpoints “b” stop execution Print “p” print/change values Print “p” print/change values Next “n” step over code Next “n” step over code Step “s” step into code Step “s” step into code Continue “c” run to exit or next “b” Continue “c” run to exit or next “b” DDD allows all this using mouse clicks and button pushes. DDD allows all this using mouse clicks and button pushes. See PDF or handout for more detailed listing of GDB commands. See PDF or handout for more detailed listing of GDB commands.
15
Debugging Scenarios Your program “hangs” Your program “hangs” –Use debugger to “attach” to running process This stops program and use debugger to find the infinite loop by stepping through code or backtrace This stops program and use debugger to find the infinite loop by stepping through code or backtrace –Send a signal ABRT to process to generate core dump file (in most instances) % kill –ABRT infiniteloop Core dumped
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.