1#!/bin/sh
2##### -*- mode:shell-script; indent-tabs-mode:nil; sh-basic-offset:2 -*-
3##### Author: Travis Cross <tc@traviscross.com>
4
5log1 () { printf '%s' "$1">&2; }
6log () { printf '%s\n' "$1">&2; }
7err () { log "$1"; exit 1; }
8
9usage () {
10  local opt="$1" bs="" be=""
11  $opt && { bs="[ "; be=" ]"; }
12  log "usage: $0 <corefile> ${bs}<path/to/freeswitch>${be}"
13}
14
15while getopts "h" o; do
16  case "$o" in
17    h) usage true; exit 0; ;;
18  esac
19done
20shift $(($OPTIND-1))
21
22
23if [ $# -lt 1 ]; then
24  usage true; exit 1
25fi
26core="$1"
27if ! [ $# -lt 2 ]; then
28  fspath="$2"
29  [ -x "$fspath" ] || err "Not executable: $fspath"
30fi
31btpath="/tmp/$(date -u +%Y%m%dT%H%M%SZ)-bt.txt"
32if [ -z "$fspath" ]; then
33  for x in "$(which freeswitch)" \
34    /usr/bin/freeswitch /usr/sbin/freeswitch \
35    /usr/local/bin/freeswitch /usr/local/sbin/freeswitch \
36    /opt/freeswitch/bin/freeswitch; do
37    ! [ -x "$x" ] || { fspath="$x"; break; }
38  done
39fi
40if [ -z "$fspath" ]; then
41  log "Couldn't find FS binary"
42  usage false; exit 1
43fi
44if test $(id -u) = 0 && test -f /etc/debian_version; then
45  cat >&2 <<'EOF'
46### You're running on Debian.  Please make sure you have appropriate
47### freeswitch-*-dbg packages installed so we get as many symbols in
48### this backtrace as possible.  I won't install these for you.  If
49### you're running the freeswitch-all package, then you should install
50### freeswitch-all-dbg.
51EOF
52log ''
53fi
54
55log1 'Generating backtrace...'
56gdb "$fspath" "$core" > $btpath <<'EOF'
57set prompt
58set pagination off
59printf "\n\n"
60printf "================================================================================\n"
61printf "# GDB session generated by FS backtrace-from-core\n"
62printf "# FreeSWITCH version: %s\n", switch_version_full_str
63printf "# FreeSWITCH version (human): %s\n", switch_version_full_human_str
64printf "================================================================================\n"
65printf "\n\n"
66printf "================================================================================\n"
67printf "# info threads\n"
68printf "================================================================================\n"
69info threads
70printf "================================================================================\n"
71printf "# bt\n"
72printf "================================================================================\n"
73bt
74printf "================================================================================\n"
75printf "# bt full\n"
76printf "================================================================================\n"
77bt full
78printf "================================================================================\n"
79printf "# thread apply all bt\n"
80printf "================================================================================\n"
81thread apply all bt
82printf "================================================================================\n"
83printf "# thread apply all bt full\n"
84printf "================================================================================\n"
85thread apply all bt full
86quit
87EOF
88log 'done'
89log ''
90log "Please attach the backtrace here:"
91log "$btpath"
92