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    mydir = TestBase.compute_mydir(__file__)
18
19    @expectedFailureAll(archs=['i386'], 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    @skipIfFreeBSD  # hangs
24    @skipIfWindows
25    def test_hitting_exec (self):
26        self.do_test(False)
27
28    @expectedFailureAll(archs=['i386'], bugnumber="rdar://28656532")
29    @expectedFailureAll(oslist=["ios", "tvos", "watchos", "bridgeos"], bugnumber="rdar://problem/34559552") # this exec test has problems on ios systems
30    @expectedFailureNetBSD
31    @skipIfAsan # rdar://problem/43756823
32    @skipIfFreeBSD  # hangs
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.assertTrue(process.GetState() == lldb.eStateStopped,
71                        STOPPED_DUE_TO_BREAKPOINT)
72
73        threads = lldbutil.get_threads_stopped_at_breakpoint(
74        process, breakpoint1)
75        self.assertTrue(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.assertTrue(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.assertFalse(process.GetState() == lldb.eStateExited,
94                             "Process should not have exited!")
95            self.assertTrue(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.assertTrue(
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.assertTrue(len(threads) == 1,
115                        "Stopped at breakpoint in exec'ed process.")
116