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