| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
from contextlib import redirect_stdout |
| 5 |
import io |
| 6 |
|
| 7 |
from _drgn_util.platform import NORMALIZED_MACHINE_NAME |
| 8 |
from drgn import offsetof, sizeof |
| 9 |
from drgn.helpers.common.memory import ( |
| 10 |
IdentifiedSymbol, |
| 11 |
identify_address, |
| 12 |
identify_address_all, |
| 13 |
print_annotated_memory, |
| 14 |
) |
| 15 |
from drgn.helpers.common.stack import print_annotated_stack, print_registers |
| 16 |
from drgn.helpers.linux.common import ( |
| 17 |
IdentifiedSlabObject, |
| 18 |
IdentifiedTaskStack, |
| 19 |
IdentifiedTaskStruct, |
| 20 |
IdentifiedVmap, |
| 21 |
) |
| 22 |
from drgn.helpers.linux.mm import pfn_to_virt |
| 23 |
from drgn.helpers.linux.sched import idle_task |
| 24 |
from tests.linux_kernel import ( |
| 25 |
HAVE_FULL_MM_SUPPORT, |
| 26 |
LinuxKernelTestCase, |
| 27 |
online_cpus, |
| 28 |
skip_if_slob, |
| 29 |
skip_unless_have_full_mm_support, |
| 30 |
skip_unless_have_stack_tracing, |
| 31 |
skip_unless_have_test_kmod, |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
class TestIdentifyAddress(LinuxKernelTestCase): |
| 36 |
@skip_unless_have_test_kmod |
| 37 |
def test_identify_symbol(self): |
| 38 |
symbol = self.prog.symbol("drgn_test_function") |
| 39 |
|
| 40 |
identified = list(identify_address_all(self.prog, symbol.address)) |
| 41 |
self.assertIsInstance(identified[0], IdentifiedSymbol) |
| 42 |
self.assertEqual(str(identified[0]), "function symbol: drgn_test_function+0x0") |
| 43 |
# Module symbols are also vmapped. |
| 44 |
self.assertIsInstance(identified[1], IdentifiedVmap) |
| 45 |
self.assertEqual(len(identified), 2) |
| 46 |
|
| 47 |
self.assertEqual( |
| 48 |
identify_address(self.prog, symbol.address + 1), |
| 49 |
"function symbol: drgn_test_function+0x1", |
| 50 |
) |
| 51 |
|
| 52 |
@skip_unless_have_full_mm_support |
| 53 |
@skip_unless_have_test_kmod |
| 54 |
def test_identify_slab_cache(self): |
| 55 |
for size in ("small", "big"): |
| 56 |
with self.subTest(size=size): |
| 57 |
objects = self.prog[f"drgn_test_{size}_slab_objects"] |
| 58 |
|
| 59 |
if self.prog["drgn_test_slob"]: |
| 60 |
for obj in objects: |
| 61 |
if size == "small": |
| 62 |
self.assertEqual( |
| 63 |
identify_address(obj), "unknown slab object" |
| 64 |
) |
| 65 |
else: |
| 66 |
self.assertIsNone(identify_address(obj)) |
| 67 |
else: |
| 68 |
for obj in objects: |
| 69 |
self.assertEqual( |
| 70 |
identify_address(obj), |
| 71 |
f"slab object: drgn_test_{size}+0x0", |
| 72 |
) |
| 73 |
|
| 74 |
@skip_unless_have_full_mm_support |
| 75 |
@skip_unless_have_test_kmod |
| 76 |
@skip_if_slob |
| 77 |
def test_identify_task(self): |
| 78 |
identified = list(identify_address_all(self.prog["drgn_test_kthread"])) |
| 79 |
self.assertIsInstance(identified[0], IdentifiedTaskStruct) |
| 80 |
self.assertEqual(identified[0].task, self.prog["drgn_test_kthread"]) |
| 81 |
self.assertEqual( |
| 82 |
str(identified[0]), |
| 83 |
f"task: {self.prog['drgn_test_kthread'].pid.value_()} (drgn_test_kthre)", |
| 84 |
) |
| 85 |
self.assertIsInstance(identified[1], IdentifiedSlabObject) |
| 86 |
self.assertEqual(len(identified), 2) |
| 87 |
|
| 88 |
@skip_unless_have_full_mm_support |
| 89 |
@skip_unless_have_test_kmod |
| 90 |
@skip_if_slob |
| 91 |
def test_identify_task_member(self): |
| 92 |
pid_offset = offsetof(self.prog.type("struct task_struct"), "pid") |
| 93 |
self.assertEqual( |
| 94 |
identify_address(self.prog, self.prog["drgn_test_kthread"].pid.address_), |
| 95 |
f"task: {self.prog['drgn_test_kthread'].pid.value_()} (drgn_test_kthre) +{hex(pid_offset)}", |
| 96 |
) |
| 97 |
|
| 98 |
def test_identify_idle_task_0(self): |
| 99 |
identified = list(identify_address_all(self.prog["init_task"].address_of_())) |
| 100 |
self.assertIsInstance(identified[0], IdentifiedTaskStruct) |
| 101 |
self.assertEqual(identified[0].task, idle_task(self.prog, 0)) |
| 102 |
self.assertIsInstance(identified[1], IdentifiedSymbol) |
| 103 |
self.assertGreaterEqual(len(identified), 2) |
| 104 |
|
| 105 |
@skip_unless_have_full_mm_support |
| 106 |
@skip_if_slob |
| 107 |
def test_identify_idle_task_1(self): |
| 108 |
for cpu in online_cpus(): |
| 109 |
if cpu > 0: |
| 110 |
break |
| 111 |
else: |
| 112 |
self.skipTest("online CPU > 0 not found") |
| 113 |
|
| 114 |
task = idle_task(self.prog, cpu) |
| 115 |
identified = list(identify_address_all(task)) |
| 116 |
self.assertIsInstance(identified[0], IdentifiedTaskStruct) |
| 117 |
self.assertEqual(identified[0].task, task) |
| 118 |
self.assertIsInstance(identified[1], IdentifiedSlabObject) |
| 119 |
self.assertEqual(len(identified), 2) |
| 120 |
|
| 121 |
@skip_unless_have_test_kmod |
| 122 |
def test_identify_vmap(self): |
| 123 |
for cache in (None, {}): |
| 124 |
with self.subTest("uncached" if cache is None else "cached"): |
| 125 |
self.assertTrue( |
| 126 |
identify_address( |
| 127 |
self.prog["drgn_test_vmalloc_va"], cache=cache |
| 128 |
).startswith("vmap: 0x") |
| 129 |
) |
| 130 |
|
| 131 |
@skip_unless_have_full_mm_support |
| 132 |
@skip_unless_have_test_kmod |
| 133 |
def test_identify_task_stack(self): |
| 134 |
if self.prog["drgn_test_slob"] and self.prog["drgn_test_slab_stack_enabled"]: |
| 135 |
self.skipTest("test does not support SLOB") |
| 136 |
|
| 137 |
for cached in (False, True): |
| 138 |
with self.subTest("cached" if cached else "uncached"): |
| 139 |
identified = list( |
| 140 |
identify_address_all( |
| 141 |
self.prog, |
| 142 |
self.prog["drgn_test_kthread"].stack.value_() + 1234, |
| 143 |
cache={} if cached else None, |
| 144 |
) |
| 145 |
) |
| 146 |
self.assertIsInstance(identified[0], IdentifiedTaskStack) |
| 147 |
self.assertEqual( |
| 148 |
str(identified[0]), |
| 149 |
f"task stack: {self.prog['drgn_test_kthread'].pid.value_()} (drgn_test_kthre) +0x4d2", |
| 150 |
) |
| 151 |
if self.prog["drgn_test_vmap_stack_enabled"]: |
| 152 |
self.assertIsInstance(identified[1], IdentifiedVmap) |
| 153 |
self.assertEqual(len(identified), 2) |
| 154 |
elif self.prog["drgn_test_slab_stack_enabled"]: |
| 155 |
self.assertIsInstance(identified[1], IdentifiedSlabObject) |
| 156 |
self.assertEqual(len(identified), 2) |
| 157 |
else: |
| 158 |
self.assertEqual(len(identified), 1) |
| 159 |
|
| 160 |
@skip_unless_have_full_mm_support |
| 161 |
def test_identify_idle_task_0_stack(self): |
| 162 |
identified = list(identify_address_all(self.prog["init_task"].stack)) |
| 163 |
self.assertIsInstance(identified[0], IdentifiedTaskStack) |
| 164 |
self.assertEqual(identified[0].task, idle_task(self.prog, 0)) |
| 165 |
# s390x between Linux kernel commits ce3dc447493f ("s390: add support |
| 166 |
# for virtually mapped kernel stacks") (in v4.20) and 944c78376a39 |
| 167 |
# ("s390: use init_thread_union aka initial stack for the first |
| 168 |
# process") (in v6.4) allocates init_task.stack. |
| 169 |
if NORMALIZED_MACHINE_NAME == "s390x": |
| 170 |
if self.prog["drgn_test_vmap_stack_enabled"]: |
| 171 |
self.assertIsInstance(identified[1], (IdentifiedSymbol, IdentifiedVmap)) |
| 172 |
self.assertEqual(len(identified), 2) |
| 173 |
elif self.prog["drgn_test_slab_stack_enabled"]: |
| 174 |
self.assertIsInstance( |
| 175 |
identified[1], (IdentifiedSymbol, IdentifiedSlabObject) |
| 176 |
) |
| 177 |
self.assertEqual(len(identified), 2) |
| 178 |
elif len(identified) > 1: |
| 179 |
self.assertIsInstance(identified[1], IdentifiedSymbol) |
| 180 |
self.assertEqual(len(identified), 2) |
| 181 |
else: |
| 182 |
self.assertIsInstance(identified[1], IdentifiedSymbol) |
| 183 |
self.assertGreaterEqual(len(identified), 2) |
| 184 |
|
| 185 |
@skip_unless_have_full_mm_support |
| 186 |
@skip_unless_have_test_kmod |
| 187 |
def test_identify_idle_task_1_stack(self): |
| 188 |
if self.prog["drgn_test_slob"] and self.prog["drgn_test_slab_stack_enabled"]: |
| 189 |
self.skipTest("test does not support SLOB") |
| 190 |
|
| 191 |
for cpu in online_cpus(): |
| 192 |
if cpu > 0: |
| 193 |
break |
| 194 |
else: |
| 195 |
self.skipTest("online CPU > 0 not found") |
| 196 |
|
| 197 |
task = idle_task(self.prog, cpu) |
| 198 |
|
| 199 |
for cached in (False, True): |
| 200 |
with self.subTest("cached" if cached else "uncached"): |
| 201 |
identified = list( |
| 202 |
identify_address_all(task.stack, cache={} if cached else None) |
| 203 |
) |
| 204 |
self.assertIsInstance(identified[0], IdentifiedTaskStack) |
| 205 |
self.assertEqual(identified[0].task, task) |
| 206 |
if self.prog["drgn_test_vmap_stack_enabled"]: |
| 207 |
self.assertIsInstance(identified[1], IdentifiedVmap) |
| 208 |
self.assertEqual(len(identified), 2) |
| 209 |
elif self.prog["drgn_test_slab_stack_enabled"]: |
| 210 |
self.assertIsInstance(identified[1], IdentifiedSlabObject) |
| 211 |
self.assertEqual(len(identified), 2) |
| 212 |
else: |
| 213 |
self.assertEqual(len(identified), 1) |
| 214 |
|
| 215 |
@skip_unless_have_test_kmod |
| 216 |
def test_identify_page(self): |
| 217 |
self.assertEqual( |
| 218 |
identify_address(self.prog["drgn_test_page"]), |
| 219 |
f"page: pfn {self.prog['drgn_test_pfn'].value_()}", |
| 220 |
) |
| 221 |
mapping_offset = offsetof(self.prog.type("struct page"), "mapping") |
| 222 |
self.assertEqual( |
| 223 |
identify_address(self.prog["drgn_test_page"].mapping.address_of_()), |
| 224 |
f"page: pfn {self.prog['drgn_test_pfn'].value_()} +{hex(mapping_offset)}", |
| 225 |
) |
| 226 |
|
| 227 |
@skip_unless_have_full_mm_support |
| 228 |
@skip_unless_have_test_kmod |
| 229 |
def test_identify_unrecognized(self): |
| 230 |
start_addr = (pfn_to_virt(self.prog["min_low_pfn"])).value_() |
| 231 |
# On s390x, the start address is 0, and identify_address() doesn't |
| 232 |
# allow a negative address. |
| 233 |
if start_addr > 0: |
| 234 |
self.assertIsNone(identify_address(self.prog, start_addr - 1)) |
| 235 |
self.assertIsNone(identify_address(self.prog, self.prog["drgn_test_va"])) |
| 236 |
|
| 237 |
|
| 238 |
class TestPrintAnnotatedMemory(LinuxKernelTestCase): |
| 239 |
@skip_unless_have_full_mm_support |
| 240 |
@skip_unless_have_test_kmod |
| 241 |
def test_print_annotated_memory(self): |
| 242 |
f = io.StringIO() |
| 243 |
with redirect_stdout(f): |
| 244 |
print_annotated_memory( |
| 245 |
self.prog, |
| 246 |
self.prog["drgn_test_small_slab_objects"].address_, |
| 247 |
sizeof(self.prog["drgn_test_small_slab_objects"]), |
| 248 |
) |
| 249 |
# For CONFIG_SLOB, we cannot find slab objects. However, |
| 250 |
# print_annotated_memory() should still function with no error. So we |
| 251 |
# don't skip the test here: just skip the assertion. |
| 252 |
if not self.prog["drgn_test_slob"]: |
| 253 |
self.assertIn("slab object: drgn_test_small+0x0", f.getvalue()) |
| 254 |
|
| 255 |
|
| 256 |
class TestPrintAnnotatedStack(LinuxKernelTestCase): |
| 257 |
@skip_unless_have_stack_tracing |
| 258 |
@skip_unless_have_test_kmod |
| 259 |
def test_print_annotated_stack(self): |
| 260 |
trace = self.prog.stack_trace(self.prog["drgn_test_kthread"]) |
| 261 |
|
| 262 |
f = io.StringIO() |
| 263 |
with redirect_stdout(f): |
| 264 |
print_annotated_stack(trace) |
| 265 |
|
| 266 |
printed_trace = f.getvalue() |
| 267 |
|
| 268 |
if HAVE_FULL_MM_SUPPORT and not self.prog["drgn_test_slob"]: |
| 269 |
self.assertIn("slab object: drgn_test_small", printed_trace) |
| 270 |
self.assertIn("[function symbol: schedule", printed_trace) |
| 271 |
self.assertIn("schedule at ", printed_trace) |
| 272 |
|
| 273 |
|
| 274 |
class TestPrintRegisters(LinuxKernelTestCase): |
| 275 |
@skip_unless_have_stack_tracing |
| 276 |
@skip_unless_have_test_kmod |
| 277 |
def test_print_registers(self): |
| 278 |
trace = self.prog.stack_trace(self.prog["drgn_test_kthread_pt_regs"]) |
| 279 |
# This is mostly a smoke test: we don't have any well-known registers, |
| 280 |
# and we don't have any specific guarantees about the availability of |
| 281 |
# registers. We can't even assert that all registers are present as hex |
| 282 |
# strings in the output, because some may be printed in decimal, and |
| 283 |
# others may be broken down into smaller fields. |
| 284 |
f = io.StringIO() |
| 285 |
with redirect_stdout(f): |
| 286 |
print_registers(self.prog, trace[0].registers()) |