xref: /freebsd/cddl/usr.sbin/dwatch/libexec/rw (revision edf8578117e8844e02c0121147f45e4609b30680)
1# -*- tab-width: 4 -*- ;; Emacs
2# vi: set filetype=sh tabstop=8 shiftwidth=8 noexpandtab :: Vi/ViM
3############################################################ IDENT(1)
4#
5# $Title: dwatch(8) module for read(2), write(2), or similar entry $
6# $Copyright: 2014-2018 Devin Teske. All rights reserved. $
7#
8############################################################ DESCRIPTION
9#
10# Display data sent/received when read(2)/write(2) occurs
11#
12############################################################ PROBE
13
14case "$PROFILE" in
15rw) : ${PROBE:=syscall::read:entry, syscall::write:entry} ;;
16 *) : ${PROBE:=syscall::$PROFILE:entry}
17esac
18
19############################################################ ACTIONS
20
21exec 9<<EOF
22this size_t nbytes;
23this string bufstr;
24this string flow;
25this void * buf;
26this void * data;
27
28$PROBE /* probe ID $ID */
29{${TRACE:+
30	printf("<$ID>");
31}
32	/*
33	 * R/W
34	 */
35	this->flow = probefunc == "read" ? "<-" : "->";
36	this->buf = (void *)arg1;
37	this->nbytes = (size_t)arg2;
38
39	/*
40	 * Allocate temporary memory for, copy, and NUL-terminate the data
41	 */
42	this->data = alloca(this->nbytes + 1);
43	copyinto((uintptr_t)this->buf, this->nbytes, this->data);
44	bcopy("\0", (void *)((uintptr_t)this->data + this->nbytes), 1);
45
46	/*
47	 * Extract string from temporary memory
48	 */
49	this->bufstr = stringof(this->data);
50}
51EOF
52ACTIONS=$( cat <&9 )
53ID=$(( $ID + 1 ))
54
55############################################################ EVENT DETAILS
56
57if [ ! "$CUSTOM_DETAILS" ]; then
58exec 9<<EOF
59	/*
60	 * Print read/write details
61	 */
62	printf("%s \"%s\" %d byte%s",
63		this->flow,
64		this->bufstr,
65		this->nbytes,
66		this->nbytes == 1 ? "" : "s");
67EOF
68EVENT_DETAILS=$( cat <&9 )
69fi
70
71################################################################################
72# END
73################################################################################
74