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

Recommended

Vim Rocks!
Vim Rocks!
Kent Chen
 
Advanced Debugging with GDB
Advanced Debugging with GDB
David Khosid
 
Introduction to gdb
Introduction to gdb
Owen Hsu
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDB
Linaro
 
Working Remotely (via SSH) Rocks!
Working Remotely (via SSH) Rocks!
Kent Chen
 
TMUX Rocks!
TMUX Rocks!
Kent Chen
 
How A Compiler Works: GNU Toolchain
How A Compiler Works: GNU Toolchain
National Cheng Kung University
 
from Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Works
National Cheng Kung University
 
How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)
Sławomir Zborowski
 
JCL FOR FRESHERS
JCL FOR FRESHERS
Nirmal Pati
 
系統程式 -- 第 1 章
系統程式 -- 第 1 章
鍾誠 陳鍾誠
 
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
National Cheng Kung University
 
GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005
Saleem Ansari
 
系統程式 -- 第 7 章
系統程式 -- 第 7 章
鍾誠 陳鍾誠
 
Grub2 Booting Process
Grub2 Booting Process
Mike Wang
 
系統程式 -- 第 8 章
系統程式 -- 第 8 章
鍾誠 陳鍾誠
 
系統程式 - 附錄
系統程式 - 附錄
鍾誠 陳鍾誠
 
Toolchain
Toolchain
Anil Kumar Pugalia
 
系統程式 -- 附錄
系統程式 -- 附錄
鍾誠 陳鍾誠
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast Storage
Kernel TLV
 
Using gcov and lcov
Using gcov and lcov
test test
 
淺談探索 Linux 系統設計之道
淺談探索 Linux 系統設計之道
National Cheng Kung University
 
Linux device drivers
Linux device drivers
Emertxe Information Technologies Pvt Ltd
 
系統程式 -- 第 10 章 作業系統
系統程式 -- 第 10 章 作業系統
鍾誠 陳鍾誠
 
Linux basic commands with examples
Linux basic commands with examples
abclearnn
 
Android JNI
Android JNI
Siva Ramakrishna kv
 
Introduction to C++ over CLI
Introduction to C++ over CLI
建興 王
 
系統程式 -- 第 4 章
系統程式 -- 第 4 章
鍾誠 陳鍾誠
 
The Stack Frame
The Stack Frame
Ivo Marinkov
 
Smashing The Stack
Smashing The Stack
Daniele Bellavista
 

More Related Content

What's hot (20)

How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)
Sławomir Zborowski
 
JCL FOR FRESHERS
JCL FOR FRESHERS
Nirmal Pati
 
系統程式 -- 第 1 章
系統程式 -- 第 1 章
鍾誠 陳鍾誠
 
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
National Cheng Kung University
 
GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005
Saleem Ansari
 
系統程式 -- 第 7 章
系統程式 -- 第 7 章
鍾誠 陳鍾誠
 
Grub2 Booting Process
Grub2 Booting Process
Mike Wang
 
系統程式 -- 第 8 章
系統程式 -- 第 8 章
鍾誠 陳鍾誠
 
系統程式 - 附錄
系統程式 - 附錄
鍾誠 陳鍾誠
 
Toolchain
Toolchain
Anil Kumar Pugalia
 
系統程式 -- 附錄
系統程式 -- 附錄
鍾誠 陳鍾誠
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast Storage
Kernel TLV
 
Using gcov and lcov
Using gcov and lcov
test test
 
淺談探索 Linux 系統設計之道
淺談探索 Linux 系統設計之道
National Cheng Kung University
 
Linux device drivers
Linux device drivers
Emertxe Information Technologies Pvt Ltd
 
系統程式 -- 第 10 章 作業系統
系統程式 -- 第 10 章 作業系統
鍾誠 陳鍾誠
 
Linux basic commands with examples
Linux basic commands with examples
abclearnn
 
Android JNI
Android JNI
Siva Ramakrishna kv
 
Introduction to C++ over CLI
Introduction to C++ over CLI
建興 王
 
系統程式 -- 第 4 章
系統程式 -- 第 4 章
鍾誠 陳鍾誠
 
How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)
Sławomir Zborowski
 
JCL FOR FRESHERS
JCL FOR FRESHERS
Nirmal Pati
 
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
National Cheng Kung University
 
GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005
Saleem Ansari
 
Grub2 Booting Process
Grub2 Booting Process
Mike Wang
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast Storage
Kernel TLV
 
Using gcov and lcov
Using gcov and lcov
test test
 
系統程式 -- 第 10 章 作業系統
系統程式 -- 第 10 章 作業系統
鍾誠 陳鍾誠
 
Linux basic commands with examples
Linux basic commands with examples
abclearnn
 
Introduction to C++ over CLI
Introduction to C++ over CLI
建興 王
 

Viewers also liked (20)

The Stack Frame
The Stack Frame
Ivo Marinkov
 
Smashing The Stack
Smashing The Stack
Daniele Bellavista
 
Introduction to Linux Exploit Development
Introduction to Linux Exploit Development
johndegruyter
 
Exploit techniques and mitigation
Exploit techniques and mitigation
Yaniv Shani
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in C
Uri Dekel
 
Low Level Exploits
Low Level Exploits
hughpearse
 
How Functions Work
How Functions Work
Saumil Shah
 
Insecure coding in C (and C++)
Insecure coding in C (and C++)
Olve Maudal
 
Ctf hello,world!
Ctf hello,world!
Hacks in Taiwan (HITCON)
 
Basic of Exploitation
Basic of Exploitation
Jongseok Choi
 
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
Andrew Case
 
Cybermania Prelims
Cybermania Prelims
Divye Kapoor
 
Kernel Recipes 2015: The stable Linux Kernel Tree - 10 years of insanity
Kernel Recipes 2015: The stable Linux Kernel Tree - 10 years of insanity
Anne Nicolas
 
A particle filter based scheme for indoor tracking on an Android Smartphone
A particle filter based scheme for indoor tracking on an Android Smartphone
Divye Kapoor
 
Linux performance
Linux performance
Will Sterling
 
Cybermania Mains
Cybermania Mains
Divye Kapoor
 
Rootkit 102 - Kernel-Based Rootkit
Rootkit 102 - Kernel-Based Rootkit
Chia-Hao Tsai
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/Core
Shay Cohen
 
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
Kevin Lo
 
LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness
Peter Griffin
 
Introduction to Linux Exploit Development
Introduction to Linux Exploit Development
johndegruyter
 
Exploit techniques and mitigation
Exploit techniques and mitigation
Yaniv Shani
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in C
Uri Dekel
 
Low Level Exploits
Low Level Exploits
hughpearse
 
How Functions Work
How Functions Work
Saumil Shah
 
Insecure coding in C (and C++)
Insecure coding in C (and C++)
Olve Maudal
 
Basic of Exploitation
Basic of Exploitation
Jongseok Choi
 
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
Andrew Case
 
Cybermania Prelims
Cybermania Prelims
Divye Kapoor
 
Kernel Recipes 2015: The stable Linux Kernel Tree - 10 years of insanity
Kernel Recipes 2015: The stable Linux Kernel Tree - 10 years of insanity
Anne Nicolas
 
A particle filter based scheme for indoor tracking on an Android Smartphone
A particle filter based scheme for indoor tracking on an Android Smartphone
Divye Kapoor
 
Rootkit 102 - Kernel-Based Rootkit
Rootkit 102 - Kernel-Based Rootkit
Chia-Hao Tsai
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/Core
Shay Cohen
 
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
Kevin Lo
 
LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness
Peter Griffin
 
Ad

Similar to Debugging Applications with GNU Debugger (20)

Gdb tutorial-handout
Gdb tutorial-handout
Suraj Kumar
 
Debuging like a pro
Debuging like a pro
Vicente Bolea
 
lab1-ppt.pdf
lab1-ppt.pdf
AbdelrahmanElewah1
 
gdb-tutorial.pdf
gdb-tutorial.pdf
ligi14
 
GDB: A Lot More Than You Knew
GDB: A Lot More Than You Knew
Undo
 
Usage of GDB
Usage of GDB
Jongseok Choi
 
Debugging Modern C++ Application with Gdb
Debugging Modern C++ Application with Gdb
SenthilKumar Selvaraj
 
Wavedigitech gdb
Wavedigitech gdb
Wave Digitech
 
Gnu debugger
Gnu debugger
Gizem Çetin
 
GNU Debugger
GNU Debugger
Gizem Çetin
 
gdb.ppt
gdb.ppt
LavishGupta22
 
Debugging embedded devices using GDB
Debugging embedded devices using GDB
Chris Simmonds
 
Gccgdb
Gccgdb
selva raj
 
Rasperry pi Part 8
Rasperry pi Part 8
Techvilla
 
Debugger.pdf
Debugger.pdf
BuTriLn
 
Reversing with gdb
Reversing with gdb
Mihir Shah
 
GDB tutorial
GDB tutorial
Anurag Patel
 
MSL2009. Gdb
MSL2009. Gdb
Juan A. Suárez Romero
 
Gdb cheat sheet
Gdb cheat sheet
Piyush Mittal
 
Gdb
Gdb
Shantanu Sharma
 
Ad

More from Priyank Kapadia (15)

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

Recently uploaded (20)

"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Safe Software
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Safe Software
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 

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”