Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,20 @@ def _run_vm(self, kernel_dir):

import vmtest.vm

command = fr"""cd {shlex.quote(os.getcwd())} &&
DRGN_RUN_LINUX_HELPER_TESTS=1 {shlex.quote(sys.executable)} -Bm \
unittest discover -t . -s tests/helpers/linux {"-v" if self.verbose else ""}"""
command = fr"""
set -e

cd {shlex.quote(os.getcwd())}
if "$BUSYBOX" [ -e /proc/vmcore ]; then
"$PYTHON" -Bm unittest discover -t . -s tests/linux_kernel/vmcore {"-v" if self.verbose else ""}
else
DRGN_RUN_LINUX_HELPER_TESTS=1 "$PYTHON" -Bm \
unittest discover -t . -s tests/helpers/linux {"-v" if self.verbose else ""}
"$PYTHON" vmtest/enter_kdump.py
# We should crash and not reach this.
exit 1
fi
"""
try:
returncode = vmtest.vm.run_in_vm(
command, Path(kernel_dir), Path(self.vmtest_dir)
Expand Down
Empty file added tests/linux_kernel/__init__.py
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions tests/linux_kernel/vmcore/test_vmcore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pathlib import Path
import unittest

from drgn import Program, ProgramFlags

VMCORE_PATH = Path("/proc/vmcore")


@unittest.skipUnless(VMCORE_PATH.exists(), "not running in kdump")
class TestAttachToVMCore(unittest.TestCase):
def test_attach_to_vmcore(self):
prog = Program()
prog.set_core_dump("/proc/vmcore")
self.assertFalse(prog.flags & ProgramFlags.IS_LIVE)
self.assertTrue(prog.flags & ProgramFlags.IS_LINUX_KERNEL)
40 changes: 40 additions & 0 deletions vmtest/enter_kdump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: GPL-3.0-or-later

# This isn't great: it's specific to x86-64, both by virtue of the syscall
# number and because kexec_file_load isn't implemented on many architectures,
# especially on older kernels.

import ctypes
import os
import re

SYS_kexec_file_load = 320 # On x86-64.
KEXEC_FILE_ON_CRASH = 2
KEXEC_FILE_NO_INITRAMFS = 4

syscall = ctypes.CDLL(None, use_errno=True).syscall
syscall.restype = ctypes.c_long

with open("/proc/cmdline", "rb") as f:
cmdline = f.read().rstrip(b"\n")
cmdline = re.sub(rb"(^|\s)crashkernel=\S+", b"", cmdline)
# `nosmp` is required to avoid QEMU sporadically failing an internal assertion
# `nokaslr` is required to avoid sporadically failing to reserve space for the
# capture kernel
cmdline += b" nosmp nokaslr"

with open(f"/lib/modules/{os.uname().release}/vmlinuz", "rb") as kernel:
if syscall(
ctypes.c_long(SYS_kexec_file_load),
ctypes.c_int(kernel.fileno()),
ctypes.c_int(-1),
ctypes.c_ulong(len(cmdline) + 1),
ctypes.c_char_p(cmdline + b"\0"),
ctypes.c_ulong(KEXEC_FILE_ON_CRASH | KEXEC_FILE_NO_INITRAMFS),
):
errno = ctypes.get_errno()
raise OSError(errno, os.strerror(errno))

with open("/proc/sysrq-trigger", "w") as f:
f.write("c")
4 changes: 3 additions & 1 deletion vmtest/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
set -eu

export BUSYBOX={busybox}
export PYTHON={python}

trap '"$BUSYBOX" poweroff -f' EXIT

Expand Down Expand Up @@ -214,6 +215,7 @@ def run_in_vm(command: str, kernel_dir: Path, build_dir: Path) -> int:
_INIT_TEMPLATE.format(
_9PFS_MSIZE=_9PFS_MSIZE,
busybox=shlex.quote(busybox),
python=shlex.quote(sys.executable),
command=shlex.quote(command),
)
)
Expand Down Expand Up @@ -246,7 +248,7 @@ def run_in_vm(command: str, kernel_dir: Path, build_dir: Path) -> int:

"-kernel", str(kernel_dir / "vmlinuz"),
"-append",
f"rootfstype=9p rootflags=trans=virtio,cache=loose,msize={_9PFS_MSIZE} ro console=0,115200 panic=-1 init={init}",
f"rootfstype=9p rootflags=trans=virtio,cache=loose,msize={_9PFS_MSIZE} ro console=0,115200 panic=-1 crashkernel=256M init={init}",
# fmt: on
],
env=env,
Expand Down