|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Run a program via lldb until it fails. |
| 5 | +The lldb executable is located via your PATH env variable, if not specified. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import sys |
| 10 | +from optparse import OptionParser |
| 11 | + |
| 12 | +def is_exe(fpath): |
| 13 | + """Check whether fpath is an executable.""" |
| 14 | + return os.path.isfile(fpath) and os.access(fpath, os.X_OK) |
| 15 | + |
| 16 | +def which(program): |
| 17 | + """Find the full path to a program, or return None.""" |
| 18 | + fpath, fname = os.path.split(program) |
| 19 | + if fpath: |
| 20 | + if is_exe(program): |
| 21 | + return program |
| 22 | + else: |
| 23 | + for path in os.environ["PATH"].split(os.pathsep): |
| 24 | + exe_file = os.path.join(path, program) |
| 25 | + if is_exe(exe_file): |
| 26 | + return exe_file |
| 27 | + return None |
| 28 | + |
| 29 | +def do_lldb_launch_loop(lldb_command, exe, exe_options): |
| 30 | + from cStringIO import StringIO |
| 31 | + import pexpect, time |
| 32 | + |
| 33 | + prompt = "\(lldb\) " |
| 34 | + lldb = pexpect.spawn(lldb_command) |
| 35 | + # Turn on logging for what lldb sends back. |
| 36 | + #lldb.logfile_read = sys.stdout |
| 37 | + lldb.expect(prompt) |
| 38 | + |
| 39 | + # Now issue the file command. |
| 40 | + print "sending file command...." |
| 41 | + lldb.sendline('file %s' % exe) |
| 42 | + lldb.expect(prompt) |
| 43 | + #print "lldb.buffer:--->", lldb.buffer, "<---" |
| 44 | + #print "lldb.before:--->", lldb.before, "<---" |
| 45 | + #print "lldb.after:--->", lldb.buffer, "<----" |
| 46 | + |
| 47 | + # Loop until it faults.... |
| 48 | + count = 0 |
| 49 | + #while True: |
| 50 | + # count = count + 1 |
| 51 | + for i in range(10): |
| 52 | + count = i |
| 53 | + print "sending process launch -- %s (iteration: %d)" % (exe_options, count) |
| 54 | + lldb.sendline('process launch -- %s' % exe_options) |
| 55 | + index = lldb.expect(['Process .* exited with status', |
| 56 | + 'Process .* stopped', |
| 57 | + pexpect.TIMEOUT]) |
| 58 | + #print "lldb.buffer:--->", lldb.buffer, "<---" |
| 59 | + #print "lldb.before:--->", lldb.before, "<----" |
| 60 | + #print "lldb.after:--->", lldb.buffer, "<----" |
| 61 | + if index == 0: |
| 62 | + # We'll try again later. |
| 63 | + time.sleep(3) |
| 64 | + elif index == 1: |
| 65 | + # Perfect, our process had stopped; break out of the loop. |
| 66 | + break; |
| 67 | + elif index == 2: |
| 68 | + # Something went wrong. |
| 69 | + print "TIMEOUT occurred:", str(lldb) |
| 70 | + |
| 71 | + # Give control of lldb shell to the user. |
| 72 | + lldb.interact() |
| 73 | + |
| 74 | +def main(): |
| 75 | + # This is to set up the Python path to include the pexpect-2.4 dir. |
| 76 | + # Remember to update this when/if things change. |
| 77 | + scriptPath = sys.path[0] |
| 78 | + sys.path.append(os.path.join(scriptPath, os.pardir, os.pardir, 'test', 'pexpect-2.4')) |
| 79 | + |
| 80 | + parser = OptionParser(usage="""\ |
| 81 | +Run a program via lldb until it fails. |
| 82 | +The lldb executable is located via your PATH env variable, if not specified. |
| 83 | +
|
| 84 | +Usage: %prog [options] |
| 85 | +""") |
| 86 | + parser.add_option('-l', '--lldb-command', |
| 87 | + type='string', action='store', metavar='LLDB_COMMAND', |
| 88 | + default='lldb', dest='lldb_command', |
| 89 | + help='Full path to your lldb command') |
| 90 | + parser.add_option('-e', '--executable', |
| 91 | + type='string', action='store', |
| 92 | + dest='exe', |
| 93 | + help="""(Mandatory) The executable to launch via lldb.""") |
| 94 | + parser.add_option('-o', '--options', |
| 95 | + type='string', action='store', |
| 96 | + default = '', dest='exe_options', |
| 97 | + help="""The args/options passed to the launched program, if specified.""") |
| 98 | + |
| 99 | + opts, args = parser.parse_args() |
| 100 | + |
| 101 | + lldb_command = which(opts.lldb_command) |
| 102 | + |
| 103 | + if not opts.exe: |
| 104 | + parser.print_help() |
| 105 | + sys.exit(1) |
| 106 | + exe = opts.exe |
| 107 | + |
| 108 | + exe_options = opts.exe_options |
| 109 | + |
| 110 | + # We have parsed the options. |
| 111 | + print "lldb command:", lldb_command |
| 112 | + print "executable:", exe |
| 113 | + print "executable options:", exe_options |
| 114 | + |
| 115 | + do_lldb_launch_loop(lldb_command, exe, exe_options) |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + main() |
0 commit comments