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