xref: /llvm-project/lldb/test/API/functionalities/thread/create_after_attach/TestCreateAfterAttach.py (revision 4cc8f2a017c76af25234afc7c380550e9c93135c)
1"""
2Test thread creation after process attach.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class CreateAfterAttachTestCase(TestBase):
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # Find the line numbers for our breakpoints.
19        self.break_1 = line_number('main.cpp', '// Set first breakpoint here')
20        self.break_2 = line_number('main.cpp', '// Set second breakpoint here')
21        self.break_3 = line_number('main.cpp', '// Set third breakpoint here')
22
23    # Occasionally hangs on Windows, may be same as other issues.
24    @skipIfWindows
25    @skipIfiOSSimulator
26    @expectedFailureNetBSD
27    def test_create_after_attach(self):
28        """Test thread creation after process attach."""
29        self.build()
30        exe = self.getBuildArtifact("a.out")
31
32        # Spawn a new process
33        # use realpath to workaround llvm.org/pr48376
34        popen = self.spawnSubprocess(os.path.realpath(exe))
35        pid = popen.pid
36
37        # Attach to the spawned process
38        self.runCmd("process attach -p " + str(pid))
39
40        target = self.dbg.GetSelectedTarget()
41
42        process = target.GetProcess()
43        self.assertTrue(process, PROCESS_IS_VALID)
44
45        # This should create a breakpoint in the main thread.
46        lldbutil.run_break_set_by_file_and_line(
47            self, "main.cpp", self.break_1, num_expected_locations=1)
48
49        # This should create a breakpoint in the second child thread.
50        lldbutil.run_break_set_by_file_and_line(
51            self, "main.cpp", self.break_2, num_expected_locations=1)
52
53        # This should create a breakpoint in the first child thread.
54        lldbutil.run_break_set_by_file_and_line(
55            self, "main.cpp", self.break_3, num_expected_locations=1)
56
57        # Note:  With std::thread, we cannot rely on particular thread numbers.  Using
58        # std::thread may cause the program to spin up a thread pool (and it does on
59        # Windows), so the thread numbers are non-deterministic.
60
61        # Run to the first breakpoint
62        self.runCmd("continue")
63
64        # The stop reason of the thread should be breakpoint.
65        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
66                    substrs=['stopped',
67                             '* thread #',
68                             'main',
69                             'stop reason = breakpoint'])
70
71        # Change a variable to escape the loop
72        self.runCmd("expression main_thread_continue = 1")
73
74        # Run to the second breakpoint
75        self.runCmd("continue")
76
77        # The stop reason of the thread should be breakpoint.
78        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
79                    substrs=['stopped',
80                             '* thread #',
81                             'thread_2_func',
82                             'stop reason = breakpoint'])
83
84        # Change a variable to escape the loop
85        self.runCmd("expression child_thread_continue = 1")
86
87        # Run to the third breakpoint
88        self.runCmd("continue")
89
90        # The stop reason of the thread should be breakpoint.
91        # Thread 3 may or may not have already exited.
92        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
93                    substrs=['stopped',
94                             '* thread #',
95                             'thread_1_func',
96                             'stop reason = breakpoint'])
97
98        # Run to completion
99        self.runCmd("continue")
100
101        # At this point, the inferior process should have exited.
102        self.assertEqual(
103            process.GetState(), lldb.eStateExited,
104            PROCESS_EXITED)
105