1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: LGPL-2.1-or-later
import ctypes
import mmap
import os
import os.path
import re
import tempfile
from drgn import Object
from drgn.helpers.linux.fs import fget
from drgn.helpers.linux.pid import find_task
from tests.linux_kernel import CLONE_NEWNS, fork_and_stop, mlock, unshare
from tests.linux_kernel.crash_commands import CrashCommandTestCase
class TestFiles(CrashCommandTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.run_crash_command("set -p")
def _test_drgn_common(self, cmd, cache=False):
self.assertIn("for_each_file(", cmd.drgn_option.stdout)
for variable in (
"task",
"pid",
"command",
"inode",
):
with self.subTest(variable=variable):
self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
if cache:
for variable in ("i_mapping", "nrpages"):
with self.subTest(variable=variable):
self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
self.assertNotIn("dentry", cmd.drgn_option.globals)
else:
for variable in ("file", "dentry"):
with self.subTest(variable=variable):
self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
self.assertNotIn("i_mapping", cmd.drgn_option.globals)
self.assertNotIn("nrpages", cmd.drgn_option.globals)
self.assertIsInstance(cmd.drgn_option.globals["cpu"], int)
self.assertIsInstance(cmd.drgn_option.globals["fd"], int)
self.assertIsInstance(cmd.drgn_option.globals["type"], str)
for variable in (
"root",
"cwd",
"path",
):
with self.subTest(variable=variable):
self.assertIsInstance(cmd.drgn_option.globals[variable], bytes)
def test_no_options(self):
with tempfile.NamedTemporaryFile() as f:
cmd = self.check_crash_command("files")
self.assertRegex(cmd.stdout, r"(?m)^ROOT: .* CWD:")
self.assertRegex(
cmd.stdout,
rf"(?m)^\s*{f.fileno()}\s+[0-9a-f]+\s+[0-9a-f]+\s+[0-9a-f]+\s+REG\s+{re.escape(f.name)}",
)
self._test_drgn_common(cmd)
def test_tasks(self):
with tempfile.NamedTemporaryFile() as f:
with fork_and_stop() as pid:
cmd = self.check_crash_command(f"files {os.getpid()} {pid}")
foreach_cmd = self.check_crash_command(
f"foreach {os.getpid()} {pid} files", mode="capture"
)
for c in (cmd, foreach_cmd):
self.assertIn(f"PID: {os.getpid()}", c.stdout)
self.assertIn(f"PID: {pid}", c.stdout)
self.assertRegex(c.stdout, r"(?m)^ROOT: .* CWD:")
self.assertEqual(
len(re.findall(rf"^\s*{f.fileno()}\b", c.stdout, flags=re.M)), 2
)
self._test_drgn_common(cmd)
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout)
def test_c(self):
with tempfile.NamedTemporaryFile() as f:
cmd = self.check_crash_command(f"files -c {os.getpid()}")
foreach_cmd = self.check_crash_command(
f"foreach {os.getpid()} files -c", mode="capture"
)
for c in (cmd, foreach_cmd):
self.assertRegex(c.stdout, r"(?m)^ROOT: .* CWD:")
self.assertRegex(
c.stdout,
rf"(?m)^\s*{f.fileno()}\s+[0-9a-f]+\s+[0-9a-f]+\s+[0-9]+\s+REG\s+{re.escape(f.name)}",
)
self._test_drgn_common(cmd, cache=True)
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout)
def test_d(self):
with tempfile.NamedTemporaryFile() as f:
dentry = fget(
find_task(self.prog, os.getpid()), f.fileno()
).f_path.dentry.read_()
cmd = self.check_crash_command(f"files -d {dentry.value_():x}")
self.assertRegex(
cmd.stdout,
rf"(?m)^\s*{dentry.value_():x}\s+[0-9a-f]+\s+[0-9a-f]+\s+REG\s+.*{re.escape(os.path.basename(f.name))}",
)
self.assertEqual(cmd.drgn_option.globals["dentry"], dentry)
for variable in ("inode", "sb"):
with self.subTest(variable=variable):
self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
self.assertEqual(cmd.drgn_option.globals["type"], "REG")
self.assertIsInstance(cmd.drgn_option.globals["path"], bytes)
def test_p(self):
with tempfile.TemporaryFile(dir="/dev/shm") as f:
f.write(os.urandom(2 * mmap.PAGESIZE))
f.flush()
inode = fget(find_task(self.prog, os.getpid()), f.fileno()).f_inode.read_()
with mmap.mmap(f.fileno(), 2 * mmap.PAGESIZE) as map:
f.close()
address = ctypes.addressof(ctypes.c_char.from_buffer(map))
# Make sure the pages are faulted in and stay that way.
mlock(address, 2 * mmap.PAGESIZE)
cmd = self.check_crash_command(f"files -p {inode.value_():x}")
self.assertRegex(cmd.stdout, r"(?m)^\s*INODE\s+NRPAGES.*\n\s*[0-9a-f]+\s+2")
self.assertRegex(
cmd.stdout, r"(?m)^\s*PAGE\s+PHYSICAL\s+MAPPING\s+INDEX\s+CNT\s+FLAGS.*\n"
)
self.assertRegex(cmd.stdout, r"(?m)^\s*[0-9a-f]+\s+[0-9a-f]+\s+[0-9a-f]+\s+\b")
self.assertIn("inode_for_each_page(", cmd.drgn_option.stdout)
for variable in (
"inode",
"i_mapping",
"nrpages",
"page",
"physical",
"cnt",
"flags",
):
with self.subTest(variable=variable):
self.assertIsInstance(cmd.drgn_option.globals[variable], Object)
self.assertIsInstance(cmd.drgn_option.globals["index"], int)
self.assertIsInstance(cmd.drgn_option.globals["decoded_flags"], str)
def _test_R(self, arg_fn, expect_no_cache, expect_cache, test_foreach=False):
with tempfile.NamedTemporaryFile() as f:
arg = arg_fn(f)
cmd = self.check_crash_command(f"files -R {arg} {os.getpid()}")
cmds = [cmd]
if test_foreach:
foreach_cmd = self.check_crash_command(
f"foreach {os.getpid()} files -R {arg}", mode="capture"
)
cmds.append(foreach_cmd)
for c in cmds:
if expect_no_cache:
self.assertRegex(
c.stdout,
rf"(?m)^\s*{f.fileno()}\s+[0-9a-f]+\s+[0-9a-f]+\s+[0-9a-f]+\s+REG\s+{re.escape(f.name)}",
)
else:
self.assertFalse(c.stdout)
self._test_drgn_common(cmd)
self.assertIsInstance(cmd.drgn_option.globals["is_match"], bool)
if test_foreach:
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout)
cmd = self.check_crash_command(f"files -c -R {arg} {os.getpid()}")
cmds = [cmd]
if test_foreach:
foreach_cmd = self.check_crash_command(
f"foreach {os.getpid()} files -c -R {arg}", mode="capture"
)
cmds.append(foreach_cmd)
for c in cmds:
if expect_cache:
self.assertRegex(
c.stdout,
rf"(?m)^\s*{f.fileno()}\s+[0-9a-f]+\s+[0-9a-f]+\s+[0-9]+\s+REG\s+{re.escape(f.name)}",
)
else:
self.assertFalse(c.stdout)
self._test_drgn_common(cmd, cache=True)
self.assertIsInstance(cmd.drgn_option.globals["is_match"], bool)
if test_foreach:
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout)
def test_R_fd(self):
self._test_R(lambda f: str(f.fileno()), True, True, True)
def test_R_filename(self):
self._test_R(lambda f: f.name, True, True)
def test_R_dentry(self):
self._test_R(
lambda f: hex(
fget(find_task(self.prog, os.getpid()), f.fileno()).f_path.dentry
),
True,
False,
)
def test_R_inode(self):
self._test_R(
lambda f: hex(fget(find_task(self.prog, os.getpid()), f.fileno()).f_inode),
True,
True,
)
def test_R_address_space(self):
self._test_R(
lambda f: hex(
fget(find_task(self.prog, os.getpid()), f.fileno()).f_inode.i_mapping
),
False,
True,
)
def test_R_file(self):
self._test_R(
lambda f: hex(fget(find_task(self.prog, os.getpid()), f.fileno())),
True,
False,
)
def test_R_cwd(self):
with tempfile.TemporaryDirectory() as tmp_dir:
with fork_and_stop(os.chdir, tmp_dir) as (pid, _):
cmd = self.check_crash_command(f"files -R {tmp_dir} {pid}")
self.assertRegex(cmd.stdout, rf"CWD: {re.escape(tmp_dir)}\n$")
self.assertEqual(cmd.drgn_option.globals["cwd"], os.fsencode(tmp_dir))
self.assertIsInstance(cmd.drgn_option.globals["is_match"], bool)
def test_no_files(self):
cmd = self.check_crash_command(f"files {hex(self.prog['init_task'].address_)}")
self.assertRegex(cmd.stdout, r"\nNo open files\n$")
class TestMount(CrashCommandTestCase):
def test_no_options(self):
self.run_crash_command("set 1")
cmd = self.check_crash_command("mount")
self.assertRegex(cmd.stdout, r"(?m)^\s*[0-9a-f]+\s+[0-9a-f]+.*proc")
self.assertIn("for_each_mount()", cmd.drgn_option.stdout)
self.assertNotIn("mnt_ns", cmd.drgn_option.globals)
def test_no_options_in_namespace(self):
with fork_and_stop(unshare, CLONE_NEWNS) as (pid, _):
self.run_crash_command(f"set {pid}")
cmd = self.check_crash_command("mount")
self.assertIn("for_each_mount(mnt_ns)", cmd.drgn_option.stdout)
self.assertEqual(
cmd.drgn_option.globals["mnt_ns"],
find_task(self.prog, pid).nsproxy.mnt_ns,
)
def test_n_pid(self):
cmd = self.check_crash_command("mount -n 1")
self.assertRegex(cmd.stdout, r"(?m)^\s*[0-9a-f]+\s+[0-9a-f]+.*proc")
self.assertIn("for_each_mount(mnt_ns)", cmd.drgn_option.stdout)
self.assertEqual(
cmd.drgn_option.globals["mnt_ns"], find_task(self.prog, 1).nsproxy.mnt_ns
)
def test_n_task(self):
task = self.prog["init_task"].address_of_()
cmd = self.check_crash_command(f"mount -n {hex(task)}")
self.assertRegex(cmd.stdout, r"(?m)^\s*[0-9a-f]+\s+[0-9a-f]+.*proc")
self.assertIn("for_each_mount(mnt_ns)", cmd.drgn_option.stdout)
self.assertEqual(
cmd.drgn_option.globals["mnt_ns"], self.prog["init_task"].nsproxy.mnt_ns
)
|