Created
September 5, 2022 18:47
-
-
Save LukasWoodtli/e3db4093d272ef43ca87453ae731d969 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
import subprocess | |
import re | |
import gdb | |
# This is just an example and might not work poperly! | |
class BreakpointAtMain(gdb.Breakpoint): | |
def __init__(self): | |
super(BreakpointAtMain, self).__init__("main") | |
def stop(self): | |
gdb.write("stopping at main()") | |
return True | |
class TracePoint(gdb.Breakpoint): | |
def __init__(self, tracePoint, traceObj, txt=None): | |
super(TracePoint, self).__init__(tracePoint) | |
self.traceObj = traceObj | |
self.txt = txt | |
def stop(self): | |
txt = "OUTPUT: " | |
if self.txt: | |
txt += self.txt | |
gdb.write(txt + "\n") | |
gdb.write(self.traceObj + ": ") | |
gdb.write(gdb.parse_and_eval(self.traceObj) + "\n") | |
return True | |
class Executor: | |
def __init__(self, cmd): | |
self.__cmd = cmd | |
def __call__(self): | |
gdb.execute(self.__cmd) | |
def event_handler(event): | |
if (isinstance(event, gdb.BreakpointEvent)): | |
gdb.write("\n[BP]") | |
return | |
if (isinstance(event, gdb.SignalEvent)): | |
gdb.execute("set scheduler-locking on") | |
frame = gdb.selected_frame() | |
cont = False | |
while frame: | |
function = str(frame.name()) | |
if "jvm" in function or "Java" in function: | |
#gdb.write("Will break") | |
cont = True | |
frame = frame.older() | |
gdb.execute("set scheduler-locking off") | |
if cont: | |
gdb.post_event(Executor("continue")) | |
def main(): | |
gdb.execute("set auto-solib-add on") | |
gdb.execute('file testexecutable') | |
gdb.execute("set args foo bar") | |
gdb.execute('set logging on') | |
gdb.execute('set logging file gdbout') | |
# not working: gdb.execute("set scheduler-locking step") | |
gdb.execute('shared mylib') | |
gdb.execute('delete') # delete all old breakpoints | |
# gdb.execute('catch throw') | |
#gdb.execute('handle SIGSEGV noprint') | |
gdb.execute('handle SIGSEGV stop') | |
gdb.events.stop.connect(event_handler) | |
# BreakpointAtMain() | |
#gdb.Breakpoint("Myclass::baz") | |
#bp = gdb.Breakpoint('break main.cpp:10') | |
TracePoint("main.cpp:14", | |
"foo.m_bar.size()", "bla") | |
gdb.execute('run') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment