]> granicus.if.org Git - strace/blob - io.c
Check HAVE_LONG_LONG_OFF_T when printing offset
[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 defined(LINUX) && 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 #if defined(SVR4)
172
173 int
174 sys_pread(struct tcb *tcp)
175 {
176         if (entering(tcp)) {
177                 printfd(tcp, tcp->u_arg[0]);
178                 tprints(", ");
179         } else {
180                 if (syserror(tcp))
181                         tprintf("%#lx", tcp->u_arg[1]);
182                 else
183                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
184 #if UNIXWARE
185                 /* off_t is signed int */
186                 tprintf(", %lu, %ld", tcp->u_arg[2], tcp->u_arg[3]);
187 #else
188                 tprintf(", %lu, %llu", tcp->u_arg[2],
189                         LONG_LONG(tcp->u_arg[3], tcp->u_arg[4]));
190 #endif
191         }
192         return 0;
193 }
194
195 int
196 sys_pwrite(struct tcb *tcp)
197 {
198         if (entering(tcp)) {
199                 printfd(tcp, tcp->u_arg[0]);
200                 tprints(", ");
201                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
202 #if UNIXWARE
203                 /* off_t is signed int */
204                 tprintf(", %lu, %ld", tcp->u_arg[2], tcp->u_arg[3]);
205 #else
206                 tprintf(", %lu, %llu", tcp->u_arg[2],
207                         LONG_LONG(tcp->u_arg[3], tcp->u_arg[4]));
208 #endif
209         }
210         return 0;
211 }
212
213 #if _LFS64_LARGEFILE
214 int
215 sys_pread64(struct tcb *tcp)
216 {
217         if (entering(tcp)) {
218                 printfd(tcp, tcp->u_arg[0]);
219                 tprints(", ");
220         } else {
221                 if (syserror(tcp))
222                         tprintf("%#lx", tcp->u_arg[1]);
223                 else
224                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
225                 tprintf(", %lu, ", tcp->u_arg[2]);
226                 printllval(tcp, "%#llx", 3);
227         }
228         return 0;
229 }
230
231 int
232 sys_pwrite64(struct tcb *tcp)
233 {
234         if (entering(tcp)) {
235                 printfd(tcp, tcp->u_arg[0]);
236                 tprints(", ");
237                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
238                 tprintf(", %lu, ", tcp->u_arg[2]);
239                 printllval(tcp, "%#llx", 3);
240         }
241         return 0;
242 }
243 #endif /* _LFS64_LARGEFILE */
244
245 #endif /* SVR4 */
246
247 #ifdef FREEBSD
248 #include <sys/types.h>
249 #include <sys/socket.h>
250
251 int
252 sys_sendfile(struct tcb *tcp)
253 {
254         if (entering(tcp)) {
255                 printfd(tcp, tcp->u_arg[0]);
256                 tprints(", ");
257                 printfd(tcp, tcp->u_arg[1]);
258                 tprintf(", %llu, %lu",
259                         LONG_LONG(tcp->u_arg[2], tcp->u_arg[3]),
260                         tcp->u_arg[4]);
261         } else {
262                 off_t offset;
263
264                 if (!tcp->u_arg[5])
265                         tprints(", NULL");
266                 else {
267                         struct sf_hdtr hdtr;
268
269                         if (umove(tcp, tcp->u_arg[5], &hdtr) < 0)
270                                 tprintf(", %#lx", tcp->u_arg[5]);
271                         else {
272                                 tprints(", { ");
273                                 tprint_iov(tcp, hdtr.hdr_cnt, hdtr.headers, 1);
274                                 tprintf(", %u, ", hdtr.hdr_cnt);
275                                 tprint_iov(tcp, hdtr.trl_cnt, hdtr.trailers, 1);
276                                 tprintf(", %u }", hdtr.hdr_cnt);
277                         }
278                 }
279                 if (!tcp->u_arg[6])
280                         tprints(", NULL");
281                 else if (umove(tcp, tcp->u_arg[6], &offset) < 0)
282                         tprintf(", %#lx", tcp->u_arg[6]);
283                 else
284                         tprintf(", [%llu]", offset);
285                 tprintf(", %lu", tcp->u_arg[7]);
286         }
287         return 0;
288 }
289 #endif /* FREEBSD */
290
291 #ifdef LINUX
292
293 /* The SH4 ABI does allow long longs in odd-numbered registers, but
294    does not allow them to be split between registers and memory - and
295    there are only four argument registers for normal functions.  As a
296    result pread takes an extra padding argument before the offset.  This
297    was changed late in the 2.4 series (around 2.4.20).  */
298 #if defined(SH)
299 #define PREAD_OFFSET_ARG 4
300 #else
301 #define PREAD_OFFSET_ARG 3
302 #endif
303
304 int
305 sys_pread(struct tcb *tcp)
306 {
307         if (entering(tcp)) {
308                 printfd(tcp, tcp->u_arg[0]);
309                 tprints(", ");
310         } else {
311                 if (syserror(tcp))
312                         tprintf("%#lx", tcp->u_arg[1]);
313                 else
314                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
315                 tprintf(", %lu, ", tcp->u_arg[2]);
316                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
317         }
318         return 0;
319 }
320
321 int
322 sys_pwrite(struct tcb *tcp)
323 {
324         if (entering(tcp)) {
325                 printfd(tcp, tcp->u_arg[0]);
326                 tprints(", ");
327                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
328                 tprintf(", %lu, ", tcp->u_arg[2]);
329                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
330         }
331         return 0;
332 }
333
334 #if HAVE_SYS_UIO_H
335 int
336 sys_preadv(struct tcb *tcp)
337 {
338         if (entering(tcp)) {
339                 printfd(tcp, tcp->u_arg[0]);
340                 tprints(", ");
341         } else {
342                 if (syserror(tcp)) {
343                         tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
344                         return 0;
345                 }
346                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
347                 tprintf(", %lu, ", tcp->u_arg[2]);
348                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
349         }
350         return 0;
351 }
352
353 int
354 sys_pwritev(struct tcb *tcp)
355 {
356         if (entering(tcp)) {
357                 printfd(tcp, tcp->u_arg[0]);
358                 tprints(", ");
359                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
360                 tprintf(", %lu, ", tcp->u_arg[2]);
361                 printllval(tcp, "%llu", PREAD_OFFSET_ARG);
362         }
363         return 0;
364 }
365 #endif /* HAVE_SYS_UIO_H */
366
367 int
368 sys_sendfile(struct tcb *tcp)
369 {
370         if (entering(tcp)) {
371                 off_t offset;
372
373                 printfd(tcp, tcp->u_arg[0]);
374                 tprints(", ");
375                 printfd(tcp, tcp->u_arg[1]);
376                 tprints(", ");
377                 if (!tcp->u_arg[2])
378                         tprints("NULL");
379                 else if (umove(tcp, tcp->u_arg[2], &offset) < 0)
380                         tprintf("%#lx", tcp->u_arg[2]);
381                 else
382 #ifdef HAVE_LONG_LONG_OFF_T
383                         tprintf("[%llu]", offset);
384 #else
385                         tprintf("[%lu]", offset);
386 #endif
387                 tprintf(", %lu", tcp->u_arg[3]);
388         }
389         return 0;
390 }
391
392 static void
393 print_loff_t(struct tcb *tcp, long addr)
394 {
395         loff_t offset;
396
397         if (!addr)
398                 tprints("NULL");
399         else if (umove(tcp, addr, &offset) < 0)
400                 tprintf("%#lx", addr);
401         else
402                 tprintf("[%llu]", (unsigned long long int) offset);
403 }
404
405 int
406 sys_sendfile64(struct tcb *tcp)
407 {
408         if (entering(tcp)) {
409                 printfd(tcp, tcp->u_arg[0]);
410                 tprints(", ");
411                 printfd(tcp, tcp->u_arg[1]);
412                 tprints(", ");
413                 print_loff_t(tcp, tcp->u_arg[2]);
414                 tprintf(", %lu", tcp->u_arg[3]);
415         }
416         return 0;
417 }
418
419 static const struct xlat splice_flags[] = {
420 #ifdef SPLICE_F_MOVE
421         { SPLICE_F_MOVE,     "SPLICE_F_MOVE"     },
422 #endif
423 #ifdef SPLICE_F_NONBLOCK
424         { SPLICE_F_NONBLOCK, "SPLICE_F_NONBLOCK" },
425 #endif
426 #ifdef SPLICE_F_MORE
427         { SPLICE_F_MORE,     "SPLICE_F_MORE"     },
428 #endif
429 #ifdef SPLICE_F_GIFT
430         { SPLICE_F_GIFT,     "SPLICE_F_GIFT"     },
431 #endif
432         { 0,                 NULL                },
433 };
434
435 int
436 sys_tee(struct tcb *tcp)
437 {
438         if (entering(tcp)) {
439                 /* int fd_in */
440                 printfd(tcp, tcp->u_arg[0]);
441                 tprints(", ");
442                 /* int fd_out */
443                 printfd(tcp, tcp->u_arg[1]);
444                 tprints(", ");
445                 /* size_t len */
446                 tprintf("%lu, ", tcp->u_arg[2]);
447                 /* unsigned int flags */
448                 printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
449         }
450         return 0;
451 }
452
453 int
454 sys_splice(struct tcb *tcp)
455 {
456         if (entering(tcp)) {
457                 /* int fd_in */
458                 printfd(tcp, tcp->u_arg[0]);
459                 tprints(", ");
460                 /* loff_t *off_in */
461                 print_loff_t(tcp, tcp->u_arg[1]);
462                 tprints(", ");
463                 /* int fd_out */
464                 printfd(tcp, tcp->u_arg[2]);
465                 tprints(", ");
466                 /* loff_t *off_out */
467                 print_loff_t(tcp, tcp->u_arg[3]);
468                 tprints(", ");
469                 /* size_t len */
470                 tprintf("%lu, ", tcp->u_arg[4]);
471                 /* unsigned int flags */
472                 printflags(splice_flags, tcp->u_arg[5], "SPLICE_F_???");
473         }
474         return 0;
475 }
476
477 int
478 sys_vmsplice(struct tcb *tcp)
479 {
480         if (entering(tcp)) {
481                 /* int fd */
482                 printfd(tcp, tcp->u_arg[0]);
483                 tprints(", ");
484                 /* const struct iovec *iov, unsigned long nr_segs */
485                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
486                 tprints(", ");
487                 /* unsigned int flags */
488                 printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
489         }
490         return 0;
491 }
492 #endif /* LINUX */
493
494 int
495 sys_ioctl(struct tcb *tcp)
496 {
497         const struct ioctlent *iop;
498
499         if (entering(tcp)) {
500                 printfd(tcp, tcp->u_arg[0]);
501                 tprints(", ");
502                 iop = ioctl_lookup(tcp->u_arg[1]);
503                 if (iop) {
504                         tprints(iop->symbol);
505                         while ((iop = ioctl_next_match(iop)))
506                                 tprintf(" or %s", iop->symbol);
507                 } else
508                         tprintf("%#lx", tcp->u_arg[1]);
509                 ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
510         }
511         else {
512                 int ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
513                 if (!ret)
514                         tprintf(", %#lx", tcp->u_arg[2]);
515                 else
516                         return ret - 1;
517         }
518         return 0;
519 }