#!/usr/bin/env bash
#
# Copyright © 2025 Robin Templeton <robin@guixotic.coop>
#
# Run `pre-inst-env guix' under a debugger:
#
# gdb-pie.sh /path/to/pre-inst-env \ 
#   debugger-command debugger-argument ... \ 
#   -- guix-subcommand guix-argument ...
#
# This loads pre-inst-env's environment variables and invokes Guix's
# main Guile script directly using its shebang line, without any
# intermediate processes. It's meant for running Guix commands under
# gdb, such as:
#
# gdb-pie.sh ./pre-inst-env gdb -ex run --args -- shell bash -- sh -c :
#
# This can also be used with other debugging tools, such as `rr record'
# or `bpftrace -c'. Note that gdb in particular can't be used to debug a
# script with a shebang line.

pre_inst_env="$1"; shift
gdb_args=()
while (( $# )); do
  if [[ "$1" == "--" ]]; then shift; break; fi
  gdb_args+=("$1"); shift
done
(( $# > 0 )) || echo BAD

[[ -x "$pre_inst_env" ]] || exit 2
. <(sed 's/^exec .*//' "$pre_inst_env")

guix="$(command -v guix)"
[[ -x "$guix" ]] || exit 4

# See (info "(guile) The Meta Switch").
guile="$(sed 's/^#![ \t]*//;1{/\\$/{N;s/\\\n//};q}' < "$guix")"
[[ -x "${guile%%[ $'\t']*}" ]] || exit 1

"${gdb_args[@]}" $guile $guix "$@"

Generated by apteryx using scpaste at Fri Oct 10 22:49:51 2025. JST. (original)