diagnostics.zsh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. builtin echo "Generating diagnostic dump; please be patient..."
  57. local thisfcn=omz_diagnostic_dump
  58. local -A opts
  59. local opt_verbose opt_noverbose opt_outfile
  60. local timestamp=$(date +%Y%m%d-%H%M%S)
  61. local outfile=omz_diagdump_$timestamp.txt
  62. builtin zparseopts -A opts -D -- "v+=opt_verbose" "V+=opt_noverbose"
  63. local verbose n_verbose=${#opt_verbose} n_noverbose=${#opt_noverbose}
  64. (( verbose = 1 + n_verbose - n_noverbose ))
  65. if [[ ${#*} > 0 ]]; then
  66. opt_outfile=$1
  67. fi
  68. if [[ ${#*} > 1 ]]; then
  69. builtin echo "$thisfcn: error: too many arguments" >&2
  70. return 1
  71. fi
  72. if [[ -n "$opt_outfile" ]]; then
  73. outfile="$opt_outfile"
  74. fi
  75. # Always write directly to a file so terminal escape sequences are
  76. # captured cleanly
  77. _omz_diag_dump_one_big_text &> "$outfile"
  78. if [[ $? != 0 ]]; then
  79. builtin echo "$thisfcn: error while creating diagnostic dump; see $outfile for details"
  80. fi
  81. builtin echo
  82. builtin echo Diagnostic dump file created at: "$outfile"
  83. builtin echo
  84. builtin echo To share this with OMZ developers, post it as a gist on GitHub
  85. builtin echo at "https://gist.github.com" and share the link to the gist.
  86. builtin echo
  87. builtin echo "WARNING: This dump file contains all your zsh and omz configuration files,"
  88. builtin echo "so don't share it publicly if there's sensitive information in them."
  89. builtin echo
  90. }
  91. function _omz_diag_dump_one_big_text() {
  92. local program programs progfile md5
  93. builtin echo oh-my-zsh diagnostic dump
  94. builtin echo
  95. builtin echo $outfile
  96. builtin echo
  97. # Basic system and zsh information
  98. command date
  99. command uname -a
  100. builtin echo OSTYPE=$OSTYPE
  101. builtin echo ZSH_VERSION=$ZSH_VERSION
  102. builtin echo User: $USER
  103. builtin echo umask: $(umask)
  104. builtin echo
  105. _omz_diag_dump_os_specific_version
  106. builtin echo
  107. # Installed programs
  108. programs=(sh zsh ksh bash sed cat grep ls find git posh)
  109. local progfile="" extra_str="" sha_str=""
  110. for program in $programs; do
  111. extra_str="" sha_str=""
  112. progfile=$(builtin which $program)
  113. if [[ $? == 0 ]]; then
  114. if [[ -e $progfile ]]; then
  115. if builtin whence shasum &>/dev/null; then
  116. sha_str=($(command shasum $progfile))
  117. sha_str=$sha_str[1]
  118. extra_str+=" SHA $sha_str"
  119. fi
  120. if [[ -h "$progfile" ]]; then
  121. extra_str+=" ( -> ${progfile:A} )"
  122. fi
  123. fi
  124. builtin printf '%-9s %-20s %s\n' "$program is" "$progfile" "$extra_str"
  125. else
  126. builtin echo "$program: not found"
  127. fi
  128. done
  129. builtin echo
  130. builtin echo Command Versions:
  131. builtin echo "zsh: $(zsh --version)"
  132. builtin echo "this zsh session: $ZSH_VERSION"
  133. builtin echo "bash: $(bash --version | command grep bash)"
  134. builtin echo "git: $(git --version)"
  135. builtin echo "grep: $(grep --version)"
  136. builtin echo
  137. # Core command definitions
  138. _omz_diag_dump_check_core_commands || return 1
  139. builtin echo
  140. # ZSH Process state
  141. builtin echo Process state:
  142. builtin echo pwd: $PWD
  143. if builtin whence pstree &>/dev/null; then
  144. builtin echo Process tree for this shell:
  145. pstree -p $$
  146. else
  147. ps -fT
  148. fi
  149. builtin set | command grep -a '^\(ZSH\|plugins\|TERM\|LC_\|LANG\|precmd\|chpwd\|preexec\|FPATH\|TTY\|DISPLAY\|PATH\)\|OMZ'
  150. builtin echo
  151. #TODO: Should this include `env` instead of or in addition to `export`?
  152. builtin echo Exported:
  153. builtin echo $(builtin export | command sed 's/=.*//')
  154. builtin echo
  155. builtin echo Locale:
  156. command locale
  157. builtin echo
  158. # Zsh installation and configuration
  159. builtin echo Zsh configuration:
  160. builtin echo setopt: $(builtin setopt)
  161. builtin echo
  162. builtin echo zstyle:
  163. builtin zstyle
  164. builtin echo
  165. builtin echo 'compaudit output:'
  166. compaudit
  167. builtin echo
  168. builtin echo '$fpath directories:'
  169. command ls -lad $fpath
  170. builtin echo
  171. # Oh-my-zsh installation
  172. builtin echo oh-my-zsh installation:
  173. command ls -ld ~/.z*
  174. command ls -ld ~/.oh*
  175. builtin echo
  176. builtin echo oh-my-zsh git state:
  177. (cd $ZSH && builtin echo "HEAD: $(git rev-parse HEAD)" && git remote -v && git status | command grep "[^[:space:]]")
  178. if [[ $verbose -ge 1 ]]; then
  179. (cd $ZSH && git reflog --date=default | command grep pull)
  180. fi
  181. builtin echo
  182. if [[ -e $ZSH_CUSTOM ]]; then
  183. local custom_dir=$ZSH_CUSTOM
  184. if [[ -h $custom_dir ]]; then
  185. custom_dir=$(cd $custom_dir && pwd -P)
  186. fi
  187. builtin echo "oh-my-zsh custom dir:"
  188. builtin echo " $ZSH_CUSTOM ($custom_dir)"
  189. (cd ${custom_dir:h} && command find ${custom_dir:t} -name .git -prune -o -print)
  190. builtin echo
  191. fi
  192. # Key binding and terminal info
  193. if [[ $verbose -ge 1 ]]; then
  194. builtin echo "bindkey:"
  195. builtin bindkey
  196. builtin echo
  197. builtin echo "infocmp:"
  198. command infocmp -L
  199. builtin echo
  200. fi
  201. # Configuration file info
  202. local zdotdir=${ZDOTDIR:-$HOME}
  203. builtin echo "Zsh configuration files:"
  204. local cfgfile cfgfiles
  205. # Some files for bash that zsh does not use are intentionally included
  206. # to help with diagnosing behavior differences between bash and zsh
  207. cfgfiles=( /etc/zshenv /etc/zprofile /etc/zshrc /etc/zlogin /etc/zlogout
  208. $zdotdir/.zshenv $zdotdir/.zprofile $zdotdir/.zshrc $zdotdir/.zlogin $zdotdir/.zlogout
  209. ~/.zsh.pre-oh-my-zsh
  210. /etc/bashrc /etc/profile ~/.bashrc ~/.profile ~/.bash_profile ~/.bash_logout )
  211. command ls -lad $cfgfiles 2>&1
  212. builtin echo
  213. if [[ $verbose -ge 1 ]]; then
  214. for cfgfile in $cfgfiles; do
  215. _omz_diag_dump_echo_file_w_header $cfgfile
  216. done
  217. fi
  218. builtin echo
  219. builtin echo "Zsh compdump files:"
  220. local dumpfile dumpfiles
  221. command ls -lad $zdotdir/.zcompdump*
  222. dumpfiles=( $zdotdir/.zcompdump*(N) )
  223. if [[ $verbose -ge 2 ]]; then
  224. for dumpfile in $dumpfiles; do
  225. _omz_diag_dump_echo_file_w_header $dumpfile
  226. done
  227. fi
  228. }
  229. function _omz_diag_dump_check_core_commands() {
  230. builtin echo "Core command check:"
  231. local redefined name builtins externals
  232. redefined=()
  233. # All the zsh non-module builtin commands
  234. # These are taken from the zsh reference manual for 5.0.2
  235. # Commands from modules should not be included.
  236. # (For back-compatibility, if any of these are newish, they should be removed,
  237. # or at least made conditional on the version of the current running zsh.)
  238. # "history" is also excluded because OMZ is known to redefine that
  239. builtins=( alias autoload bg bindkey break builtin bye cd chdir command
  240. comparguments compcall compctl compdescribe compfiles compgroups compquote comptags
  241. comptry compvalues continue declare dirs disable disown echo echotc echoti emulate
  242. enable eval exec exit export false fc fg float functions getln getopts hash
  243. integer jobs kill let limit local log logout noglob popd print printf
  244. pushd pushln pwd r read readonly rehash return sched set setopt shift
  245. source suspend test times trap true ttyctl type typeset ulimit umask unalias
  246. unfunction unhash unlimit unset unsetopt vared wait whence where which zcompile
  247. zle zmodload zparseopts zregexparse zstyle )
  248. builtins_fatal=( builtin command local )
  249. externals=( zsh )
  250. for name in $builtins; do
  251. if [[ $(builtin whence -w $name) != "$name: builtin" ]]; then
  252. builtin echo "builtin '$name' has been redefined"
  253. builtin which $name
  254. redefined+=$name
  255. fi
  256. done
  257. for name in $externals; do
  258. if [[ $(builtin whence -w $name) != "$name: command" ]]; then
  259. builtin echo "command '$name' has been redefined"
  260. builtin which $name
  261. redefined+=$name
  262. fi
  263. done
  264. if [[ -n "$redefined" ]]; then
  265. builtin echo "SOME CORE COMMANDS HAVE BEEN REDEFINED: $redefined"
  266. else
  267. builtin echo "All core commands are defined normally"
  268. fi
  269. }
  270. function _omz_diag_dump_echo_file_w_header() {
  271. local file=$1
  272. if [[ ( -f $file || -h $file ) ]]; then
  273. builtin echo "========== $file =========="
  274. if [[ -h $file ]]; then
  275. builtin echo "========== ( => ${file:A} ) =========="
  276. fi
  277. command cat $file
  278. builtin echo "========== end $file =========="
  279. builtin echo
  280. elif [[ -d $file ]]; then
  281. builtin echo "File '$file' is a directory"
  282. elif [[ ! -e $file ]]; then
  283. builtin echo "File '$file' does not exist"
  284. else
  285. command ls -lad "$file"
  286. fi
  287. }
  288. function _omz_diag_dump_os_specific_version() {
  289. local osname osver version_file version_files
  290. case "$OSTYPE" in
  291. darwin*)
  292. osname=$(command sw_vers -productName)
  293. osver=$(command sw_vers -productVersion)
  294. builtin echo "OS Version: $osname $osver build $(sw_vers -buildVersion)"
  295. ;;
  296. cygwin)
  297. command systeminfo | command head -4 | command tail -2
  298. ;;
  299. esac
  300. if builtin which lsb_release >/dev/null; then
  301. builtin echo "OS Release: $(command lsb_release -s -d)"
  302. fi
  303. version_files=( /etc/*-release(N) /etc/*-version(N) /etc/*_version(N) )
  304. for version_file in $version_files; do
  305. builtin echo "$version_file:"
  306. command cat "$version_file"
  307. builtin echo
  308. done
  309. }