(Slides) Debugging Applications with GNU Debugger

Slide Transcript
Slide 1: Debugging applications with the GNU Debugger Presenter: Prakash Varandani

Slide 2: When to use a debugger? Point-in-time debugging ? When a problem is easily reproducible. ? When the problem behavior can be predicted ? When a problem can be localized to a small period ? of time When system level problem determination tools do ? not help When the source code is readily available. ?

Slide 3: When not to use debugger? When causes of a problem span a long ? history and time. Problem is difficult to predict in nature. ? Problem is not reproducible at will. ?

Slide 4: Why gdb? Easily available. ? Easy installation. ? Configurable. ? Support for various Object File Formats. ? Support for various architectures. ? Rich feature set. ? Open Source (Of Course). ?

Slide 5: Compiling for Debugging. Compiling with the “-g” option: ? e.g. gcc –g stack.c –o stack Preprocessor information: ? e.g. gcc –dwarf-2 –g3 stack.c –o stack

Slide 6: Attaching a process Run a program directly through the ? debugger. Attach to a running process. ? Use a core file for post-mortem ? analysis.

Slide 7: Invoking gdb Executable program: ? gdb program Executable and core file: ? gdb program core. Executable and process: ? gdb program
.

Slide 8: Program’s arguments. (gdb) set args abc def ? (gdb) set args ? (gdb) run abc def ? Example 1: ?

Slide 9: gdb files It is possible to start gdb without any ? process/executable/core file. Add an executable ? (gdb) file/exec-file Attach to a already running process ? (gdb) attach
Add a core file ? (gdb) core-file ?

Slide 10: Setting breakpoints: (gdb) break function ? (gdb) break +/- offset ? (gdb) break linenum ? (gdb) break filename:linenum ? (gdb) break filename:function ? (gdb) break *address ? (gdb) break ... if cond ? Example 2: ?

Slide 11: Setting breakpoints contd… (gdb) tbreak args ? (gdb) hbreak args ? (gdb) thbreak args ? (gdb) rbreak regex ?

Slide 12: Watchpoints (gdb) watch expr ? (gdb) rwatch expr ? (gdb) awatch expr ? (gdb) info watchpoints ? (provides similar information as for info breakpoints)

Slide 13: Getting information about breakpoints info breakpoints [n] ? Breakpoint Numbers ? Type ? Disposition ? Enabled or Disabled ? Address ? What ? Example 4: ?

Slide 14: Breakpoints contd… Simple breakpoints stop the program every time they ? are hit. (gdb) condition bnum expression ? (gdb) condition bnum ? (gdb) ignore bnum count ? (gdb) commands [bnum] ? ... command-list ... end If bnum is not provided the commands refer to the last ? set breakpoint/watchpoint.

Slide 15: Breakpoints contd... (gdb) clear ? (gdb) clear function ? (gdb) clear linenum ? (gdb) delete [breakpoints] [range...] ? (gdb) disable [breakpoints] [range...] ? (gdb) enable [breakpoints] once range ? (gdb) enable [breakpoints] delete range ?

Slide 16: Continuing and Stepping (gdb) continue [ignore-count] ? (gdb) step [count] ? (gdb) next [count] ? (gdb) finish ? (gdb) until ? (gdb) until location ? (gdb) stepi ? (gdb) nexti ? Example 5: ?

Slide 17: Examining the stack Frames: data associated with each function ? call like arguments, local variables, ra etc... The most recently created frame is called the ? innermost frame and the initial one is called the outermost frame. gdb assign numbers to the stack frames, 0 for ? the innermost and so on..

Slide 18: How we got there?.. backtraces backtrace, bt -> Print a backtrace of ? the entire stack. backtrace n, bt n -> print n innermost ? frames. backtrace -n, bt –n -> print n ? outermost frames. backtrace full -> Print the values of ? the local variables also.

Slide 19: Controlling backtrace set backtrace past-main [on/off] ? to configure printing of system specific code. set backtrace past-entry [on/off] ? show backtrace past-entry ? set backtrace limit n ? set backtrace limit 0 (unlimited) ? show backtrace limit ?

Slide 20: Selecting a frame (gdb) frame n, f n -> select frame n ? (gdb) frame addr, f addr -> useful when the ? program has multiple stacks (highly system specific). (gdb) up [n] -> for positive n move “n” frames ? towards the outermost frame. (gdb) down [n] -> for positive n move “n” frames ? towards the innermost frame. If n is not provided move one frame up or down. ?

Slide 21: Information about a frame (gdb) info frame ? 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 ? This information is useful when a stack format fail to fit the ? usual convention.

Slide 22: Information about a frame ... (gdb) info frame addr , info f addr ? (gdb) info args ? (gdb) info locals ?

Slide 23: Printing source lines (gdb) list linenum ? (gdb) list function ? (gdb) list ? (gdb) list – ? (gdb) list *address ?

Slide 24: Searching source files. (gdb) forward-search regexp ? following the last line printed, search for a match with regexp and print the first line found. (gdb) search regexp ? Same as forward-search. (gdb) reverse-search regexp ? Starting with the line one above the last line printed, search for a match with regexp and print the first line found.

Slide 25: Examining Data (gdb) print expr ? (gdb) print /f expr ? (gdb) print ? (gdb) print /f ?

Slide 26: Output formats x -> hexadecimal ? d -> signed decimal ? u -> unsigned decimal ? o -> octal ? t -> binary ? c -> character ? f -> floating point ? a -> address format ?

Slide 27: Examining memory (gdb) x /nfu addr ? (gdb) x addr ? n -> the repeat count. Default 1. ? f -> format for printing. Default x ? and changes eventually. u -> unit size, can be one of ? b -> byte ? h -> half word (2 bytes) ? w -> word (4 bytes) ? g -> giant word (8 bytes) ?

Slide 28: Automatic display (gdb) display expr ? (gdb) display /f expr ? (gdb) undisplay dnums ? delete display dnums (gdb) disable display dnums ? (gdb) enable display dnums ? (gdb) display ? (gdb) info display ?

Slide 29: Assembly Language Disassembling a function: ? (gdb) disassemble main Dump of assembler code for function main: 0x00010754 : save %sp, -120, %sp 0x00010758 : mov 3, %o0 0x0001075c : st %o0, [ %fp + -20 ] 0x00010760 : ld [ %fp + -20 ], %o0 0x00010764 : call 0x10718 0x00010768 : nop 0x0001076c : clr %i0 ! 0x0 0x00010770 : b 0x10778 0x00010774 : nop 0x00010778 : ret 0x0001077c : restore End of assembler dump.

Slide 30: Looking into the registers A single register: ? (gdb) p $eax $4 = 6 (gdb) p $ecx $5 = 1 All of them: ? (gdb) info registers eax 0x6 6 ecx 0x1 1 edx 0x4015c490 1075168400 ebx 0x4015afd8 1075163096 … …

Slide 31: Signals (gdb) info signals ? (gdb) info handle ? (gdb) info signal sig ? (gdb) handle signal keywords ? keywords can be ? stop/nostop ? print/noprint ? pass(noignore)/nopass(ignore) ?

Slide 32: Altering Execution Assigning values to variables at ? runtime using print/set. Continuing at a different address ? Sending a signal ? Cancelling execution of a function ? Calling program functions ?

Slide 33: Canning the commands define command ? can accept upto 10 arguments viz. arg0 ? to arg9 document command ? dont-repeat ? help user-defined ? show user ?

Slide 34: Command hooks run a sequence of commands when a ? particular command is executed. hook- runs before ? is executed. hookpost- runs after ? command is executed. The pseudo command “stop” ?