150 likes | 344 Views
Debugging Tips for Programmers. Outline. Script debugging C shell BASH/Bourne/Korn shell tips Compiled language debugging GNU debugger (GDB) DDD Valgrind. Basic Programming Cycle. You’re supposed to start. C Shell. Shell provides nice user interface features
E N D
Outline • Script debugging • C shell • BASH/Bourne/Korn shell tips • Compiled language debugging • GNU debugger (GDB) • DDD • Valgrind
Basic Programming Cycle You’re supposed to start
C Shell • 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.
C-Shell Programming? • Good for quick and small tasks • Setting up your preferences, e.g. $HOME/.cshrc • See http://www.gregor.com/dgregor/csh_whynot.htmlfor more details of why you shouldn’t do serious programming in C shell. • BASH seems to have caught-up with C shell as far as friendliness of user-interface and is much better for programming.
Bourne/Korn/BASH shells • Widely available • Coding & Test can go very fast – logic & control flow generally not complex • Consequently, methods for debugging aren’t too sophisticated: • echo statement • -x & -v flags • Any others?
Bourne/Korn/BASH shell programming • ‘-v’ flag shows each line in your script during processing • ‘-x’ flag shows each command as evaluated by the shell interpreter • IMHO, most useful when both are combined, i.e. –xv
Bourne/Korn/BASH shell programming • Use ‘-xv’ flag at beginning of script to see everything. For example #!/bin/sh -xv % /bin/ksh –xv <your script typed here>
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.
Example of selective debugging #!/bin/sh echo Hello MDL set –xv if [ “X$DISPLAY” = “X” ] then echo DISPLAY not set fi set +xv echo Goodbye % test.sh Hello MDL if [ “X$DISPLAY” = “X” ] then echo DISPLAY is not set fi + ‘[‘ Xtyr:48.0 = X ‘]’ set +xv + set +xv Goodbye
Debugging Compiled Programs • 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
Debugging Compiled Programs • 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 established programs, the debugger can help you find and fix problems rapidly, as long as the code was compiled with –g flag!
GDB - GNU debugger • 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. • /usr/bin/ddd • /usr/bin/gdb
Essential GDB commands • Set breakpoints “b” stop execution • Print “p” print/change values • Next “n” step over code • Step “s” step into code • Continue “c” run to exit or next “b” • DDD allows all this using mouse clicks and button pushes. • See PDF or handout for more detailed listing of GDB commands.
Debugging Scenarios • 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 • Send a signal ABRT to process to generate core dump file (in most instances) % kill –ABRT infiniteloop Core dumped