debugging tips for programmers. outline script debugging script debugging –c shell...

15
Debugging Tips for Debugging Tips for Programmers Programmers

Upload: miranda-rice

Post on 25-Dec-2015

231 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Debugging Tips for Debugging Tips for ProgrammersProgrammers

Page 2: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

OutlineOutline

• Script debuggingScript debugging– C shellC shell– BASH/Bourne/Korn shell tipsBASH/Bourne/Korn shell tips

• Compiled language debuggingCompiled language debugging– GNU debugger (GDB)GNU debugger (GDB)– DDDDDD– ValgrindValgrind

Page 3: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Basic Programming CycleBasic Programming Cycle

Coding

Documentation

Understand Problem

Debugging

You’re supposed to start

Page 4: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

C ShellC Shell

• Shell provides nice user interface Shell provides nice user interface featuresfeatures– Can do cool things to make command line Can do cool things to make command line

environment work for you.environment work for you.– However, “serious programming in C shell” However, “serious programming in C shell”

is an oxymoronis an oxymoron• Interpreter has bugs and weird quirks and can Interpreter has bugs and weird quirks and can

cause loss of hair, insanity, excessive weight cause loss of hair, insanity, excessive weight gain, hypertension, LDL ,HDL . In other gain, hypertension, LDL ,HDL . In other words, you age faster.words, you age faster.

Page 5: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

C-Shell Programming?C-Shell Programming?

• Good for quick and small tasksGood for quick and small tasks

• Setting up your preferences, e.g. Setting up your preferences, e.g. $HOME/.cshrc$HOME/.cshrc

• See See http://www.gregor.com/dgregor/csh_whynot.htmlhttp://www.gregor.com/dgregor/csh_whynot.html

for more details of why you shouldn’t for more details of why you shouldn’t do serious programming in C shell.do serious programming in C shell.

• BASH seems to have caught-up with C BASH seems to have caught-up with C shell as far as friendliness of user-shell as far as friendliness of user-interface and is much better for interface and is much better for programming.programming.

Page 6: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Bourne/Korn/BASH shellsBourne/Korn/BASH shells

• Widely availableWidely available

• Coding & Test can go very fast – logic Coding & Test can go very fast – logic & control flow generally not complex& control flow generally not complex

• Consequently, methods for Consequently, methods for debugging aren’t too sophisticated:debugging aren’t too sophisticated:

•echo statementecho statement

•-x & -v flags-x & -v flags

•Any others?Any others?

Page 7: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Bourne/Korn/BASH shell Bourne/Korn/BASH shell programmingprogramming

• ‘‘-v’ flag shows each line in your -v’ flag shows each line in your script during processingscript during processing

• ‘‘-x’ flag shows each command as -x’ flag shows each command as evaluated by the shell interpreterevaluated by the shell interpreter

• IMHO, most useful when both are IMHO, most useful when both are combined, i.e. –xv combined, i.e. –xv

Page 8: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Bourne/Korn/BASH shell Bourne/Korn/BASH shell programmingprogramming

• Use ‘-xv’ flag at beginning of script to Use ‘-xv’ flag at beginning of script to see everything. For examplesee everything. For example

% /bin/ksh –xv <your script typed here>

#!/bin/sh -xv

Page 9: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Bourne/Korn/BASH shell Bourne/Korn/BASH shell programmingprogramming

• If output too voluminous, consider If output too voluminous, consider placing set –xv/set +xv pairs that placing set –xv/set +xv pairs that bracket the code that you’re bracket the code that you’re interested in debugging.interested in debugging.

Page 10: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Example of selective Example of selective debuggingdebugging

% test.shHello MDLif [ “X$DISPLAY” = “X” ]then echo DISPLAY is not setfi+ ‘[‘ Xtyr:48.0 = X ‘]’set +xv+ set +xvGoodbye

#!/bin/shecho Hello MDLset –xvif [ “X$DISPLAY” = “X” ] then echo DISPLAY not setfiset +xvecho Goodbye

Page 11: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Debugging Compiled Debugging Compiled ProgramsPrograms• Need the “-g” flag in the compilation Need the “-g” flag in the compilation

lineline– For Makefiles, you can use the command For Makefiles, you can use the command

line to override predefined macros, e.g.,line to override predefined macros, e.g.,

– If you know the buggy routine, you can If you know the buggy routine, you can debug just that w/o recompiling the entire debug just that w/o recompiling the entire program.program.

% make myprogram CFLAG=-g

Page 12: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Debugging Compiled Debugging Compiled ProgramsPrograms• Development cycle is longer, Development cycle is longer,

algorithms more complexalgorithms more complex• For new programs, using the debugger For new programs, using the debugger

to watch the code as it executes can to watch the code as it executes can catch a lot of errors efficiently.catch a lot of errors efficiently.

• For established programs, the For established programs, the debugger can help you find and fix debugger can help you find and fix problems rapidly, as long as the code problems rapidly, as long as the code was compiled with –g flag!was compiled with –g flag!

Page 13: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

GDB - GNU debuggerGDB - GNU debugger

• For AWIPS, it’s the For AWIPS, it’s the tool to use for C, C+tool to use for C, C++ and FORTRAN + and FORTRAN codecode

• DDD is a wrapper DDD is a wrapper around GDB and around GDB and provides a provides a “friendly” face to “friendly” face to users.users.

• /usr/bin/ddd/usr/bin/ddd

• /usr/bin/gdb/usr/bin/gdb

GUI

Interpreter

GDB

Page 14: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Essential GDB commandsEssential GDB commands• Set breakpoints “b” stop executionSet breakpoints “b” stop execution

• Print “p” print/change valuesPrint “p” print/change values

• Next “n” step over codeNext “n” step over code

• Step “s” step into codeStep “s” step into code

• Continue “c” run to exit or next Continue “c” run to exit or next “b”“b”

• DDD allows all this using mouse clicks DDD allows all this using mouse clicks and button pushes.and button pushes.

• See PDF or handout for more detailed See PDF or handout for more detailed listing of GDB commands.listing of GDB commands.

Page 15: Debugging Tips for Programmers. Outline Script debugging Script debugging –C shell –BASH/Bourne/Korn shell tips Compiled language debugging Compiled language

Debugging ScenariosDebugging Scenarios

• Your program “hangs”Your program “hangs”– Use debugger to “attach” to running processUse debugger to “attach” to running process

•This stops program and use debugger to find the This stops program and use debugger to find the infinite loop by stepping through code or infinite loop by stepping through code or backtracebacktrace

– Send a signal ABRT to process to generate Send a signal ABRT to process to generate core dump file (in most instances)core dump file (in most instances)

% kill –ABRT infiniteloopCore dumped