xref: /llvm-project/lldb/test/API/functionalities/exec/TestExec.py (revision 773d9c36043204c284324bc37a1de769a7575034)
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        self.assertState(process.GetState(), lldb.eStateStopped)
106        for t in process.threads:
107            if t.stop_reason != lldb.eStopReasonNone:
108                self.assertStopReason(t.stop_reason, lldb.eStopReasonBreakpoint,
109                    "Unexpected stop reason")
110                if self.TraceOn():
111                    print(t)
112                    if t.stop_reason != lldb.eStopReasonBreakpoint:
113                        self.runCmd("bt")
114
115        threads = lldbutil.get_threads_stopped_at_breakpoint(
116            process, breakpoint2)
117        self.assertEqual(len(threads), 1,
118                        "Stopped at breakpoint in exec'ed process.")
119
120    @expectedFailureAll(archs=['i386'],
121                        oslist=no_match(["freebsd"]),
122                        bugnumber="rdar://28656532")
123    @expectedFailureAll(oslist=["ios", "tvos", "watchos", "bridgeos"], bugnumber="rdar://problem/34559552") # this exec test has problems on ios systems
124    @expectedFailureNetBSD
125    @skipIfAsan # rdar://problem/43756823
126    @skipIfWindows
127    def test_correct_thread_plan_state_before_exec(self):
128        '''
129        In this test we make sure that the Thread* cache in the ThreadPlans
130        is cleared correctly when performing exec
131        '''
132
133        self.build()
134        exe = self.getBuildArtifact("a.out")
135        target = self.dbg.CreateTarget(exe)
136
137        (target, process, thread, breakpoint1) = lldbutil.run_to_source_breakpoint(
138            self, 'Set breakpoint 1 here', lldb.SBFileSpec('main.c', False))
139
140        # The stop reason of the thread should be breakpoint.
141        self.assertState(process.GetState(), lldb.eStateStopped,
142                         STOPPED_DUE_TO_BREAKPOINT)
143
144        threads = lldbutil.get_threads_stopped_at_breakpoint(process, breakpoint1)
145        self.assertEqual(len(threads), 1)
146
147        # We perform an instruction step, which effectively sets the cache of the base
148        # thread plan, which should be cleared when a new thread list appears.
149        #
150        # Continuing after this instruction step will trigger a call to
151        # ThreadPlan::ShouldReportRun, which sets the ThreadPlan's Thread cache to
152        # the old Thread* value. In Process::UpdateThreadList we are clearing this
153        # cache in preparation for the new ThreadList.
154        #
155        # Not doing this stepping will cause LLDB to first execute a private single step
156        # past the current breakpoint, which eventually avoids the call to ShouldReportRun,
157        # thus not setting the cache to its invalid value.
158        thread.StepInstruction(False)
159
160        # Run and we should stop due to exec
161        breakpoint2 = target.BreakpointCreateBySourceRegex(
162            'Set breakpoint 2 here', lldb.SBFileSpec("secondprog.cpp", False))
163
164        process.Continue()
165
166        self.assertNotEqual(process.GetState(), lldb.eStateExited,
167                            "Process should not have exited!")
168        self.assertState(process.GetState(), lldb.eStateStopped,
169                         "Process should be stopped at __dyld_start")
170
171        threads = lldbutil.get_stopped_threads(
172            process, lldb.eStopReasonExec)
173        self.assertEqual(
174            len(threads), 1,
175            "We got a thread stopped for exec.")
176
177        # Run and we should stop at breakpoint in main after exec
178        process.Continue()
179
180        threads = lldbutil.get_threads_stopped_at_breakpoint(
181            process, breakpoint2)
182        self.assertEqual(len(threads), 1,
183                        "Stopped at breakpoint in exec'ed process.")
184