Last active
May 10, 2024 21:50
-
-
Save daniel-noland/ad1f50aef407dc0b4a7aa117dcd41e6b to your computer and use it in GitHub Desktop.
Easier netlink captures
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
#!/usr/bin/env bash | |
set -euo pipefail | |
clean_up_strace_io() { | |
printf -- 'command: %s\n' "${*}" | |
printf -- 'messages:\n' | |
declare line | |
declare strace_parse | |
declare label | |
declare message="" | |
while read -r line; do | |
if [[ "${line}" =~ "sendto"* ]] || [[ "${line}" =~ "recvmsg"* ]] || [[ "${line}" =~ "sendmsg"* ]]; then | |
if [[ -n "${message}" ]]; then | |
printf -- ' message: "%s"\n' "${message}" | |
fi | |
message="" | |
strace_parse="${line}" | |
label="$( | |
sed 's|sendto(\([0-9]\+\).*|sendto \1|;s|sendmsg(\([0-9]\+\).*|sendmsg \1|;s|recvmsg(\([0-9]\+\).*|recvmsg \1|;' <<< "${strace_parse}" | |
)" | |
printf -- ' - op: %s\n' "${label}" | |
printf -- ' strace-parse: %s\n' "${strace_parse}" | |
continue | |
fi | |
# remove stray message size lines | |
if [[ "${line}" =~ "bytes in buffer" ]]; then | |
continue | |
fi | |
message+="$(cut -d' ' -f3-21 <<< "${line}" | sed 's| \+||g')" | |
done | |
if [[ -n "${message}" ]]; then | |
printf -- ' message: "%s"\n' "${message}" | |
fi | |
} | |
trace_send_recv() { | |
strace \ | |
-e sendto,sendmsg,recvmsg \ | |
--quiet=all \ | |
--string-limit=999999999 \ | |
--write=3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 \ | |
--read=3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 \ | |
--strings-in-hex=all \ | |
--const-print-style=raw \ | |
--output=>(clean_up_strace_io "${@}" > /tmp/trace.yml) \ | |
"${@}" | |
} | |
trace_send_recv ip route | |
wait | |
cat /tmp/trace.yml | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment