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