]> granicus.if.org Git - strace/blob - file.c
db024ef83891d3ffde0784f7e9ec13eb22fe0895
[strace] / file.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 <dirent.h>
36
37 #ifdef LINUX
38 struct kernel_dirent {
39         unsigned long   d_ino;
40         unsigned long   d_off;
41         unsigned short  d_reclen;
42         char            d_name[1];
43 };
44 #else
45 # define kernel_dirent dirent
46 #endif
47
48 #ifdef LINUX
49 #  ifdef LINUXSPARC
50 struct stat {
51         unsigned short  st_dev;
52         unsigned int    st_ino;
53         unsigned short  st_mode;
54         short           st_nlink;
55         unsigned short  st_uid;
56         unsigned short  st_gid;
57         unsigned short  st_rdev;
58         unsigned int    st_size;
59         int             st_atime;
60         unsigned int    __unused1;
61         int             st_mtime;
62         unsigned int    __unused2;
63         int             st_ctime;
64         unsigned int    __unused3;
65         int             st_blksize;
66         int             st_blocks;
67         unsigned int    __unused4[2];
68 };
69 #if defined(SPARC64)
70 struct stat_sparc64 {
71         unsigned int    st_dev;
72         unsigned long   st_ino;
73         unsigned int    st_mode;
74         unsigned int    st_nlink;
75         unsigned int    st_uid;
76         unsigned int    st_gid;
77         unsigned int    st_rdev;
78         long            st_size;
79         long            st_atime;
80         long            st_mtime;
81         long            st_ctime;
82         long            st_blksize;
83         long            st_blocks;
84         unsigned long   __unused4[2];
85 };
86 #endif /* SPARC64 */
87 #    define stat kernel_stat
88 #    include <asm/stat.h>
89 #    undef stat
90 #  else
91 #    undef dev_t
92 #    undef ino_t
93 #    undef mode_t
94 #    undef nlink_t
95 #    undef uid_t
96 #    undef gid_t
97 #    undef off_t
98 #    undef loff_t
99
100 #    define dev_t __kernel_dev_t
101 #    define ino_t __kernel_ino_t
102 #    define mode_t __kernel_mode_t
103 #    define nlink_t __kernel_nlink_t
104 #    define uid_t __kernel_uid_t
105 #    define gid_t __kernel_gid_t
106 #    define off_t __kernel_off_t
107 #    define loff_t __kernel_loff_t
108
109 #    include <asm/stat.h>
110
111 #    undef dev_t
112 #    undef ino_t
113 #    undef mode_t
114 #    undef nlink_t
115 #    undef uid_t
116 #    undef gid_t
117 #    undef off_t
118 #    undef loff_t
119
120 #    define dev_t dev_t
121 #    define ino_t ino_t
122 #    define mode_t mode_t
123 #    define nlink_t nlink_t
124 #    define uid_t uid_t
125 #    define gid_t gid_t
126 #    define off_t off_t
127 #    define loff_t loff_t
128 #  endif
129 #  ifdef HPPA   /* asm-parisc/stat.h defines stat64 */
130 #    undef stat64
131 #  endif
132 #  define stat libc_stat
133 #  define stat64 libc_stat64
134 #  include <sys/stat.h>
135 #  undef stat
136 #  undef stat64
137    /* These might be macros. */
138 #  undef st_atime
139 #  undef st_mtime
140 #  undef st_ctime
141 #  ifdef HPPA
142 #    define stat64 hpux_stat64
143 #  endif
144 #else
145 #  include <sys/stat.h>
146 #endif
147
148 #include <fcntl.h>
149
150 #ifdef SVR4
151 #  include <sys/cred.h>
152 #endif /* SVR4 */
153
154 #ifdef HAVE_SYS_VFS_H
155 #include <sys/vfs.h>
156 #endif
157
158 #ifdef HAVE_LINUX_XATTR_H
159 #include <linux/xattr.h>
160 #elif defined linux
161 #define XATTR_CREATE 1
162 #define XATTR_REPLACE 2
163 #endif
164
165 #ifdef FREEBSD
166 #include <sys/param.h>
167 #include <sys/mount.h>
168 #include <sys/stat.h>
169 #endif
170
171 #if _LFS64_LARGEFILE && (defined(LINUX) || defined(SVR4))
172 # ifdef HAVE_INTTYPES_H
173 #  include <inttypes.h>
174 # else
175 #  define PRId64 "lld"
176 #  define PRIu64 "llu"
177 # endif
178 #endif
179
180 #if HAVE_LONG_LONG_OFF_T
181 /*
182  * Ugly hacks for systems that have typedef long long off_t
183  */
184
185 #define stat64 stat
186 #define HAVE_STAT64 1   /* Ugly hack */
187
188 #define sys_stat64      sys_stat
189 #define sys_fstat64     sys_fstat
190 #define sys_lstat64     sys_lstat
191 #define sys_lseek64     sys_lseek
192 #define sys_truncate64  sys_truncate
193 #define sys_ftruncate64 sys_ftruncate
194 #endif
195
196 #ifdef MAJOR_IN_SYSMACROS
197 #include <sys/sysmacros.h>
198 #endif
199
200 #ifdef MAJOR_IN_MKDEV
201 #include <sys/mkdev.h>
202 #endif
203
204 #ifdef HAVE_SYS_ASYNCH_H
205 #include <sys/asynch.h>
206 #endif
207
208 #ifdef SUNOS4
209 #include <ustat.h>
210 #endif
211
212 const struct xlat open_access_modes[] = {
213         { O_RDONLY,     "O_RDONLY"      },
214         { O_WRONLY,     "O_WRONLY"      },
215         { O_RDWR,       "O_RDWR"        },
216 #ifdef O_ACCMODE
217         { O_ACCMODE,    "O_ACCMODE"     },
218 #endif
219         { 0,            NULL            },
220 };
221
222 const struct xlat open_mode_flags[] = {
223         { O_CREAT,      "O_CREAT"       },
224         { O_EXCL,       "O_EXCL"        },
225         { O_NOCTTY,     "O_NOCTTY"      },
226         { O_TRUNC,      "O_TRUNC"       },
227         { O_APPEND,     "O_APPEND"      },
228         { O_NONBLOCK,   "O_NONBLOCK"    },
229 #ifdef O_SYNC
230         { O_SYNC,       "O_SYNC"        },
231 #endif
232 #ifdef O_ASYNC
233         { O_ASYNC,      "O_ASYNC"       },
234 #endif
235 #ifdef O_DSYNC
236         { O_DSYNC,      "O_DSYNC"       },
237 #endif
238 #ifdef O_RSYNC
239         { O_RSYNC,      "O_RSYNC"       },
240 #endif
241 #if defined(O_NDELAY) && (O_NDELAY != O_NONBLOCK)
242         { O_NDELAY,     "O_NDELAY"      },
243 #endif
244 #ifdef O_PRIV
245         { O_PRIV,       "O_PRIV"        },
246 #endif
247 #ifdef O_DIRECT
248         { O_DIRECT,     "O_DIRECT"      },
249 #endif
250 #ifdef O_LARGEFILE
251 # if O_LARGEFILE == 0           /* biarch platforms in 64-bit mode */
252 #  undef O_LARGEFILE
253 #  ifdef SPARC64
254 #   define O_LARGEFILE  0x40000
255 #  elif defined X86_64 || defined S390X
256 #   define O_LARGEFILE  0100000
257 #  endif
258 # endif
259 # ifdef O_LARGEFILE
260         { O_LARGEFILE,  "O_LARGEFILE"   },
261 # endif
262 #endif
263 #ifdef O_DIRECTORY
264         { O_DIRECTORY,  "O_DIRECTORY"   },
265 #endif
266 #ifdef O_NOFOLLOW
267         { O_NOFOLLOW,   "O_NOFOLLOW"    },
268 #endif
269 #ifdef O_NOATIME
270         { O_NOATIME,    "O_NOATIME"     },
271 #endif
272 #ifdef O_CLOEXEC
273         { O_CLOEXEC,    "O_CLOEXEC"     },
274 #endif
275 #ifdef FNDELAY
276         { FNDELAY,      "FNDELAY"       },
277 #endif
278 #ifdef FAPPEND
279         { FAPPEND,      "FAPPEND"       },
280 #endif
281 #ifdef FMARK
282         { FMARK,        "FMARK"         },
283 #endif
284 #ifdef FDEFER
285         { FDEFER,       "FDEFER"        },
286 #endif
287 #ifdef FASYNC
288         { FASYNC,       "FASYNC"        },
289 #endif
290 #ifdef FSHLOCK
291         { FSHLOCK,      "FSHLOCK"       },
292 #endif
293 #ifdef FEXLOCK
294         { FEXLOCK,      "FEXLOCK"       },
295 #endif
296 #ifdef FCREAT
297         { FCREAT,       "FCREAT"        },
298 #endif
299 #ifdef FTRUNC
300         { FTRUNC,       "FTRUNC"        },
301 #endif
302 #ifdef FEXCL
303         { FEXCL,        "FEXCL"         },
304 #endif
305 #ifdef FNBIO
306         { FNBIO,        "FNBIO"         },
307 #endif
308 #ifdef FSYNC
309         { FSYNC,        "FSYNC"         },
310 #endif
311 #ifdef FNOCTTY
312         { FNOCTTY,      "FNOCTTY"       },
313 #endif
314 #ifdef O_SHLOCK
315         { O_SHLOCK,     "O_SHLOCK"      },
316 #endif
317 #ifdef O_EXLOCK
318         { O_EXLOCK,     "O_EXLOCK"      },
319 #endif
320         { 0,            NULL            },
321 };
322
323 #ifdef LINUX
324
325 #ifndef AT_FDCWD
326 # define AT_FDCWD                -100
327 #endif
328
329 /* The fd is an "int", so when decoding x86 on x86_64, we need to force sign
330  * extension to get the right value.  We do this by declaring fd as int here.
331  */
332 static void
333 print_dirfd(struct tcb *tcp, int fd)
334 {
335         if (fd == AT_FDCWD)
336                 tprints("AT_FDCWD, ");
337         else {
338                 printfd(tcp, fd);
339                 tprints(", ");
340         }
341 }
342 #endif
343
344 /*
345  * low bits of the open(2) flags define access mode,
346  * other bits are real flags.
347  */
348 const char *
349 sprint_open_modes(mode_t flags)
350 {
351         static char outstr[(1 + ARRAY_SIZE(open_mode_flags)) * sizeof("O_LARGEFILE")];
352         char *p;
353         char sep;
354         const char *str;
355         const struct xlat *x;
356
357         sep = ' ';
358         p = stpcpy(outstr, "flags");
359         str = xlookup(open_access_modes, flags & 3);
360         if (str) {
361                 *p++ = sep;
362                 p = stpcpy(p, str);
363                 flags &= ~3;
364                 if (!flags)
365                         return outstr;
366                 sep = '|';
367         }
368
369         for (x = open_mode_flags; x->str; x++) {
370                 if ((flags & x->val) == x->val) {
371                         *p++ = sep;
372                         p = stpcpy(p, x->str);
373                         flags &= ~x->val;
374                         if (!flags)
375                                 return outstr;
376                         sep = '|';
377                 }
378         }
379         /* flags is still nonzero */
380         *p++ = sep;
381         sprintf(p, "%#x", flags);
382         return outstr;
383 }
384
385 void
386 tprint_open_modes(mode_t flags)
387 {
388         tprints(sprint_open_modes(flags) + sizeof("flags"));
389 }
390
391 static int
392 decode_open(struct tcb *tcp, int offset)
393 {
394         if (entering(tcp)) {
395                 printpath(tcp, tcp->u_arg[offset]);
396                 tprints(", ");
397                 /* flags */
398                 tprint_open_modes(tcp->u_arg[offset + 1]);
399                 if (tcp->u_arg[offset + 1] & O_CREAT) {
400                         /* mode */
401                         tprintf(", %#lo", tcp->u_arg[offset + 2]);
402                 }
403         }
404         return 0;
405 }
406
407 int
408 sys_open(struct tcb *tcp)
409 {
410         return decode_open(tcp, 0);
411 }
412
413 #ifdef LINUX
414 int
415 sys_openat(struct tcb *tcp)
416 {
417         if (entering(tcp))
418                 print_dirfd(tcp, tcp->u_arg[0]);
419         return decode_open(tcp, 1);
420 }
421 #endif
422
423 #ifdef LINUXSPARC
424 static const struct xlat openmodessol[] = {
425         { 0,            "O_RDWR"        },
426         { 1,            "O_RDONLY"      },
427         { 2,            "O_WRONLY"      },
428         { 0x80,         "O_NONBLOCK"    },
429         { 8,            "O_APPEND"      },
430         { 0x100,        "O_CREAT"       },
431         { 0x200,        "O_TRUNC"       },
432         { 0x400,        "O_EXCL"        },
433         { 0x800,        "O_NOCTTY"      },
434         { 0x10,         "O_SYNC"        },
435         { 0x40,         "O_DSYNC"       },
436         { 0x8000,       "O_RSYNC"       },
437         { 4,            "O_NDELAY"      },
438         { 0x1000,       "O_PRIV"        },
439         { 0,            NULL            },
440 };
441
442 int
443 solaris_open(struct tcb *tcp)
444 {
445         if (entering(tcp)) {
446                 printpath(tcp, tcp->u_arg[0]);
447                 tprints(", ");
448                 /* flags */
449                 printflags(openmodessol, tcp->u_arg[1] + 1, "O_???");
450                 if (tcp->u_arg[1] & 0x100) {
451                         /* mode */
452                         tprintf(", %#lo", tcp->u_arg[2]);
453                 }
454         }
455         return 0;
456 }
457
458 #endif
459
460 int
461 sys_creat(struct tcb *tcp)
462 {
463         if (entering(tcp)) {
464                 printpath(tcp, tcp->u_arg[0]);
465                 tprintf(", %#lo", tcp->u_arg[1]);
466         }
467         return 0;
468 }
469
470 static const struct xlat access_flags[] = {
471         { F_OK,         "F_OK",         },
472         { R_OK,         "R_OK"          },
473         { W_OK,         "W_OK"          },
474         { X_OK,         "X_OK"          },
475 #ifdef EFF_ONLY_OK
476         { EFF_ONLY_OK,  "EFF_ONLY_OK"   },
477 #endif
478 #ifdef EX_OK
479         { EX_OK,        "EX_OK"         },
480 #endif
481         { 0,            NULL            },
482 };
483
484 static int
485 decode_access(struct tcb *tcp, int offset)
486 {
487         if (entering(tcp)) {
488                 printpath(tcp, tcp->u_arg[offset]);
489                 tprints(", ");
490                 printflags(access_flags, tcp->u_arg[offset + 1], "?_OK");
491         }
492         return 0;
493 }
494
495 int
496 sys_access(struct tcb *tcp)
497 {
498         return decode_access(tcp, 0);
499 }
500
501 #ifdef LINUX
502 int
503 sys_faccessat(struct tcb *tcp)
504 {
505         if (entering(tcp))
506                 print_dirfd(tcp, tcp->u_arg[0]);
507         return decode_access(tcp, 1);
508 }
509 #endif
510
511 int
512 sys_umask(struct tcb *tcp)
513 {
514         if (entering(tcp)) {
515                 tprintf("%#lo", tcp->u_arg[0]);
516         }
517         return RVAL_OCTAL;
518 }
519
520 static const struct xlat whence[] = {
521         { SEEK_SET,     "SEEK_SET"      },
522         { SEEK_CUR,     "SEEK_CUR"      },
523         { SEEK_END,     "SEEK_END"      },
524         { 0,            NULL            },
525 };
526
527 #ifndef HAVE_LONG_LONG_OFF_T
528 #if defined (LINUX_MIPSN32)
529 int
530 sys_lseek(struct tcb *tcp)
531 {
532         long long offset;
533         int _whence;
534
535         if (entering(tcp)) {
536                 printfd(tcp, tcp->u_arg[0]);
537                 tprints(", ");
538                 offset = tcp->ext_arg[1];
539                 _whence = tcp->u_arg[2];
540                 if (_whence == SEEK_SET)
541                         tprintf("%llu, ", offset);
542                 else
543                         tprintf("%lld, ", offset);
544                 printxval(whence, _whence, "SEEK_???");
545         }
546         return RVAL_UDECIMAL;
547 }
548 #else /* !LINUX_MIPSN32 */
549 int
550 sys_lseek(struct tcb *tcp)
551 {
552         off_t offset;
553         int _whence;
554
555         if (entering(tcp)) {
556                 printfd(tcp, tcp->u_arg[0]);
557                 tprints(", ");
558                 offset = tcp->u_arg[1];
559                 _whence = tcp->u_arg[2];
560                 if (_whence == SEEK_SET)
561                         tprintf("%lu, ", offset);
562                 else
563                         tprintf("%ld, ", offset);
564                 printxval(whence, _whence, "SEEK_???");
565         }
566         return RVAL_UDECIMAL;
567 }
568 #endif /* LINUX_MIPSN32 */
569 #endif
570
571 #ifdef LINUX
572 int
573 sys_llseek(struct tcb *tcp)
574 {
575         if (entering(tcp)) {
576                 printfd(tcp, tcp->u_arg[0]);
577                 /*
578                  * This one call takes explicitly two 32-bit arguments hi, lo,
579                  * rather than one 64-bit argument for which LONG_LONG works
580                  * appropriate for the native byte order.
581                  */
582                 if (tcp->u_arg[4] == SEEK_SET)
583                         tprintf(", %llu, ",
584                                 ((long long int) tcp->u_arg[1]) << 32 |
585                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
586                 else
587                         tprintf(", %lld, ",
588                                 ((long long int) tcp->u_arg[1]) << 32 |
589                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
590         }
591         else {
592                 long long int off;
593                 if (syserror(tcp) || umove(tcp, tcp->u_arg[3], &off) < 0)
594                         tprintf("%#lx, ", tcp->u_arg[3]);
595                 else
596                         tprintf("[%llu], ", off);
597                 printxval(whence, tcp->u_arg[4], "SEEK_???");
598         }
599         return 0;
600 }
601
602 int
603 sys_readahead(struct tcb *tcp)
604 {
605         if (entering(tcp)) {
606                 int argn;
607                 printfd(tcp, tcp->u_arg[0]);
608                 tprints(", ");
609                 argn = printllval(tcp, "%lld", 1);
610                 tprintf(", %ld", tcp->u_arg[argn]);
611         }
612         return 0;
613 }
614 #endif
615
616 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
617 int
618 sys_lseek64(struct tcb *tcp)
619 {
620         if (entering(tcp)) {
621                 int argn;
622                 printfd(tcp, tcp->u_arg[0]);
623                 tprints(", ");
624                 if (tcp->u_arg[3] == SEEK_SET)
625                         argn = printllval(tcp, "%llu, ", 1);
626                 else
627                         argn = printllval(tcp, "%lld, ", 1);
628                 printxval(whence, tcp->u_arg[argn], "SEEK_???");
629         }
630         return RVAL_LUDECIMAL;
631 }
632 #endif
633
634 #ifndef HAVE_LONG_LONG_OFF_T
635 int
636 sys_truncate(struct tcb *tcp)
637 {
638         if (entering(tcp)) {
639                 printpath(tcp, tcp->u_arg[0]);
640                 tprintf(", %lu", tcp->u_arg[1]);
641         }
642         return 0;
643 }
644 #endif
645
646 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
647 int
648 sys_truncate64(struct tcb *tcp)
649 {
650         if (entering(tcp)) {
651                 printpath(tcp, tcp->u_arg[0]);
652                 printllval(tcp, ", %llu", 1);
653         }
654         return 0;
655 }
656 #endif
657
658 #ifndef HAVE_LONG_LONG_OFF_T
659 int
660 sys_ftruncate(struct tcb *tcp)
661 {
662         if (entering(tcp)) {
663                 printfd(tcp, tcp->u_arg[0]);
664                 tprintf(", %lu", tcp->u_arg[1]);
665         }
666         return 0;
667 }
668 #endif
669
670 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
671 int
672 sys_ftruncate64(struct tcb *tcp)
673 {
674         if (entering(tcp)) {
675                 printfd(tcp, tcp->u_arg[0]);
676                 tprints(", ");
677                 printllval(tcp, "%llu", 1);
678         }
679         return 0;
680 }
681 #endif
682
683 /* several stats */
684
685 static const struct xlat modetypes[] = {
686         { S_IFREG,      "S_IFREG"       },
687         { S_IFSOCK,     "S_IFSOCK"      },
688         { S_IFIFO,      "S_IFIFO"       },
689         { S_IFLNK,      "S_IFLNK"       },
690         { S_IFDIR,      "S_IFDIR"       },
691         { S_IFBLK,      "S_IFBLK"       },
692         { S_IFCHR,      "S_IFCHR"       },
693         { 0,            NULL            },
694 };
695
696 static const char *
697 sprintmode(int mode)
698 {
699         static char buf[64];
700         const char *s;
701
702         if ((mode & S_IFMT) == 0)
703                 s = "";
704         else if ((s = xlookup(modetypes, mode & S_IFMT)) == NULL) {
705                 sprintf(buf, "%#o", mode);
706                 return buf;
707         }
708         sprintf(buf, "%s%s%s%s", s,
709                 (mode & S_ISUID) ? "|S_ISUID" : "",
710                 (mode & S_ISGID) ? "|S_ISGID" : "",
711                 (mode & S_ISVTX) ? "|S_ISVTX" : "");
712         mode &= ~(S_IFMT|S_ISUID|S_ISGID|S_ISVTX);
713         if (mode)
714                 sprintf(buf + strlen(buf), "|%#o", mode);
715         s = (*buf == '|') ? buf + 1 : buf;
716         return *s ? s : "0";
717 }
718
719 static char *
720 sprinttime(time_t t)
721 {
722         struct tm *tmp;
723         static char buf[32];
724
725         if (t == 0) {
726                 strcpy(buf, "0");
727                 return buf;
728         }
729         tmp = localtime(&t);
730         if (tmp)
731                 snprintf(buf, sizeof buf, "%02d/%02d/%02d-%02d:%02d:%02d",
732                         tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
733                         tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
734         else
735                 snprintf(buf, sizeof buf, "%lu", (unsigned long) t);
736
737         return buf;
738 }
739
740 #ifdef LINUXSPARC
741 typedef struct {
742         int     tv_sec;
743         int     tv_nsec;
744 } timestruct_t;
745
746 struct solstat {
747         unsigned        st_dev;
748         int             st_pad1[3];     /* network id */
749         unsigned        st_ino;
750         unsigned        st_mode;
751         unsigned        st_nlink;
752         unsigned        st_uid;
753         unsigned        st_gid;
754         unsigned        st_rdev;
755         int             st_pad2[2];
756         int             st_size;
757         int             st_pad3;        /* st_size, off_t expansion */
758         timestruct_t    st_atime;
759         timestruct_t    st_mtime;
760         timestruct_t    st_ctime;
761         int             st_blksize;
762         int             st_blocks;
763         char            st_fstype[16];
764         int             st_pad4[8];     /* expansion area */
765 };
766
767 static void
768 printstatsol(struct tcb *tcp, long addr)
769 {
770         struct solstat statbuf;
771
772         if (umove(tcp, addr, &statbuf) < 0) {
773                 tprints("{...}");
774                 return;
775         }
776         if (!abbrev(tcp)) {
777                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
778                         (unsigned long) ((statbuf.st_dev >> 18) & 0x3fff),
779                         (unsigned long) (statbuf.st_dev & 0x3ffff),
780                         (unsigned long) statbuf.st_ino,
781                         sprintmode(statbuf.st_mode));
782                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
783                         (unsigned long) statbuf.st_nlink,
784                         (unsigned long) statbuf.st_uid,
785                         (unsigned long) statbuf.st_gid);
786                 tprintf("st_blksize=%lu, ", (unsigned long) statbuf.st_blksize);
787                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
788         }
789         else
790                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
791         switch (statbuf.st_mode & S_IFMT) {
792         case S_IFCHR: case S_IFBLK:
793                 tprintf("st_rdev=makedev(%lu, %lu), ",
794                         (unsigned long) ((statbuf.st_rdev >> 18) & 0x3fff),
795                         (unsigned long) (statbuf.st_rdev & 0x3ffff));
796                 break;
797         default:
798                 tprintf("st_size=%u, ", statbuf.st_size);
799                 break;
800         }
801         if (!abbrev(tcp)) {
802                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime.tv_sec));
803                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime.tv_sec));
804                 tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime.tv_sec));
805         }
806         else
807                 tprints("...}");
808 }
809
810 #if defined (SPARC64)
811 static void
812 printstat_sparc64(struct tcb *tcp, long addr)
813 {
814         struct stat_sparc64 statbuf;
815
816         if (umove(tcp, addr, &statbuf) < 0) {
817                 tprints("{...}");
818                 return;
819         }
820
821         if (!abbrev(tcp)) {
822                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
823                         (unsigned long) major(statbuf.st_dev),
824                         (unsigned long) minor(statbuf.st_dev),
825                         (unsigned long) statbuf.st_ino,
826                         sprintmode(statbuf.st_mode));
827                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
828                         (unsigned long) statbuf.st_nlink,
829                         (unsigned long) statbuf.st_uid,
830                         (unsigned long) statbuf.st_gid);
831                 tprintf("st_blksize=%lu, ",
832                         (unsigned long) statbuf.st_blksize);
833                 tprintf("st_blocks=%lu, ",
834                         (unsigned long) statbuf.st_blocks);
835         }
836         else
837                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
838         switch (statbuf.st_mode & S_IFMT) {
839         case S_IFCHR: case S_IFBLK:
840                 tprintf("st_rdev=makedev(%lu, %lu), ",
841                         (unsigned long) major(statbuf.st_rdev),
842                         (unsigned long) minor(statbuf.st_rdev));
843                 break;
844         default:
845                 tprintf("st_size=%lu, ", statbuf.st_size);
846                 break;
847         }
848         if (!abbrev(tcp)) {
849                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
850                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
851                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
852                 tprints("}");
853         }
854         else
855                 tprints("...}");
856 }
857 #endif /* SPARC64 */
858 #endif /* LINUXSPARC */
859
860 #if defined LINUX && defined POWERPC64
861 struct stat_powerpc32 {
862         unsigned int    st_dev;
863         unsigned int    st_ino;
864         unsigned int    st_mode;
865         unsigned short  st_nlink;
866         unsigned int    st_uid;
867         unsigned int    st_gid;
868         unsigned int    st_rdev;
869         unsigned int    st_size;
870         unsigned int    st_blksize;
871         unsigned int    st_blocks;
872         unsigned int    st_atime;
873         unsigned int    st_atime_nsec;
874         unsigned int    st_mtime;
875         unsigned int    st_mtime_nsec;
876         unsigned int    st_ctime;
877         unsigned int    st_ctime_nsec;
878         unsigned int    __unused4;
879         unsigned int    __unused5;
880 };
881
882 static void
883 printstat_powerpc32(struct tcb *tcp, long addr)
884 {
885         struct stat_powerpc32 statbuf;
886
887         if (umove(tcp, addr, &statbuf) < 0) {
888                 tprints("{...}");
889                 return;
890         }
891
892         if (!abbrev(tcp)) {
893                 tprintf("{st_dev=makedev(%u, %u), st_ino=%u, st_mode=%s, ",
894                         major(statbuf.st_dev), minor(statbuf.st_dev),
895                         statbuf.st_ino,
896                         sprintmode(statbuf.st_mode));
897                 tprintf("st_nlink=%u, st_uid=%u, st_gid=%u, ",
898                         statbuf.st_nlink, statbuf.st_uid, statbuf.st_gid);
899                 tprintf("st_blksize=%u, ", statbuf.st_blksize);
900                 tprintf("st_blocks=%u, ", statbuf.st_blocks);
901         }
902         else
903                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
904         switch (statbuf.st_mode & S_IFMT) {
905         case S_IFCHR: case S_IFBLK:
906                 tprintf("st_rdev=makedev(%lu, %lu), ",
907                         (unsigned long) major(statbuf.st_rdev),
908                         (unsigned long) minor(statbuf.st_rdev));
909                 break;
910         default:
911                 tprintf("st_size=%u, ", statbuf.st_size);
912                 break;
913         }
914         if (!abbrev(tcp)) {
915                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
916                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
917                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
918                 tprints("}");
919         }
920         else
921                 tprints("...}");
922 }
923 #endif /* LINUX && POWERPC64 */
924
925 static const struct xlat fileflags[] = {
926 #ifdef FREEBSD
927         { UF_NODUMP,    "UF_NODUMP"     },
928         { UF_IMMUTABLE, "UF_IMMUTABLE"  },
929         { UF_APPEND,    "UF_APPEND"     },
930         { UF_OPAQUE,    "UF_OPAQUE"     },
931         { UF_NOUNLINK,  "UF_NOUNLINK"   },
932         { SF_ARCHIVED,  "SF_ARCHIVED"   },
933         { SF_IMMUTABLE, "SF_IMMUTABLE"  },
934         { SF_APPEND,    "SF_APPEND"     },
935         { SF_NOUNLINK,  "SF_NOUNLINK"   },
936 #elif UNIXWARE >= 2
937 #ifdef _S_ISMLD
938         { _S_ISMLD,     "_S_ISMLD"      },
939 #endif
940 #ifdef _S_ISMOUNTED
941         { _S_ISMOUNTED, "_S_ISMOUNTED"  },
942 #endif
943 #endif
944         { 0,            NULL            },
945 };
946
947 #ifdef FREEBSD
948 int
949 sys_chflags(struct tcb *tcp)
950 {
951         if (entering(tcp)) {
952                 printpath(tcp, tcp->u_arg[0]);
953                 tprints(", ");
954                 printflags(fileflags, tcp->u_arg[1], "UF_???");
955         }
956         return 0;
957 }
958
959 int
960 sys_fchflags(struct tcb *tcp)
961 {
962         if (entering(tcp)) {
963                 tprintf("%ld, ", tcp->u_arg[0]);
964                 printflags(fileflags, tcp->u_arg[1], "UF_???");
965         }
966         return 0;
967 }
968 #endif
969
970 #ifndef HAVE_LONG_LONG_OFF_T
971 static void
972 realprintstat(struct tcb *tcp, struct stat *statbuf)
973 {
974         if (!abbrev(tcp)) {
975                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
976                         (unsigned long) major(statbuf->st_dev),
977                         (unsigned long) minor(statbuf->st_dev),
978                         (unsigned long) statbuf->st_ino,
979                         sprintmode(statbuf->st_mode));
980                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
981                         (unsigned long) statbuf->st_nlink,
982                         (unsigned long) statbuf->st_uid,
983                         (unsigned long) statbuf->st_gid);
984 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
985                 tprintf("st_blksize=%lu, ", (unsigned long) statbuf->st_blksize);
986 #endif
987 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
988                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf->st_blocks);
989 #endif
990         }
991         else
992                 tprintf("{st_mode=%s, ", sprintmode(statbuf->st_mode));
993         switch (statbuf->st_mode & S_IFMT) {
994         case S_IFCHR: case S_IFBLK:
995 #ifdef HAVE_STRUCT_STAT_ST_RDEV
996                 tprintf("st_rdev=makedev(%lu, %lu), ",
997                         (unsigned long) major(statbuf->st_rdev),
998                         (unsigned long) minor(statbuf->st_rdev));
999 #else /* !HAVE_STRUCT_STAT_ST_RDEV */
1000                 tprintf("st_size=makedev(%lu, %lu), ",
1001                         (unsigned long) major(statbuf->st_size),
1002                         (unsigned long) minor(statbuf->st_size));
1003 #endif /* !HAVE_STRUCT_STAT_ST_RDEV */
1004                 break;
1005         default:
1006                 tprintf("st_size=%lu, ", (unsigned long) statbuf->st_size);
1007                 break;
1008         }
1009         if (!abbrev(tcp)) {
1010                 tprintf("st_atime=%s, ", sprinttime(statbuf->st_atime));
1011                 tprintf("st_mtime=%s, ", sprinttime(statbuf->st_mtime));
1012                 tprintf("st_ctime=%s", sprinttime(statbuf->st_ctime));
1013 #if HAVE_STRUCT_STAT_ST_FLAGS
1014                 tprints(", st_flags=");
1015                 printflags(fileflags, statbuf->st_flags, "UF_???");
1016 #endif
1017 #if HAVE_STRUCT_STAT_ST_ACLCNT
1018                 tprintf(", st_aclcnt=%d", statbuf->st_aclcnt);
1019 #endif
1020 #if HAVE_STRUCT_STAT_ST_LEVEL
1021                 tprintf(", st_level=%ld", statbuf->st_level);
1022 #endif
1023 #if HAVE_STRUCT_STAT_ST_FSTYPE
1024                 tprintf(", st_fstype=%.*s",
1025                         (int) sizeof statbuf->st_fstype, statbuf->st_fstype);
1026 #endif
1027 #if HAVE_STRUCT_STAT_ST_GEN
1028                 tprintf(", st_gen=%u", statbuf->st_gen);
1029 #endif
1030                 tprints("}");
1031         }
1032         else
1033                 tprints("...}");
1034 }
1035
1036
1037 static void
1038 printstat(struct tcb *tcp, long addr)
1039 {
1040         struct stat statbuf;
1041
1042         if (!addr) {
1043                 tprints("NULL");
1044                 return;
1045         }
1046         if (syserror(tcp) || !verbose(tcp)) {
1047                 tprintf("%#lx", addr);
1048                 return;
1049         }
1050
1051 #ifdef LINUXSPARC
1052         if (current_personality == 1) {
1053                 printstatsol(tcp, addr);
1054                 return;
1055         }
1056 #ifdef SPARC64
1057         else if (current_personality == 2) {
1058                 printstat_sparc64(tcp, addr);
1059                 return;
1060         }
1061 #endif
1062 #endif /* LINUXSPARC */
1063
1064 #if defined LINUX && defined POWERPC64
1065         if (current_personality == 1) {
1066                 printstat_powerpc32(tcp, addr);
1067                 return;
1068         }
1069 #endif
1070
1071         if (umove(tcp, addr, &statbuf) < 0) {
1072                 tprints("{...}");
1073                 return;
1074         }
1075
1076         realprintstat(tcp, &statbuf);
1077 }
1078 #endif  /* !HAVE_LONG_LONG_OFF_T */
1079
1080 #if !defined HAVE_STAT64 && defined LINUX && defined X86_64
1081 /*
1082  * Linux x86_64 has unified `struct stat' but its i386 biarch needs
1083  * `struct stat64'.  Its <asm-i386/stat.h> definition expects 32-bit `long'.
1084  * <linux/include/asm-x86_64/ia32.h> is not in the public includes set.
1085  * __GNUC__ is needed for the required __attribute__ below.
1086  */
1087 struct stat64 {
1088         unsigned long long      st_dev;
1089         unsigned char   __pad0[4];
1090         unsigned int    __st_ino;
1091         unsigned int    st_mode;
1092         unsigned int    st_nlink;
1093         unsigned int    st_uid;
1094         unsigned int    st_gid;
1095         unsigned long long      st_rdev;
1096         unsigned char   __pad3[4];
1097         long long       st_size;
1098         unsigned int    st_blksize;
1099         unsigned long long      st_blocks;
1100         unsigned int    st_atime;
1101         unsigned int    st_atime_nsec;
1102         unsigned int    st_mtime;
1103         unsigned int    st_mtime_nsec;
1104         unsigned int    st_ctime;
1105         unsigned int    st_ctime_nsec;
1106         unsigned long long      st_ino;
1107 } __attribute__((packed));
1108 # define HAVE_STAT64    1
1109 # define STAT64_SIZE    96
1110 #endif
1111
1112 #ifdef HAVE_STAT64
1113 static void
1114 printstat64(struct tcb *tcp, long addr)
1115 {
1116         struct stat64 statbuf;
1117
1118 #ifdef STAT64_SIZE
1119         (void) sizeof(char[sizeof statbuf == STAT64_SIZE ? 1 : -1]);
1120 #endif
1121
1122         if (!addr) {
1123                 tprints("NULL");
1124                 return;
1125         }
1126         if (syserror(tcp) || !verbose(tcp)) {
1127                 tprintf("%#lx", addr);
1128                 return;
1129         }
1130
1131 #ifdef LINUXSPARC
1132         if (current_personality == 1) {
1133                 printstatsol(tcp, addr);
1134                 return;
1135         }
1136 # ifdef SPARC64
1137         else if (current_personality == 2) {
1138                 printstat_sparc64(tcp, addr);
1139                 return;
1140         }
1141 # endif
1142 #endif /* LINUXSPARC */
1143
1144 #if defined LINUX && defined X86_64
1145         if (current_personality == 0) {
1146                 printstat(tcp, addr);
1147                 return;
1148         }
1149 #endif
1150
1151         if (umove(tcp, addr, &statbuf) < 0) {
1152                 tprints("{...}");
1153                 return;
1154         }
1155
1156         if (!abbrev(tcp)) {
1157 #ifdef HAVE_LONG_LONG
1158                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%llu, st_mode=%s, ",
1159 #else
1160                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
1161 #endif
1162                         (unsigned long) major(statbuf.st_dev),
1163                         (unsigned long) minor(statbuf.st_dev),
1164 #ifdef HAVE_LONG_LONG
1165                         (unsigned long long) statbuf.st_ino,
1166 #else
1167                         (unsigned long) statbuf.st_ino,
1168 #endif
1169                         sprintmode(statbuf.st_mode));
1170                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
1171                         (unsigned long) statbuf.st_nlink,
1172                         (unsigned long) statbuf.st_uid,
1173                         (unsigned long) statbuf.st_gid);
1174 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1175                 tprintf("st_blksize=%lu, ",
1176                         (unsigned long) statbuf.st_blksize);
1177 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1178 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1179                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
1180 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
1181         }
1182         else
1183                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
1184         switch (statbuf.st_mode & S_IFMT) {
1185         case S_IFCHR: case S_IFBLK:
1186 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1187                 tprintf("st_rdev=makedev(%lu, %lu), ",
1188                         (unsigned long) major(statbuf.st_rdev),
1189                         (unsigned long) minor(statbuf.st_rdev));
1190 #else /* !HAVE_STRUCT_STAT_ST_RDEV */
1191                 tprintf("st_size=makedev(%lu, %lu), ",
1192                         (unsigned long) major(statbuf.st_size),
1193                         (unsigned long) minor(statbuf.st_size));
1194 #endif /* !HAVE_STRUCT_STAT_ST_RDEV */
1195                 break;
1196         default:
1197 #ifdef HAVE_LONG_LONG
1198                 tprintf("st_size=%llu, ", (unsigned long long) statbuf.st_size);
1199 #else
1200                 tprintf("st_size=%lu, ", (unsigned long) statbuf.st_size);
1201 #endif
1202                 break;
1203         }
1204         if (!abbrev(tcp)) {
1205                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
1206                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
1207                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
1208 #if HAVE_STRUCT_STAT_ST_FLAGS
1209                 tprints(", st_flags=");
1210                 printflags(fileflags, statbuf.st_flags, "UF_???");
1211 #endif
1212 #if HAVE_STRUCT_STAT_ST_ACLCNT
1213                 tprintf(", st_aclcnt=%d", statbuf.st_aclcnt);
1214 #endif
1215 #if HAVE_STRUCT_STAT_ST_LEVEL
1216                 tprintf(", st_level=%ld", statbuf.st_level);
1217 #endif
1218 #if HAVE_STRUCT_STAT_ST_FSTYPE
1219                 tprintf(", st_fstype=%.*s",
1220                         (int) sizeof statbuf.st_fstype, statbuf.st_fstype);
1221 #endif
1222 #if HAVE_STRUCT_STAT_ST_GEN
1223                 tprintf(", st_gen=%u", statbuf.st_gen);
1224 #endif
1225                 tprints("}");
1226         }
1227         else
1228                 tprints("...}");
1229 }
1230 #endif /* HAVE_STAT64 */
1231
1232 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT) \
1233     && !defined(HAVE_LONG_LONG_OFF_T)
1234 static void
1235 convertoldstat(const struct __old_kernel_stat *oldbuf, struct stat *newbuf)
1236 {
1237         newbuf->st_dev = oldbuf->st_dev;
1238         newbuf->st_ino = oldbuf->st_ino;
1239         newbuf->st_mode = oldbuf->st_mode;
1240         newbuf->st_nlink = oldbuf->st_nlink;
1241         newbuf->st_uid = oldbuf->st_uid;
1242         newbuf->st_gid = oldbuf->st_gid;
1243         newbuf->st_rdev = oldbuf->st_rdev;
1244         newbuf->st_size = oldbuf->st_size;
1245         newbuf->st_atime = oldbuf->st_atime;
1246         newbuf->st_mtime = oldbuf->st_mtime;
1247         newbuf->st_ctime = oldbuf->st_ctime;
1248         newbuf->st_blksize = 0; /* not supported in old_stat */
1249         newbuf->st_blocks = 0; /* not supported in old_stat */
1250 }
1251
1252
1253 static void
1254 printoldstat(struct tcb *tcp, long addr)
1255 {
1256         struct __old_kernel_stat statbuf;
1257         struct stat newstatbuf;
1258
1259         if (!addr) {
1260                 tprints("NULL");
1261                 return;
1262         }
1263         if (syserror(tcp) || !verbose(tcp)) {
1264                 tprintf("%#lx", addr);
1265                 return;
1266         }
1267
1268 #ifdef LINUXSPARC
1269         if (current_personality == 1) {
1270                 printstatsol(tcp, addr);
1271                 return;
1272         }
1273 #endif /* LINUXSPARC */
1274
1275         if (umove(tcp, addr, &statbuf) < 0) {
1276                 tprints("{...}");
1277                 return;
1278         }
1279
1280         convertoldstat(&statbuf, &newstatbuf);
1281         realprintstat(tcp, &newstatbuf);
1282 }
1283 #endif /* LINUX && !IA64 && !HPPA && !X86_64 && !S390 && !S390X */
1284
1285 #ifndef HAVE_LONG_LONG_OFF_T
1286 int
1287 sys_stat(struct tcb *tcp)
1288 {
1289         if (entering(tcp)) {
1290                 printpath(tcp, tcp->u_arg[0]);
1291                 tprints(", ");
1292         } else {
1293                 printstat(tcp, tcp->u_arg[1]);
1294         }
1295         return 0;
1296 }
1297 #endif
1298
1299 int
1300 sys_stat64(struct tcb *tcp)
1301 {
1302 #ifdef HAVE_STAT64
1303         if (entering(tcp)) {
1304                 printpath(tcp, tcp->u_arg[0]);
1305                 tprints(", ");
1306         } else {
1307                 printstat64(tcp, tcp->u_arg[1]);
1308         }
1309         return 0;
1310 #else
1311         return printargs(tcp);
1312 #endif
1313 }
1314
1315 #ifdef LINUX
1316 static const struct xlat fstatatflags[] = {
1317 #ifndef AT_SYMLINK_NOFOLLOW
1318 # define AT_SYMLINK_NOFOLLOW     0x100
1319 #endif
1320         { AT_SYMLINK_NOFOLLOW,  "AT_SYMLINK_NOFOLLOW"   },
1321         { 0,                    NULL                    },
1322 };
1323 #define utimensatflags fstatatflags
1324
1325 int
1326 sys_newfstatat(struct tcb *tcp)
1327 {
1328         if (entering(tcp)) {
1329                 print_dirfd(tcp, tcp->u_arg[0]);
1330                 printpath(tcp, tcp->u_arg[1]);
1331                 tprints(", ");
1332         } else {
1333 #ifdef POWERPC64
1334                 if (current_personality == 0)
1335                         printstat(tcp, tcp->u_arg[2]);
1336                 else
1337                         printstat64(tcp, tcp->u_arg[2]);
1338 #elif defined HAVE_STAT64
1339                 printstat64(tcp, tcp->u_arg[2]);
1340 #else
1341                 printstat(tcp, tcp->u_arg[2]);
1342 #endif
1343                 tprints(", ");
1344                 printflags(fstatatflags, tcp->u_arg[3], "AT_???");
1345         }
1346         return 0;
1347 }
1348 #endif
1349
1350 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT) \
1351     && !defined(HAVE_LONG_LONG_OFF_T)
1352 int
1353 sys_oldstat(struct tcb *tcp)
1354 {
1355         if (entering(tcp)) {
1356                 printpath(tcp, tcp->u_arg[0]);
1357                 tprints(", ");
1358         } else {
1359                 printoldstat(tcp, tcp->u_arg[1]);
1360         }
1361         return 0;
1362 }
1363 #endif /* LINUX && HAVE_STRUCT___OLD_KERNEL_STAT */
1364
1365 #ifndef HAVE_LONG_LONG_OFF_T
1366 int
1367 sys_fstat(struct tcb *tcp)
1368 {
1369         if (entering(tcp)) {
1370                 printfd(tcp, tcp->u_arg[0]);
1371                 tprints(", ");
1372         } else {
1373                 printstat(tcp, tcp->u_arg[1]);
1374         }
1375         return 0;
1376 }
1377 #endif
1378
1379 int
1380 sys_fstat64(struct tcb *tcp)
1381 {
1382 #ifdef HAVE_STAT64
1383         if (entering(tcp)) {
1384                 printfd(tcp, tcp->u_arg[0]);
1385                 tprints(", ");
1386         } else {
1387                 printstat64(tcp, tcp->u_arg[1]);
1388         }
1389         return 0;
1390 #else
1391         return printargs(tcp);
1392 #endif
1393 }
1394
1395 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT) \
1396     && !defined(HAVE_LONG_LONG_OFF_T)
1397 int
1398 sys_oldfstat(struct tcb *tcp)
1399 {
1400         if (entering(tcp)) {
1401                 printfd(tcp, tcp->u_arg[0]);
1402                 tprints(", ");
1403         } else {
1404                 printoldstat(tcp, tcp->u_arg[1]);
1405         }
1406         return 0;
1407 }
1408 #endif /* LINUX && HAVE_STRUCT___OLD_KERNEL_STAT */
1409
1410 #ifndef HAVE_LONG_LONG_OFF_T
1411 int
1412 sys_lstat(struct tcb *tcp)
1413 {
1414         if (entering(tcp)) {
1415                 printpath(tcp, tcp->u_arg[0]);
1416                 tprints(", ");
1417         } else {
1418                 printstat(tcp, tcp->u_arg[1]);
1419         }
1420         return 0;
1421 }
1422 #endif
1423
1424 int
1425 sys_lstat64(struct tcb *tcp)
1426 {
1427 #ifdef HAVE_STAT64
1428         if (entering(tcp)) {
1429                 printpath(tcp, tcp->u_arg[0]);
1430                 tprints(", ");
1431         } else {
1432                 printstat64(tcp, tcp->u_arg[1]);
1433         }
1434         return 0;
1435 #else
1436         return printargs(tcp);
1437 #endif
1438 }
1439
1440 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT) \
1441     && !defined(HAVE_LONG_LONG_OFF_T)
1442 int
1443 sys_oldlstat(struct tcb *tcp)
1444 {
1445         if (entering(tcp)) {
1446                 printpath(tcp, tcp->u_arg[0]);
1447                 tprints(", ");
1448         } else {
1449                 printoldstat(tcp, tcp->u_arg[1]);
1450         }
1451         return 0;
1452 }
1453 #endif /* LINUX && HAVE_STRUCT___OLD_KERNEL_STAT */
1454
1455
1456 #if defined(SVR4) || defined(LINUXSPARC)
1457
1458 int
1459 sys_xstat(struct tcb *tcp)
1460 {
1461         if (entering(tcp)) {
1462                 tprintf("%ld, ", tcp->u_arg[0]);
1463                 printpath(tcp, tcp->u_arg[1]);
1464                 tprints(", ");
1465         } else {
1466 #ifdef _STAT64_VER
1467                 if (tcp->u_arg[0] == _STAT64_VER)
1468                         printstat64 (tcp, tcp->u_arg[2]);
1469                 else
1470 #endif
1471                 printstat(tcp, tcp->u_arg[2]);
1472         }
1473         return 0;
1474 }
1475
1476 int
1477 sys_fxstat(struct tcb *tcp)
1478 {
1479         if (entering(tcp))
1480                 tprintf("%ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1]);
1481         else {
1482 #ifdef _STAT64_VER
1483                 if (tcp->u_arg[0] == _STAT64_VER)
1484                         printstat64 (tcp, tcp->u_arg[2]);
1485                 else
1486 #endif
1487                 printstat(tcp, tcp->u_arg[2]);
1488         }
1489         return 0;
1490 }
1491
1492 int
1493 sys_lxstat(struct tcb *tcp)
1494 {
1495         if (entering(tcp)) {
1496                 tprintf("%ld, ", tcp->u_arg[0]);
1497                 printpath(tcp, tcp->u_arg[1]);
1498                 tprints(", ");
1499         } else {
1500 #ifdef _STAT64_VER
1501                 if (tcp->u_arg[0] == _STAT64_VER)
1502                         printstat64 (tcp, tcp->u_arg[2]);
1503                 else
1504 #endif
1505                 printstat(tcp, tcp->u_arg[2]);
1506         }
1507         return 0;
1508 }
1509
1510 int
1511 sys_xmknod(struct tcb *tcp)
1512 {
1513         int mode = tcp->u_arg[2];
1514
1515         if (entering(tcp)) {
1516                 tprintf("%ld, ", tcp->u_arg[0]);
1517                 printpath(tcp, tcp->u_arg[1]);
1518                 tprintf(", %s", sprintmode(mode));
1519                 switch (mode & S_IFMT) {
1520                 case S_IFCHR: case S_IFBLK:
1521 #ifdef LINUXSPARC
1522                         tprintf(", makedev(%lu, %lu)",
1523                                 (unsigned long) ((tcp->u_arg[3] >> 18) & 0x3fff),
1524                                 (unsigned long) (tcp->u_arg[3] & 0x3ffff));
1525 #else
1526                         tprintf(", makedev(%lu, %lu)",
1527                                 (unsigned long) major(tcp->u_arg[3]),
1528                                 (unsigned long) minor(tcp->u_arg[3]));
1529 #endif
1530                         break;
1531                 default:
1532                         break;
1533                 }
1534         }
1535         return 0;
1536 }
1537
1538 #ifdef HAVE_SYS_ACL_H
1539
1540 #include <sys/acl.h>
1541
1542 static const struct xlat aclcmds[] = {
1543 #ifdef SETACL
1544         { SETACL,       "SETACL"        },
1545 #endif
1546 #ifdef GETACL
1547         { GETACL,       "GETACL"        },
1548 #endif
1549 #ifdef GETACLCNT
1550         { GETACLCNT,    "GETACLCNT"     },
1551 #endif
1552 #ifdef ACL_GET
1553         { ACL_GET,      "ACL_GET"       },
1554 #endif
1555 #ifdef ACL_SET
1556         { ACL_SET,      "ACL_SET"       },
1557 #endif
1558 #ifdef ACL_CNT
1559         { ACL_CNT,      "ACL_CNT"       },
1560 #endif
1561         { 0,            NULL            },
1562 };
1563
1564 int
1565 sys_acl(struct tcb *tcp)
1566 {
1567         if (entering(tcp)) {
1568                 printpath(tcp, tcp->u_arg[0]);
1569                 tprints(", ");
1570                 printxval(aclcmds, tcp->u_arg[1], "???ACL???");
1571                 tprintf(", %ld", tcp->u_arg[2]);
1572                 /*
1573                  * FIXME - dump out the list of aclent_t's pointed to
1574                  * by "tcp->u_arg[3]" if it's not NULL.
1575                  */
1576                 if (tcp->u_arg[3])
1577                         tprintf(", %#lx", tcp->u_arg[3]);
1578                 else
1579                         tprints(", NULL");
1580         }
1581         return 0;
1582 }
1583
1584
1585 int
1586 sys_facl(struct tcb *tcp)
1587 {
1588         if (entering(tcp)) {
1589                 tprintf("%ld, ", tcp->u_arg[0]);
1590                 printxval(aclcmds, tcp->u_arg[1], "???ACL???");
1591                 tprintf(", %ld", tcp->u_arg[2]);
1592                 /*
1593                  * FIXME - dump out the list of aclent_t's pointed to
1594                  * by "tcp->u_arg[3]" if it's not NULL.
1595                  */
1596                 if (tcp->u_arg[3])
1597                         tprintf(", %#lx", tcp->u_arg[3]);
1598                 else
1599                         tprints(", NULL");
1600         }
1601         return 0;
1602 }
1603
1604
1605 static const struct xlat aclipc[] = {
1606 #ifdef IPC_SHM
1607         { IPC_SHM,      "IPC_SHM"       },
1608 #endif
1609 #ifdef IPC_SEM
1610         { IPC_SEM,      "IPC_SEM"       },
1611 #endif
1612 #ifdef IPC_MSG
1613         { IPC_MSG,      "IPC_MSG"       },
1614 #endif
1615         { 0,            NULL            },
1616 };
1617
1618
1619 int
1620 sys_aclipc(struct tcb *tcp)
1621 {
1622         if (entering(tcp)) {
1623                 printxval(aclipc, tcp->u_arg[0], "???IPC???");
1624                 tprintf(", %#lx, ", tcp->u_arg[1]);
1625                 printxval(aclcmds, tcp->u_arg[2], "???ACL???");
1626                 tprintf(", %ld", tcp->u_arg[3]);
1627                 /*
1628                  * FIXME - dump out the list of aclent_t's pointed to
1629                  * by "tcp->u_arg[4]" if it's not NULL.
1630                  */
1631                 if (tcp->u_arg[4])
1632                         tprintf(", %#lx", tcp->u_arg[4]);
1633                 else
1634                         tprints(", NULL");
1635         }
1636         return 0;
1637 }
1638
1639 #endif /* HAVE_SYS_ACL_H */
1640
1641 #endif /* SVR4 || LINUXSPARC */
1642
1643 #ifdef LINUX
1644
1645 static const struct xlat fsmagic[] = {
1646         { 0x73757245,   "CODA_SUPER_MAGIC"      },
1647         { 0x012ff7b7,   "COH_SUPER_MAGIC"       },
1648         { 0x1373,       "DEVFS_SUPER_MAGIC"     },
1649         { 0x1cd1,       "DEVPTS_SUPER_MAGIC"    },
1650         { 0x414A53,     "EFS_SUPER_MAGIC"       },
1651         { 0xef51,       "EXT2_OLD_SUPER_MAGIC"  },
1652         { 0xef53,       "EXT2_SUPER_MAGIC"      },
1653         { 0x137d,       "EXT_SUPER_MAGIC"       },
1654         { 0xf995e849,   "HPFS_SUPER_MAGIC"      },
1655         { 0x9660,       "ISOFS_SUPER_MAGIC"     },
1656         { 0x137f,       "MINIX_SUPER_MAGIC"     },
1657         { 0x138f,       "MINIX_SUPER_MAGIC2"    },
1658         { 0x2468,       "MINIX2_SUPER_MAGIC"    },
1659         { 0x2478,       "MINIX2_SUPER_MAGIC2"   },
1660         { 0x4d44,       "MSDOS_SUPER_MAGIC"     },
1661         { 0x564c,       "NCP_SUPER_MAGIC"       },
1662         { 0x6969,       "NFS_SUPER_MAGIC"       },
1663         { 0x9fa0,       "PROC_SUPER_MAGIC"      },
1664         { 0x002f,       "QNX4_SUPER_MAGIC"      },
1665         { 0x52654973,   "REISERFS_SUPER_MAGIC"  },
1666         { 0x02011994,   "SHMFS_SUPER_MAGIC"     },
1667         { 0x517b,       "SMB_SUPER_MAGIC"       },
1668         { 0x012ff7b6,   "SYSV2_SUPER_MAGIC"     },
1669         { 0x012ff7b5,   "SYSV4_SUPER_MAGIC"     },
1670         { 0x00011954,   "UFS_MAGIC"             },
1671         { 0x54190100,   "UFS_CIGAM"             },
1672         { 0x012ff7b4,   "XENIX_SUPER_MAGIC"     },
1673         { 0x012fd16d,   "XIAFS_SUPER_MAGIC"     },
1674         { 0x62656572,   "SYSFS_MAGIC"           },
1675         { 0,            NULL                    },
1676 };
1677
1678 #endif /* LINUX */
1679
1680 #ifndef SVR4
1681
1682 static const char *
1683 sprintfstype(int magic)
1684 {
1685         static char buf[32];
1686 #ifdef LINUX
1687         const char *s;
1688
1689         s = xlookup(fsmagic, magic);
1690         if (s) {
1691                 sprintf(buf, "\"%s\"", s);
1692                 return buf;
1693         }
1694 #endif /* LINUX */
1695         sprintf(buf, "%#x", magic);
1696         return buf;
1697 }
1698
1699 static void
1700 printstatfs(struct tcb *tcp, long addr)
1701 {
1702         struct statfs statbuf;
1703
1704         if (syserror(tcp) || !verbose(tcp)) {
1705                 tprintf("%#lx", addr);
1706                 return;
1707         }
1708         if (umove(tcp, addr, &statbuf) < 0) {
1709                 tprints("{...}");
1710                 return;
1711         }
1712 #ifdef ALPHA
1713
1714         tprintf("{f_type=%s, f_fbsize=%u, f_blocks=%u, f_bfree=%u, ",
1715                 sprintfstype(statbuf.f_type),
1716                 statbuf.f_bsize, statbuf.f_blocks, statbuf.f_bfree);
1717         tprintf("f_bavail=%u, f_files=%u, f_ffree=%u, f_fsid={%d, %d}, f_namelen=%u",
1718                 statbuf.f_bavail, statbuf.f_files, statbuf.f_ffree,
1719                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1],
1720                 statbuf.f_namelen);
1721 #else /* !ALPHA */
1722         tprintf("{f_type=%s, f_bsize=%lu, f_blocks=%lu, f_bfree=%lu, ",
1723                 sprintfstype(statbuf.f_type),
1724                 (unsigned long)statbuf.f_bsize,
1725                 (unsigned long)statbuf.f_blocks,
1726                 (unsigned long)statbuf.f_bfree);
1727         tprintf("f_bavail=%lu, f_files=%lu, f_ffree=%lu, f_fsid={%d, %d}",
1728                 (unsigned long)statbuf.f_bavail,
1729                 (unsigned long)statbuf.f_files,
1730                 (unsigned long)statbuf.f_ffree,
1731                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
1732 #ifdef LINUX
1733         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
1734 #endif /* LINUX */
1735 #endif /* !ALPHA */
1736 #ifdef _STATFS_F_FRSIZE
1737         tprintf(", f_frsize=%lu", (unsigned long)statbuf.f_frsize);
1738 #endif
1739         tprints("}");
1740 }
1741
1742 int
1743 sys_statfs(struct tcb *tcp)
1744 {
1745         if (entering(tcp)) {
1746                 printpath(tcp, tcp->u_arg[0]);
1747                 tprints(", ");
1748         } else {
1749                 printstatfs(tcp, tcp->u_arg[1]);
1750         }
1751         return 0;
1752 }
1753
1754 int
1755 sys_fstatfs(struct tcb *tcp)
1756 {
1757         if (entering(tcp)) {
1758                 printfd(tcp, tcp->u_arg[0]);
1759                 tprints(", ");
1760         } else {
1761                 printstatfs(tcp, tcp->u_arg[1]);
1762         }
1763         return 0;
1764 }
1765
1766 #if defined LINUX && defined HAVE_STATFS64
1767 static void
1768 printstatfs64(struct tcb *tcp, long addr)
1769 {
1770         struct statfs64 statbuf;
1771
1772         if (syserror(tcp) || !verbose(tcp)) {
1773                 tprintf("%#lx", addr);
1774                 return;
1775         }
1776         if (umove(tcp, addr, &statbuf) < 0) {
1777                 tprints("{...}");
1778                 return;
1779         }
1780         tprintf("{f_type=%s, f_bsize=%llu, f_blocks=%llu, f_bfree=%llu, ",
1781                 sprintfstype(statbuf.f_type),
1782                 (unsigned long long)statbuf.f_bsize,
1783                 (unsigned long long)statbuf.f_blocks,
1784                 (unsigned long long)statbuf.f_bfree);
1785         tprintf("f_bavail=%llu, f_files=%llu, f_ffree=%llu, f_fsid={%d, %d}",
1786                 (unsigned long long)statbuf.f_bavail,
1787                 (unsigned long long)statbuf.f_files,
1788                 (unsigned long long)statbuf.f_ffree,
1789                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
1790         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
1791 #ifdef _STATFS_F_FRSIZE
1792         tprintf(", f_frsize=%llu", (unsigned long long)statbuf.f_frsize);
1793 #endif
1794 #ifdef _STATFS_F_FLAGS
1795         tprintf(", f_flags=%llu", (unsigned long long)statbuf.f_flags);
1796 #endif
1797         tprints("}");
1798 }
1799
1800 struct compat_statfs64 {
1801         uint32_t f_type;
1802         uint32_t f_bsize;
1803         uint64_t f_blocks;
1804         uint64_t f_bfree;
1805         uint64_t f_bavail;
1806         uint64_t f_files;
1807         uint64_t f_ffree;
1808         fsid_t f_fsid;
1809         uint32_t f_namelen;
1810         uint32_t f_frsize;
1811         uint32_t f_flags;
1812         uint32_t f_spare[4];
1813 }
1814 #if defined(X86_64) || defined(IA64)
1815   __attribute__ ((packed, aligned(4)))
1816 #endif
1817 ;
1818
1819 static void
1820 printcompat_statfs64(struct tcb *tcp, long addr)
1821 {
1822         struct compat_statfs64 statbuf;
1823
1824         if (syserror(tcp) || !verbose(tcp)) {
1825                 tprintf("%#lx", addr);
1826                 return;
1827         }
1828         if (umove(tcp, addr, &statbuf) < 0) {
1829                 tprints("{...}");
1830                 return;
1831         }
1832         tprintf("{f_type=%s, f_bsize=%lu, f_blocks=%llu, f_bfree=%llu, ",
1833                 sprintfstype(statbuf.f_type),
1834                 (unsigned long)statbuf.f_bsize,
1835                 (unsigned long long)statbuf.f_blocks,
1836                 (unsigned long long)statbuf.f_bfree);
1837         tprintf("f_bavail=%llu, f_files=%llu, f_ffree=%llu, f_fsid={%d, %d}",
1838                 (unsigned long long)statbuf.f_bavail,
1839                 (unsigned long long)statbuf.f_files,
1840                 (unsigned long long)statbuf.f_ffree,
1841                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
1842         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
1843         tprintf(", f_frsize=%lu", (unsigned long)statbuf.f_frsize);
1844         tprintf(", f_flags=%lu", (unsigned long)statbuf.f_frsize);
1845         tprints("}");
1846 }
1847
1848 int
1849 sys_statfs64(struct tcb *tcp)
1850 {
1851         if (entering(tcp)) {
1852                 printpath(tcp, tcp->u_arg[0]);
1853                 tprintf(", %lu, ", tcp->u_arg[1]);
1854         } else {
1855                 if (tcp->u_arg[1] == sizeof(struct statfs64))
1856                         printstatfs64(tcp, tcp->u_arg[2]);
1857                 else if (tcp->u_arg[1] == sizeof(struct compat_statfs64))
1858                         printcompat_statfs64(tcp, tcp->u_arg[2]);
1859                 else
1860                         tprints("{???}");
1861         }
1862         return 0;
1863 }
1864
1865 int
1866 sys_fstatfs64(struct tcb *tcp)
1867 {
1868         if (entering(tcp)) {
1869                 printfd(tcp, tcp->u_arg[0]);
1870                 tprintf(", %lu, ", tcp->u_arg[1]);
1871         } else {
1872                 if (tcp->u_arg[1] == sizeof(struct statfs64))
1873                         printstatfs64(tcp, tcp->u_arg[2]);
1874                 else if (tcp->u_arg[1] == sizeof(struct compat_statfs64))
1875                         printcompat_statfs64(tcp, tcp->u_arg[2]);
1876                 else
1877                         tprints("{???}");
1878         }
1879         return 0;
1880 }
1881 #endif
1882
1883 #if defined(LINUX) && defined(__alpha)
1884
1885 int
1886 osf_statfs(struct tcb *tcp)
1887 {
1888         if (entering(tcp)) {
1889                 printpath(tcp, tcp->u_arg[0]);
1890                 tprints(", ");
1891         } else {
1892                 printstatfs(tcp, tcp->u_arg[1]);
1893                 tprintf(", %lu", tcp->u_arg[2]);
1894         }
1895         return 0;
1896 }
1897
1898 int
1899 osf_fstatfs(struct tcb *tcp)
1900 {
1901         if (entering(tcp)) {
1902                 tprintf("%lu, ", tcp->u_arg[0]);
1903         } else {
1904                 printstatfs(tcp, tcp->u_arg[1]);
1905                 tprintf(", %lu", tcp->u_arg[2]);
1906         }
1907         return 0;
1908 }
1909 #endif /* LINUX && __alpha */
1910
1911 #endif /* !SVR4 */
1912
1913 #ifdef SUNOS4
1914 int
1915 sys_ustat(struct tcb *tcp)
1916 {
1917         struct ustat statbuf;
1918
1919         if (entering(tcp)) {
1920                 tprintf("makedev(%lu, %lu), ",
1921                                 (long) major(tcp->u_arg[0]),
1922                                 (long) minor(tcp->u_arg[0]));
1923         }
1924         else {
1925                 if (syserror(tcp) || !verbose(tcp))
1926                         tprintf("%#lx", tcp->u_arg[1]);
1927                 else if (umove(tcp, tcp->u_arg[1], &statbuf) < 0)
1928                         tprints("{...}");
1929                 else {
1930                         tprintf("{f_tfree=%lu, f_tinode=%lu, ",
1931                                 statbuf.f_tfree, statbuf.f_tinode);
1932                         tprintf("f_fname=\"%.*s\", ",
1933                                 (int) sizeof(statbuf.f_fname),
1934                                 statbuf.f_fname);
1935                         tprintf("f_fpack=\"%.*s\"}",
1936                                 (int) sizeof(statbuf.f_fpack),
1937                                 statbuf.f_fpack);
1938                 }
1939         }
1940         return 0;
1941 }
1942 #endif /* SUNOS4 */
1943
1944 /* directory */
1945 int
1946 sys_chdir(struct tcb *tcp)
1947 {
1948         if (entering(tcp)) {
1949                 printpath(tcp, tcp->u_arg[0]);
1950         }
1951         return 0;
1952 }
1953
1954 static int
1955 decode_mkdir(struct tcb *tcp, int offset)
1956 {
1957         if (entering(tcp)) {
1958                 printpath(tcp, tcp->u_arg[offset]);
1959                 tprintf(", %#lo", tcp->u_arg[offset + 1]);
1960         }
1961         return 0;
1962 }
1963
1964 int
1965 sys_mkdir(struct tcb *tcp)
1966 {
1967         return decode_mkdir(tcp, 0);
1968 }
1969
1970 #ifdef LINUX
1971 int
1972 sys_mkdirat(struct tcb *tcp)
1973 {
1974         if (entering(tcp))
1975                 print_dirfd(tcp, tcp->u_arg[0]);
1976         return decode_mkdir(tcp, 1);
1977 }
1978 #endif
1979
1980 int
1981 sys_link(struct tcb *tcp)
1982 {
1983         if (entering(tcp)) {
1984                 printpath(tcp, tcp->u_arg[0]);
1985                 tprints(", ");
1986                 printpath(tcp, tcp->u_arg[1]);
1987         }
1988         return 0;
1989 }
1990
1991 #ifdef LINUX
1992 int
1993 sys_linkat(struct tcb *tcp)
1994 {
1995         if (entering(tcp)) {
1996                 print_dirfd(tcp, tcp->u_arg[0]);
1997                 printpath(tcp, tcp->u_arg[1]);
1998                 tprints(", ");
1999                 print_dirfd(tcp, tcp->u_arg[2]);
2000                 printpath(tcp, tcp->u_arg[3]);
2001                 tprints(", ");
2002                 printfd(tcp, tcp->u_arg[4]);
2003         }
2004         return 0;
2005 }
2006 #endif
2007
2008 #ifdef LINUX
2009 static const struct xlat unlinkatflags[] = {
2010 #ifndef AT_REMOVEDIR
2011 # define AT_REMOVEDIR            0x200
2012 #endif
2013         { AT_REMOVEDIR, "AT_REMOVEDIR"  },
2014         { 0,            NULL            },
2015 };
2016
2017 int
2018 sys_unlinkat(struct tcb *tcp)
2019 {
2020         if (entering(tcp)) {
2021                 print_dirfd(tcp, tcp->u_arg[0]);
2022                 printpath(tcp, tcp->u_arg[1]);
2023                 tprints(", ");
2024                 printflags(unlinkatflags, tcp->u_arg[2], "AT_???");
2025         }
2026         return 0;
2027 }
2028 #endif
2029
2030 #ifdef LINUX
2031 int
2032 sys_symlinkat(struct tcb *tcp)
2033 {
2034         if (entering(tcp)) {
2035                 printpath(tcp, tcp->u_arg[0]);
2036                 tprints(", ");
2037                 print_dirfd(tcp, tcp->u_arg[1]);
2038                 printpath(tcp, tcp->u_arg[2]);
2039         }
2040         return 0;
2041 }
2042 #endif
2043
2044 static int
2045 decode_readlink(struct tcb *tcp, int offset)
2046 {
2047         if (entering(tcp)) {
2048                 printpath(tcp, tcp->u_arg[offset]);
2049                 tprints(", ");
2050         } else {
2051                 if (syserror(tcp))
2052                         tprintf("%#lx", tcp->u_arg[offset + 1]);
2053                 else
2054                         /* Used to use printpathn(), but readlink
2055                          * neither includes NUL in the returned count,
2056                          * nor actually writes it into memory.
2057                          * printpathn() would decide on printing
2058                          * "..." continuation based on garbage
2059                          * past return buffer's end.
2060                          */
2061                         printstr(tcp, tcp->u_arg[offset + 1], tcp->u_rval);
2062                 tprintf(", %lu", tcp->u_arg[offset + 2]);
2063         }
2064         return 0;
2065 }
2066
2067 int
2068 sys_readlink(struct tcb *tcp)
2069 {
2070         return decode_readlink(tcp, 0);
2071 }
2072
2073 #ifdef LINUX
2074 int
2075 sys_readlinkat(struct tcb *tcp)
2076 {
2077         if (entering(tcp))
2078                 print_dirfd(tcp, tcp->u_arg[0]);
2079         return decode_readlink(tcp, 1);
2080 }
2081 #endif
2082
2083 #ifdef LINUX
2084 int
2085 sys_renameat(struct tcb *tcp)
2086 {
2087         if (entering(tcp)) {
2088                 print_dirfd(tcp, tcp->u_arg[0]);
2089                 printpath(tcp, tcp->u_arg[1]);
2090                 tprints(", ");
2091                 print_dirfd(tcp, tcp->u_arg[2]);
2092                 printpath(tcp, tcp->u_arg[3]);
2093         }
2094         return 0;
2095 }
2096 #endif
2097
2098 int
2099 sys_chown(struct tcb *tcp)
2100 {
2101         if (entering(tcp)) {
2102                 printpath(tcp, tcp->u_arg[0]);
2103                 printuid(", ", tcp->u_arg[1]);
2104                 printuid(", ", tcp->u_arg[2]);
2105         }
2106         return 0;
2107 }
2108
2109 #ifdef LINUX
2110 int
2111 sys_fchownat(struct tcb *tcp)
2112 {
2113         if (entering(tcp)) {
2114                 print_dirfd(tcp, tcp->u_arg[0]);
2115                 printpath(tcp, tcp->u_arg[1]);
2116                 printuid(", ", tcp->u_arg[2]);
2117                 printuid(", ", tcp->u_arg[3]);
2118                 tprints(", ");
2119                 printflags(fstatatflags, tcp->u_arg[4], "AT_???");
2120         }
2121         return 0;
2122 }
2123 #endif
2124
2125 int
2126 sys_fchown(struct tcb *tcp)
2127 {
2128         if (entering(tcp)) {
2129                 printfd(tcp, tcp->u_arg[0]);
2130                 printuid(", ", tcp->u_arg[1]);
2131                 printuid(", ", tcp->u_arg[2]);
2132         }
2133         return 0;
2134 }
2135
2136 static int
2137 decode_chmod(struct tcb *tcp, int offset)
2138 {
2139         if (entering(tcp)) {
2140                 printpath(tcp, tcp->u_arg[offset]);
2141                 tprintf(", %#lo", tcp->u_arg[offset + 1]);
2142         }
2143         return 0;
2144 }
2145
2146 int
2147 sys_chmod(struct tcb *tcp)
2148 {
2149         return decode_chmod(tcp, 0);
2150 }
2151
2152 #ifdef LINUX
2153 int
2154 sys_fchmodat(struct tcb *tcp)
2155 {
2156         if (entering(tcp))
2157                 print_dirfd(tcp, tcp->u_arg[0]);
2158         return decode_chmod(tcp, 1);
2159 }
2160 #endif
2161
2162 int
2163 sys_fchmod(struct tcb *tcp)
2164 {
2165         if (entering(tcp)) {
2166                 printfd(tcp, tcp->u_arg[0]);
2167                 tprintf(", %#lo", tcp->u_arg[1]);
2168         }
2169         return 0;
2170 }
2171
2172 #ifdef ALPHA
2173 int
2174 sys_osf_utimes(struct tcb *tcp)
2175 {
2176         if (entering(tcp)) {
2177                 printpath(tcp, tcp->u_arg[0]);
2178                 tprints(", ");
2179                 printtv_bitness(tcp, tcp->u_arg[1], BITNESS_32,  0);
2180         }
2181         return 0;
2182 }
2183 #endif
2184
2185 static int
2186 decode_utimes(struct tcb *tcp, int offset, int special)
2187 {
2188         if (entering(tcp)) {
2189                 printpath(tcp, tcp->u_arg[offset]);
2190                 tprints(", ");
2191                 if (tcp->u_arg[offset + 1] == 0)
2192                         tprints("NULL");
2193                 else {
2194                         tprints("{");
2195                         printtv_bitness(tcp, tcp->u_arg[offset + 1],
2196                                         BITNESS_CURRENT, special);
2197                         tprints(", ");
2198                         printtv_bitness(tcp, tcp->u_arg[offset + 1]
2199                                         + sizeof(struct timeval),
2200                                         BITNESS_CURRENT, special);
2201                         tprints("}");
2202                 }
2203         }
2204         return 0;
2205 }
2206
2207 int
2208 sys_utimes(struct tcb *tcp)
2209 {
2210         return decode_utimes(tcp, 0, 0);
2211 }
2212
2213 #ifdef LINUX
2214 int
2215 sys_futimesat(struct tcb *tcp)
2216 {
2217         if (entering(tcp))
2218                 print_dirfd(tcp, tcp->u_arg[0]);
2219         return decode_utimes(tcp, 1, 0);
2220 }
2221
2222 int
2223 sys_utimensat(struct tcb *tcp)
2224 {
2225         if (entering(tcp)) {
2226                 print_dirfd(tcp, tcp->u_arg[0]);
2227                 decode_utimes(tcp, 1, 1);
2228                 tprints(", ");
2229                 printflags(utimensatflags, tcp->u_arg[3], "AT_???");
2230         }
2231         return 0;
2232 }
2233 #endif
2234
2235 int
2236 sys_utime(struct tcb *tcp)
2237 {
2238         union {
2239                 long utl[2];
2240                 int uti[2];
2241         } u;
2242
2243         if (entering(tcp)) {
2244                 printpath(tcp, tcp->u_arg[0]);
2245                 tprints(", ");
2246                 if (!tcp->u_arg[1])
2247                         tprints("NULL");
2248                 else if (!verbose(tcp))
2249                         tprintf("%#lx", tcp->u_arg[1]);
2250                 else if (umoven(tcp, tcp->u_arg[1],
2251                                 2 * personality_wordsize[current_personality],
2252                                 (char *) &u) < 0)
2253                         tprints("[?, ?]");
2254                 else if (personality_wordsize[current_personality]
2255                          == sizeof u.utl[0]) {
2256                         tprintf("[%s,", sprinttime(u.utl[0]));
2257                         tprintf(" %s]", sprinttime(u.utl[1]));
2258                 }
2259                 else if (personality_wordsize[current_personality]
2260                          == sizeof u.uti[0]) {
2261                         tprintf("[%s,", sprinttime(u.uti[0]));
2262                         tprintf(" %s]", sprinttime(u.uti[1]));
2263                 }
2264                 else
2265                         abort();
2266         }
2267         return 0;
2268 }
2269
2270 static int
2271 decode_mknod(struct tcb *tcp, int offset)
2272 {
2273         int mode = tcp->u_arg[offset + 1];
2274
2275         if (entering(tcp)) {
2276                 printpath(tcp, tcp->u_arg[offset]);
2277                 tprintf(", %s", sprintmode(mode));
2278                 switch (mode & S_IFMT) {
2279                 case S_IFCHR: case S_IFBLK:
2280 #ifdef LINUXSPARC
2281                         if (current_personality == 1)
2282                         tprintf(", makedev(%lu, %lu)",
2283                                 (unsigned long) ((tcp->u_arg[offset + 2] >> 18) & 0x3fff),
2284                                 (unsigned long) (tcp->u_arg[offset + 2] & 0x3ffff));
2285                         else
2286 #endif
2287                         tprintf(", makedev(%lu, %lu)",
2288                                 (unsigned long) major(tcp->u_arg[offset + 2]),
2289                                 (unsigned long) minor(tcp->u_arg[offset + 2]));
2290                         break;
2291                 default:
2292                         break;
2293                 }
2294         }
2295         return 0;
2296 }
2297
2298 int
2299 sys_mknod(struct tcb *tcp)
2300 {
2301         return decode_mknod(tcp, 0);
2302 }
2303
2304 #ifdef LINUX
2305 int
2306 sys_mknodat(struct tcb *tcp)
2307 {
2308         if (entering(tcp))
2309                 print_dirfd(tcp, tcp->u_arg[0]);
2310         return decode_mknod(tcp, 1);
2311 }
2312 #endif
2313
2314 #ifdef FREEBSD
2315 int
2316 sys_mkfifo(struct tcb *tcp)
2317 {
2318         if (entering(tcp)) {
2319                 printpath(tcp, tcp->u_arg[0]);
2320                 tprintf(", %#lo", tcp->u_arg[1]);
2321         }
2322         return 0;
2323 }
2324 #endif /* FREEBSD */
2325
2326 int
2327 sys_fsync(struct tcb *tcp)
2328 {
2329         if (entering(tcp)) {
2330                 printfd(tcp, tcp->u_arg[0]);
2331         }
2332         return 0;
2333 }
2334
2335 #ifdef LINUX
2336
2337 static void
2338 printdir(struct tcb *tcp, long addr)
2339 {
2340         struct dirent d;
2341
2342         if (!verbose(tcp)) {
2343                 tprintf("%#lx", addr);
2344                 return;
2345         }
2346         if (umove(tcp, addr, &d) < 0) {
2347                 tprints("{...}");
2348                 return;
2349         }
2350         tprintf("{d_ino=%ld, ", (unsigned long) d.d_ino);
2351         tprints("d_name=");
2352         printpathn(tcp, (long) ((struct dirent *) addr)->d_name, d.d_reclen);
2353         tprints("}");
2354 }
2355
2356 int
2357 sys_readdir(struct tcb *tcp)
2358 {
2359         if (entering(tcp)) {
2360                 printfd(tcp, tcp->u_arg[0]);
2361                 tprints(", ");
2362         } else {
2363                 if (syserror(tcp) || tcp->u_rval == 0 || !verbose(tcp))
2364                         tprintf("%#lx", tcp->u_arg[1]);
2365                 else
2366                         printdir(tcp, tcp->u_arg[1]);
2367                 /* Not much point in printing this out, it is always 1. */
2368                 if (tcp->u_arg[2] != 1)
2369                         tprintf(", %lu", tcp->u_arg[2]);
2370         }
2371         return 0;
2372 }
2373
2374 #endif /* LINUX */
2375
2376 #if defined FREEBSD || defined LINUX
2377 static const struct xlat direnttypes[] = {
2378         { DT_UNKNOWN,   "DT_UNKNOWN"    },
2379         { DT_FIFO,      "DT_FIFO"       },
2380         { DT_CHR,       "DT_CHR"        },
2381         { DT_DIR,       "DT_DIR"        },
2382         { DT_BLK,       "DT_BLK"        },
2383         { DT_REG,       "DT_REG"        },
2384         { DT_LNK,       "DT_LNK"        },
2385         { DT_SOCK,      "DT_SOCK"       },
2386         { DT_WHT,       "DT_WHT"        },
2387         { 0,            NULL            },
2388 };
2389
2390 #endif
2391
2392 int
2393 sys_getdents(struct tcb *tcp)
2394 {
2395         int i, len, dents = 0;
2396         char *buf;
2397
2398         if (entering(tcp)) {
2399                 printfd(tcp, tcp->u_arg[0]);
2400                 tprints(", ");
2401                 return 0;
2402         }
2403         if (syserror(tcp) || !verbose(tcp)) {
2404                 tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
2405                 return 0;
2406         }
2407         len = tcp->u_rval;
2408         /* Beware of insanely large or negative values in tcp->u_rval */
2409         if (tcp->u_rval > 1024*1024)
2410                 len = 1024*1024;
2411         if (tcp->u_rval < 0)
2412                 len = 0;
2413         buf = len ? malloc(len) : NULL;
2414         if (len && !buf)
2415                 die_out_of_memory();
2416         if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
2417                 tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
2418                 free(buf);
2419                 return 0;
2420         }
2421         if (!abbrev(tcp))
2422                 tprints("{");
2423         for (i = 0; i < len;) {
2424                 struct kernel_dirent *d = (struct kernel_dirent *) &buf[i];
2425 #ifdef LINUX
2426                 if (!abbrev(tcp)) {
2427                         tprintf("%s{d_ino=%lu, d_off=%lu, ",
2428                                 i ? " " : "", d->d_ino, d->d_off);
2429                         tprintf("d_reclen=%u, d_name=\"%s\"}",
2430                                 d->d_reclen, d->d_name);
2431                 }
2432 #endif /* LINUX */
2433 #ifdef SVR4
2434                 if (!abbrev(tcp)) {
2435                         tprintf("%s{d_ino=%lu, d_off=%lu, ",
2436                                 i ? " " : "",
2437                                 (unsigned long) d->d_ino,
2438                                 (unsigned long) d->d_off);
2439                         tprintf("d_reclen=%u, d_name=\"%s\"}",
2440                                 d->d_reclen, d->d_name);
2441                 }
2442 #endif /* SVR4 */
2443 #ifdef SUNOS4
2444                 if (!abbrev(tcp)) {
2445                         tprintf("%s{d_off=%lu, d_fileno=%lu, d_reclen=%u, ",
2446                                 i ? " " : "", d->d_off, d->d_fileno,
2447                                 d->d_reclen);
2448                         tprintf("d_namlen=%u, d_name=\"%.*s\"}",
2449                                 d->d_namlen, d->d_namlen, d->d_name);
2450                 }
2451 #endif /* SUNOS4 */
2452 #ifdef FREEBSD
2453                 if (!abbrev(tcp)) {
2454                         tprintf("%s{d_fileno=%u, d_reclen=%u, d_type=",
2455                                 i ? " " : "", d->d_fileno, d->d_reclen);
2456                         printxval(direnttypes, d->d_type, "DT_???");
2457                         tprintf(", d_namlen=%u, d_name=\"%.*s\"}",
2458                                 d->d_namlen, d->d_namlen, d->d_name);
2459                 }
2460 #endif /* FREEBSD */
2461                 if (!d->d_reclen) {
2462                         tprints("/* d_reclen == 0, problem here */");
2463                         break;
2464                 }
2465                 i += d->d_reclen;
2466                 dents++;
2467         }
2468         if (!abbrev(tcp))
2469                 tprints("}");
2470         else
2471                 tprintf("/* %u entries */", dents);
2472         tprintf(", %lu", tcp->u_arg[2]);
2473         free(buf);
2474         return 0;
2475 }
2476
2477
2478 #if _LFS64_LARGEFILE
2479 int
2480 sys_getdents64(struct tcb *tcp)
2481 {
2482         int i, len, dents = 0;
2483         char *buf;
2484
2485         if (entering(tcp)) {
2486                 printfd(tcp, tcp->u_arg[0]);
2487                 tprints(", ");
2488                 return 0;
2489         }
2490         if (syserror(tcp) || !verbose(tcp)) {
2491                 tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
2492                 return 0;
2493         }
2494
2495         len = tcp->u_rval;
2496         /* Beware of insanely large or negative tcp->u_rval */
2497         if (tcp->u_rval > 1024*1024)
2498                 len = 1024*1024;
2499         if (tcp->u_rval < 0)
2500                 len = 0;
2501         buf = len ? malloc(len) : NULL;
2502         if (len && !buf)
2503                 die_out_of_memory();
2504
2505         if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
2506                 tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
2507                 free(buf);
2508                 return 0;
2509         }
2510         if (!abbrev(tcp))
2511                 tprints("{");
2512         for (i = 0; i < len;) {
2513                 struct dirent64 *d = (struct dirent64 *) &buf[i];
2514 #if defined(LINUX) || defined(SVR4)
2515                 if (!abbrev(tcp)) {
2516                         tprintf("%s{d_ino=%" PRIu64 ", d_off=%" PRId64 ", ",
2517                                 i ? " " : "",
2518                                 d->d_ino,
2519                                 d->d_off);
2520 #ifdef LINUX
2521                         tprints("d_type=");
2522                         printxval(direnttypes, d->d_type, "DT_???");
2523                         tprints(", ");
2524 #endif
2525                         tprintf("d_reclen=%u, d_name=\"%s\"}",
2526                                 d->d_reclen, d->d_name);
2527                 }
2528 #endif /* LINUX || SVR4 */
2529 #ifdef SUNOS4
2530                 if (!abbrev(tcp)) {
2531                         tprintf("%s{d_off=%lu, d_fileno=%lu, d_reclen=%u, ",
2532                                 i ? " " : "", d->d_off, d->d_fileno,
2533                                 d->d_reclen);
2534                         tprintf("d_namlen=%u, d_name=\"%.*s\"}",
2535                                 d->d_namlen, d->d_namlen, d->d_name);
2536                 }
2537 #endif /* SUNOS4 */
2538                 if (!d->d_reclen) {
2539                         tprints("/* d_reclen == 0, problem here */");
2540                         break;
2541                 }
2542                 i += d->d_reclen;
2543                 dents++;
2544         }
2545         if (!abbrev(tcp))
2546                 tprints("}");
2547         else
2548                 tprintf("/* %u entries */", dents);
2549         tprintf(", %lu", tcp->u_arg[2]);
2550         free(buf);
2551         return 0;
2552 }
2553 #endif
2554
2555 #ifdef FREEBSD
2556 int
2557 sys_getdirentries(struct tcb *tcp)
2558 {
2559         int i, len, dents = 0;
2560         long basep;
2561         char *buf;
2562
2563         if (entering(tcp)) {
2564                 printfd(tcp, tcp->u_arg[0]);
2565                 tprints(", ");
2566                 return 0;
2567         }
2568         if (syserror(tcp) || !verbose(tcp)) {
2569                 tprintf("%#lx, %lu, %#lx", tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3]);
2570                 return 0;
2571         }
2572
2573         len = tcp->u_rval;
2574         /* Beware of insanely large or negative tcp->u_rval */
2575         if (tcp->u_rval > 1024*1024)
2576                 len = 1024*1024;
2577         if (tcp->u_rval < 0)
2578                 len = 0;
2579         buf = malloc(len);
2580         if (!buf)
2581                 die_out_of_memory();
2582
2583         if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
2584                 tprintf("%#lx, %lu, %#lx", tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3]);
2585                 free(buf);
2586                 return 0;
2587         }
2588         if (!abbrev(tcp))
2589                 tprints("{");
2590         for (i = 0; i < len;) {
2591                 struct kernel_dirent *d = (struct kernel_dirent *) &buf[i];
2592                 if (!abbrev(tcp)) {
2593                         tprintf("%s{d_fileno=%u, d_reclen=%u, d_type=",
2594                                 i ? " " : "", d->d_fileno, d->d_reclen);
2595                         printxval(direnttypes, d->d_type, "DT_???");
2596                         tprintf(", d_namlen=%u, d_name=\"%.*s\"}",
2597                                 d->d_namlen, d->d_namlen, d->d_name);
2598                 }
2599                 if (!d->d_reclen) {
2600                         tprints("/* d_reclen == 0, problem here */");
2601                         break;
2602                 }
2603                 i += d->d_reclen;
2604                 dents++;
2605         }
2606         if (!abbrev(tcp))
2607                 tprints("}");
2608         else
2609                 tprintf("/* %u entries */", dents);
2610         free(buf);
2611         tprintf(", %lu", tcp->u_arg[2]);
2612         if (umove(tcp, tcp->u_arg[3], &basep) < 0)
2613                 tprintf(", %#lx", tcp->u_arg[3]);
2614         else
2615                 tprintf(", [%lu]", basep);
2616         return 0;
2617 }
2618 #endif
2619
2620 #ifdef LINUX
2621 int
2622 sys_getcwd(struct tcb *tcp)
2623 {
2624         if (exiting(tcp)) {
2625                 if (syserror(tcp))
2626                         tprintf("%#lx", tcp->u_arg[0]);
2627                 else
2628                         printpathn(tcp, tcp->u_arg[0], tcp->u_rval - 1);
2629                 tprintf(", %lu", tcp->u_arg[1]);
2630         }
2631         return 0;
2632 }
2633 #endif /* LINUX */
2634
2635 #ifdef FREEBSD
2636 int
2637 sys___getcwd(struct tcb *tcp)
2638 {
2639         if (exiting(tcp)) {
2640                 if (syserror(tcp))
2641                         tprintf("%#lx", tcp->u_arg[0]);
2642                 else
2643                         printpathn(tcp, tcp->u_arg[0], tcp->u_arg[1]);
2644                 tprintf(", %lu", tcp->u_arg[1]);
2645         }
2646         return 0;
2647 }
2648 #endif
2649
2650 #ifdef HAVE_SYS_ASYNCH_H
2651
2652 int
2653 sys_aioread(struct tcb *tcp)
2654 {
2655         struct aio_result_t res;
2656
2657         if (entering(tcp)) {
2658                 tprintf("%lu, ", tcp->u_arg[0]);
2659         } else {
2660                 if (syserror(tcp))
2661                         tprintf("%#lx", tcp->u_arg[1]);
2662                 else
2663                         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2664                 tprintf(", %lu, %lu, ", tcp->u_arg[2], tcp->u_arg[3]);
2665                 printxval(whence, tcp->u_arg[4], "L_???");
2666                 if (syserror(tcp) || tcp->u_arg[5] == 0
2667                     || umove(tcp, tcp->u_arg[5], &res) < 0)
2668                         tprintf(", %#lx", tcp->u_arg[5]);
2669                 else
2670                         tprintf(", {aio_return %d aio_errno %d}",
2671                                 res.aio_return, res.aio_errno);
2672         }
2673         return 0;
2674 }
2675
2676 int
2677 sys_aiowrite(struct tcb *tcp)
2678 {
2679         struct aio_result_t res;
2680
2681         if (entering(tcp)) {
2682                 tprintf("%lu, ", tcp->u_arg[0]);
2683                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2684                 tprintf(", %lu, %lu, ", tcp->u_arg[2], tcp->u_arg[3]);
2685                 printxval(whence, tcp->u_arg[4], "L_???");
2686         }
2687         else {
2688                 if (tcp->u_arg[5] == 0)
2689                         tprints(", NULL");
2690                 else if (syserror(tcp)
2691                     || umove(tcp, tcp->u_arg[5], &res) < 0)
2692                         tprintf(", %#lx", tcp->u_arg[5]);
2693                 else
2694                         tprintf(", {aio_return %d aio_errno %d}",
2695                                 res.aio_return, res.aio_errno);
2696         }
2697         return 0;
2698 }
2699
2700 int
2701 sys_aiowait(struct tcb *tcp)
2702 {
2703         if (entering(tcp))
2704                 printtv(tcp, tcp->u_arg[0]);
2705         return 0;
2706 }
2707
2708 int
2709 sys_aiocancel(struct tcb *tcp)
2710 {
2711         struct aio_result_t res;
2712
2713         if (exiting(tcp)) {
2714                 if (tcp->u_arg[0] == 0)
2715                         tprints("NULL");
2716                 else if (syserror(tcp)
2717                     || umove(tcp, tcp->u_arg[0], &res) < 0)
2718                         tprintf("%#lx", tcp->u_arg[0]);
2719                 else
2720                         tprintf("{aio_return %d aio_errno %d}",
2721                                 res.aio_return, res.aio_errno);
2722         }
2723         return 0;
2724 }
2725
2726 #endif /* HAVE_SYS_ASYNCH_H */
2727
2728 static const struct xlat xattrflags[] = {
2729 #ifdef XATTR_CREATE
2730         { XATTR_CREATE,  "XATTR_CREATE" },
2731         { XATTR_REPLACE, "XATTR_REPLACE" },
2732 #endif
2733         { 0,             NULL }
2734 };
2735
2736 static void
2737 print_xattr_val(struct tcb *tcp, int failed,
2738                 unsigned long arg,
2739                 unsigned long insize,
2740                 unsigned long size)
2741 {
2742         if (!failed) {
2743                 unsigned long capacity = 4 * size + 1;
2744                 unsigned char *buf = (capacity < size) ? NULL : malloc(capacity);
2745                 if (buf == NULL || /* probably a bogus size argument */
2746                         umoven(tcp, arg, size, (char *) &buf[3 * size]) < 0) {
2747                         failed = 1;
2748                 }
2749                 else {
2750                         unsigned char *out = buf;
2751                         unsigned char *in = &buf[3 * size];
2752                         size_t i;
2753                         for (i = 0; i < size; ++i) {
2754                                 if (isprint(in[i]))
2755                                         *out++ = in[i];
2756                                 else {
2757 #define tohex(n) "0123456789abcdef"[n]
2758                                         *out++ = '\\';
2759                                         *out++ = 'x';
2760                                         *out++ = tohex(in[i] / 16);
2761                                         *out++ = tohex(in[i] % 16);
2762                                 }
2763                         }
2764                         /* Don't print terminating NUL if there is one.  */
2765                         if (i > 0 && in[i - 1] == '\0')
2766                                 out -= 4;
2767                         *out = '\0';
2768                         tprintf(", \"%s\", %ld", buf, insize);
2769                 }
2770                 free(buf);
2771         }
2772         if (failed)
2773                 tprintf(", 0x%lx, %ld", arg, insize);
2774 }
2775
2776 int
2777 sys_setxattr(struct tcb *tcp)
2778 {
2779         if (entering(tcp)) {
2780                 printpath(tcp, tcp->u_arg[0]);
2781                 tprints(", ");
2782                 printstr(tcp, tcp->u_arg[1], -1);
2783                 print_xattr_val(tcp, 0, tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[3]);
2784                 tprints(", ");
2785                 printflags(xattrflags, tcp->u_arg[4], "XATTR_???");
2786         }
2787         return 0;
2788 }
2789
2790 int
2791 sys_fsetxattr(struct tcb *tcp)
2792 {
2793         if (entering(tcp)) {
2794                 printfd(tcp, tcp->u_arg[0]);
2795                 tprints(", ");
2796                 printstr(tcp, tcp->u_arg[1], -1);
2797                 print_xattr_val(tcp, 0, tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[3]);
2798                 tprints(", ");
2799                 printflags(xattrflags, tcp->u_arg[4], "XATTR_???");
2800         }
2801         return 0;
2802 }
2803
2804 int
2805 sys_getxattr(struct tcb *tcp)
2806 {
2807         if (entering(tcp)) {
2808                 printpath(tcp, tcp->u_arg[0]);
2809                 tprints(", ");
2810                 printstr(tcp, tcp->u_arg[1], -1);
2811         } else {
2812                 print_xattr_val(tcp, syserror(tcp), tcp->u_arg[2], tcp->u_arg[3],
2813                                 tcp->u_rval);
2814         }
2815         return 0;
2816 }
2817
2818 int
2819 sys_fgetxattr(struct tcb *tcp)
2820 {
2821         if (entering(tcp)) {
2822                 printfd(tcp, tcp->u_arg[0]);
2823                 tprints(", ");
2824                 printstr(tcp, tcp->u_arg[1], -1);
2825         } else {
2826                 print_xattr_val(tcp, syserror(tcp), tcp->u_arg[2], tcp->u_arg[3],
2827                                 tcp->u_rval);
2828         }
2829         return 0;
2830 }
2831
2832 int
2833 sys_listxattr(struct tcb *tcp)
2834 {
2835         if (entering(tcp)) {
2836                 printpath(tcp, tcp->u_arg[0]);
2837         } else {
2838                 /* XXX Print value in format */
2839                 tprintf(", %p, %lu", (void *) tcp->u_arg[1], tcp->u_arg[2]);
2840         }
2841         return 0;
2842 }
2843
2844 int
2845 sys_flistxattr(struct tcb *tcp)
2846 {
2847         if (entering(tcp)) {
2848                 printfd(tcp, tcp->u_arg[0]);
2849         } else {
2850                 /* XXX Print value in format */
2851                 tprintf(", %p, %lu", (void *) tcp->u_arg[1], tcp->u_arg[2]);
2852         }
2853         return 0;
2854 }
2855
2856 int
2857 sys_removexattr(struct tcb *tcp)
2858 {
2859         if (entering(tcp)) {
2860                 printpath(tcp, tcp->u_arg[0]);
2861                 tprints(", ");
2862                 printstr(tcp, tcp->u_arg[1], -1);
2863         }
2864         return 0;
2865 }
2866
2867 int
2868 sys_fremovexattr(struct tcb *tcp)
2869 {
2870         if (entering(tcp)) {
2871                 printfd(tcp, tcp->u_arg[0]);
2872                 tprints(", ");
2873                 printstr(tcp, tcp->u_arg[1], -1);
2874         }
2875         return 0;
2876 }
2877
2878
2879 static const struct xlat advise[] = {
2880   { POSIX_FADV_NORMAL,          "POSIX_FADV_NORMAL"     },
2881   { POSIX_FADV_RANDOM,          "POSIX_FADV_RANDOM"     },
2882   { POSIX_FADV_SEQUENTIAL,      "POSIX_FADV_SEQUENTIAL" },
2883   { POSIX_FADV_WILLNEED,        "POSIX_FADV_WILLNEED"   },
2884   { POSIX_FADV_DONTNEED,        "POSIX_FADV_DONTNEED"   },
2885   { POSIX_FADV_NOREUSE,         "POSIX_FADV_NOREUSE"    },
2886   { 0,                          NULL                    }
2887 };
2888
2889
2890 #ifdef LINUX
2891 int
2892 sys_fadvise64(struct tcb *tcp)
2893 {
2894         if (entering(tcp)) {
2895                 int argn;
2896                 printfd(tcp, tcp->u_arg[0]);
2897                 tprints(", ");
2898                 argn = printllval(tcp, "%lld", 1);
2899                 tprintf(", %ld, ", tcp->u_arg[argn++]);
2900                 printxval(advise, tcp->u_arg[argn], "POSIX_FADV_???");
2901         }
2902         return 0;
2903 }
2904 #endif
2905
2906
2907 int
2908 sys_fadvise64_64(struct tcb *tcp)
2909 {
2910         if (entering(tcp)) {
2911                 int argn;
2912                 printfd(tcp, tcp->u_arg[0]);
2913                 tprints(", ");
2914 #if defined ARM || defined POWERPC
2915                 argn = printllval(tcp, "%lld, ", 2);
2916 #else
2917                 argn = printllval(tcp, "%lld, ", 1);
2918 #endif
2919                 argn = printllval(tcp, "%lld, ", argn);
2920 #if defined ARM || defined POWERPC
2921                 printxval(advise, tcp->u_arg[1], "POSIX_FADV_???");
2922 #else
2923                 printxval(advise, tcp->u_arg[argn], "POSIX_FADV_???");
2924 #endif
2925         }
2926         return 0;
2927 }
2928
2929 #ifdef LINUX
2930 static const struct xlat inotify_modes[] = {
2931         { 0x00000001,   "IN_ACCESS"     },
2932         { 0x00000002,   "IN_MODIFY"     },
2933         { 0x00000004,   "IN_ATTRIB"     },
2934         { 0x00000008,   "IN_CLOSE_WRITE"},
2935         { 0x00000010,   "IN_CLOSE_NOWRITE"},
2936         { 0x00000020,   "IN_OPEN"       },
2937         { 0x00000040,   "IN_MOVED_FROM" },
2938         { 0x00000080,   "IN_MOVED_TO"   },
2939         { 0x00000100,   "IN_CREATE"     },
2940         { 0x00000200,   "IN_DELETE"     },
2941         { 0x00000400,   "IN_DELETE_SELF"},
2942         { 0x00000800,   "IN_MOVE_SELF"  },
2943         { 0x00002000,   "IN_UNMOUNT"    },
2944         { 0x00004000,   "IN_Q_OVERFLOW" },
2945         { 0x00008000,   "IN_IGNORED"    },
2946         { 0x01000000,   "IN_ONLYDIR"    },
2947         { 0x02000000,   "IN_DONT_FOLLOW"},
2948         { 0x20000000,   "IN_MASK_ADD"   },
2949         { 0x40000000,   "IN_ISDIR"      },
2950         { 0x80000000,   "IN_ONESHOT"    },
2951         { 0,            NULL            }
2952 };
2953
2954 static const struct xlat inotify_init_flags[] = {
2955         { 0x00000800,   "IN_NONBLOCK"   },
2956         { 0x00080000,   "IN_CLOEXEC"    },
2957         { 0,            NULL            }
2958 };
2959
2960 int
2961 sys_inotify_add_watch(struct tcb *tcp)
2962 {
2963         if (entering(tcp)) {
2964                 printfd(tcp, tcp->u_arg[0]);
2965                 tprints(", ");
2966                 printpath(tcp, tcp->u_arg[1]);
2967                 tprints(", ");
2968                 printflags(inotify_modes, tcp->u_arg[2], "IN_???");
2969         }
2970         return 0;
2971 }
2972
2973 int
2974 sys_inotify_rm_watch(struct tcb *tcp)
2975 {
2976         if (entering(tcp)) {
2977                 printfd(tcp, tcp->u_arg[0]);
2978                 tprintf(", %ld", tcp->u_arg[1]);
2979         }
2980         return 0;
2981 }
2982
2983 int
2984 sys_inotify_init1(struct tcb *tcp)
2985 {
2986         if (entering(tcp))
2987                 printflags(inotify_init_flags, tcp->u_arg[0], "IN_???");
2988         return 0;
2989 }
2990
2991 int
2992 sys_fallocate(struct tcb *tcp)
2993 {
2994         if (entering(tcp)) {
2995                 int argn;
2996                 printfd(tcp, tcp->u_arg[0]);            /* fd */
2997                 tprints(", ");
2998                 tprintf("%#lo, ", tcp->u_arg[1]);       /* mode */
2999                 argn = printllval(tcp, "%llu, ", 2);    /* offset */
3000                 printllval(tcp, "%llu", argn);          /* len */
3001         }
3002         return 0;
3003 }
3004 #endif