| 1 |
# Copyright (c) 2024 Oracle and/or its affiliates |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
import re |
| 4 |
import tempfile |
| 5 |
from unittest import TestCase |
| 6 |
|
| 7 |
from drgn import Symbol, SymbolBinding, SymbolKind |
| 8 |
from drgn.helpers.linux.kallsyms import ( |
| 9 |
_load_builtin_kallsyms, |
| 10 |
_load_proc_kallsyms, |
| 11 |
load_module_kallsyms, |
| 12 |
) |
| 13 |
from tests.linux_kernel import LinuxKernelTestCase, skip_unless_have_test_kmod |
| 14 |
|
| 15 |
|
| 16 |
def compare_local_symbols(self, finder, modules=False): |
| 17 |
expr = re.compile( |
| 18 |
r"(?P<address>[0-9a-f]+) (?P<kind>.) " r"(?P<name>[^\s]+)(\s+(?P<mod>\[\w+\]))?" |
| 19 |
) |
| 20 |
names = {} |
| 21 |
count = 0 |
| 22 |
with open("/proc/kallsyms") as f: |
| 23 |
for line in f: |
| 24 |
match = expr.fullmatch(line.strip()) |
| 25 |
self.assertIsNotNone(match, line) |
| 26 |
if match.group("mod") and not modules: |
| 27 |
break |
| 28 |
count += 1 |
| 29 |
name = match.group("name") |
| 30 |
addr = int(match.group("address"), 16) |
| 31 |
names.setdefault(name, []).append((addr, match.group("kind"), name)) |
| 32 |
|
| 33 |
for name, syms in names.items(): |
| 34 |
res = finder(None, name, None, False) |
| 35 |
expected_addrs = sorted(t[0] for t in syms) |
| 36 |
found_addrs = sorted(s.address for s in res) |
| 37 |
self.assertEqual(expected_addrs, found_addrs) |
| 38 |
|
| 39 |
all_res = finder(None, None, None, False) |
| 40 |
self.assertEqual(count, len(all_res)) |
| 41 |
|
| 42 |
|
| 43 |
KALLSYMS_DATA = b"""\ |
| 44 |
0000000000000000 u null |
| 45 |
0000000000000008 d local_data |
| 46 |
0000000000000010 B global_bss |
| 47 |
0000000000000020 v weak_symbol |
| 48 |
0000000000000040 ? unknown |
| 49 |
0000000000001000 T text [mymod] |
| 50 |
0000000000002000 T modfunc1 [mymod2] |
| 51 |
0000000000002010 T modfunc2 [mymod2] |
| 52 |
0000000000002008 T modfunc3 [mymod2] |
| 53 |
""" |
| 54 |
|
| 55 |
|
| 56 |
class TestProcKallsyms(TestCase): |
| 57 |
def test_static_data(self): |
| 58 |
with tempfile.NamedTemporaryFile() as f: |
| 59 |
f.write(KALLSYMS_DATA) |
| 60 |
f.flush() |
| 61 |
finder = _load_proc_kallsyms(filename=f.name) |
| 62 |
|
| 63 |
syms = finder(None, None, None, False) |
| 64 |
expected = [ |
| 65 |
Symbol("null", 0x0, 8, SymbolBinding.UNIQUE, SymbolKind.UNKNOWN), |
| 66 |
Symbol("local_data", 0x8, 8, SymbolBinding.UNKNOWN, SymbolKind.OBJECT), |
| 67 |
Symbol("global_bss", 0x10, 16, SymbolBinding.GLOBAL, SymbolKind.OBJECT), |
| 68 |
Symbol("weak_symbol", 0x20, 32, SymbolBinding.WEAK, SymbolKind.OBJECT), |
| 69 |
# this one has zero size since it is at the end of vmlinux |
| 70 |
Symbol("unknown", 0x40, 0, SymbolBinding.UNKNOWN, SymbolKind.UNKNOWN), |
| 71 |
] |
| 72 |
self.assertEqual(syms, expected) |
| 73 |
|
| 74 |
def test_static_data_with_modules(self): |
| 75 |
with tempfile.NamedTemporaryFile() as f: |
| 76 |
f.write(KALLSYMS_DATA) |
| 77 |
f.flush() |
| 78 |
finder = _load_proc_kallsyms(filename=f.name, modules=True) |
| 79 |
|
| 80 |
syms = finder(None, None, None, False) |
| 81 |
expected = [ |
| 82 |
Symbol("null", 0x0, 8, SymbolBinding.UNIQUE, SymbolKind.UNKNOWN), |
| 83 |
Symbol("local_data", 0x8, 8, SymbolBinding.UNKNOWN, SymbolKind.OBJECT), |
| 84 |
Symbol("global_bss", 0x10, 16, SymbolBinding.GLOBAL, SymbolKind.OBJECT), |
| 85 |
Symbol("weak_symbol", 0x20, 32, SymbolBinding.WEAK, SymbolKind.OBJECT), |
| 86 |
# this one has zero size since it is at the end of vmlinux |
| 87 |
Symbol("unknown", 0x40, 0, SymbolBinding.UNKNOWN, SymbolKind.UNKNOWN), |
| 88 |
# this one has zero size since it is at the end of a module |
| 89 |
Symbol("text", 0x1000, 0, SymbolBinding.GLOBAL, SymbolKind.FUNC), |
| 90 |
# this one has a non-zero size since it is within a module |
| 91 |
Symbol("modfunc1", 0x2000, 16, SymbolBinding.GLOBAL, SymbolKind.FUNC), |
| 92 |
# this one has a zero size since it is at the end of the file. It is |
| 93 |
# returned in sorted order by address despite kallsyms not |
| 94 |
# containing it in sorted order. |
| 95 |
Symbol("modfunc3", 0x2008, 0, SymbolBinding.GLOBAL, SymbolKind.FUNC), |
| 96 |
# this one has a zero size since it is followed by an out-of-order |
| 97 |
# symbol |
| 98 |
Symbol("modfunc2", 0x2010, 0, SymbolBinding.GLOBAL, SymbolKind.FUNC), |
| 99 |
] |
| 100 |
self.assertEqual(syms, expected) |
| 101 |
|
| 102 |
|
| 103 |
class TestBuiltinKallsyms(LinuxKernelTestCase): |
| 104 |
def test_builtin_kallsyms(self): |
| 105 |
if b"kallsyms_num_syms" not in self.prog["VMCOREINFO"].string_(): |
| 106 |
self.skipTest("VMCOREINFO is missing necessary symbols") |
| 107 |
finder = _load_builtin_kallsyms(self.prog) |
| 108 |
compare_local_symbols(self, finder) |
| 109 |
|
| 110 |
@skip_unless_have_test_kmod |
| 111 |
def test_module_kallsyms(self): |
| 112 |
finder = load_module_kallsyms(self.prog) |
| 113 |
test_data = finder(None, "drgn_test_empty_list", None, True)[0] |
| 114 |
self.assertEqual("drgn_test_empty_list", test_data.name) |
| 115 |
self.assertEqual(SymbolKind.OBJECT, test_data.kind) |
| 116 |
self.assertIn(test_data.binding, (SymbolBinding.GLOBAL, SymbolBinding.UNKNOWN)) |
| 117 |
size = self.prog.type("struct list_head").size |
| 118 |
self.assertEqual(size, test_data.size) |
| 119 |
address = self.prog.object("drgn_test_empty_list").address_ |
| 120 |
self.assertEqual(address, test_data.address) |