]> granicus.if.org Git - strace/blob - io.c
Cleanup after non-Linux code removal.
[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
172
173
174 /* The SH4 ABI does allow long longs in odd-numbered registers, but
175    does not allow them to be split between registers and memory - and
176    there are only four argument registers for normal functions.  As a
177    result pread takes an extra padding argument before the offset.  This
178    was changed late in the 2.4 series (around 2.4.20).  */
179 #if defined(SH)
180 #define PREAD_OFFSET_ARG 4
181 #else
182 #define PREAD_OFFSET_ARG 3
183 #endif
184
185 int
186 sys_pread(struct tcb *tcp)
187 {
188         if (entering(tcp)) {
189                 printfd(tcp, tcp->u_arg[0]);
190                 tprints(", ");
191         } else {
192                 if (syserror(tcp))
193                         tprintf("%#lx", tcp->u_arg[1]);
194                 else
195                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
196                 tprintf(", %lu, ", tcp->u_arg[2]);
197                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
198         }
199         return 0;
200 }
201
202 int
203 sys_pwrite(struct tcb *tcp)
204 {
205         if (entering(tcp)) {
206                 printfd(tcp, tcp->u_arg[0]);
207                 tprints(", ");
208                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
209                 tprintf(", %lu, ", tcp->u_arg[2]);
210                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
211         }
212         return 0;
213 }
214
215 #if HAVE_SYS_UIO_H
216 int
217 sys_preadv(struct tcb *tcp)
218 {
219         if (entering(tcp)) {
220                 printfd(tcp, tcp->u_arg[0]);
221                 tprints(", ");
222         } else {
223                 if (syserror(tcp)) {
224                         tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
225                         return 0;
226                 }
227                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
228                 tprintf(", %lu, ", tcp->u_arg[2]);
229                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
230         }
231         return 0;
232 }
233
234 int
235 sys_pwritev(struct tcb *tcp)
236 {
237         if (entering(tcp)) {
238                 printfd(tcp, tcp->u_arg[0]);
239                 tprints(", ");
240                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
241                 tprintf(", %lu, ", tcp->u_arg[2]);
242                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
243         }
244         return 0;
245 }
246 #endif /* HAVE_SYS_UIO_H */
247
248 int
249 sys_sendfile(struct tcb *tcp)
250 {
251         if (entering(tcp)) {
252                 off_t offset;
253
254                 printfd(tcp, tcp->u_arg[0]);
255                 tprints(", ");
256                 printfd(tcp, tcp->u_arg[1]);
257                 tprints(", ");
258                 if (!tcp->u_arg[2])
259                         tprints("NULL");
260                 else if (umove(tcp, tcp->u_arg[2], &offset) < 0)
261                         tprintf("%#lx", tcp->u_arg[2]);
262                 else
263 #ifdef HAVE_LONG_LONG_OFF_T
264                         tprintf("[%llu]", offset);
265 #else
266                         tprintf("[%lu]", offset);
267 #endif
268                 tprintf(", %lu", tcp->u_arg[3]);
269         }
270         return 0;
271 }
272
273 static void
274 print_loff_t(struct tcb *tcp, long addr)
275 {
276         loff_t offset;
277
278         if (!addr)
279                 tprints("NULL");
280         else if (umove(tcp, addr, &offset) < 0)
281                 tprintf("%#lx", addr);
282         else
283                 tprintf("[%llu]", (unsigned long long int) offset);
284 }
285
286 int
287 sys_sendfile64(struct tcb *tcp)
288 {
289         if (entering(tcp)) {
290                 printfd(tcp, tcp->u_arg[0]);
291                 tprints(", ");
292                 printfd(tcp, tcp->u_arg[1]);
293                 tprints(", ");
294                 print_loff_t(tcp, tcp->u_arg[2]);
295                 tprintf(", %lu", tcp->u_arg[3]);
296         }
297         return 0;
298 }
299
300 static const struct xlat splice_flags[] = {
301 #ifdef SPLICE_F_MOVE
302         { SPLICE_F_MOVE,     "SPLICE_F_MOVE"     },
303 #endif
304 #ifdef SPLICE_F_NONBLOCK
305         { SPLICE_F_NONBLOCK, "SPLICE_F_NONBLOCK" },
306 #endif
307 #ifdef SPLICE_F_MORE
308         { SPLICE_F_MORE,     "SPLICE_F_MORE"     },
309 #endif
310 #ifdef SPLICE_F_GIFT
311         { SPLICE_F_GIFT,     "SPLICE_F_GIFT"     },
312 #endif
313         { 0,                 NULL                },
314 };
315
316 int
317 sys_tee(struct tcb *tcp)
318 {
319         if (entering(tcp)) {
320                 /* int fd_in */
321                 printfd(tcp, tcp->u_arg[0]);
322                 tprints(", ");
323                 /* int fd_out */
324                 printfd(tcp, tcp->u_arg[1]);
325                 tprints(", ");
326                 /* size_t len */
327                 tprintf("%lu, ", tcp->u_arg[2]);
328                 /* unsigned int flags */
329                 printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
330         }
331         return 0;
332 }
333
334 int
335 sys_splice(struct tcb *tcp)
336 {
337         if (entering(tcp)) {
338                 /* int fd_in */
339                 printfd(tcp, tcp->u_arg[0]);
340                 tprints(", ");
341                 /* loff_t *off_in */
342                 print_loff_t(tcp, tcp->u_arg[1]);
343                 tprints(", ");
344                 /* int fd_out */
345                 printfd(tcp, tcp->u_arg[2]);
346                 tprints(", ");
347                 /* loff_t *off_out */
348                 print_loff_t(tcp, tcp->u_arg[3]);
349                 tprints(", ");
350                 /* size_t len */
351                 tprintf("%lu, ", tcp->u_arg[4]);
352                 /* unsigned int flags */
353                 printflags(splice_flags, tcp->u_arg[5], "SPLICE_F_???");
354         }
355         return 0;
356 }
357
358 int
359 sys_vmsplice(struct tcb *tcp)
360 {
361         if (entering(tcp)) {
362                 /* int fd */
363                 printfd(tcp, tcp->u_arg[0]);
364                 tprints(", ");
365                 /* const struct iovec *iov, unsigned long nr_segs */
366                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
367                 tprints(", ");
368                 /* unsigned int flags */
369                 printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
370         }
371         return 0;
372 }
373
374 int
375 sys_ioctl(struct tcb *tcp)
376 {
377         const struct ioctlent *iop;
378
379         if (entering(tcp)) {
380                 printfd(tcp, tcp->u_arg[0]);
381                 tprints(", ");
382                 iop = ioctl_lookup(tcp->u_arg[1]);
383                 if (iop) {
384                         tprints(iop->symbol);
385                         while ((iop = ioctl_next_match(iop)))
386                                 tprintf(" or %s", iop->symbol);
387                 } else
388                         tprintf("%#lx", tcp->u_arg[1]);
389                 ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
390         }
391         else {
392                 int ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
393                 if (!ret)
394                         tprintf(", %#lx", tcp->u_arg[2]);
395                 else
396                         return ret - 1;
397         }
398         return 0;
399 }