1"""
2Test lldb process crash info.
3"""
4
5import os
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11from lldbsuite.test import lldbtest
12
13
14class PlatformProcessCrashInfoTestCase(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17
18    def setUp(self):
19        TestBase.setUp(self)
20        self.runCmd("settings set auto-confirm true")
21        self.source = "main.c"
22        self.line = line_number(self.source, '// break here')
23
24    def tearDown(self):
25        self.runCmd("settings clear auto-confirm")
26        TestBase.tearDown(self)
27
28    @skipUnlessDarwin
29    def test_cli(self):
30        """Test that `process status --verbose` fetches the extended crash
31        information dictionnary from the command-line properly."""
32        self.build()
33        exe = self.getBuildArtifact("a.out")
34        self.expect("file " + exe,
35                    patterns=["Current executable set to .*a.out"])
36
37        self.expect('process launch',
38                    patterns=["Process .* launched: .*a.out"])
39
40        self.expect('process status --verbose',
41                    patterns=["\"message\".*pointer being freed was not allocated"])
42
43
44    @skipUnlessDarwin
45    def test_api(self):
46        """Test that lldb can fetch a crashed process' extended crash information
47        dictionnary from the api properly."""
48        self.build()
49        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
50        self.assertTrue(target, VALID_TARGET)
51
52        target.LaunchSimple(None, None, os.getcwd())
53
54        stream = lldb.SBStream()
55        self.assertTrue(stream)
56
57        process = target.GetProcess()
58        self.assertTrue(process)
59
60        crash_info = process.GetExtendedCrashInformation()
61
62        error = crash_info.GetAsJSON(stream)
63
64        self.assertTrue(error.Success())
65
66        self.assertTrue(crash_info.IsValid())
67
68        self.assertIn("pointer being freed was not allocated", stream.GetData())
69
70    def test_on_sane_process(self):
71        """Test that lldb doesn't fetch the extended crash information
72        dictionnary from a 'sane' stopped process."""
73        self.build()
74        target, _, _, _ = lldbutil.run_to_line_breakpoint(self, lldb.SBFileSpec(self.source),
75                                        self.line)
76
77        stream = lldb.SBStream()
78        self.assertTrue(stream)
79
80        process = target.GetProcess()
81        self.assertTrue(process)
82
83        crash_info = process.GetExtendedCrashInformation()
84
85        error = crash_info.GetAsJSON(stream)
86        self.assertFalse(error.Success())
87        self.assertIn("No structured data.", error.GetCString())
88