Skip to content

Instantly share code, notes, and snippets.

@kwbr
Forked from MatthewHannigan/gist:5210474
Created March 24, 2013 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwbr/5232698 to your computer and use it in GitHub Desktop.
Save kwbr/5232698 to your computer and use it in GitHub Desktop.
#!/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