Output $myname, not $0
[with-gdb-backtrace-to.git] / with-gdb-backtrace-to
blob346c1e8b2301d3bb7845100c29790b02e5b3dc39
1 #!/bin/sh
3 # by christian at jaeger mine nu, put into the public domain
4 # thanks go to tromey on #gdb
6 set -eu
8 myname=`basename "$0"`
10 if [ $# -lt 2 ]; then
11 echo "usage: $myname outfile program arguments"
12 echo " runs program with arguments and outputs gdb log to"
13 echo " \${outfile}.log and the backtrace to \${outfile}_backtrace.txt"
14 exit 1
17 outfile="$1"
18 shift
19 program="$1"
20 shift
22 cmdfile=`tempfile`
23 logfile="${outfile}.log"
24 backtracefile="${outfile}_backtrace.txt"
26 moveold () {
27 if test -e "$1"; then
28 mvnumber "$1" || true
32 moveold "$logfile"
33 moveold "$backtracefile"
35 ##todo: can't use quotes in cmd files. but what about files with spaces?...
36 cat <<EOF > "$cmdfile"
37 set pagination off
38 set logging file $logfile
39 set logging on
40 set logging redirect on
41 run
42 set logging off
43 set logging redirect off
44 set logging file $backtracefile
45 set logging on
46 set logging redirect on
47 bt full
48 quit
49 EOF
51 gdb -x "$cmdfile" -batch-silent --args "$program" "$@"
52 rm "$cmdfile"
54 if [ "No stack." == "`cat "$backtracefile"`" ]; then
55 rm "$backtracefile"
56 else
57 echo "$myname: application generated a backtrace" >&2
60 # produce a success/error exit code:
61 tail -1 "$logfile" | grep --quiet "Program exited normally"