• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 1999-2017 The strace developers.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "defs.h"
33 #include <linux/aio_abi.h>
34 
SYS_FUNC(io_setup)35 SYS_FUNC(io_setup)
36 {
37 	if (entering(tcp))
38 		tprintf("%u, ", (unsigned int) tcp->u_arg[0]);
39 	else
40 		printnum_ptr(tcp, tcp->u_arg[1]);
41 	return 0;
42 }
43 
SYS_FUNC(io_destroy)44 SYS_FUNC(io_destroy)
45 {
46 	printaddr(tcp->u_arg[0]);
47 
48 	return RVAL_DECODED;
49 }
50 
51 enum iocb_sub {
52 	SUB_NONE, SUB_COMMON, SUB_VECTOR
53 };
54 
55 static enum iocb_sub
tprint_lio_opcode(unsigned cmd)56 tprint_lio_opcode(unsigned cmd)
57 {
58 	static const struct {
59 		const char *name;
60 		enum iocb_sub sub;
61 	} cmds[] = {
62 		{ "pread", SUB_COMMON },
63 		{ "pwrite", SUB_COMMON },
64 		{ "fsync", SUB_NONE },
65 		{ "fdsync", SUB_NONE },
66 		{ "preadx", SUB_NONE },
67 		{ "poll", SUB_NONE },
68 		{ "noop", SUB_NONE },
69 		{ "preadv", SUB_VECTOR },
70 		{ "pwritev", SUB_VECTOR },
71 	};
72 
73 	if (cmd < ARRAY_SIZE(cmds)) {
74 		tprints(cmds[cmd].name);
75 		return cmds[cmd].sub;
76 	}
77 	tprintf("%u", cmd);
78 	tprints_comment("SUB_???");
79 	return SUB_NONE;
80 }
81 
82 static void
print_common_flags(struct tcb * tcp,const struct iocb * cb)83 print_common_flags(struct tcb *tcp, const struct iocb *cb)
84 {
85 /* IOCB_FLAG_RESFD is available since v2.6.22-rc1~47 */
86 #ifdef IOCB_FLAG_RESFD
87 	if (cb->aio_flags & IOCB_FLAG_RESFD) {
88 		tprints(", resfd=");
89 		printfd(tcp, cb->aio_resfd);
90 	}
91 	if (cb->aio_flags & ~IOCB_FLAG_RESFD)
92 		tprintf(", flags=%#x", cb->aio_flags);
93 #endif
94 }
95 
96 static bool
iocb_is_valid(const struct iocb * cb)97 iocb_is_valid(const struct iocb *cb)
98 {
99 	return cb->aio_buf == (unsigned long) cb->aio_buf &&
100 	       cb->aio_nbytes == (size_t) cb->aio_nbytes &&
101 	       (ssize_t) cb->aio_nbytes >= 0;
102 }
103 
104 static enum iocb_sub
print_iocb_header(struct tcb * tcp,const struct iocb * cb)105 print_iocb_header(struct tcb *tcp, const struct iocb *cb)
106 {
107 	enum iocb_sub sub;
108 
109 	if (cb->aio_data)
110 		tprintf("data=%#" PRIx64 ", ",
111 			(uint64_t) cb->aio_data);
112 
113 	if (cb->aio_key)
114 		tprintf("key=%u, ", cb->aio_key);
115 
116 	sub = tprint_lio_opcode(cb->aio_lio_opcode);
117 	if (cb->aio_reqprio)
118 		tprintf(", reqprio=%hd", cb->aio_reqprio);
119 
120 	tprints(", fildes=");
121 	printfd(tcp, cb->aio_fildes);
122 
123 	return sub;
124 }
125 
126 static void
print_iocb(struct tcb * tcp,const struct iocb * cb)127 print_iocb(struct tcb *tcp, const struct iocb *cb)
128 {
129 	enum iocb_sub sub = print_iocb_header(tcp, cb);
130 
131 	switch (sub) {
132 	case SUB_COMMON:
133 		if (cb->aio_lio_opcode == 1 && iocb_is_valid(cb)) {
134 			tprints(", str=");
135 			printstrn(tcp, cb->aio_buf, cb->aio_nbytes);
136 		} else {
137 			tprintf(", buf=%#" PRIx64, (uint64_t) cb->aio_buf);
138 		}
139 		tprintf(", nbytes=%" PRIu64 ", offset=%" PRId64,
140 			(uint64_t) cb->aio_nbytes, (int64_t) cb->aio_offset);
141 		print_common_flags(tcp, cb);
142 		break;
143 	case SUB_VECTOR:
144 		if (iocb_is_valid(cb)) {
145 			tprints(", iovec=");
146 			tprint_iov(tcp, cb->aio_nbytes, cb->aio_buf,
147 				   cb->aio_lio_opcode == 8
148 				   ? IOV_DECODE_STR
149 				   : IOV_DECODE_ADDR);
150 		} else {
151 			tprintf(", buf=%#" PRIx64 ", nbytes=%" PRIu64,
152 				(uint64_t) cb->aio_buf,
153 				(uint64_t) cb->aio_nbytes);
154 		}
155 		tprintf(", offset=%" PRId64, (int64_t) cb->aio_offset);
156 		print_common_flags(tcp, cb);
157 		break;
158 	case SUB_NONE:
159 		break;
160 	}
161 }
162 
163 static bool
print_iocbp(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)164 print_iocbp(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
165 {
166 	kernel_ulong_t addr;
167 	struct iocb cb;
168 
169 	if (elem_size < sizeof(kernel_ulong_t)) {
170 		addr = *(unsigned int *) elem_buf;
171 	} else {
172 		addr = *(kernel_ulong_t *) elem_buf;
173 	}
174 
175 	tprints("{");
176 	if (!umove_or_printaddr(tcp, addr, &cb))
177 		print_iocb(tcp, &cb);
178 	tprints("}");
179 
180 	return true;
181 }
182 
SYS_FUNC(io_submit)183 SYS_FUNC(io_submit)
184 {
185 	const kernel_long_t nr =
186 		truncate_klong_to_current_wordsize(tcp->u_arg[1]);
187 	const kernel_ulong_t addr = tcp->u_arg[2];
188 	kernel_ulong_t iocbp;
189 
190 	printaddr(tcp->u_arg[0]);
191 	tprintf(", %" PRI_kld ", ", nr);
192 
193 	if (nr < 0)
194 		printaddr(addr);
195 	else
196 		print_array(tcp, addr, nr, &iocbp, current_wordsize,
197 			    umoven_or_printaddr, print_iocbp, 0);
198 
199 	return RVAL_DECODED;
200 }
201 
202 static bool
print_io_event(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)203 print_io_event(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
204 {
205 	struct io_event *event = elem_buf;
206 
207 	tprintf("{data=%#" PRIx64 ", obj=%#" PRIx64
208 		", res=%" PRId64 ", res2=%" PRId64 "}",
209 		(uint64_t) event->data, (uint64_t) event->obj,
210 		(int64_t) event->res, (int64_t) event->res2);
211 
212 	return true;
213 }
214 
SYS_FUNC(io_cancel)215 SYS_FUNC(io_cancel)
216 {
217 	if (entering(tcp)) {
218 		printaddr(tcp->u_arg[0]);
219 		tprints(", ");
220 
221 		struct iocb cb;
222 
223 		if (!umove_or_printaddr(tcp, tcp->u_arg[1], &cb)) {
224 			tprints("{");
225 			print_iocb_header(tcp, &cb);
226 			tprints("}");
227 		}
228 		tprints(", ");
229 	} else {
230 		struct io_event event;
231 
232 		if (!umove_or_printaddr(tcp, tcp->u_arg[2], &event))
233 			print_io_event(tcp, &event, sizeof(event), 0);
234 	}
235 	return 0;
236 }
237 
SYS_FUNC(io_getevents)238 SYS_FUNC(io_getevents)
239 {
240 	if (entering(tcp)) {
241 		printaddr(tcp->u_arg[0]);
242 		tprintf(", %" PRI_kld ", %" PRI_kld ", ",
243 			truncate_klong_to_current_wordsize(tcp->u_arg[1]),
244 			truncate_klong_to_current_wordsize(tcp->u_arg[2]));
245 	} else {
246 		struct io_event buf;
247 		print_array(tcp, tcp->u_arg[3], tcp->u_rval, &buf, sizeof(buf),
248 			    umoven_or_printaddr, print_io_event, 0);
249 		tprints(", ");
250 		/*
251 		 * Since the timeout parameter is read by the kernel
252 		 * on entering syscall, it has to be decoded the same way
253 		 * whether the syscall has failed or not.
254 		 */
255 		temporarily_clear_syserror(tcp);
256 		print_timespec(tcp, tcp->u_arg[4]);
257 		restore_cleared_syserror(tcp);
258 	}
259 	return 0;
260 }
261