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