]> granicus.if.org Git - strace/blob - desc.c
Stop using external libaio.h.
[strace] / desc.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 #include <sys/file.h>
34 #ifdef HAVE_SYS_EPOLL_H
35 # include <sys/epoll.h>
36 #endif
37 #ifdef HAVE_LINUX_PERF_EVENT_H
38 # include  <linux/perf_event.h>
39 #endif
40
41 /* --- Copied from libaio-0.3.109/src/libaio.h ---
42  * Why keep a copy instead of using external libaio.h?
43  * Because we want to properly decode 32-bit aio calls
44  * by 64-bit strace. For that, we need more definitions than
45  * libaio.h provides. (TODO).
46  * Keeping our local 32-bit compat defs in sync with libaio.h
47  * _without seeing libaio structs_ is hard/more bug-prone.
48  * A smaller benefit is that we don't need libaio installed.
49  */
50 #define HAVE_LIBAIO_H 1
51 typedef enum io_iocb_cmd {
52         IO_CMD_PREAD = 0,
53         IO_CMD_PWRITE = 1,
54
55         IO_CMD_FSYNC = 2,
56         IO_CMD_FDSYNC = 3,
57
58         IO_CMD_POLL = 5, /* Never implemented in mainline, see io_prep_poll */
59         IO_CMD_NOOP = 6,
60         IO_CMD_PREADV = 7,
61         IO_CMD_PWRITEV = 8,
62 } io_iocb_cmd_t;
63
64 #if defined(__i386__) /* little endian, 32 bits */
65 #define PADDED(x, y)    x; unsigned y
66 #define PADDEDptr(x, y) x; unsigned y
67 #define PADDEDul(x, y)  unsigned long x; unsigned y
68 #elif defined(__ia64__) || defined(__x86_64__) || defined(__alpha__)
69 #define PADDED(x, y)    x, y
70 #define PADDEDptr(x, y) x
71 #define PADDEDul(x, y)  unsigned long x
72 #elif defined(__powerpc64__) /* big endian, 64 bits */
73 #define PADDED(x, y)    unsigned y; x
74 #define PADDEDptr(x,y)  x
75 #define PADDEDul(x, y)  unsigned long x
76 #elif defined(__PPC__)  /* big endian, 32 bits */
77 #define PADDED(x, y)    unsigned y; x
78 #define PADDEDptr(x, y) unsigned y; x
79 #define PADDEDul(x, y)  unsigned y; unsigned long x
80 #elif defined(__s390x__) /* big endian, 64 bits */
81 #define PADDED(x, y)    unsigned y; x
82 #define PADDEDptr(x,y)  x
83 #define PADDEDul(x, y)  unsigned long x
84 #elif defined(__s390__) /* big endian, 32 bits */
85 #define PADDED(x, y)    unsigned y; x
86 #define PADDEDptr(x, y) unsigned y; x
87 #define PADDEDul(x, y)  unsigned y; unsigned long x
88 #elif defined(__arm__)
89 #  if defined (__ARMEB__) /* big endian, 32 bits */
90 #define PADDED(x, y)    unsigned y; x
91 #define PADDEDptr(x, y) unsigned y; x
92 #define PADDEDul(x, y)  unsigned y; unsigned long x
93 #  else                   /* little endian, 32 bits */
94 #define PADDED(x, y)    x; unsigned y
95 #define PADDEDptr(x, y) x; unsigned y
96 #define PADDEDul(x, y)  unsigned long x; unsigned y
97 #  endif
98 #else
99 #  warning No AIO definitions for this architecture => no io_submit decoding
100 #  undef HAVE_LIBAIO_H
101 #endif
102
103 #ifdef HAVE_LIBAIO_H
104 struct io_iocb_poll {
105         PADDED(int events, __pad1);
106 };      /* result code is the set of result flags or -'ve errno */
107
108 struct io_iocb_sockaddr {
109         struct sockaddr *addr;
110         int             len;
111 };      /* result code is the length of the sockaddr, or -'ve errno */
112
113 struct io_iocb_common {
114         PADDEDptr(void  *buf, __pad1);
115         PADDEDul(nbytes, __pad2);
116         long long       offset;
117         long long       __pad3;
118         unsigned        flags;
119         unsigned        resfd;
120 };      /* result code is the amount read or -'ve errno */
121
122 struct io_iocb_vector {
123         const struct iovec      *vec;
124         int                     nr;
125         long long               offset;
126 };      /* result code is the amount read or -'ve errno */
127
128 struct iocb {
129         PADDEDptr(void *data, __pad1);  /* Return in the io completion event */
130         PADDED(unsigned key, __pad2);   /* For use in identifying io requests */
131
132         short           aio_lio_opcode;
133         short           aio_reqprio;
134         int             aio_fildes;
135
136         union {
137                 struct io_iocb_common           c;
138                 struct io_iocb_vector           v;
139                 struct io_iocb_poll             poll;
140                 struct io_iocb_sockaddr saddr;
141         } u;
142 };
143
144 struct io_event {
145         PADDEDptr(void *data, __pad1);
146         PADDEDptr(struct iocb *obj,  __pad2);
147         PADDEDul(res,  __pad3);
148         PADDEDul(res2, __pad4);
149 };
150
151 #undef PADDED
152 #undef PADDEDptr
153 #undef PADDEDul
154
155 #endif /* HAVE_LIBAIO_H */
156
157 /* --- End of a chunk of libaio.h --- */
158 /* Not defined in libaio.h */
159 #ifndef IOCB_RESFD
160 # define IOCB_RESFD (1 << 0)
161 #endif
162
163
164 static const struct xlat fcntlcmds[] = {
165         { F_DUPFD,      "F_DUPFD"       },
166         { F_GETFD,      "F_GETFD"       },
167         { F_SETFD,      "F_SETFD"       },
168         { F_GETFL,      "F_GETFL"       },
169         { F_SETFL,      "F_SETFL"       },
170         { F_GETLK,      "F_GETLK"       },
171         { F_SETLK,      "F_SETLK"       },
172         { F_SETLKW,     "F_SETLKW"      },
173         { F_GETOWN,     "F_GETOWN"      },
174         { F_SETOWN,     "F_SETOWN"      },
175 #ifdef F_RSETLK
176         { F_RSETLK,     "F_RSETLK"      },
177 #endif
178 #ifdef F_RSETLKW
179         { F_RSETLKW,    "F_RSETLKW"     },
180 #endif
181 #ifdef F_RGETLK
182         { F_RGETLK,     "F_RGETLK"      },
183 #endif
184 #ifdef F_CNVT
185         { F_CNVT,       "F_CNVT"        },
186 #endif
187 #ifdef F_SETSIG
188         { F_SETSIG,     "F_SETSIG"      },
189 #endif
190 #ifdef F_GETSIG
191         { F_GETSIG,     "F_GETSIG"      },
192 #endif
193 #ifdef F_CHKFL
194         { F_CHKFL,      "F_CHKFL"       },
195 #endif
196 #ifdef F_DUP2FD
197         { F_DUP2FD,     "F_DUP2FD"      },
198 #endif
199 #ifdef F_ALLOCSP
200         { F_ALLOCSP,    "F_ALLOCSP"     },
201 #endif
202 #ifdef F_ISSTREAM
203         { F_ISSTREAM,   "F_ISSTREAM"    },
204 #endif
205 #ifdef F_PRIV
206         { F_PRIV,       "F_PRIV"        },
207 #endif
208 #ifdef F_NPRIV
209         { F_NPRIV,      "F_NPRIV"       },
210 #endif
211 #ifdef F_QUOTACL
212         { F_QUOTACL,    "F_QUOTACL"     },
213 #endif
214 #ifdef F_BLOCKS
215         { F_BLOCKS,     "F_BLOCKS"      },
216 #endif
217 #ifdef F_BLKSIZE
218         { F_BLKSIZE,    "F_BLKSIZE"     },
219 #endif
220 #ifdef F_GETOWN
221         { F_GETOWN,     "F_GETOWN"      },
222 #endif
223 #ifdef F_SETOWN
224         { F_SETOWN,     "F_SETOWN"      },
225 #endif
226 #ifdef F_REVOKE
227         { F_REVOKE,     "F_REVOKE"      },
228 #endif
229 #ifdef F_SETLK
230         { F_SETLK,      "F_SETLK"       },
231 #endif
232 #ifdef F_SETLKW
233         { F_SETLKW,     "F_SETLKW"      },
234 #endif
235 #ifdef F_FREESP
236         { F_FREESP,     "F_FREESP"      },
237 #endif
238 #ifdef F_GETLK
239         { F_GETLK,      "F_GETLK"       },
240 #endif
241 #ifdef F_SETLK64
242         { F_SETLK64,    "F_SETLK64"     },
243 #endif
244 #ifdef F_SETLKW64
245         { F_SETLKW64,   "F_SETLKW64"    },
246 #endif
247 #ifdef F_FREESP64
248         { F_FREESP64,   "F_FREESP64"    },
249 #endif
250 #ifdef F_GETLK64
251         { F_GETLK64,    "F_GETLK64"     },
252 #endif
253 #ifdef F_SHARE
254         { F_SHARE,      "F_SHARE"       },
255 #endif
256 #ifdef F_UNSHARE
257         { F_UNSHARE,    "F_UNSHARE"     },
258 #endif
259 #ifdef F_SETLEASE
260         { F_SETLEASE,   "F_SETLEASE"    },
261 #endif
262 #ifdef F_GETLEASE
263         { F_GETLEASE,   "F_GETLEASE"    },
264 #endif
265 #ifdef F_NOTIFY
266         { F_NOTIFY,     "F_NOTIFY"      },
267 #endif
268 #ifdef F_DUPFD_CLOEXEC
269         { F_DUPFD_CLOEXEC,"F_DUPFD_CLOEXEC"},
270 #endif
271         { 0,            NULL            },
272 };
273
274 static const struct xlat fdflags[] = {
275 #ifdef FD_CLOEXEC
276         { FD_CLOEXEC,   "FD_CLOEXEC"    },
277 #endif
278         { 0,            NULL            },
279 };
280
281 #ifdef LOCK_SH
282
283 static const struct xlat flockcmds[] = {
284         { LOCK_SH,      "LOCK_SH"       },
285         { LOCK_EX,      "LOCK_EX"       },
286         { LOCK_NB,      "LOCK_NB"       },
287         { LOCK_UN,      "LOCK_UN"       },
288         { 0,            NULL            },
289 };
290
291 #endif /* LOCK_SH */
292
293 static const struct xlat lockfcmds[] = {
294         { F_RDLCK,      "F_RDLCK"       },
295         { F_WRLCK,      "F_WRLCK"       },
296         { F_UNLCK,      "F_UNLCK"       },
297 #ifdef F_EXLCK
298         { F_EXLCK,      "F_EXLCK"       },
299 #endif
300 #ifdef F_SHLCK
301         { F_SHLCK,      "F_SHLCK"       },
302 #endif
303         { 0,            NULL            },
304 };
305
306 #ifdef F_NOTIFY
307 static const struct xlat notifyflags[] = {
308 #ifdef DN_ACCESS
309         { DN_ACCESS,    "DN_ACCESS"     },
310 #endif
311 #ifdef DN_MODIFY
312         { DN_MODIFY,    "DN_MODIFY"     },
313 #endif
314 #ifdef DN_CREATE
315         { DN_CREATE,    "DN_CREATE"     },
316 #endif
317 #ifdef DN_DELETE
318         { DN_DELETE,    "DN_DELETE"     },
319 #endif
320 #ifdef DN_RENAME
321         { DN_RENAME,    "DN_RENAME"     },
322 #endif
323 #ifdef DN_ATTRIB
324         { DN_ATTRIB,    "DN_ATTRIB"     },
325 #endif
326 #ifdef DN_MULTISHOT
327         { DN_MULTISHOT, "DN_MULTISHOT"  },
328 #endif
329         { 0,            NULL            },
330 };
331 #endif
332
333 static const struct xlat perf_event_open_flags[] = {
334 #ifdef PERF_FLAG_FD_NO_GROUP
335         { PERF_FLAG_FD_NO_GROUP,        "PERF_FLAG_FD_NO_GROUP" },
336 #endif
337 #ifdef PERF_FLAG_FD_OUTPUT
338         { PERF_FLAG_FD_OUTPUT,          "PERF_FLAG_FD_OUTPUT"   },
339 #endif
340 #ifdef PERF_FLAG_PID_CGROUP
341         { PERF_FLAG_PID_CGROUP,         "PERF_FLAG_PID_CGROUP"  },
342 #endif
343         { 0,                            NULL                    },
344 };
345
346 #if _LFS64_LARGEFILE
347 /* fcntl/lockf */
348 static void
349 printflock64(struct tcb *tcp, long addr, int getlk)
350 {
351         struct flock64 fl;
352
353         if (umove(tcp, addr, &fl) < 0) {
354                 tprints("{...}");
355                 return;
356         }
357         tprints("{type=");
358         printxval(lockfcmds, fl.l_type, "F_???");
359         tprints(", whence=");
360         printxval(whence_codes, fl.l_whence, "SEEK_???");
361         tprintf(", start=%lld, len=%lld", (long long) fl.l_start, (long long) fl.l_len);
362         if (getlk)
363                 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
364         else
365                 tprints("}");
366 }
367 #endif
368
369 /* fcntl/lockf */
370 static void
371 printflock(struct tcb *tcp, long addr, int getlk)
372 {
373         struct flock fl;
374
375 #if SUPPORTED_PERSONALITIES > 1
376 # ifdef X32
377         if (current_personality == 0) {
378                 printflock64(tcp, addr, getlk);
379                 return;
380         }
381 # endif
382         if (current_wordsize != sizeof(fl.l_start)) {
383                 if (current_wordsize == 4) {
384                         /* 32-bit x86 app on x86_64 and similar cases */
385                         struct {
386                                 short int l_type;
387                                 short int l_whence;
388                                 int32_t l_start; /* off_t */
389                                 int32_t l_len; /* off_t */
390                                 int32_t l_pid; /* pid_t */
391                         } fl32;
392                         if (umove(tcp, addr, &fl32) < 0) {
393                                 tprints("{...}");
394                                 return;
395                         }
396                         fl.l_type = fl32.l_type;
397                         fl.l_whence = fl32.l_whence;
398                         fl.l_start = fl32.l_start;
399                         fl.l_len = fl32.l_len;
400                         fl.l_pid = fl32.l_pid;
401                 } else {
402                         /* let people know we have a problem here */
403                         tprintf("<decode error: unsupported wordsize %d>",
404                                 current_wordsize);
405                         return;
406                 }
407         } else
408 #endif
409         {
410                 if (umove(tcp, addr, &fl) < 0) {
411                         tprints("{...}");
412                         return;
413                 }
414         }
415         tprints("{type=");
416         printxval(lockfcmds, fl.l_type, "F_???");
417         tprints(", whence=");
418         printxval(whence_codes, fl.l_whence, "SEEK_???");
419 #ifdef X32
420         tprintf(", start=%lld, len=%lld", fl.l_start, fl.l_len);
421 #else
422         tprintf(", start=%ld, len=%ld", fl.l_start, fl.l_len);
423 #endif
424         if (getlk)
425                 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
426         else
427                 tprints("}");
428 }
429
430 int
431 sys_fcntl(struct tcb *tcp)
432 {
433         if (entering(tcp)) {
434                 printfd(tcp, tcp->u_arg[0]);
435                 tprints(", ");
436                 printxval(fcntlcmds, tcp->u_arg[1], "F_???");
437                 switch (tcp->u_arg[1]) {
438                 case F_SETFD:
439                         tprints(", ");
440                         printflags(fdflags, tcp->u_arg[2], "FD_???");
441                         break;
442                 case F_SETOWN: case F_DUPFD:
443 #ifdef F_DUPFD_CLOEXEC
444                 case F_DUPFD_CLOEXEC:
445 #endif
446                         tprintf(", %ld", tcp->u_arg[2]);
447                         break;
448                 case F_SETFL:
449                         tprints(", ");
450                         tprint_open_modes(tcp->u_arg[2]);
451                         break;
452                 case F_SETLK: case F_SETLKW:
453 #ifdef F_FREESP
454                 case F_FREESP:
455 #endif
456                         tprints(", ");
457                         printflock(tcp, tcp->u_arg[2], 0);
458                         break;
459 #if _LFS64_LARGEFILE
460 #ifdef F_FREESP64
461                 case F_FREESP64:
462 #endif
463                 /* Linux glibc defines SETLK64 as SETLK,
464                    even though the kernel has different values - as does Solaris. */
465 #if defined(F_SETLK64) && F_SETLK64 + 0 != F_SETLK
466                 case F_SETLK64:
467 #endif
468 #if defined(F_SETLKW64) && F_SETLKW64 + 0 != F_SETLKW
469                 case F_SETLKW64:
470 #endif
471                         tprints(", ");
472                         printflock64(tcp, tcp->u_arg[2], 0);
473                         break;
474 #endif
475 #ifdef F_NOTIFY
476                 case F_NOTIFY:
477                         tprints(", ");
478                         printflags(notifyflags, tcp->u_arg[2], "DN_???");
479                         break;
480 #endif
481 #ifdef F_SETLEASE
482                 case F_SETLEASE:
483                         tprints(", ");
484                         printxval(lockfcmds, tcp->u_arg[2], "F_???");
485                         break;
486 #endif
487                 }
488         }
489         else {
490                 switch (tcp->u_arg[1]) {
491                 case F_DUPFD:
492 #ifdef F_DUPFD_CLOEXEC
493                 case F_DUPFD_CLOEXEC:
494 #endif
495                 case F_SETFD: case F_SETFL:
496                 case F_SETLK: case F_SETLKW:
497                 case F_SETOWN: case F_GETOWN:
498 #ifdef F_NOTIFY
499                 case F_NOTIFY:
500 #endif
501 #ifdef F_SETLEASE
502                 case F_SETLEASE:
503 #endif
504                         break;
505                 case F_GETFD:
506                         if (syserror(tcp) || tcp->u_rval == 0)
507                                 return 0;
508                         tcp->auxstr = sprintflags("flags ", fdflags, tcp->u_rval);
509                         return RVAL_HEX|RVAL_STR;
510                 case F_GETFL:
511                         if (syserror(tcp))
512                                 return 0;
513                         tcp->auxstr = sprint_open_modes(tcp->u_rval);
514                         return RVAL_HEX|RVAL_STR;
515                 case F_GETLK:
516                         tprints(", ");
517                         printflock(tcp, tcp->u_arg[2], 1);
518                         break;
519 #if _LFS64_LARGEFILE
520 #if defined(F_GETLK64) && F_GETLK64+0 != F_GETLK
521                 case F_GETLK64:
522 #endif
523                         tprints(", ");
524                         printflock64(tcp, tcp->u_arg[2], 1);
525                         break;
526 #endif
527 #ifdef F_GETLEASE
528                 case F_GETLEASE:
529                         if (syserror(tcp))
530                                 return 0;
531                         tcp->auxstr = xlookup(lockfcmds, tcp->u_rval);
532                         return RVAL_HEX|RVAL_STR;
533 #endif
534                 default:
535                         tprintf(", %#lx", tcp->u_arg[2]);
536                         break;
537                 }
538         }
539         return 0;
540 }
541
542 #ifdef LOCK_SH
543
544 int
545 sys_flock(struct tcb *tcp)
546 {
547         if (entering(tcp)) {
548                 printfd(tcp, tcp->u_arg[0]);
549                 tprints(", ");
550                 printflags(flockcmds, tcp->u_arg[1], "LOCK_???");
551         }
552         return 0;
553 }
554 #endif /* LOCK_SH */
555
556 int
557 sys_close(struct tcb *tcp)
558 {
559         if (entering(tcp)) {
560                 printfd(tcp, tcp->u_arg[0]);
561         }
562         return 0;
563 }
564
565 static int
566 do_dup2(struct tcb *tcp, int flags_arg)
567 {
568         if (entering(tcp)) {
569                 printfd(tcp, tcp->u_arg[0]);
570                 tprints(", ");
571                 printfd(tcp, tcp->u_arg[1]);
572                 if (flags_arg >= 0) {
573                         tprints(", ");
574                         printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
575                 }
576         }
577         return 0;
578 }
579
580 int
581 sys_dup2(struct tcb *tcp)
582 {
583         return do_dup2(tcp, -1);
584 }
585
586 int
587 sys_dup3(struct tcb *tcp)
588 {
589         return do_dup2(tcp, 2);
590 }
591
592 #if defined(ALPHA)
593 int
594 sys_getdtablesize(struct tcb *tcp)
595 {
596         return 0;
597 }
598 #endif
599
600 static int
601 decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
602 {
603         int i, j;
604         int nfds, fdsize;
605         fd_set *fds;
606         const char *sep;
607         long arg;
608
609         /* Kernel truncates arg[0] to int, we do the same. */
610         nfds = (int) args[0];
611
612         /* Kernel rejects negative nfds, so we don't parse it either. */
613         if (nfds < 0) {
614                 nfds = 0;
615                 fds = NULL;
616         }
617         /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
618         if (nfds > 1024*1024)
619                 nfds = 1024*1024;
620
621         /*
622          * We had bugs a-la "while (j < args[0])" and "umoven(args[0])" below.
623          * Instead of args[0], use nfds for fd count, fdsize for array lengths.
624          */
625         fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
626
627         if (entering(tcp)) {
628                 tprintf("%d", (int) args[0]);
629
630                 if (fdsize > 0) {
631                         fds = malloc(fdsize);
632                         if (!fds)
633                                 die_out_of_memory();
634                 }
635                 for (i = 0; i < 3; i++) {
636                         arg = args[i+1];
637                         if (arg == 0) {
638                                 tprints(", NULL");
639                                 continue;
640                         }
641                         if (!verbose(tcp) || !fds) {
642                                 tprintf(", %#lx", arg);
643                                 continue;
644                         }
645                         if (umoven(tcp, arg, fdsize, (char *) fds) < 0) {
646                                 tprints(", [?]");
647                                 continue;
648                         }
649                         tprints(", [");
650                         for (j = 0, sep = "";; j++) {
651                                 j = next_set_bit(fds, j, nfds);
652                                 if (j < 0)
653                                         break;
654                                 tprints(sep);
655                                 printfd(tcp, j);
656                                 sep = " ";
657                         }
658                         tprints("]");
659                 }
660                 free(fds);
661                 tprints(", ");
662                 printtv_bitness(tcp, args[4], bitness, 0);
663         }
664         else {
665                 static char outstr[1024];
666                 char *outptr;
667 #define end_outstr (outstr + sizeof(outstr))
668                 int ready_fds;
669
670                 if (syserror(tcp))
671                         return 0;
672
673                 ready_fds = tcp->u_rval;
674                 if (ready_fds == 0) {
675                         tcp->auxstr = "Timeout";
676                         return RVAL_STR;
677                 }
678
679                 fds = malloc(fdsize);
680                 if (!fds)
681                         die_out_of_memory();
682
683                 outptr = outstr;
684                 sep = "";
685                 for (i = 0; i < 3 && ready_fds > 0; i++) {
686                         int first = 1;
687
688                         arg = args[i+1];
689                         if (!arg || umoven(tcp, arg, fdsize, (char *) fds) < 0)
690                                 continue;
691                         for (j = 0;; j++) {
692                                 j = next_set_bit(fds, j, nfds);
693                                 if (j < 0)
694                                         break;
695                                 /* +2 chars needed at the end: ']',NUL */
696                                 if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
697                                         if (first) {
698                                                 outptr += sprintf(outptr, "%s%s [%u",
699                                                         sep,
700                                                         i == 0 ? "in" : i == 1 ? "out" : "except",
701                                                         j
702                                                 );
703                                                 first = 0;
704                                                 sep = ", ";
705                                         }
706                                         else {
707                                                 outptr += sprintf(outptr, " %u", j);
708                                         }
709                                 }
710                                 if (--ready_fds == 0)
711                                         break;
712                         }
713                         if (outptr != outstr)
714                                 *outptr++ = ']';
715                 }
716                 free(fds);
717                 /* This contains no useful information on SunOS.  */
718                 if (args[4]) {
719                         if (outptr < end_outstr - (10 + TIMEVAL_TEXT_BUFSIZE)) {
720                                 outptr += sprintf(outptr, "%sleft ", sep);
721                                 outptr = sprinttv(outptr, tcp, args[4], bitness, /*special:*/ 0);
722                         }
723                 }
724                 *outptr = '\0';
725                 tcp->auxstr = outstr;
726                 return RVAL_STR;
727 #undef end_outstr
728         }
729         return 0;
730 }
731
732 int
733 sys_oldselect(struct tcb *tcp)
734 {
735         long args[5];
736
737         if (umoven(tcp, tcp->u_arg[0], sizeof args, (char *) args) < 0) {
738                 tprints("[...]");
739                 return 0;
740         }
741         return decode_select(tcp, args, BITNESS_CURRENT);
742 }
743
744 #ifdef ALPHA
745 int
746 sys_osf_select(struct tcb *tcp)
747 {
748         long *args = tcp->u_arg;
749         return decode_select(tcp, args, BITNESS_32);
750 }
751 #endif
752
753 static const struct xlat epollctls[] = {
754 #ifdef EPOLL_CTL_ADD
755         { EPOLL_CTL_ADD,        "EPOLL_CTL_ADD" },
756 #endif
757 #ifdef EPOLL_CTL_MOD
758         { EPOLL_CTL_MOD,        "EPOLL_CTL_MOD" },
759 #endif
760 #ifdef EPOLL_CTL_DEL
761         { EPOLL_CTL_DEL,        "EPOLL_CTL_DEL" },
762 #endif
763         { 0,                    NULL            }
764 };
765
766 static const struct xlat epollevents[] = {
767 #ifdef EPOLLIN
768         { EPOLLIN,      "EPOLLIN"       },
769 #endif
770 #ifdef EPOLLPRI
771         { EPOLLPRI,     "EPOLLPRI"      },
772 #endif
773 #ifdef EPOLLOUT
774         { EPOLLOUT,     "EPOLLOUT"      },
775 #endif
776 #ifdef EPOLLRDNORM
777         { EPOLLRDNORM,  "EPOLLRDNORM"   },
778 #endif
779 #ifdef EPOLLRDBAND
780         { EPOLLRDBAND,  "EPOLLRDBAND"   },
781 #endif
782 #ifdef EPOLLWRNORM
783         { EPOLLWRNORM,  "EPOLLWRNORM"   },
784 #endif
785 #ifdef EPOLLWRBAND
786         { EPOLLWRBAND,  "EPOLLWRBAND"   },
787 #endif
788 #ifdef EPOLLMSG
789         { EPOLLMSG,     "EPOLLMSG"      },
790 #endif
791 #ifdef EPOLLERR
792         { EPOLLERR,     "EPOLLERR"      },
793 #endif
794 #ifdef EPOLLHUP
795         { EPOLLHUP,     "EPOLLHUP"      },
796 #endif
797 #ifdef EPOLLRDHUP
798         { EPOLLRDHUP,   "EPOLLRDHUP"    },
799 #endif
800 #ifdef EPOLLONESHOT
801         { EPOLLONESHOT, "EPOLLONESHOT"  },
802 #endif
803 #ifdef EPOLLET
804         { EPOLLET,      "EPOLLET"       },
805 #endif
806         { 0,            NULL            }
807 };
808
809 /* Not aliased to printargs_ld: we want it to have a distinct address */
810 int
811 sys_epoll_create(struct tcb *tcp)
812 {
813         return printargs_ld(tcp);
814 }
815
816 static const struct xlat epollflags[] = {
817 #ifdef EPOLL_CLOEXEC
818         { EPOLL_CLOEXEC,        "EPOLL_CLOEXEC" },
819 #endif
820 #ifdef EPOLL_NONBLOCK
821         { EPOLL_NONBLOCK,       "EPOLL_NONBLOCK"        },
822 #endif
823         { 0,            NULL            }
824 };
825
826 int
827 sys_epoll_create1(struct tcb *tcp)
828 {
829         if (entering(tcp))
830                 printflags(epollflags, tcp->u_arg[0], "EPOLL_???");
831         return 0;
832 }
833
834 #ifdef HAVE_SYS_EPOLL_H
835 static void
836 print_epoll_event(struct epoll_event *ev)
837 {
838         tprints("{");
839         printflags(epollevents, ev->events, "EPOLL???");
840         /* We cannot know what format the program uses, so print u32 and u64
841            which will cover every value.  */
842         tprintf(", {u32=%" PRIu32 ", u64=%" PRIu64 "}}",
843                 ev->data.u32, ev->data.u64);
844 }
845 #endif
846
847 int
848 sys_epoll_ctl(struct tcb *tcp)
849 {
850         if (entering(tcp)) {
851                 printfd(tcp, tcp->u_arg[0]);
852                 tprints(", ");
853                 printxval(epollctls, tcp->u_arg[1], "EPOLL_CTL_???");
854                 tprints(", ");
855                 printfd(tcp, tcp->u_arg[2]);
856                 tprints(", ");
857                 if (tcp->u_arg[3] == 0)
858                         tprints("NULL");
859                 else {
860 #ifdef HAVE_SYS_EPOLL_H
861                         struct epoll_event ev;
862                         if (umove(tcp, tcp->u_arg[3], &ev) == 0)
863                                 print_epoll_event(&ev);
864                         else
865 #endif
866                                 tprints("{...}");
867                 }
868         }
869         return 0;
870 }
871
872 static void
873 epoll_wait_common(struct tcb *tcp)
874 {
875         if (entering(tcp)) {
876                 printfd(tcp, tcp->u_arg[0]);
877                 tprints(", ");
878         } else {
879                 if (syserror(tcp))
880                         tprintf("%lx", tcp->u_arg[1]);
881                 else if (tcp->u_rval == 0)
882                         tprints("{}");
883                 else {
884 #ifdef HAVE_SYS_EPOLL_H
885                         struct epoll_event ev, *start, *cur, *end;
886                         int failed = 0;
887
888                         tprints("{");
889                         start = (struct epoll_event *) tcp->u_arg[1];
890                         end = start + tcp->u_rval;
891                         for (cur = start; cur < end; ++cur) {
892                                 if (cur > start)
893                                         tprints(", ");
894                                 if (umove(tcp, (long) cur, &ev) == 0)
895                                         print_epoll_event(&ev);
896                                 else {
897                                         tprints("?");
898                                         failed = 1;
899                                         break;
900                                 }
901                         }
902                         tprints("}");
903                         if (failed)
904                                 tprintf(" %#lx", (long) start);
905 #else
906                         tprints("{...}");
907 #endif
908                 }
909                 tprintf(", %d, %d", (int) tcp->u_arg[2], (int) tcp->u_arg[3]);
910         }
911 }
912
913 int
914 sys_epoll_wait(struct tcb *tcp)
915 {
916         epoll_wait_common(tcp);
917         return 0;
918 }
919
920 int
921 sys_epoll_pwait(struct tcb *tcp)
922 {
923         epoll_wait_common(tcp);
924         if (exiting(tcp)) {
925                 tprints(", ");
926                 /* NB: kernel requires arg[5] == NSIG / 8 */
927                 print_sigset_addr_len(tcp, tcp->u_arg[4], tcp->u_arg[5]);
928                 tprintf(", %lu", tcp->u_arg[5]);
929         }
930         return 0;
931 }
932
933 int
934 sys_io_setup(struct tcb *tcp)
935 {
936         if (entering(tcp))
937                 tprintf("%ld, ", tcp->u_arg[0]);
938         else {
939                 if (syserror(tcp))
940                         tprintf("0x%0lx", tcp->u_arg[1]);
941                 else {
942                         unsigned long user_id;
943                         if (umove(tcp, tcp->u_arg[1], &user_id) == 0)
944                                 tprintf("{%lu}", user_id);
945                         else
946                                 tprints("{...}");
947                 }
948         }
949         return 0;
950 }
951
952 int
953 sys_io_destroy(struct tcb *tcp)
954 {
955         if (entering(tcp))
956                 tprintf("%lu", tcp->u_arg[0]);
957         return 0;
958 }
959
960 #ifdef HAVE_LIBAIO_H
961
962 enum iocb_sub {
963         SUB_NONE, SUB_COMMON, SUB_POLL, SUB_VECTOR
964 };
965
966 static enum iocb_sub
967 tprint_lio_opcode(unsigned cmd)
968 {
969         static const struct {
970                 const char *name;
971                 enum iocb_sub sub;
972         } cmds[] = {
973                 { "pread", SUB_COMMON },
974                 { "pwrite", SUB_COMMON },
975                 { "fsync", SUB_NONE },
976                 { "fdsync", SUB_NONE },
977                 { "op4", SUB_NONE },
978                 { "poll", SUB_POLL },
979                 { "noop", SUB_NONE },
980                 { "preadv", SUB_VECTOR },
981                 { "pwritev", SUB_VECTOR },
982         };
983
984         if (cmd < ARRAY_SIZE(cmds)) {
985                 tprints(cmds[cmd].name);
986                 return cmds[cmd].sub;
987         }
988         tprintf("%u /* SUB_??? */", cmd);
989         return SUB_NONE;
990 }
991
992 static void
993 print_common_flags(struct iocb *iocb)
994 {
995         if (iocb->u.c.flags & IOCB_RESFD)
996                 tprintf(", resfd=%d", iocb->u.c.resfd);
997         if (iocb->u.c.flags & ~IOCB_RESFD)
998                 tprintf(", flags=%x", iocb->u.c.flags);
999 }
1000
1001 #endif /* HAVE_LIBAIO_H */
1002
1003 int
1004 sys_io_submit(struct tcb *tcp)
1005 {
1006         if (entering(tcp)) {
1007 #ifdef HAVE_LIBAIO_H
1008                 long nr = tcp->u_arg[1];
1009                 /* if nr <= 0, we end up printing just "{}" */
1010                 tprintf("%lu, %ld, {", tcp->u_arg[0], tcp->u_arg[1]);
1011                 {
1012                         long i;
1013                         struct iocb **iocbs = (void *)tcp->u_arg[2];
1014 //FIXME: decoding of 32-bit call by 64-bit strace
1015
1016                         for (i = 0; i < nr; i++, iocbs++) {
1017                                 enum iocb_sub sub;
1018                                 struct iocb *iocbp;
1019                                 struct iocb iocb;
1020                                 if (i)
1021                                         tprints(", ");
1022
1023                                 if (umove(tcp, (unsigned long)iocbs, &iocbp)) {
1024                                         tprintf("%#lx", (unsigned long)iocbs);
1025                                         /* No point in trying to read iocbs+1 etc */
1026                                         /* (nr can be ridiculously large): */
1027                                         break;
1028                                 }
1029                                 if (umove(tcp, (unsigned long)iocbp, &iocb)) {
1030                                         tprintf("{%#lx}", (unsigned long)iocbp);
1031                                         continue;
1032                                 }
1033                                 tprints("{");
1034                                 if (iocb.data)
1035                                         tprintf("data:%p, ", iocb.data);
1036                                 if (iocb.key)
1037                                         tprintf("key:%u, ", iocb.key);
1038                                 sub = tprint_lio_opcode(iocb.aio_lio_opcode);
1039                                 if (iocb.aio_reqprio)
1040                                         tprintf(", reqprio:%d", iocb.aio_reqprio);
1041                                 tprintf(", filedes:%d", iocb.aio_fildes);
1042                                 switch (sub) {
1043                                 case SUB_COMMON:
1044                                         if (iocb.aio_lio_opcode == IO_CMD_PWRITE) {
1045                                                 tprints(", str:");
1046                                                 printstr(tcp, (unsigned long)iocb.u.c.buf,
1047                                                          iocb.u.c.nbytes);
1048                                         } else
1049                                                 tprintf(", buf:%p", iocb.u.c.buf);
1050                                         tprintf(", nbytes:%lu, offset:%lld",
1051                                                 iocb.u.c.nbytes,
1052                                                 iocb.u.c.offset);
1053                                         print_common_flags(&iocb);
1054                                         break;
1055                                 case SUB_VECTOR:
1056                                         tprintf(", %lld", iocb.u.v.offset);
1057                                         print_common_flags(&iocb);
1058                                         tprints(", ");
1059                                         tprint_iov(tcp, iocb.u.v.nr,
1060                                                    (unsigned long)iocb.u.v.vec,
1061                                                    iocb.aio_lio_opcode == IO_CMD_PWRITEV
1062                                                   );
1063                                         break;
1064                                 case SUB_POLL:
1065                                         tprintf(", %x", iocb.u.poll.events);
1066                                         break;
1067                                 case SUB_NONE:
1068                                         break;
1069                                 }
1070                                 tprints("}");
1071                         }
1072                 }
1073                 tprints("}");
1074 #else
1075                 tprintf("%lu, %ld, %#lx", tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
1076 #endif
1077         }
1078         return 0;
1079 }
1080
1081 int
1082 sys_io_cancel(struct tcb *tcp)
1083 {
1084         if (entering(tcp)) {
1085 #ifdef HAVE_LIBAIO_H
1086                 struct iocb iocb;
1087 #endif
1088                 tprintf("%lu, ", tcp->u_arg[0]);
1089 #ifdef HAVE_LIBAIO_H
1090                 if (umove(tcp, tcp->u_arg[1], &iocb) == 0) {
1091                         tprintf("{%p, %u, %u, %u, %d}, ",
1092                                 iocb.data, iocb.key,
1093                                 (unsigned)iocb.aio_lio_opcode,
1094                                 (unsigned)iocb.aio_reqprio, iocb.aio_fildes);
1095                 } else
1096 #endif
1097                         tprints("{...}, ");
1098         } else {
1099                 if (tcp->u_rval < 0)
1100                         tprints("{...}");
1101                 else {
1102 #ifdef HAVE_LIBAIO_H
1103                         struct io_event event;
1104                         if (umove(tcp, tcp->u_arg[2], &event) == 0)
1105                                 tprintf("{%p, %p, %ld, %ld}",
1106                                         event.data, event.obj,
1107                                         event.res, event.res2);
1108                         else
1109 #endif
1110                                 tprints("{...}");
1111                 }
1112         }
1113         return 0;
1114 }
1115
1116 int
1117 sys_io_getevents(struct tcb *tcp)
1118 {
1119         if (entering(tcp)) {
1120                 tprintf("%ld, %ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1],
1121                         tcp->u_arg[2]);
1122         } else {
1123                 if (tcp->u_rval == 0) {
1124                         tprints("{}");
1125                 } else {
1126 #ifdef HAVE_LIBAIO_H
1127                         struct io_event *events = (void *)tcp->u_arg[3];
1128                         long i, nr = tcp->u_rval;
1129
1130                         for (i = 0; i < nr; i++, events++) {
1131                                 struct io_event event;
1132
1133                                 if (i == 0)
1134                                         tprints("{");
1135                                 else
1136                                         tprints(", ");
1137
1138                                 if (umove(tcp, (unsigned long)events, &event) != 0) {
1139                                         tprints("{...}");
1140                                         continue;
1141                                 }
1142                                 tprintf("{%p, %p, %ld, %ld}", event.data,
1143                                         event.obj, event.res, event.res2);
1144                         }
1145                         tprints("}, ");
1146 #else
1147                         tprints("{...}");
1148 #endif
1149                 }
1150
1151                 print_timespec(tcp, tcp->u_arg[4]);
1152         }
1153         return 0;
1154 }
1155
1156 int
1157 sys_select(struct tcb *tcp)
1158 {
1159         return decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
1160 }
1161
1162 int
1163 sys_pselect6(struct tcb *tcp)
1164 {
1165         int rc = decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
1166         if (entering(tcp)) {
1167                 long r;
1168                 struct {
1169                         unsigned long ptr;
1170                         unsigned long len;
1171                 } data;
1172 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1173                 if (current_wordsize == 4) {
1174                         struct {
1175                                 uint32_t ptr;
1176                                 uint32_t len;
1177                         } data32;
1178                         r = umove(tcp, tcp->u_arg[5], &data32);
1179                         data.ptr = data32.ptr;
1180                         data.len = data32.len;
1181                 } else
1182 #endif
1183                         r = umove(tcp, tcp->u_arg[5], &data);
1184                 if (r < 0)
1185                         tprintf(", %#lx", tcp->u_arg[5]);
1186                 else {
1187                         tprints(", {");
1188                         /* NB: kernel requires data.len == NSIG / 8 */
1189                         print_sigset_addr_len(tcp, data.ptr, data.len);
1190                         tprintf(", %lu}", data.len);
1191                 }
1192         }
1193         return rc;
1194 }
1195
1196 static int
1197 do_eventfd(struct tcb *tcp, int flags_arg)
1198 {
1199         if (entering(tcp)) {
1200                 tprintf("%lu", tcp->u_arg[0]);
1201                 if (flags_arg >= 0) {
1202                         tprints(", ");
1203                         printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
1204                 }
1205         }
1206         return 0;
1207 }
1208
1209 int
1210 sys_eventfd(struct tcb *tcp)
1211 {
1212         return do_eventfd(tcp, -1);
1213 }
1214
1215 int
1216 sys_eventfd2(struct tcb *tcp)
1217 {
1218         return do_eventfd(tcp, 1);
1219 }
1220
1221 int
1222 sys_perf_event_open(struct tcb *tcp)
1223 {
1224         if (entering(tcp)) {
1225                 tprintf("%#lx, %d, %d, %d, ",
1226                         tcp->u_arg[0],
1227                         (int) tcp->u_arg[1],
1228                         (int) tcp->u_arg[2],
1229                         (int) tcp->u_arg[3]);
1230                 printflags(perf_event_open_flags, tcp->u_arg[4],
1231                            "PERF_FLAG_???");
1232         }
1233         return 0;
1234 }