Last active
March 6, 2017 10:35
Star
You must be signed in to star a gist
strace-many.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SCRIPT_PATH="$0" | |
SCRIPT_NAME="$(basename $0)" | |
function __get_list() { | |
SEARCH_TERM="$1" | |
LIST=$(ps -ef | grep "${SEARCH_TERM}" | awk '{print $2}') | |
echo "${LIST}" | |
} | |
function __usage() { | |
cat <<EOF | |
${SCRIPT_NAME} {search-term} {strace args}... | |
search-term Anything that should show up in "$ ps -ef" | |
strace-args Any number of strace args (for example, '-f') | |
EOF | |
} | |
function __cleanup() { | |
OUTPUT_DIR="$1" | |
# Todo: Delete this automatically with like, read -p | |
# Also, need to name the process somehow. Random in /tmp/ isnt so awesome | |
cat <<EOF | |
WARNING: While you're all done tracking, we saved all that output to "${OUTPUT_DIR}" just in case we missed something | |
and needed to go and have a look. If you don't need it, clean it up with | |
rm -rf "${OUTPUT_DIR}" | |
EOF | |
} | |
# Uses sudo to escalate privileges if required | |
function __privesc() { | |
CMD_PRIVESC="" | |
if [[ $(id -u) != 0 ]]; then | |
CMD_PRIVESC="sudo " | |
fi | |
$CMD_PRIVESC $@ | |
} | |
main() { | |
QUERY="$1" | |
STRACE_ARGS=${@:2} | |
if [[ -z "${QUERY}" ]]; then | |
__usage; | |
exit 1; | |
fi; | |
STRACE_PROCESSES=() | |
TMP_DIRECTORY=$(mktemp -d) | |
trap "for PID in ${STRACE_PROCESSES[@]}; do kill $PID; done; __cleanup '${TMP_DIRECTORY}'" SIGINT SIGTERM SIGKILL | |
for PROCESS in $(__get_list "${QUERY}"); do | |
__privesc strace -p $PROCESS ${STRACE_ARGS} 2> "${TMP_DIRECTORY}/${PROCESS}.strace" & | |
STRACE_PROCESSES+=($!) | |
done; | |
tail -f ${TMP_DIRECTORY}/* | |
} | |
main ${@:1} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment