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