Skip to content

Instantly share code, notes, and snippets.

@ben0x539
Created February 26, 2017 16:55
Show Gist options
  • Save ben0x539/2e91305c75ff251782a32512d0d0ffac to your computer and use it in GitHub Desktop.
Save ben0x539/2e91305c75ff251782a32512d0d0ffac to your computer and use it in GitHub Desktop.
trace-file() {
local cmd="$1"
local path
if [[ "$cmd" == /* ]]; then
path="$cmd"
elif [[ "$cmd" == */* ]]; then
path="$PWD/$cmd"
else
path="$(type -P "$cmd")"
fi
path="${path//"/./"/"/"}"
if [[ "$path" == "" ]] || ! [[ -e "$path" ]]; then
printf >&2 '%s: No such file or directory\n' "$cmd"
return -1
fi
printf "%s\n" "${path//"$HOME"/"\$HOME"}"
local i=0
while true; do
i=$(( i + 1 ))
if [[ "$i" -gt 30 ]]; then
printf >&2 'recursion limit exceeded\n'
return -1
fi
local step=""
local next
if next="$(readlink "$path")"; then
step=" -> "
elif [[ "$(file "$path")" == *"ASCII text executable"* ]]; then
if next="$(grep --max-count=1 --no-messages '^ *exec ' "$path")"; then
while true; do
local next_="$(printf %s "$next" | sed -e 's/^ *exec \+//; s/^-[^ a]*a *\+[^ ]\+ \+//; s/^-[^ ]\+ \+//; s/^\([^-][^ ]\+\) .*/\1/')"
[[ "$next" == "$next_" ]] && break
next="$next_"
done
step="exec"
fi
fi
[[ "$step" == "" ]] || [[ "$next" == "" ]] && break
[[ "$next" != /* ]] && [[ "$path" == */* ]] && next="$(dirname "$path")/$next"
next="${next//"/./"/"/"}"
[[ -e "$next" ]] || break
printf " %s %s\n" "$step" "$next"
path="$next"
done
file --brief "$path"
}
@ben0x539
Copy link
Author

$ trace-file ls
/run/current-system/sw/bin/ls
   ->  /nix/store/4g12477iwgjhxhpym3zb34qyp2x3lqjl-coreutils-8.26/bin/ls
   ->  /nix/store/4g12477iwgjhxhpym3zb34qyp2x3lqjl-coreutils-8.26/bin/coreutils
ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /nix/store/a5gvhlwrday3dj8z3v09nr65ngn5jzq3-glibc-2.25/lib/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, not stripped
$ trace-file firefox
$HOME/.nix-profile/bin/firefox
   ->  /nix/store/0jbqb33sk7f2aqz8y2jzqi2m08yfdd3m-firefox-51.0.1/bin/firefox
   ->  /nix/store/0jbqb33sk7f2aqz8y2jzqi2m08yfdd3m-firefox-51.0.1/lib/firefox-51.0.1/firefox.uFGHd6
  exec /nix/store/fpvnrnj681d53bhm1ymvlh9gx3dl2i32-firefox-unwrapped-51.0.1/bin/firefox
   ->  /nix/store/fpvnrnj681d53bhm1ymvlh9gx3dl2i32-firefox-unwrapped-51.0.1/lib/firefox-51.0.1/firefox
ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /nix/store/amjgskg17wv125v9kahqdfxh8sx6mxgp-glibc-2.24/lib/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=f66904e973d2de422b02f2e592c0cefafdf90e3c, stripped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment