]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/driver/batch_mode/TestBatchMode.py
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / driver / batch_mode / TestBatchMode.py
1 """
2 Test that the lldb driver's batch mode works correctly.
3 """
4
5 from __future__ import print_function
6
7
8
9 import os, time
10 import lldb
11 from lldbsuite.test.decorators import *
12 from lldbsuite.test.lldbtest import *
13 from lldbsuite.test import lldbutil
14
15 class DriverBatchModeTest (TestBase):
16
17     mydir = TestBase.compute_mydir(__file__)
18
19     def setUp(self):
20         # Call super's setUp().
21         TestBase.setUp(self)
22         # Our simple source filename.
23         self.source = 'main.c'
24         self.victim = None
25
26     def expect_string (self, string):
27         import pexpect
28         """This expects for "string", with timeout & EOF being test fails."""
29         try:
30             self.child.expect_exact(string)
31         except pexpect.EOF:
32             self.fail ("Got EOF waiting for '%s'"%(string))
33         except pexpect.TIMEOUT:
34             self.fail ("Timed out waiting for '%s'"%(string))
35
36     @skipIfRemote # test not remote-ready llvm.org/pr24813
37     @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
38     @expectedFlakeyLinux("llvm.org/pr25172")
39     @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr22274: need a pexpect replacement for windows")
40     def test_batch_mode_run_crash (self):
41         """Test that the lldb driver's batch mode works correctly."""
42         self.build()
43         self.setTearDownCleanup()
44
45         import pexpect
46         exe = os.path.join(os.getcwd(), "a.out")
47         prompt = "(lldb) "
48
49         # Pass CRASH so the process will crash and stop in batch mode.
50         run_commands = ' -b -o "break set -n main" -o "run" -o "continue" -k "frame var touch_me_not"'
51         self.child = pexpect.spawn('%s %s %s %s -- CRASH' % (lldbtest_config.lldbExec, self.lldbOption, run_commands, exe))
52         child = self.child
53         # Turn on logging for what the child sends back.
54         if self.TraceOn():
55             child.logfile_read = sys.stdout
56
57         # We should see the "run":
58         self.expect_string ("run")
59         # We should have hit the breakpoint & continued:
60         self.expect_string ("continue")
61         # The App should have crashed:
62         self.expect_string("About to crash")
63         # The -k option should have printed the frame variable once:
64         self.expect_string ('(char *) touch_me_not')
65         # Then we should have a live prompt:
66         self.expect_string (prompt)
67         self.child.sendline("frame variable touch_me_not")
68         self.expect_string ('(char *) touch_me_not')
69         
70         self.deletePexpectChild()
71
72
73     @skipIfRemote # test not remote-ready llvm.org/pr24813
74     @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
75     @expectedFlakeyLinux("llvm.org/pr25172")
76     @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr22274: need a pexpect replacement for windows")
77     def test_batch_mode_run_exit (self):
78         """Test that the lldb driver's batch mode works correctly."""
79         self.build()
80         self.setTearDownCleanup()
81
82         import pexpect
83         exe = os.path.join(os.getcwd(), "a.out")
84         prompt = "(lldb) "
85
86         # Now do it again, and make sure if we don't crash, we quit:
87         run_commands = ' -b -o "break set -n main" -o "run" -o "continue" '
88         self.child = pexpect.spawn('%s %s %s %s -- NOCRASH' % (lldbtest_config.lldbExec, self.lldbOption, run_commands, exe))
89         child = self.child
90         # Turn on logging for what the child sends back.
91         if self.TraceOn():
92             child.logfile_read = sys.stdout
93
94         # We should see the "run":
95         self.expect_string ("run")
96         # We should have hit the breakpoint & continued:
97         self.expect_string ("continue")
98         # The App should have not have crashed:
99         self.expect_string("Got there on time and it did not crash.")
100         # Then we should have a live prompt:
101         self.expect_string ("exited")
102         index = self.child.expect([pexpect.EOF, pexpect.TIMEOUT])
103         self.assertTrue(index == 0, "lldb didn't close on successful batch completion.")
104
105     def closeVictim(self):
106         if self.victim != None:
107             self.victim.close()
108             self.victim = None
109
110     @skipIfRemote # test not remote-ready llvm.org/pr24813
111     @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
112     @expectedFlakeyLinux("llvm.org/pr25172")
113     @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr22274: need a pexpect replacement for windows")
114     def test_batch_mode_attach_exit (self):
115         """Test that the lldb driver's batch mode works correctly."""
116         self.build()
117         self.setTearDownCleanup()
118  
119         import pexpect
120         exe = os.path.join(os.getcwd(), "a.out")
121         prompt = "(lldb) "
122
123         # Finally, start up the process by hand, attach to it, and wait for its completion.
124         # Attach is funny, since it looks like it stops with a signal on most Unixen so 
125         # care must be taken not to treat that as a reason to exit batch mode.
126         
127         # Start up the process by hand and wait for it to get to the wait loop.
128
129         self.victim = pexpect.spawn('%s WAIT' %(exe))
130         if self.victim == None:
131             self.fail("Could not spawn ", exe, ".")
132
133         self.addTearDownHook (self.closeVictim)
134
135         if self.TraceOn():
136             self.victim.logfile_read = sys.stdout
137
138         self.victim.expect("PID: ([0-9]+) END")
139         if self.victim.match == None:
140             self.fail("Couldn't get the target PID.")
141
142         victim_pid = int(self.victim.match.group(1))
143         
144         self.victim.expect("Waiting")
145
146         run_commands = ' -b -o "process attach -p %d" -o "breakpoint set --file %s -p \'Stop here to unset keep_waiting\' -N keep_waiting" -o "continue" -o "break delete keep_waiting" -o "expr keep_waiting = 0" -o "continue" ' % (victim_pid, self.source)
147         self.child = pexpect.spawn('%s %s %s %s' % (lldbtest_config.lldbExec, self.lldbOption, run_commands, exe))
148
149         child = self.child
150         # Turn on logging for what the child sends back.
151         if self.TraceOn():
152             child.logfile_read = sys.stdout
153
154         # We should see the "run":
155         self.expect_string ("attach")
156
157         self.expect_string(prompt + "continue")
158
159         self.expect_string(prompt + "continue")
160
161         # Then we should see the process exit:
162         self.expect_string ("Process %d exited with status"%(victim_pid))
163         
164         victim_index = self.victim.expect([pexpect.EOF, pexpect.TIMEOUT])
165         self.assertTrue(victim_index == 0, "Victim didn't really exit.")
166
167         index = self.child.expect([pexpect.EOF, pexpect.TIMEOUT])
168         self.assertTrue(index == 0, "lldb didn't close on successful batch completion.")
169
170