3 # Diagnostic and debugging support for oh-my-zsh
5 # omz_diagnostic_dump()
7 # Author: Andrew Janke <andrew@apjanke.net>
11 # omz_diagnostic_dump [-v] [-V] [file]
13 # NOTE: This is a work in progress. Its interface and behavior are going to change,
14 # and probably in non-back-compatible ways.
16 # Outputs a bunch of information about the state and configuration of
17 # oh-my-zsh, zsh, and the user's system. This is intended to provide a
18 # bunch of context for diagnosing your own or a third party's problems, and to
19 # be suitable for posting to public bug reports.
21 # The output is human-readable and its format may change over time. It is not
22 # suitable for parsing. All the output is in one single file so it can be posted
23 # as a gist or bug comment on GitHub. GitHub doesn't support attaching tarballs
24 # or other files to bugs; otherwise, this would probably have an option to produce
25 # tarballs that contain copies of the config and customization files instead of
26 # catting them all in to one file.
28 # This is intended to be widely portable, and run anywhere that oh-my-zsh does.
29 # Feel free to report any portability issues as bugs.
31 # This is written in a defensive style so it still works (and can detect) cases when
32 # basic functionality like echo and which have been redefined. In particular, almost
33 # everything is invoked with "builtin" or "command", to work in the face of user
38 # [file] Specifies the output file. If not given, a file in the current directory
39 # is selected automatically.
41 # -v Increase the verbosity of the dump output. May be specified multiple times.
43 # 0 - Basic info, shell state, omz configuration, git state
44 # 1 - (default) Adds key binding info and configuration file contents
45 # 2 - Adds zcompdump file contents
47 # -V Reduce the verbosity of the dump output. May be specified multiple times.
50 # * Multi-file capture
51 # * Add automatic gist uploading
52 # * Consider whether to move default output file location to TMPDIR. More robust
53 # but less user friendly.
56 autoload -Uz is-at-least
58 function omz_diagnostic_dump() {
61 builtin echo "Generating diagnostic dump; please be patient..."
63 local thisfcn=omz_diagnostic_dump
65 local opt_verbose opt_noverbose opt_outfile
66 local timestamp=$(date +%Y%m%d-%H%M%S)
67 local outfile=omz_diagdump_$timestamp.txt
68 builtin zparseopts -A opts -D -- "v+=opt_verbose" "V+=opt_noverbose"
69 local verbose n_verbose=${#opt_verbose} n_noverbose=${#opt_noverbose}
70 (( verbose = 1 + n_verbose - n_noverbose ))
72 if [[ ${#*} > 0 ]]; then
75 if [[ ${#*} > 1 ]]; then
76 builtin echo "$thisfcn: error: too many arguments" >&2
79 if [[ -n "$opt_outfile" ]]; then
80 outfile="$opt_outfile"
83 # Always write directly to a file so terminal escape sequences are
85 _omz_diag_dump_one_big_text &> "$outfile"
86 if [[ $? != 0 ]]; then
87 builtin echo "$thisfcn: error while creating diagnostic dump; see $outfile for details"
91 builtin echo Diagnostic dump file created at: "$outfile"
93 builtin echo To share this with OMZ developers, post it as a gist on GitHub
94 builtin echo at "https://gist.github.com" and share the link to the gist.
96 builtin echo "WARNING: This dump file contains all your zsh and omz configuration files,"
97 builtin echo "so don't share it publicly if there's sensitive information in them."
102 function _omz_diag_dump_one_big_text() {
103 local program programs progfile md5
105 builtin echo oh-my-zsh diagnostic dump
107 builtin echo $outfile
110 # Basic system and zsh information
113 builtin echo OSTYPE=$OSTYPE
114 builtin echo ZSH_VERSION=$ZSH_VERSION
115 builtin echo User: $USERNAME
116 builtin echo umask: $(umask)
118 _omz_diag_dump_os_specific_version
122 programs=(sh zsh ksh bash sed cat grep ls find git posh)
123 local progfile="" extra_str="" sha_str=""
124 for program in $programs; do
125 extra_str="" sha_str=""
126 progfile=$(builtin which $program)
127 if [[ $? == 0 ]]; then
128 if [[ -e $progfile ]]; then
129 if builtin whence shasum &>/dev/null; then
130 sha_str=($(command shasum $progfile))
132 extra_str+=" SHA $sha_str"
134 if [[ -h "$progfile" ]]; then
135 extra_str+=" ( -> ${progfile:A} )"
138 builtin printf '%-9s %-20s %s\n' "$program is" "$progfile" "$extra_str"
140 builtin echo "$program: not found"
144 builtin echo Command Versions:
145 builtin echo "zsh: $(zsh --version)"
146 builtin echo "this zsh session: $ZSH_VERSION"
147 builtin echo "bash: $(bash --version | command grep bash)"
148 builtin echo "git: $(git --version)"
149 builtin echo "grep: $(grep --version)"
152 # Core command definitions
153 _omz_diag_dump_check_core_commands || return 1
157 builtin echo Process state:
158 builtin echo pwd: $PWD
159 if builtin whence pstree &>/dev/null; then
160 builtin echo Process tree for this shell:
165 builtin set | command grep -a '^\(ZSH\|plugins\|TERM\|LC_\|LANG\|precmd\|chpwd\|preexec\|FPATH\|TTY\|DISPLAY\|PATH\)\|OMZ'
167 #TODO: Should this include `env` instead of or in addition to `export`?
168 builtin echo Exported:
169 builtin echo $(builtin export | command sed 's/=.*//')
175 # Zsh installation and configuration
176 builtin echo Zsh configuration:
177 builtin echo setopt: $(builtin setopt)
182 builtin echo 'compaudit output:'
185 builtin echo '$fpath directories:'
186 command ls -lad $fpath
189 # Oh-my-zsh installation
190 builtin echo oh-my-zsh installation:
192 command ls -ld ~/.oh*
194 builtin echo oh-my-zsh git state:
195 (builtin cd $ZSH && builtin echo "HEAD: $(git rev-parse HEAD)" && git remote -v && git status | command grep "[^[:space:]]")
196 if [[ $verbose -ge 1 ]]; then
197 (builtin cd $ZSH && git reflog --date=default | command grep pull)
200 if [[ -e $ZSH_CUSTOM ]]; then
201 local custom_dir=$ZSH_CUSTOM
202 if [[ -h $custom_dir ]]; then
203 custom_dir=$(builtin cd $custom_dir && pwd -P)
205 builtin echo "oh-my-zsh custom dir:"
206 builtin echo " $ZSH_CUSTOM ($custom_dir)"
207 (builtin cd ${custom_dir:h} && command find ${custom_dir:t} -name .git -prune -o -print)
211 # Key binding and terminal info
212 if [[ $verbose -ge 1 ]]; then
213 builtin echo "bindkey:"
216 builtin echo "infocmp:"
221 # Configuration file info
222 local zdotdir=${ZDOTDIR:-$HOME}
223 builtin echo "Zsh configuration files:"
224 local cfgfile cfgfiles
225 # Some files for bash that zsh does not use are intentionally included
226 # to help with diagnosing behavior differences between bash and zsh
227 cfgfiles=( /etc/zshenv /etc/zprofile /etc/zshrc /etc/zlogin /etc/zlogout
228 $zdotdir/.zshenv $zdotdir/.zprofile $zdotdir/.zshrc $zdotdir/.zlogin $zdotdir/.zlogout
230 /etc/bashrc /etc/profile ~/.bashrc ~/.profile ~/.bash_profile ~/.bash_logout )
231 command ls -lad $cfgfiles 2>&1
233 if [[ $verbose -ge 1 ]]; then
234 for cfgfile in $cfgfiles; do
235 _omz_diag_dump_echo_file_w_header $cfgfile
239 builtin echo "Zsh compdump files:"
240 local dumpfile dumpfiles
241 command ls -lad $zdotdir/.zcompdump*
242 dumpfiles=( $zdotdir/.zcompdump*(N) )
243 if [[ $verbose -ge 2 ]]; then
244 for dumpfile in $dumpfiles; do
245 _omz_diag_dump_echo_file_w_header $dumpfile
251 function _omz_diag_dump_check_core_commands() {
252 builtin echo "Core command check:"
253 local redefined name builtins externals reserved_words
255 # All the zsh non-module builtin commands
256 # These are taken from the zsh reference manual for 5.0.2
257 # Commands from modules should not be included.
258 # (For back-compatibility, if any of these are newish, they should be removed,
259 # or at least made conditional on the version of the current running zsh.)
260 # "history" is also excluded because OMZ is known to redefine that
261 reserved_words=( do done esac then elif else fi for case if while function
262 repeat time until select coproc nocorrect foreach end '!' '[[' '{' '}'
264 builtins=( alias autoload bg bindkey break builtin bye cd chdir command
265 comparguments compcall compctl compdescribe compfiles compgroups compquote comptags
266 comptry compvalues continue dirs disable disown echo echotc echoti emulate
267 enable eval exec exit false fc fg functions getln getopts hash
268 jobs kill let limit log logout noglob popd print printf
269 pushd pushln pwd r read rehash return sched set setopt shift
270 source suspend test times trap true ttyctl type ulimit umask unalias
271 unfunction unhash unlimit unset unsetopt vared wait whence where which zcompile
272 zle zmodload zparseopts zregexparse zstyle )
273 if is-at-least 5.1; then
274 reserved_word+=( declare export integer float local readonly typeset )
276 builtins+=( declare export integer float local readonly typeset )
278 builtins_fatal=( builtin command local )
280 for name in $reserved_words; do
281 if [[ $(builtin whence -w $name) != "$name: reserved" ]]; then
282 builtin echo "reserved word '$name' has been redefined"
287 for name in $builtins; do
288 if [[ $(builtin whence -w $name) != "$name: builtin" ]]; then
289 builtin echo "builtin '$name' has been redefined"
294 for name in $externals; do
295 if [[ $(builtin whence -w $name) != "$name: command" ]]; then
296 builtin echo "command '$name' has been redefined"
302 if [[ -n "$redefined" ]]; then
303 builtin echo "SOME CORE COMMANDS HAVE BEEN REDEFINED: $redefined"
305 builtin echo "All core commands are defined normally"
310 function _omz_diag_dump_echo_file_w_header() {
312 if [[ ( -f $file || -h $file ) ]]; then
313 builtin echo "========== $file =========="
314 if [[ -h $file ]]; then
315 builtin echo "========== ( => ${file:A} ) =========="
318 builtin echo "========== end $file =========="
320 elif [[ -d $file ]]; then
321 builtin echo "File '$file' is a directory"
322 elif [[ ! -e $file ]]; then
323 builtin echo "File '$file' does not exist"
325 command ls -lad "$file"
329 function _omz_diag_dump_os_specific_version() {
330 local osname osver version_file version_files
333 osname=$(command sw_vers -productName)
334 osver=$(command sw_vers -productVersion)
335 builtin echo "OS Version: $osname $osver build $(sw_vers -buildVersion)"
338 command systeminfo | command head -n 4 | command tail -n 2
342 if builtin which lsb_release >/dev/null; then
343 builtin echo "OS Release: $(command lsb_release -s -d)"
346 version_files=( /etc/*-release(N) /etc/*-version(N) /etc/*_version(N) )
347 for version_file in $version_files; do
348 builtin echo "$version_file:"
349 command cat "$version_file"