debugger

15
Dale Roberts Debugger Debugger Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected] Department of Computer and Information Science, School of Science, IUPUI Fall 2003

Upload: jasmine-lancaster

Post on 31-Dec-2015

18 views

Category:

Documents


0 download

DESCRIPTION

Department of Computer and Information Science, School of Science, IUPUI Fall 2003. Debugger. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected]. GDB. The purpose of a debugger such as GDB is to allow you to see what is going on inside the program while it executes. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Debugger

Dale Roberts

DebuggerDebugger

Dale Roberts, Lecturer

Computer Science, IUPUI

E-mail: [email protected]

Department of Computer and Information Science,School of Science, IUPUI

Fall 2003

Page 2: Debugger

Dale Roberts

GDB GDB The purpose of a debugger such as GDB is to The purpose of a debugger such as GDB is to allow you to see what is going on inside the allow you to see what is going on inside the program while it executes.program while it executes.GDB – GNU debuggerGDB – GNU debuggerGDB can be used to debug programs written in GDB can be used to debug programs written in C, C++.C, C++.The C++ debugging facilities are jointly The C++ debugging facilities are jointly implemented by the C++ compiler and GDB.implemented by the C++ compiler and GDB.To debug your C++ code, you must compile To debug your C++ code, you must compile your C++ programs with a supported C++ your C++ programs with a supported C++ compiler, such as GNU compiler, such as GNU g++g++, or the HP ANSI C+, or the HP ANSI C++ compiler (+ compiler (aCCaCC) with a –g option to include ) with a –g option to include symbols. symbols.

Page 3: Debugger

Dale Roberts

GDB cont….GDB cont….GDB can do four main kinds of things (plus other things in support of GDB can do four main kinds of things (plus other things in support of

these) to help you catch bugs in the act: these) to help you catch bugs in the act: Start your program, specifying anything that might affect its behavior. Start your program, specifying anything that might affect its behavior. Make your program stop on specified conditions. Make your program stop on specified conditions. Examine what has happened, when your program has stopped. Examine what has happened, when your program has stopped. Change things in your program, so that you can experiment with correcting Change things in your program, so that you can experiment with correcting the effects of one bug and go on to learn about another.the effects of one bug and go on to learn about another.

Page 4: Debugger

Dale Roberts

GDB CommandsGDB Commands

GDB is invoked by running the program gdb. Once the GDB is invoked by running the program gdb. Once the debugger is started, GDB reads command until you tell debugger is started, GDB reads command until you tell it to exit. GDB can be started with variety of arguments it to exit. GDB can be started with variety of arguments and options.and options.

The most usual way to start debugging isThe most usual way to start debugging is

gdb gdb program – program – one argument, which is an executable file one argument, which is an executable file more than one arguments can be providedmore than one arguments can be provided

gdb gdb program coreprogram core – two arguments, one executable and – two arguments, one executable and one core fileone core file

gdb gdb program 1234 – program 1234 – specify process id as second specify process id as second argumentargument

Page 5: Debugger

Dale Roberts

Run commandRun command

When gdb starts, your program is not When gdb starts, your program is not actually running. It won't run until actually running. It won't run until you instruct gdb how to run it.you instruct gdb how to run it.

run – will start your program as if you run – will start your program as if you had typed a.outhad typed a.out

You can provide command line You can provide command line arguments if the program requires.arguments if the program requires.

Page 6: Debugger

Dale Roberts

Break pointsBreak points

A A break break point makes your program stop point makes your program stop whenever a certain point in the program is whenever a certain point in the program is reachedreachedWays to set a break pointWays to set a break point

break break functionfunction - Set a breakpoint at entry to - Set a breakpoint at entry to function function functionfunctionbreak break +offset+offset - -break -break -offsetoffset - Sets a breakpoint some number - Sets a breakpoint some number of lines forward or back from the position at which of lines forward or back from the position at which execution stopped.execution stopped.break break linenumlinenum - Sets a breakpoint at line - Sets a breakpoint at line linenumlinenum in the current source file in the current source file

Page 7: Debugger

Dale Roberts

Break points Cont….Break points Cont….

break break filenamefilename::functionfunction - Sets a - Sets a breakpoint at entry to function breakpoint at entry to function functionfunction found in file found in file filenamefilename. . Specifying a file name as well as a Specifying a file name as well as a function name is superfluous except function name is superfluous except when multiple files contain similarly when multiple files contain similarly named functions.named functions.info breakpoints info breakpoints info break - info break - Prints a table of all Prints a table of all breakpoints set and not deletedbreakpoints set and not deleted

Page 8: Debugger

Dale Roberts

Watch PointsWatch Points

A A watch watch point is a special breakpoint point is a special breakpoint that stops your program when the that stops your program when the value of an expression changesvalue of an expression changes

Ways to set a watch pointWays to set a watch point watch watch exprexpr - Sets a watchpoint for - Sets a watchpoint for

an expression. an expression.

info watchpointsinfo watchpoints - Prints a table of - Prints a table of all watch points set and not deleted all watch points set and not deleted

Page 9: Debugger

Dale Roberts

Deleting Break and Watch pointsDeleting Break and Watch points

Deleting Break points and watch Deleting Break points and watch pointspoints

clearclear clear clear functionfunction clear clear filenamefilename::functionfunction clear clear linenumlinenum clear clear filenamefilename::linenumlinenum delete n – deletes the break point. n is a delete n – deletes the break point. n is a break point number.break point number.

Page 10: Debugger

Dale Roberts

Continue and steppingContinue and steppingcontinue [continue [ignore-countignore-count]] - - ContinuingContinuing means resuming program execution until your means resuming program execution until your program completes normally.program completes normally.

Resumes program execution, at the address where Resumes program execution, at the address where your program last stopped, any breakpoints set at your program last stopped, any breakpoints set at that address are bypassed. The optional that address are bypassed. The optional argument argument ignore-countignore-count allows you to specify a allows you to specify a further number of times to ignore a breakpoint at further number of times to ignore a breakpoint at this location.this location.

stepstep - Continue running your program until - Continue running your program until control reaches a different source line, then control reaches a different source line, then stop it and return control.stop it and return control.

Page 11: Debugger

Dale Roberts

Stepping ContinuedStepping Continued

step step countcount Continue running as in Continue running as in stepstep, but do so , but do so

countcount times. If a breakpoint is times. If a breakpoint is reached, or a signal not related to reached, or a signal not related to stepping occurs before stepping occurs before countcount steps, steps, stepping stops right away.stepping stops right away.

Next - Like step, however, if the current Next - Like step, however, if the current line of the program contains a function line of the program contains a function call, it executes the function and stops at call, it executes the function and stops at the next line.Step stops at the beginning the next line.Step stops at the beginning of the function.of the function.

Page 12: Debugger

Dale Roberts

MiscellaneousMiscellaneous

Print E – prints the value of E,in the Print E – prints the value of E,in the current frame,in the program.E is a current frame,in the program.E is a variablevariable

Quit or ctrl-x – will quit gdbQuit or ctrl-x – will quit gdb

display E - display E - Print a variable's value at each Print a variable's value at each step of a programstep of a program

Help Help command – command – Provides a brief Provides a brief description of a GDB commanddescription of a GDB command

Page 13: Debugger

Dale Roberts

Miscellaneous (cont)Miscellaneous (cont)

Where – helps you to know where the Where – helps you to know where the program crashed.program crashed.

Source Source www.cs.princeton.edu/~benjasik/gdb/gdbtwww.cs.princeton.edu/~benjasik/gdb/gdbtut.htmlut.html

There is also a reference card available There is also a reference card available through the web site.through the web site.

Page 14: Debugger

Dale Roberts

Page 15: Debugger

Dale Roberts