Created
February 7, 2020 19:57
-
-
Save vedantk/7602b20a9e1d44c42d32dcca36591cc0 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
from __future__ import print_function | |
import lldb | |
def doit(dbg, cmd): | |
print('::', cmd) | |
dbg.HandleCommand(cmd) | |
def should_stop_stepping(process): | |
state = process.GetState() | |
if state in (lldb.eStateExited, lldb.eStateDetached): | |
print('Process has exited or has been detached, exiting...') | |
return True | |
if state in (lldb.eStateCrashed, lldb.eStateInvalid): | |
print('Process has crashed or is in an invalid state, exiting...') | |
return True | |
return False | |
def alter_PC(dbg, process, cmd): | |
# Check the process state /after/ we step. Any time we advance the PC, | |
# the process state may change. | |
doit(dbg, cmd) | |
return should_stop_stepping(process) | |
def __lldb_init_module(dbg, internal_dict): | |
# Make each debugger command synchronous. | |
dbg.SetAsync(False) | |
# Run the program and stop it when it reaches main(). | |
doit(dbg, 'breakpoint set -n main') | |
doit(dbg, 'run') | |
# Step through the program until it exits. | |
gen = 0 | |
target = dbg.GetSelectedTarget() | |
process = target.GetProcess() | |
visited_pcs = set() | |
while True: | |
gen += 1 | |
print(':: Generation', gen) | |
if alter_PC(dbg, process, 'step'): | |
break | |
thread = process.GetSelectedThread() | |
frame = thread.GetSelectedFrame() | |
# Don't spend a lot of time at the same PC. | |
cur_pc = frame.GetPC() | |
if cur_pc in visited_pcs: | |
continue | |
visited_pcs.add(cur_pc) | |
# FIXME: We need a way to quickly step out of fully-covered functions. | |
# Exercise `bt` and `frame variable`. | |
doit(dbg, 'bt') | |
doit(dbg, 'frame variable') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment