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