Thursday, October 13, 2011

What do I do with this core file?

Use this script to generate a useful backtrace from the core file.

#!/bin/bash

corefile=$1
outfile=$1.gdbtrace.log

function getprocess {

        commandstring=$(gdb -c $corefile \
                --eval-command='set pagination off' \
                --eval-command='bt' \
                --eval-command='quit' \
                |awk '/Core was generated by/ { split($0, a, "`"); split(a[2], b, "'\''"); split(b[1], c, " "); print c[1] }')

        echo $commandstring|awk '{print $1}'
}


if [ -a $corefile ] ; then
        process=$(getprocess)
else
        echo You must provide a core file as an argument
        exit
fi

if [ -a $process ] ; then

echo Core was generated by $process

gdb $process \
                -c $corefile \
                --eval-command='set pagination off' \
                --eval-command="set logging file $outfile" \
                --eval-command='set logging on' \
                --eval-command='bt' \
                --eval-command='bt full' \
                --eval-command='thread apply all bt' \
                --eval-command='thread apply all bt full' \
                --eval-command='quit'

echo "Trace written to $outfile"

else
        echo "Can not find $process. This script my be broken, or the file disappeared."
        exit
fi