Skip to content

Commit 5038a0a

Browse files
committed
Use a main function in lldb_batchmode.py
1 parent ceb7df7 commit 5038a0a

File tree

1 file changed

+72
-68
lines changed

1 file changed

+72
-68
lines changed

src/etc/lldb_batchmode.py

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -181,72 +181,76 @@ def watchdog():
181181
####################################################################################################
182182

183183

184-
if len(sys.argv) != 3:
185-
print("usage: python lldb_batchmode.py target-path script-path")
186-
sys.exit(1)
187-
188-
target_path = sys.argv[1]
189-
script_path = sys.argv[2]
190-
191-
print("LLDB batch-mode script")
192-
print("----------------------")
193-
print("Debugger commands script is '%s'." % script_path)
194-
print("Target executable is '%s'." % target_path)
195-
print("Current working directory is '%s'" % os.getcwd())
196-
197-
# Start the timeout watchdog
198-
start_watchdog()
199-
200-
# Create a new debugger instance
201-
debugger = lldb.SBDebugger.Create()
202-
203-
# When we step or continue, don't return from the function until the process
204-
# stops. We do this by setting the async mode to false.
205-
debugger.SetAsync(False)
206-
207-
# Create a target from a file and arch
208-
print("Creating a target for '%s'" % target_path)
209-
target_error = lldb.SBError()
210-
target = debugger.CreateTarget(target_path, None, None, True, target_error)
211-
212-
if not target:
213-
print(
214-
"Could not create debugging target '"
215-
+ target_path
216-
+ "': "
217-
+ str(target_error)
218-
+ ". Aborting.",
219-
file=sys.stderr,
220-
)
221-
sys.exit(1)
222-
223-
224-
# Register the breakpoint callback for every breakpoint
225-
start_breakpoint_listener(target)
184+
def main():
185+
if len(sys.argv) != 3:
186+
print("usage: python lldb_batchmode.py target-path script-path")
187+
sys.exit(1)
188+
189+
target_path = sys.argv[1]
190+
script_path = sys.argv[2]
191+
192+
print("LLDB batch-mode script")
193+
print("----------------------")
194+
print("Debugger commands script is '%s'." % script_path)
195+
print("Target executable is '%s'." % target_path)
196+
print("Current working directory is '%s'" % os.getcwd())
197+
198+
# Start the timeout watchdog
199+
start_watchdog()
200+
201+
# Create a new debugger instance
202+
debugger = lldb.SBDebugger.Create()
203+
204+
# When we step or continue, don't return from the function until the process
205+
# stops. We do this by setting the async mode to false.
206+
debugger.SetAsync(False)
207+
208+
# Create a target from a file and arch
209+
print("Creating a target for '%s'" % target_path)
210+
target_error = lldb.SBError()
211+
target = debugger.CreateTarget(target_path, None, None, True, target_error)
212+
213+
if not target:
214+
print(
215+
"Could not create debugging target '"
216+
+ target_path
217+
+ "': "
218+
+ str(target_error)
219+
+ ". Aborting.",
220+
file=sys.stderr,
221+
)
222+
sys.exit(1)
223+
224+
# Register the breakpoint callback for every breakpoint
225+
start_breakpoint_listener(target)
226+
227+
command_interpreter = debugger.GetCommandInterpreter()
226228

227-
command_interpreter = debugger.GetCommandInterpreter()
228-
229-
try:
230-
script_file = open(script_path, "r")
231-
232-
for line in script_file:
233-
command = line.strip()
234-
if (
235-
command == "run"
236-
or command == "r"
237-
or re.match(r"^process\s+launch.*", command)
238-
):
239-
# Before starting to run the program, let the thread sleep a bit, so all
240-
# breakpoint added events can be processed
241-
time.sleep(0.5)
242-
if command != "":
243-
execute_command(command_interpreter, command)
244-
245-
except IOError as e:
246-
print("Could not read debugging script '%s'." % script_path, file=sys.stderr)
247-
print(e, file=sys.stderr)
248-
print("Aborting.", file=sys.stderr)
249-
sys.exit(1)
250-
finally:
251-
debugger.Terminate()
252-
script_file.close()
229+
try:
230+
script_file = open(script_path, "r")
231+
232+
for line in script_file:
233+
command = line.strip()
234+
if (
235+
command == "run"
236+
or command == "r"
237+
or re.match(r"^process\s+launch.*", command)
238+
):
239+
# Before starting to run the program, let the thread sleep a bit, so all
240+
# breakpoint added events can be processed
241+
time.sleep(0.5)
242+
if command != "":
243+
execute_command(command_interpreter, command)
244+
245+
except IOError as e:
246+
print("Could not read debugging script '%s'." % script_path, file=sys.stderr)
247+
print(e, file=sys.stderr)
248+
print("Aborting.", file=sys.stderr)
249+
sys.exit(1)
250+
finally:
251+
debugger.Terminate()
252+
script_file.close()
253+
254+
255+
if __name__ == "__main__":
256+
main()

0 commit comments

Comments
 (0)