Introduction to GDB Python - lse.epita.fr · Introduction to GDB Python Pierre-Marie de Rodat Introduction Usage examples Conclusion..... GDB Well known source-level debugger from
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Well known source-level debugger from the GNUProjectCan be scripted using a quite resticting specificlanguage (one more to learn!)As far as I could read, it can only define macros
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
GDB
Well known source-level debugger from the GNUProjectCan be scripted using a quite resticting specificlanguage (one more to learn!)As far as I could read, it can only define macros
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
GDB
Well known source-level debugger from the GNUProjectCan be scripted using a quite resticting specificlanguage (one more to learn!)As far as I could read, it can only define macros
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
PythonGDB
GDB 7 introduced Python scripting capabilitiesIt can be accessed through a dedicated API (gdbmodule)This API enables scripts to:
Define pretty-printers for types defined in debuggedprograms (inferiors)Define new commands (like macros)Deal with symbols, stack frames, values typeInspect and modify inferiors memoryRegister callbacks for events (inferior termination,. . . )Set breakpoints. . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
PythonGDB
GDB 7 introduced Python scripting capabilitiesIt can be accessed through a dedicated API (gdbmodule)This API enables scripts to:
Define pretty-printers for types defined in debuggedprograms (inferiors)Define new commands (like macros)Deal with symbols, stack frames, values typeInspect and modify inferiors memoryRegister callbacks for events (inferior termination,. . . )Set breakpoints. . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
PythonGDB
GDB 7 introduced Python scripting capabilitiesIt can be accessed through a dedicated API (gdbmodule)This API enables scripts to:
Define pretty-printers for types defined in debuggedprograms (inferiors)Define new commands (like macros)Deal with symbols, stack frames, values typeInspect and modify inferiors memoryRegister callbacks for events (inferior termination,. . . )Set breakpoints. . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
PythonGDB
GDB 7 introduced Python scripting capabilitiesIt can be accessed through a dedicated API (gdbmodule)This API enables scripts to:
Define pretty-printers for types defined in debuggedprograms (inferiors)Define new commands (like macros)Deal with symbols, stack frames, values typeInspect and modify inferiors memoryRegister callbacks for events (inferior termination,. . . )Set breakpoints. . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
PythonGDB
GDB 7 introduced Python scripting capabilitiesIt can be accessed through a dedicated API (gdbmodule)This API enables scripts to:
Define pretty-printers for types defined in debuggedprograms (inferiors)Define new commands (like macros)Deal with symbols, stack frames, values typeInspect and modify inferiors memoryRegister callbacks for events (inferior termination,. . . )Set breakpoints. . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
PythonGDB
GDB 7 introduced Python scripting capabilitiesIt can be accessed through a dedicated API (gdbmodule)This API enables scripts to:
Define pretty-printers for types defined in debuggedprograms (inferiors)Define new commands (like macros)Deal with symbols, stack frames, values typeInspect and modify inferiors memoryRegister callbacks for events (inferior termination,. . . )Set breakpoints. . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
PythonGDB
GDB 7 introduced Python scripting capabilitiesIt can be accessed through a dedicated API (gdbmodule)This API enables scripts to:
Define pretty-printers for types defined in debuggedprograms (inferiors)Define new commands (like macros)Deal with symbols, stack frames, values typeInspect and modify inferiors memoryRegister callbacks for events (inferior termination,. . . )Set breakpoints. . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
PythonGDB
GDB 7 introduced Python scripting capabilitiesIt can be accessed through a dedicated API (gdbmodule)This API enables scripts to:
Define pretty-printers for types defined in debuggedprograms (inferiors)Define new commands (like macros)Deal with symbols, stack frames, values typeInspect and modify inferiors memoryRegister callbacks for events (inferior termination,. . . )Set breakpoints. . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Plan
...2 Usage examplesLoading scriptsDefining a pretty-printerDefining breakpointsConvenience functions
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Loading scripts
Most simple way (by hand):source my_script.py
gdb my_program -ex"source my_script.py"
Auto-loading (eg. for libraries):Enable auto-load python-scriptsRename your script to objfile-gdb.pyFix security-related settings (scripts-directory,safe-path, . . . )
Other strange ways (.debug_gdb_scripts) . . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Loading scripts
Most simple way (by hand):source my_script.py
gdb my_program -ex"source my_script.py"
Auto-loading (eg. for libraries):Enable auto-load python-scriptsRename your script to objfile-gdb.pyFix security-related settings (scripts-directory,safe-path, . . . )
Other strange ways (.debug_gdb_scripts) . . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Loading scripts
Most simple way (by hand):source my_script.py
gdb my_program -ex"source my_script.py"
Auto-loading (eg. for libraries):Enable auto-load python-scriptsRename your script to objfile-gdb.pyFix security-related settings (scripts-directory,safe-path, . . . )
Other strange ways (.debug_gdb_scripts) . . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Loading scripts
Most simple way (by hand):source my_script.py
gdb my_program -ex"source my_script.py"
Auto-loading (eg. for libraries):Enable auto-load python-scriptsRename your script to objfile-gdb.pyFix security-related settings (scripts-directory,safe-path, . . . )
Other strange ways (.debug_gdb_scripts) . . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Loading scripts
Most simple way (by hand):source my_script.py
gdb my_program -ex"source my_script.py"
Auto-loading (eg. for libraries):Enable auto-load python-scriptsRename your script to objfile-gdb.pyFix security-related settings (scripts-directory,safe-path, . . . )
Other strange ways (.debug_gdb_scripts) . . .
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Why? (1/2)
GDB itself can pretty-print process data followingthe (eg. C) layout (with -g)
Useful to debug low-level stuff, not algorithmsRegister pretty-printers for your data types and printthem!Container libraries (like the libstdc++) alreadybundle some
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Why? (2/2)
Useful to debug low-level stuff, not algorithmsRegister pretty-printers for your data types and printthem!Container libraries (like the libstdc++) alreadybundle some
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Why? (2/2)
Useful to debug low-level stuff, not algorithmsRegister pretty-printers for your data types and printthem!Container libraries (like the libstdc++) alreadybundle some
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Example — Definition
import gdb
class MyListPrinter(object):’’’Print a struct my_list (*)’’’
Pretty-printer matching can be very complexThink about generic data structure in C++. . .GDB has a list of (user provided) "value handlers" tolook for a pretty-printer"handlers" are called with the value to printIf one of them returns a pretty-printer, GDB uses it.
Pretty-printer matching can be very complexThink about generic data structure in C++. . .GDB has a list of (user provided) "value handlers" tolook for a pretty-printer"handlers" are called with the value to printIf one of them returns a pretty-printer, GDB uses it.
Pretty-printer matching can be very complexThink about generic data structure in C++. . .GDB has a list of (user provided) "value handlers" tolook for a pretty-printer"handlers" are called with the value to printIf one of them returns a pretty-printer, GDB uses it.
Pretty-printer matching can be very complexThink about generic data structure in C++. . .GDB has a list of (user provided) "value handlers" tolook for a pretty-printer"handlers" are called with the value to printIf one of them returns a pretty-printer, GDB uses it.
Pretty-printer matching can be very complexThink about generic data structure in C++. . .GDB has a list of (user provided) "value handlers" tolook for a pretty-printer"handlers" are called with the value to printIf one of them returns a pretty-printer, GDB uses it.
prefix = prefix.string()frame = gdb.newest_frame().older()fr_name = frame.name()return not fr_name.startswith(prefix)
ForbiddenCaller()
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examplesLoading scripts
Defining a pretty-printer
Defining breakpoints
Convenience functions
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Example — Usage
(gdb) source my_script.py(gdb) b internal_callback if $forbidden_call("internal_")Breakpoint 1 at 0x400507: file callback.c, line 7.(gdb) rStarting program: /tmp/callbackInternal callback call with no = 205
Breakpoint 1, internal_callback (no=204) at callback.c:77 printf("Internal callback call with no = %d\n", no);(gdb) bt#0 internal_callback (no=204) at callback.c:7#1 0x000000000040054f in main () at callback.c:23
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Plan
...3 Conclusion
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Conclusion
The Python API is really easy to useIt looks more powerful than the specific languageIt is still evolving: stay tuned!
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Conclusion
The Python API is really easy to useIt looks more powerful than the specific languageIt is still evolving: stay tuned!
Introduction toGDB Python
Pierre-Marie deRodat
Introduction
Usage examples
Conclusion
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Conclusion
The Python API is really easy to useIt looks more powerful than the specific languageIt is still evolving: stay tuned!