]> granicus.if.org Git - strace/blob - io.c
Implement decoding of splice, tee and vmsplice(2) syscalls
[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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *      $Id$
31  */
32
33 #include "defs.h"
34
35 #include <fcntl.h>
36 #if HAVE_SYS_UIO_H
37 #include <sys/uio.h>
38 #endif
39
40 #ifdef HAVE_LONG_LONG_OFF_T
41 /*
42  * Hacks for systems that have a long long off_t
43  */
44
45 #define sys_pread64     sys_pread
46 #define sys_pwrite64    sys_pwrite
47 #endif
48
49 int
50 sys_read(struct tcb *tcp)
51 {
52         if (entering(tcp)) {
53                 printfd(tcp, tcp->u_arg[0]);
54                 tprints(", ");
55         } else {
56                 if (syserror(tcp))
57                         tprintf("%#lx", tcp->u_arg[1]);
58                 else
59                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
60                 tprintf(", %lu", tcp->u_arg[2]);
61         }
62         return 0;
63 }
64
65 int
66 sys_write(struct tcb *tcp)
67 {
68         if (entering(tcp)) {
69                 printfd(tcp, tcp->u_arg[0]);
70                 tprints(", ");
71                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
72                 tprintf(", %lu", tcp->u_arg[2]);
73         }
74         return 0;
75 }
76
77 #if HAVE_SYS_UIO_H
78 void
79 tprint_iov(struct tcb *tcp, unsigned long len, unsigned long addr, int decode_iov)
80 {
81 #if defined(LINUX) && SUPPORTED_PERSONALITIES > 1
82         union {
83                 struct { u_int32_t base; u_int32_t len; } iov32;
84                 struct { u_int64_t base; u_int64_t len; } iov64;
85         } iov;
86 #define sizeof_iov \
87   (personality_wordsize[current_personality] == 4 \
88    ? sizeof(iov.iov32) : sizeof(iov.iov64))
89 #define iov_iov_base \
90   (personality_wordsize[current_personality] == 4 \
91    ? (u_int64_t) iov.iov32.base : iov.iov64.base)
92 #define iov_iov_len \
93   (personality_wordsize[current_personality] == 4 \
94    ? (u_int64_t) iov.iov32.len : iov.iov64.len)
95 #else
96         struct iovec iov;
97 #define sizeof_iov sizeof(iov)
98 #define iov_iov_base iov.iov_base
99 #define iov_iov_len iov.iov_len
100 #endif
101         unsigned long size, cur, end, abbrev_end;
102         int failed = 0;
103
104         if (!len) {
105                 tprints("[]");
106                 return;
107         }
108         size = len * sizeof_iov;
109         end = addr + size;
110         if (!verbose(tcp) || size / sizeof_iov != len || end < addr) {
111                 tprintf("%#lx", addr);
112                 return;
113         }
114         if (abbrev(tcp)) {
115                 abbrev_end = addr + max_strlen * sizeof_iov;
116                 if (abbrev_end < addr)
117                         abbrev_end = end;
118         } else {
119                 abbrev_end = end;
120         }
121         tprints("[");
122         for (cur = addr; cur < end; cur += sizeof_iov) {
123                 if (cur > addr)
124                         tprints(", ");
125                 if (cur >= abbrev_end) {
126                         tprints("...");
127                         break;
128                 }
129                 if (umoven(tcp, cur, sizeof_iov, (char *) &iov) < 0) {
130                         tprints("?");
131                         failed = 1;
132                         break;
133                 }
134                 tprints("{");
135                 if (decode_iov)
136                         printstr(tcp, (long) iov_iov_base, iov_iov_len);
137                 else
138                         tprintf("%#lx", (long) iov_iov_base);
139                 tprintf(", %lu}", (unsigned long)iov_iov_len);
140         }
141         tprints("]");
142         if (failed)
143                 tprintf(" %#lx", addr);
144 #undef sizeof_iov
145 #undef iov_iov_base
146 #undef iov_iov_len
147 }
148
149 int
150 sys_readv(struct tcb *tcp)
151 {
152         if (entering(tcp)) {
153                 printfd(tcp, tcp->u_arg[0]);
154                 tprints(", ");
155         } else {
156                 if (syserror(tcp)) {
157                         tprintf("%#lx, %lu",
158                                         tcp->u_arg[1], tcp->u_arg[2]);
159                         return 0;
160                 }
161                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
162                 tprintf(", %lu", tcp->u_arg[2]);
163         }
164         return 0;
165 }
166
167 int
168 sys_writev(struct tcb *tcp)
169 {
170         if (entering(tcp)) {
171                 printfd(tcp, tcp->u_arg[0]);
172                 tprints(", ");
173                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
174                 tprintf(", %lu", tcp->u_arg[2]);
175         }
176         return 0;
177 }
178 #endif
179
180 #if defined(SVR4)
181
182 int
183 sys_pread(struct tcb *tcp)
184 {
185         if (entering(tcp)) {
186                 printfd(tcp, tcp->u_arg[0]);
187                 tprints(", ");
188         } else {
189                 if (syserror(tcp))
190                         tprintf("%#lx", tcp->u_arg[1]);
191                 else
192                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
193 #if UNIXWARE
194                 /* off_t is signed int */
195                 tprintf(", %lu, %ld", tcp->u_arg[2], tcp->u_arg[3]);
196 #else
197                 tprintf(", %lu, %llu", tcp->u_arg[2],
198                         LONG_LONG(tcp->u_arg[3], tcp->u_arg[4]));
199 #endif
200         }
201         return 0;
202 }
203
204 int
205 sys_pwrite(struct tcb *tcp)
206 {
207         if (entering(tcp)) {
208                 printfd(tcp, tcp->u_arg[0]);
209                 tprints(", ");
210                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
211 #if UNIXWARE
212                 /* off_t is signed int */
213                 tprintf(", %lu, %ld", tcp->u_arg[2], tcp->u_arg[3]);
214 #else
215                 tprintf(", %lu, %llu", tcp->u_arg[2],
216                         LONG_LONG(tcp->u_arg[3], tcp->u_arg[4]));
217 #endif
218         }
219         return 0;
220 }
221 #endif /* SVR4 */
222
223 #ifdef FREEBSD
224 #include <sys/types.h>
225 #include <sys/socket.h>
226
227 int
228 sys_sendfile(struct tcb *tcp)
229 {
230         if (entering(tcp)) {
231                 printfd(tcp, tcp->u_arg[0]);
232                 tprints(", ");
233                 printfd(tcp, tcp->u_arg[1]);
234                 tprintf(", %llu, %lu",
235                         LONG_LONG(tcp->u_arg[2], tcp->u_arg[3]),
236                         tcp->u_arg[4]);
237         } else {
238                 off_t offset;
239
240                 if (!tcp->u_arg[5])
241                         tprints(", NULL");
242                 else {
243                         struct sf_hdtr hdtr;
244
245                         if (umove(tcp, tcp->u_arg[5], &hdtr) < 0)
246                                 tprintf(", %#lx", tcp->u_arg[5]);
247                         else {
248                                 tprints(", { ");
249                                 tprint_iov(tcp, hdtr.hdr_cnt, hdtr.headers, 1);
250                                 tprintf(", %u, ", hdtr.hdr_cnt);
251                                 tprint_iov(tcp, hdtr.trl_cnt, hdtr.trailers, 1);
252                                 tprintf(", %u }", hdtr.hdr_cnt);
253                         }
254                 }
255                 if (!tcp->u_arg[6])
256                         tprints(", NULL");
257                 else if (umove(tcp, tcp->u_arg[6], &offset) < 0)
258                         tprintf(", %#lx", tcp->u_arg[6]);
259                 else
260                         tprintf(", [%llu]", offset);
261                 tprintf(", %lu", tcp->u_arg[7]);
262         }
263         return 0;
264 }
265 #endif /* FREEBSD */
266
267 #ifdef LINUX
268
269 /* The SH4 ABI does allow long longs in odd-numbered registers, but
270    does not allow them to be split between registers and memory - and
271    there are only four argument registers for normal functions.  As a
272    result pread takes an extra padding argument before the offset.  This
273    was changed late in the 2.4 series (around 2.4.20).  */
274 #if defined(SH)
275 #define PREAD_OFFSET_ARG 4
276 #else
277 #define PREAD_OFFSET_ARG 3
278 #endif
279
280 int
281 sys_pread(struct tcb *tcp)
282 {
283         if (entering(tcp)) {
284                 printfd(tcp, tcp->u_arg[0]);
285                 tprints(", ");
286         } else {
287                 if (syserror(tcp))
288                         tprintf("%#lx", tcp->u_arg[1]);
289                 else
290                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
291                 tprintf(", %lu, ", tcp->u_arg[2]);
292                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
293         }
294         return 0;
295 }
296
297 int
298 sys_pwrite(struct tcb *tcp)
299 {
300         if (entering(tcp)) {
301                 printfd(tcp, tcp->u_arg[0]);
302                 tprints(", ");
303                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
304                 tprintf(", %lu, ", tcp->u_arg[2]);
305                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
306         }
307         return 0;
308 }
309
310 #if HAVE_SYS_UIO_H
311 int
312 sys_preadv(struct tcb *tcp)
313 {
314         if (entering(tcp)) {
315                 printfd(tcp, tcp->u_arg[0]);
316                 tprints(", ");
317         } else {
318                 if (syserror(tcp)) {
319                         tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
320                         return 0;
321                 }
322                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
323                 tprintf(", %lu, ", tcp->u_arg[2]);
324                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
325         }
326         return 0;
327 }
328
329 int
330 sys_pwritev(struct tcb *tcp)
331 {
332         if (entering(tcp)) {
333                 printfd(tcp, tcp->u_arg[0]);
334                 tprints(", ");
335                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
336                 tprintf(", %lu, ", tcp->u_arg[2]);
337                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
338         }
339         return 0;
340 }
341 #endif /* HAVE_SYS_UIO_H */
342
343 int
344 sys_sendfile(struct tcb *tcp)
345 {
346         if (entering(tcp)) {
347                 off_t offset;
348
349                 printfd(tcp, tcp->u_arg[0]);
350                 tprints(", ");
351                 printfd(tcp, tcp->u_arg[1]);
352                 tprints(", ");
353                 if (!tcp->u_arg[2])
354                         tprints("NULL");
355                 else if (umove(tcp, tcp->u_arg[2], &offset) < 0)
356                         tprintf("%#lx", tcp->u_arg[2]);
357                 else
358                         tprintf("[%lu]", offset);
359                 tprintf(", %lu", tcp->u_arg[3]);
360         }
361         return 0;
362 }
363
364 static void
365 print_loff_t(struct tcb *tcp, long addr)
366 {
367         loff_t offset;
368
369         if (!addr)
370                 tprints("NULL");
371         else if (umove(tcp, addr, &offset) < 0)
372                 tprintf("%#lx", addr);
373         else
374                 tprintf("[%llu]", (unsigned long long int) offset);
375 }
376
377 int
378 sys_sendfile64(struct tcb *tcp)
379 {
380         if (entering(tcp)) {
381                 printfd(tcp, tcp->u_arg[0]);
382                 tprints(", ");
383                 printfd(tcp, tcp->u_arg[1]);
384                 tprints(", ");
385                 print_loff_t(tcp, tcp->u_arg[2]);
386                 tprintf(", %lu", tcp->u_arg[3]);
387         }
388         return 0;
389 }
390
391 static const struct xlat splice_flags[] = {
392 #ifdef SPLICE_F_MOVE
393         { SPLICE_F_MOVE,     "SPLICE_F_MOVE"     },
394 #endif
395 #ifdef SPLICE_F_NONBLOCK
396         { SPLICE_F_NONBLOCK, "SPLICE_F_NONBLOCK" },
397 #endif
398 #ifdef SPLICE_F_MORE
399         { SPLICE_F_MORE,     "SPLICE_F_MORE"     },
400 #endif
401 #ifdef SPLICE_F_GIFT
402         { SPLICE_F_GIFT,     "SPLICE_F_GIFT"     },
403 #endif
404         { 0,                 NULL                },
405 };
406
407 int
408 sys_tee(struct tcb *tcp)
409 {
410         if (entering(tcp)) {
411                 /* int fd_in */
412                 printfd(tcp, tcp->u_arg[0]);
413                 tprints(", ");
414                 /* int fd_out */
415                 printfd(tcp, tcp->u_arg[1]);
416                 tprints(", ");
417                 /* size_t len */
418                 tprintf("%lu, ", tcp->u_arg[2]);
419                 /* unsigned int flags */
420                 printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
421         }
422         return 0;
423 }
424
425 int
426 sys_splice(struct tcb *tcp)
427 {
428         if (entering(tcp)) {
429                 /* int fd_in */
430                 printfd(tcp, tcp->u_arg[0]);
431                 tprints(", ");
432                 /* loff_t *off_in */
433                 print_loff_t(tcp, tcp->u_arg[1]);
434                 tprints(", ");
435                 /* int fd_out */
436                 printfd(tcp, tcp->u_arg[2]);
437                 tprints(", ");
438                 /* loff_t *off_out */
439                 print_loff_t(tcp, tcp->u_arg[3]);
440                 tprints(", ");
441                 /* size_t len */
442                 tprintf("%lu, ", tcp->u_arg[4]);
443                 /* unsigned int flags */
444                 printflags(splice_flags, tcp->u_arg[5], "SPLICE_F_???");
445         }
446         return 0;
447 }
448
449 int
450 sys_vmsplice(struct tcb *tcp)
451 {
452         if (entering(tcp)) {
453                 /* int fd */
454                 printfd(tcp, tcp->u_arg[0]);
455                 tprints(", ");
456                 /* const struct iovec *iov, unsigned long nr_segs */
457                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
458                 tprints(", ");
459                 /* unsigned int flags */
460                 printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
461         }
462         return 0;
463 }
464 #endif /* LINUX */
465
466 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
467 int
468 sys_pread64(struct tcb *tcp)
469 {
470         if (entering(tcp)) {
471                 printfd(tcp, tcp->u_arg[0]);
472                 tprints(", ");
473         } else {
474                 if (syserror(tcp))
475                         tprintf("%#lx", tcp->u_arg[1]);
476                 else
477                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
478                 tprintf(", %lu, ", tcp->u_arg[2]);
479                 printllval(tcp, "%#llx", 3);
480         }
481         return 0;
482 }
483
484 int
485 sys_pwrite64(struct tcb *tcp)
486 {
487         if (entering(tcp)) {
488                 printfd(tcp, tcp->u_arg[0]);
489                 tprints(", ");
490                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
491                 tprintf(", %lu, ", tcp->u_arg[2]);
492                 printllval(tcp, "%#llx", 3);
493         }
494         return 0;
495 }
496 #endif
497
498 int
499 sys_ioctl(struct tcb *tcp)
500 {
501         const struct ioctlent *iop;
502
503         if (entering(tcp)) {
504                 printfd(tcp, tcp->u_arg[0]);
505                 tprints(", ");
506                 iop = ioctl_lookup(tcp->u_arg[1]);
507                 if (iop) {
508                         tprints(iop->symbol);
509                         while ((iop = ioctl_next_match(iop)))
510                                 tprintf(" or %s", iop->symbol);
511                 } else
512                         tprintf("%#lx", tcp->u_arg[1]);
513                 ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
514         }
515         else {
516                 int ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
517                 if (!ret)
518                         tprintf(", %#lx", tcp->u_arg[2]);
519                 else
520                         return ret - 1;
521         }
522         return 0;
523 }