drgn/vmtest/enter_kdump.py
Omar Sandoval 87b7292aa5 Relicense drgn from GPLv3+ to LGPLv2.1+
drgn is currently licensed as GPLv3+. Part of the long term vision for
drgn is that other projects can use it as a library providing
programmatic interfaces for debugger functionality. A more permissive
license is better suited to this goal. We decided on LGPLv2.1+ as a good
balance between software freedom and permissiveness.

All contributors not employed by Meta were contacted via email and
consented to the license change. The only exception was the author of
commit c4fbf7e589 ("libdrgn: fix for compilation error"), who did not
respond. That commit reverted a single line of code to one originally
written by me in commit 640b1c011d ("libdrgn: embed DWARF index in
DWARF info cache").

Signed-off-by: Omar Sandoval <osandov@osandov.com>
2022-11-01 17:05:16 -07:00

47 lines
1.5 KiB
Python

# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: LGPL-2.1-or-later
import ctypes
import os
import re
import sys
from util import NORMALIZED_MACHINE_NAME, SYS
# Some architectures don't implement kexec_file_load. Note that even if the
# number is defined, it may not be implemented.
if "kexec_file_load" not in SYS:
sys.exit(f"kexec_file_load is not defined on {NORMALIZED_MACHINE_NAME}")
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)
# `nokaslr` is required to avoid sporadically failing to reserve space for the
# capture kernel
cmdline += b" nokaslr"
if os.getenv("KDUMP_NEEDS_NOSMP"):
# `nosmp` is required to avoid QEMU sporadically failing an internal
# assertion when using emulation.
cmdline += b" nosmp"
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")