Debian Codegraph @

tests/linux_kernel/crash_commands/test_rd.py

raw
1 # Copyright (c) 2025, Oracle and/or its affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import ctypes
5 import math
6 import os
7 import tempfile
8
9 from drgn import Architecture, FaultError, PlatformFlags
10 from drgn.helpers.linux.pid import find_task
11 from tests.linux_kernel import (
12 skip_if_highmem,
13 skip_unless_have_full_mm_support,
14 skip_unless_have_test_kmod,
15 )
16 from tests.linux_kernel.crash_commands import CrashCommandTestCase
17
18
19 class TestRd(CrashCommandTestCase):
20 def setUp(self):
21 self.w = self.prog.address_size() * 2
22
23 def test_no_options(self):
24 address = self.prog["init_task"].address_
25 cmd = self.check_crash_command(f"rd {address:x}")
26 self.assertRegex(cmd.stdout, rf"^{address:{self.w}x}:")
27 self.assertRegex(cmd.stdout, r"[0-9a-f]{" + str(self.w) + "}")
28
29 def test_count(self):
30 address = self.prog["init_task"].address_
31 cmd = self.check_crash_command(f"rd -64 {address:x} 3")
32 lines = cmd.stdout.splitlines()
33 self.assertGreaterEqual(len(lines), 2)
34 self.assertRegex(lines[0], rf"^{address:{self.w}x}:")
35
36 def test_format_decimal(self):
37 address = self.prog["init_task"].address_
38 cmd = self.check_crash_command(f"rd -d {address:x}")
39 self.assertRegex(cmd.stdout, rf"^{address:{self.w}x}:")
40 # Should have decimal numbers
41 self.assertRegex(cmd.stdout, r"\b-?\d+\b")
42
43 def test_units(self):
44 data = b"Linux ve"
45 address = self.prog.symbol("linux_banner").address
46 if self.prog.platform.flags & PlatformFlags.IS_LITTLE_ENDIAN:
47 endian = "little"
48 else:
49 endian = "big"
50 for unit in (1, 2, 4, 8):
51 with self.subTest(unit=unit):
52 count = 8 // unit
53 cmd = self.check_crash_command(f"rd -{unit * 8} linux_banner {count}")
54 value_list = []
55 for i in range(count):
56 int_bytes = data[i * unit : (i + 1) * unit]
57 value_list.append(int.from_bytes(int_bytes, byteorder=endian))
58 values = " ".join(f"{v:0{unit * 2}x}" for v in value_list)
59 self.assertRegex(
60 cmd.stdout,
61 rf"^{address:{self.w}x}: {values} +Linux ve$",
62 )
63
64 def test_network_byte_order(self):
65 data = b"Linux ve"
66 address = self.prog.symbol("linux_banner").address
67 for unit in (2, 4, 8):
68 with self.subTest(unit=unit):
69 count = 8 // unit
70 cmd = self.check_crash_command(
71 f"rd -{unit * 8} -N linux_banner {count}"
72 )
73 value_list = []
74 for i in range(count):
75 int_bytes = data[i * unit : (i + 1) * unit]
76 value_list.append(int.from_bytes(int_bytes, byteorder="big"))
77 values = " ".join(f"{v:0{unit * 2}x}" for v in value_list)
78 self.assertRegex(
79 cmd.stdout,
80 rf"^{address:{self.w}x}: {values} +Linux ve$",
81 )
82
83 def test_ascii(self):
84 cmd = self.check_crash_command("rd -a linux_banner")
85 address = self.prog.symbol("linux_banner").address
86 self.assertRegex(cmd.stdout, rf"{address:{self.w}x}: Linux version")
87 lines = cmd.stdout.strip().split("\n")
88
89 # The ascii variant formats output over multiple lines, breaking at 79
90 # characters. The linux banner is good test data for this, because it
91 # has no special whitespace and it is generally rather long. While we
92 # don't necessarily care about the specific line break point, it is good
93 # to verify that we get roughly the expected amount of output, as a
94 # smoke test.
95 string = self.prog["linux_banner"].string_()
96 chars_per_line = 79 - self.w - 3
97 line_count = math.ceil(len(string) / chars_per_line)
98 self.assertEqual(len(lines), line_count)
99
100 def test_ascii_count(self):
101 cmd = self.check_crash_command("rd -a linux_banner 5")
102 address = self.prog.symbol("linux_banner").address
103 self.assertEqual(cmd.stdout.strip(), f"{address:x}: Linux")
104
105 def test_ascii_offset(self):
106 address = self.prog.symbol("linux_banner").address
107 cmd = self.check_crash_command("rd -o 4 -a linux_banner")
108 self.assertRegex(cmd.stdout, f"^{address + 4:{self.w}x}: x version.*")
109
110 def test_symbol(self):
111 address = self.prog["init_task"].address_
112 cmd = self.check_crash_command("rd init_task")
113 self.assertRegex(cmd.stdout, rf"^{address:{self.w}x}:")
114
115 def test_annotate_symbols(self):
116 address = self.prog["slab_caches"].prev.value_()
117 cmd = self.check_crash_command(f"rd -s {address:x} 2")
118 self.assertRegex(cmd.stdout, rf"^{address:{self.w}x}: slab_caches\+0")
119
120 @skip_unless_have_full_mm_support
121 @skip_unless_have_test_kmod
122 def test_annotate_slab(self):
123 address = self.prog.symbol("drgn_test_small_slab_objects").address
124 cmd = self.check_crash_command("rd -S drgn_test_small_slab_objects 2")
125 if self.prog["drgn_test_slob"]:
126 self.assertRegex(
127 cmd.stdout,
128 rf"^{address:{self.w}x}: \[unknown slab object\] +\[unknown slab object\]",
129 )
130 else:
131 self.assertRegex(
132 cmd.stdout,
133 rf"^{address:{self.w}x}: \[drgn_test_small\] +\[drgn_test_small\]",
134 )
135
136 @skip_unless_have_full_mm_support
137 @skip_unless_have_test_kmod
138 def test_annotate_slab_verbose(self):
139 address = self.prog.symbol("drgn_test_small_slab_objects").address
140 cmd = self.check_crash_command("rd -SS drgn_test_small_slab_objects 2")
141 if self.prog["drgn_test_slob"]:
142 self.assertRegex(
143 cmd.stdout,
144 rf"^{address:{self.w}x}: \[[0-9a-f]+:unknown slab object\] +\[[0-9a-f]+:unknown slab object\]",
145 )
146 else:
147 self.assertRegex(
148 cmd.stdout,
149 rf"^{address:{self.w}x}: \[[0-9a-f]+:drgn_test_small\] +\[[0-9a-f]+:drgn_test_small\]",
150 )
151
152 def test_reverse(self):
153 address = self.prog["init_task"].address_
154 cmd = self.check_crash_command(f"rd -R {address:x} 2")
155 unit = self.prog.address_size()
156 start_addr = address - unit
157 self.assertRegex(cmd.stdout, rf"^{start_addr:{self.w}x}: [0-9a-f]+ [0-9a-f]+")
158
159 def test_raw(self):
160 address = self.prog["init_task"].address_
161 with tempfile.NamedTemporaryFile(delete=False) as f:
162 temp_file = f.name
163 cmd = self.check_crash_command(f"rd -r {temp_file} {address:x} 16")
164 self.assertIn("16 bytes copied", cmd.stdout)
165 self.assertTrue(os.path.exists(temp_file))
166 with open(temp_file, "rb") as f:
167 data = f.read()
168 self.assertEqual(len(data), 16)
169
170 @skip_if_highmem
171 def test_ascii_user(self):
172 string = "hello, world!"
173 buf = ctypes.create_string_buffer(string.encode("ascii"))
174 address = ctypes.addressof(buf)
175 self.prog.config["crash_context"] = find_task(self.prog, os.getpid())
176
177 # On s390x, the user and kernel address spaces occupy the same range, so
178 # we cannot detect whether an address is a user address. Explicitly pass
179 # the hint here.
180 need_hint = self.prog.platform.arch == Architecture.S390X
181
182 expected_value = f"{address:{self.w}x}: hello world, I am a string!"
183
184 if not need_hint:
185 with self.subTest("without -u hint"):
186 cmd = self.check_crash_command(f"rd -a -o {self.w} 0x{address:x}")
187 self.assertEqual(cmd.stdout.strip(), expected_value)
188 self.assertIn("access_process_vm", cmd.drgn_option)
189 self.assertNotIn("prog.read", cmd.drgn_option)
190 with self.subTest("with -u hint"):
191 cmd = self.check_crash_command(f"rd -u -a -o {self.w} 0x{address:x}")
192 self.assertEqual(cmd.stdout.strip(), expected_value)
193 self.assertIn("access_process_vm", cmd.drgn_option)
194 self.assertNotIn("prog.read", cmd.drgn_option)
195
196 def check_failing_command(self, command, exc_type):
197 # Directly running the command should fail
198 with self.assertRaises(exc_type):
199 self.run_crash_command(command)
200
201 # The drgn option should generate valid code that compiles
202 self.run_crash_command_drgn_option(command, mode="compile")
203
204 # But the code should also fail with the same error as the command
205 with self.assertRaises(exc_type):
206 self.run_crash_command_drgn_option(command, mode="exec")
207
208 def test_not_existing_symbol(self):
209 self.check_failing_command("rd this_symbol_does_not_exist", LookupError)
210
211 @skip_unless_have_full_mm_support
212 def test_fault(self):
213 self.check_failing_command("rd -u 0", FaultError)