| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# Copyright (c) 2025, Kylin Software, Inc. and affiliates. |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
|
| 5 |
import mmap |
| 6 |
import os |
| 7 |
import re |
| 8 |
|
| 9 |
from drgn import Object |
| 10 |
from drgn.helpers.linux.cpumask import for_each_online_cpu |
| 11 |
from drgn.helpers.linux.mm import PFN_PHYS, TaskRss, phys_to_virt |
| 12 |
from drgn.helpers.linux.percpu import per_cpu_ptr |
| 13 |
from tests.linux_kernel import ( |
| 14 |
skip_if_highmem, |
| 15 |
skip_unless_have_full_mm_support, |
| 16 |
skip_unless_have_test_disk, |
| 17 |
skip_unless_have_test_kmod, |
| 18 |
) |
| 19 |
from tests.linux_kernel.crash_commands import CrashCommandTestCase |
| 20 |
from tests.linux_kernel.helpers.test_mm import MmTestCase |
| 21 |
from tests.linux_kernel.helpers.test_swap import tmp_swaps |
| 22 |
|
| 23 |
|
| 24 |
class TestBtop(CrashCommandTestCase): |
| 25 |
def test_single(self): |
| 26 |
addr = mmap.PAGESIZE * 2 |
| 27 |
cmd = self.check_crash_command(f"btop {addr:x}") |
| 28 |
self.assertEqual(cmd.stdout, f"{addr:x}: 2\n") |
| 29 |
self.assertEqual(cmd.drgn_option.globals["phys_addr"], addr) |
| 30 |
self.assertEqual(cmd.drgn_option.globals["pfn"], 2) |
| 31 |
|
| 32 |
def test_multiple(self): |
| 33 |
addr1 = mmap.PAGESIZE * 2 |
| 34 |
addr2 = mmap.PAGESIZE * 10 |
| 35 |
cmd = self.check_crash_command(f"btop {addr1:x} {addr2:x}") |
| 36 |
self.assertEqual(cmd.stdout, f"{addr1:x}: 2\n{addr2:x}: a\n") |
| 37 |
self.assertRegex(cmd.drgn_option.stdout, r"\bfor\b.*\bin\b") |
| 38 |
self.assertEqual(cmd.drgn_option.globals["phys_addr"], addr2) |
| 39 |
self.assertEqual(cmd.drgn_option.globals["pfn"], 10) |
| 40 |
|
| 41 |
|
| 42 |
class TestPtob(CrashCommandTestCase): |
| 43 |
def test_single(self): |
| 44 |
addr = mmap.PAGESIZE * 2 |
| 45 |
cmd = self.check_crash_command("ptob 2") |
| 46 |
self.assertEqual(cmd.stdout, f"2: {addr:x}\n") |
| 47 |
self.assertEqual(cmd.drgn_option.globals["pfn"], 2) |
| 48 |
self.assertEqual(cmd.drgn_option.globals["phys_addr"], addr) |
| 49 |
|
| 50 |
def test_multiple(self): |
| 51 |
addr1 = mmap.PAGESIZE * 2 |
| 52 |
addr2 = mmap.PAGESIZE * 10 |
| 53 |
cmd = self.check_crash_command("ptob 2 10") |
| 54 |
self.assertEqual(cmd.stdout, f"2: {addr1:x}\na: {addr2:x}\n") |
| 55 |
self.assertRegex(cmd.drgn_option.stdout, r"\bfor\b.*\bin\b") |
| 56 |
self.assertEqual(cmd.drgn_option.globals["pfn"], 10) |
| 57 |
self.assertEqual(cmd.drgn_option.globals["phys_addr"], addr2) |
| 58 |
|
| 59 |
|
| 60 |
class TestPtov(CrashCommandTestCase): |
| 61 |
@skip_unless_have_full_mm_support |
| 62 |
def test_phy_to_virt(self): |
| 63 |
"""Test physical address to virtual address conversion.""" |
| 64 |
phys_addr = 0x123 |
| 65 |
virt_addr = phys_to_virt(self.prog, phys_addr) |
| 66 |
virt_addr_int = virt_addr.value_() |
| 67 |
|
| 68 |
cmd = self.check_crash_command(f"ptov {hex(phys_addr)}") |
| 69 |
self.assertRegex(cmd.stdout, r"(?m)^\s*VIRTUAL\s+PHYSICAL") |
| 70 |
self.assertRegex(cmd.stdout, rf"(?m)^\s*{virt_addr_int:016x}\s+{phys_addr:x}") |
| 71 |
|
| 72 |
def test_per_cpu_offset_single_cpu(self): |
| 73 |
"""Test per-CPU offset conversion for a single CPU.""" |
| 74 |
offset = 0x100 |
| 75 |
cpu = 0 |
| 76 |
ptr = Object(self.prog, "unsigned long", offset) |
| 77 |
virt_ptr = per_cpu_ptr(ptr, cpu) |
| 78 |
virt_int = virt_ptr.value_() |
| 79 |
|
| 80 |
cmd = self.check_crash_command(f"ptov {hex(offset)}:{cpu}") |
| 81 |
self.assertRegex(cmd.stdout, rf"(?m)^\s*PER-CPU OFFSET:\s+{offset:x}") |
| 82 |
self.assertRegex(cmd.stdout, r"(?m)^\s*CPU\s+VIRTUAL") |
| 83 |
self.assertRegex(cmd.stdout, rf"(?m)^\s*\[{cpu}\]\s+{virt_int:016x}") |
| 84 |
|
| 85 |
def test_per_cpu_offset_all_cpus(self): |
| 86 |
"""Test per-CPU offset conversion for all CPUs.""" |
| 87 |
offset = 0x200 |
| 88 |
cmd = self.check_crash_command(f"ptov {hex(offset)}:a") |
| 89 |
|
| 90 |
self.assertRegex(cmd.stdout, rf"(?m)^\s*PER-CPU OFFSET:\s+{offset:x}") |
| 91 |
self.assertRegex(cmd.stdout, r"(?m)^\s*CPU\s+VIRTUAL") |
| 92 |
|
| 93 |
ptr = Object(self.prog, "unsigned long", offset) |
| 94 |
for cpu in for_each_online_cpu(self.prog): |
| 95 |
virt = per_cpu_ptr(ptr, cpu) |
| 96 |
self.assertRegex(cmd.stdout, rf"(?m)^\s*\[{cpu}\]\s+{virt.value_():016x}") |
| 97 |
|
| 98 |
def test_per_cpu_offset_cpu_list(self): |
| 99 |
"""Test per-CPU offset conversion for a CPU list.""" |
| 100 |
offset = 0x300 |
| 101 |
cpus = sorted(os.sched_getaffinity(0)) |
| 102 |
cmd = self.check_crash_command(f"ptov {hex(offset)}:{','.join(map(str, cpus))}") |
| 103 |
|
| 104 |
self.assertRegex(cmd.stdout, rf"(?m)^\s*PER-CPU OFFSET:\s+{offset:x}") |
| 105 |
self.assertRegex(cmd.stdout, r"(?m)^\s*CPU\s+VIRTUAL") |
| 106 |
|
| 107 |
ptr = Object(self.prog, "unsigned long", offset) |
| 108 |
for cpu in cpus: |
| 109 |
virt = per_cpu_ptr(ptr, cpu) |
| 110 |
self.assertRegex(cmd.stdout, rf"(?m)^\s*\[{cpu}\]\s+{virt.value_():016x}") |
| 111 |
|
| 112 |
def test_invalid_address(self): |
| 113 |
"""Test invalid physical address input.""" |
| 114 |
with self.assertRaises(Exception) as cm: |
| 115 |
self.check_crash_command("ptov invalid_address") |
| 116 |
msg = str(cm.exception).lower() |
| 117 |
self.assertTrue( |
| 118 |
"invalid literal" in msg or "base 16" in msg, |
| 119 |
f"Unexpected error message: {msg}", |
| 120 |
) |
| 121 |
|
| 122 |
def test_invalid_cpu_spec(self): |
| 123 |
"""Test invalid per-CPU specifier.""" |
| 124 |
offset = 0x400 |
| 125 |
with self.assertRaises(Exception) as cm: |
| 126 |
self.check_crash_command(f"ptov {hex(offset)}:invalid") |
| 127 |
msg = str(cm.exception).lower() |
| 128 |
self.assertIn("invalid cpuspec", msg, f"Unexpected error message: {msg}") |
| 129 |
|
| 130 |
|
| 131 |
@skip_unless_have_full_mm_support |
| 132 |
class TestVtop(CrashCommandTestCase, MmTestCase): |
| 133 |
@skip_unless_have_test_kmod |
| 134 |
def test_kernel(self): |
| 135 |
virt_addr = self.prog["drgn_test_va"].value_() |
| 136 |
phys_addr = self.prog["drgn_test_pa"].value_() |
| 137 |
|
| 138 |
cmd = self.check_crash_command(f"vtop -k {virt_addr:x}") |
| 139 |
self.assertIn(f"{phys_addr:x}", cmd.stdout) |
| 140 |
|
| 141 |
self.assertIn("follow_phys", cmd.drgn_option.stdout) |
| 142 |
self.assertEqual(cmd.drgn_option.globals["virtual_address"], virt_addr) |
| 143 |
self.assertEqual(cmd.drgn_option.globals["physical_address"], phys_addr) |
| 144 |
|
| 145 |
@skip_unless_have_test_kmod |
| 146 |
def test_kernel_multiple(self): |
| 147 |
virt_addrs = [ |
| 148 |
self.prog["drgn_test_va"].value_(), |
| 149 |
self.prog["drgn_test_vmalloc_va"].value_(), |
| 150 |
] |
| 151 |
phys_addrs = [ |
| 152 |
self.prog["drgn_test_pa"].value_(), |
| 153 |
self.prog["drgn_test_vmalloc_pa"].value_(), |
| 154 |
] |
| 155 |
|
| 156 |
cmd = self.check_crash_command(f"vtop -k {virt_addrs[0]:x} {virt_addrs[1]:x}") |
| 157 |
for phys_addr in phys_addrs: |
| 158 |
self.assertIn(f"{phys_addr:x}", cmd.stdout) |
| 159 |
|
| 160 |
self.assertIn("follow_phys", cmd.drgn_option.stdout) |
| 161 |
for virt_addr in virt_addrs: |
| 162 |
self.assertIn(hex(virt_addr), cmd.drgn_option.stdout) |
| 163 |
self.assertIn(cmd.drgn_option.globals["virtual_address"], virt_addrs) |
| 164 |
self.assertIn(cmd.drgn_option.globals["physical_address"], phys_addrs) |
| 165 |
|
| 166 |
@skip_if_highmem |
| 167 |
def test_user(self): |
| 168 |
with self.map_pages() as (_, address, pfns): |
| 169 |
phys_addr = PFN_PHYS(self.prog, pfns[0]).value_() |
| 170 |
cmd = self.check_crash_command(f"vtop -u -c {os.getpid()} {address:x}") |
| 171 |
self.assertIn(f"{phys_addr:x}", cmd.stdout) |
| 172 |
|
| 173 |
self.assertIn("follow_phys", cmd.drgn_option.stdout) |
| 174 |
self.assertEqual(cmd.drgn_option.globals["virtual_address"], address) |
| 175 |
self.assertEqual(cmd.drgn_option.globals["physical_address"], phys_addr) |
| 176 |
|
| 177 |
@skip_if_highmem |
| 178 |
def test_user_multiple(self): |
| 179 |
with self.map_pages() as (_, address, pfns): |
| 180 |
virt_addrs = [address, address + mmap.PAGESIZE] |
| 181 |
phys_addrs = [PFN_PHYS(self.prog, pfns[i]).value_() for i in range(2)] |
| 182 |
cmd = self.check_crash_command( |
| 183 |
f"vtop -u -c {os.getpid()} {virt_addrs[0]:x} {virt_addrs[1]:x}" |
| 184 |
) |
| 185 |
for phys_addr in phys_addrs: |
| 186 |
self.assertIn(f"{phys_addr:x}", cmd.stdout) |
| 187 |
|
| 188 |
self.assertIn("follow_phys", cmd.drgn_option.stdout) |
| 189 |
for virt_addr in virt_addrs: |
| 190 |
self.assertIn(hex(virt_addr), cmd.drgn_option.stdout) |
| 191 |
self.assertIn(cmd.drgn_option.globals["virtual_address"], virt_addrs) |
| 192 |
self.assertIn(cmd.drgn_option.globals["physical_address"], phys_addrs) |
| 193 |
|
| 194 |
@skip_unless_have_test_kmod |
| 195 |
def test_guess(self): |
| 196 |
virt_addr = self.prog["drgn_test_va"].value_() |
| 197 |
phys_addr = self.prog["drgn_test_pa"].value_() |
| 198 |
|
| 199 |
cmd = self.check_crash_command(f"vtop {virt_addr:x}") |
| 200 |
self.assertTrue( |
| 201 |
f"{phys_addr:x}" in cmd.stdout or "ambiguous address" in cmd.stderr |
| 202 |
) |
| 203 |
|
| 204 |
self.assertIn("follow_phys(init_mm", cmd.drgn_option.stdout) |
| 205 |
self.assertIn("follow_phys(mm", cmd.drgn_option.stdout) |
| 206 |
self.assertEqual(cmd.drgn_option.globals["virtual_address"], virt_addr) |
| 207 |
|
| 208 |
|
| 209 |
class TestSwap(CrashCommandTestCase): |
| 210 |
@skip_unless_have_test_disk |
| 211 |
def test_swap(self): |
| 212 |
with tmp_swaps() as swaps: |
| 213 |
cmd = self.check_crash_command("swap") |
| 214 |
for path, is_file in swaps: |
| 215 |
type = "FILE" if is_file else "PARTITION" |
| 216 |
self.assertRegex(cmd.stdout, f"{type} .* {re.escape(str(path))}") |
| 217 |
self.assertEqual( |
| 218 |
cmd.drgn_option.globals["si"].type_.type_name(), "struct swap_info_struct *" |
| 219 |
) |
| 220 |
self.assertIn("pages", cmd.drgn_option.globals) |
| 221 |
self.assertIn("used_pages", cmd.drgn_option.globals) |
| 222 |
self.assertIn("priority", cmd.drgn_option.globals) |
| 223 |
self.assertIsInstance(cmd.drgn_option.globals["path"], bytes) |
| 224 |
|
| 225 |
|
| 226 |
class TestVm(CrashCommandTestCase): |
| 227 |
def _test_common(self, cmd): |
| 228 |
self.assertRegex( |
| 229 |
cmd.stdout, |
| 230 |
r"\bMM\s+PGD\s+RSS\s+TOTAL_VM(?:\s+[0-9a-f]+){2}(?:\s+[0-9]+k){2}\b", |
| 231 |
) |
| 232 |
self.assertRegex( |
| 233 |
cmd.stdout, r"\bVMA\s+START\s+END\s+FLAGS\s+FILE(?:\s+[0-9a-f]+){4}" |
| 234 |
) |
| 235 |
self.assertRegex(cmd.stdout, r"\bVMA(?s:.)*/") # Check for any file path. |
| 236 |
|
| 237 |
def _test_drgn_option_common(self, cmd): |
| 238 |
for variable in ( |
| 239 |
"pid", |
| 240 |
"task", |
| 241 |
"command", |
| 242 |
"mm", |
| 243 |
"pgd", |
| 244 |
"vma", |
| 245 |
"start", |
| 246 |
"end", |
| 247 |
"flags", |
| 248 |
): |
| 249 |
with self.subTest(variable=variable): |
| 250 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 251 |
self.assertIsInstance(cmd.drgn_option.globals["cpu"], int) |
| 252 |
self.assertIsInstance(cmd.drgn_option.globals["rss"], TaskRss) |
| 253 |
self.assertIsInstance(cmd.drgn_option.globals["file"], bytes) |
| 254 |
|
| 255 |
def test_no_args(self): |
| 256 |
self.run_crash_command("set -p") |
| 257 |
|
| 258 |
cmd = self.check_crash_command("vm") |
| 259 |
self.assertIn(f"PID: {os.getpid()}", cmd.stdout) |
| 260 |
self._test_common(cmd) |
| 261 |
|
| 262 |
self._test_drgn_option_common(cmd) |
| 263 |
|
| 264 |
def test_tasks(self): |
| 265 |
cmd = self.check_crash_command(f"vm 1 {os.getpid()}") |
| 266 |
foreach_cmd = self.check_crash_command( |
| 267 |
f"foreach 1 {os.getpid()} vm", mode="capture" |
| 268 |
) |
| 269 |
|
| 270 |
for c in (cmd, foreach_cmd): |
| 271 |
self.assertIn("PID: 1", c.stdout) |
| 272 |
self.assertIn(f"PID: {os.getpid()}", c.stdout) |
| 273 |
self._test_common(c) |
| 274 |
|
| 275 |
self._test_drgn_option_common(cmd) |
| 276 |
|
| 277 |
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout) |
| 278 |
|
| 279 |
def test_kernel_thread(self): |
| 280 |
cmd = self.check_crash_command("vm 2") |
| 281 |
|
| 282 |
self.assertRegex(cmd.stdout, r"\bMM\s+PGD\s+RSS\s+TOTAL_VM\s+0\s+0\s+0k\s+0k\b") |
| 283 |
self.assertNotIn("VMA", cmd.stdout) |
| 284 |
|
| 285 |
self.assertFalse(cmd.drgn_option.globals["mm"]) |