interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / scripts / p65-gdb-sample.sh

34 lines · 1682 bytes Raw Blame History
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...]
8set -euo pipefail
9out=$1; max=$2; interval=$3; shift 3
10[ "$1" = "--" ] && shift
11cmds=$(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"
19gdb -q -batch -x "$cmds" --args "$@" > "$out" 2>&1 &
20gdbpid=$!
21child=""
22while [ -z "$child" ] && kill -0 "$gdbpid" 2>/dev/null; do child=$(pgrep -P "$gdbpid" | head -1 || true); sleep 0.05; done
23echo "inferior pid $child comm $(cat /proc/$child/comm 2>/dev/null)" >&2
24n=0
25while 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
30done
31echo "signals sent: $n" >&2
32wait "$gdbpid" || true
33rm -f "$cmds"
34echo "samples: $(grep -c '^==SAMPLE==' "$out")"