xref: /llvm-project/lldb/test/API/functionalities/exec/TestExec.py (revision 193259cbcec77add8e189c4dedeefb15fef50d5e)
1"""
2Test some lldb command abbreviations.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class ExecTestCase(TestBase):
12
13    NO_DEBUG_INFO_TESTCASE = True
14
15    @expectedFailureAll(archs=['i386'],
16                        oslist=no_match(["freebsd"]),
17                        bugnumber="rdar://28656532")
18    @expectedFailureAll(oslist=["ios", "tvos", "watchos", "bridgeos"], bugnumber="rdar://problem/34559552") # this exec test has problems on ios systems
19    @expectedFailureNetBSD
20    @skipIfAsan # rdar://problem/43756823
21    @skipIfWindows
22    def test_hitting_exec (self):
23        self.do_test(False)
24
25    @expectedFailureAll(archs=['i386'],
26                        oslist=no_match(["freebsd"]),
27                        bugnumber="rdar://28656532")
28    @expectedFailureAll(oslist=["ios", "tvos", "watchos", "bridgeos"], bugnumber="rdar://problem/34559552") # this exec test has problems on ios systems
29    @expectedFailureNetBSD
30    @skipIfAsan # rdar://problem/43756823
31    @skipIfWindows
32    def test_skipping_exec (self):
33        self.do_test(True)
34
35    def do_test(self, skip_exec):
36        self.build()
37        exe = self.getBuildArtifact("a.out")
38        secondprog = self.getBuildArtifact("secondprog")
39
40        # Create the target
41        target = self.dbg.CreateTarget(exe)
42
43        # Create any breakpoints we need
44        breakpoint1 = target.BreakpointCreateBySourceRegex(
45            'Set breakpoint 1 here', lldb.SBFileSpec("main.c", False))
46        self.assertTrue(breakpoint1, VALID_BREAKPOINT)
47        breakpoint2 = target.BreakpointCreateBySourceRegex(
48            'Set breakpoint 2 here', lldb.SBFileSpec("secondprog.cpp", False))
49        self.assertTrue(breakpoint2, VALID_BREAKPOINT)
50
51        # Launch the process
52        process = target.LaunchSimple(
53            None, None, self.get_process_working_directory())
54        self.assertTrue(process, PROCESS_IS_VALID)
55
56        if self.TraceOn():
57            self.runCmd("settings show target.process.stop-on-exec", check=False)
58        if skip_exec:
59            self.dbg.HandleCommand("settings set target.process.stop-on-exec false")
60            def cleanup():
61                self.runCmd("settings set target.process.stop-on-exec false",
62                            check=False)
63
64            # Execute the cleanup function during test case tear down.
65            self.addTearDownHook(cleanup)
66
67        # The stop reason of the thread should be breakpoint.
68        self.assertState(process.GetState(), lldb.eStateStopped,
69                         STOPPED_DUE_TO_BREAKPOINT)
70
71        threads = lldbutil.get_threads_stopped_at_breakpoint(
72        process, breakpoint1)
73        self.assertEqual(len(threads), 1)
74
75        # We had a deadlock tearing down the TypeSystemMap on exec, but only if some
76        # expression had been evaluated.  So make sure we do that here so the teardown
77        # is not trivial.
78
79        thread = threads[0]
80        value = thread.frames[0].EvaluateExpression("1 + 2")
81        self.assertTrue(
82            value.IsValid(),
83            "Expression evaluated successfully")
84        int_value = value.GetValueAsSigned()
85        self.assertEqual(int_value, 3, "Expression got the right result.")
86
87        # Run and we should stop due to exec
88        process.Continue()
89
90        if not skip_exec:
91            self.assertNotEqual(process.GetState(), lldb.eStateExited,
92                                "Process should not have exited!")
93            self.assertState(process.GetState(), lldb.eStateStopped,
94                             "Process should be stopped at __dyld_start")
95
96            threads = lldbutil.get_stopped_threads(
97                process, lldb.eStopReasonExec)
98            self.assertEqual(
99                len(threads), 1,
100                "We got a thread stopped for exec.")
101
102            # Run and we should stop at breakpoint in main after exec
103            process.Continue()
104
105        threads = lldbutil.get_threads_stopped_at_breakpoint(
106            process, breakpoint2)
107        if self.TraceOn():
108            for t in process.threads:
109                print(t)
110                if t.GetStopReason() != lldb.eStopReasonBreakpoint:
111                    self.runCmd("bt")
112        self.assertEqual(len(threads), 1,
113                        "Stopped at breakpoint in exec'ed process.")
114
115    @expectedFailureAll(archs=['i386'],
116                        oslist=no_match(["freebsd"]),
117                        bugnumber="rdar://28656532")
118    @expectedFailureAll(oslist=["ios", "tvos", "watchos", "bridgeos"], bugnumber="rdar://problem/34559552") # this exec test has problems on ios systems
119    @expectedFailureNetBSD
120    @skipIfAsan # rdar://problem/43756823
121    @skipIfWindows
122    def test_correct_thread_plan_state_before_exec(self):
123        '''
124        In this test we make sure that the Thread* cache in the ThreadPlans
125        is cleared correctly when performing exec
126        '''
127
128        self.build()
129        exe = self.getBuildArtifact("a.out")
130        target = self.dbg.CreateTarget(exe)
131
132        (target, process, thread, breakpoint1) = lldbutil.run_to_source_breakpoint(
133            self, 'Set breakpoint 1 here', lldb.SBFileSpec('main.c', False))
134
135        # The stop reason of the thread should be breakpoint.
136        self.assertState(process.GetState(), lldb.eStateStopped,
137                         STOPPED_DUE_TO_BREAKPOINT)
138
139        threads = lldbutil.get_threads_stopped_at_breakpoint(process, breakpoint1)
140        self.assertEqual(len(threads), 1)
141
142        # We perform an instruction step, which effectively sets the cache of the base
143        # thread plan, which should be cleared when a new thread list appears.
144        #
145        # Continuing after this instruction step will trigger a call to
146        # ThreadPlan::ShouldReportRun, which sets the ThreadPlan's Thread cache to
147        # the old Thread* value. In Process::UpdateThreadList we are clearing this
148        # cache in preparation for the new ThreadList.
149        #
150        # Not doing this stepping will cause LLDB to first execute a private single step
151        # past the current breakpoint, which eventually avoids the call to ShouldReportRun,
152        # thus not setting the cache to its invalid value.
153        thread.StepInstruction(False)
154
155        # Run and we should stop due to exec
156        breakpoint2 = target.BreakpointCreateBySourceRegex(
157            'Set breakpoint 2 here', lldb.SBFileSpec("secondprog.cpp", False))
158
159        process.Continue()
160
161        self.assertNotEqual(process.GetState(), lldb.eStateExited,
162                            "Process should not have exited!")
163        self.assertState(process.GetState(), lldb.eStateStopped,
164                         "Process should be stopped at __dyld_start")
165
166        threads = lldbutil.get_stopped_threads(
167            process, lldb.eStopReasonExec)
168        self.assertEqual(
169            len(threads), 1,
170            "We got a thread stopped for exec.")
171
172        # Run and we should stop at breakpoint in main after exec
173        process.Continue()
174
175        threads = lldbutil.get_threads_stopped_at_breakpoint(
176            process, breakpoint2)
177        self.assertEqual(len(threads), 1,
178                        "Stopped at breakpoint in exec'ed process.")
179