blob: 23a777c125700f938fb657a6b777fdf09d39b34b [file] [log] [blame]
Andrew Kaylor3bd2ebd2013-05-28 23:04:25 +00001"""
2Test number of threads.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class CreateDuringStepTestCase(TestBase):
12
13 mydir = os.path.join("functionalities", "thread", "create_during_step")
14
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 @expectedFailureDarwin("llvm.org/pr15824") # thread states not properly maintained
17 @dsym_test
18 def test_step_inst_with_dsym(self):
19 """Test thread creation during step-inst handling."""
20 self.buildDsym(dictionary=self.getBuildFlags())
21 self.create_during_step_inst_test()
22
23 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
24 @expectedFailureDarwin("llvm.org/pr15824") # thread states not properly maintained
25 @dsym_test
26 def test_step_over_with_dsym(self):
27 """Test thread creation during step-over handling."""
28 self.buildDsym(dictionary=self.getBuildFlags())
29 self.create_during_step_over_test()
30
31 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
32 @expectedFailureDarwin("llvm.org/pr15824") # thread states not properly maintained
33 @dsym_test
34 def test_step_in_with_dsym(self):
35 """Test thread creation during step-in handling."""
36 self.buildDsym(dictionary=self.getBuildFlags())
37 self.create_during_step_in_test()
38
39 @expectedFailureDarwin("llvm.org/pr15824") # thread states not properly maintained
40 @dwarf_test
41 def test_step_inst_with_dwarf(self):
42 """Test thread creation during step-inst handling."""
43 self.buildDwarf(dictionary=self.getBuildFlags())
44 self.create_during_step_inst_test()
45
46 @expectedFailureDarwin("llvm.org/pr15824") # thread states not properly maintained
Andrew Kaylor3bd2ebd2013-05-28 23:04:25 +000047 @dwarf_test
48 def test_step_over_with_dwarf(self):
49 """Test thread creation during step-over handling."""
50 self.buildDwarf(dictionary=self.getBuildFlags())
51 self.create_during_step_over_test()
52
53 @expectedFailureDarwin("llvm.org/pr15824") # thread states not properly maintained
Andrew Kaylor3bd2ebd2013-05-28 23:04:25 +000054 @dwarf_test
55 def test_step_in_with_dwarf(self):
56 """Test thread creation during step-in handling."""
57 self.buildDwarf(dictionary=self.getBuildFlags())
58 self.create_during_step_in_test()
59
60 def setUp(self):
61 # Call super's setUp().
62 TestBase.setUp(self)
63 # Find the line numbers to break and continue.
64 self.breakpoint = line_number('main.cpp', '// Set breakpoint here')
65 self.continuepoint = line_number('main.cpp', '// Continue from here')
66
67 def create_during_step_inst_test(self):
68 """Test thread creation while using step-inst."""
Andrew Kaylorf3657f42013-05-29 22:40:17 +000069 self.create_during_step_base("thread step-inst -m all-threads", 'stop reason = instruction step')
Andrew Kaylor3bd2ebd2013-05-28 23:04:25 +000070
71 def create_during_step_over_test(self):
72 """Test thread creation while using step-over."""
Andrew Kaylorf3657f42013-05-29 22:40:17 +000073 self.create_during_step_base("thread step-over -m all-threads", 'stop reason = step over')
Andrew Kaylor3bd2ebd2013-05-28 23:04:25 +000074
75 def create_during_step_in_test(self):
76 """Test thread creation while using step-in."""
Andrew Kaylorf3657f42013-05-29 22:40:17 +000077 self.create_during_step_base("thread step-in -m all-threads", 'stop reason = step in')
Andrew Kaylor3bd2ebd2013-05-28 23:04:25 +000078
Andrew Kaylorf3657f42013-05-29 22:40:17 +000079 def create_during_step_base(self, step_cmd, step_stop_reason):
Andrew Kaylor3bd2ebd2013-05-28 23:04:25 +000080 """Test thread creation while using step-in."""
81 exe = os.path.join(os.getcwd(), "a.out")
82 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
83
84 # This should create a breakpoint in the stepping thread.
85 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.breakpoint, num_expected_locations=1)
86
87 # The breakpoint list should show 1 location.
88 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
89 substrs = ["1: file ='main.cpp', line = %d, locations = 1" % self.breakpoint])
90
91 # Run the program.
92 self.runCmd("run", RUN_SUCCEEDED)
93
94 # The stop reason of the thread should be breakpoint.
95 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
96 substrs = ['stopped',
97 'stop reason = breakpoint'])
98
99 # Get the target process
100 target = self.dbg.GetSelectedTarget()
101 process = target.GetProcess()
102
103 # Get the number of threads
104 num_threads = process.GetNumThreads()
105
106 # Make sure we see only two threads
107 self.assertTrue(num_threads == 2, 'Number of expected threads and actual threads do not match.')
108
109 # Get the thread objects
110 thread1 = process.GetThreadAtIndex(0)
111 thread2 = process.GetThreadAtIndex(1)
112
113 # Make sure both threads are stopped
114 self.assertTrue(thread1.IsStopped(), "Thread 1 didn't stop during breakpoint")
115 self.assertTrue(thread2.IsStopped(), "Thread 2 didn't stop during breakpoint")
116
117 # Keep stepping until we've reached our designated continue point
118 stepping_thread = process.GetSelectedThread()
119 current_line = self.breakpoint
120 while current_line != self.continuepoint:
121 self.runCmd(step_cmd)
122
123 # The thread creation may change the selected thread.
124 # If it does, we just change it back here.
125 if stepping_thread != process.GetSelectedThread():
126 process.SetSelectedThread(stepping_thread)
127
128 frame = stepping_thread.GetFrameAtIndex(0)
129 current_line = frame.GetLineEntry().GetLine()
130
131 # Make sure we're still where we thought we were
132 self.assertTrue(current_line >= self.breakpoint, "Stepped to unexpected line, " + str(current_line))
133 self.assertTrue(current_line <= self.continuepoint, "Stepped to unexpected line, " + str(current_line))
134
135 # Update the number of threads
136 num_threads = process.GetNumThreads()
137
138 # Check to see that we increased the number of threads as expected
139 self.assertTrue(num_threads == 3, 'Number of expected threads and actual threads do not match after thread exit.')
140
Andrew Kaylorf3657f42013-05-29 22:40:17 +0000141 self.expect("thread list", 'Process state is stopped due to step',
Andrew Kaylor3bd2ebd2013-05-28 23:04:25 +0000142 substrs = ['stopped',
Andrew Kaylorf3657f42013-05-29 22:40:17 +0000143 step_stop_reason])
Andrew Kaylor3bd2ebd2013-05-28 23:04:25 +0000144
145 # Run to completion
146 self.runCmd("process continue")
147
148 # At this point, the inferior process should have exited.
149 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
150
151if __name__ == '__main__':
152 import atexit
153 lldb.SBDebugger.Initialize()
154 atexit.register(lambda: lldb.SBDebugger.Terminate())
155 unittest2.main()