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