#!/usr/bin/env bash
set -euo pipefail

ubuntu_release="$(lsb_release --codename --short)"
kernel_version="$(uname --kernel-release)"
kernel_symbols_package="linux-image-${kernel_version}-dbgsym"

# Do we already have the debug symbols installed?
if dpkg-query --list "$kernel_symbols_package" &> /dev/null; then
  echo "Kernel debug symbols are already installed."
  exit 0
fi

# Nope, add the debug symbols repository and install them.
echo "deb http://ddebs.ubuntu.com $ubuntu_release main restricted universe multiverse" | sudo tee /etc/apt/sources.list.d/ddebs.list
echo "deb http://ddebs.ubuntu.com ${ubuntu_release}-updates main restricted universe multiverse" | sudo tee --append /etc/apt/sources.list.d/ddebs.list
echo "deb http://ddebs.ubuntu.com ${ubuntu_release}-proposed main restricted universe multiverse" | sudo tee --append /etc/apt/sources.list.d/ddebs.list
wget --output-document - http://ddebs.ubuntu.com/dbgsym-release-key.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get --assume-yes install "linux-image-${kernel_version}-dbgsym"

echo
echo Kernel debug symbols are now installed.
echo Recapture your flame graph and you should see what the kernel is up to.
