| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import os |
| 5 |
from pathlib import Path |
| 6 |
import tempfile |
| 7 |
|
| 8 |
from drgn import ( |
| 9 |
DebugInfoOptions, |
| 10 |
KmodSearchMethod, |
| 11 |
MainModule, |
| 12 |
Program, |
| 13 |
RelocatableModule, |
| 14 |
) |
| 15 |
from drgn.helpers.linux.module import find_module |
| 16 |
from tests import modifyenv |
| 17 |
from tests.linux_kernel import LinuxKernelTestCase, skip_unless_have_test_kmod |
| 18 |
|
| 19 |
|
| 20 |
def iter_proc_modules(): |
| 21 |
try: |
| 22 |
f = open("/proc/modules", "r") |
| 23 |
except FileNotFoundError: |
| 24 |
return |
| 25 |
with f: |
| 26 |
for line in f: |
| 27 |
tokens = line.split() |
| 28 |
yield tokens[0], int(tokens[5], 16) |
| 29 |
|
| 30 |
|
| 31 |
class TestDebugInfo(LinuxKernelTestCase): |
| 32 |
def test_debug_info(self): |
| 33 |
# This is actually two test cases squished into one to avoid indexing |
| 34 |
# vmlinux another time. |
| 35 |
prog = Program() |
| 36 |
prog.set_kernel() |
| 37 |
prog.set_enabled_debug_info_finders([]) |
| 38 |
|
| 39 |
with self.subTest("vmlinux_no_build_id"): |
| 40 |
for module, _ in prog.loaded_modules(): |
| 41 |
if isinstance(module, MainModule): |
| 42 |
module.build_id = None |
| 43 |
break |
| 44 |
else: |
| 45 |
self.fail("main module not found") |
| 46 |
prog.load_debug_info([self.prog.main_module().debug_file_path]) |
| 47 |
self.assertEqual( |
| 48 |
prog.main_module().debug_file_path, |
| 49 |
self.prog.main_module().debug_file_path, |
| 50 |
) |
| 51 |
|
| 52 |
with self.subTest("kmod_walk"), tempfile.TemporaryDirectory() as temp_dir: |
| 53 |
temp_dir = Path(temp_dir) |
| 54 |
found_modules = set() |
| 55 |
for i, module in enumerate(self.prog.modules()): |
| 56 |
if isinstance(module, RelocatableModule) and module.debug_file_path: |
| 57 |
found_modules.add(module.name) |
| 58 |
link = temp_dir / str(i) / (module.name + ".ko") |
| 59 |
link.parent.mkdir() |
| 60 |
link.symlink_to(module.debug_file_path) |
| 61 |
|
| 62 |
modules = [ |
| 63 |
module |
| 64 |
for module, _ in prog.loaded_modules() |
| 65 |
if module.name in found_modules |
| 66 |
] |
| 67 |
prog.find_standard_debug_info( |
| 68 |
modules, |
| 69 |
options=DebugInfoOptions( |
| 70 |
kernel_directories=(temp_dir,), try_kmod=KmodSearchMethod.WALK |
| 71 |
), |
| 72 |
) |
| 73 |
for module in modules: |
| 74 |
with self.subTest(module=module.name): |
| 75 |
self.assertIsNotNone(module.debug_file_path) |
| 76 |
|
| 77 |
|
| 78 |
class TestModule(LinuxKernelTestCase): |
| 79 |
def test_loaded_modules(self): |
| 80 |
expected = [("kernel", None), *iter_proc_modules()] |
| 81 |
|
| 82 |
loaded_modules = [] |
| 83 |
for module, _ in self.prog.loaded_modules(): |
| 84 |
if isinstance(module, RelocatableModule): |
| 85 |
loaded_modules.append((module.name, module.address)) |
| 86 |
else: |
| 87 |
loaded_modules.append((module.name, None)) |
| 88 |
|
| 89 |
self.assertCountEqual(loaded_modules, expected) |
| 90 |
|
| 91 |
@skip_unless_have_test_kmod |
| 92 |
def test_find(self): |
| 93 |
self.assertEqual(self.prog.main_module().name, "kernel") |
| 94 |
for name, address in iter_proc_modules(): |
| 95 |
if name == "drgn_test": |
| 96 |
self.assertEqual( |
| 97 |
self.prog.relocatable_module(name, address).name, "drgn_test" |
| 98 |
) |
| 99 |
break |
| 100 |
else: |
| 101 |
self.fail("test module not found") |
| 102 |
|
| 103 |
@skip_unless_have_test_kmod |
| 104 |
def test_find_by_obj(self): |
| 105 |
for module in self.prog.modules(): |
| 106 |
if module.name == "drgn_test": |
| 107 |
break |
| 108 |
else: |
| 109 |
self.fail("test module not found") |
| 110 |
|
| 111 |
module_obj = find_module(self.prog, "drgn_test") |
| 112 |
self.assertEqual(self.prog.linux_kernel_loadable_module(module_obj), module) |
| 113 |
self.assertEqual( |
| 114 |
self.prog.linux_kernel_loadable_module(module_obj, create=True), module |
| 115 |
) |
| 116 |
|
| 117 |
def test_no_sys_module(self): |
| 118 |
# Test that we get the same modules with and without using /sys/module. |
| 119 |
|
| 120 |
def module_dict(prog): |
| 121 |
return { |
| 122 |
(module.name, module.address): ( |
| 123 |
( |
| 124 |
None |
| 125 |
if module.address_ranges is None |
| 126 |
else sorted(module.address_ranges) |
| 127 |
), |
| 128 |
module.build_id, |
| 129 |
dict(module.section_addresses), |
| 130 |
) |
| 131 |
for module, _ in prog.loaded_modules() |
| 132 |
if isinstance(module, RelocatableModule) |
| 133 |
} |
| 134 |
|
| 135 |
use_sys_module = int(os.environ.get("DRGN_USE_SYS_MODULE", "1")) != 0 |
| 136 |
|
| 137 |
with modifyenv({"DRGN_USE_SYS_MODULE": str(int(not use_sys_module))}): |
| 138 |
prog = Program() |
| 139 |
prog.set_kernel() |
| 140 |
|
| 141 |
if use_sys_module: |
| 142 |
with_sys_module = module_dict(self.prog) |
| 143 |
without_sys_module = module_dict(prog) |
| 144 |
else: |
| 145 |
with_sys_module = module_dict(prog) |
| 146 |
without_sys_module = module_dict(self.prog) |
| 147 |
|
| 148 |
self.assertEqual(with_sys_module, without_sys_module) |