blob: 794f4fc847867791a413224615d64df971ac4d81 [file] [log] [blame]
Johnny Chen511e8c02011-04-02 01:20:28 +00001#!/usr/bin/env python
2
3"""
4Run a program via lldb until it fails.
5The lldb executable is located via your PATH env variable, if not specified.
6"""
7
8import os
9import sys
10from optparse import OptionParser
11
Kate Stoneb9c1b512016-09-06 20:57:50 +000012
Johnny Chen511e8c02011-04-02 01:20:28 +000013def is_exe(fpath):
14 """Check whether fpath is an executable."""
15 return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
16
Kate Stoneb9c1b512016-09-06 20:57:50 +000017
Johnny Chen511e8c02011-04-02 01:20:28 +000018def which(program):
19 """Find the full path to a program, or return None."""
20 fpath, fname = os.path.split(program)
21 if fpath:
22 if is_exe(program):
23 return program
24 else:
25 for path in os.environ["PATH"].split(os.pathsep):
26 exe_file = os.path.join(path, program)
27 if is_exe(exe_file):
28 return exe_file
29 return None
30
Kate Stoneb9c1b512016-09-06 20:57:50 +000031
Johnny Chen511e8c02011-04-02 01:20:28 +000032def do_lldb_launch_loop(lldb_command, exe, exe_options):
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 from cStringIO import StringIO
34 import pexpect
35 import time
Johnny Chen511e8c02011-04-02 01:20:28 +000036
37 prompt = "\(lldb\) "
38 lldb = pexpect.spawn(lldb_command)
39 # Turn on logging for what lldb sends back.
Johnny Chen8831e182011-06-01 22:12:27 +000040 lldb.logfile_read = sys.stdout
Johnny Chen511e8c02011-04-02 01:20:28 +000041 lldb.expect(prompt)
42
43 # Now issue the file command.
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 # print "sending 'file %s' command..." % exe
Johnny Chen511e8c02011-04-02 01:20:28 +000045 lldb.sendline('file %s' % exe)
46 lldb.expect(prompt)
Johnny Chen511e8c02011-04-02 01:20:28 +000047
48 # Loop until it faults....
49 count = 0
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 # while True:
Johnny Chen511e8c02011-04-02 01:20:28 +000051 # count = count + 1
Johnny Chen8831e182011-06-01 22:12:27 +000052 for i in range(100):
Johnny Chen511e8c02011-04-02 01:20:28 +000053 count = i
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 # print "sending 'process launch -- %s' command... (iteration: %d)" %
55 # (exe_options, count)
Johnny Chen511e8c02011-04-02 01:20:28 +000056 lldb.sendline('process launch -- %s' % exe_options)
57 index = lldb.expect(['Process .* exited with status',
58 'Process .* stopped',
59 pexpect.TIMEOUT])
Johnny Chen511e8c02011-04-02 01:20:28 +000060 if index == 0:
61 # We'll try again later.
62 time.sleep(3)
63 elif index == 1:
64 # Perfect, our process had stopped; break out of the loop.
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 break
Johnny Chen511e8c02011-04-02 01:20:28 +000066 elif index == 2:
67 # Something went wrong.
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 print "TIMEOUT occurred:", str(lldb)
Johnny Chen511e8c02011-04-02 01:20:28 +000069
70 # Give control of lldb shell to the user.
71 lldb.interact()
72
Kate Stoneb9c1b512016-09-06 20:57:50 +000073
Johnny Chen511e8c02011-04-02 01:20:28 +000074def 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]
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 sys.path.append(
79 os.path.join(
80 scriptPath,
81 os.pardir,
82 os.pardir,
83 'test',
84 'pexpect-2.4'))
Johnny Chen511e8c02011-04-02 01:20:28 +000085
86 parser = OptionParser(usage="""\
Johnny Chen8831e182011-06-01 22:12:27 +000087%prog [options]
Johnny Chen511e8c02011-04-02 01:20:28 +000088Run a program via lldb until it fails.
Johnny Chen8831e182011-06-01 22:12:27 +000089The lldb executable is located via your PATH env variable, if not specified.\
Johnny Chen511e8c02011-04-02 01:20:28 +000090""")
91 parser.add_option('-l', '--lldb-command',
92 type='string', action='store', metavar='LLDB_COMMAND',
93 default='lldb', dest='lldb_command',
94 help='Full path to your lldb command')
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 parser.add_option(
96 '-e',
97 '--executable',
98 type='string',
99 action='store',
100 dest='exe',
101 help="""(Mandatory) The executable to launch via lldb.""")
102 parser.add_option(
103 '-o',
104 '--options',
105 type='string',
106 action='store',
107 default='',
108 dest='exe_options',
109 help="""The args/options passed to the launched program, if specified.""")
Johnny Chen511e8c02011-04-02 01:20:28 +0000110
111 opts, args = parser.parse_args()
112
113 lldb_command = which(opts.lldb_command)
114
115 if not opts.exe:
116 parser.print_help()
117 sys.exit(1)
118 exe = opts.exe
119
120 exe_options = opts.exe_options
121
122 # We have parsed the options.
123 print "lldb command:", lldb_command
124 print "executable:", exe
125 print "executable options:", exe_options
126
127 do_lldb_launch_loop(lldb_command, exe, exe_options)
128
129if __name__ == '__main__':
130 main()