]> git.lizzy.rs Git - rust.git/blob - src/etc/lldb_batchmode.py
Auto merge of #56462 - Zoxc:query-macro, r=oli-obk
[rust.git] / src / etc / lldb_batchmode.py
1 # This script allows to use LLDB in a way similar to GDB's batch mode. That is, given a text file
2 # containing LLDB commands (one command per line), this script will execute the commands one after
3 # the other.
4 # LLDB also has the -s and -S commandline options which also execute a list of commands from a text
5 # file. However, this command are execute `immediately`: the command of a `run` or `continue`
6 # command will be executed immediately after the `run` or `continue`, without waiting for the next
7 # breakpoint to be hit. This a command sequence like the following will not yield reliable results:
8 #
9 #   break 11
10 #   run
11 #   print x
12 #
13 # Most of the time the `print` command will be executed while the program is still running will thus
14 # fail. Using this Python script, the above will work as expected.
15
16 from __future__ import print_function
17 import lldb
18 import os
19 import sys
20 import threading
21 import re
22 import time
23
24 try:
25     import thread
26 except ModuleNotFoundError:
27     # The `thread` module was renamed to `_thread` in Python 3.
28     import _thread as thread
29
30 # Set this to True for additional output
31 DEBUG_OUTPUT = False
32
33
34 def print_debug(s):
35     """Print something if DEBUG_OUTPUT is True"""
36     global DEBUG_OUTPUT
37     if DEBUG_OUTPUT:
38         print("DEBUG: " + str(s))
39
40
41 def normalize_whitespace(s):
42     """Replace newlines, tabs, multiple spaces, etc with exactly one space"""
43     return re.sub("\s+", " ", s)
44
45
46 def breakpoint_callback(frame, bp_loc, dict):
47     """This callback is registered with every breakpoint and makes sure that the
48     frame containing the breakpoint location is selected"""
49     print("Hit breakpoint " + str(bp_loc))
50
51     # Select the frame and the thread containing it
52     frame.thread.process.SetSelectedThread(frame.thread)
53     frame.thread.SetSelectedFrame(frame.idx)
54
55     # Returning True means that we actually want to stop at this breakpoint
56     return True
57
58
59 # This is a list of breakpoints that are not registered with the breakpoint callback. The list is
60 # populated by the breakpoint listener and checked/emptied whenever a command has been executed
61 new_breakpoints = []
62
63 # This set contains all breakpoint ids that have already been registered with a callback, and is
64 # used to avoid hooking callbacks into breakpoints more than once
65 registered_breakpoints = set()
66
67
68 def execute_command(command_interpreter, command):
69     """Executes a single CLI command"""
70     global new_breakpoints
71     global registered_breakpoints
72
73     res = lldb.SBCommandReturnObject()
74     print(command)
75     command_interpreter.HandleCommand(command, res)
76
77     if res.Succeeded():
78         if res.HasResult():
79             print(normalize_whitespace(res.GetOutput() or ''), end='\n')
80
81         # If the command introduced any breakpoints, make sure to register
82         # them with the breakpoint
83         # callback
84         while len(new_breakpoints) > 0:
85             res.Clear()
86             breakpoint_id = new_breakpoints.pop()
87
88             if breakpoint_id in registered_breakpoints:
89                 print_debug("breakpoint with id %s is already registered. Ignoring." %
90                             str(breakpoint_id))
91             else:
92                 print_debug("registering breakpoint callback, id = " + str(breakpoint_id))
93                 callback_command = ("breakpoint command add -F breakpoint_callback " +
94                                     str(breakpoint_id))
95                 command_interpreter.HandleCommand(callback_command, res)
96                 if res.Succeeded():
97                     print_debug("successfully registered breakpoint callback, id = " +
98                                 str(breakpoint_id))
99                     registered_breakpoints.add(breakpoint_id)
100                 else:
101                     print("Error while trying to register breakpoint callback, id = " +
102                           str(breakpoint_id))
103     else:
104         print(res.GetError())
105
106
107 def start_breakpoint_listener(target):
108     """Listens for breakpoints being added and adds new ones to the callback
109     registration list"""
110     listener = lldb.SBListener("breakpoint listener")
111
112     def listen():
113         event = lldb.SBEvent()
114         try:
115             while True:
116                 if listener.WaitForEvent(120, event):
117                     if lldb.SBBreakpoint.EventIsBreakpointEvent(event) and \
118                             lldb.SBBreakpoint.GetBreakpointEventTypeFromEvent(event) == \
119                             lldb.eBreakpointEventTypeAdded:
120                         global new_breakpoints
121                         breakpoint = lldb.SBBreakpoint.GetBreakpointFromEvent(event)
122                         print_debug("breakpoint added, id = " + str(breakpoint.id))
123                         new_breakpoints.append(breakpoint.id)
124         except:
125             print_debug("breakpoint listener shutting down")
126
127     # Start the listener and let it run as a daemon
128     listener_thread = threading.Thread(target=listen)
129     listener_thread.daemon = True
130     listener_thread.start()
131
132     # Register the listener with the target
133     target.GetBroadcaster().AddListener(listener, lldb.SBTarget.eBroadcastBitBreakpointChanged)
134
135
136 def start_watchdog():
137     """Starts a watchdog thread that will terminate the process after a certain
138     period of time"""
139     watchdog_start_time = time.clock()
140     watchdog_max_time = watchdog_start_time + 30
141
142     def watchdog():
143         while time.clock() < watchdog_max_time:
144             time.sleep(1)
145         print("TIMEOUT: lldb_batchmode.py has been running for too long. Aborting!")
146         thread.interrupt_main()
147
148     # Start the listener and let it run as a daemon
149     watchdog_thread = threading.Thread(target=watchdog)
150     watchdog_thread.daemon = True
151     watchdog_thread.start()
152
153 ####################################################################################################
154 # ~main
155 ####################################################################################################
156
157 if len(sys.argv) != 3:
158     print("usage: python lldb_batchmode.py target-path script-path")
159     sys.exit(1)
160
161 target_path = sys.argv[1]
162 script_path = sys.argv[2]
163
164 print("LLDB batch-mode script")
165 print("----------------------")
166 print("Debugger commands script is '%s'." % script_path)
167 print("Target executable is '%s'." % target_path)
168 print("Current working directory is '%s'" % os.getcwd())
169
170 # Start the timeout watchdog
171 start_watchdog()
172
173 # Create a new debugger instance
174 debugger = lldb.SBDebugger.Create()
175
176 # When we step or continue, don't return from the function until the process
177 # stops. We do this by setting the async mode to false.
178 debugger.SetAsync(False)
179
180 # Create a target from a file and arch
181 print("Creating a target for '%s'" % target_path)
182 target_error = lldb.SBError()
183 target = debugger.CreateTarget(target_path, None, None, True, target_error)
184
185 if not target:
186     print("Could not create debugging target '" + target_path + "': " +
187           str(target_error) + ". Aborting.", file=sys.stderr)
188     sys.exit(1)
189
190
191 # Register the breakpoint callback for every breakpoint
192 start_breakpoint_listener(target)
193
194 command_interpreter = debugger.GetCommandInterpreter()
195
196 try:
197     script_file = open(script_path, 'r')
198
199     for line in script_file:
200         command = line.strip()
201         if command == "run" or command == "r" or re.match("^process\s+launch.*", command):
202             # Before starting to run the program, let the thread sleep a bit, so all
203             # breakpoint added events can be processed
204             time.sleep(0.5)
205         if command != '':
206             execute_command(command_interpreter, command)
207
208 except IOError as e:
209     print("Could not read debugging script '%s'." % script_path, file=sys.stderr)
210     print(e, file=sys.stderr)
211     print("Aborting.", file=sys.stderr)
212     sys.exit(1)
213 finally:
214     debugger.Terminate()
215     script_file.close()