1
0
Fork 0
mirror of synced 2024-11-13 19:48:58 -05:00
zimfw/modules/debug/functions/trace-zim

141 lines
4.1 KiB
Text
Raw Normal View History

2015-12-27 19:08:43 -05:00
#!/usr/bin/env zsh
#
# Generates trace log to debug zim and zsh issues
#
2015-12-27 19:33:30 -05:00
print "This function creates a trace log to debug
2015-12-27 19:08:43 -05:00
Zsh and Zim functionality.
It will copy your .zshrc to /tmp/ztrace/, ammend profiling
code, launch a new shell, log the trace, close the shell,
2015-12-27 19:33:30 -05:00
archive the logs, and finally print the path to the archive."
2015-12-27 19:08:43 -05:00
read \?"Press [Enter] to begin trace."
mkdir -p /tmp/ztrace
# make sure that we were able to create the directory
if [[ ! -d /tmp/ztrace ]]; then
print 'failed to create /tmp/ztrace directory. Aborting.'
return 1
else
# check if known output file, if exists
# rm all directory contents
if [[ -e /tmp/ztrace/ztrace.log ]]; then
2015-12-27 19:33:30 -05:00
print "\nLogs from previous run of trace-zim are present
Deleting old logs now..."
2015-12-27 19:08:43 -05:00
# use of anonymous function for dotglob opt
() {
setopt dotglob
rm -rf /tmp/ztrace/*
}
fi
fi
# get some basic system information (kernel and zsh version)
2015-12-27 19:33:30 -05:00
print "Zsh version:
$(zsh --version)
Kernel information:
$(uname -mosr)
fpath info:
${fpath}" >! /tmp/ztrace/sysinfo
2015-12-27 19:08:43 -05:00
cp ${ZDOTDIR:-${HOME}}/.zshrc /tmp/ztrace/.zshrc.orig
cp ${ZDOTDIR:-${HOME}}/.zimrc /tmp/ztrace/.zimrc
# rsync will allow us to not have to copy the .git folder; use if available
2015-12-27 19:33:30 -05:00
if (( ${+commands[rsync]} )); then
rsync -az --exclude .git ${ZDOTDIR:-${HOME}}/.zim /tmp/ztrace/
2015-12-27 19:08:43 -05:00
else
2015-12-27 19:33:30 -05:00
cp -R ${ZDOTDIR:-${HOME}}/.zim /tmp/ztrace/
2015-12-27 19:08:43 -05:00
fi
# trace code to add to modified .zshrc
2015-12-27 19:33:30 -05:00
if [[ ${OSTYPE} == linux-gnu ]]; then
2015-12-27 19:08:43 -05:00
read -d '' tracetop << EOF || true
2015-12-27 19:33:30 -05:00
###################
2015-12-27 19:08:43 -05:00
# zim trace start #
2015-12-27 19:33:30 -05:00
###################
2015-12-27 19:08:43 -05:00
exec 3>&2 2> >(tee /tmp/ztrace/sample-time.\$\$.log |
sed -u 's/^.*$/now/' |
date -f - +%s.%N >/tmp/ztrace/sample-time.\$\$.tim)
zmodload zsh/zprof
set -x
EOF
# 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.
2015-12-27 19:33:30 -05:00
elif [[ ${OSTYPE} == (darwin*|*bsd*) ]]; then
if (( ${+commands[gdate]} && ${+commands[gsed]} )); then
2015-12-27 19:08:43 -05:00
read -d '' tracetop << EOF || true
2015-12-27 19:33:30 -05:00
###################
2015-12-27 19:08:43 -05:00
# zim trace start #
2015-12-27 19:33:30 -05:00
###################
2015-12-27 19:08:43 -05:00
exec 3>&2 2> >(tee /tmp/ztrace/sample-time.\$\$.log |
gsed -u 's/^.*$/now/' |
gdate -f - +%s.%N >/tmp/ztrace/sample-time.\$\$.tim)
zmodload zsh/zprof
set -x
EOF
else
2015-12-27 19:33:30 -05:00
print "debug module requires both gnu-sed (gsed) and gnu-date (gdate).
Please install these with brew before attempting to trace."
2015-12-27 19:08:43 -05:00
return 1
fi
else
2015-12-27 19:33:30 -05:00
print "your system, ${OSTYPE}, is an unknown system.
Please create an issue at:
https://github.com/Eriner/zim/issues
Include the following output in your report:
${OSTYPE} - $(uname -mosr)"
2015-12-27 19:08:43 -05:00
return 1
fi
read -d '' tracebot << EOF || true
2015-12-27 19:33:30 -05:00
#################
2015-12-27 19:08:43 -05:00
# zim trace end #
2015-12-27 19:33:30 -05:00
#################
2015-12-27 19:08:43 -05:00
set +x
zprof >! /tmp/ztrace/zprof
#non-linux systems have weird fd; also, no real need to redirect back
#prompt is (practically speaking) non-interactive, fd exists only for that process
#which is closed (by typing exit)
#exec 2>&3 3>&-
EOF
# create a modified .zshrc to produce a trace log
origzshrc=$(</tmp/ztrace/.zshrc.orig)
2015-12-27 19:33:30 -05:00
print "${tracetop}\n${origzshrc}\n${tracebot}" >! /tmp/ztrace/.zshrc
2015-12-27 19:08:43 -05:00
# clean up the vars now that we are done with them.
2015-12-27 19:33:30 -05:00
unset origzshrc tracetop tracebot
2015-12-27 19:08:43 -05:00
print "\nSpawning zsh and producing trace...\n\n"
ZDOTDIR=/tmp/ztrace zsh -ic 'exit'
2015-12-27 19:33:30 -05:00
print "Trace complete.
Parsing logs to a nicer format; this may take some time..."
2015-12-27 19:08:43 -05:00
# this is ugly thing makes it pretty...
2015-12-27 19:33:30 -05:00
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
2015-12-27 19:08:43 -05:00
print "Parsing complete!"
# safe to remove old, unneeded environment files
print "Tidying up before archive..."
rm -f /tmp/ztrace/sample-time.*
rm -rf /tmp/ztrace/.zim
rm -f /tmp/ztrace/.zshrc
mv /tmp/ztrace/.zshrc.orig /tmp/ztrace/.zshrc
rm -f /tmp/ztrace/.zhistory
2015-12-27 19:33:30 -05:00
rm -f /tmp/ztrace/.zcompdump*
2015-12-27 19:08:43 -05:00
print "Archiving trace logs...\n"
2015-12-27 19:33:30 -05:00
tar -cf /tmp/ztrace.tar.gz /tmp/ztrace/
2015-12-27 19:08:43 -05:00
2015-12-27 19:33:30 -05:00
print "Archive complete!\n
Trace by with execution time available at:
/tmp/ztrace/ztrace.log
Archive (for sharing/help) available at:
/tmp/ztrace.tar.gz"
2015-12-27 19:08:43 -05:00