Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | #---------------------------------------------------------------------- |
| 4 | # Be sure to add the python path that points to the LLDB shared library. |
| 5 | # On MacOSX csh, tcsh: |
| 6 | # setenv PYTHONPATH /Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python |
| 7 | # On MacOSX sh, bash: |
| 8 | # export PYTHONPATH=/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python |
| 9 | #---------------------------------------------------------------------- |
| 10 | |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 11 | import commands |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 12 | import optparse |
| 13 | import os |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 14 | import platform |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 15 | import sys |
| 16 | |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 17 | #---------------------------------------------------------------------- |
| 18 | # Code that auto imports LLDB |
| 19 | #---------------------------------------------------------------------- |
| 20 | try: |
| 21 | # Just try for LLDB in case PYTHONPATH is already correctly setup |
| 22 | import lldb |
| 23 | except ImportError: |
| 24 | lldb_python_dirs = list() |
| 25 | # lldb is not in the PYTHONPATH, try some defaults for the current platform |
| 26 | platform_system = platform.system() |
| 27 | if platform_system == 'Darwin': |
| 28 | # On Darwin, try the currently selected Xcode directory |
| 29 | xcode_dir = commands.getoutput("xcode-select --print-path") |
| 30 | if xcode_dir: |
| 31 | lldb_python_dirs.append(os.path.realpath(xcode_dir + '/../SharedFrameworks/LLDB.framework/Resources/Python')) |
| 32 | lldb_python_dirs.append(xcode_dir + '/Library/PrivateFrameworks/LLDB.framework/Resources/Python') |
| 33 | lldb_python_dirs.append('/System/Library/PrivateFrameworks/LLDB.framework/Resources/Python') |
| 34 | success = False |
| 35 | for lldb_python_dir in lldb_python_dirs: |
| 36 | if os.path.exists(lldb_python_dir): |
| 37 | if not (sys.path.__contains__(lldb_python_dir)): |
| 38 | sys.path.append(lldb_python_dir) |
| 39 | try: |
| 40 | import lldb |
| 41 | except ImportError: |
| 42 | pass |
| 43 | else: |
| 44 | print 'imported lldb from: "%s"' % (lldb_python_dir) |
| 45 | success = True |
| 46 | break |
| 47 | if not success: |
| 48 | print "error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly" |
| 49 | sys.exit(1) |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 58 | def print_threads(process, options): |
| 59 | if options.show_threads: |
| 60 | for thread in process: |
| 61 | print '%s %s' % (thread, thread.GetFrameAtIndex(0)) |
| 62 | |
| 63 | def run_commands(command_interpreter, commands): |
| 64 | return_obj = lldb.SBCommandReturnObject() |
| 65 | for command in commands: |
| 66 | command_interpreter.HandleCommand( command, return_obj ) |
| 67 | if return_obj.Succeeded(): |
| 68 | print return_obj.GetOutput() |
| 69 | else: |
| 70 | print return_obj |
| 71 | if options.stop_on_error: |
| 72 | break |
| 73 | |
| 74 | def main(argv): |
| 75 | description='''Debugs a program using the LLDB python API and uses asynchronous broadcast events to watch for process state changes.''' |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 76 | epilog='''Examples: |
| 77 | |
| 78 | #---------------------------------------------------------------------- |
| 79 | # Run "/bin/ls" with the arguments "-lAF /tmp/", and set a breakpoint |
| 80 | # at "malloc" and backtrace and read all registers each time we stop |
| 81 | #---------------------------------------------------------------------- |
| 82 | % ./process_events.py --breakpoint malloc --stop-command bt --stop-command 'register read' -- /bin/ls -lAF /tmp/ |
| 83 | |
| 84 | ''' |
| 85 | optparse.OptionParser.format_epilog = lambda self, formatter: self.epilog |
| 86 | parser = optparse.OptionParser(description=description, prog='process_events',usage='usage: process_events [options] program [arg1 arg2]', epilog=epilog) |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 87 | parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help="Enable verbose logging.", default=False) |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 88 | parser.add_option('-b', '--breakpoint', action='append', type='string', metavar='BPEXPR', dest='breakpoints', help='Breakpoint commands to create after the target has been created, the values will be sent to the "_regexp-break" command which supports breakpoints by name, file:line, and address.') |
| 89 | parser.add_option('-a', '--arch', type='string', dest='arch', help='The architecture to use when creating the debug target.', default=None) |
| 90 | parser.add_option('-l', '--launch-command', action='append', type='string', metavar='CMD', dest='launch_commands', help='LLDB command interpreter commands to run once after the process has launched. This option can be specified more than once.', default=[]) |
| 91 | parser.add_option('-s', '--stop-command', action='append', type='string', metavar='CMD', dest='stop_commands', help='LLDB command interpreter commands to run each time the process stops. This option can be specified more than once.', default=[]) |
| 92 | parser.add_option('-c', '--crash-command', action='append', type='string', metavar='CMD', dest='crash_commands', help='LLDB command interpreter commands to run in case the process crashes. This option can be specified more than once.', default=[]) |
| 93 | parser.add_option('-x', '--exit-command', action='append', type='string', metavar='CMD', dest='exit_commands', help='LLDB command interpreter commands to run once after the process has exited. This option can be specified more than once.', default=[]) |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 94 | parser.add_option('-T', '--no-threads', action='store_false', dest='show_threads', help="Don't show threads when process stops.", default=True) |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 95 | parser.add_option('--ignore-errors', action='store_false', dest='stop_on_error', help="Don't stop executing LLDB commands if the command returns an error. This applies to all of the LLDB command interpreter commands that get run for launch, stop, crash and exit.", default=True) |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 96 | parser.add_option('-n', '--run-count', type='int', dest='run_count', metavar='N', help='How many times to run the process in case the process exits.', default=1) |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 97 | parser.add_option('-t', '--event-timeout', type='int', dest='event_timeout', metavar='SEC', help='Specify the timeout in seconds to wait for process state change events.', default=lldb.UINT32_MAX) |
| 98 | parser.add_option('-e', '--environment', action='append', type='string', metavar='ENV', dest='env_vars', help='Environment variables to set in the inferior process when launching a process.') |
| 99 | parser.add_option('-d', '--working-dir', type='string', metavar='DIR', dest='working_dir', help='The the current working directory when launching a process.', default=None) |
| 100 | parser.add_option('-p', '--attach-pid', type='int', dest='attach_pid', metavar='PID', help='Specify a process to attach to by process ID.', default=-1) |
| 101 | parser.add_option('-P', '--attach-name', type='string', dest='attach_name', metavar='PROCESSNAME', help='Specify a process to attach to by name.', default=None) |
| 102 | parser.add_option('-w', '--attach-wait', action='store_true', dest='attach_wait', help='Wait for the next process to launch when attaching to a process by name.', default=False) |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 103 | try: |
| 104 | (options, args) = parser.parse_args(argv) |
| 105 | except: |
| 106 | return |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 107 | |
| 108 | attach_info = None |
| 109 | launch_info = None |
| 110 | exe = None |
| 111 | if args: |
| 112 | exe = args.pop(0) |
| 113 | launch_info = lldb.SBLaunchInfo (args) |
| 114 | if options.env_vars: |
| 115 | launch_info.SetEnvironmentEntries(options.env_vars, True) |
| 116 | if options.working_dir: |
| 117 | launch_info.SetWorkingDirectory(options.working_dir) |
| 118 | elif options.attach_pid != -1: |
| 119 | if options.run_count == 1: |
| 120 | attach_info = lldb.SBAttachInfo (options.attach_pid) |
| 121 | else: |
| 122 | print "error: --run-count can't be used with the --attach-pid option" |
| 123 | sys.exit(1) |
| 124 | elif not options.attach_name is None: |
| 125 | if options.run_count == 1: |
| 126 | attach_info = lldb.SBAttachInfo (options.attach_name, options.attach_wait) |
| 127 | else: |
| 128 | print "error: --run-count can't be used with the --attach-name option" |
| 129 | sys.exit(1) |
| 130 | else: |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 131 | print 'error: a program path for a program to debug and its arguments are required' |
| 132 | sys.exit(1) |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 133 | |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 134 | |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 135 | |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 136 | # Create a new debugger instance |
| 137 | debugger = lldb.SBDebugger.Create() |
| 138 | command_interpreter = debugger.GetCommandInterpreter() |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 139 | # Create a target from a file and arch |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 140 | |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 141 | if exe: |
| 142 | print "Creating a target for '%s'" % exe |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 143 | target = debugger.CreateTargetWithFileAndArch (exe, options.arch) |
| 144 | |
| 145 | if target: |
| 146 | |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 147 | # Set any breakpoints that were specified in the args if we are launching |
| 148 | if launch_info and options.breakpoints: |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 149 | for bp in options.breakpoints: |
| 150 | debugger.HandleCommand( "_regexp-break %s" % (bp)) |
| 151 | run_commands(command_interpreter, ['breakpoint list']) |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 152 | |
| 153 | for run_idx in range(options.run_count): |
| 154 | # Launch the process. Since we specified synchronous mode, we won't return |
| 155 | # from this function until we hit the breakpoint at main |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 156 | error = lldb.SBError() |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 157 | |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 158 | if launch_info: |
| 159 | if options.run_count == 1: |
| 160 | print 'Launching "%s"...' % (exe) |
| 161 | else: |
| 162 | print 'Launching "%s"... (launch %u of %u)' % (exe, run_idx + 1, options.run_count) |
| 163 | |
| 164 | process = target.Launch (launch_info, error) |
| 165 | else: |
| 166 | if options.attach_pid != -1: |
| 167 | print 'Attaching to process %i...' % (options.attach_pid) |
| 168 | else: |
| 169 | if options.attach_wait: |
| 170 | print 'Waiting for next to process named "%s" to launch...' % (options.attach_name) |
| 171 | else: |
| 172 | print 'Attaching to existing process named "%s"...' % (options.attach_name) |
| 173 | process = target.Attach (attach_info, error) |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 174 | |
| 175 | # Make sure the launch went ok |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 176 | if process and process.GetProcessID() != lldb.LLDB_INVALID_PROCESS_ID: |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 177 | pid = process.GetProcessID() |
| 178 | listener = lldb.SBListener("event_listener") |
| 179 | # sign up for process state change events |
| 180 | process.GetBroadcaster().AddListener(listener, lldb.SBProcess.eBroadcastBitStateChanged) |
| 181 | stop_idx = 0 |
| 182 | done = False |
| 183 | while not done: |
| 184 | event = lldb.SBEvent() |
| 185 | if listener.WaitForEvent (options.event_timeout, event): |
| 186 | state = lldb.SBProcess.GetStateFromEvent (event) |
| 187 | if state == lldb.eStateStopped: |
| 188 | if stop_idx == 0: |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 189 | if launch_info: |
| 190 | print "process %u launched" % (pid) |
| 191 | else: |
| 192 | print "attached to process %u" % (pid) |
| 193 | for m in target.modules: |
| 194 | print m |
| 195 | if options.breakpoints: |
| 196 | for bp in options.breakpoints: |
| 197 | debugger.HandleCommand( "_regexp-break %s" % (bp)) |
| 198 | run_commands(command_interpreter, ['breakpoint list']) |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 199 | run_commands (command_interpreter, options.launch_commands) |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 200 | else: |
| 201 | if options.verbose: |
| 202 | print "process %u stopped" % (pid) |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 203 | run_commands (command_interpreter, options.stop_commands) |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 204 | stop_idx += 1 |
| 205 | print_threads (process, options) |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 206 | process.Continue() |
| 207 | elif state == lldb.eStateExited: |
| 208 | exit_desc = process.GetExitDescription() |
| 209 | if exit_desc: |
| 210 | print "process %u exited with status %u: %s" % (pid, process.GetExitStatus (), exit_desc) |
| 211 | else: |
| 212 | print "process %u exited with status %u" % (pid, process.GetExitStatus ()) |
Greg Clayton | 97e44a0 | 2012-08-31 02:55:56 +0000 | [diff] [blame] | 213 | run_commands (command_interpreter, options.exit_commands) |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 214 | done = True |
| 215 | elif state == lldb.eStateCrashed: |
| 216 | print "process %u crashed" % (pid) |
| 217 | print_threads (process, options) |
| 218 | run_commands (command_interpreter, options.crash_commands) |
| 219 | done = True |
| 220 | elif state == lldb.eStateDetached: |
| 221 | print "process %u detached" % (pid) |
| 222 | done = True |
| 223 | elif state == lldb.eStateRunning: |
| 224 | # process is running, don't say anything, we will always get one of these after resuming |
| 225 | if options.verbose: |
| 226 | print "process %u resumed" % (pid) |
| 227 | elif state == lldb.eStateUnloaded: |
| 228 | print "process %u unloaded, this shouldn't happen" % (pid) |
| 229 | done = True |
| 230 | elif state == lldb.eStateConnected: |
| 231 | print "process connected" |
| 232 | elif state == lldb.eStateAttaching: |
| 233 | print "process attaching" |
| 234 | elif state == lldb.eStateLaunching: |
| 235 | print "process launching" |
| 236 | else: |
| 237 | # timeout waiting for an event |
| 238 | print "no process event for %u seconds, killing the process..." % (options.event_timeout) |
| 239 | done = True |
| 240 | process.Kill() # kill the process |
Greg Clayton | 984fee5 | 2012-09-25 18:27:12 +0000 | [diff] [blame] | 241 | else: |
| 242 | if error: |
| 243 | print error |
| 244 | else: |
| 245 | if launch_info: |
| 246 | print 'error: launch failed' |
| 247 | else: |
| 248 | print 'error: attach failed' |
Greg Clayton | c769722 | 2012-08-31 01:11:17 +0000 | [diff] [blame] | 249 | |
| 250 | lldb.SBDebugger.Terminate() |
| 251 | |
| 252 | if __name__ == '__main__': |
| 253 | main(sys.argv[1:]) |