1"""Test lldb reloads the inferior after it was changed during the session."""
2
3
4
5import time
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import configuration
10from lldbsuite.test import lldbutil
11
12
13class ChangedInferiorTestCase(TestBase):
14
15    @skipIf(hostoslist=["windows"])
16    @no_debug_info_test
17    def test_inferior_crashing(self):
18        """Test lldb reloads the inferior after it was changed during the session."""
19        self.build()
20        self.inferior_crashing()
21        self.cleanup()
22        # lldb needs to recognize the inferior has changed. If lldb needs to check the
23        # new module timestamp, make sure it is not the same as the old one, so add a
24        # 1 second delay.
25        time.sleep(1)
26        d = {'C_SOURCES': 'main2.c'}
27        self.build(dictionary=d)
28        self.setTearDownCleanup(dictionary=d)
29        self.inferior_not_crashing()
30
31    def setUp(self):
32        # Call super's setUp().
33        TestBase.setUp(self)
34        # Find the line number of the crash.
35        self.line1 = line_number('main.c', '// Crash here.')
36        self.line2 = line_number('main2.c', '// Not crash here.')
37
38    def inferior_crashing(self):
39        """Inferior crashes upon launching; lldb should catch the event and stop."""
40        self.exe = self.getBuildArtifact("a.out")
41        self.runCmd("file " + self.exe, CURRENT_EXECUTABLE_SET)
42
43        self.runCmd("run", RUN_SUCCEEDED)
44
45        # We should have one crashing thread
46        self.assertEqual(
47            len(lldbutil.get_crashed_threads(self, self.dbg.GetSelectedTarget().GetProcess())),
48            1,
49            STOPPED_DUE_TO_EXC_BAD_ACCESS)
50
51        # And it should report the correct line number.
52        self.expect("thread backtrace all", substrs=['main.c:%d' % self.line1])
53
54    def inferior_not_crashing(self):
55        """Test lldb reloads the inferior after it was changed during the session."""
56        self.runCmd("process kill")
57        self.runCmd("run", RUN_SUCCEEDED)
58        self.runCmd("process status")
59
60        self.assertNotEqual(
61            len(lldbutil.get_crashed_threads(self, self.dbg.GetSelectedTarget().GetProcess())),
62            1,
63            "Inferior changed, but lldb did not perform a reload")
64
65        # Break inside the main.
66        lldbutil.run_break_set_by_file_and_line(
67            self, "main2.c", self.line2, num_expected_locations=1, loc_exact=True)
68
69        self.runCmd("run", RUN_SUCCEEDED)
70
71        # The stop reason of the thread should be breakpoint.
72        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
73                    substrs=['stopped',
74                             'stop reason = breakpoint'])
75
76        self.runCmd("frame variable int_ptr")
77        self.expect("frame variable *int_ptr",
78                    substrs=['= 7'])
79        self.expect("expression *int_ptr",
80                    substrs=['= 7'])
81