GDB Tricks: stack trace on a SIGSEGV

One thing I commonly use GDB for is getting a stack trace after a program I am working on sigfaults

I added this to my .bashrc

gcatch(){
  gdb -ex 'r' -ex 'back' -ex 'q' --args $*
}

So if my program segfaults I can press up and prepend gcatch to the command and voila! Stack trace.

The name gcatch comes from the frysk command line utility fcatch which performs the same job.

Advertisement

4 Responses to “GDB Tricks: stack trace on a SIGSEGV”

  1. Josh Stone Says:

    Nice tip! The $* will have issues if the command needs quoting though. I think “$@” will work better, or just use an alias so the args are implicitly appended as originally quoted:

    alias gcatch=’gdb -ex r -ex back -ex q –args’

  2. wagiaalla Says:

    Cool… alias it is :)

  3. Colin Says:

    Nice! I’d actually wondered how to do this. We should just stick this in the gdb package =)

  4. Casey Dahlin Says:

    you could also type gcatch !! instead of the press up/prepend dance.

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.