Effective Use of GDB

24
01/22/08 Effective use of GDB February 24 th 2008

Transcript of Effective Use of GDB

Page 1: Effective Use of GDB

01/22/08

Effective use of GDBFebruary 24th 2008

Page 2: Effective Use of GDB

2

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it

Brian W. Kernighan's Law of Debugging Difficulty

Page 3: Effective Use of GDB

3

B

Page 4: Effective Use of GDB

4

Agenda

Programming GDB Defining commands Controlling program flow

Inspecting and Changing Program State Changing Debuggee state Post-mortem analysis with GDB

Core Files Debugging Tips, Tricks and Myths.

Page 5: Effective Use of GDB

5

Some Definitions Debugger – gdb Debuggee – Program being debugged

Page 6: Effective Use of GDB

6

Programming GDB. Code

Commands Conditional Breakpoints – Default Actions with Breakpoints. Sequencing Looping

Data Convenience Variables. Parameters to commands

Environment Command files .gdbinit

Page 7: Effective Use of GDB

7

Defining Custom Commands Build on top of basic gdb commands . Useful to traverse long data structures and print

values. Sequence of GDB commands to which you can add

a new name Upto 10 arguments ($arg0... $arg9) allowed . Supports if-else, conditional and basic looping

constructs. Convenience variables can be used.

Page 8: Effective Use of GDB

8

Defining Custom Commands

(gdb) define factorialType commands for definition of "factorial".End with a line saying just "end".>set $fact=1>set $n=$arg0>while $n>1 >set $fact=$fact*$n >set $n=$n-1 >end>print $fact>end(gdb) factorial 5

$30 = 120.

Page 9: Effective Use of GDB

9

Convenience Variables Use to hold values that can be reused. Variables exist within gdb and not as a part of your

debuggee. Prefixed with `$' No fixed type

set $foo = *obj_ptr

show convenience

Page 10: Effective Use of GDB

10

Inspecting Debuggee State. Inspecting data Interpreter on the debuggee Changing debuggee program state

Page 11: Effective Use of GDB

11

Interpreter on the debuggee Possible to call functions in the debuggee

Shows return value. Pass multiple parameters includes convenience variables Usecases

Change Debuggee state to reset or initialize Pretty Print large data structures Quickly examine behaviour of a function

Caveats No breakpoints allowed in such functions Called routine cannot abort or the debug session will terminate Can't single step in such functions.

Page 12: Effective Use of GDB

12

Changing Debuggee State Setting values to variables

print var = value set variable name = value set variable name = func (....)

Continuing from a different location jump linespec jump *address set $pc = value

Return from a function return expr

Giving a signal to your program signal {signalname | signalnumber}

Page 13: Effective Use of GDB

13

Debugging Signals Sending signals to the debuggee Signal handlers are not special. Trap Execution on signal delivery  signal <signalname /number>

Pass a signal to a program Equivalent of raise.

Page 14: Effective Use of GDB

14

Debugging Signals

handle signum/name [Keywords] Decide on what to do with the signal (action)

{no}stop – Give control to the debugger. {no}print – Print information on the console. {no}pass – Pass the signal to the debuggee {no}ignore-

Ignore = nopass noignore=pass

Resume a program with signal 0 .

Page 15: Effective Use of GDB

15

Core Files Generate core files how ? What is in a core file ? Debug a core file as you would any other program Backtraces Work ! up, down, Frame tracing works. Threads .. hmmm !

Page 16: Effective Use of GDB

16

Generating Core Files $> ulimit -c unlimited Run your program normally get a core file. Typically a file called core or core.pid on other

distros. Configuring file info by

/proc/sys/kernel/core_pattern and /proc/sys/kernel/core_uses_pid

Page 17: Effective Use of GDB

17

What is in a core file.

Memory and Register dump of the program being run.

Linker map dump from dynamic linker. Stack dump at time of crash. Default data section. All threads are dumped.

Page 18: Effective Use of GDB

18

/proc/kcore Treat it as any other core file . Get a kernel compiled with debug info in it.

Page 19: Effective Use of GDB

19

Rudimentary Replay Debugging Save program state Resume from the program state. Rudimentary form of replay debugging. Caveats

File IO, Signals, Sockets , Threads etc.

Shameless plug – Go look at LIZARD (Lizard iz a Replay Debugger) http://lizard.sourceforge.net

Page 20: Effective Use of GDB

20

Some popular Myths Code generated with -g is different from without it. Do we lose MIPS with -g ? Stepping forward in straight line code takes you

back in time. Hmmm compiler bug ? printf is my best friend !

Page 21: Effective Use of GDB

21

Moral of the story.

Get familiar with assembly – even if it is rudimentary

Trust the debugger. It usually works. Don't blame the compiler always . Its usually a buffer overflow

some place !

Start automating your debug tasks. Define your commands. Write your own gdbinit scripts with your software. Document them !

Structure your code better. Remember from the command line

help info

Page 22: Effective Use of GDB

22

Some questions The debugger being a user level process gets

information about other user level processes . A security hole ?

Software breakpoints – How big is the instruction used?

How do breakpoints in shared libraries work across programs ?

Can I use gdb on gdb ?

Page 23: Effective Use of GDB

23

Thank you and Questions References

GDB User Manual GDB Reference Manual

Page 24: Effective Use of GDB

01/22/08

www.azingo.com