1 / 15

Debugging Tips for Programmers

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

said
Download Presentation

Debugging Tips for Programmers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Debugging Tips for Programmers

  2. Outline • Script debugging • C shell • BASH/Bourne/Korn shell tips • 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 • 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.

  5. 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.

  6. 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?

  7. 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

  8. 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>

  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.

  10. 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

  11. 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

  12. 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!

  13. 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

  14. 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.

  15. 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

More Related