@@ -176,77 +176,85 @@ def watchdog():
176176 watchdog_thread .start ()
177177
178178
179+ def get_env_arg (name ):
180+ value = os .environ .get (name )
181+ if value is None :
182+ print ("must set %s" % name )
183+ sys .exit (1 )
184+ return value
185+
186+
179187####################################################################################################
180188# ~main
181189####################################################################################################
182190
183191
184- if len ( sys . argv ) != 3 :
185- print ( "usage: python lldb_batchmode.py target-path script-path " )
186- sys . exit ( 1 )
192+ def main () :
193+ target_path = get_env_arg ( "LLDB_BATCHMODE_TARGET_PATH " )
194+ script_path = get_env_arg ( "LLDB_BATCHMODE_SCRIPT_PATH" )
187195
188- target_path = sys .argv [1 ]
189- script_path = sys .argv [2 ]
196+ print ("LLDB batch-mode script" )
197+ print ("----------------------" )
198+ print ("Debugger commands script is '%s'." % script_path )
199+ print ("Target executable is '%s'." % target_path )
200+ print ("Current working directory is '%s'" % os .getcwd ())
190201
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 ())
202+ # Start the timeout watchdog
203+ start_watchdog ()
196204
197- # Start the timeout watchdog
198- start_watchdog ()
205+ # Create a new debugger instance
206+ debugger = lldb . SBDebugger . Create ()
199207
200- # Create a new debugger instance
201- debugger = lldb .SBDebugger .Create ()
208+ # When we step or continue, don't return from the function until the process
209+ # stops. We do this by setting the async mode to false.
210+ debugger .SetAsync (False )
202211
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 )
212+ # Create a target from a file and arch
213+ print ("Creating a target for '%s'" % target_path )
214+ target_error = lldb .SBError ()
215+ target = debugger .CreateTarget (target_path , None , None , True , target_error )
206216
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 )
217+ if not target :
218+ print (
219+ "Could not create debugging target '"
220+ + target_path
221+ + "': "
222+ + str (target_error )
223+ + ". Aborting." ,
224+ file = sys .stderr ,
225+ )
226+ sys .exit (1 )
222227
228+ # Register the breakpoint callback for every breakpoint
229+ start_breakpoint_listener (target )
223230
224- # Register the breakpoint callback for every breakpoint
225- start_breakpoint_listener (target )
231+ command_interpreter = debugger .GetCommandInterpreter ()
226232
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 ()
233+ try :
234+ script_file = open (script_path , "r" )
235+
236+ for line in script_file :
237+ command = line .strip ()
238+ if (
239+ command == "run"
240+ or command == "r"
241+ or re .match (r"^process\s+launch.*" , command )
242+ ):
243+ # Before starting to run the program, let the thread sleep a bit, so all
244+ # breakpoint added events can be processed
245+ time .sleep (0.5 )
246+ if command != "" :
247+ execute_command (command_interpreter , command )
248+
249+ except IOError as e :
250+ print ("Could not read debugging script '%s'." % script_path , file = sys .stderr )
251+ print (e , file = sys .stderr )
252+ print ("Aborting." , file = sys .stderr )
253+ sys .exit (1 )
254+ finally :
255+ debugger .Terminate ()
256+ script_file .close ()
257+
258+
259+ if __name__ == "__main__" :
260+ main ()
0 commit comments