]> granicus.if.org Git - strace/blob - io.c
Add copyright headers
[strace] / io.c
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 <fcntl.h>
34 #include <sys/uio.h>
35
36 SYS_FUNC(read)
37 {
38         if (entering(tcp)) {
39                 printfd(tcp, tcp->u_arg[0]);
40                 tprints(", ");
41         } else {
42                 if (syserror(tcp))
43                         printaddr(tcp->u_arg[1]);
44                 else
45                         printstrn(tcp, tcp->u_arg[1], tcp->u_rval);
46                 tprintf(", %" PRI_klu, tcp->u_arg[2]);
47         }
48         return 0;
49 }
50
51 SYS_FUNC(write)
52 {
53         printfd(tcp, tcp->u_arg[0]);
54         tprints(", ");
55         printstrn(tcp, tcp->u_arg[1], tcp->u_arg[2]);
56         tprintf(", %" PRI_klu, tcp->u_arg[2]);
57
58         return RVAL_DECODED;
59 }
60
61 struct print_iovec_config {
62         enum iov_decode decode_iov;
63         kernel_ulong_t data_size;
64 };
65
66 static bool
67 print_iovec(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
68 {
69         const kernel_ulong_t *iov;
70         kernel_ulong_t iov_buf[2], len;
71         struct print_iovec_config *c = data;
72
73         if (elem_size < sizeof(iov_buf)) {
74                 iov_buf[0] = ((unsigned int *) elem_buf)[0];
75                 iov_buf[1] = ((unsigned int *) elem_buf)[1];
76                 iov = iov_buf;
77         } else {
78                 iov = elem_buf;
79         }
80
81         tprints("{iov_base=");
82
83         len = iov[1];
84
85         switch (c->decode_iov) {
86                 case IOV_DECODE_STR:
87                         if (len > c->data_size)
88                                 len = c->data_size;
89                         if (c->data_size != (kernel_ulong_t) -1)
90                                 c->data_size -= len;
91                         printstrn(tcp, iov[0], len);
92                         break;
93                 case IOV_DECODE_NETLINK:
94                         if (len > c->data_size)
95                                 len = c->data_size;
96                         if (c->data_size != (kernel_ulong_t) -1)
97                                 c->data_size -= len;
98                         decode_netlink(tcp, iov[0], len);
99                         break;
100                 default:
101                         printaddr(iov[0]);
102                         break;
103         }
104
105         tprintf(", iov_len=%" PRI_klu "}", iov[1]);
106
107         return true;
108 }
109
110 /*
111  * data_size limits the cumulative size of printed data.
112  * Example: recvmsg returing a short read.
113  */
114 void
115 tprint_iov_upto(struct tcb *const tcp, const kernel_ulong_t len,
116                 const kernel_ulong_t addr, const enum iov_decode decode_iov,
117                 const kernel_ulong_t data_size)
118 {
119         kernel_ulong_t iov[2];
120         struct print_iovec_config config =
121                 { .decode_iov = decode_iov, .data_size = data_size };
122
123         print_array(tcp, addr, len, iov, current_wordsize * 2,
124                     umoven_or_printaddr_ignore_syserror, print_iovec, &config);
125 }
126
127 SYS_FUNC(readv)
128 {
129         if (entering(tcp)) {
130                 printfd(tcp, tcp->u_arg[0]);
131                 tprints(", ");
132         } else {
133                 tprint_iov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
134                                 syserror(tcp) ? IOV_DECODE_ADDR :
135                                 IOV_DECODE_STR, tcp->u_rval);
136                 tprintf(", %" PRI_klu, tcp->u_arg[2]);
137         }
138         return 0;
139 }
140
141 SYS_FUNC(writev)
142 {
143         printfd(tcp, tcp->u_arg[0]);
144         tprints(", ");
145         tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], IOV_DECODE_STR);
146         tprintf(", %" PRI_klu, tcp->u_arg[2]);
147
148         return RVAL_DECODED;
149 }
150
151 SYS_FUNC(pread)
152 {
153         if (entering(tcp)) {
154                 printfd(tcp, tcp->u_arg[0]);
155                 tprints(", ");
156         } else {
157                 if (syserror(tcp))
158                         printaddr(tcp->u_arg[1]);
159                 else
160                         printstrn(tcp, tcp->u_arg[1], tcp->u_rval);
161                 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
162                 printllval(tcp, "%lld", 3);
163         }
164         return 0;
165 }
166
167 SYS_FUNC(pwrite)
168 {
169         printfd(tcp, tcp->u_arg[0]);
170         tprints(", ");
171         printstrn(tcp, tcp->u_arg[1], tcp->u_arg[2]);
172         tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
173         printllval(tcp, "%lld", 3);
174
175         return RVAL_DECODED;
176 }
177
178 static void
179 print_lld_from_low_high_val(struct tcb *tcp, int arg)
180 {
181 #if SIZEOF_KERNEL_LONG_T > 4
182 # ifndef current_klongsize
183         if (current_klongsize < SIZEOF_KERNEL_LONG_T) {
184                 tprintf("%" PRI_kld, (tcp->u_arg[arg + 1] << 32)
185                                | tcp->u_arg[arg]);
186         } else
187 # endif /* !current_klongsize */
188         {
189                 tprintf("%" PRI_kld, tcp->u_arg[arg]);
190         }
191 #else /* SIZEOF_KERNEL_LONG_T == 4 */
192         tprintf("%lld",
193                   ((long long) tcp->u_arg[arg + 1] << 32)
194                 | ((long long) tcp->u_arg[arg]));
195 #endif
196 }
197
198 #include "xlat/rwf_flags.h"
199
200 static int
201 do_preadv(struct tcb *tcp, const int flags_arg)
202 {
203         if (entering(tcp)) {
204                 printfd(tcp, tcp->u_arg[0]);
205                 tprints(", ");
206         } else {
207                 kernel_ulong_t len =
208                         truncate_kulong_to_current_wordsize(tcp->u_arg[2]);
209
210                 tprint_iov_upto(tcp, len, tcp->u_arg[1],
211                                 syserror(tcp) ? IOV_DECODE_ADDR :
212                                 IOV_DECODE_STR, tcp->u_rval);
213                 tprintf(", %" PRI_klu ", ", len);
214                 print_lld_from_low_high_val(tcp, 3);
215                 if (flags_arg >= 0) {
216                         tprints(", ");
217                         printflags(rwf_flags, tcp->u_arg[flags_arg], "RWF_???");
218                 }
219         }
220         return 0;
221 }
222
223 SYS_FUNC(preadv)
224 {
225         return do_preadv(tcp, -1);
226 }
227
228 static int
229 do_pwritev(struct tcb *tcp, const int flags_arg)
230 {
231         kernel_ulong_t len =
232                 truncate_kulong_to_current_wordsize(tcp->u_arg[2]);
233
234         printfd(tcp, tcp->u_arg[0]);
235         tprints(", ");
236         tprint_iov(tcp, len, tcp->u_arg[1], IOV_DECODE_STR);
237         tprintf(", %" PRI_klu ", ", len);
238         print_lld_from_low_high_val(tcp, 3);
239         if (flags_arg >= 0) {
240                 tprints(", ");
241                 printflags(rwf_flags, tcp->u_arg[flags_arg], "RWF_???");
242         }
243
244         return RVAL_DECODED;
245 }
246
247 SYS_FUNC(pwritev)
248 {
249         return do_pwritev(tcp, -1);
250 }
251
252 /*
253  * x32 is the only architecture where preadv2 takes 5 arguments
254  * instead of 6, see preadv64v2 in kernel sources.
255  * Likewise, x32 is the only architecture where pwritev2 takes 5 arguments
256  * instead of 6, see pwritev64v2 in kernel sources.
257  */
258
259 #if defined X86_64
260 # define PREADV2_PWRITEV2_FLAGS_ARG_NO (current_personality == 2 ? 4 : 5)
261 #elif defined X32
262 # define PREADV2_PWRITEV2_FLAGS_ARG_NO (current_personality == 0 ? 4 : 5)
263 #else
264 # define PREADV2_PWRITEV2_FLAGS_ARG_NO 5
265 #endif
266
267 SYS_FUNC(preadv2)
268 {
269         return do_preadv(tcp, PREADV2_PWRITEV2_FLAGS_ARG_NO);
270 }
271
272 SYS_FUNC(pwritev2)
273 {
274         return do_pwritev(tcp, PREADV2_PWRITEV2_FLAGS_ARG_NO);
275 }
276
277 #include "xlat/splice_flags.h"
278
279 SYS_FUNC(tee)
280 {
281         /* int fd_in */
282         printfd(tcp, tcp->u_arg[0]);
283         tprints(", ");
284         /* int fd_out */
285         printfd(tcp, tcp->u_arg[1]);
286         tprints(", ");
287         /* size_t len */
288         tprintf("%" PRI_klu ", ", tcp->u_arg[2]);
289         /* unsigned int flags */
290         printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
291
292         return RVAL_DECODED;
293 }
294
295 SYS_FUNC(splice)
296 {
297         /* int fd_in */
298         printfd(tcp, tcp->u_arg[0]);
299         tprints(", ");
300         /* loff_t *off_in */
301         printnum_int64(tcp, tcp->u_arg[1], "%" PRId64);
302         tprints(", ");
303         /* int fd_out */
304         printfd(tcp, tcp->u_arg[2]);
305         tprints(", ");
306         /* loff_t *off_out */
307         printnum_int64(tcp, tcp->u_arg[3], "%" PRId64);
308         tprints(", ");
309         /* size_t len */
310         tprintf("%" PRI_klu ", ", tcp->u_arg[4]);
311         /* unsigned int flags */
312         printflags(splice_flags, tcp->u_arg[5], "SPLICE_F_???");
313
314         return RVAL_DECODED;
315 }
316
317 SYS_FUNC(vmsplice)
318 {
319         /* int fd */
320         printfd(tcp, tcp->u_arg[0]);
321         tprints(", ");
322         /* const struct iovec *iov, unsigned long nr_segs */
323         tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], IOV_DECODE_STR);
324         tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
325         /* unsigned int flags */
326         printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
327
328         return RVAL_DECODED;
329 }