Last active
May 22, 2018 09:07
-
-
Save disconnect3d/4bfedfb49cc4aa26fca3712bc822f087 to your computer and use it in GitHub Desktop.
GDB command that adds a break with prefix functionality; load in GDB with `source breakprefix.py`
This file contains hidden or 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
""" | |
Adds a command to GDB that let you specify prefix for a breakpoint. | |
E.g. you want to invoke `z foo` and make it so that it creates a breakpoint on `packageX.foo`. | |
With this command you just do `z prefix packageX.` and then `z SOMETHING` will | |
set a breakpoint at `packageX.SOMETHING`. | |
For more info on GDB API refer to its docs: | |
https://sourceware.org/gdb/onlinedocs/gdb/Commands-In-Python.html#Commands-In-Python | |
""" | |
import gdb | |
COMMAND_NAME = 'z' | |
class BreakWithPrefix(gdb.Command): | |
"""Breaks with given prefix""" | |
def __init__(self): | |
super(BreakWithPrefix, self).__init__(COMMAND_NAME, gdb.COMMAND_USER) | |
self.prefix = None | |
def invoke(self, arg, from_tty): | |
prefix_str = 'prefix' | |
if arg.startswith(prefix_str): | |
self.prefix = arg[len(prefix_str)+1:] | |
print('The command %s will break using prefix %s from now' % (COMMAND_NAME, self.prefix)) | |
elif not self.prefix: | |
print('You need to apply a prefix first with "%s %s <prefix>"' % (COMMAND_NAME, prefix_str)) | |
else: | |
cmd = 'break %s%s' % (self.prefix, arg) | |
print("Executing '%s' command" % cmd) | |
gdb.execute(cmd) | |
# registering command with GDB | |
BreakWithPrefix() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment