]> granicus.if.org Git - strace/blobdiff - aio.c
CREDITS.in: use UTF-8 consistently
[strace] / aio.c
diff --git a/aio.c b/aio.c
index 18e53013f208c5373496299e375e7f11dc13fd26..600f88efc8446a6d2b682702c414450897a41ef9 100644 (file)
--- a/aio.c
+++ b/aio.c
@@ -3,6 +3,7 @@
  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
+ * Copyright (c) 1999-2017 The strace developers.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  */
 
 #include "defs.h"
-#ifdef HAVE_LIBAIO_H
-# include <libaio.h>
-#endif
-
-/* Not defined in libaio.h */
-#ifndef IOCB_RESFD
-# define IOCB_RESFD (1 << 0)
-#endif
+#include "print_fields.h"
+#include <linux/aio_abi.h>
 
-int
-sys_io_setup(struct tcb *tcp)
+SYS_FUNC(io_setup)
 {
        if (entering(tcp))
-               tprintf("%ld, ", tcp->u_arg[0]);
-       else {
-               if (syserror(tcp))
-                       tprintf("0x%0lx", tcp->u_arg[1]);
-               else {
-                       unsigned long user_id;
-                       if (umove(tcp, tcp->u_arg[1], &user_id) == 0)
-                               tprintf("{%lu}", user_id);
-                       else
-                               tprints("{...}");
-               }
-       }
+               tprintf("%u, ", (unsigned int) tcp->u_arg[0]);
+       else
+               printnum_ptr(tcp, tcp->u_arg[1]);
        return 0;
 }
 
-int
-sys_io_destroy(struct tcb *tcp)
+SYS_FUNC(io_destroy)
 {
-       if (entering(tcp))
-               tprintf("%lu", tcp->u_arg[0]);
-       return 0;
-}
+       printaddr(tcp->u_arg[0]);
 
-#ifdef HAVE_LIBAIO_H
+       return RVAL_DECODED;
+}
 
 enum iocb_sub {
-       SUB_NONE, SUB_COMMON, SUB_POLL, SUB_VECTOR
+       SUB_NONE, SUB_COMMON, SUB_VECTOR
 };
 
 static enum iocb_sub
-tprint_lio_opcode(unsigned cmd)
+tprint_lio_opcode(unsigned int cmd)
 {
        static const struct {
                const char *name;
                enum iocb_sub sub;
        } cmds[] = {
-               { "pread", SUB_COMMON },
-               { "pwrite", SUB_COMMON },
-               { "fsync", SUB_NONE },
-               { "fdsync", SUB_NONE },
-               { "op4", SUB_NONE },
-               { "poll", SUB_POLL },
-               { "noop", SUB_NONE },
-               { "preadv", SUB_VECTOR },
-               { "pwritev", SUB_VECTOR },
+               { "IOCB_CMD_PREAD", SUB_COMMON },
+               { "IOCB_CMD_PWRITE", SUB_COMMON },
+               { "IOCB_CMD_FSYNC", SUB_NONE },
+               { "IOCB_CMD_FDSYNC", SUB_NONE },
+               { "IOCB_CMD_PREADX", SUB_NONE },
+               { "IOCB_CMD_POLL", SUB_NONE },
+               { "IOCB_CMD_NOOP", SUB_NONE },
+               { "IOCB_CMD_PREADV", SUB_VECTOR },
+               { "IOCB_CMD_PWRITEV", SUB_VECTOR },
        };
 
        if (cmd < ARRAY_SIZE(cmds)) {
                tprints(cmds[cmd].name);
                return cmds[cmd].sub;
        }
-       tprintf("%u /* SUB_??? */", cmd);
+       tprintf("%u", cmd);
+       tprints_comment("IOCB_CMD_???");
        return SUB_NONE;
 }
 
 static void
-print_common_flags(struct iocb *iocb)
+print_common_flags(struct tcb *tcp, const struct iocb *cb)
 {
-#if HAVE_STRUCT_IOCB_U_C_FLAGS
-       if (iocb->u.c.flags & IOCB_RESFD)
-               tprintf(", resfd=%d", iocb->u.c.resfd);
-       if (iocb->u.c.flags & ~IOCB_RESFD)
-               tprintf(", flags=%x", iocb->u.c.flags);
-#else
-# warning "libaio.h is too old => limited io_submit decoding"
+/* IOCB_FLAG_RESFD is available since v2.6.22-rc1~47 */
+#ifdef IOCB_FLAG_RESFD
+       if (cb->aio_flags & IOCB_FLAG_RESFD)
+               PRINT_FIELD_FD(", ", *cb, aio_resfd, tcp);
+
+       if (cb->aio_flags & ~IOCB_FLAG_RESFD)
+               PRINT_FIELD_X(", ", *cb, aio_flags);
 #endif
 }
 
-#endif /* HAVE_LIBAIO_H */
+static bool
+iocb_is_valid(const struct iocb *cb)
+{
+       return cb->aio_buf == (unsigned long) cb->aio_buf &&
+              cb->aio_nbytes == (size_t) cb->aio_nbytes &&
+              (ssize_t) cb->aio_nbytes >= 0;
+}
 
-int
-sys_io_submit(struct tcb *tcp)
+static enum iocb_sub
+print_iocb_header(struct tcb *tcp, const struct iocb *cb)
 {
-       if (entering(tcp)) {
-#ifdef HAVE_LIBAIO_H
-               long nr = tcp->u_arg[1];
-               /* if nr <= 0, we end up printing just "{}" */
-               tprintf("%lu, %ld, {", tcp->u_arg[0], tcp->u_arg[1]);
-               {
-                       long i;
-                       struct iocb **iocbs = (void *)tcp->u_arg[2];
-//FIXME: decoding of 32-bit call by 64-bit strace
-
-                       for (i = 0; i < nr; i++, iocbs++) {
-                               enum iocb_sub sub;
-                               struct iocb *iocbp;
-                               struct iocb iocb;
-                               if (i)
-                                       tprints(", ");
-
-                               if (umove(tcp, (unsigned long)iocbs, &iocbp)) {
-                                       tprintf("%#lx", (unsigned long)iocbs);
-                                       /* No point in trying to read iocbs+1 etc */
-                                       /* (nr can be ridiculously large): */
-                                       break;
-                               }
-                               if (umove(tcp, (unsigned long)iocbp, &iocb)) {
-                                       tprintf("{%#lx}", (unsigned long)iocbp);
-                                       continue;
-                               }
-                               tprints("{");
-                               if (iocb.data)
-                                       tprintf("data:%p, ", iocb.data);
-                               if (iocb.key)
-                                       tprintf("key:%u, ", iocb.key);
-                               sub = tprint_lio_opcode(iocb.aio_lio_opcode);
-                               if (iocb.aio_reqprio)
-                                       tprintf(", reqprio:%d", iocb.aio_reqprio);
-                               tprintf(", filedes:%d", iocb.aio_fildes);
-                               switch (sub) {
-                               case SUB_COMMON:
-#if HAVE_DECL_IO_CMD_PWRITE
-                                       if (iocb.aio_lio_opcode == IO_CMD_PWRITE) {
-                                               tprints(", str:");
-                                               printstr(tcp, (unsigned long)iocb.u.c.buf,
-                                                        iocb.u.c.nbytes);
-                                       } else
-#endif
-                                               tprintf(", buf:%p", iocb.u.c.buf);
-                                       tprintf(", nbytes:%lu, offset:%lld",
-                                               iocb.u.c.nbytes,
-                                               iocb.u.c.offset);
-                                       print_common_flags(&iocb);
-                                       break;
-                               case SUB_VECTOR:
-                                       tprintf(", %lld", iocb.u.v.offset);
-                                       print_common_flags(&iocb);
-                                       tprints(", ");
-                                       tprint_iov(tcp, iocb.u.v.nr,
-                                                  (unsigned long)iocb.u.v.vec,
-#if HAVE_DECL_IO_CMD_PWRITEV
-                                                  iocb.aio_lio_opcode == IO_CMD_PWRITEV
-#else
-                                                  0
-#endif
-                                                 );
-                                       break;
-                               case SUB_POLL:
-                                       tprintf(", %x", iocb.u.poll.events);
-                                       break;
-                               case SUB_NONE:
-                                       break;
-                               }
-                               tprints("}");
-                       }
+       enum iocb_sub sub;
+
+       if (cb->aio_data){
+               PRINT_FIELD_X("", *cb, aio_data);
+               tprints(", ");
+       }
+
+       if (cb->aio_key) {
+               PRINT_FIELD_U("", *cb, aio_key);
+               tprints(", ");
+       }
+
+       tprints("aio_lio_opcode=");
+       sub = tprint_lio_opcode(cb->aio_lio_opcode);
+       if (cb->aio_reqprio)
+               PRINT_FIELD_D(", ", *cb, aio_reqprio);
+
+       PRINT_FIELD_FD(", ", *cb, aio_fildes, tcp);
+
+       return sub;
+}
+
+static void
+print_iocb(struct tcb *tcp, const struct iocb *cb)
+{
+       enum iocb_sub sub = print_iocb_header(tcp, cb);
+
+       switch (sub) {
+       case SUB_COMMON:
+               if (cb->aio_lio_opcode == 1 && iocb_is_valid(cb)) {
+                       PRINT_FIELD_STRN(", ", *cb, aio_buf,
+                                        cb->aio_nbytes, tcp);
+               } else {
+                       PRINT_FIELD_X(", ", *cb, aio_buf);
                }
-               tprints("}");
-#else
-# warning "libaio.h is not available => no io_submit decoding"
-               tprintf("%lu, %ld, %#lx", tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
-#endif
+               PRINT_FIELD_U(", ", *cb, aio_nbytes);
+               PRINT_FIELD_D(", ", *cb, aio_offset);
+               print_common_flags(tcp, cb);
+               break;
+       case SUB_VECTOR:
+               if (iocb_is_valid(cb)) {
+                       tprints(", aio_buf=");
+                       tprint_iov(tcp, cb->aio_nbytes, cb->aio_buf,
+                                  cb->aio_lio_opcode == 8
+                                  ? IOV_DECODE_STR
+                                  : IOV_DECODE_ADDR);
+               } else {
+                       PRINT_FIELD_X(", ", *cb, aio_buf);
+                       PRINT_FIELD_U(", ", *cb, aio_nbytes);
+               }
+               PRINT_FIELD_D(", ", *cb, aio_offset);
+               print_common_flags(tcp, cb);
+               break;
+       case SUB_NONE:
+               break;
        }
-       return 0;
 }
 
-int
-sys_io_cancel(struct tcb *tcp)
+static bool
+print_iocbp(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
 {
-       if (entering(tcp)) {
-#ifdef HAVE_LIBAIO_H
-               struct iocb iocb;
-#endif
-               tprintf("%lu, ", tcp->u_arg[0]);
-#ifdef HAVE_LIBAIO_H
-               if (umove(tcp, tcp->u_arg[1], &iocb) == 0) {
-                       tprintf("{%p, %u, %u, %u, %d}, ",
-                               iocb.data, iocb.key,
-                               (unsigned)iocb.aio_lio_opcode,
-                               (unsigned)iocb.aio_reqprio, iocb.aio_fildes);
-               } else
-#endif
-                       tprints("{...}, ");
+       kernel_ulong_t addr;
+       struct iocb cb;
+
+       if (elem_size < sizeof(kernel_ulong_t)) {
+               addr = *(unsigned int *) elem_buf;
        } else {
-               if (tcp->u_rval < 0)
-                       tprints("{...}");
-               else {
-#ifdef HAVE_LIBAIO_H
-                       struct io_event event;
-                       if (umove(tcp, tcp->u_arg[2], &event) == 0)
-                               tprintf("{%p, %p, %ld, %ld}",
-                                       event.data, event.obj,
-                                       event.res, event.res2);
-                       else
-#endif
-                               tprints("{...}");
+               addr = *(kernel_ulong_t *) elem_buf;
+       }
+
+       tprints("{");
+       if (!umove_or_printaddr(tcp, addr, &cb))
+               print_iocb(tcp, &cb);
+       tprints("}");
+
+       return true;
+}
+
+SYS_FUNC(io_submit)
+{
+       const kernel_long_t nr =
+               truncate_klong_to_current_wordsize(tcp->u_arg[1]);
+       const kernel_ulong_t addr = tcp->u_arg[2];
+       kernel_ulong_t iocbp;
+
+       printaddr(tcp->u_arg[0]);
+       tprintf(", %" PRI_kld ", ", nr);
+
+       if (nr < 0)
+               printaddr(addr);
+       else
+               print_array(tcp, addr, nr, &iocbp, current_wordsize,
+                           umoven_or_printaddr, print_iocbp, 0);
+
+       return RVAL_DECODED;
+}
+
+static bool
+print_io_event(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
+{
+       struct io_event *event = elem_buf;
+
+       PRINT_FIELD_X("{", *event, data);
+       PRINT_FIELD_X(", ", *event, obj);
+       PRINT_FIELD_D(", ", *event, res);
+       PRINT_FIELD_D(", ", *event, res2);
+       tprints("}");
+
+       return true;
+}
+
+SYS_FUNC(io_cancel)
+{
+       if (entering(tcp)) {
+               printaddr(tcp->u_arg[0]);
+               tprints(", ");
+
+               struct iocb cb;
+
+               if (!umove_or_printaddr(tcp, tcp->u_arg[1], &cb)) {
+                       tprints("{");
+                       print_iocb_header(tcp, &cb);
+                       tprints("}");
                }
+               tprints(", ");
+       } else {
+               struct io_event event;
+
+               if (!umove_or_printaddr(tcp, tcp->u_arg[2], &event))
+                       print_io_event(tcp, &event, sizeof(event), 0);
        }
        return 0;
 }
 
-int
-sys_io_getevents(struct tcb *tcp)
+SYS_FUNC(io_getevents)
 {
        if (entering(tcp)) {
-               tprintf("%ld, %ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1],
-                       tcp->u_arg[2]);
+               printaddr(tcp->u_arg[0]);
+               tprintf(", %" PRI_kld ", %" PRI_kld ", ",
+                       truncate_klong_to_current_wordsize(tcp->u_arg[1]),
+                       truncate_klong_to_current_wordsize(tcp->u_arg[2]));
        } else {
-               if (tcp->u_rval == 0) {
-                       tprints("{}");
-               } else {
-#ifdef HAVE_LIBAIO_H
-                       struct io_event *events = (void *)tcp->u_arg[3];
-                       long i, nr = tcp->u_rval;
-
-                       for (i = 0; i < nr; i++, events++) {
-                               struct io_event event;
-
-                               if (i == 0)
-                                       tprints("{");
-                               else
-                                       tprints(", ");
-
-                               if (umove(tcp, (unsigned long)events, &event) != 0) {
-                                       tprints("{...}");
-                                       continue;
-                               }
-                               tprintf("{%p, %p, %ld, %ld}", event.data,
-                                       event.obj, event.res, event.res2);
-                       }
-                       tprints("}, ");
-#else
-                       tprints("{...}");
-#endif
-               }
-
+               struct io_event buf;
+               print_array(tcp, tcp->u_arg[3], tcp->u_rval, &buf, sizeof(buf),
+                           umoven_or_printaddr, print_io_event, 0);
+               tprints(", ");
+               /*
+                * Since the timeout parameter is read by the kernel
+                * on entering syscall, it has to be decoded the same way
+                * whether the syscall has failed or not.
+                */
+               temporarily_clear_syserror(tcp);
                print_timespec(tcp, tcp->u_arg[4]);
+               restore_cleared_syserror(tcp);
        }
        return 0;
 }