diagnostics.zsh 11 KB

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