Created
September 20, 2019 19:01
-
-
Save psanford/aa5aa5e2aa1851345cdcb67a3ed0f01f to your computer and use it in GitHub Desktop.
bpftrace wrapper to trace reads from stdin and writes to stdout and stderr for a given process name
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 | |
COMM=$1 | |
if [ -z "$COMM"] ; then | |
echo "usage: $0 <name_of_process>" >&2 | |
exit 1 | |
fi | |
script='' | |
read -r -d '' script <<EOF | |
tracepoint:syscalls:sys_enter_read /comm =="$COMM"/ { | |
if (args->fd == 0) { | |
@read_buf=args->buf; | |
@read_fd=args->fd; | |
} | |
} | |
tracepoint:syscalls:sys_exit_read /comm =="$COMM"/ { | |
time("%H:%M:%S "); | |
printf("exit fd=%d\n", @read_fd); | |
if (@read_fd >= 0 && args->ret > 0) { | |
printf("read pid=%d fd=%d <%s>\n", pid, @read_fd, str(@read_buf, args->ret)); | |
@read_buf=0; | |
@read_fd=-1; | |
} | |
} | |
tracepoint:syscalls:sys_enter_write /comm =="$COMM"/ { | |
if (args->fd <= 2) { | |
time("%H:%M:%S "); | |
printf("write pid=%d fd=%d <%s>\n", pid, args->fd, str(args->buf, args->count)); | |
} | |
} | |
EOF | |
bpftrace -e "$script" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment