| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
from pathlib import Path |
| 5 |
import re |
| 6 |
import tempfile |
| 7 |
|
| 8 |
from drgn import Object |
| 9 |
from drgn.commands._crash.common import CRASH_COMMAND_NAMESPACE |
| 10 |
from tests.linux_kernel import possible_cpus, skip_unless_have_test_kmod |
| 11 |
from tests.linux_kernel.crash_commands import CrashCommandTestCase |
| 12 |
|
| 13 |
|
| 14 |
class TestEval(CrashCommandTestCase): |
| 15 |
def test_value(self): |
| 16 |
cmd = self.check_crash_command("eval 10") |
| 17 |
self.assertRegex(cmd.stdout, r"\bhexadecimal: a\b") |
| 18 |
self.assertRegex(cmd.stdout, r"\bdecimal: 10\b") |
| 19 |
self.assertRegex(cmd.stdout, r"\boctal: 12\b") |
| 20 |
self.assertRegex(cmd.stdout, r"\bbinary: 0*1010\b") |
| 21 |
|
| 22 |
self.assertIdentical( |
| 23 |
cmd.drgn_option.globals["value"], Object(self.prog, "unsigned long", 10) |
| 24 |
) |
| 25 |
self.assertIdentical( |
| 26 |
cmd.drgn_option.globals["signed"], Object(self.prog, "long", 10) |
| 27 |
) |
| 28 |
self.assertIsInstance(cmd.drgn_option.globals["in_units"], str) |
| 29 |
self.assertEqual(cmd.drgn_option.globals["in_hex"], "0xa") |
| 30 |
self.assertEqual(cmd.drgn_option.globals["in_octal"], "0o12") |
| 31 |
self.assertEqual(cmd.drgn_option.globals["in_binary"], "0b1010") |
| 32 |
|
| 33 |
def test_negate(self): |
| 34 |
cmd = self.check_crash_command("eval -99") |
| 35 |
self.assertRegex(cmd.stdout, r"\bhexadecimal: f+9d\b") |
| 36 |
self.assertRegex(cmd.stdout, r"\bdecimal: [0-9]+ \(-99\)") |
| 37 |
|
| 38 |
self.assertIdentical( |
| 39 |
cmd.drgn_option.globals["value"], Object(self.prog, "unsigned long", -99) |
| 40 |
) |
| 41 |
self.assertIdentical( |
| 42 |
cmd.drgn_option.globals["signed"], Object(self.prog, "long", -99) |
| 43 |
) |
| 44 |
|
| 45 |
def test_invert(self): |
| 46 |
cmd = self.check_crash_command("eval ~99") |
| 47 |
self.assertRegex(cmd.stdout, r"\bhexadecimal: f+9c\b") |
| 48 |
self.assertRegex(cmd.stdout, r"\bdecimal: [0-9]+ \(-100\)") |
| 49 |
|
| 50 |
self.assertIdentical( |
| 51 |
cmd.drgn_option.globals["value"], Object(self.prog, "unsigned long", ~99) |
| 52 |
) |
| 53 |
self.assertIdentical( |
| 54 |
cmd.drgn_option.globals["signed"], Object(self.prog, "long", ~99) |
| 55 |
) |
| 56 |
|
| 57 |
def test_multiple_unary(self): |
| 58 |
cmd = self.check_crash_command("eval -~--99") |
| 59 |
self.assertRegex(cmd.stdout, r"\bhexadecimal: 64\b") |
| 60 |
self.assertRegex(cmd.stdout, r"\bdecimal: 100\b") |
| 61 |
|
| 62 |
self.assertIdentical( |
| 63 |
cmd.drgn_option.globals["value"], Object(self.prog, "unsigned long", 100) |
| 64 |
) |
| 65 |
self.assertIdentical( |
| 66 |
cmd.drgn_option.globals["signed"], Object(self.prog, "long", 100) |
| 67 |
) |
| 68 |
|
| 69 |
def test_units(self): |
| 70 |
for value, in_units in ( |
| 71 |
(1024, "1K"), |
| 72 |
(1024 * 1024, "1M"), |
| 73 |
(1024 * 1024 * 1024, "1G"), |
| 74 |
): |
| 75 |
for expr in (str(value), in_units, in_units.lower()): |
| 76 |
with self.subTest(expr=expr): |
| 77 |
cmd = self.check_crash_command("eval " + expr) |
| 78 |
self.assertRegex( |
| 79 |
cmd.stdout, rf"\bhexadecimal: {value:x} \({in_units}B\)" |
| 80 |
) |
| 81 |
self.assertEqual(cmd.drgn_option.globals["in_units"], in_units) |
| 82 |
|
| 83 |
def test_negate_with_unit(self): |
| 84 |
cmd = self.check_crash_command("eval -1k") |
| 85 |
self.assertRegex(cmd.stdout, r"\bdecimal: [0-9]+ \(-1024\)") |
| 86 |
self.assertIdentical( |
| 87 |
cmd.drgn_option.globals["value"], Object(self.prog, "unsigned long", -1024) |
| 88 |
) |
| 89 |
|
| 90 |
def test_bits_set(self): |
| 91 |
cmd = self.check_crash_command("eval -b 10") |
| 92 |
self.assertRegex(cmd.stdout, r"\bbits set: 3 1\n") |
| 93 |
self.assertIdentical(cmd.drgn_option.globals["bits_set"], [1, 3]) |
| 94 |
|
| 95 |
def test_long_long(self): |
| 96 |
cmd = self.check_crash_command("eval -l 10") |
| 97 |
self.assertRegex( |
| 98 |
cmd.stdout, |
| 99 |
r"\bbinary: 0000000000000000000000000000000000000000000000000000000000001010\b", |
| 100 |
) |
| 101 |
self.assertIdentical( |
| 102 |
cmd.drgn_option.globals["value"], |
| 103 |
Object(self.prog, "unsigned long long", 10), |
| 104 |
) |
| 105 |
|
| 106 |
def test_bits_set_and_long_long(self): |
| 107 |
cmd = self.check_crash_command("eval -bl 10") |
| 108 |
self.assertRegex( |
| 109 |
cmd.stdout, |
| 110 |
r"\bbinary: 0000000000000000000000000000000000000000000000000000000000001010\b", |
| 111 |
) |
| 112 |
self.assertRegex(cmd.stdout, r"\bbits set: 3 1\n") |
| 113 |
self.assertIdentical(cmd.drgn_option.globals["bits_set"], [1, 3]) |
| 114 |
self.assertIdentical( |
| 115 |
cmd.drgn_option.globals["value"], |
| 116 |
Object(self.prog, "unsigned long long", 10), |
| 117 |
) |
| 118 |
|
| 119 |
def test_negative_b(self): |
| 120 |
cmd = self.check_crash_command("eval -b") |
| 121 |
self.assertRegex(cmd.stdout, r"\bdecimal: [0-9]+ \(-11\)") |
| 122 |
self.assertNotIn("bits set", cmd.stdout) |
| 123 |
self.assertIdentical( |
| 124 |
cmd.drgn_option.globals["value"], Object(self.prog, "unsigned long", -0xB) |
| 125 |
) |
| 126 |
|
| 127 |
def test_bits_set_in_negative_b(self): |
| 128 |
cmd = self.check_crash_command("eval -b -b") |
| 129 |
self.assertRegex(cmd.stdout, r"\bdecimal: [0-9]+ \(-11\)") |
| 130 |
self.assertIn("bits set", cmd.stdout) |
| 131 |
self.assertIn("bits_set", cmd.drgn_option.globals) |
| 132 |
|
| 133 |
def test_binary(self): |
| 134 |
for expr, result in ( |
| 135 |
("3 + 4", 7), |
| 136 |
("4 - 3", 1), |
| 137 |
("12 & 5", 4), |
| 138 |
("(12 | 5)", 13), |
| 139 |
("12 ^ 5", 9), |
| 140 |
("12 * 5", 60), |
| 141 |
("12 % 5", 2), |
| 142 |
("16 / 5", 3), |
| 143 |
("(3 << 5)", 96), |
| 144 |
("(100 >> 1)", 50), |
| 145 |
): |
| 146 |
cmd = self.check_crash_command("eval " + expr) |
| 147 |
self.assertRegex(cmd.stdout, rf"\bdecimal: {result}\b") |
| 148 |
self.assertIdentical( |
| 149 |
cmd.drgn_option.globals["value"], |
| 150 |
Object(self.prog, "unsigned long", result), |
| 151 |
) |
| 152 |
|
| 153 |
def test_pipe(self): |
| 154 |
with tempfile.TemporaryDirectory() as temp_dir: |
| 155 |
path = Path(temp_dir) / "file" |
| 156 |
CRASH_COMMAND_NAMESPACE.run(self.prog, f"eval (12 | 5) | grep 13 > {path}") |
| 157 |
self.assertRegex(path.read_text(), r"\bdecimal: 13\b") |
| 158 |
|
| 159 |
def test_redirect(self): |
| 160 |
with tempfile.TemporaryDirectory() as temp_dir: |
| 161 |
path = Path(temp_dir) / "file" |
| 162 |
CRASH_COMMAND_NAMESPACE.run(self.prog, f"eval (3 << 5) >> {path}") |
| 163 |
self.assertRegex(path.read_text(), r"\bdecimal: 96\b") |
| 164 |
|
| 165 |
|
| 166 |
class TestP(CrashCommandTestCase): |
| 167 |
def test_object(self): |
| 168 |
cmd = self.check_crash_command("p jiffies") |
| 169 |
self.assertRegex(cmd.stdout, r"jiffies = \([^)]+\)[0-9]+") |
| 170 |
self.assertIdentical(cmd.drgn_option.globals["object"], self.prog["jiffies"]) |
| 171 |
|
| 172 |
@skip_unless_have_test_kmod |
| 173 |
def test_cpuspec(self): |
| 174 |
cmd = self.check_crash_command("p drgn_test_percpu_structs:a") |
| 175 |
matches = re.findall( |
| 176 |
r"^per_cpu\(drgn_test_percpu_structs, ([0-9]+)\) = \(struct drgn_test_percpu_struct\)\{", |
| 177 |
cmd.stdout, |
| 178 |
flags=re.MULTILINE, |
| 179 |
) |
| 180 |
cpus = sorted(possible_cpus()) |
| 181 |
self.assertEqual([int(match) for match in matches], cpus) |
| 182 |
self.assertIn("per_cpu(", cmd.drgn_option.stdout) |
| 183 |
self.assertEqual(cmd.drgn_option.globals["object"].cpu, max(cpus)) |
| 184 |
|
| 185 |
def test_member(self): |
| 186 |
cmd = self.check_crash_command("p init_task.pid") |
| 187 |
self.assertRegex(cmd.stdout, r"init_task\.pid = \([^)]*\)0") |
| 188 |
self.assertIn(".pid", cmd.drgn_option.stdout) |
| 189 |
self.assertIdentical( |
| 190 |
cmd.drgn_option.globals["object"], self.prog["init_task"].pid |
| 191 |
) |
| 192 |
|
| 193 |
@skip_unless_have_test_kmod |
| 194 |
def test_radix(self): |
| 195 |
self.addCleanup(self.prog.config.pop, "crash_radix", None) |
| 196 |
|
| 197 |
self.run_crash_command("set radix 16") |
| 198 |
cmd = self.run_crash_command("p drgn_test_singular_list_entry") |
| 199 |
self.assertRegex(cmd.stdout, r"value = \([^)]+\)0x0") |
| 200 |
|
| 201 |
cmd = self.run_crash_command("p -d drgn_test_singular_list_entry") |
| 202 |
self.assertRegex(cmd.stdout, r"value = \([^)]+\)0\b") |
| 203 |
|
| 204 |
self.run_crash_command("set radix 10") |
| 205 |
cmd = self.run_crash_command("p drgn_test_singular_list_entry") |
| 206 |
self.assertRegex(cmd.stdout, r"value = \([^)]+\)0\b") |
| 207 |
|
| 208 |
cmd = self.run_crash_command("p -x drgn_test_singular_list_entry") |
| 209 |
self.assertRegex(cmd.stdout, r"value = \([^)]+\)0x0") |