Skip to content

Instantly share code, notes, and snippets.

@tekknolagi
Forked from ianloic/strace-deps
Created August 8, 2022 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tekknolagi/323ede059457f7ef1ce597095907acbc to your computer and use it in GitHub Desktop.
Save tekknolagi/323ede059457f7ef1ce597095907acbc to your computer and use it in GitHub Desktop.
#!/bin/bash
if [[ $# -lt 2 ]]; then
echo "Usage: $0 <depfile> <command...>" >&2
exit 1
fi
# The .d file we're going to write.
DEPSFILE=$1
shift
# The temporary file that holds strace output.
STRACEFILE=$DEPSFILE.$$.strace
# Remove the temp file when we're done.
function cleanup {
rm -f "${STRACEFILE}"
}
trap cleanup EXIT
# strace the command and keep track of openat calls.
strace -o "${STRACEFILE}" -f -qq -y -e openat "$@"
# Function to de-dupe files and remove ones to ignore.
filterfiles() {
# This may require tweaking if for example the directory you build
# in is under /usr/.
sort | uniq | grep -E -v '^(/etc/|/var/|/proc/|/lib/|/usr/|/tmp/)'
}
# Files that were written by this command.
OUTPUTS=$(gawk 'match($0, /O_WRONLY.* = [0-9]+<(.+)>$/, arr) { print arr[1]; }' "${STRACEFILE}" | filterfiles | xargs echo)
# Files that were read by this command.
INPUTS=$(gawk 'match($0, /O_RDONLY.* = [0-9]+<(.+)>$/, arr) { print arr[1]; }' "${STRACEFILE}" | filterfiles | xargs echo)
# Generate the deps file.
echo "${OUTPUTS}: ${INPUTS}" > "${DEPSFILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment