Debugging Applications with GNU Debugger

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

Post a comment
Embed Video
Edit your comment Cancel

4 Favorites

Debugging Applications with GNU Debugger - Presentation Transcript

  1. Debugging applications with the GNU Debugger
      • Presenter: Prakash Varandani
  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.
  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.
  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).
  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
  6. Attaching a process
    • Run a program directly through the debugger.
    • Attach to a running process.
    • Use a core file for post-mortem analysis.
  7. Invoking gdb
    • Executable program:
    • gdb program
    • Executable and core file:
    • gdb program core.
    • Executable and process:
    • gdb program <pid>.
  8. Program’s arguments.
    • (gdb) set args abc def
    • (gdb) set args
    • (gdb) run abc def
    • Example 1:
  9. gdb files
    • It is possible to start gdb without any process/executable/core file.
    • Add an executable
    • (gdb) file/exec-file <executable>
    • Attach to a already running process
    • (gdb) attach <pid>
    • Add a core file
    • (gdb) core-file <filename>
  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:
  11. Setting breakpoints contd…
    • (gdb) tbreak args
    • (gdb) hbreak args
    • (gdb) thbreak args
    • (gdb) rbreak regex
  12. Watchpoints
    • (gdb) watch expr
    • (gdb) rwatch expr
    • (gdb) awatch expr
    • (gdb) info watchpoints
    • (provides similar information as
    • for info breakpoints)
  13. Getting information about breakpoints
    • info breakpoints [ n ]
      • Breakpoint Numbers
      • Type
      • Disposition
      • Enabled or Disabled
      • Address
      • What
    • Example 4:
  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.
  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
  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:
  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..
  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.
  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
  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.
  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.
  22. Information about a frame ...
    • (gdb) info frame addr , info f addr
    • (gdb) info args
    • (gdb) info locals
  23. Printing source lines
    • (gdb) list linenum
    • (gdb) list function
    • (gdb) list
    • (gdb) list –
    • (gdb) list *address
  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.
  25. Examining Data
    • (gdb) print expr
    • (gdb) print /f expr
    • (gdb) print
    • (gdb) print /f
  26. Output formats
    • x -> hexadecimal
    • d -> signed decimal
    • u -> unsigned decimal
    • o -> octal
    • t -> binary
    • c -> character
    • f -> floating point
    • a -> address format
  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)
  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
  29. Assembly Language
    • Disassembling a function:
    • (gdb) disassemble main
    • Dump of assembler code for function main:
    • 0x00010754 <main+0>: save %sp, -120, %sp
    • 0x00010758 <main+4>: mov 3, %o0
    • 0x0001075c <main+8>: st %o0, [ %fp + -20 ]
    • 0x00010760 <main+12>: ld [ %fp + -20 ], %o0
    • 0x00010764 <main+16>: call 0x10718 <fun1>
    • 0x00010768 <main+20>: nop
    • 0x0001076c <main+24>: clr %i0 ! 0x0
    • 0x00010770 <main+28>: b 0x10778 <main+36>
    • 0x00010774 <main+32>: nop
    • 0x00010778 <main+36>: ret
    • 0x0001077c <main+40>: restore
    • End of assembler dump.
  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
  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)
  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
  33. Canning the commands
    • define command
    • can accept upto 10 arguments viz. arg0 to arg9
    • document command
    • dont-repeat
    • help user-defined
    • show user
  34. Command hooks
    • run a sequence of commands when a particular command is executed.
    • hook-<command> runs before <command> is executed.
    • hookpost-<command> runs after command is executed.
    • The pseudo command “stop”

+ gnunifygnunify, 3 years ago

custom

2223 views, 4 favs, 5 embeds more stats

Debugging Applications with GNU Debugger

More Info

© All Rights Reserved

Go to text version
  • Total Views 2223
    • 2056 on SlideShare
    • 167 from embeds
  • Comments 1
  • Favorites 4
  • Downloads 0
Most viewed embeds
  • 92 views on http://c4swimmers.net
  • 67 views on http://www.c4swimmers.net
  • 5 views on http://deadlycode.blogspot.com
  • 2 views on http://rockthecode.blogspot.com
  • 1 views on http://feeds.feedburner.com

more

All embeds
  • 92 views on http://c4swimmers.net
  • 67 views on http://www.c4swimmers.net
  • 5 views on http://deadlycode.blogspot.com
  • 2 views on http://rockthecode.blogspot.com
  • 1 views on http://feeds.feedburner.com

less

Flagged as inappropriate Flag as inappropriate
Flag as innappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel

Categories