LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2008, 02:15 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
Question gdb: backtrace to file


Is it possible to send the backtrace from gdb to as file?
 
Old 11-28-2008, 03:11 PM   #2
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi eantoranz,

Several years ago I got an script on internet which opens the core file, prints the back trace and the program name on the standard output without getting the prompt. I used it to scan several core dumps and save the bt with the program name onto a file to identify problems on my customers. As a source is worth a thousand words, I'm posting the script here:
Code:
osvaldo@osvaldo:~$ cat /util/idcore
#!/bin/sh

# name:      idcore  --  identify which binary caused a core dump
# author:    c.l.s. (cspiel@physik.tu-muenchen.de)
# last rev.: 1998-01-22  11:14
# bash ver.: 1.14.7(1)
# $Id$

# display help message
# char* disp_help(void)
function disp_help
{
        echo "usage:"
        echo "  idcore [OPTION] [[COREDUMP] ...]"
        echo
        echo "  If COREDUMP is omitted the core file in the current"
        echo "  directory is used."
        echo
        echo "  -h, --help      display this help message"
        echo "  -v, --version   show version of idcore"
        echo "  -b, --brief     brief format, i.e. filename only"
        echo "  -l, --long      long format, with filename, signal, user,"
        echo "                  date, and backtrace"
}

# retrieve name binary that caused core dump via gdb
# char* get_name(const char* mode, const char* name)
function get_name
{
        case "$1" in
                brief)
                        echo q | gdb --quiet --core="$2" 2>&1 | head -1 | \
                        sed -ne "s/^.*\`\(.*\)'\.$/\1/g;s/ .*$//p"
                        ;;
                standard)
                        echo q | gdb --quiet --core="$2" 2>&1 | head -2
                        ;;
                long)
                        execp=$(idcore --brief $2)
                        echo $execp
                        if [ ! "$execp" = "" ] && [ -x $execp ]
                        then
                          dump=$(echo -e "where\nq" | \
                                gdb --quiet --core="$2" "$execp" 2>&1)
                        else
                          dump=$(echo -e "where\nq" | \
                                gdb --quiet --core="$2" 2>&1)
                        fi
                        echo "$dump" | head -2 | sed -ne '2,2s/\.$//p'
                        ls -l "$2" | \
                        awk '{ print "on", $6, $7, $8, "caused by", $3 }'
                        echo
                        echo "backtrace:"
                        echo "$dump" | sed -e 's/^(gdb) //p' | grep '^#' | awk '{ if ($0 != ant)
  {
    ant = $0
    print $0}
}'
                        ;;
        esac
}
#
# start of main
#
myname=$(basename "$0")         # name of shell-script
mode=standard                   # normal mode of operation

case "$1" in
        -h | --help)
                disp_help
                exit 1
                ;;
        -v | --version)
                echo "version 0.1.0"
                exit 0
                ;;
        -b | --brief)
                mode=brief
                shift
                ;;
        -l | --long)
                mode=long
                shift
                ;;
        -* | --*)
                echo "$myname: unknown option $1"
                exit 2
                ;;
esac

if [ -z "$1" ]; then
        # no argument -> look at core in the current directory
        get_name "$mode" core
else
        # process all arguments
        for c; do
                # echo file we are processing
                if [ "$mode" != "brief" ]; then
                        echo "$c: "
                fi
                get_name "$mode" "$c"
        done
fi
exit 0
Have fun!

Osvaldo.
 
  


Reply

Tags
gdb



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gdb: problem while debug an executable file smartgupta Programming 2 10-16-2008 04:51 AM
Help using gdb to provide a backtrace for kwin ? yossarianuk Linux - Software 0 04-03-2008 01:01 PM
gdb won't recognize a.out file format jakykong Programming 17 08-25-2007 05:21 AM
gdb breakpoints cant find source file AM1SHFURN1TURE Programming 4 01-14-2006 01:10 PM
How to interpret backtrace (gdb) jnusa Programming 1 12-06-2004 09:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration