PyBay 2019 talk by Lisa Roach.
Use the GDB python extensions to write custom GDB commands in python and build a tool to inject code into a running Python process to analyze its memory usage.
13. 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
43. 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/
Editor's Notes
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.
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.
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.
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.
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.
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.
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
You can pass C Code into GDB execute and it will be sent directly to the infererior process
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
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
You can pass C Code into GDB execute and it will be sent directly to the infererior process
You can pass C Code into GDB execute and it will be sent directly to the infererior process
You can pass C Code into GDB execute and it will be sent directly to the infererior process