Debian Codegraph @

tests/linux_kernel/vmcore/test_vmcore.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import os
5 import unittest
6
7 from _drgn_util.platform import NORMALIZED_MACHINE_NAME
8 from drgn import Object, Program, ProgramFlags
9 from drgn.helpers.linux.pid import find_task
10 from drgn.helpers.linux.stack import StackKind, kernel_stack_trace
11 from drgn.helpers.linux.timekeeping import ktime_get_real_seconds
12 from tests import TestCase
13 from tests.linux_kernel import skip_unless_have_stack_tracing
14 from tests.linux_kernel.vmcore import VMCORE_PATH, LinuxVMCoreTestCase
15 from util import KernelVersion
16
17
18 class TestVMCore(LinuxVMCoreTestCase):
19 # When CPU 0 panics on s390x, the kernel switches to a different stack that
20 # we don't know how to unwind through. This is a bug. For now,
21 # vmtest.enter_kdump tries to avoid this so we can get some test coverage,
22 # and we skip the tests otherwise.
23 def _skip_if_cpu0_on_s390x(self):
24 if NORMALIZED_MACHINE_NAME == "s390x" and self.prog["panic_cpu"].counter == 0:
25 self.skipTest("drgn can't unwind s390x panic stack on CPU 0")
26
27 def test_program_flags(self):
28 self.assertFalse(self.prog.flags & ProgramFlags.IS_LIVE)
29 self.assertTrue(self.prog.flags & ProgramFlags.IS_LINUX_KERNEL)
30
31 def test_threads(self):
32 tids = [thread.tid for thread in self.prog.threads()]
33 self.assertIn(1, tids)
34 self.assertIn(self.prog.crashed_thread().tid, tids)
35
36 def test_thread(self):
37 thread = self.prog.thread(1)
38 self.assertEqual(thread.tid, 1)
39 self.assertEqual(thread.object, find_task(self.prog, 1))
40
41 crashed_thread_tid = self.prog.crashed_thread().tid
42 self.assertEqual(self.prog.thread(crashed_thread_tid).tid, crashed_thread_tid)
43
44 def test_thread_not_found(self):
45 tids = {thread.tid for thread in self.prog.threads()}
46 tid = 1
47 while tid in tids:
48 tid += 1
49 self.assertRaises(LookupError, self.prog.thread, tid)
50
51 def test_main_thread(self):
52 self.assertRaisesRegex(
53 ValueError,
54 "main thread is not defined for the Linux kernel",
55 self.prog.main_thread,
56 )
57
58 def test_crashed_thread(self):
59 crashed_thread = self.prog.crashed_thread()
60 # This assumes that we crashed from vmtest.enter_kdump. I don't know
61 # why anyone would run these tests from kdump otherwise.
62 self.assertEqual(crashed_thread.object.comm.string_(), b"selfdestruct")
63
64 def _test_crashed_thread_stack_trace(self, trace):
65 # This assumes that we crashed using the drgn_test kmod. Note that on
66 # supported architectures, drgn_test_crash_func() is called from an IRQ
67 # handler that interrupts drgn_test_crash_store().
68 trace_iter = iter(trace)
69 for frame in trace_iter:
70 if frame.name == "drgn_test_crash_func":
71 break
72 else:
73 self.fail("drgn_test_crash_func frame not found")
74
75 for frame in trace_iter:
76 if frame.name == "drgn_test_crash_store":
77 break
78 else:
79 self.fail(
80 "drgn_test_crash_store frame not found below drgn_test_crash_func"
81 )
82
83 @skip_unless_have_stack_tracing
84 def test_crashed_thread_stack_trace(self):
85 self._skip_if_cpu0_on_s390x()
86 self._test_crashed_thread_stack_trace(self.prog.crashed_thread().stack_trace())
87
88 @skip_unless_have_stack_tracing
89 def test_crashed_thread_stack_trace_by_tid(self):
90 self._skip_if_cpu0_on_s390x()
91 self._test_crashed_thread_stack_trace(
92 self.prog.stack_trace(self.prog.crashed_thread().tid)
93 )
94
95 @skip_unless_have_stack_tracing
96 def test_crashed_thread_stack_trace_by_task_struct(self):
97 self._skip_if_cpu0_on_s390x()
98 self._test_crashed_thread_stack_trace(
99 self.prog.stack_trace(self.prog.crashed_thread().object)
100 )
101
102 @skip_unless_have_stack_tracing
103 def test_kernel_stack_trace(self):
104 self._skip_if_cpu0_on_s390x()
105 trace = kernel_stack_trace(self.prog.crashed_thread().object)
106 self.assertEqual(trace.prog, self.prog)
107 if NORMALIZED_MACHINE_NAME == "x86_64":
108 # Since v5.1 we can identify NMI. Older kernels we just classify as
109 # "exception".
110 self.assertIn(trace.segments[0].kind, (StackKind.NMI, StackKind.EXCEPTION))
111 # Prior to commit 946c191161cef ("x86/entry/unwind: Create stack
112 # frames for saved interrupt registers"), and without ORC, drgn
113 # cannot find the saved pt_regs, so the interrupted frame is not
114 # marked interrupted. The full stack is still unwound, but without
115 # the interrupted frame, we can't split it into multiple segments.
116 if KernelVersion(os.uname().release) >= KernelVersion("4.14"):
117 self.assertEqual(trace.segments[1].kind, StackKind.IRQ)
118 self.assertEqual(trace.segments[2].kind, StackKind.TASK)
119 elif NORMALIZED_MACHINE_NAME in ("aarch64", "arm"):
120 # arm and aarch64 can unwind through IRQ stacks and correctly
121 # identify them
122 self.assertEqual(trace.segments[0].kind, StackKind.IRQ)
123 self.assertEqual(trace.segments[1].kind, StackKind.TASK)
124 else:
125 # On ppc64 drgn can unwind through IRQ stacks, but there is no
126 # separate IRQ stack, nor does drgn see an interrupted stack frame.
127 # drgn cannot yet unwind through s390x exceptions, so we don't crash
128 # from IRQ there.
129 self.assertEqual(trace.segments[0].kind, StackKind.TASK)
130
131
132 @unittest.skipUnless(VMCORE_PATH.exists(), "not running in kdump")
133 class TestVMCoreNoDebugInfo(TestCase):
134 @classmethod
135 def setUpClass(cls):
136 super().setUpClass()
137 cls.prog = Program()
138 cls.prog.set_core_dump(VMCORE_PATH)
139
140 @classmethod
141 def tearDownClass(cls):
142 del cls.prog
143
144 def test_ktime_get_real_seconds(self):
145 self.assertIsInstance(ktime_get_real_seconds(self.prog), Object)