Debian Codegraph @

tests/linux_kernel/crash_commands/test_sys.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import contextlib
5 import ctypes
6 import gzip
7 import mmap
8 import os
9 from pathlib import Path
10 import re
11 import unittest
12
13 from _drgn_util.platform import NORMALIZED_MACHINE_NAME
14 from drgn import Object, Symbol
15 from tests.linux_kernel import (
16 IOCB_CMD_PREAD,
17 IOCB_CMD_PWRITE,
18 io_destroy,
19 io_setup,
20 io_submit,
21 iocb,
22 possible_cpus,
23 skip_unless_have_test_kmod,
24 )
25 from tests.linux_kernel.crash_commands import CrashCommandTestCase
26 from tests.linux_kernel.helpers.test_irq import proc_irq_smp_affinity_list
27
28
29 class TestSys(CrashCommandTestCase):
30 def test_sys(self):
31 cmd = self.check_crash_command("sys")
32 for field in (
33 "KERNEL",
34 "DUMPFILE",
35 "CPUS",
36 "DATE",
37 "UPTIME",
38 "LOAD AVERAGE",
39 "TASKS",
40 "NODENAME",
41 "RELEASE",
42 "VERSION",
43 "MACHINE",
44 "MEMORY",
45 ):
46 self.assertRegex(cmd.stdout, rf"(?m)^\s*{field}:")
47
48 for variable in (
49 "kernel",
50 "taints",
51 "dumpfile",
52 "cpus",
53 "offline_cpus",
54 "timestamp",
55 "uptime_",
56 "load_average",
57 "num_tasks",
58 "nodename",
59 "release",
60 "version",
61 "machine",
62 "memory",
63 ):
64 self.assertIn(variable, cmd.drgn_option.globals)
65
66 def test_sys_config(self):
67 try:
68 with gzip.open("/proc/config.gz", "rt") as f:
69 expected = f.read()
70 except FileNotFoundError:
71 self.skipTest("kernel not built with CONFIG_IKCONFIG_PROC")
72
73 cmd = self.check_crash_command("sys config")
74 self.assertEqual(cmd.stdout, expected)
75 self.assertIn("kconfig", cmd.drgn_option.globals)
76
77
78 class TestDev(CrashCommandTestCase):
79 def test_no_args(self):
80 cmd = self.check_crash_command("dev")
81 with open("/proc/devices", "r") as f:
82 for line in f:
83 if line.isspace() or line in (
84 "Character devices:\n",
85 "Block devices:\n",
86 ):
87 continue
88 match = re.fullmatch(r"\s*([0-9]+)\s+(.*)\n", line)
89 self.assertRegex(
90 cmd.stdout, rf"(?m)^\s*{match.group(1)}\s+{match.group(2)}"
91 )
92
93 for variable in ("cdev", "operations", "disk"):
94 with self.subTest(variable=variable):
95 self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
96 self.assertIn("major", cmd.drgn_option.globals)
97
98 def test_ioports(self):
99 cmd = self.check_crash_command("dev -i")
100 lines = iter(cmd.stdout.splitlines())
101 # Skip the header and root resource.
102 next(lines)
103 next(lines)
104 with open("/proc/ioports", "r") as f:
105 for output_line, file_line in zip(lines, f):
106 output_match = re.fullmatch(
107 r"\s*[0-9a-f]+\s+([0-9a-f]+)-([0-9a-f]+)\s+(.*)", output_line
108 )
109 file_match = re.fullmatch(
110 r"\s*([0-9a-f]+)-([0-9a-f]+)\s*:\s*(.*)\n", file_line
111 )
112 self.assertEqual(
113 (
114 int(output_match.group(1), 16),
115 int(output_match.group(2), 16),
116 output_match.group(3),
117 ),
118 (
119 int(file_match.group(1), 16),
120 int(file_match.group(2), 16),
121 file_match.group(3),
122 ),
123 )
124
125 for variable in ("resource", "start", "end", "name"):
126 with self.subTest(variable=variable):
127 self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
128
129 def test_pci(self):
130 cmd = self.check_crash_command("dev -p")
131 for path in Path("/sys/bus/pci/devices").iterdir():
132 self.assertIn(path.name, cmd.stdout)
133
134 self.assertIsInstance(cmd.drgn_option.globals["root_bus"], Object)
135 self.assertIsInstance(cmd.drgn_option.globals["busname"], str)
136 # Everything else is a local variable in a recursive function.
137
138 @skip_unless_have_test_kmod
139 def test_disks(self):
140 cmd_d_while_idle = self.check_crash_command("dev -d", mode="capture")
141 cmd_D_while_idle = self.check_crash_command("dev -D", mode="capture")
142
143 with contextlib.ExitStack() as exit_stack:
144 fd = os.open("/dev/drgntestb0", os.O_RDWR | os.O_DIRECT)
145 exit_stack.callback(os.close, fd)
146
147 major = os.major(os.fstat(fd).st_rdev)
148
149 map = exit_stack.enter_context(
150 mmap.mmap(-1, 2 * mmap.PAGESIZE, mmap.MAP_PRIVATE)
151 )
152
153 iocbs = (iocb * 2)()
154 iocbs[0].aio_lio_opcode = IOCB_CMD_PWRITE
155 iocbs[0].aio_fildes = fd
156 iocbs[0].aio_buf = ctypes.addressof(ctypes.c_char.from_buffer(map))
157 iocbs[0].aio_nbytes = mmap.PAGESIZE
158 iocbs[0].aio_offset = mmap.PAGESIZE
159
160 iocbs[1].aio_lio_opcode = IOCB_CMD_PREAD
161 iocbs[1].aio_fildes = fd
162 iocbs[1].aio_buf = ctypes.addressof(
163 ctypes.c_char.from_buffer(map, mmap.PAGESIZE)
164 )
165 iocbs[1].aio_nbytes = mmap.PAGESIZE
166 iocbs[1].aio_offset = 3 * mmap.PAGESIZE
167
168 ctx_id = io_setup(len(iocbs))
169 exit_stack.callback(io_destroy, ctx_id)
170
171 truant = Path("/sys/block/drgntestb0/truant")
172 truant.write_text("1\n")
173 exit_stack.callback(truant.write_text, "0\n")
174
175 io_submit(ctx_id, iocbs)
176
177 cmd_d_while_busy = self.check_crash_command("dev -d")
178 cmd_D_while_busy = self.check_crash_command("dev -D", mode="capture")
179
180 self.assertIn("drgntestb0", cmd_d_while_idle.stdout)
181 self.assertNotIn("drgntestb0", cmd_D_while_idle.stdout)
182 for cmd in (cmd_d_while_busy, cmd_D_while_busy):
183 self.assertRegex(
184 cmd.stdout,
185 rf"(?m)^\s*{major}\s+[0-9a-f]+\s+drgntestb0\s+[0-9a-f]+\s+2\s+1\s+1\s*$",
186 )
187
188 for cmd in (cmd_d_while_idle, cmd_D_while_idle, cmd_D_while_busy):
189 self.assertEqual(
190 cmd.drgn_option.stdout, cmd_d_while_busy.drgn_option.stdout
191 )
192
193 cmd = cmd_d_while_busy
194 for variable in ("disk", "major", "queue"):
195 with self.subTest(variable=variable):
196 self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
197 self.assertIsInstance(cmd.drgn_option.globals["name"], bytes)
198 for variable in ("read", "write", "total"):
199 with self.subTest(variable=variable):
200 self.assertIsInstance(cmd.drgn_option.globals[variable], int)
201
202
203 def nr_irqs():
204 with open("/proc/stat", "r") as f:
205 for line in f:
206 if line.startswith("intr "):
207 return len(line.split()) - 2
208
209
210 def nr_softirqs():
211 with open("/proc/stat", "r") as f:
212 for line in f:
213 if line.startswith("softirq "):
214 return len(line.split()) - 2
215
216
217 class TestIrq(CrashCommandTestCase):
218 def _test_irq_common(self, cmd):
219 for path in Path("/sys/kernel/irq").iterdir():
220 expected = (path / "actions").read_text().rstrip("\n")
221 if expected:
222 irq = int(path.name)
223 break
224 else:
225 self.skipTest("IRQ with action not found")
226
227 action = expected.partition(",")[0]
228 self.assertRegex(cmd.stdout, rf'(?m)^\s*{irq}.*"{re.escape(action)}"')
229
230 self.assertIn("for_each_irq_desc()", cmd.drgn_option.stdout)
231 self.assertIsInstance(cmd.drgn_option.globals["irq"], int)
232 for variable in ("irq_desc", "action", "name"):
233 with self.subTest(variable=variable):
234 self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
235
236 def test_irq(self):
237 cmd = self.check_crash_command("irq")
238
239 irqs = [int(x) for x in re.findall(r"^\s*([0-9]+)", cmd.stdout, flags=re.M)]
240 self.assertEqual(irqs, list(range(nr_irqs())))
241
242 self._test_irq_common(cmd)
243
244 def test_used(self):
245 cmd = self.check_crash_command("irq -u")
246
247 irqs = [int(x) for x in re.findall(r"^\s*([0-9]+)", cmd.stdout, flags=re.M)]
248 expected = [int(path.name) for path in Path("/sys/kernel/irq").iterdir()]
249 expected.sort()
250 self.assertEqual(irqs, expected)
251
252 self._test_irq_common(cmd)
253
254 def test_number(self):
255 numbers = []
256 for path in Path("/sys/kernel/irq").iterdir():
257 numbers.append(int(path.name))
258 if len(numbers) >= 2:
259 break
260 numbers.reverse()
261
262 cmd = self.check_crash_command(f"irq {' '.join([str(x) for x in numbers])}")
263
264 irqs = [int(x) for x in re.findall(r"^\s*([0-9]+)", cmd.stdout, flags=re.M)]
265 self.assertEqual(irqs, numbers)
266
267 self.assertIn("irq_to_desc(", cmd.drgn_option.stdout)
268 self.assertIsInstance(cmd.drgn_option.globals["irq"], int)
269 # Omit "name" since the IRQs we chose may not have any actions with
270 # names.
271 for variable in ("irq_desc", "action"):
272 with self.subTest(variable=variable):
273 self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
274
275 def test_invalid_number(self):
276 number = nr_irqs()
277 # The drgn code would fault on a NULL irq_desc, so just compile it.
278 cmd = self.check_crash_command(f"irq {number}", mode="compile")
279 self.assertEqual(cmd.stdout, f"irq: invalid IRQ number: {number}\n")
280
281 @unittest.skipUnless(NORMALIZED_MACHINE_NAME == "x86_64", "machine is not x86_64")
282 def test_idt(self):
283 cmd = self.check_crash_command("irq -d")
284
285 vecs = [int(x) for x in re.findall(r"^\s*\[([0-9]+)\]", cmd.stdout, flags=re.M)]
286 self.assertEqual(vecs, list(range(256)))
287
288 self.assertRegex(cmd.stdout, r"(?m)^\s*\[3\] .*int3")
289
290 self.assertIsInstance(cmd.drgn_option.globals["vec"], int)
291 for variable in ("gate", "func"):
292 with self.subTest(variable=variable):
293 self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
294 self.assertIsInstance(cmd.drgn_option.globals["sym"], Symbol)
295
296 def test_softirqs(self):
297 cmd = self.check_crash_command("irq -b")
298
299 vecs = [int(x) for x in re.findall(r"^\s*\[([0-9]+)\]", cmd.stdout, flags=re.M)]
300 self.assertEqual(vecs, list(range(nr_softirqs())))
301
302 self.assertRegex(cmd.stdout, r"(?m)^\s*\[[0-9]+\] .*<\w+\>\s*$")
303
304 self.assertIsInstance(cmd.drgn_option.globals["vec"], int)
305 self.assertIsInstance(cmd.drgn_option.globals["action"], Object)
306 self.assertIsInstance(cmd.drgn_option.globals["sym"], Symbol)
307
308 def test_affinity(self):
309 cmd = self.check_crash_command("irq -a")
310
311 proc_path = Path("/proc/irq")
312 expected = [
313 (
314 int(path.name),
315 proc_irq_smp_affinity_list(proc_path / path.name).rstrip("\n"),
316 )
317 for path in Path("/sys/kernel/irq").iterdir()
318 if (path / "actions").read_text().rstrip("\n")
319 ]
320 expected.sort()
321 actual = [
322 (int(irq), affinity)
323 for irq, affinity in re.findall(
324 r"^\s*([0-9]+).*\s(\S+)\s*$", cmd.stdout, flags=re.M
325 )
326 ]
327 self.assertEqual(actual, expected)
328
329 self.assertIsInstance(cmd.drgn_option.globals["irq"], int)
330 for variable in ("irq_desc", "affinity"):
331 with self.subTest(variable=variable):
332 self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
333 self.assertIsInstance(cmd.drgn_option.globals["names"], list)
334 self.assertIsInstance(cmd.drgn_option.globals["affinity_list"], str)
335
336 def test_stats(self):
337 cmd = self.check_crash_command("irq -s")
338
339 expected = [
340 int(path.name)
341 for path in Path("/sys/kernel/irq").iterdir()
342 if (path / "actions").read_text().rstrip("\n")
343 ]
344 expected.sort()
345 num_possible_cpus = len(possible_cpus())
346 actual = [
347 int(irq)
348 for irq in re.findall(
349 rf"^\s*([0-9]+):(?:\s+[0-9]+){{{num_possible_cpus}}}",
350 cmd.stdout,
351 flags=re.M,
352 )
353 ]
354 self.assertEqual(actual, expected)
355
356 self.assertIsInstance(cmd.drgn_option.globals["irq"], int)
357 self.assertIsInstance(cmd.drgn_option.globals["irq_desc"], Object)
358 self.assertIsInstance(cmd.drgn_option.globals["names"], list)
359 self.assertIn("chip_name", cmd.drgn_option.globals)
360 self.assertIsInstance(cmd.drgn_option.globals["count"], int)
361
362 def test_stats_cpu(self):
363 cpu = max(possible_cpus())
364 cmd = self.check_crash_command(f"irq -s -c {cpu}")
365
366 expected = [
367 int(path.name)
368 for path in Path("/sys/kernel/irq").iterdir()
369 if (path / "actions").read_text().rstrip("\n")
370 ]
371 expected.sort()
372 actual = [
373 int(irq)
374 for irq in re.findall(r"^\s*([0-9]+):\s+[0-9]+", cmd.stdout, flags=re.M)
375 ]
376 self.assertEqual(actual, expected)
377
378 self.assertEqual(cmd.drgn_option.globals["cpu"], cpu)
379 self.assertIsInstance(cmd.drgn_option.globals["irq"], int)
380 self.assertIsInstance(cmd.drgn_option.globals["irq_desc"], Object)
381 self.assertIsInstance(cmd.drgn_option.globals["names"], list)
382 self.assertIn("chip_name", cmd.drgn_option.globals)
383 self.assertIsInstance(cmd.drgn_option.globals["count"], int)
384
385
386 class TestLog(CrashCommandTestCase):
387 def test_no_args(self):
388 cmd = self.check_crash_command("log")
389
390 self.assertRegex(cmd.stdout, r"^\[\s*[0-9]+\.[0-9]+\] .")
391
392 self.assertIn("get_dmesg()", cmd.drgn_option.stdout)
393 self.assertIsInstance(cmd.drgn_option.globals["dmesg"], bytes)
394
395 def test_t(self):
396 cmd = self.check_crash_command("log -t")
397
398 self.assertRegex(cmd.stdout, r"^[^[]")
399
400 self.assertIn("get_dmesg(timestamps=False)", cmd.drgn_option.stdout)
401 self.assertIsInstance(cmd.drgn_option.globals["dmesg"], bytes)
402
403 def test_T(self):
404 cmd = self.check_crash_command("log -T")
405
406 self.assertRegex(cmd.stdout, r"^\[[A-Z]")
407
408 self.assertIn('get_dmesg(timestamps="human")', cmd.drgn_option.stdout)
409 self.assertIsInstance(cmd.drgn_option.globals["dmesg"], bytes)