synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / scripts / p65-gdb-sample.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Issue #65 Stage A: wall-clock stack sampler for unprivileged hosts (perf_event_paranoid = 4 blocks |
| 3 | # perf and the PMU). gdb launches the program as its own child (allowed under yama ptrace_scope 1); |
| 4 | # a loop sends SIGINT to the running inferior every INTERVAL seconds of its run time, gdb stops it, |
| 5 | # prints every thread's backtrace, and continues. Stopped time is not sampled (the loop skips a |
| 6 | # stopped inferior), so samples fall in running time. |
| 7 | # Usage: p65-gdb-sample.sh <out-samples.txt> <max-samples> <interval-s> -- <program> [args...] |
| 8 | set -euo pipefail |
| 9 | out=$1; max=$2; interval=$3; shift 3 |
| 10 | [ "$1" = "--" ] && shift |
| 11 | cmds=$(mktemp) |
| 12 | { |
| 13 | echo "set pagination off"; echo "set confirm off"; echo "set print thread-events off" |
| 14 | echo "set startup-with-shell off"; echo "set print frame-arguments none" |
| 15 | echo "handle SIGINT stop print nopass"; echo "handle SIGPIPE nostop noprint pass" |
| 16 | echo "run" |
| 17 | for _ in $(seq "$max"); do echo 'echo ==SAMPLE==\n'; echo "thread apply all bt 48"; echo "continue"; done |
| 18 | } > "$cmds" |
| 19 | gdb -q -batch -x "$cmds" --args "$@" > "$out" 2>&1 & |
| 20 | gdbpid=$! |
| 21 | child="" |
| 22 | while [ -z "$child" ] && kill -0 "$gdbpid" 2>/dev/null; do child=$(pgrep -P "$gdbpid" | head -1 || true); sleep 0.05; done |
| 23 | echo "inferior pid $child comm $(cat /proc/$child/comm 2>/dev/null)" >&2 |
| 24 | n=0 |
| 25 | while kill -0 "$child" 2>/dev/null; do |
| 26 | sleep "$interval" |
| 27 | st=$(awk '{print $3}' "/proc/$child/stat" 2>/dev/null || echo X) |
| 28 | [ "$st" = "t" ] || [ "$st" = "T" ] || [ "$st" = "X" ] && continue |
| 29 | kill -INT "$child" 2>/dev/null && n=$((n+1)) || true |
| 30 | done |
| 31 | echo "signals sent: $n" >&2 |
| 32 | wait "$gdbpid" || true |
| 33 | rm -f "$cmds" |
| 34 | echo "samples: $(grep -c '^==SAMPLE==' "$out")" |