trace-zim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env zsh
  2. #
  3. # Generates trace log to debug zim and zsh issues
  4. #
  5. print "This function creates a trace log to debug
  6. Zsh and Zim functionality.
  7. It will copy your .zshrc to /tmp/ztrace/, ammend profiling
  8. code, launch a new shell, log the trace, close the shell,
  9. archive the logs, and finally print the path to the archive."
  10. read \?"Press [Enter] to begin trace."
  11. mkdir -p /tmp/ztrace
  12. # make sure that we were able to create the directory
  13. if [[ ! -d /tmp/ztrace ]]; then
  14. print 'failed to create /tmp/ztrace directory. Aborting.'
  15. return 1
  16. else
  17. # check if known output file, if exists
  18. # rm all directory contents
  19. if [[ -e /tmp/ztrace/ztrace.log ]]; then
  20. print "\nLogs from previous run of trace-zim are present
  21. Deleting old logs now..."
  22. # use of anonymous function for dotglob opt
  23. () {
  24. setopt dotglob
  25. rm -rf /tmp/ztrace/*
  26. }
  27. fi
  28. fi
  29. # get some basic system information (kernel and zsh version)
  30. print "Zsh version:
  31. $(zsh --version)
  32. Kernel information:
  33. $(uname -a)
  34. fpath info:
  35. ${fpath}" >! /tmp/ztrace/sysinfo
  36. cp ${ZDOTDIR:-${HOME}}/.zshrc /tmp/ztrace/.zshrc.orig
  37. cp ${ZDOTDIR:-${HOME}}/.zimrc /tmp/ztrace/.zimrc
  38. # rsync will allow us to not have to copy the .git folder; use if available
  39. if (( ${+commands[rsync]} )); then
  40. rsync -az --exclude .git ${ZIM_HOME} /tmp/ztrace/
  41. else
  42. cp -R ${ZIM_HOME} /tmp/ztrace/
  43. fi
  44. # create a modified .zshrc to produce a trace log
  45. cat <<EOF >! /tmp/ztrace/.zshrc
  46. ###################
  47. # zim trace start #
  48. ###################
  49. PS4=$'%D{%s%6.}-_-'
  50. exec 3>&2 2>/tmp/ztrace/sample-time.$$.log
  51. zmodload zsh/zprof
  52. setopt xtrace prompt_subst
  53. EOF
  54. cat /tmp/ztrace/.zshrc.orig >>! /tmp/ztrace/.zshrc
  55. cat <<EOF >>! /tmp/ztrace/.zshrc
  56. #################
  57. # zim trace end #
  58. #################
  59. unsetopt xtrace
  60. zprof >! /tmp/ztrace/zprof
  61. #non-linux systems have weird fd; also, no real need to redirect back
  62. #prompt is (practically speaking) non-interactive, fd exists only for that process
  63. #which is closed (by typing exit)
  64. #exec 2>&3 3>&-
  65. EOF
  66. print "\nSpawning zsh and producing trace...\n\n"
  67. ZDOTDIR=/tmp/ztrace zsh -ic 'exit'
  68. print "Trace complete.
  69. Parsing logs to a nicer format; this may take some time..."
  70. # this is ugly thing makes it pretty...
  71. while read line; do if [[ ${line} =~ '^[0-9]+-_-' ]]; then crt=000000$((${line%%-_-*}-10#0$last)); printf "%12.9f %s\n" ${crt:0:${#crt}-6}.${crt:${#crt}-6} ${line#*-_-}; last=${line%%-_-*}; fi; done < /tmp/ztrace/sample-time.(*).log > /tmp/ztrace/ztrace.log
  72. print "Parsing complete!"
  73. # safe to remove old, unneeded environment files
  74. print "Tidying up before archive..."
  75. rm -f /tmp/ztrace/sample-time.*
  76. rm -rf /tmp/ztrace/.zim
  77. rm -f /tmp/ztrace/.zshrc
  78. mv /tmp/ztrace/.zshrc.orig /tmp/ztrace/.zshrc
  79. rm -f /tmp/ztrace/.zhistory
  80. rm -f /tmp/ztrace/.zcompdump*
  81. print "Archiving trace logs...\n"
  82. tar -czf /tmp/ztrace.tar.gz /tmp/ztrace/
  83. print "Archive complete!\n
  84. Trace by with execution time available at:
  85. /tmp/ztrace/ztrace.log
  86. Archive (for sharing/help) available at:
  87. /tmp/ztrace.tar.gz"