core-handler (1841B)
- #!/bin/sh
- # Copyright © 2024 Haelwenn (lanodan) Monnier <contact+core-handler@hacktivis.me>
- # SPDX-License-Identifier: MIT
- applyuidgid=$(command -v applyuidgid 2>/dev/null || echo s6-applyuidgid)
- # kernel.core_pattern = |/usr/local/bin/core-handler %P %u %g %s %t %c %e %E
- grep -q "$0"' %P %u %g %s %t %c %e %E$' /proc/sys/kernel/core_pattern || exit 1
- pid="$1"; shift
- uid="$1"; shift
- gid="$1"; shift
- signum="$1"; shift
- epoch="$1"; shift
- core_limit="$1"; shift
- comm="$1"; shift
- full_path="$1"; shift
- destdir="/var/crash/"
- dest="${destdir}/${epoch}-${pid}-${uid}-${comm}"
- umask u=r,og=
- # Using this script, /var/crash should be "0755/drwxr-xr-x root root"
- # Let's make sure it both exists and is safe before writing anything
- mkdir -m 0755 -p "${destdir}"
- chown 0:0 "${destdir}"
- chmod 0755 "${destdir}"
- cat - >"${dest}.core"
- chown -- "${uid}:${gid}" "${dest}.core"
- sync -d "${dest}.core"
- {
- printf 'pid: %s\n' "$pid"
- printf 'uid: %s\n' "$uid"
- printf 'gid: %s\n' "$gid"
- printf 'signum: %s\n' "$signum"
- printf 'epoch: %s\n' "$epoch"
- printf 'core_limit: %s\n' "$core_limit"
- printf 'comm: %s\n' "$comm"
- printf 'full_path: %s\n' "${full_path}" | tr '!' '/'
- } > "${dest}.info"
- chown -- "${uid}:${gid}" "${dest}.info"
- sync -d "${dest}.info"
- # Extract the following from a coredump with LLDB:
- # - backtrace, all threads
- # - current frame for readability
- # - global and frame-local variables
- # - registers
- # - dissasembly of current frame (with mixed source code when available)
- nice -n 20 -- "${applyuidgid?}" -u "$uid" -g "$gid" -G '' lldb \
- --core "${dest}.core" \
- -b \
- -o 'bt all' \
- -o 'f' \
- -o 'v -A -g -P2 -c -s' \
- -o 'register read' \
- -o 'di -m' \
- -o 'quit' \
- >"${dest}.backtrace.txt" 2>&1
- # -o "session save \"${dest}.backtrace.txt\""
- chown -- "${uid}:${gid}" "${dest}.backtrace.txt"
- sync -d "${dest}.backtrace.txt"