xref: /llvm-project/lldb/test/API/functionalities/postmortem/FreeBSDKernel/TestFreeBSDKernelLive.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1fb785877SMichał Górnyimport os
2fb785877SMichał Górnyimport struct
3fb785877SMichał Górnyimport subprocess
4fb785877SMichał Górny
5fb785877SMichał Górnyimport lldb
6fb785877SMichał Górnyfrom lldbsuite.test.decorators import *
7fb785877SMichał Górnyfrom lldbsuite.test.lldbtest import *
8fb785877SMichał Górnyfrom lldbsuite.test import lldbutil
9fb785877SMichał Górny
10fb785877SMichał Górny
11fb785877SMichał Górnyclass FreeBSDKernelVMCoreTestCase(TestBase):
12fb785877SMichał Górny    NO_DEBUG_INFO_TESTCASE = True
13fb785877SMichał Górny
14fb785877SMichał Górny    def test_mem(self):
15fb785877SMichał Górny        kernel_exec = "/boot/kernel/kernel"
16fb785877SMichał Górny        mem_device = "/dev/mem"
17fb785877SMichał Górny
18fb785877SMichał Górny        if not os.access(kernel_exec, os.R_OK):
19fb785877SMichał Górny            self.skipTest("Kernel @ %s is not readable" % (kernel_exec,))
20fb785877SMichał Górny        if not os.access(mem_device, os.R_OK):
21fb785877SMichał Górny            self.skipTest("Memory @ %s is not readable" % (mem_device,))
22fb785877SMichał Górny
23fb785877SMichał Górny        target = self.dbg.CreateTarget(kernel_exec)
24fb785877SMichał Górny        process = target.LoadCore(mem_device)
25fb785877SMichał Górny        hz_value = int(subprocess.check_output(["sysctl", "-n", "kern.hz"]))
26fb785877SMichał Górny
27fb785877SMichał Górny        self.assertTrue(process, PROCESS_IS_VALID)
28fb785877SMichał Górny        self.assertEqual(process.GetNumThreads(), 1)
29fb785877SMichał Górny        self.assertEqual(process.GetProcessID(), 0)
30fb785877SMichał Górny
31fb785877SMichał Górny        # test memory reading
32*2238dcc3SJonas Devlieghere        self.expect("expr -- *(int *) &hz", substrs=["(int) $0 = %d" % hz_value])
33fb785877SMichał Górny
34fb785877SMichał Górny        main_mod = target.GetModuleAtIndex(0)
35*2238dcc3SJonas Devlieghere        hz_addr = main_mod.FindSymbols("hz")[0].symbol.addr.GetLoadAddress(target)
36fb785877SMichał Górny        error = lldb.SBError()
37*2238dcc3SJonas Devlieghere        self.assertEqual(
38*2238dcc3SJonas Devlieghere            process.ReadMemory(hz_addr, 4, error), struct.pack("<I", hz_value)
39*2238dcc3SJonas Devlieghere        )
40fb785877SMichał Górny
41fb785877SMichał Górny        self.dbg.DeleteTarget(target)
42