]> granicus.if.org Git - strace/blob - io.c
Compress blank lines
[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 int
41 sys_read(struct tcb *tcp)
42 {
43         if (entering(tcp)) {
44                 printfd(tcp, tcp->u_arg[0]);
45                 tprints(", ");
46         } else {
47                 if (syserror(tcp))
48                         tprintf("%#lx", tcp->u_arg[1]);
49                 else
50                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
51                 tprintf(", %lu", tcp->u_arg[2]);
52         }
53         return 0;
54 }
55
56 int
57 sys_write(struct tcb *tcp)
58 {
59         if (entering(tcp)) {
60                 printfd(tcp, tcp->u_arg[0]);
61                 tprints(", ");
62                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
63                 tprintf(", %lu", tcp->u_arg[2]);
64         }
65         return 0;
66 }
67
68 #if HAVE_SYS_UIO_H
69 void
70 tprint_iov(struct tcb *tcp, unsigned long len, unsigned long addr, int decode_iov)
71 {
72 #if SUPPORTED_PERSONALITIES > 1
73         union {
74                 struct { u_int32_t base; u_int32_t len; } iov32;
75                 struct { u_int64_t base; u_int64_t len; } iov64;
76         } iov;
77 #define sizeof_iov \
78   (personality_wordsize[current_personality] == 4 \
79    ? sizeof(iov.iov32) : sizeof(iov.iov64))
80 #define iov_iov_base \
81   (personality_wordsize[current_personality] == 4 \
82    ? (u_int64_t) iov.iov32.base : iov.iov64.base)
83 #define iov_iov_len \
84   (personality_wordsize[current_personality] == 4 \
85    ? (u_int64_t) iov.iov32.len : iov.iov64.len)
86 #else
87         struct iovec iov;
88 #define sizeof_iov sizeof(iov)
89 #define iov_iov_base iov.iov_base
90 #define iov_iov_len iov.iov_len
91 #endif
92         unsigned long size, cur, end, abbrev_end;
93         int failed = 0;
94
95         if (!len) {
96                 tprints("[]");
97                 return;
98         }
99         size = len * sizeof_iov;
100         end = addr + size;
101         if (!verbose(tcp) || size / sizeof_iov != len || end < addr) {
102                 tprintf("%#lx", addr);
103                 return;
104         }
105         if (abbrev(tcp)) {
106                 abbrev_end = addr + max_strlen * sizeof_iov;
107                 if (abbrev_end < addr)
108                         abbrev_end = end;
109         } else {
110                 abbrev_end = end;
111         }
112         tprints("[");
113         for (cur = addr; cur < end; cur += sizeof_iov) {
114                 if (cur > addr)
115                         tprints(", ");
116                 if (cur >= abbrev_end) {
117                         tprints("...");
118                         break;
119                 }
120                 if (umoven(tcp, cur, sizeof_iov, (char *) &iov) < 0) {
121                         tprints("?");
122                         failed = 1;
123                         break;
124                 }
125                 tprints("{");
126                 if (decode_iov)
127                         printstr(tcp, (long) iov_iov_base, iov_iov_len);
128                 else
129                         tprintf("%#lx", (long) iov_iov_base);
130                 tprintf(", %lu}", (unsigned long)iov_iov_len);
131         }
132         tprints("]");
133         if (failed)
134                 tprintf(" %#lx", addr);
135 #undef sizeof_iov
136 #undef iov_iov_base
137 #undef iov_iov_len
138 }
139
140 int
141 sys_readv(struct tcb *tcp)
142 {
143         if (entering(tcp)) {
144                 printfd(tcp, tcp->u_arg[0]);
145                 tprints(", ");
146         } else {
147                 if (syserror(tcp)) {
148                         tprintf("%#lx, %lu",
149                                         tcp->u_arg[1], tcp->u_arg[2]);
150                         return 0;
151                 }
152                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
153                 tprintf(", %lu", tcp->u_arg[2]);
154         }
155         return 0;
156 }
157
158 int
159 sys_writev(struct tcb *tcp)
160 {
161         if (entering(tcp)) {
162                 printfd(tcp, tcp->u_arg[0]);
163                 tprints(", ");
164                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
165                 tprintf(", %lu", tcp->u_arg[2]);
166         }
167         return 0;
168 }
169 #endif
170
171 /* The SH4 ABI does allow long longs in odd-numbered registers, but
172    does not allow them to be split between registers and memory - and
173    there are only four argument registers for normal functions.  As a
174    result pread takes an extra padding argument before the offset.  This
175    was changed late in the 2.4 series (around 2.4.20).  */
176 #if defined(SH)
177 #define PREAD_OFFSET_ARG 4
178 #else
179 #define PREAD_OFFSET_ARG 3
180 #endif
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                 tprintf(", %lu, ", tcp->u_arg[2]);
194                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
195         }
196         return 0;
197 }
198
199 int
200 sys_pwrite(struct tcb *tcp)
201 {
202         if (entering(tcp)) {
203                 printfd(tcp, tcp->u_arg[0]);
204                 tprints(", ");
205                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
206                 tprintf(", %lu, ", tcp->u_arg[2]);
207                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
208         }
209         return 0;
210 }
211
212 #if HAVE_SYS_UIO_H
213 int
214 sys_preadv(struct tcb *tcp)
215 {
216         if (entering(tcp)) {
217                 printfd(tcp, tcp->u_arg[0]);
218                 tprints(", ");
219         } else {
220                 if (syserror(tcp)) {
221                         tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
222                         return 0;
223                 }
224                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
225                 tprintf(", %lu, ", tcp->u_arg[2]);
226                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
227         }
228         return 0;
229 }
230
231 int
232 sys_pwritev(struct tcb *tcp)
233 {
234         if (entering(tcp)) {
235                 printfd(tcp, tcp->u_arg[0]);
236                 tprints(", ");
237                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
238                 tprintf(", %lu, ", tcp->u_arg[2]);
239                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
240         }
241         return 0;
242 }
243 #endif /* HAVE_SYS_UIO_H */
244
245 int
246 sys_sendfile(struct tcb *tcp)
247 {
248         if (entering(tcp)) {
249                 off_t offset;
250
251                 printfd(tcp, tcp->u_arg[0]);
252                 tprints(", ");
253                 printfd(tcp, tcp->u_arg[1]);
254                 tprints(", ");
255                 if (!tcp->u_arg[2])
256                         tprints("NULL");
257                 else if (umove(tcp, tcp->u_arg[2], &offset) < 0)
258                         tprintf("%#lx", tcp->u_arg[2]);
259                 else
260 #ifdef HAVE_LONG_LONG_OFF_T
261                         tprintf("[%llu]", offset);
262 #else
263                         tprintf("[%lu]", offset);
264 #endif
265                 tprintf(", %lu", tcp->u_arg[3]);
266         }
267         return 0;
268 }
269
270 static void
271 print_loff_t(struct tcb *tcp, long addr)
272 {
273         loff_t offset;
274
275         if (!addr)
276                 tprints("NULL");
277         else if (umove(tcp, addr, &offset) < 0)
278                 tprintf("%#lx", addr);
279         else
280                 tprintf("[%llu]", (unsigned long long int) offset);
281 }
282
283 int
284 sys_sendfile64(struct tcb *tcp)
285 {
286         if (entering(tcp)) {
287                 printfd(tcp, tcp->u_arg[0]);
288                 tprints(", ");
289                 printfd(tcp, tcp->u_arg[1]);
290                 tprints(", ");
291                 print_loff_t(tcp, tcp->u_arg[2]);
292                 tprintf(", %lu", tcp->u_arg[3]);
293         }
294         return 0;
295 }
296
297 static const struct xlat splice_flags[] = {
298 #ifdef SPLICE_F_MOVE
299         { SPLICE_F_MOVE,     "SPLICE_F_MOVE"     },
300 #endif
301 #ifdef SPLICE_F_NONBLOCK
302         { SPLICE_F_NONBLOCK, "SPLICE_F_NONBLOCK" },
303 #endif
304 #ifdef SPLICE_F_MORE
305         { SPLICE_F_MORE,     "SPLICE_F_MORE"     },
306 #endif
307 #ifdef SPLICE_F_GIFT
308         { SPLICE_F_GIFT,     "SPLICE_F_GIFT"     },
309 #endif
310         { 0,                 NULL                },
311 };
312
313 int
314 sys_tee(struct tcb *tcp)
315 {
316         if (entering(tcp)) {
317                 /* int fd_in */
318                 printfd(tcp, tcp->u_arg[0]);
319                 tprints(", ");
320                 /* int fd_out */
321                 printfd(tcp, tcp->u_arg[1]);
322                 tprints(", ");
323                 /* size_t len */
324                 tprintf("%lu, ", tcp->u_arg[2]);
325                 /* unsigned int flags */
326                 printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
327         }
328         return 0;
329 }
330
331 int
332 sys_splice(struct tcb *tcp)
333 {
334         if (entering(tcp)) {
335                 /* int fd_in */
336                 printfd(tcp, tcp->u_arg[0]);
337                 tprints(", ");
338                 /* loff_t *off_in */
339                 print_loff_t(tcp, tcp->u_arg[1]);
340                 tprints(", ");
341                 /* int fd_out */
342                 printfd(tcp, tcp->u_arg[2]);
343                 tprints(", ");
344                 /* loff_t *off_out */
345                 print_loff_t(tcp, tcp->u_arg[3]);
346                 tprints(", ");
347                 /* size_t len */
348                 tprintf("%lu, ", tcp->u_arg[4]);
349                 /* unsigned int flags */
350                 printflags(splice_flags, tcp->u_arg[5], "SPLICE_F_???");
351         }
352         return 0;
353 }
354
355 int
356 sys_vmsplice(struct tcb *tcp)
357 {
358         if (entering(tcp)) {
359                 /* int fd */
360                 printfd(tcp, tcp->u_arg[0]);
361                 tprints(", ");
362                 /* const struct iovec *iov, unsigned long nr_segs */
363                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
364                 tprints(", ");
365                 /* unsigned int flags */
366                 printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
367         }
368         return 0;
369 }
370
371 int
372 sys_ioctl(struct tcb *tcp)
373 {
374         const struct ioctlent *iop;
375
376         if (entering(tcp)) {
377                 printfd(tcp, tcp->u_arg[0]);
378                 tprints(", ");
379                 iop = ioctl_lookup(tcp->u_arg[1]);
380                 if (iop) {
381                         tprints(iop->symbol);
382                         while ((iop = ioctl_next_match(iop)))
383                                 tprintf(" or %s", iop->symbol);
384                 } else
385                         tprintf("%#lx", tcp->u_arg[1]);
386                 ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
387         }
388         else {
389                 int ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
390                 if (!ret)
391                         tprintf(", %#lx", tcp->u_arg[2]);
392                 else
393                         return ret - 1;
394         }
395         return 0;
396 }