Fix PEP8 in lldb_batchmode.py
This commit is contained in:
parent
958dea1745
commit
f697a06da4
1 changed files with 103 additions and 97 deletions
|
@ -30,36 +30,35 @@ import sys
|
||||||
import threading
|
import threading
|
||||||
import thread
|
import thread
|
||||||
import re
|
import re
|
||||||
import atexit
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# Set this to True for additional output
|
# Set this to True for additional output
|
||||||
DEBUG_OUTPUT = False
|
DEBUG_OUTPUT = False
|
||||||
|
|
||||||
|
|
||||||
def print_debug(s):
|
def print_debug(s):
|
||||||
"Print something if DEBUG_OUTPUT is True"
|
"Print something if DEBUG_OUTPUT is True"
|
||||||
global DEBUG_OUTPUT
|
global DEBUG_OUTPUT
|
||||||
if DEBUG_OUTPUT:
|
if DEBUG_OUTPUT:
|
||||||
print("DEBUG: " + str(s))
|
print("DEBUG: " + str(s))
|
||||||
|
|
||||||
|
|
||||||
def normalize_whitespace(s):
|
def normalize_whitespace(s):
|
||||||
"Replace newlines, tabs, multiple spaces, etc with exactly one space"
|
"Replace newlines, tabs, multiple spaces, etc with exactly one space"
|
||||||
return re.sub("\s+", " ", s)
|
return re.sub("\s+", " ", s)
|
||||||
|
|
||||||
|
|
||||||
# This callback is registered with every breakpoint and makes sure that the frame containing the
|
|
||||||
# breakpoint location is selected
|
|
||||||
def breakpoint_callback(frame, bp_loc, dict):
|
def breakpoint_callback(frame, bp_loc, dict):
|
||||||
"Called whenever a breakpoint is hit"
|
"""This callback is registered with every breakpoint and makes sure that the
|
||||||
print("Hit breakpoint " + str(bp_loc))
|
frame containing the breakpoint location is selected"""
|
||||||
|
print("Hit breakpoint " + str(bp_loc))
|
||||||
|
|
||||||
# Select the frame and the thread containing it
|
# Select the frame and the thread containing it
|
||||||
frame.thread.process.SetSelectedThread(frame.thread)
|
frame.thread.process.SetSelectedThread(frame.thread)
|
||||||
frame.thread.SetSelectedFrame(frame.idx)
|
frame.thread.SetSelectedFrame(frame.idx)
|
||||||
|
|
||||||
# Returning True means that we actually want to stop at this breakpoint
|
# Returning True means that we actually want to stop at this breakpoint
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# This is a list of breakpoints that are not registered with the breakpoint callback. The list is
|
# This is a list of breakpoints that are not registered with the breakpoint callback. The list is
|
||||||
|
@ -70,91 +69,99 @@ new_breakpoints = []
|
||||||
# used to avoid hooking callbacks into breakpoints more than once
|
# used to avoid hooking callbacks into breakpoints more than once
|
||||||
registered_breakpoints = set()
|
registered_breakpoints = set()
|
||||||
|
|
||||||
|
|
||||||
def execute_command(command_interpreter, command):
|
def execute_command(command_interpreter, command):
|
||||||
"Executes a single CLI command"
|
"Executes a single CLI command"
|
||||||
global new_breakpoints
|
global new_breakpoints
|
||||||
global registered_breakpoints
|
global registered_breakpoints
|
||||||
|
|
||||||
res = lldb.SBCommandReturnObject()
|
res = lldb.SBCommandReturnObject()
|
||||||
print(command)
|
print(command)
|
||||||
command_interpreter.HandleCommand(command, res)
|
command_interpreter.HandleCommand(command, res)
|
||||||
|
|
||||||
if res.Succeeded():
|
if res.Succeeded():
|
||||||
if res.HasResult():
|
if res.HasResult():
|
||||||
print(normalize_whitespace(res.GetOutput()), end = '\n')
|
print(normalize_whitespace(res.GetOutput()), end='\n')
|
||||||
|
|
||||||
# If the command introduced any breakpoints, make sure to register them with the breakpoint
|
# If the command introduced any breakpoints, make sure to register
|
||||||
# callback
|
# them with the breakpoint
|
||||||
while len(new_breakpoints) > 0:
|
# callback
|
||||||
res.Clear()
|
while len(new_breakpoints) > 0:
|
||||||
breakpoint_id = new_breakpoints.pop()
|
res.Clear()
|
||||||
|
breakpoint_id = new_breakpoints.pop()
|
||||||
|
|
||||||
if breakpoint_id in registered_breakpoints:
|
if breakpoint_id in registered_breakpoints:
|
||||||
print_debug("breakpoint with id %s is already registered. Ignoring." % str(breakpoint_id))
|
print_debug("breakpoint with id %s is already registered. Ignoring." %
|
||||||
else:
|
str(breakpoint_id))
|
||||||
print_debug("registering breakpoint callback, id = " + str(breakpoint_id))
|
else:
|
||||||
callback_command = "breakpoint command add -F breakpoint_callback " + str(breakpoint_id)
|
print_debug("registering breakpoint callback, id = " + str(breakpoint_id))
|
||||||
command_interpreter.HandleCommand(callback_command, res)
|
callback_command = ("breakpoint command add -F breakpoint_callback " +
|
||||||
if res.Succeeded():
|
str(breakpoint_id))
|
||||||
print_debug("successfully registered breakpoint callback, id = " + str(breakpoint_id))
|
command_interpreter.HandleCommand(callback_command, res)
|
||||||
registered_breakpoints.add(breakpoint_id)
|
if res.Succeeded():
|
||||||
else:
|
print_debug("successfully registered breakpoint callback, id = " +
|
||||||
print("Error while trying to register breakpoint callback, id = " + str(breakpoint_id))
|
str(breakpoint_id))
|
||||||
else:
|
registered_breakpoints.add(breakpoint_id)
|
||||||
print(res.GetError())
|
else:
|
||||||
|
print("Error while trying to register breakpoint callback, id = " +
|
||||||
|
str(breakpoint_id))
|
||||||
|
else:
|
||||||
|
print(res.GetError())
|
||||||
|
|
||||||
|
|
||||||
def start_breakpoint_listener(target):
|
def start_breakpoint_listener(target):
|
||||||
"Listens for breakpoints being added and adds new ones to the callback registration list"
|
"""Listens for breakpoints being added and adds new ones to the callback
|
||||||
listener = lldb.SBListener("breakpoint listener")
|
registration list"""
|
||||||
|
listener = lldb.SBListener("breakpoint listener")
|
||||||
|
|
||||||
def listen():
|
def listen():
|
||||||
event = lldb.SBEvent()
|
event = lldb.SBEvent()
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
if listener.WaitForEvent(120, event):
|
if listener.WaitForEvent(120, event):
|
||||||
if lldb.SBBreakpoint.EventIsBreakpointEvent(event) and \
|
if lldb.SBBreakpoint.EventIsBreakpointEvent(event) and \
|
||||||
lldb.SBBreakpoint.GetBreakpointEventTypeFromEvent(event) == \
|
lldb.SBBreakpoint.GetBreakpointEventTypeFromEvent(event) == \
|
||||||
lldb.eBreakpointEventTypeAdded:
|
lldb.eBreakpointEventTypeAdded:
|
||||||
global new_breakpoints
|
global new_breakpoints
|
||||||
breakpoint = lldb.SBBreakpoint.GetBreakpointFromEvent(event)
|
breakpoint = lldb.SBBreakpoint.GetBreakpointFromEvent(event)
|
||||||
print_debug("breakpoint added, id = " + str(breakpoint.id))
|
print_debug("breakpoint added, id = " + str(breakpoint.id))
|
||||||
new_breakpoints.append(breakpoint.id)
|
new_breakpoints.append(breakpoint.id)
|
||||||
except:
|
except:
|
||||||
print_debug("breakpoint listener shutting down")
|
print_debug("breakpoint listener shutting down")
|
||||||
|
|
||||||
# Start the listener and let it run as a daemon
|
# Start the listener and let it run as a daemon
|
||||||
listener_thread = threading.Thread(target = listen)
|
listener_thread = threading.Thread(target=listen)
|
||||||
listener_thread.daemon = True
|
listener_thread.daemon = True
|
||||||
listener_thread.start()
|
listener_thread.start()
|
||||||
|
|
||||||
# Register the listener with the target
|
# Register the listener with the target
|
||||||
target.GetBroadcaster().AddListener(listener, lldb.SBTarget.eBroadcastBitBreakpointChanged)
|
target.GetBroadcaster().AddListener(listener, lldb.SBTarget.eBroadcastBitBreakpointChanged)
|
||||||
|
|
||||||
|
|
||||||
def start_watchdog():
|
def start_watchdog():
|
||||||
"Starts a watchdog thread that will terminate the process after a certain period of time"
|
"""Starts a watchdog thread that will terminate the process after a certain
|
||||||
watchdog_start_time = time.clock()
|
period of time"""
|
||||||
watchdog_max_time = watchdog_start_time + 30
|
watchdog_start_time = time.clock()
|
||||||
|
watchdog_max_time = watchdog_start_time + 30
|
||||||
|
|
||||||
def watchdog():
|
def watchdog():
|
||||||
while time.clock() < watchdog_max_time:
|
while time.clock() < watchdog_max_time:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
print("TIMEOUT: lldb_batchmode.py has been running for too long. Aborting!")
|
print("TIMEOUT: lldb_batchmode.py has been running for too long. Aborting!")
|
||||||
thread.interrupt_main()
|
thread.interrupt_main()
|
||||||
|
|
||||||
# Start the listener and let it run as a daemon
|
# Start the listener and let it run as a daemon
|
||||||
watchdog_thread = threading.Thread(target = watchdog)
|
watchdog_thread = threading.Thread(target=watchdog)
|
||||||
watchdog_thread.daemon = True
|
watchdog_thread.daemon = True
|
||||||
watchdog_thread.start()
|
watchdog_thread.start()
|
||||||
|
|
||||||
####################################################################################################
|
####################################################################################################
|
||||||
# ~main
|
# ~main
|
||||||
####################################################################################################
|
####################################################################################################
|
||||||
|
|
||||||
if len(sys.argv) != 3:
|
if len(sys.argv) != 3:
|
||||||
print("usage: python lldb_batchmode.py target-path script-path")
|
print("usage: python lldb_batchmode.py target-path script-path")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
target_path = sys.argv[1]
|
target_path = sys.argv[1]
|
||||||
script_path = sys.argv[2]
|
script_path = sys.argv[2]
|
||||||
|
@ -181,9 +188,9 @@ target_error = lldb.SBError()
|
||||||
target = debugger.CreateTarget(target_path, None, None, True, target_error)
|
target = debugger.CreateTarget(target_path, None, None, True, target_error)
|
||||||
|
|
||||||
if not target:
|
if not target:
|
||||||
print("Could not create debugging target '" + target_path + "': " + str(target_error) +
|
print("Could not create debugging target '" + target_path + "': " +
|
||||||
". Aborting.", file=sys.stderr)
|
str(target_error) + ". Aborting.", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
# Register the breakpoint callback for every breakpoint
|
# Register the breakpoint callback for every breakpoint
|
||||||
|
@ -192,22 +199,21 @@ start_breakpoint_listener(target)
|
||||||
command_interpreter = debugger.GetCommandInterpreter()
|
command_interpreter = debugger.GetCommandInterpreter()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
script_file = open(script_path, 'r')
|
script_file = open(script_path, 'r')
|
||||||
|
|
||||||
for line in script_file:
|
for line in script_file:
|
||||||
command = line.strip()
|
command = line.strip()
|
||||||
if command == "run" or command == "r" or re.match("^process\s+launch.*", command):
|
if command == "run" or command == "r" or re.match("^process\s+launch.*", command):
|
||||||
# Before starting to run the program, let the thread sleep a bit, so all
|
# Before starting to run the program, let the thread sleep a bit, so all
|
||||||
# breakpoint added events can be processed
|
# breakpoint added events can be processed
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
if command != '':
|
if command != '':
|
||||||
execute_command(command_interpreter, command)
|
execute_command(command_interpreter, command)
|
||||||
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
print("Could not read debugging script '%s'." % script_path, file = sys.stderr)
|
print("Could not read debugging script '%s'." % script_path, file=sys.stderr)
|
||||||
print(e, file = sys.stderr)
|
print(e, file=sys.stderr)
|
||||||
print("Aborting.", file = sys.stderr)
|
print("Aborting.", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
finally:
|
finally:
|
||||||
script_file.close()
|
script_file.close()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue