xref: /llvm-project/lldb/test/API/macosx/save_crashlog/TestSaveCrashlog.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test that the save_crashlog command functions
3"""
4
5
6import os
7import lldb
8import lldbsuite.test.lldbutil as lldbutil
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test.decorators import *
11
12
13class TestSaveCrashlog(TestBase):
14    # If your test case doesn't stress debug info, then
15    # set this to true.  That way it won't be run once for
16    # each debug info format.
17    NO_DEBUG_INFO_TESTCASE = True
18
19    @skipUnlessDarwin
20    def test_save_crashlog(self):
21        """There can be many tests in a test case - describe this test here."""
22        self.build()
23        self.main_source_file = lldb.SBFileSpec("main.c")
24        self.save_crashlog()
25
26    def save_crashlog(self):
27        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
28            self, "I was called", self.main_source_file
29        )
30
31        self.runCmd("command script import lldb.macosx.crashlog")
32        out_file = os.path.join(self.getBuildDir(), "crash.log")
33        self.runCmd("save_crashlog '%s'" % (out_file))
34
35        # Make sure we wrote the file:
36        self.assertTrue(os.path.exists(out_file), "We wrote our file")
37
38        # Now scan the file to make sure it looks right:
39        # First get a few facts we'll use:
40        exe_module = target.FindModule(target.GetExecutable())
41        uuid_str = exe_module.GetUUIDString()
42
43        # We'll set these to true when we find the elements in the file
44        found_call_me = False
45        found_main_line = False
46        found_thread_header = False
47        found_uuid_str = False
48
49        with open(out_file, "r") as f:
50            # We want to see a line with
51            for line in f:
52                if "Thread 0:" in line:
53                    found_thread_header = True
54                if "call_me" in line and "main.c:" in line:
55                    found_call_me = True
56                if "main" in line and "main.c:" in line:
57                    found_main_line = True
58                if uuid_str in line and "a.out" in line:
59                    found_uuid_str = True
60
61        self.assertTrue(found_thread_header, "Found thread header")
62        self.assertTrue(found_call_me, "Found call_me line in stack")
63        self.assertTrue(found_uuid_str, "Found main binary UUID")
64        self.assertTrue(found_main_line, "Found main line in call stack")
65