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