Created
November 5, 2014 16:48
-
-
Save nojhan/c3bc28e2fa0608f21551 to your computer and use it in GitHub Desktop.
Manipulate the output of GDB with a python function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We want to be able to manipulate the output of GDB with a python function. | |
####################################################################### | |
# Example 1: Defining the hook as a python function that would gdb.execute the same command | |
# Problem: The command is executed twice and we can't access args in the hook. | |
####################################################################### | |
python | |
import gdb | |
class Run_py(gdb.Command): | |
def __init__ (self): | |
# Bind to run as if we wanted to replace it | |
super (Run_py, self).__init__ ("run_py", gdb.COMMAND_SUPPORT, gdb.COMPLETE_FILENAME) | |
def invoke (self, arg, from_tty): | |
print("invoke run_py") | |
# Call the underlying native command... | |
res = gdb.execute("run "+arg, to_string=True) | |
# Let's pretend we have a pythonic grep | |
# print(pygrep(res)) | |
Run_py() | |
end | |
define hook-run | |
python | |
# Problem: How to access args? | |
print("execute run") | |
res = gdb.execute("run_py", to_string=True) | |
print(res) | |
end | |
end | |
####################################################################### | |
# Example 2: overload the command with a python class | |
# Problem: maximum recursion depth exceeded while calling a Python object | |
####################################################################### | |
# python | |
# | |
# import gdb | |
# | |
# class Run_py(gdb.Command): | |
# def __init__ (self): | |
# # Bind to run as if we wanted to replace it | |
# super (Run_py, self).__init__ ("run", gdb.COMMAND_SUPPORT, gdb.COMPLETE_FILENAME) | |
# | |
# def invoke (self, arg, from_tty): | |
# # Call the underlying native command... | |
# # ... fails in a recursive trap. | |
# res = gdb.execute("run "+arg, to_string=True) | |
# # Let's pretend we have a pythonic grep | |
# #print(pygrep(res)) | |
# | |
# Run_py() | |
# end | |
####################################################################### | |
# Example 3: use pipe to communicate with a python program | |
# Problem: the time taken to load and execute the external python program | |
# and thus the ugly sleep hack. | |
####################################################################### | |
# # Don't wrap line or the coloring regexp won't work. | |
# set width 0 | |
# | |
# # Create a named pipe to get outputs from gdb | |
# shell test -e /tmp/gdbpipe && rm /tmp/gdbpipe | |
# shell mkfifo /tmp/gdbpipe | |
# | |
# define logging_on | |
# # Instead of printing on stdout only, log everything... | |
# set logging redirect on | |
# # ... in our named pipe. | |
# set logging on /tmp/gdbpipe | |
# end | |
# | |
# define logging_off | |
# set logging off | |
# set logging redirect off | |
# # Because both gdb and our commands are writing on the same pipe at the same | |
# # time, it is more than probable that gdb will end before our (higher level) | |
# # commands. The gdb prompt will thus render before the result of the command, | |
# # which is highly akward. To prevent this, we need to wait before displaying | |
# # the prompt again. The more your commands are complex, the higher you will | |
# # need to set this. | |
# shell sleep 0.4s | |
# end | |
# | |
# define hook-run | |
# # Let's pretend we have a pythonic grep | |
# shell cat /tmp/gdbpipe | pygrep "Program received signal" & | |
# logging_on | |
# end | |
# define hookpost-run | |
# logging_off | |
# end | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment