| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# Copyright (c) 2026, Oracle and/or its affiliates. |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
|
| 5 |
|
| 6 |
import os |
| 7 |
from pathlib import Path |
| 8 |
import re |
| 9 |
import signal |
| 10 |
|
| 11 |
from drgn import Object |
| 12 |
from drgn.commands import CommandArgumentError |
| 13 |
from tests.linux_kernel import fork_and_stop, skip_unless_have_test_kmod |
| 14 |
from tests.linux_kernel.crash_commands import CrashCommandTestCase |
| 15 |
|
| 16 |
|
| 17 |
class TestRunq(CrashCommandTestCase): |
| 18 |
def test_no_args(self): |
| 19 |
cmd = self.check_crash_command("runq") |
| 20 |
self.assertRegex(cmd.stdout, r"CPU \d+ RUNQUEUE [0-9a-f]+") |
| 21 |
self.assertIn("CURRENT:", cmd.stdout) |
| 22 |
self.assertIn("RT PRIO_ARRAY:", cmd.stdout) |
| 23 |
self.assertIn("CFS RB_ROOT:", cmd.stdout) |
| 24 |
self.assertIn("PID:", cmd.stdout) |
| 25 |
|
| 26 |
self.assertIn("cpu_rq(", cmd.drgn_option.stdout) |
| 27 |
self.assertIn("rq_for_each_rt_task(", cmd.drgn_option.stdout) |
| 28 |
self.assertIn("cfs_rq_for_each_entity(", cmd.drgn_option.stdout) |
| 29 |
self.assertIsInstance(cmd.drgn_option.globals["curr"], Object) |
| 30 |
|
| 31 |
def test_timestamps(self): |
| 32 |
cmd = self.check_crash_command("runq -t") |
| 33 |
self.assertRegex(cmd.stdout, r"CPU \d+: \d{16}") |
| 34 |
self.assertIn("PID:", cmd.stdout) |
| 35 |
|
| 36 |
self.assertIn("rq.clock", cmd.drgn_option.stdout) |
| 37 |
self.assertIn("sched_info.last_arrival", cmd.drgn_option.stdout) |
| 38 |
self.assertIsInstance(cmd.drgn_option.globals["rq_clock"], Object) |
| 39 |
self.assertIsInstance(cmd.drgn_option.globals["curr"], Object) |
| 40 |
self.assertIsInstance(cmd.drgn_option.globals["curr_clock"], Object) |
| 41 |
|
| 42 |
def test_lag(self): |
| 43 |
cmd = self.check_crash_command("runq -T") |
| 44 |
self.assertRegex(cmd.stdout, r"CPU \d+: \d+\.\d+ secs") |
| 45 |
|
| 46 |
self.assertIsInstance(cmd.drgn_option.globals["rq_clock"], Object) |
| 47 |
|
| 48 |
def test_elapsed(self): |
| 49 |
cmd = self.check_crash_command("runq -m") |
| 50 |
self.assertRegex(cmd.stdout, r"CPU \d+: \[\s*\d+ \d{2}:\d{2}:\d{2}\.\d{3}\]") |
| 51 |
self.assertIn("PID:", cmd.stdout) |
| 52 |
|
| 53 |
self.assertIn("rq.clock", cmd.drgn_option.stdout) |
| 54 |
self.assertIn("sched_info.last_arrival", cmd.drgn_option.stdout) |
| 55 |
self.assertIsInstance(cmd.drgn_option.globals["rq_clock"], Object) |
| 56 |
self.assertIsInstance(cmd.drgn_option.globals["curr"], Object) |
| 57 |
self.assertIsInstance(cmd.drgn_option.globals["curr_clock"], Object) |
| 58 |
|
| 59 |
def test_task_groups(self): |
| 60 |
cmd = self.check_crash_command("runq -g") |
| 61 |
self.assertRegex(cmd.stdout, r"CPU \d+") |
| 62 |
self.assertIn("CURRENT:", cmd.stdout) |
| 63 |
self.assertIn("ROOT_TASK_GROUP:", cmd.stdout) |
| 64 |
self.assertIn("CFS_RQ:", cmd.stdout) |
| 65 |
self.assertIn("PID:", cmd.stdout) |
| 66 |
|
| 67 |
self.assertIn("cfs_rq_for_each_entity(", cmd.drgn_option.stdout) |
| 68 |
self.assertIn("task_group_name(", cmd.drgn_option.stdout) |
| 69 |
|
| 70 |
def test_cpu(self): |
| 71 |
cpu = max(os.sched_getaffinity(0)) |
| 72 |
cmd = self.check_crash_command(f"runq -c {cpu}") |
| 73 |
self.assertIn(f"CPU {cpu}", cmd.stdout) |
| 74 |
self.assertEqual(cmd.stdout.count("CURRENT:"), 1) |
| 75 |
|
| 76 |
|
| 77 |
class TestSig(CrashCommandTestCase): |
| 78 |
def _test_sig_drgn_option_common(self, cmd): |
| 79 |
for variable in ( |
| 80 |
"pid", |
| 81 |
"command", |
| 82 |
"signal_struct", |
| 83 |
"nr_threads", |
| 84 |
"sigaction", |
| 85 |
"handler", |
| 86 |
"mask", |
| 87 |
"flags", |
| 88 |
"blocked", |
| 89 |
"private_pending", |
| 90 |
"private_pending_signals", |
| 91 |
"shared_pending", |
| 92 |
"shared_pending_signals", |
| 93 |
): |
| 94 |
with self.subTest(variable=variable): |
| 95 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 96 |
|
| 97 |
for variable in ("cpu", "signo"): |
| 98 |
with self.subTest(variable=variable): |
| 99 |
self.assertIsInstance(cmd.drgn_option.globals[variable], int) |
| 100 |
|
| 101 |
self.assertIsInstance(cmd.drgn_option.globals["decoded_flags"], str) |
| 102 |
|
| 103 |
def test_sig(self): |
| 104 |
with fork_and_stop( |
| 105 |
signal.pthread_sigmask, signal.SIG_BLOCK, {signal.SIGUSR1, signal.SIGUSR2} |
| 106 |
) as (pid, _): |
| 107 |
os.kill(pid, signal.SIGUSR1) |
| 108 |
os.kill(pid, signal.SIGUSR2) |
| 109 |
|
| 110 |
sig_status = dict( |
| 111 |
re.findall( |
| 112 |
r"^(SigBlk|SigPnd|ShdPnd):\s*([0-9a-f]+)", |
| 113 |
Path(f"/proc/{pid}/status").read_text(), |
| 114 |
flags=re.M, |
| 115 |
) |
| 116 |
) |
| 117 |
|
| 118 |
cmd = self.check_crash_command(f"sig {pid}") |
| 119 |
foreach_cmd = self.check_crash_command(f"foreach {pid} sig", mode="capture") |
| 120 |
|
| 121 |
for c in (cmd, foreach_cmd): |
| 122 |
self.assertIn("PID:", c.stdout) |
| 123 |
self.assertIn("SIGNAL_STRUCT:", c.stdout) |
| 124 |
self.assertIn("NR_THREADS:", c.stdout) |
| 125 |
|
| 126 |
self.assertEqual( |
| 127 |
re.search(r"^\s*BLOCKED: ([0-9a-f]+)$", c.stdout, flags=re.M).group(1), |
| 128 |
sig_status["SigBlk"], |
| 129 |
) |
| 130 |
|
| 131 |
for crash_name, proc_name in ( |
| 132 |
("PRIVATE_PENDING", "SigPnd"), |
| 133 |
("SHARED_PENDING", "ShdPnd"), |
| 134 |
): |
| 135 |
with self.subTest(set=crash_name): |
| 136 |
self.assertEqual( |
| 137 |
re.search( |
| 138 |
rf"^{crash_name}\n\s*SIGNAL: ([0-9a-f]+)$", |
| 139 |
c.stdout, |
| 140 |
flags=re.M, |
| 141 |
).group(1), |
| 142 |
sig_status[proc_name], |
| 143 |
) |
| 144 |
sigqueue = re.search( |
| 145 |
rf"^{crash_name}\n.*\n\s*SIGQUEUE: (.*)", c.stdout, flags=re.M |
| 146 |
).group(1) |
| 147 |
if sig_status[proc_name].replace("0", ""): |
| 148 |
self.assertRegex(sigqueue, r"^\s*SIG\s+SIGINFO\s*$") |
| 149 |
else: |
| 150 |
self.assertEqual(sigqueue, "(empty)") |
| 151 |
|
| 152 |
self._test_sig_drgn_option_common(cmd) |
| 153 |
for variable in ("sigqueue", "info", "pending_signo"): |
| 154 |
with self.subTest(variable=variable): |
| 155 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 156 |
|
| 157 |
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout) |
| 158 |
|
| 159 |
def test_thread_group(self): |
| 160 |
cmd = self.check_crash_command(f"sig -g {os.getpid()}") |
| 161 |
foreach_cmd = self.check_crash_command( |
| 162 |
f"foreach {os.getpid()} sig -g", mode="capture" |
| 163 |
) |
| 164 |
|
| 165 |
for c in (cmd, foreach_cmd): |
| 166 |
self.assertEqual( |
| 167 |
len(re.findall(r"^\s*PID:", c.stdout, flags=re.M)), |
| 168 |
len(os.listdir("/proc/self/task")) + 1, |
| 169 |
) |
| 170 |
|
| 171 |
self.assertIn("for_each_task_in_group(", cmd.drgn_option.stdout) |
| 172 |
self._test_sig_drgn_option_common(cmd) |
| 173 |
# We may not have any pending signals, so don't check for those variables. |
| 174 |
|
| 175 |
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout) |
| 176 |
|
| 177 |
def test_list(self): |
| 178 |
cmd = self.check_crash_command("sig -l") |
| 179 |
self.assertRegex(cmd.stdout, r"(?m)^\s*\[9\] SIGKILL$") |
| 180 |
|
| 181 |
self.assertIn("signal_numbers(", cmd.drgn_option.stdout) |
| 182 |
self.assertIsInstance(cmd.drgn_option.globals["number"], int) |
| 183 |
self.assertIsInstance(cmd.drgn_option.globals["names"], list) |
| 184 |
self.assertIsInstance(cmd.drgn_option.globals["names"][0], str) |
| 185 |
|
| 186 |
def test_sigset(self): |
| 187 |
value = (1 << (signal.SIGHUP - 1)) | (1 << (signal.SIGTERM - 1)) |
| 188 |
cmd = self.check_crash_command(f"sig -s {value:x}") |
| 189 |
self.assertEqual(cmd.stdout, "SIGHUP SIGTERM\n") |
| 190 |
self.assertIn("decode_sigset(0x", cmd.drgn_option.stdout) |
| 191 |
self.assertEqual(cmd.drgn_option.globals["decoded"], "{SIGHUP,SIGTERM}") |
| 192 |
|
| 193 |
|
| 194 |
class TestWaitq(CrashCommandTestCase): |
| 195 |
@skip_unless_have_test_kmod |
| 196 |
def test_empty(self): |
| 197 |
cmd = self.check_crash_command("waitq drgn_test_empty_waitq") |
| 198 |
self.assertIn("is empty", cmd.stdout) |
| 199 |
self.assertIn("waitqueue_for_each_task(", cmd.drgn_option.stdout) |
| 200 |
|
| 201 |
@skip_unless_have_test_kmod |
| 202 |
def test_non_empty(self): |
| 203 |
cmd = self.check_crash_command("waitq drgn_test_waitq") |
| 204 |
self.assertIn('COMMAND: "drgn_test_', cmd.stdout) |
| 205 |
self.assertIn("waitqueue_for_each_task(", cmd.drgn_option.stdout) |
| 206 |
for variable in ( |
| 207 |
"task", |
| 208 |
"pid", |
| 209 |
"command", |
| 210 |
): |
| 211 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 212 |
self.assertIsInstance(cmd.drgn_option.globals["cpu"], int) |
| 213 |
|
| 214 |
@skip_unless_have_test_kmod |
| 215 |
def test_empty_symbol(self): |
| 216 |
address = self.prog.symbol("drgn_test_empty_waitq").address |
| 217 |
cmd = self.check_crash_command(f"waitq {address:x}") |
| 218 |
self.assertIn("is empty", cmd.stdout) |
| 219 |
self.assertIn("waitqueue_for_each_task(", cmd.drgn_option.stdout) |
| 220 |
|
| 221 |
@skip_unless_have_test_kmod |
| 222 |
def test_struct(self): |
| 223 |
address = self.prog.symbol("drgn_test_waitq_container").address |
| 224 |
cmd = self.check_crash_command( |
| 225 |
f"waitq drgn_test_waitq_container_struct.waitq {address:x}" |
| 226 |
) |
| 227 |
self.assertIn("is empty", cmd.stdout) |
| 228 |
self.assertIn("waitqueue_for_each_task(", cmd.drgn_option.stdout) |
| 229 |
|
| 230 |
def test_no_arguments(self): |
| 231 |
self.assertRaisesRegex( |
| 232 |
CommandArgumentError, |
| 233 |
"is required", |
| 234 |
self.run_crash_command, |
| 235 |
"waitq", |
| 236 |
) |
| 237 |
|
| 238 |
def test_too_many_arguments(self): |
| 239 |
self.assertRaisesRegex( |
| 240 |
CommandArgumentError, |
| 241 |
"unrecognized", |
| 242 |
self.run_crash_command, |
| 243 |
"waitq foo.bar abcd1234 baz", |
| 244 |
) |