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