Created
March 21, 2013 03:28
-
-
Save MatthewHannigan/5210474 to your computer and use it in GitHub Desktop.
atlassian gdb dump core script with modifications to use mktemp and trap to clean up untested :-)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 ([email protected]) | |
# 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
thought it be helpful for someone that lands here without seeing the article to have a link to the article: http://blogs.atlassian.com/2013/03/so-you-want-your-jvms-heap/