#!/bin/sh

# Copyright C 2010-2010 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
#
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
#
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems.
#
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

#####################################################################
# USAGE: This script allows one to load gdb with appropriate context
# for core files. This includes uncompressing the core if necessary,
# setting search paths and absolute-prefix for symbol directories,
# etc.
#####################################################################

: ${GDB=/usr/local/bin/gdb}
CMDFILE=/tmp/gdbcoreinit
IFSCORE=/ifs/.ifsvar/tmp/core

HAVE_ISI_MOUNT_SYMBOLS=$(which isi_mount_symbols >/dev/null 2>&1 && echo 1 || echo 0)

usage() {
    if [ ${HAVE_ISI_MOUNT_SYMBOLS} -eq 1 ]; then
        echo "Usage: ${0##*/} [-b build] [-c command] [-f {release|debug}] [-h] [-l] <executable> <core file> [symbol directory]" >&2
        echo "  -b build | symbol directory:  Prefer symbols/binaries from the specified build/directory." >&2
        echo "  -c command                 :  GDB command to run after load." >&2
        echo "  -f                         :  Build type when used with -b." >&2
        echo "  -l                         :  Prefer local symbols/binaries." >&2
    else
        echo "Usage: ${0##*/} [-c command] <executable> <core file> [symbol directory]" >&2
        echo "  -c command                 :  GDB command to run after load." >&2
    fi
    exit 1
}

# Only support advanced options internally.
if [ ${HAVE_ISI_MOUNT_SYMBOLS} -eq 1 ]; then
    PREFER_TARGET_BINS=1
    while getopts "b:c:f:hl" flag; do
        case "${flag}" in
          b)
              ISI_BUILD_NUMBER="${OPTARG}"
              ;;
          c)
              GDB_CMD="${OPTARG}"
              ;;
          f)
              FLAV="${OPTARG}"
              ;;
          h)
              usage
              ;;
          l)
              PREFER_TARGET_BINS=0
              ;;
          *)
              usage
              ;;
        esac
    done
    shift $((OPTIND-1))
fi

if [ $# -lt 2 -o $# -gt 4 ]; then
    usage
fi

EXEC=$1
CORE=$2
SYM="$3"

echo "Executable: ${EXEC}"
echo "Core: ${CORE}"

if [ ! -e ${CORE} ]; then
    echo "Core file '${CORE}' not found, exiting."
    exit 1
fi

# Test the extension
if [ "${CORE##*.}" = "gz" ]; then
    if ! mount | grep -q OneFS; then
	echo "/ifs not mounted, needed to uncompress core"
	exit 1
    fi
    CORETIME=$(stat -r ${CORE} | cut -f 11 -d\ )
    NEWCORE=${IFSCORE}/$(basename ${CORE} .core.gz).$(hostname).${CORETIME}.core
    if [ ! -e ${NEWCORE} ]; then
	mkdir -p ${IFSCORE} || exit 1
	gzip -d < ${CORE} > ${NEWCORE} || exit 1
    fi
    echo "Uncompressed core: ${NEWCORE}"
    CORE=${NEWCORE}
fi

rm -f ${CMDFILE} 2>/dev/null || :

# Load gdbinit if present
if [ -e ~/.gdbinit ]; then
    echo "source ~/.gdbinit" >> ${CMDFILE}
fi

if [ -z "${SYM}" -a ${HAVE_ISI_MOUNT_SYMBOLS} -eq 1 ]; then
    # No symbol directory specified. Use the build's symbol directory if given
    # (by exporting ISI_BUILD_NUMBER/FLAV) otherwise fallback on the
    # running build via isi_mount_symbols.
    export ISI_BUILD_NUMBER
    export FLAV
    if isi_mount_symbols; then
        SYM="/var/crash/tools/symbols"
    fi
fi

LOCAL_SYMBOLS_DIRS="/usr/lib/debug:/usr/local/lib/debug"

if [ -n "${SYM}" ]; then
    SYM="$(realpath "${SYM}")"
    echo "Symbols directory: ${SYM}"
    SYMBOLS_PREFIX="${SYM}"
    TARGET_SYMBOLS_DIRS="${SYMBOLS_PREFIX}:${SYMBOLS_PREFIX}/usr/lib/debug:${SYMBOLS_PREFIX}/usr/local/lib/debug"
    if [ ${PREFER_TARGET_BINS} -eq 1 ]; then
        echo "Using binaries from symbols directory.  Specify the '-l' option to use local symbols/binaries."
        EXEC=${SYM%/}/${EXEC#/}
        echo "set solib-absolute-prefix ${SYM}" >> ${CMDFILE}
        echo "set solib-search-path ${SYM}/usr/lib:${SYM}/usr/lib/debug:${SYM}/usr/likewise/lib" >> ${CMDFILE}
        SYMBOLS_DIRS="${TARGET_SYMBOLS_DIRS}:${LOCAL_SYMBOLS_DIRS}"
    else
        SYMBOLS_DIRS="${LOCAL_SYMBOLS_DIRS}:${TARGET_SYMBOLS_DIRS}"
    fi
else
    # No directory or build, use local symbols only.
    SYMBOLS_DIRS="${LOCAL_SYMBOLS_DIRS}"
fi

echo "Symbols search path: ${SYMBOLS_DIRS}"
echo "set debug-file-directory ${SYMBOLS_DIRS}" >> ${CMDFILE}

echo "file ${EXEC}" >> ${CMDFILE}
echo "core ${CORE}" >> ${CMDFILE}
[ -n "${GDB_CMD}" ] && echo "${GDB_CMD}" >> ${CMDFILE}

echo "Loading gdb with these commands:"
echo "--------------------------------"
cat ${CMDFILE}
echo "--------------------------------"

${GDB} -n -x ${CMDFILE}
