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