Skip to content

Instantly share code, notes, and snippets.

@groleo
Last active April 12, 2016 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groleo/4731938c7a207f388f8ed2233661c3b1 to your computer and use it in GitHub Desktop.
Save groleo/4731938c7a207f388f8ed2233661c3b1 to your computer and use it in GitHub Desktop.
GDB extension to automatically get the source files when doing a "backtrace"
# GDB python extension to pull the source files from a server, when executing "gdb backtrace"
# Modify file_server to match your needs
# Tested on gdb 7.10
# It depends on scp and password-less login.
# Adapted from a GDB example [https://sourceware.org/gdb/onlinedocs/gdb/Python.html#Python]
import gdb
from gdb.FrameDecorator import FrameDecorator
file_server='host where you've built'
gdb_dir="gdb-sources"
# Note: doesn't check if the files have changed, only if they exist
def get_file(fname):
os.makedirs(gdb_dir,exist_ok=True)
local_file = gdb_dir + os.path.sep + os.path.basename(fname)
if not os.path.isfile(local_file):
os.system('scp %s:%s %s' % (file_server,fname,local_file) )
class InlinedFrameDecorator(FrameDecorator):
def __init__(self, fobj):
super(InlinedFrameDecorator, self).__init__(fobj)
self.fobj = fobj
def filename(self):
fname = self.fobj.filename()
get_file(fname)
return fname
class InlineFilter():
def __init__(self):
# Frame filter attribute creation.
#
# 'name' is the name of the filter that GDB will display.
#
# 'priority' is the priority of the filter relative to other
# filters.
#
# 'enabled' is a boolean that indicates whether this filter is
# enabled and should be executed.
self.name = "InlinedFrameFilter"
self.priority = 100
self.enabled = True
# Register this frame filter with the global frame_filters
# dictionary.
gdb.frame_filters[self.name] = self
gdb.execute("dir %s" % gdb_dir)
def filter(self, frame_iter):
frame_iter = map(InlinedFrameDecorator,
frame_iter)
return frame_iter
InlineFilter()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment