Debugging Applications with GNU Debugger

6,508 views
6,246 views

Published on

Debugging Applications with GNU Debugger

Published in: Technology
3 Comments
8 Likes
Statistics
Notes
No Downloads
Views
Total views
6,508
On SlideShare
0
From Embeds
0
Number of Embeds
145
Actions
Shares
0
Downloads
0
Comments
3
Likes
8
Embeds 0
No embeds

No notes for slide
  • Debugging Applications with GNU Debugger

    1. 1. Debugging applications with the GNU Debugger <ul><ul><li>Presenter: Prakash Varandani </li></ul></ul>
    2. 2. When to use a debugger? <ul><li>Point-in-time debugging </li></ul><ul><li>When a problem is easily reproducible. </li></ul><ul><li>When the problem behavior can be predicted </li></ul><ul><li>When a problem can be localized to a small period of time </li></ul><ul><li>When system level problem determination tools do not help </li></ul><ul><li>When the source code is readily available. </li></ul>
    3. 3. When not to use debugger? <ul><li>When causes of a problem span a long history and time. </li></ul><ul><li>Problem is difficult to predict in nature. </li></ul><ul><li>Problem is not reproducible at will. </li></ul>
    4. 4. Why gdb? <ul><li>Easily available. </li></ul><ul><li>Easy installation. </li></ul><ul><li>Configurable. </li></ul><ul><li>Support for various Object File Formats. </li></ul><ul><li>Support for various architectures. </li></ul><ul><li>Rich feature set. </li></ul><ul><li>Open Source (Of Course). </li></ul>
    5. 5. Compiling for Debugging. <ul><li>Compiling with the “-g” option: </li></ul><ul><li>e.g. gcc –g stack.c –o stack </li></ul><ul><li>Preprocessor information: </li></ul><ul><li>e.g. gcc –dwarf-2 –g3 stack.c –o stack </li></ul>
    6. 6. Attaching a process <ul><li>Run a program directly through the debugger. </li></ul><ul><li>Attach to a running process. </li></ul><ul><li>Use a core file for post-mortem analysis. </li></ul>
    7. 7. Invoking gdb <ul><li>Executable program: </li></ul><ul><li>gdb program </li></ul><ul><li>Executable and core file: </li></ul><ul><li>gdb program core. </li></ul><ul><li>Executable and process: </li></ul><ul><li>gdb program <pid>. </li></ul>
    8. 8. Program’s arguments. <ul><li>(gdb) set args abc def </li></ul><ul><li>(gdb) set args </li></ul><ul><li>(gdb) run abc def </li></ul><ul><li>Example 1: </li></ul>
    9. 9. gdb files <ul><li>It is possible to start gdb without any process/executable/core file. </li></ul><ul><li>Add an executable </li></ul><ul><li>(gdb) file/exec-file <executable> </li></ul><ul><li>Attach to a already running process </li></ul><ul><li>(gdb) attach <pid> </li></ul><ul><li>Add a core file </li></ul><ul><li>(gdb) core-file <filename> </li></ul>
    10. 10. Setting breakpoints: <ul><li>(gdb) break function </li></ul><ul><li>(gdb) break +/- offset </li></ul><ul><li>(gdb) break linenum </li></ul><ul><li>(gdb) break filename : linenum </li></ul><ul><li>(gdb) break filename : function </li></ul><ul><li>(gdb) break * address </li></ul><ul><li>(gdb) break ... if cond </li></ul><ul><li>Example 2: </li></ul>
    11. 11. Setting breakpoints contd… <ul><li>(gdb) tbreak args </li></ul><ul><li>(gdb) hbreak args </li></ul><ul><li>(gdb) thbreak args </li></ul><ul><li>(gdb) rbreak regex </li></ul>
    12. 12. Watchpoints <ul><li>(gdb) watch expr </li></ul><ul><li>(gdb) rwatch expr </li></ul><ul><li>(gdb) awatch expr </li></ul><ul><li>(gdb) info watchpoints </li></ul><ul><li> (provides similar information as </li></ul><ul><li> for info breakpoints) </li></ul>
    13. 13. Getting information about breakpoints <ul><li>info breakpoints [ n ] </li></ul><ul><ul><li>Breakpoint Numbers </li></ul></ul><ul><ul><li>Type </li></ul></ul><ul><ul><li>Disposition </li></ul></ul><ul><ul><li>Enabled or Disabled </li></ul></ul><ul><ul><li>Address </li></ul></ul><ul><ul><li>What </li></ul></ul><ul><li>Example 4: </li></ul>
    14. 14. Breakpoints contd… <ul><li>Simple breakpoints stop the program every time they are hit. </li></ul><ul><li>(gdb) condition bnum expression </li></ul><ul><li>(gdb) condition bnum </li></ul><ul><li>(gdb) ignore bnum count </li></ul><ul><li>(gdb) commands [ bnum ] </li></ul><ul><li>... command-list ... </li></ul><ul><li>end </li></ul><ul><li>If bnum is not provided the commands refer to the last set breakpoint/watchpoint. </li></ul>
    15. 15. Breakpoints contd... <ul><li>(gdb) clear </li></ul><ul><li>(gdb) clear function </li></ul><ul><li>(gdb) clear linenum </li></ul><ul><li>(gdb) delete [breakpoints] [range...] </li></ul><ul><li>(gdb) disable [breakpoints] [range...] </li></ul><ul><li>(gdb) enable [breakpoints] once range </li></ul><ul><li>(gdb) enable [breakpoints] delete range </li></ul>
    16. 16. Continuing and Stepping <ul><li>(gdb) continue [ignore-count] </li></ul><ul><li>(gdb) step [count] </li></ul><ul><li>(gdb) next [count] </li></ul><ul><li>(gdb) finish </li></ul><ul><li>(gdb) until </li></ul><ul><li>(gdb) until location </li></ul><ul><li>(gdb) stepi </li></ul><ul><li>(gdb) nexti </li></ul><ul><li>Example 5: </li></ul>
    17. 17. Examining the stack <ul><li>Frames: data associated with each function call like arguments, local variables, ra etc... </li></ul><ul><li>The most recently created frame is called the innermost frame and the initial one is called the outermost frame. </li></ul><ul><li>gdb assign numbers to the stack frames, 0 for the innermost and so on.. </li></ul>
    18. 18. How we got there?.. backtraces <ul><li>backtrace, bt -> Print a backtrace of the entire stack. </li></ul><ul><li>backtrace n , bt n -> print n innermost frames. </li></ul><ul><li>backtrace - n , bt – n -> print n outermost frames. </li></ul><ul><li>backtrace full -> Print the values of the local variables also. </li></ul>
    19. 19. Controlling backtrace <ul><li>set backtrace past-main [on/off] </li></ul><ul><li>to configure printing of system specific code. </li></ul><ul><li>set backtrace past-entry [on/off] </li></ul><ul><li>show backtrace past-entry </li></ul><ul><li>set backtrace limit n </li></ul><ul><li>set backtrace limit 0 (unlimited) </li></ul><ul><li>show backtrace limit </li></ul>
    20. 20. Selecting a frame <ul><li>(gdb) frame n, f n -> select frame n </li></ul><ul><li>(gdb) frame addr , f addr -> useful when the program has multiple stacks (highly system specific). </li></ul><ul><li>(gdb) up [n] -> for positive n move “n” frames towards the outermost frame. </li></ul><ul><li>(gdb) down [n] -> for positive n move “n” frames towards the innermost frame. </li></ul><ul><li>If n is not provided move one frame up or down. </li></ul>
    21. 21. Information about a frame <ul><li>(gdb) info frame </li></ul><ul><li>This command prints a verbose description of the selected stack frame, including: </li></ul><ul><ul><li>the address of the frame </li></ul></ul><ul><ul><li>the address of the next frame down (called by this frame) </li></ul></ul><ul><ul><li>the address of the next frame up (caller of this frame) </li></ul></ul><ul><ul><li>the language in which the source code corresponding to this frame is written </li></ul></ul><ul><ul><li>the address of the frame's arguments </li></ul></ul><ul><ul><li>the address of the frame's local variables </li></ul></ul><ul><ul><li>the program counter saved in it (the address of execution in the caller frame) </li></ul></ul><ul><ul><li>which registers were saved in the frame </li></ul></ul><ul><li>This information is useful when a stack format fail to fit the usual convention. </li></ul>
    22. 22. Information about a frame ... <ul><li>(gdb) info frame addr , info f addr </li></ul><ul><li>(gdb) info args </li></ul><ul><li>(gdb) info locals </li></ul>
    23. 23. Printing source lines <ul><li>(gdb) list linenum </li></ul><ul><li>(gdb) list function </li></ul><ul><li>(gdb) list </li></ul><ul><li>(gdb) list – </li></ul><ul><li>(gdb) list *address </li></ul>
    24. 24. Searching source files. <ul><li>(gdb) forward-search regexp </li></ul><ul><li>following the last line printed, search for a match with regexp and print the first line found. </li></ul><ul><li>(gdb) search regexp </li></ul><ul><li>Same as forward-search. </li></ul><ul><li>(gdb) reverse-search regexp </li></ul><ul><li>Starting with the line one above the last line printed, </li></ul><ul><li>search for a match with regexp and print the first line found. </li></ul>
    25. 25. Examining Data <ul><li>(gdb) print expr </li></ul><ul><li>(gdb) print /f expr </li></ul><ul><li>(gdb) print </li></ul><ul><li>(gdb) print /f </li></ul>
    26. 26. Output formats <ul><li>x -> hexadecimal </li></ul><ul><li>d -> signed decimal </li></ul><ul><li>u -> unsigned decimal </li></ul><ul><li>o -> octal </li></ul><ul><li>t -> binary </li></ul><ul><li>c -> character </li></ul><ul><li>f -> floating point </li></ul><ul><li>a -> address format </li></ul>
    27. 27. Examining memory <ul><li>(gdb) x /nfu addr </li></ul><ul><li>(gdb) x addr </li></ul><ul><li>n -> the repeat count. Default 1. </li></ul><ul><li>f -> format for printing. Default x and changes eventually. </li></ul><ul><li>u -> unit size, can be one of </li></ul><ul><ul><li>b -> byte </li></ul></ul><ul><ul><li>h -> half word (2 bytes) </li></ul></ul><ul><ul><li>w -> word (4 bytes) </li></ul></ul><ul><ul><li>g -> giant word (8 bytes) </li></ul></ul>
    28. 28. Automatic display <ul><li>(gdb) display expr </li></ul><ul><li>(gdb) display /f expr </li></ul><ul><li>(gdb) undisplay dnums delete display dnums </li></ul><ul><li>(gdb) disable display dnums </li></ul><ul><li>(gdb) enable display dnums </li></ul><ul><li>(gdb) display </li></ul><ul><li>(gdb) info display </li></ul>
    29. 29. Assembly Language <ul><li>Disassembling a function: </li></ul><ul><li>(gdb) disassemble main </li></ul><ul><li>Dump of assembler code for function main: </li></ul><ul><li>0x00010754 <main+0>: save %sp, -120, %sp </li></ul><ul><li>0x00010758 <main+4>: mov 3, %o0 </li></ul><ul><li>0x0001075c <main+8>: st %o0, [ %fp + -20 ] </li></ul><ul><li>0x00010760 <main+12>: ld [ %fp + -20 ], %o0 </li></ul><ul><li>0x00010764 <main+16>: call 0x10718 <fun1> </li></ul><ul><li>0x00010768 <main+20>: nop </li></ul><ul><li>0x0001076c <main+24>: clr %i0 ! 0x0 </li></ul><ul><li>0x00010770 <main+28>: b 0x10778 <main+36> </li></ul><ul><li>0x00010774 <main+32>: nop </li></ul><ul><li>0x00010778 <main+36>: ret </li></ul><ul><li>0x0001077c <main+40>: restore </li></ul><ul><li>End of assembler dump. </li></ul>
    30. 30. Looking into the registers <ul><li>A single register: </li></ul><ul><li>(gdb) p $eax </li></ul><ul><li>$4 = 6 </li></ul><ul><li>(gdb) p $ecx </li></ul><ul><li>$5 = 1 </li></ul><ul><li>All of them: </li></ul><ul><ul><li>(gdb) info registers </li></ul></ul><ul><ul><li>eax 0x6 6 </li></ul></ul><ul><ul><li>ecx 0x1 1 </li></ul></ul><ul><ul><li>edx 0x4015c490 1075168400 </li></ul></ul><ul><ul><li>ebx 0x4015afd8 1075163096 </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>… </li></ul></ul>
    31. 31. Signals <ul><li>(gdb) info signals </li></ul><ul><li>(gdb) info handle </li></ul><ul><li>(gdb) info signal sig </li></ul><ul><li>(gdb) handle signal keywords </li></ul><ul><li>keywords can be </li></ul><ul><ul><li>stop/nostop </li></ul></ul><ul><ul><li>print/noprint </li></ul></ul><ul><ul><li>pass(noignore)/nopass(ignore) </li></ul></ul>
    32. 32. Altering Execution <ul><li>Assigning values to variables at runtime using print/set. </li></ul><ul><li>Continuing at a different address </li></ul><ul><li>Sending a signal </li></ul><ul><li>Cancelling execution of a function </li></ul><ul><li>Calling program functions </li></ul>
    33. 33. Canning the commands <ul><li>define command </li></ul><ul><li>can accept upto 10 arguments viz. arg0 to arg9 </li></ul><ul><li>document command </li></ul><ul><li>dont-repeat </li></ul><ul><li>help user-defined </li></ul><ul><li>show user </li></ul>
    34. 34. Command hooks <ul><li>run a sequence of commands when a particular command is executed. </li></ul><ul><li>hook-<command> runs before <command> is executed. </li></ul><ul><li>hookpost-<command> runs after command is executed. </li></ul><ul><li>The pseudo command “stop” </li></ul>

    ×