30
30
import threading
31
31
import thread
32
32
import re
33
- import atexit
34
33
import time
35
34
36
35
# Set this to True for additional output
37
36
DEBUG_OUTPUT = False
38
37
38
+
39
39
def print_debug (s ):
40
- "Print something if DEBUG_OUTPUT is True"
41
- global DEBUG_OUTPUT
42
- if DEBUG_OUTPUT :
43
- print ("DEBUG: " + str (s ))
40
+ "Print something if DEBUG_OUTPUT is True"
41
+ global DEBUG_OUTPUT
42
+ if DEBUG_OUTPUT :
43
+ print ("DEBUG: " + str (s ))
44
44
45
45
46
46
def normalize_whitespace (s ):
47
- "Replace newlines, tabs, multiple spaces, etc with exactly one space"
48
- return re .sub ("\s+" , " " , s )
47
+ "Replace newlines, tabs, multiple spaces, etc with exactly one space"
48
+ return re .sub ("\s+" , " " , s )
49
49
50
50
51
- # This callback is registered with every breakpoint and makes sure that the frame containing the
52
- # breakpoint location is selected
53
51
def breakpoint_callback (frame , bp_loc , dict ):
54
- "Called whenever a breakpoint is hit"
55
- print ("Hit breakpoint " + str (bp_loc ))
52
+ """This callback is registered with every breakpoint and makes sure that the
53
+ frame containing the breakpoint location is selected"""
54
+ print ("Hit breakpoint " + str (bp_loc ))
56
55
57
- # Select the frame and the thread containing it
58
- frame .thread .process .SetSelectedThread (frame .thread )
59
- frame .thread .SetSelectedFrame (frame .idx )
56
+ # Select the frame and the thread containing it
57
+ frame .thread .process .SetSelectedThread (frame .thread )
58
+ frame .thread .SetSelectedFrame (frame .idx )
60
59
61
- # Returning True means that we actually want to stop at this breakpoint
62
- return True
60
+ # Returning True means that we actually want to stop at this breakpoint
61
+ return True
63
62
64
63
65
64
# This is a list of breakpoints that are not registered with the breakpoint callback. The list is
@@ -70,91 +69,99 @@ def breakpoint_callback(frame, bp_loc, dict):
70
69
# used to avoid hooking callbacks into breakpoints more than once
71
70
registered_breakpoints = set ()
72
71
72
+
73
73
def execute_command (command_interpreter , command ):
74
- "Executes a single CLI command"
75
- global new_breakpoints
76
- global registered_breakpoints
77
-
78
- res = lldb .SBCommandReturnObject ()
79
- print (command )
80
- command_interpreter .HandleCommand (command , res )
81
-
82
- if res .Succeeded ():
83
- if res .HasResult ():
84
- print (normalize_whitespace (res .GetOutput ()), end = '\n ' )
85
-
86
- # If the command introduced any breakpoints, make sure to register them with the breakpoint
87
- # callback
88
- while len (new_breakpoints ) > 0 :
89
- res .Clear ()
90
- breakpoint_id = new_breakpoints .pop ()
91
-
92
- if breakpoint_id in registered_breakpoints :
93
- print_debug ("breakpoint with id %s is already registered. Ignoring." % str (breakpoint_id ))
94
- else :
95
- print_debug ("registering breakpoint callback, id = " + str (breakpoint_id ))
96
- callback_command = "breakpoint command add -F breakpoint_callback " + str (breakpoint_id )
97
- command_interpreter .HandleCommand (callback_command , res )
98
- if res .Succeeded ():
99
- print_debug ("successfully registered breakpoint callback, id = " + str (breakpoint_id ))
100
- registered_breakpoints .add (breakpoint_id )
101
- else :
102
- print ("Error while trying to register breakpoint callback, id = " + str (breakpoint_id ))
103
- else :
104
- print (res .GetError ())
74
+ "Executes a single CLI command"
75
+ global new_breakpoints
76
+ global registered_breakpoints
77
+
78
+ res = lldb .SBCommandReturnObject ()
79
+ print (command )
80
+ command_interpreter .HandleCommand (command , res )
81
+
82
+ if res .Succeeded ():
83
+ if res .HasResult ():
84
+ print (normalize_whitespace (res .GetOutput ()), end = '\n ' )
85
+
86
+ # If the command introduced any breakpoints, make sure to register
87
+ # them with the breakpoint
88
+ # callback
89
+ while len (new_breakpoints ) > 0 :
90
+ res .Clear ()
91
+ breakpoint_id = new_breakpoints .pop ()
92
+
93
+ if breakpoint_id in registered_breakpoints :
94
+ print_debug ("breakpoint with id %s is already registered. Ignoring." %
95
+ str (breakpoint_id ))
96
+ else :
97
+ print_debug ("registering breakpoint callback, id = " + str (breakpoint_id ))
98
+ callback_command = ("breakpoint command add -F breakpoint_callback " +
99
+ str (breakpoint_id ))
100
+ command_interpreter .HandleCommand (callback_command , res )
101
+ if res .Succeeded ():
102
+ print_debug ("successfully registered breakpoint callback, id = " +
103
+ str (breakpoint_id ))
104
+ registered_breakpoints .add (breakpoint_id )
105
+ else :
106
+ print ("Error while trying to register breakpoint callback, id = " +
107
+ str (breakpoint_id ))
108
+ else :
109
+ print (res .GetError ())
105
110
106
111
107
112
def start_breakpoint_listener (target ):
108
- "Listens for breakpoints being added and adds new ones to the callback registration list"
109
- listener = lldb .SBListener ("breakpoint listener" )
110
-
111
- def listen ():
112
- event = lldb .SBEvent ()
113
- try :
114
- while True :
115
- if listener .WaitForEvent (120 , event ):
116
- if lldb .SBBreakpoint .EventIsBreakpointEvent (event ) and \
117
- lldb .SBBreakpoint .GetBreakpointEventTypeFromEvent (event ) == \
118
- lldb .eBreakpointEventTypeAdded :
119
- global new_breakpoints
120
- breakpoint = lldb .SBBreakpoint .GetBreakpointFromEvent (event )
121
- print_debug ("breakpoint added, id = " + str (breakpoint .id ))
122
- new_breakpoints .append (breakpoint .id )
123
- except :
124
- print_debug ("breakpoint listener shutting down" )
125
-
126
- # Start the listener and let it run as a daemon
127
- listener_thread = threading .Thread (target = listen )
128
- listener_thread .daemon = True
129
- listener_thread .start ()
130
-
131
- # Register the listener with the target
132
- target .GetBroadcaster ().AddListener (listener , lldb .SBTarget .eBroadcastBitBreakpointChanged )
113
+ """Listens for breakpoints being added and adds new ones to the callback
114
+ registration list"""
115
+ listener = lldb .SBListener ("breakpoint listener" )
116
+
117
+ def listen ():
118
+ event = lldb .SBEvent ()
119
+ try :
120
+ while True :
121
+ if listener .WaitForEvent (120 , event ):
122
+ if lldb .SBBreakpoint .EventIsBreakpointEvent (event ) and \
123
+ lldb .SBBreakpoint .GetBreakpointEventTypeFromEvent (event ) == \
124
+ lldb .eBreakpointEventTypeAdded :
125
+ global new_breakpoints
126
+ breakpoint = lldb .SBBreakpoint .GetBreakpointFromEvent (event )
127
+ print_debug ("breakpoint added, id = " + str (breakpoint .id ))
128
+ new_breakpoints .append (breakpoint .id )
129
+ except :
130
+ print_debug ("breakpoint listener shutting down" )
131
+
132
+ # Start the listener and let it run as a daemon
133
+ listener_thread = threading .Thread (target = listen )
134
+ listener_thread .daemon = True
135
+ listener_thread .start ()
136
+
137
+ # Register the listener with the target
138
+ target .GetBroadcaster ().AddListener (listener , lldb .SBTarget .eBroadcastBitBreakpointChanged )
133
139
134
140
135
141
def start_watchdog ():
136
- "Starts a watchdog thread that will terminate the process after a certain period of time"
137
- watchdog_start_time = time .clock ()
138
- watchdog_max_time = watchdog_start_time + 30
139
-
140
- def watchdog ():
141
- while time .clock () < watchdog_max_time :
142
- time .sleep (1 )
143
- print ("TIMEOUT: lldb_batchmode.py has been running for too long. Aborting!" )
144
- thread .interrupt_main ()
145
-
146
- # Start the listener and let it run as a daemon
147
- watchdog_thread = threading .Thread (target = watchdog )
148
- watchdog_thread .daemon = True
149
- watchdog_thread .start ()
142
+ """Starts a watchdog thread that will terminate the process after a certain
143
+ period of time"""
144
+ watchdog_start_time = time .clock ()
145
+ watchdog_max_time = watchdog_start_time + 30
146
+
147
+ def watchdog ():
148
+ while time .clock () < watchdog_max_time :
149
+ time .sleep (1 )
150
+ print ("TIMEOUT: lldb_batchmode.py has been running for too long. Aborting!" )
151
+ thread .interrupt_main ()
152
+
153
+ # Start the listener and let it run as a daemon
154
+ watchdog_thread = threading .Thread (target = watchdog )
155
+ watchdog_thread .daemon = True
156
+ watchdog_thread .start ()
150
157
151
158
####################################################################################################
152
159
# ~main
153
160
####################################################################################################
154
161
155
162
if len (sys .argv ) != 3 :
156
- print ("usage: python lldb_batchmode.py target-path script-path" )
157
- sys .exit (1 )
163
+ print ("usage: python lldb_batchmode.py target-path script-path" )
164
+ sys .exit (1 )
158
165
159
166
target_path = sys .argv [1 ]
160
167
script_path = sys .argv [2 ]
@@ -181,9 +188,9 @@ def watchdog():
181
188
target = debugger .CreateTarget (target_path , None , None , True , target_error )
182
189
183
190
if not target :
184
- print ("Could not create debugging target '" + target_path + "': " + str ( target_error ) +
185
- ". Aborting." , file = sys .stderr )
186
- sys .exit (1 )
191
+ print ("Could not create debugging target '" + target_path + "': " +
192
+ str ( target_error ) + ". Aborting." , file = sys .stderr )
193
+ sys .exit (1 )
187
194
188
195
189
196
# Register the breakpoint callback for every breakpoint
@@ -192,22 +199,21 @@ def watchdog():
192
199
command_interpreter = debugger .GetCommandInterpreter ()
193
200
194
201
try :
195
- script_file = open (script_path , 'r' )
202
+ script_file = open (script_path , 'r' )
196
203
197
- for line in script_file :
198
- command = line .strip ()
199
- if command == "run" or command == "r" or re .match ("^process\s+launch.*" , command ):
200
- # Before starting to run the program, let the thread sleep a bit, so all
201
- # breakpoint added events can be processed
202
- time .sleep (0.5 )
203
- if command != '' :
204
- execute_command (command_interpreter , command )
204
+ for line in script_file :
205
+ command = line .strip ()
206
+ if command == "run" or command == "r" or re .match ("^process\s+launch.*" , command ):
207
+ # Before starting to run the program, let the thread sleep a bit, so all
208
+ # breakpoint added events can be processed
209
+ time .sleep (0.5 )
210
+ if command != '' :
211
+ execute_command (command_interpreter , command )
205
212
206
213
except IOError as e :
207
- print ("Could not read debugging script '%s'." % script_path , file = sys .stderr )
208
- print (e , file = sys .stderr )
209
- print ("Aborting." , file = sys .stderr )
210
- sys .exit (1 )
214
+ print ("Could not read debugging script '%s'." % script_path , file = sys .stderr )
215
+ print (e , file = sys .stderr )
216
+ print ("Aborting." , file = sys .stderr )
217
+ sys .exit (1 )
211
218
finally :
212
- script_file .close ()
213
-
219
+ script_file .close ()
0 commit comments