SlideShare a Scribd company logo
Debugging applications with the GNU Debugger Presenter: Prakash Varandani
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.
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.
Why gdb? Easily available. Easy installation. Configurable. Support for various Object File Formats. Support for various architectures. Rich feature set. Open Source (Of Course).
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
Attaching a process Run a program directly through the debugger. Attach to a running process. Use a core file for post-mortem analysis.
Invoking gdb Executable program: gdb program Executable and core file:  gdb program core. Executable and process:  gdb program <pid>.
Program’s arguments. (gdb) set args abc def (gdb) set args (gdb) run abc def Example 1:
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>
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:
Setting breakpoints contd… (gdb) tbreak  args   (gdb) hbreak  args   (gdb) thbreak  args   (gdb) rbreak  regex
Watchpoints (gdb) watch  expr   (gdb) rwatch expr (gdb) awatch  expr   (gdb) info watchpoints   (provides similar information as    for info breakpoints)
Getting information about breakpoints info breakpoints [ n ]  Breakpoint Numbers   Type   Disposition   Enabled or Disabled   Address   What   Example 4:
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.
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
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:
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..
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.
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
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.
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.
Information about a frame ... (gdb) info frame  addr  , info f  addr (gdb) info args  (gdb) info locals
Printing source lines (gdb) list  linenum   (gdb) list  function   (gdb) list  (gdb) list – (gdb) list *address
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.
Examining Data (gdb) print expr (gdb) print /f expr (gdb) print  (gdb) print /f
Output formats x -> hexadecimal d -> signed decimal u -> unsigned decimal o -> octal t -> binary c -> character f -> floating point a -> address format
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)
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
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.
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  … …
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)
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
Canning the commands define command can accept upto 10 arguments viz. arg0 to arg9 document command dont-repeat  help user-defined  show user
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”

More Related Content

PDF
Vim Rocks!
PDF
TMUX Rocks!
PDF
淺談探索 Linux 系統設計之道
PDF
from Source to Binary: How GNU Toolchain Works
PDF
Working Remotely (via SSH) Rocks!
PDF
用十分鐘 向jserv學習作業系統設計
PDF
ARM Trusted FirmwareのBL31を単体で使う!
PDF
Q2.12: Debugging with GDB
Vim Rocks!
TMUX Rocks!
淺談探索 Linux 系統設計之道
from Source to Binary: How GNU Toolchain Works
Working Remotely (via SSH) Rocks!
用十分鐘 向jserv學習作業系統設計
ARM Trusted FirmwareのBL31を単体で使う!
Q2.12: Debugging with GDB

What's hot (20)

PPTX
QEMU - Binary Translation
PPTX
Advanced Debugging with GDB
PDF
JIT のコードを読んでみた
PDF
Interpreter, Compiler, JIT from scratch
PDF
Embedded Linux BSP Training (Intro)
PDF
GPGPU Seminar (GPGPU and CUDA Fortran)
PDF
GNU ld的linker script簡介
PDF
Let's trace Linux Lernel with KGDB @ COSCUP 2021
PDF
マーク&スイープ勉強会
PDF
Zynq MPSoC勉強会 Codec編
PPT
Introduction to gdb
PPTX
C#や.NET Frameworkがやっていること
PDF
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
PDF
Share the Experience of Using Embedded Development Board
PPTX
90分 Scheme to C(勝手に抄訳版)
PDF
GoでMinecraftっぽいの作る
PPTX
GCC RTL and Machine Description
PPTX
PDF
GDB Rocks!
PDF
What Can Compilers Do for Us?
QEMU - Binary Translation
Advanced Debugging with GDB
JIT のコードを読んでみた
Interpreter, Compiler, JIT from scratch
Embedded Linux BSP Training (Intro)
GPGPU Seminar (GPGPU and CUDA Fortran)
GNU ld的linker script簡介
Let's trace Linux Lernel with KGDB @ COSCUP 2021
マーク&スイープ勉強会
Zynq MPSoC勉強会 Codec編
Introduction to gdb
C#や.NET Frameworkがやっていること
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Share the Experience of Using Embedded Development Board
90分 Scheme to C(勝手に抄訳版)
GoでMinecraftっぽいの作る
GCC RTL and Machine Description
GDB Rocks!
What Can Compilers Do for Us?
Ad

Viewers also liked (20)

PDF
The Stack Frame
PDF
Smashing The Stack
PPTX
Introduction to Linux Exploit Development
PDF
Exploit techniques and mitigation
PPT
Introduction to pointers and memory management in C
PDF
Low Level Exploits
PPTX
How Functions Work
PDF
Insecure coding in C (and C++)
PDF
Ctf hello,world!
PDF
Basic of Exploitation
PPTX
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
PPTX
Cybermania Prelims
PDF
Kernel Recipes 2015: The stable Linux Kernel Tree - 10 years of insanity
PPTX
A particle filter based scheme for indoor tracking on an Android Smartphone
PDF
Linux performance
PPTX
Cybermania Mains
PDF
Rootkit 102 - Kernel-Based Rootkit
ODP
Linux Internals - Kernel/Core
PDF
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
PDF
LAS16-403 - GDB Linux Kernel Awareness
The Stack Frame
Smashing The Stack
Introduction to Linux Exploit Development
Exploit techniques and mitigation
Introduction to pointers and memory management in C
Low Level Exploits
How Functions Work
Insecure coding in C (and C++)
Ctf hello,world!
Basic of Exploitation
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
Cybermania Prelims
Kernel Recipes 2015: The stable Linux Kernel Tree - 10 years of insanity
A particle filter based scheme for indoor tracking on an Android Smartphone
Linux performance
Cybermania Mains
Rootkit 102 - Kernel-Based Rootkit
Linux Internals - Kernel/Core
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
LAS16-403 - GDB Linux Kernel Awareness
Ad

Similar to Debugging Applications with GNU Debugger (20)

PDF
Gdb tutorial-handout
PPTX
Debuging like a pro
PDF
lab1-ppt.pdf
PDF
gdb-tutorial.pdf
PPTX
GDB: A Lot More Than You Knew
PDF
Usage of GDB
PPTX
Debugging Modern C++ Application with Gdb
PPTX
Wavedigitech gdb
PPTX
Gnu debugger
PPTX
GNU Debugger
PPT
gdb.ppt
PDF
Debugging embedded devices using GDB
PPT
Gccgdb
PPTX
Rasperry pi Part 8
PDF
Debugger.pdf
PPTX
Reversing with gdb
PPT
gdb-debug analysis and commnds on gcc.ppt
PDF
GDB tutorial
PDF
PDF
Gdb cheat sheet
Gdb tutorial-handout
Debuging like a pro
lab1-ppt.pdf
gdb-tutorial.pdf
GDB: A Lot More Than You Knew
Usage of GDB
Debugging Modern C++ Application with Gdb
Wavedigitech gdb
Gnu debugger
GNU Debugger
gdb.ppt
Debugging embedded devices using GDB
Gccgdb
Rasperry pi Part 8
Debugger.pdf
Reversing with gdb
gdb-debug analysis and commnds on gcc.ppt
GDB tutorial
Gdb cheat sheet

More from Priyank Kapadia (15)

ODP
Ubuntu, Canonical and the release of Feisty
PDF
OLPC and INDIA
PDF
Open Source - Hip not Hype
ODP
How to start an Open Source Project
ODP
Developing Multilingual Applications
PDF
Open Solaris
ODP
How to build Debian packages
ODP
PDF
ASTERISK - Open Source PBS
ODP
C Types - Extending Python
ODP
Applying Security Algorithms Using openSSL crypto library
PDF
Authentication Modules For Linux - PAM Architecture
ODP
Google Web toolkit
PPT
Storage Management using LVM
PPT
Linux Kernel Development
Ubuntu, Canonical and the release of Feisty
OLPC and INDIA
Open Source - Hip not Hype
How to start an Open Source Project
Developing Multilingual Applications
Open Solaris
How to build Debian packages
ASTERISK - Open Source PBS
C Types - Extending Python
Applying Security Algorithms Using openSSL crypto library
Authentication Modules For Linux - PAM Architecture
Google Web toolkit
Storage Management using LVM
Linux Kernel Development

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Per capita expenditure prediction using model stacking based on satellite ima...
Mobile App Security Testing_ A Comprehensive Guide.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
Review of recent advances in non-invasive hemoglobin estimation
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
The Rise and Fall of 3GPP – Time for a Sabbatical?
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced methodologies resolving dimensionality complications for autism neur...
Diabetes mellitus diagnosis method based random forest with bat algorithm
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Debugging Applications with GNU Debugger

  • 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”