#!/bin/bash
#
# dump_limit_malloc_stacks: Dump all disallowed malloc stack traces
#
# Automates a 'gdb' session to dump stack traces from all allocations
# disallowed by LimitMalloc. The entire session prints to stdout; use
# redirections to save the result.
#
# Example:
#
#  $ bazel build -c dbg //common/test_utilities:limit_malloc_manual_test
#  $ tools/dynamic_analysis/dump_limit_malloc_stacks \
#      bazel-bin/common/test_utilities/limit_malloc_manual_test \
#      |& tee stacks.out
#
# The output will contain some useless stacks generated by ordinary gdb
# operation, and one stack indicating the deliberate failure encoded in
# limit_malloc_manual_test.cc.
#
set -eu -o pipefail

SCRIPT=$(mktemp)
clean () {
    rm $SCRIPT
}
trap clean EXIT

cat <<eof > $SCRIPT
set pagination off

catch signal SIGTRAP
commands
# 'backtrace full' can crash if the data structures are too fancy.
backtrace
# 'signal 0' banishes the signal so we can continue.
signal 0
continue
end

run
quit
eof

DRAKE_LIMIT_MALLOC_NONFATAL_BREAKPOINT=1 \
 exec gdb --batch -x $SCRIPT --args "$@"
