public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Mark Geisert <mark@maxrnd.com>
To: cygwin@cygwin.com
Subject: An increment to Jon Turney's stackdump2backtrace script
Date: Mon, 01 Oct 2018 09:21:00 -0000	[thread overview]
Message-ID: <af84c3ce-364e-e058-ac39-ef2713e06cda@maxrnd.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 177 bytes --]

For those Cygwin developers who tend to attract stackdump files...

..mark (who defines an alias 'bt' to launch the script because he can't get gdb 
out of his head)

h/t to JT

[-- Attachment #2: stackdump2backtrace --]
[-- Type: text/plain, Size: 2638 bytes --]

#!/bin/bash
#
#	stackdump2backtrace (incremented)
#	Munges a Cygwin stackdump file into a real backtrace
#
#	Based on nifty stackdump2backtrace script by Jon Turney, as
#	posted in https://sourceware.org/ml/cygwin/2015-08/msg00311.html
#
#	2018/10/01 increment by Mark A. Geisert

# The user provides the name of the stackdump file
STACKDUMP=$1
if [ -z "$STACKDUMP" ]; then
    echo Usage: $0 name-of-stackdump-file
    exit 1
fi
if [ ! -e "$STACKDUMP" ]; then
    echo cannot find stackdump file $STACKDUMP
    exit 1
fi

# Look for executable's best available stashed debug info
IMG1=/usr/lib/debug/usr/bin/${STACKDUMP%.stackdump}.dbg

# If no stashed debug info available for executable, read from EXE
if [ ! -e "$IMG1" ]; then
    IMG1=${STACKDUMP%.stackdump}

    # If we can't find the EXE, try to locate it with 'which'
    if [ ! -e "$IMG1" ]; then
	IMG1=`basename $IMG1`
	IMG1=`which $IMG1`
    fi
fi

# If no luck locating executable image file, bail
if [ ! -e "$IMG1" ]; then
    echo cannot find executable file $IMG1
    exit 1
fi

# Look for Cygwin DLL's best available stashed debug info
IMG2=/usr/lib/debug/usr/bin/cygwin1.dbg
if [ ! -e "$IMG2" ]; then
    IMG2="" # not found; just use what ldd locates later on
fi

# Construct list of image files to resolve addresses against
# Sorting into reverse order seems to improve address lookup performance
OTHERS=`ldd $IMG1 | awk '{print $3}' | sort -r | tr '\\n' ' '`
IMAGES="$IMG1 $IMG2 $OTHERS"

display() {
    declare -g IMAGES

    # look up address $1 in each known image; break on first success
    for img in $IMAGES
    do
	line=`addr2line -asfiC -e $img $1`
	echo $line | fgrep -q "??:0"
	if [ $? -ne 0 ]; then
	    break
	fi
    done

    # state machine for prettier display of addr2line output
    addr=""
    temp=""
    for word in $line
    do
	case $word in
	0x*)
	    addr=$word
	    temp=""
	    ;;
	\?\?:0 | *.*:*)
	    echo "$addr | $word | $temp"
	    addr="     or maybe     "
	    temp=""
	    ;;
	*)
	    temp+="$word "
	    ;;
	esac
    done
}

# Read and process each stack frame line of the stackdump file
grep "^Exception:" $STACKDUMP
awk '/^[0-9A-F][0-9A-F]/ {print $2}' $STACKDUMP | while read ADDR
do
    display $ADDR
done | column -o' ' -t -s'|'

# Deal with register-dump form of stackdump file. No frames here.
COUNT=`grep -c "^[0-9A-F][0-9A-F]" $STACKDUMP`
if [ X"$COUNT" = X0 ]; then
    ADDR=`grep "^Exception:" $STACKDUMP | awk '{print $4}' | sed 's/rip=/0x/'`
    if [ X"$ADDR" != X ]; then
	display $ADDR | column -o' ' -t -s'|'
    fi
    echo no stack frames present
fi

# And that's all the misinfotainment we have time for
exit 0

[-- Attachment #3: Type: text/plain, Size: 219 bytes --]


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

             reply	other threads:[~2018-10-01  9:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-01  9:21 Mark Geisert [this message]
2018-10-03 17:08 ` Jon Turney
2018-10-04  9:18   ` Mark Geisert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=af84c3ce-364e-e058-ac39-ef2713e06cda@maxrnd.com \
    --to=mark@maxrnd.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).