diagnostics.zsh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # diagnostics.zsh
  2. #
  3. # Diagnostic and debugging support for oh-my-zsh
  4. # omz_diagnostic_dump()
  5. #
  6. # Author: Andrew Janke <andrew@apjanke.net>
  7. #
  8. # Usage:
  9. #
  10. # omz_diagnostic_dump [-v] [-V] [file]
  11. #
  12. # NOTE: This is a work in progress. Its interface and behavior are going to change,
  13. # and probably in non-back-compatible ways.
  14. #
  15. # Outputs a bunch of information about the state and configuration of
  16. # oh-my-zsh, zsh, and the user's system. This is intended to provide a
  17. # bunch of context for diagnosing your own or a third party's problems, and to
  18. # be suitable for posting to public bug reports.
  19. #
  20. # The output is human-readable and its format may change over time. It is not
  21. # suitable for parsing. All the output is in one single file so it can be posted
  22. # as a gist or bug comment on GitHub. GitHub doesn't support attaching tarballs
  23. # or other files to bugs; otherwise, this would probably have an option to produce
  24. # tarballs that contain copies of the config and customization files instead of
  25. # catting them all in to one file.
  26. #
  27. # This is intended to be widely portable, and run anywhere that oh-my-zsh does.
  28. # Feel free to report any portability issues as bugs.
  29. #
  30. # This is written in a defensive style so it still works (and can detect) cases when
  31. # basic functionality like echo and which have been redefined. In particular, almost
  32. # everything is invoked with "builtin" or "command", to work in the face of user
  33. # redefinitions.
  34. #
  35. # OPTIONS
  36. #
  37. # [file] Specifies the output file. If not given, a file in the current directory
  38. # is selected automatically.
  39. #
  40. # -v Increase the verbosity of the dump output. May be specified multiple times.
  41. # Verbosity levels:
  42. # 0 - Basic info, shell state, omz configuration, git state
  43. # 1 - (default) Adds key binding info and configuration file contents
  44. # 2 - Adds zcompdump file contents
  45. #
  46. # -V Reduce the verbosity of the dump output. May be specified multiple times.
  47. #
  48. # TODO:
  49. # * Multi-file capture
  50. # * Add automatic gist uploading
  51. # * Consider whether to move default output file location to TMPDIR. More robust
  52. # but less user friendly.
  53. #
  54. function omz_diagnostic_dump() {
  55. emulate -L zsh
  56. local thisfcn=omz_diagnostic_dump
  57. local -A opts
  58. local opt_verbose opt_noverbose opt_outfile
  59. local timestamp=$(date +%Y%m%d-%H%M%S)
  60. local outfile=omz_diagdump_$timestamp.txt
  61. builtin zparseopts -A opts -D -- "v+=opt_verbose" "V+=opt_noverbose"
  62. local verbose n_verbose=${#opt_verbose} n_noverbose=${#opt_noverbose}
  63. (( verbose = 1 + n_verbose - n_noverbose ))
  64. if [[ ${#*} > 0 ]]; then
  65. opt_outfile=$1
  66. fi
  67. if [[ ${#*} > 1 ]]; then
  68. builtin echo "$thisfcn: error: too many arguments" >&2
  69. return 1
  70. fi
  71. if [[ -n "$opt_outfile" ]]; then
  72. outfile="$opt_outfile"
  73. fi
  74. # Always write directly to a file so terminal escape sequences are
  75. # captured cleanly
  76. _omz_diag_dump_one_big_text &> "$outfile"
  77. if [[ $? != 0 ]]; then
  78. builtin echo "$thisfcn: error while creating diagnostic dump; see $outfile for details"
  79. fi
  80. builtin echo
  81. builtin echo Diagnostic dump file created at: "$outfile"
  82. builtin echo
  83. builtin echo To share this with OMZ developers, post it as a gist on GitHub
  84. builtin echo at "https://gist.github.com" and share the link to the gist.
  85. builtin echo
  86. builtin echo "WARNING: This dump file contains all your zsh and omz configuration files,"
  87. builtin echo "so don't share it publicly if there's sensitive information in them."
  88. builtin echo
  89. }
  90. function _omz_diag_dump_one_big_text() {
  91. local program programs progfile md5
  92. builtin echo oh-my-zsh diagnostic dump
  93. builtin echo
  94. # Basic system and zsh information
  95. command date
  96. command uname -a
  97. builtin echo OSTYPE=$OSTYPE
  98. builtin echo ZSH_VERSION=$ZSH_VERSION
  99. builtin echo User: $USER
  100. builtin echo
  101. # Installed programs
  102. programs=(sh zsh ksh bash sed cat grep ls find git posh)
  103. for program in $programs; do
  104. local md5_str="" md5="" link_str="" extra_str=""
  105. progfile=$(builtin which $program)
  106. if [[ $? == 0 ]]; then
  107. if [[ -e $progfile ]]; then
  108. if builtin whence md5 &>/dev/null; then
  109. extra_str+=" $(md5 -q $progfile)"
  110. fi
  111. if [[ -h "$progfile" ]]; then
  112. extra_str+=" ( -> ${progfile:A} )"
  113. fi
  114. fi
  115. builtin printf '%-9s %-20s %s\n' "$program is" "$progfile" "$extra_str"
  116. else
  117. builtin echo "$program: not found"
  118. fi
  119. done
  120. builtin echo
  121. builtin echo Command Versions:
  122. builtin echo "zsh: $(zsh --version)"
  123. builtin echo "this zsh session: $ZSH_VERSION"
  124. builtin echo "bash: $(bash --version | command grep bash)"
  125. builtin echo "git: $(git --version)"
  126. builtin echo "grep: $(grep --version)"
  127. builtin echo
  128. # Core command definitions
  129. _omz_diag_dump_check_core_commands || return 1
  130. builtin echo
  131. # ZSH Process state
  132. builtin echo Process state:
  133. builtin echo pwd: $PWD
  134. if builtin whence pstree &>/dev/null; then
  135. builtin echo Process tree for this shell:
  136. pstree -p $$
  137. else
  138. ps -fT
  139. fi
  140. builtin set | command grep -a '^\(ZSH\|plugins\|TERM\|LC_\|LANG\|precmd\|chpwd\|preexec\|FPATH\|TTY\|DISPLAY\|PATH\)\|OMZ'
  141. builtin echo
  142. #TODO: Should this include `env` instead of or in addition to `export`?
  143. builtin echo Exported:
  144. builtin echo $(builtin export | command sed 's/=.*//')
  145. builtin echo
  146. builtin echo Locale:
  147. command locale
  148. builtin echo
  149. # Zsh configuration
  150. builtin echo Zsh configuration:
  151. builtin echo setopt: $(builtin setopt)
  152. builtin echo
  153. builtin echo zstyle:
  154. builtin zstyle
  155. builtin echo
  156. # Oh-my-zsh installation
  157. builtin echo oh-my-zsh installation:
  158. command ls -ld ~/.z*
  159. command ls -ld ~/.oh*
  160. builtin echo
  161. builtin echo oh-my-zsh git state:
  162. (cd $ZSH && builtin echo "HEAD: $(git rev-parse HEAD)" && git remote -v && git status | command grep "[^[:space:]]")
  163. if [[ $verbose -ge 1 ]]; then
  164. (cd $ZSH && git reflog --date=default | command grep pull)
  165. fi
  166. builtin echo
  167. if [[ -e $ZSH_CUSTOM ]]; then
  168. local custom_dir=$ZSH_CUSTOM
  169. if [[ -h $custom_dir ]]; then
  170. custom_dir=$(cd $custom_dir && pwd -P)
  171. fi
  172. builtin echo "oh-my-zsh custom dir:"
  173. builtin echo " $ZSH_CUSTOM ($custom_dir)"
  174. (cd ${custom_dir:h} && command find ${custom_dir:t} -name .git -prune -o -print)
  175. builtin echo
  176. fi
  177. # Key binding and terminal info
  178. if [[ $verbose -ge 1 ]]; then
  179. builtin echo "bindkey:"
  180. builtin bindkey
  181. builtin echo
  182. builtin echo "infocmp:"
  183. command infocmp
  184. builtin echo
  185. fi
  186. # Configuration file info
  187. local zdotdir=${ZDOTDIR:-$HOME}
  188. builtin echo "Zsh configuration files:"
  189. local cfgfile cfgfiles
  190. # Some files for bash that zsh does not use are intentionally included
  191. # to help with diagnosing behavior differences between bash and zsh
  192. cfgfiles=( /etc/zshenv /etc/zprofile /etc/zshrc /etc/zlogin /etc/zlogout
  193. $zdotdir/.zshenv $zdotdir/.zprofile $zdotdir/.zshrc $zdotdir/.zlogin $zdotdir/.zlogout
  194. ~/.zsh-pre-oh-my-zsh
  195. /etc/bashrc /etc/profile ~/.bashrc ~/.profile ~/.bash_profile ~/.bash_logout )
  196. command ls -lad $cfgfiles 2>&1
  197. builtin echo
  198. if [[ $verbose -ge 1 ]]; then
  199. for cfgfile in $cfgfiles; do
  200. _omz_diag_dump_echo_file_w_header $cfgfile
  201. done
  202. fi
  203. builtin echo
  204. builtin echo "Zsh compdump files:"
  205. local dumpfile dumpfiles
  206. command ls -lad $zdotdir/.zcompdump*
  207. dumpfiles=( $zdotdir/.zcompdump*(N) )
  208. if [[ $verbose -ge 2 ]]; then
  209. for dumpfile in $dumpfiles; do
  210. _omz_diag_dump_echo_file_w_header $dumpfile
  211. done
  212. fi
  213. }
  214. function _omz_diag_dump_check_core_commands() {
  215. builtin echo "Core command check:"
  216. local redefined name builtins externals
  217. redefined=()
  218. # All the zsh non-module builtin commands
  219. # These are taken from the zsh reference manual for 5.0.2
  220. # Commands from modules should not be included.
  221. # (For back-compatibility, if any of these are newish, they should be removed,
  222. # or at least made conditional on the version of the current running zsh.)
  223. # "history" is also excluded because OMZ is known to redefine that
  224. builtins=( alias autoload bg bindkey break builtin bye cd chdir command
  225. comparguments compcall compctl compdescribe compfiles compgroups compquote comptags
  226. comptry compvalues continue declare dirs disable disown echo echotc echoti emulate
  227. enable eval exec exit export false fc fg float functions getln getopts hash
  228. integer jobs kill let limit local log logout noglob popd print printf
  229. pushd pushln pwd r read readonly rehash return sched set setopt shift
  230. source suspend test times trap true ttyctl type typeset ulimit umask unalias
  231. unfunction unhash unlimit unset unsetopt vared wait whence where which zcompile
  232. zle zmodload zparseopts zregexparse zstyle )
  233. builtins_fatal=( builtin command local )
  234. externals=( zsh )
  235. for name in $builtins; do
  236. if [[ $(builtin whence -w $name) != "$name: builtin" ]]; then
  237. builtin echo "builtin '$name' has been redefined"
  238. builtin which $name
  239. redefined+=$name
  240. fi
  241. done
  242. for name in $externals; do
  243. if [[ $(builtin whence -w $name) != "$name: command" ]]; then
  244. builtin echo "command '$name' has been redefined"
  245. builtin which $name
  246. redefined+=$name
  247. fi
  248. done
  249. if [[ -n "$redefined" ]]; then
  250. builtin echo "SOME CORE COMMANDS HAVE BEEN REDEFINED: $redefined"
  251. else
  252. builtin echo "All core commands are defined normally"
  253. fi
  254. }
  255. function _omz_diag_dump_echo_file_w_header() {
  256. local file=$1
  257. if [[ ( -f $file || -h $file ) ]]; then
  258. builtin echo "========== $file =========="
  259. if [[ -h $file ]]; then
  260. builtin echo "========== ( => ${file:A} ) =========="
  261. fi
  262. command cat $file
  263. builtin echo "========== end $file =========="
  264. builtin echo
  265. elif [[ -d $file ]]; then
  266. builtin echo "File '$file' is a directory"
  267. elif [[ ! -e $file ]]; then
  268. builtin echo "File '$file' does not exist"
  269. else
  270. command ls -lad "$file"
  271. fi
  272. }