1
0
Fork 0
mirror of synced 2024-11-26 17:25:35 -05:00

cleanup trace function a bit

This commit is contained in:
Matt Hamilton 2015-12-27 19:33:30 -05:00
parent 50c8f70179
commit 5cccdc255b

View file

@ -3,16 +3,12 @@
# Generates trace log to debug zim and zsh issues # Generates trace log to debug zim and zsh issues
# #
cat >&2 <<EOF print "This function creates a trace log to debug
This function creates a trace log to debug
Zsh and Zim functionality. Zsh and Zim functionality.
It will copy your .zshrc to /tmp/ztrace/, ammend profiling It will copy your .zshrc to /tmp/ztrace/, ammend profiling
code, launch a new shell, log the trace, close the shell, code, launch a new shell, log the trace, close the shell,
archive the logs, and finally print the path to the archive. archive the logs, and finally print the path to the archive."
EOF
read \?"Press [Enter] to begin trace." read \?"Press [Enter] to begin trace."
@ -25,8 +21,8 @@ else
# check if known output file, if exists # check if known output file, if exists
# rm all directory contents # rm all directory contents
if [[ -e /tmp/ztrace/ztrace.log ]]; then if [[ -e /tmp/ztrace/ztrace.log ]]; then
print "\nLogs from previous run of trace-zim are present" print "\nLogs from previous run of trace-zim are present
print "Deleting old logs now..." Deleting old logs now..."
# use of anonymous function for dotglob opt # use of anonymous function for dotglob opt
() { () {
setopt dotglob setopt dotglob
@ -36,28 +32,28 @@ else
fi fi
# get some basic system information (kernel and zsh version) # get some basic system information (kernel and zsh version)
print 'Zsh version:' > /tmp/ztrace/sysinfo print "Zsh version:
print $(zsh --version) >> /tmp/ztrace/sysinfo $(zsh --version)
print "\nkernel information:" >> /tmp/ztrace/sysinfo Kernel information:
print $(uname -mosr) >> /tmp/ztrace/sysinfo $(uname -mosr)
print "\n$fpath info:" >> /tmp/ztrace/sysinfo fpath info:
print -l ${fpath} >> /tmp/ztrace/sysinfo ${fpath}" >! /tmp/ztrace/sysinfo
cp ${ZDOTDIR:-${HOME}}/.zshrc /tmp/ztrace/.zshrc.orig cp ${ZDOTDIR:-${HOME}}/.zshrc /tmp/ztrace/.zshrc.orig
cp ${ZDOTDIR:-${HOME}}/.zimrc /tmp/ztrace/.zimrc cp ${ZDOTDIR:-${HOME}}/.zimrc /tmp/ztrace/.zimrc
# rsync will allow us to not have to copy the .git folder; use if available # rsync will allow us to not have to copy the .git folder; use if available
if (( $+commands[rsync] )); then if (( ${+commands[rsync]} )); then
rsync -az --exclude .git "${ZDOTDIR:-$HOME}/.zim" "/tmp/ztrace/" rsync -az --exclude .git ${ZDOTDIR:-${HOME}}/.zim /tmp/ztrace/
else else
cp -R "${ZDOTDIR:-$HOME}/.zim" "/tmp/ztrace/" cp -R ${ZDOTDIR:-${HOME}}/.zim /tmp/ztrace/
fi fi
# trace code to add to modified .zshrc # trace code to add to modified .zshrc
if [[ "$OSTYPE" == linux-gnu ]]; then if [[ ${OSTYPE} == linux-gnu ]]; then
read -d '' tracetop << EOF || true read -d '' tracetop << EOF || true
####################### ###################
# zim trace start # # zim trace start #
####################### ###################
exec 3>&2 2> >(tee /tmp/ztrace/sample-time.\$\$.log | exec 3>&2 2> >(tee /tmp/ztrace/sample-time.\$\$.log |
sed -u 's/^.*$/now/' | sed -u 's/^.*$/now/' |
date -f - +%s.%N >/tmp/ztrace/sample-time.\$\$.tim) date -f - +%s.%N >/tmp/ztrace/sample-time.\$\$.tim)
@ -67,12 +63,12 @@ EOF
# we need gnu-utils to do this, so we must check for darwin/bsd. # we need gnu-utils to do this, so we must check for darwin/bsd.
# if these platforms, we need to call gsed and gdate explicitly. # if these platforms, we need to call gsed and gdate explicitly.
elif [[ "$OSTYPE" == (darwin*|*bsd*) ]]; then elif [[ ${OSTYPE} == (darwin*|*bsd*) ]]; then
if (( $+commands[gdate] && $+commands[gsed] )); then if (( ${+commands[gdate]} && ${+commands[gsed]} )); then
read -d '' tracetop << EOF || true read -d '' tracetop << EOF || true
####################### ###################
# zim trace start # # zim trace start #
####################### ###################
exec 3>&2 2> >(tee /tmp/ztrace/sample-time.\$\$.log | exec 3>&2 2> >(tee /tmp/ztrace/sample-time.\$\$.log |
gsed -u 's/^.*$/now/' | gsed -u 's/^.*$/now/' |
gdate -f - +%s.%N >/tmp/ztrace/sample-time.\$\$.tim) gdate -f - +%s.%N >/tmp/ztrace/sample-time.\$\$.tim)
@ -80,23 +76,23 @@ zmodload zsh/zprof
set -x set -x
EOF EOF
else else
print "debug module requires both gnu-sed (gsed) and gnu-date (gdate)." print "debug module requires both gnu-sed (gsed) and gnu-date (gdate).
print "please install these with brew before attempting to trace." Please install these with brew before attempting to trace."
return 1 return 1
fi fi
else else
print "your system, ${OSTYPE}, is an unknown system." print "your system, ${OSTYPE}, is an unknown system.
print "please create an issue at:" Please create an issue at:
print "\thttps://github.com/Eriner/zim/issues" https://github.com/Eriner/zim/issues
print "Please paste the following output in your report:" Include the following output in your report:
print "\t${OSTYPE} - $(uname -mosr)" ${OSTYPE} - $(uname -mosr)"
return 1 return 1
fi fi
read -d '' tracebot << EOF || true read -d '' tracebot << EOF || true
##################### #################
# zim trace end # # zim trace end #
##################### #################
set +x set +x
zprof >! /tmp/ztrace/zprof zprof >! /tmp/ztrace/zprof
@ -109,20 +105,18 @@ EOF
# create a modified .zshrc to produce a trace log # create a modified .zshrc to produce a trace log
origzshrc=$(</tmp/ztrace/.zshrc.orig) origzshrc=$(</tmp/ztrace/.zshrc.orig)
print "$tracetop\n$origzshrc\n$tracebot" >! "/tmp/ztrace/.zshrc" print "${tracetop}\n${origzshrc}\n${tracebot}" >! /tmp/ztrace/.zshrc
# clean up the vars now that we are done with them. # clean up the vars now that we are done with them.
unset origzshrc unset origzshrc tracetop tracebot
unset tracetop
unset tracebot
print "\nSpawning zsh and producing trace...\n\n" print "\nSpawning zsh and producing trace...\n\n"
ZDOTDIR=/tmp/ztrace zsh -ic 'exit' ZDOTDIR=/tmp/ztrace zsh -ic 'exit'
print "Trace complete.\n" print "Trace complete.
print "Parsing logs to a nicer format; this may take some time..." Parsing logs to a nicer format; this may take some time..."
# this is ugly thing makes it pretty... # this is ugly thing makes it pretty...
paste <( while read tim; do crt=000000000$((${tim//.}-10#0$last)); printf "%12.9f\n" ${crt:0:${#crt}-9}.${crt:${#crt}-9}; last=${tim//.}; done < /tmp/ztrace/sample-time.(*).tim; ) /tmp/ztrace/sample-time.(*).log > "/tmp/ztrace/ztrace.log" paste <( while read tim; do crt=000000000$((${tim//.}-10#0$last)); printf "%12.9f\n" ${crt:0:${#crt}-9}.${crt:${#crt}-9}; last=${tim//.}; done < /tmp/ztrace/sample-time.(*).tim; ) /tmp/ztrace/sample-time.(*).log > /tmp/ztrace/ztrace.log
print "Parsing complete!" print "Parsing complete!"
# safe to remove old, unneeded environment files # safe to remove old, unneeded environment files
@ -132,16 +126,15 @@ rm -rf /tmp/ztrace/.zim
rm -f /tmp/ztrace/.zshrc rm -f /tmp/ztrace/.zshrc
mv /tmp/ztrace/.zshrc.orig /tmp/ztrace/.zshrc mv /tmp/ztrace/.zshrc.orig /tmp/ztrace/.zshrc
rm -f /tmp/ztrace/.zhistory rm -f /tmp/ztrace/.zhistory
rm -f /tmp/ztrace/.zcompdump.zwc rm -f /tmp/ztrace/.zcompdump*
print "Archiving trace logs...\n" print "Archiving trace logs...\n"
tar -cf "/tmp/ztrace.tar.gz" "/tmp/ztrace/" tar -cf /tmp/ztrace.tar.gz /tmp/ztrace/
print "Archive complete!\n" print "Archive complete!\n
Trace by with execution time available at:
print "\nTrace by with execution time available at:" /tmp/ztrace/ztrace.log
print "\t/tmp/ztrace/ztrace.log" Archive (for sharing/help) available at:
print "\nArchive (for sharing/help) available at:" /tmp/ztrace.tar.gz"
print "\t/tmp/ztrace.tar.gz"