Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 64f000b

Browse files
author
Johnny Chen
committedApr 2, 2011
Add a Python script which launches a program from within lldb and loop until the
process stops for some reason. main.c (compiled into a.out) is used as an example in the README-run-until-faulted file. git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@128755 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2570825 commit 64f000b

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
 

‎utils/test/README-run-until-faulted

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
A example usage of the Python script run-until-faulted.py:
2+
3+
[18:20:29] johnny:/Volumes/data/lldb/svn/trunk/utils/test $ ./run-until-faulted.py -l /Volumes/data/lldb/svn/trunk/build/Debug/lldb -e './a.out'
4+
lldb command: /Volumes/data/lldb/svn/trunk/build/Debug/lldb
5+
executable: ./a.out
6+
executable options:
7+
sending file command....
8+
sending process launch -- (iteration: 0)
9+
10+
* thread #1: tid = 0x2d03, 0x0000000100000eef a.out`main + 39 at main.c:7, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
11+
4 {
12+
5 int *null_ptr = 0;
13+
6 printf("Hello, fault!\n");
14+
-> 7 printf("Now segfault %d\n", *null_ptr);
15+
8 }
16+
17+
(lldb) q
18+
[18:20:40] johnny:/Volumes/data/lldb/svn/trunk/utils/test $

‎utils/test/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdio.h>
2+
3+
int main(int argc, const char* argv[])
4+
{
5+
int *null_ptr = 0;
6+
printf("Hello, fault!\n");
7+
printf("Now segfault %d\n", *null_ptr);
8+
}

‎utils/test/run-until-faulted.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)
This repository has been archived.