Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# atlassian-heap-dump.sh - dump a heap using GDB for a crashed application
# Accepts a single argument: the PID of the JVM
# Author: James Gray (jgray@atlassian.com)
# Copyright Atlassian P/L
# License: MIT
# Are we root?
if [ $UID -ne 0 ]; then
echo "Be gone peon - you must be root"
exit 1
fi
# 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
fi
# 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_CORE=$mytmpdir/jvm.core
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}"
HERE="$(pwd)"
# 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}"
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.