aboutsummaryrefslogtreecommitdiff
path: root/lldb/test/API/functionalities/exec/TestExec.py
blob: 968b879c4cd4417dce4cd479c1c16305267c3e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
Test some lldb command abbreviations.
"""

import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class ExecTestCase(TestBase):
    NO_DEBUG_INFO_TESTCASE = True

    @expectedFailureAll(
        archs=["i386"], oslist=no_match(["freebsd"]), bugnumber="rdar://28656532"
    )
    @expectedFailureAll(
        oslist=["ios", "tvos", "watchos", "bridgeos"],
        bugnumber="rdar://problem/34559552",
    )  # this exec test has problems on ios systems
    @expectedFailureNetBSD
    @skipIfAsan  # rdar://problem/43756823
    @skipIfWindows
    def test_hitting_exec(self):
        self.do_test(False)

    @expectedFailureAll(
        archs=["i386"], oslist=no_match(["freebsd"]), bugnumber="rdar://28656532"
    )
    @expectedFailureAll(
        oslist=["ios", "tvos", "watchos", "bridgeos"],
        bugnumber="rdar://problem/34559552",
    )  # this exec test has problems on ios systems
    @expectedFailureNetBSD
    @skipIfAsan  # rdar://problem/43756823
    @skipIfWindows
    def test_skipping_exec(self):
        self.do_test(True)

    def do_test(self, skip_exec):
        self.build()
        exe = self.getBuildArtifact("a.out")
        secondprog = self.getBuildArtifact("secondprog")

        # Create the target
        target = self.dbg.CreateTarget(exe)

        lldbutil.install_to_target(self, secondprog)

        # Create any breakpoints we need
        breakpoint1 = target.BreakpointCreateBySourceRegex(
            "Set breakpoint 1 here", lldb.SBFileSpec("main.c", False)
        )
        self.assertTrue(breakpoint1, VALID_BREAKPOINT)
        breakpoint2 = target.BreakpointCreateBySourceRegex(
            "Set breakpoint 2 here", lldb.SBFileSpec("secondprog.cpp", False)
        )
        self.assertTrue(breakpoint2, VALID_BREAKPOINT)

        # Launch the process
        process = target.LaunchSimple(None, None, self.get_process_working_directory())
        self.assertTrue(process, PROCESS_IS_VALID)

        if self.TraceOn():
            self.runCmd("settings show target.process.stop-on-exec", check=False)
        if skip_exec:
            self.dbg.HandleCommand("settings set target.process.stop-on-exec false")

            def cleanup():
                self.runCmd(
                    "settings set target.process.stop-on-exec false", check=False
                )

            # Execute the cleanup function during test case tear down.
            self.addTearDownHook(cleanup)

        # The stop reason of the thread should be breakpoint.
        self.assertState(
            process.GetState(), lldb.eStateStopped, STOPPED_DUE_TO_BREAKPOINT
        )

        threads = lldbutil.get_threads_stopped_at_breakpoint(process, breakpoint1)
        self.assertEqual(len(threads), 1)

        # We had a deadlock tearing down the TypeSystemMap on exec, but only if some
        # expression had been evaluated.  So make sure we do that here so the teardown
        # is not trivial.

        thread = threads[0]
        value = thread.frames[0].EvaluateExpression("1 + 2")
        self.assertTrue(value.IsValid(), "Expression evaluated successfully")
        int_value = value.GetValueAsSigned()
        self.assertEqual(int_value, 3, "Expression got the right result.")

        # Run and we should stop due to exec
        process.Continue()

        if not skip_exec:
            self.assertNotEqual(
                process.GetState(), lldb.eStateExited, "Process should not have exited!"
            )
            self.assertState(
                process.GetState(),
                lldb.eStateStopped,
                "Process should be stopped at __dyld_start",
            )

            threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonExec)
            self.assertEqual(len(threads), 1, "We got a thread stopped for exec.")

            # Run and we should stop at breakpoint in main after exec
            process.Continue()

        self.assertState(process.GetState(), lldb.eStateStopped)
        for t in process.threads:
            if t.stop_reason != lldb.eStopReasonNone:
                self.assertStopReason(
                    t.stop_reason, lldb.eStopReasonBreakpoint, "Unexpected stop reason"
                )
                if self.TraceOn():
                    print(t)
                    if t.stop_reason != lldb.eStopReasonBreakpoint:
                        self.runCmd("bt")

        threads = lldbutil.get_threads_stopped_at_breakpoint(process, breakpoint2)
        self.assertEqual(len(threads), 1, "Stopped at breakpoint in exec'ed process.")

    @expectedFailureAll(
        archs=["i386"], oslist=no_match(["freebsd"]), bugnumber="rdar://28656532"
    )
    @expectedFailureAll(
        oslist=["ios", "tvos", "watchos", "bridgeos"],
        bugnumber="rdar://problem/34559552",
    )  # this exec test has problems on ios systems
    @expectedFailureNetBSD
    @skipIfAsan  # rdar://problem/43756823
    @skipIfWindows
    def test_correct_thread_plan_state_before_exec(self):
        """
        In this test we make sure that the Thread* cache in the ThreadPlans
        is cleared correctly when performing exec
        """

        self.build()
        exe = self.getBuildArtifact("a.out")
        target = self.dbg.CreateTarget(exe)

        lldbutil.install_to_target(self, self.getBuildArtifact("secondprog"))

        (target, process, thread, breakpoint1) = lldbutil.run_to_source_breakpoint(
            self, "Set breakpoint 1 here", lldb.SBFileSpec("main.c", False)
        )

        # The stop reason of the thread should be breakpoint.
        self.assertState(
            process.GetState(), lldb.eStateStopped, STOPPED_DUE_TO_BREAKPOINT
        )

        threads = lldbutil.get_threads_stopped_at_breakpoint(process, breakpoint1)
        self.assertEqual(len(threads), 1)

        # We perform an instruction step, which effectively sets the cache of the base
        # thread plan, which should be cleared when a new thread list appears.
        #
        # Continuing after this instruction step will trigger a call to
        # ThreadPlan::ShouldReportRun, which sets the ThreadPlan's Thread cache to
        # the old Thread* value. In Process::UpdateThreadList we are clearing this
        # cache in preparation for the new ThreadList.
        #
        # Not doing this stepping will cause LLDB to first execute a private single step
        # past the current breakpoint, which eventually avoids the call to ShouldReportRun,
        # thus not setting the cache to its invalid value.
        thread.StepInstruction(False)

        # Run and we should stop due to exec
        breakpoint2 = target.BreakpointCreateBySourceRegex(
            "Set breakpoint 2 here", lldb.SBFileSpec("secondprog.cpp", False)
        )

        process.Continue()

        self.assertNotEqual(
            process.GetState(), lldb.eStateExited, "Process should not have exited!"
        )
        self.assertState(
            process.GetState(),
            lldb.eStateStopped,
            "Process should be stopped at __dyld_start",
        )

        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonExec)
        self.assertEqual(len(threads), 1, "We got a thread stopped for exec.")

        # Run and we should stop at breakpoint in main after exec
        process.Continue()

        threads = lldbutil.get_threads_stopped_at_breakpoint(process, breakpoint2)
        self.assertEqual(len(threads), 1, "Stopped at breakpoint in exec'ed process.")