|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import semver |
| 6 | +from invoke.context import Context |
| 7 | + |
| 8 | +from tasks.kernel_matrix_testing import stacks |
| 9 | +from tasks.kernel_matrix_testing.infra import LibvirtDomain, build_infrastructure |
| 10 | +from tasks.kernel_matrix_testing.platforms import get_platforms |
| 11 | +from tasks.kernel_matrix_testing.tool import Exit, info |
| 12 | +from tasks.kernel_matrix_testing.vars import KMTPaths |
| 13 | +from tasks.libs.common.utils import get_repo_root |
| 14 | +from tasks.libs.types.arch import Arch |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from tasks.kernel_matrix_testing.types import ( |
| 18 | + Component, # noqa: F401 |
| 19 | + KMTArchNameOrLocal, |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +class GDBPaths: |
| 24 | + def __init__(self, vm_tag: str, image_version: str, stack: str, arch: KMTArchNameOrLocal): |
| 25 | + self.tag = vm_tag |
| 26 | + self.image_version = image_version |
| 27 | + self.kmt_paths = KMTPaths(stack, Arch.from_str(arch)) |
| 28 | + |
| 29 | + @property |
| 30 | + def vmlinux(self): |
| 31 | + return self.kmt_paths.gdb / self.tag / self.image_version / "vmlinux.dbg" |
| 32 | + |
| 33 | + @property |
| 34 | + def kernel_source(self): |
| 35 | + return self.kmt_paths.gdb / self.tag / self.image_version / "kernel-source" |
| 36 | + |
| 37 | + |
| 38 | +class UbuntuGDBProvision: |
| 39 | + def __init__(self, vm: LibvirtDomain, image_version: str, kernel: str): |
| 40 | + self.target = vm |
| 41 | + self.image_version = image_version |
| 42 | + self.kernel = semver.VersionInfo.parse(kernel) |
| 43 | + |
| 44 | + def run(self, ctx: Context, stack: str): |
| 45 | + self.target.copy( |
| 46 | + ctx, get_repo_root() / "tasks/kernel_matrix_testing/provision/ubuntu-dbg.sh", "/tmp/provision.sh" |
| 47 | + ) |
| 48 | + self.target.run_cmd(ctx, "chmod +x /tmp/provision.sh && /tmp/provision.sh") |
| 49 | + |
| 50 | + gdb_paths = GDBPaths(self.target.tag, self.image_version, stack, self.target.arch) |
| 51 | + gdb_paths.vmlinux.parent.mkdir(exist_ok=True, parents=True) |
| 52 | + self.target.download(ctx, "/usr/lib/debug/boot/vmlinux.dbg", f"{gdb_paths.vmlinux}") |
| 53 | + |
| 54 | + # make sure we are deleteing from a sane path |
| 55 | + if gdb_paths.kernel_source.name == "kernel-source" and f"{gdb_paths.kernel_source.absolute()}".startswith( |
| 56 | + f"{get_repo_root().absolute()}" |
| 57 | + ): |
| 58 | + ctx.run(f"rm -rf {gdb_paths.kernel_source}") |
| 59 | + |
| 60 | + gdb_paths.kernel_source.mkdir(parents=True) |
| 61 | + self.target.download( |
| 62 | + ctx, |
| 63 | + f"/usr/src/linux-source-{self.kernel.finalize_version()}/linux-source-{self.kernel.finalize_version()}.tar.bz2", |
| 64 | + f"{gdb_paths.kernel_source.parent}", |
| 65 | + ) |
| 66 | + ctx.run( |
| 67 | + f"cd {gdb_paths.kernel_source.parent} && tar xvf linux-source-{self.kernel.finalize_version()}.tar.bz2 -C {gdb_paths.kernel_source} --strip-components=1", |
| 68 | + hide="out", |
| 69 | + echo=True, |
| 70 | + ) |
| 71 | + |
| 72 | + if self.kernel > semver.VersionInfo.parse("4.4.0"): |
| 73 | + ctx.run(f"cd {gdb_paths.kernel_source} && make defconfig && make scripts_gdb") |
| 74 | + |
| 75 | + self.target.run_cmd(ctx, "shutdown -h now", verbose=True, allow_fail=True) |
| 76 | + |
| 77 | + |
| 78 | +gdb_provision = { |
| 79 | + "ubuntu": { |
| 80 | + "22.04": UbuntuGDBProvision, |
| 81 | + "23.10": UbuntuGDBProvision, |
| 82 | + "24.04": UbuntuGDBProvision, |
| 83 | + "24.10": UbuntuGDBProvision, |
| 84 | + "20.04": UbuntuGDBProvision, |
| 85 | + # TODO: Add support for bionic/ubuntu_18.04. Currently failing to find debug kernel build. |
| 86 | + "16.04": UbuntuGDBProvision, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +def setup_gdb_debugging(ctx: Context, stack: str) -> None: |
| 92 | + infra = build_infrastructure(stack) |
| 93 | + platforms = get_platforms() |
| 94 | + |
| 95 | + arch = Arch.local().kmt_arch |
| 96 | + for kmt_arch, instance in infra.items(): |
| 97 | + if kmt_arch != "local": |
| 98 | + # TODO: add support to attach gdb to remote VMs |
| 99 | + raise Exit("stacks with remote VMs cannot be launched with GDB") |
| 100 | + |
| 101 | + for vm in instance.microvms: |
| 102 | + platinfo = platforms[arch][vm.tag] |
| 103 | + os_id = platinfo['os_id'] |
| 104 | + os_version = platinfo['os_version'] |
| 105 | + image_version = platinfo['image_version'] |
| 106 | + kernel = platinfo['kernel'] |
| 107 | + |
| 108 | + if os_id not in gdb_provision: |
| 109 | + raise Exit(f"{os_id} is currently not supported for kernel debugging") |
| 110 | + |
| 111 | + if os_version not in gdb_provision[os_id]: |
| 112 | + raise Exit(f"{os_id}_{os_version} is currently not supported for kernel debugging") |
| 113 | + |
| 114 | + provisioner = gdb_provision[os_id][os_version](vm, image_version, kernel) |
| 115 | + info(f"[+] Provisioning {vm.tag} for debugging.") |
| 116 | + provisioner.run(ctx, stack) |
| 117 | + |
| 118 | + stacks.pause_stack(stack) |
| 119 | + stacks.resume_stack(stack) |
0 commit comments