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