SlideShare a Scribd company logo
Extending GDB With Python
Lisa Roach
About Me
● CPython Core Developer
● Facebook Production Engineer on the Python Foundation
team
What is Today’s Goal?
Access a running Python process and
perform live debugging beyond GDB’s
typical capabilities
GDB Primer
Allows you to see what is
going on ‘inside’ a program
while it executes.
GDB – The GNU Project Debugger
GDB- What can it do?
● Read and write data from the program
What languages are supported?
● Ada
● Assembly
● C
● C++
● D
● Fortran
● Go
● Objective-C
● OpenCL
● Modula-2
● Pascal
● Rust
GDB Commands
● break <line-number|function-name>
● next
● help
● call <expr>
A text file containing GDB
commands that are executed
sequentially (unless flow
control changes the order).
GDB Command Files
source <filename>
gdb –x <filename>
GDB Command Files
Extending GDB
./configure --with-python
+
Python Debug Symbols
What does the GDB python extension get us?
1. Access high-level CPython interpreter information
from GDB
2. Access GDB commands from Python code
3. Build custom GDB commands written in Python
Access Python with GDB - Basic Commands
● python [command]
● py-list
● py-bt
● py-up/py-down
● py-locals
● py-print
Extending GDB with Python
Access GDB From Python
GDB CPython System Python
Extending GDB with Python
gdb_commands.py
Let’s not write code in the interpreter
Let’s not write code in the interpreter
Access GDB From Python
Execute Code in the Debugged Process
Execute Code in the Debugged Process
call <expr>
I’m a Python Developer! I don’t know C!
C API
C API
Execute Code in the Native Process
gdb.execute(“call <C Code>”)
Execute Code in the Debugged Process
Extending GDB with Python
Execute Code in the Native Process
The GIL
C API
● PyGILState_Ensure()
● PyGILState_Release()
● Video of the above happening
Build Custom GDB Commands
gdb_commands.py
Extending GDB with Python
Use Case Example
Debugging memory usage in a
running Python process
Step 1: Write a Python Script to Analyze Memory
memory_analysis.py
Step 2: Acquire the GIL
gdb_commands.py
Step 3: Create a Custom GDB Command
gdb_commands.py
Step 4: Execute the New Command
gdb_commands.py
Step 5: Launch GDB with the Command File
main.py
Step 6: Run
Extending GDB with Python
Conclusion
● See the full code:
○ https://github.com/facebookincubator/memory-analyzer
● GDB Documentation:
○ https://sourceware.org/gdb/onlinedocs/gdb/Python.html#Python
● Python GDB Documentation
○ https://devguide.python.org/gdb/
Ad

Recommended

PDF
Quand l'intelligence artificielle s'invite au bloc opératoire Eric VIbert.pdf
Centre Hepato-Biliaire / AP-HP Hopital Paul Brousse
 
PDF
Rapport de Soutenance 1
BartOunay
 
PDF
Why I Am a Software Engineer
Craig Saunders
 
PDF
HCL Notes und Nomad Fehlerbehebung für Dummies
panagenda
 
PDF
達人プログラマーになろう!!.pdf
yoshinorimatsuda
 
PPS
Solução TELEMÁTICA para controle e modernização de arenas e estádios
Telemática Sistemas Inteligentes
 
PDF
Termos eléctricos Cointra
AhorraClima.es
 
PDF
Internet of things
Sara Scotti
 
ODP
BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...
Hemmerling
 
PPTX
GNU Debugger
Gizem Çetin
 
PPTX
Gnu debugger
Gizem Çetin
 
PDF
lab1-ppt.pdf
AbdelrahmanElewah1
 
PDF
Dev + DevOps для PHP розробника
phpfriendsclub
 
PPTX
Debugging Modern C++ Application with Gdb
SenthilKumar Selvaraj
 
PDF
MSL2009. Gdb
Juan A. Suárez Romero
 
PDF
Debugging of (C)Python applications
Roman Podoliaka
 
PDF
Putting the Fun into Functioning CI/CD with JHipster
Gerard Gigliotti
 
PDF
Web technologies for desktop development
Darko Kukovec
 
PDF
gdb-tutorial.pdf
ligi14
 
PDF
Webinar - Unbox GitLab CI/CD
Annie Huang
 
PDF
Debugging Hung Python Processes With GDB
bmbouter
 
PPTX
Process (OS),GNU,Introduction to Linux oS
Harrytoye2
 
PDF
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io
 
PDF
Debugging embedded devices using GDB
Chris Simmonds
 
PDF
It's a Breeze to develop Apache Airflow (Apache Con Berlin)
Jarek Potiuk
 
PPTX
Debuging like a pro
Vicente Bolea
 
PDF
AIS Technical Development Workshop 3: Getting Started with Git and GitHub
Nhi Nguyen
 
PDF
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
PPTX
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 

More Related Content

Similar to Extending GDB with Python (20)

ODP
BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...
Hemmerling
 
PPTX
GNU Debugger
Gizem Çetin
 
PPTX
Gnu debugger
Gizem Çetin
 
PDF
lab1-ppt.pdf
AbdelrahmanElewah1
 
PDF
Dev + DevOps для PHP розробника
phpfriendsclub
 
PPTX
Debugging Modern C++ Application with Gdb
SenthilKumar Selvaraj
 
PDF
MSL2009. Gdb
Juan A. Suárez Romero
 
PDF
Debugging of (C)Python applications
Roman Podoliaka
 
PDF
Putting the Fun into Functioning CI/CD with JHipster
Gerard Gigliotti
 
PDF
Web technologies for desktop development
Darko Kukovec
 
PDF
gdb-tutorial.pdf
ligi14
 
PDF
Webinar - Unbox GitLab CI/CD
Annie Huang
 
PDF
Debugging Hung Python Processes With GDB
bmbouter
 
PPTX
Process (OS),GNU,Introduction to Linux oS
Harrytoye2
 
PDF
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io
 
PDF
Debugging embedded devices using GDB
Chris Simmonds
 
PDF
It's a Breeze to develop Apache Airflow (Apache Con Berlin)
Jarek Potiuk
 
PPTX
Debuging like a pro
Vicente Bolea
 
PDF
AIS Technical Development Workshop 3: Getting Started with Git and GitHub
Nhi Nguyen
 
BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...
Hemmerling
 
GNU Debugger
Gizem Çetin
 
Gnu debugger
Gizem Çetin
 
lab1-ppt.pdf
AbdelrahmanElewah1
 
Dev + DevOps для PHP розробника
phpfriendsclub
 
Debugging Modern C++ Application with Gdb
SenthilKumar Selvaraj
 
Debugging of (C)Python applications
Roman Podoliaka
 
Putting the Fun into Functioning CI/CD with JHipster
Gerard Gigliotti
 
Web technologies for desktop development
Darko Kukovec
 
gdb-tutorial.pdf
ligi14
 
Webinar - Unbox GitLab CI/CD
Annie Huang
 
Debugging Hung Python Processes With GDB
bmbouter
 
Process (OS),GNU,Introduction to Linux oS
Harrytoye2
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io
 
Debugging embedded devices using GDB
Chris Simmonds
 
It's a Breeze to develop Apache Airflow (Apache Con Berlin)
Jarek Potiuk
 
Debuging like a pro
Vicente Bolea
 
AIS Technical Development Workshop 3: Getting Started with Git and GitHub
Nhi Nguyen
 

Recently uploaded (20)

PDF
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
PPTX
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
PDF
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PDF
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
PDF
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
PDF
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
PDF
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
PDF
The Growing Value and Application of FME & GenAI
Safe Software
 
PDF
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PDF
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
PDF
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
PPTX
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
PPTX
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
PDF
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
PDF
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
The Growing Value and Application of FME & GenAI
Safe Software
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Ad

Extending GDB with Python

Editor's Notes

  • #6: Start your program, specifying anything that might affect its behavior. Make your program stop on specified conditions. Examine what has happened, when your program has stopped. Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
  • #7: Start your program, specifying anything that might affect its behavior. Make your program stop on specified conditions. Examine what has happened, when your program has stopped. Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
  • #9: Part of the linux kernel. Allows a process the control the execution of another, and can poke at the innards. Has access to all the kernel level information about the process. GDB also works on non-linux systems, but I don’t have time to dive into all the specifics there.
  • #17: Start your program, specifying anything that might affect its behavior. Make your program stop on specified conditions. Examine what has happened, when your program has stopped. Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
  • #20: Start your program, specifying anything that might affect its behavior. Make your program stop on specified conditions. Examine what has happened, when your program has stopped. Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
  • #23: Now you can access information about Python using GDB, but there is a second part to the extension. You can access GDB from Python. Which means we can control GDB (meaning ptrace under the hood for linux) and script it to do interesting and repeatable debugging on Python code (or beyond Python code!). You can only access the gdb module from within python if you are using gdb, you cannot access it from a standard interpreter session.
  • #28: Executing Python code from GDB will just execute code in the GDB process To execute code in the inferior process we need to use: Gdb.execute executes the string command passed to it as a gdb CLI command
  • #29: You can pass C Code into GDB execute and it will be sent directly to the infererior process
  • #30: Executing Python code from GDB will just execute code in the GDB process To execute code in the inferior process we need to use: Gdb.execute executes the string command passed to it as a gdb CLI command
  • #33: Can execute functions that have symbols loaded in GDB --with-python extensions and python debug symbols load symbols for the Cpython interpreter, we don’t have symbols have Python code Cannot run print(‘hello world”) no symbol in current context
  • #37: You can pass C Code into GDB execute and it will be sent directly to the infererior process
  • #38: You can pass C Code into GDB execute and it will be sent directly to the infererior process
  • #40: You can pass C Code into GDB execute and it will be sent directly to the infererior process