xref: /llvm-project/lldb/test/API/macosx/stack-corefile/TestStackCorefile.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""Test that lldb can create a stack-only corefile, and load the main binary."""
2
3import os
4import re
5import subprocess
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class TestStackCorefile(TestBase):
14    @skipIfOutOfTreeDebugserver  # newer debugserver required for these qMemoryRegionInfo types
15    @no_debug_info_test
16    @skipUnlessDarwin
17    @skipIfRemote
18    def test(self):
19        corefile = self.getBuildArtifact("process.core")
20        self.build()
21        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
22            self, "// break here", lldb.SBFileSpec("main.c")
23        )
24
25        frame = thread.GetFrameAtIndex(0)
26        stack_int = frame.GetValueForVariablePath("stack_int")
27        heap_int = frame.GetValueForVariablePath("*heap_int")
28        stack_str = frame.GetValueForVariablePath("stack_str")
29        heap_str = frame.GetValueForVariablePath("heap_str")
30        self.assertEqual(stack_int.GetValueAsUnsigned(), 5)
31        self.assertEqual(heap_int.GetValueAsUnsigned(), 10)
32        self.assertEqual(stack_str.summary, '"stack string"')
33        self.assertEqual(heap_str.summary, '"heap string"')
34
35        self.runCmd("process save-core -s stack " + corefile)
36        process.Kill()
37        self.dbg.DeleteTarget(target)
38
39        # Now load the corefile
40        target = self.dbg.CreateTarget("")
41        process = target.LoadCore(corefile)
42        thread = process.GetSelectedThread()
43        self.assertTrue(process.IsValid())
44        self.assertTrue(process.GetSelectedThread().IsValid())
45        if self.TraceOn():
46            self.runCmd("image list")
47            self.runCmd("bt")
48            self.runCmd("fr v")
49        num_modules = target.GetNumModules()
50        #  We should only have the a.out binary and possibly
51        # the libdyld.dylib.  Extra libraries loaded means
52        # extra LC_NOTE and unnecessarily large corefile.
53        self.assertTrue(num_modules == 1 or num_modules == 2)
54
55        # The heap variables should be unavailable now.
56        frame = thread.GetFrameAtIndex(0)
57        stack_int = frame.GetValueForVariablePath("stack_int")
58        heap_int = frame.GetValueForVariablePath("*heap_int")
59        stack_str = frame.GetValueForVariablePath("stack_str")
60        heap_str = frame.GetValueForVariablePath("heap_str")
61
62        ## The heap SBValues both come back as IsValid()==true,
63        ## which I'm not so sure is a great/correct thing --
64        ## it hides the memory read error that actually happened,
65        ## and we don't have a real value.
66        self.assertEqual(stack_int.GetValueAsUnsigned(), 5)
67        self.assertEqual(heap_int.GetValueAsUnsigned(), 0)
68        self.assertEqual(stack_str.summary, '"stack string"')
69        self.assertEqual(heap_str.summary, '""')
70