A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and...

34
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology

Transcript of A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and...

Page 1: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

A Tutorial on

Introduction to gdbBy

Sasanka Madiraju

Graduate Assistant

Center for Computation and Technology

Page 2: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

WISDOM

• W - What do you learn from this• I - What is your Interest• S - Your level of Sophistication• D - Level of Detail• O - Ownership-“The Pragmatic

Programmer”» By Andy Hunt and David Thomas

• M - My Motivation

Page 3: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Topics discussed• Part 1: Introduction• Part 2: THE WHY: gdb?

– Features

• Part 3: THE HOW– Commands

• Part 4: PRACTICE: Hands on debugging session

• Part 4: Advanced concepts • Part 5: Conclusion

– Q & A– References

Page 4: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Part 1

Introduction

Page 5: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Introduction

• What is a debugger?• Why do we need one?• gdb is NOT

– A shell – A text editor– A programming environment

• Other types of debuggers– ddd, GDBTk, Insight, dbx, softice, etc.

Page 6: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

What is a BUG?

• Bug- program does not generate the expected output

• BUG or Fault?• Errors or bugs

– syntactic – Logical

• Syntactic errors - caught by compiler

• Logical errors - difficult to find and remove.

Page 7: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Part 2

Why do you need to learn GDB?

Page 8: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Advantages of gdb

• Interactive• Responsive• Permissive• Capability to work with very large

programs– Reported cases: file size close to 1 GB

• Extremely portable– No other debugger works on so many

configurations

Page 9: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Advantages continued

• Can debug optimized code

• Can debug macros

• Multiple processor support

• Capability to debug multi threaded applications

• Remote debugging

Page 10: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Major Features

• Set Breakpoints

• Examine Data

• Specify Files

• Run Program

• Examine Stack

• Report Status

• Trace Execution

Page 11: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Part 3: Working

Page 12: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Compiling the program

• C-program - compiled using gcc compiler• Compilation command:

– Example: “gcc -o testout -g test.c”• “-o” generates the output• “-g” enables gdb

• C++ program - compiled using g++• Compilation command:

– Example: “g++ -o testout -g test.cpp”• “-o” generates the output• “-g” enables gdb

Page 13: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

How to get in and get out

• Command: gdb• Alternate Formats

– gdb– gdb <testout>– gdb program pid– gdb program core– gdb a.out (not recommended)– gdb --args ./exe/<cactus_config> <.par file>

• To quit gdb use the “quit” or “q” command

Page 14: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Modes of Operation

• Has many modes some of which are:– Silent– Batch

• useful for running GDB as a filter.– Example: Download and run a program on another

computer

– Shell– No windows– Set baud rate (for remote debugging)– Output statistics about time and memory

Page 15: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Logging output• set logging on

– Enable logging. • set logging off

– Disable logging. • set logging file file

– Change the name of the current logfile. The default logfile is `gdb.txt'. • set logging overwrite [on|off]

– By default, GDB will append to the logfile. Set overwrite if you want set logging on to overwrite the logfile instead.

• set logging redirect [on|off] – By default, GDB output will go to both the terminal and the logfile. Set

redirect if you want output to go only to the log file. • show logging

– Show the current values of the logging settings.

Page 16: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Halting program execution

• Break Points– Predefined points where program stops

• Watch Points– Special breakpoint that stops the program

when the value of an expression changes

• Catch Points– Special breakpoint that stops the program on

an event• Throwing of a C++ exception • Loading of a library

Page 17: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Break Points

• break function

• break +/-offset

• break linenum

• break filename:linenum

• break filename:function

• break *address

• break

• break ... if cond

Page 18: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Watch Points

• watch expr

• rwatch expr

• awatch expr

• info watchpoints

• Hardware & Software watchpoints– set can-use-hw-watchpoints 0

Page 19: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Catch Points• Syntax:

– catch event• Event can be any of the following

– throw • The throwing of a C++ exception

– catch • The catching of a C++ exception

– exec • A call to exec. This is currently only available for HP-UX

– fork • A call to fork. This is currently only available for HP-UX

– vfork • A call to vfork. This is currently only available for HP-UX

Page 20: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Break point commands

• info break

• list

• ignore <break point number>

• Remove break points– “delete” <break point number>– “clear” <line number> or <function name>

Page 21: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Stepping

• Stepping is the process of executing the program line by line

• Commands– next – step over– step – step through– continue – jump between break points

• To step over a few lines of code– step <Number of lines>– next <Number of lines>

Page 22: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Print command

• Formats– o – octal– x – hexadecimal– d – decimal– u - unsigned decimal– t – binary– f – float– a – address– i – instruction– c – char– s - string

Page 23: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Useful commands

• set• whatis• ptype• Call• Return• gdb has command completion

– Hit TAB to complete a command

• complete r– Lists all commands starting with r.

• aprops

Page 24: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Most useful command

help

Page 25: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Examining Data

• list linenum

• list function

• list

• list -

• list +

• set listsize count

• show listsize

Page 26: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Edit Data

• edit

• edit number

• edit function

• edit *address

• edit filename:number

• edit filename:function

Page 27: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Editors

• Vi

• Emacs

• XEmacs

• Choose an editor– EDITOR=/usr/bin/vi– export EDITOR– gdb …….

Page 28: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Search source files

• forward-search regexp • search regexp • reverse-search regexp • SPECIFY SOURCE DIRECTORY

– dir dirname ... • Adds the name of the directory to the beginning of the source

directory path

– show directories • Shows the source path

– show directories • Resets the source path to empty.

Page 29: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Examine Stack

• Very useful in debugging• Each function is placed on a stack• Function “main” is the only function

– one stack frame– Each additional function has a stack frame

• frame args: Move from one stack to another

• select-frame: silent version of frame command

Page 30: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Frame Information

• info f – This command prints a verbose description of the

selected stack frame, including: • the address of the frame • the address of the next frame down (called by this frame) • the address of the next frame up (caller of this frame) • the language in which the source code corresponding to this

frame is written • the address of the frame's arguments • the address of the frame's local variables • the program counter saved in it (the address of execution in

the caller frame) • which registers were saved in the frame

Page 31: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Info command for frames

• info frame addr • info args • info locals • info catch • SELECT A STACK FRAME

– frame <number>– frame addr – up n – down n

Page 32: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Back Trace

• bt (backtrace)– Stop by hitting control-C

• backtrace n

• backtrace full

Page 33: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

D E M O

Debugging Session

Getting our hands dirty

Page 34: A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Q & A

10 Minutes