Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
atlassian gdb dump core script with modifications to use mktemp and trap to clean up untested :-)
# - dump a heap using GDB for a crashed application
# Accepts a single argument: the PID of the JVM
# Author: James Gray (
# Copyright Atlassian P/L
# License: MIT
# Are we root?
if [ $UID -ne 0 ]; then
echo "Be gone peon - you must be root"
exit 1
# Did we get a command line argument?
if [ -z $1 ]; then
# 1st command line arg is empty...dump usage and quit
echo "Must have a JVM PID to dump"
echo -e "eg,\n$(basename $0) \n"
exit 1
# create a temp dir to hold cores; /tmp might be too small so choose /var/tmp
mytmpdir=$(mktemp -p /var/tmp -d -t jvm.core.XXXXXX)
# clean up on exit
trap "rm -r $mytmpdir" 0
# OK, we have a PID, we are root...hit it
JVM_HEAP=$mytmpdir/application-name-$(date +'%Y%m%d').hprof
JMAP_OPTS="-dump:format=b,file=${JVM_HEAP} /usr/bin/java ${JVM_CORE}.${1}"
GCORE_OPTS="-o ${JVM_CORE} ${1}"
# Now run gdb and get the core:
echo "Dumping the core for PID: \"${1}\""
gcore ${GCORE_OPTS}
# Now get the heap and dump it to the preferred name:
echo "Core created at ${JVM_CORE}.${1} - YOU CAN NOW RESTART THE APPLICATION"
jmap ${JMAP_OPTS}
echo "Your JVM Heap is now available at: ${JVM_HEAP}"
# Go back to whence we came...
cd "${HERE}"

This comment has been minimized.

Copy link

mindscratch commented Aug 24, 2014

thought it be helpful for someone that lands here without seeing the article to have a link to the article:


This comment has been minimized.

Copy link
Owner Author

MatthewHannigan commented Oct 18, 2014

Thanks @mindscratch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.