]> granicus.if.org Git - strace/blob - file.c
file.c: export sprintmode and move it to a separate file
[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
33 #if defined(SPARC) || defined(SPARC64)
34 struct stat {
35         unsigned short  st_dev;
36         unsigned int    st_ino;
37         unsigned short  st_mode;
38         short           st_nlink;
39         unsigned short  st_uid;
40         unsigned short  st_gid;
41         unsigned short  st_rdev;
42         unsigned int    st_size;
43         int             st_atime;
44         unsigned int    __unused1;
45         int             st_mtime;
46         unsigned int    __unused2;
47         int             st_ctime;
48         unsigned int    __unused3;
49         int             st_blksize;
50         int             st_blocks;
51         unsigned int    __unused4[2];
52 };
53 # if defined(SPARC64)
54 struct stat_sparc64 {
55         unsigned int    st_dev;
56         unsigned long   st_ino;
57         unsigned int    st_mode;
58         unsigned int    st_nlink;
59         unsigned int    st_uid;
60         unsigned int    st_gid;
61         unsigned int    st_rdev;
62         long            st_size;
63         long            st_atime;
64         long            st_mtime;
65         long            st_ctime;
66         long            st_blksize;
67         long            st_blocks;
68         unsigned long   __unused4[2];
69 };
70 # endif /* SPARC64 */
71 # define stat kernel_stat
72 # include <asm/stat.h>
73 # undef stat
74 #elif defined(X32)
75 struct stat {
76         unsigned long long      st_dev;
77         unsigned long long      st_ino;
78         unsigned long long      st_nlink;
79
80         unsigned int            st_mode;
81         unsigned int            st_uid;
82         unsigned int            st_gid;
83         unsigned int            __pad0;
84         unsigned long long      st_rdev;
85         long long               st_size;
86         long long               st_blksize;
87         long long               st_blocks;
88
89         unsigned long long      st_atime;
90         unsigned long long      st_atime_nsec;
91         unsigned long long      st_mtime;
92         unsigned long long      st_mtime_nsec;
93         unsigned long long      st_ctime;
94         unsigned long long      st_ctime_nsec;
95         long long               __unused[3];
96 };
97
98 struct stat64 {
99         unsigned long long      st_dev;
100         unsigned char           __pad0[4];
101         unsigned long           __st_ino;
102         unsigned int            st_mode;
103         unsigned int            st_nlink;
104         unsigned long           st_uid;
105         unsigned long           st_gid;
106         unsigned long long      st_rdev;
107         unsigned char           __pad3[4];
108         long long               st_size;
109         unsigned long           st_blksize;
110         unsigned long long      st_blocks;
111         unsigned long           st_atime;
112         unsigned long           st_atime_nsec;
113         unsigned long           st_mtime;
114         unsigned int            st_mtime_nsec;
115         unsigned long           st_ctime;
116         unsigned long           st_ctime_nsec;
117         unsigned long long      st_ino;
118 } __attribute__((packed));
119 # define HAVE_STAT64    1
120
121 struct __old_kernel_stat {
122         unsigned short st_dev;
123         unsigned short st_ino;
124         unsigned short st_mode;
125         unsigned short st_nlink;
126         unsigned short st_uid;
127         unsigned short st_gid;
128         unsigned short st_rdev;
129         unsigned int   st_size;
130         unsigned int   st_atime;
131         unsigned int   st_mtime;
132         unsigned int   st_ctime;
133 };
134 #else
135 # undef dev_t
136 # undef ino_t
137 # undef mode_t
138 # undef nlink_t
139 # undef uid_t
140 # undef gid_t
141 # undef off_t
142 # undef loff_t
143 # define dev_t __kernel_dev_t
144 # define ino_t __kernel_ino_t
145 # define mode_t __kernel_mode_t
146 # define nlink_t __kernel_nlink_t
147 # define uid_t __kernel_uid_t
148 # define gid_t __kernel_gid_t
149 # define off_t __kernel_off_t
150 # define loff_t __kernel_loff_t
151
152 # include <asm/stat.h>
153
154 # undef dev_t
155 # undef ino_t
156 # undef mode_t
157 # undef nlink_t
158 # undef uid_t
159 # undef gid_t
160 # undef off_t
161 # undef loff_t
162 # define dev_t dev_t
163 # define ino_t ino_t
164 # define mode_t mode_t
165 # define nlink_t nlink_t
166 # define uid_t uid_t
167 # define gid_t gid_t
168 # define off_t off_t
169 # define loff_t loff_t
170 #endif
171
172 #define stat libc_stat
173 #define stat64 libc_stat64
174 #include <sys/stat.h>
175 #undef stat
176 #undef stat64
177 /* These might be macros. */
178 #undef st_atime
179 #undef st_mtime
180 #undef st_ctime
181
182 #include <fcntl.h>
183
184 #ifdef MAJOR_IN_SYSMACROS
185 # include <sys/sysmacros.h>
186 #endif
187
188 #ifdef MAJOR_IN_MKDEV
189 # include <sys/mkdev.h>
190 #endif
191
192 #ifdef O_LARGEFILE
193 # if O_LARGEFILE == 0          /* biarch platforms in 64-bit mode */
194 #  undef O_LARGEFILE
195 #  ifdef SPARC64
196 #   define O_LARGEFILE 0x40000
197 #  elif defined X86_64 || defined S390X
198 #   define O_LARGEFILE 0100000
199 #  endif
200 # endif
201 #endif
202
203 #include "xlat/open_access_modes.h"
204 #include "xlat/open_mode_flags.h"
205
206 #ifndef AT_FDCWD
207 # define AT_FDCWD                -100
208 #endif
209
210 /* The fd is an "int", so when decoding x86 on x86_64, we need to force sign
211  * extension to get the right value.  We do this by declaring fd as int here.
212  */
213 void
214 print_dirfd(struct tcb *tcp, int fd)
215 {
216         if (fd == AT_FDCWD)
217                 tprints("AT_FDCWD, ");
218         else {
219                 printfd(tcp, fd);
220                 tprints(", ");
221         }
222 }
223
224 /*
225  * low bits of the open(2) flags define access mode,
226  * other bits are real flags.
227  */
228 const char *
229 sprint_open_modes(int flags)
230 {
231         static char outstr[(1 + ARRAY_SIZE(open_mode_flags)) * sizeof("O_LARGEFILE")];
232         char *p;
233         char sep;
234         const char *str;
235         const struct xlat *x;
236
237         sep = ' ';
238         p = stpcpy(outstr, "flags");
239         str = xlookup(open_access_modes, flags & 3);
240         if (str) {
241                 *p++ = sep;
242                 p = stpcpy(p, str);
243                 flags &= ~3;
244                 if (!flags)
245                         return outstr;
246                 sep = '|';
247         }
248
249         for (x = open_mode_flags; x->str; x++) {
250                 if ((flags & x->val) == x->val) {
251                         *p++ = sep;
252                         p = stpcpy(p, x->str);
253                         flags &= ~x->val;
254                         if (!flags)
255                                 return outstr;
256                         sep = '|';
257                 }
258         }
259         /* flags is still nonzero */
260         *p++ = sep;
261         sprintf(p, "%#x", flags);
262         return outstr;
263 }
264
265 void
266 tprint_open_modes(int flags)
267 {
268         tprints(sprint_open_modes(flags) + sizeof("flags"));
269 }
270
271 static int
272 decode_open(struct tcb *tcp, int offset)
273 {
274         if (entering(tcp)) {
275                 printpath(tcp, tcp->u_arg[offset]);
276                 tprints(", ");
277                 /* flags */
278                 tprint_open_modes(tcp->u_arg[offset + 1]);
279                 if (tcp->u_arg[offset + 1] & O_CREAT) {
280                         /* mode */
281                         tprintf(", %#lo", tcp->u_arg[offset + 2]);
282                 }
283         }
284         return RVAL_FD;
285 }
286
287 int
288 sys_open(struct tcb *tcp)
289 {
290         return decode_open(tcp, 0);
291 }
292
293 int
294 sys_openat(struct tcb *tcp)
295 {
296         if (entering(tcp))
297                 print_dirfd(tcp, tcp->u_arg[0]);
298         return decode_open(tcp, 1);
299 }
300
301 #if defined(SPARC) || defined(SPARC64)
302 #include "xlat/openmodessol.h"
303
304 int
305 solaris_open(struct tcb *tcp)
306 {
307         if (entering(tcp)) {
308                 printpath(tcp, tcp->u_arg[0]);
309                 tprints(", ");
310                 /* flags */
311                 printflags(openmodessol, tcp->u_arg[1] + 1, "O_???");
312                 if (tcp->u_arg[1] & 0x100) {
313                         /* mode */
314                         tprintf(", %#lo", tcp->u_arg[2]);
315                 }
316         }
317         return 0;
318 }
319
320 #endif
321
322 int
323 sys_creat(struct tcb *tcp)
324 {
325         if (entering(tcp)) {
326                 printpath(tcp, tcp->u_arg[0]);
327                 tprintf(", %#lo", tcp->u_arg[1]);
328         }
329         return RVAL_FD;
330 }
331
332 #include "xlat/access_flags.h"
333
334 static int
335 decode_access(struct tcb *tcp, int offset)
336 {
337         if (entering(tcp)) {
338                 printpath(tcp, tcp->u_arg[offset]);
339                 tprints(", ");
340                 printflags(access_flags, tcp->u_arg[offset + 1], "?_OK");
341         }
342         return 0;
343 }
344
345 int
346 sys_access(struct tcb *tcp)
347 {
348         return decode_access(tcp, 0);
349 }
350
351 int
352 sys_faccessat(struct tcb *tcp)
353 {
354         if (entering(tcp))
355                 print_dirfd(tcp, tcp->u_arg[0]);
356         return decode_access(tcp, 1);
357 }
358
359 int
360 sys_umask(struct tcb *tcp)
361 {
362         if (entering(tcp)) {
363                 tprintf("%#lo", tcp->u_arg[0]);
364         }
365         return RVAL_OCTAL;
366 }
367
368 #include "xlat/whence_codes.h"
369
370 /* Linux kernel has exactly one version of lseek:
371  * fs/read_write.c::SYSCALL_DEFINE3(lseek, unsigned, fd, off_t, offset, unsigned, origin)
372  * In kernel, off_t is always the same as (kernel's) long
373  * (see include/uapi/asm-generic/posix_types.h),
374  * which means that on x32 we need to use tcp->ext_arg[N] to get offset argument.
375  * Use test/x32_lseek.c to test lseek decoding.
376  */
377 #if defined(LINUX_MIPSN32) || defined(X32)
378 int
379 sys_lseek(struct tcb *tcp)
380 {
381         long long offset;
382         int whence;
383
384         if (entering(tcp)) {
385                 printfd(tcp, tcp->u_arg[0]);
386                 offset = tcp->ext_arg[1];
387                 whence = tcp->u_arg[2];
388                 if (whence == SEEK_SET)
389                         tprintf(", %llu, ", offset);
390                 else
391                         tprintf(", %lld, ", offset);
392                 printxval(whence_codes, whence, "SEEK_???");
393         }
394         return RVAL_LUDECIMAL;
395 }
396 #else
397 int
398 sys_lseek(struct tcb *tcp)
399 {
400         long offset;
401         int whence;
402
403         if (entering(tcp)) {
404                 printfd(tcp, tcp->u_arg[0]);
405                 offset = tcp->u_arg[1];
406                 whence = tcp->u_arg[2];
407                 if (whence == SEEK_SET)
408                         tprintf(", %lu, ", offset);
409                 else
410                         tprintf(", %ld, ", offset);
411                 printxval(whence_codes, whence, "SEEK_???");
412         }
413         return RVAL_UDECIMAL;
414 }
415 #endif
416
417 /* llseek syscall takes explicitly two ulong arguments hi, lo,
418  * rather than one 64-bit argument for which LONG_LONG works
419  * appropriate for the native byte order.
420  *
421  * See kernel's fs/read_write.c::SYSCALL_DEFINE5(llseek, ...)
422  *
423  * hi,lo are "unsigned longs" and combined exactly this way in kernel:
424  * ((loff_t) hi << 32) | lo
425  * Note that for architectures with kernel's long wider than userspace long
426  * (such as x32), combining code will use *kernel's*, i.e. *wide* longs
427  * for hi and lo. We would need to use tcp->ext_arg[N] on x32...
428  * ...however, x32 (and x86_64) does not _have_ llseek syscall as such.
429  */
430 int
431 sys_llseek(struct tcb *tcp)
432 {
433         if (entering(tcp)) {
434                 printfd(tcp, tcp->u_arg[0]);
435                 if (tcp->u_arg[4] == SEEK_SET)
436                         tprintf(", %llu, ",
437                                 ((long long) tcp->u_arg[1]) << 32 |
438                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
439                 else
440                         tprintf(", %lld, ",
441                                 ((long long) tcp->u_arg[1]) << 32 |
442                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
443         }
444         else {
445                 long long off;
446                 if (syserror(tcp) || umove(tcp, tcp->u_arg[3], &off) < 0)
447                         tprintf("%#lx, ", tcp->u_arg[3]);
448                 else
449                         tprintf("[%llu], ", off);
450                 printxval(whence_codes, tcp->u_arg[4], "SEEK_???");
451         }
452         return 0;
453 }
454
455 int
456 sys_readahead(struct tcb *tcp)
457 {
458         if (entering(tcp)) {
459                 int argn;
460                 printfd(tcp, tcp->u_arg[0]);
461                 argn = printllval(tcp, ", %lld", 1);
462                 tprintf(", %ld", tcp->u_arg[argn]);
463         }
464         return 0;
465 }
466
467 int
468 sys_truncate(struct tcb *tcp)
469 {
470         if (entering(tcp)) {
471                 printpath(tcp, tcp->u_arg[0]);
472                 tprintf(", %lu", tcp->u_arg[1]);
473         }
474         return 0;
475 }
476
477 int
478 sys_truncate64(struct tcb *tcp)
479 {
480         if (entering(tcp)) {
481                 printpath(tcp, tcp->u_arg[0]);
482                 printllval(tcp, ", %llu", 1);
483         }
484         return 0;
485 }
486
487 int
488 sys_ftruncate(struct tcb *tcp)
489 {
490         if (entering(tcp)) {
491                 printfd(tcp, tcp->u_arg[0]);
492                 tprintf(", %lu", tcp->u_arg[1]);
493         }
494         return 0;
495 }
496
497 int
498 sys_ftruncate64(struct tcb *tcp)
499 {
500         if (entering(tcp)) {
501                 printfd(tcp, tcp->u_arg[0]);
502                 printllval(tcp, ", %llu", 1);
503         }
504         return 0;
505 }
506
507 /* several stats */
508
509 static char *
510 sprinttime(time_t t)
511 {
512         struct tm *tmp;
513         static char buf[sizeof("yyyy/mm/dd-hh:mm:ss")];
514
515         if (t == 0) {
516                 strcpy(buf, "0");
517                 return buf;
518         }
519         tmp = localtime(&t);
520         if (tmp)
521                 snprintf(buf, sizeof buf, "%02d/%02d/%02d-%02d:%02d:%02d",
522                         tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
523                         tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
524         else
525                 snprintf(buf, sizeof buf, "%lu", (unsigned long) t);
526
527         return buf;
528 }
529
530 #if defined(SPARC) || defined(SPARC64)
531 typedef struct {
532         int     tv_sec;
533         int     tv_nsec;
534 } timestruct_t;
535
536 struct solstat {
537         unsigned        st_dev;
538         int             st_pad1[3];     /* network id */
539         unsigned        st_ino;
540         unsigned        st_mode;
541         unsigned        st_nlink;
542         unsigned        st_uid;
543         unsigned        st_gid;
544         unsigned        st_rdev;
545         int             st_pad2[2];
546         int             st_size;
547         int             st_pad3;        /* st_size, off_t expansion */
548         timestruct_t    st_atime;
549         timestruct_t    st_mtime;
550         timestruct_t    st_ctime;
551         int             st_blksize;
552         int             st_blocks;
553         char            st_fstype[16];
554         int             st_pad4[8];     /* expansion area */
555 };
556
557 static void
558 printstatsol(struct tcb *tcp, long addr)
559 {
560         struct solstat statbuf;
561
562         if (umove(tcp, addr, &statbuf) < 0) {
563                 tprints("{...}");
564                 return;
565         }
566         if (!abbrev(tcp)) {
567                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
568                         (unsigned long) ((statbuf.st_dev >> 18) & 0x3fff),
569                         (unsigned long) (statbuf.st_dev & 0x3ffff),
570                         (unsigned long) statbuf.st_ino,
571                         sprintmode(statbuf.st_mode));
572                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
573                         (unsigned long) statbuf.st_nlink,
574                         (unsigned long) statbuf.st_uid,
575                         (unsigned long) statbuf.st_gid);
576                 tprintf("st_blksize=%lu, ", (unsigned long) statbuf.st_blksize);
577                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
578         }
579         else
580                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
581         switch (statbuf.st_mode & S_IFMT) {
582         case S_IFCHR: case S_IFBLK:
583                 tprintf("st_rdev=makedev(%lu, %lu), ",
584                         (unsigned long) ((statbuf.st_rdev >> 18) & 0x3fff),
585                         (unsigned long) (statbuf.st_rdev & 0x3ffff));
586                 break;
587         default:
588                 tprintf("st_size=%u, ", statbuf.st_size);
589                 break;
590         }
591         if (!abbrev(tcp)) {
592                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime.tv_sec));
593                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime.tv_sec));
594                 tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime.tv_sec));
595         }
596         else
597                 tprints("...}");
598 }
599
600 # if defined(SPARC64)
601 static void
602 printstat_sparc64(struct tcb *tcp, long addr)
603 {
604         struct stat_sparc64 statbuf;
605
606         if (umove(tcp, addr, &statbuf) < 0) {
607                 tprints("{...}");
608                 return;
609         }
610
611         if (!abbrev(tcp)) {
612                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
613                         (unsigned long) major(statbuf.st_dev),
614                         (unsigned long) minor(statbuf.st_dev),
615                         (unsigned long) statbuf.st_ino,
616                         sprintmode(statbuf.st_mode));
617                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
618                         (unsigned long) statbuf.st_nlink,
619                         (unsigned long) statbuf.st_uid,
620                         (unsigned long) statbuf.st_gid);
621                 tprintf("st_blksize=%lu, ",
622                         (unsigned long) statbuf.st_blksize);
623                 tprintf("st_blocks=%lu, ",
624                         (unsigned long) statbuf.st_blocks);
625         }
626         else
627                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
628         switch (statbuf.st_mode & S_IFMT) {
629         case S_IFCHR: case S_IFBLK:
630                 tprintf("st_rdev=makedev(%lu, %lu), ",
631                         (unsigned long) major(statbuf.st_rdev),
632                         (unsigned long) minor(statbuf.st_rdev));
633                 break;
634         default:
635                 tprintf("st_size=%lu, ", statbuf.st_size);
636                 break;
637         }
638         if (!abbrev(tcp)) {
639                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
640                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
641                 tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime));
642         }
643         else
644                 tprints("...}");
645 }
646 # endif /* SPARC64 */
647 #endif /* SPARC[64] */
648
649 #if defined POWERPC64
650 struct stat_powerpc32 {
651         unsigned int    st_dev;
652         unsigned int    st_ino;
653         unsigned int    st_mode;
654         unsigned short  st_nlink;
655         unsigned int    st_uid;
656         unsigned int    st_gid;
657         unsigned int    st_rdev;
658         unsigned int    st_size;
659         unsigned int    st_blksize;
660         unsigned int    st_blocks;
661         unsigned int    st_atime;
662         unsigned int    st_atime_nsec;
663         unsigned int    st_mtime;
664         unsigned int    st_mtime_nsec;
665         unsigned int    st_ctime;
666         unsigned int    st_ctime_nsec;
667         unsigned int    __unused4;
668         unsigned int    __unused5;
669 };
670
671 static void
672 printstat_powerpc32(struct tcb *tcp, long addr)
673 {
674         struct stat_powerpc32 statbuf;
675
676         if (umove(tcp, addr, &statbuf) < 0) {
677                 tprints("{...}");
678                 return;
679         }
680
681         if (!abbrev(tcp)) {
682                 tprintf("{st_dev=makedev(%u, %u), st_ino=%u, st_mode=%s, ",
683                         major(statbuf.st_dev), minor(statbuf.st_dev),
684                         statbuf.st_ino,
685                         sprintmode(statbuf.st_mode));
686                 tprintf("st_nlink=%u, st_uid=%u, st_gid=%u, ",
687                         statbuf.st_nlink, statbuf.st_uid, statbuf.st_gid);
688                 tprintf("st_blksize=%u, ", statbuf.st_blksize);
689                 tprintf("st_blocks=%u, ", statbuf.st_blocks);
690         }
691         else
692                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
693         switch (statbuf.st_mode & S_IFMT) {
694         case S_IFCHR: case S_IFBLK:
695                 tprintf("st_rdev=makedev(%lu, %lu), ",
696                         (unsigned long) major(statbuf.st_rdev),
697                         (unsigned long) minor(statbuf.st_rdev));
698                 break;
699         default:
700                 tprintf("st_size=%u, ", statbuf.st_size);
701                 break;
702         }
703         if (!abbrev(tcp)) {
704                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
705                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
706                 tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime));
707         }
708         else
709                 tprints("...}");
710 }
711 #endif /* POWERPC64 */
712
713 #include "xlat/fileflags.h"
714
715 static void
716 realprintstat(struct tcb *tcp, struct stat *statbuf)
717 {
718         if (!abbrev(tcp)) {
719                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
720                         (unsigned long) major(statbuf->st_dev),
721                         (unsigned long) minor(statbuf->st_dev),
722                         (unsigned long) statbuf->st_ino,
723                         sprintmode(statbuf->st_mode));
724                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
725                         (unsigned long) statbuf->st_nlink,
726                         (unsigned long) statbuf->st_uid,
727                         (unsigned long) statbuf->st_gid);
728 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
729                 tprintf("st_blksize=%lu, ", (unsigned long) statbuf->st_blksize);
730 #endif
731 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
732                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf->st_blocks);
733 #endif
734         }
735         else
736                 tprintf("{st_mode=%s, ", sprintmode(statbuf->st_mode));
737         switch (statbuf->st_mode & S_IFMT) {
738         case S_IFCHR: case S_IFBLK:
739 #ifdef HAVE_STRUCT_STAT_ST_RDEV
740                 tprintf("st_rdev=makedev(%lu, %lu), ",
741                         (unsigned long) major(statbuf->st_rdev),
742                         (unsigned long) minor(statbuf->st_rdev));
743 #else /* !HAVE_STRUCT_STAT_ST_RDEV */
744                 tprintf("st_size=makedev(%lu, %lu), ",
745                         (unsigned long) major(statbuf->st_size),
746                         (unsigned long) minor(statbuf->st_size));
747 #endif /* !HAVE_STRUCT_STAT_ST_RDEV */
748                 break;
749         default:
750                 tprintf("st_size=%lu, ", (unsigned long) statbuf->st_size);
751                 break;
752         }
753         if (!abbrev(tcp)) {
754                 tprintf("st_atime=%s, ", sprinttime(statbuf->st_atime));
755                 tprintf("st_mtime=%s, ", sprinttime(statbuf->st_mtime));
756                 tprintf("st_ctime=%s", sprinttime(statbuf->st_ctime));
757 #if HAVE_STRUCT_STAT_ST_FLAGS
758                 tprints(", st_flags=");
759                 printflags(fileflags, statbuf->st_flags, "UF_???");
760 #endif
761 #if HAVE_STRUCT_STAT_ST_ACLCNT
762                 tprintf(", st_aclcnt=%d", statbuf->st_aclcnt);
763 #endif
764 #if HAVE_STRUCT_STAT_ST_LEVEL
765                 tprintf(", st_level=%ld", statbuf->st_level);
766 #endif
767 #if HAVE_STRUCT_STAT_ST_FSTYPE
768                 tprintf(", st_fstype=%.*s",
769                         (int) sizeof statbuf->st_fstype, statbuf->st_fstype);
770 #endif
771 #if HAVE_STRUCT_STAT_ST_GEN
772                 tprintf(", st_gen=%u", statbuf->st_gen);
773 #endif
774                 tprints("}");
775         }
776         else
777                 tprints("...}");
778 }
779
780 #ifndef X32
781 static void
782 printstat(struct tcb *tcp, long addr)
783 {
784         struct stat statbuf;
785
786         if (!addr) {
787                 tprints("NULL");
788                 return;
789         }
790         if (syserror(tcp) || !verbose(tcp)) {
791                 tprintf("%#lx", addr);
792                 return;
793         }
794
795 #if defined(SPARC) || defined(SPARC64)
796         if (current_personality == 1) {
797                 printstatsol(tcp, addr);
798                 return;
799         }
800 #ifdef SPARC64
801         else if (current_personality == 2) {
802                 printstat_sparc64(tcp, addr);
803                 return;
804         }
805 #endif
806 #endif /* SPARC[64] */
807
808 #if defined POWERPC64
809         if (current_personality == 1) {
810                 printstat_powerpc32(tcp, addr);
811                 return;
812         }
813 #endif
814
815         if (umove(tcp, addr, &statbuf) < 0) {
816                 tprints("{...}");
817                 return;
818         }
819
820         realprintstat(tcp, &statbuf);
821 }
822 #else /* X32 */
823 # define printstat printstat64
824 #endif
825
826 #if !defined HAVE_STAT64 && (defined AARCH64 || defined X86_64)
827 /*
828  * Linux x86_64 has unified `struct stat' but its i386 biarch needs
829  * `struct stat64'.  Its <asm-i386/stat.h> definition expects 32-bit `long'.
830  * <linux/include/asm-x86_64/ia32.h> is not in the public includes set.
831  * __GNUC__ is needed for the required __attribute__ below.
832  *
833  * Similarly, aarch64 has a unified `struct stat' but its arm personality
834  * needs `struct stat64' (which also expects a 32-bit `long' but which
835  * shouldn't be packed).
836  */
837 struct stat64 {
838         unsigned long long      st_dev;
839         unsigned char   __pad0[4];
840         unsigned int    __st_ino;
841         unsigned int    st_mode;
842         unsigned int    st_nlink;
843         unsigned int    st_uid;
844         unsigned int    st_gid;
845         unsigned long long      st_rdev;
846         unsigned char   __pad3[4];
847         long long       st_size;
848         unsigned int    st_blksize;
849         unsigned long long      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 long long      st_ino;
857 }
858 # if defined X86_64
859    __attribute__((packed))
860 #  define STAT64_SIZE   96
861 #else
862 #  define STAT64_SIZE   104
863 # endif
864 ;
865 # define HAVE_STAT64    1
866 #endif
867
868 #ifdef HAVE_STAT64
869 static void
870 printstat64(struct tcb *tcp, long addr)
871 {
872 #ifdef X32
873         struct stat statbuf;
874 #else
875         struct stat64 statbuf;
876 #endif
877
878 #ifdef STAT64_SIZE
879         (void) sizeof(char[sizeof statbuf == STAT64_SIZE ? 1 : -1]);
880 #endif
881
882         if (!addr) {
883                 tprints("NULL");
884                 return;
885         }
886         if (syserror(tcp) || !verbose(tcp)) {
887                 tprintf("%#lx", addr);
888                 return;
889         }
890
891 #if defined(SPARC) || defined(SPARC64)
892         if (current_personality == 1) {
893                 printstatsol(tcp, addr);
894                 return;
895         }
896 # ifdef SPARC64
897         else if (current_personality == 2) {
898                 printstat_sparc64(tcp, addr);
899                 return;
900         }
901 # endif
902 #endif /* SPARC[64] */
903
904 #if defined AARCH64
905         if (current_personality != 0) {
906                 printstat(tcp, addr);
907                 return;
908         }
909 #endif
910 #if defined X86_64
911         if (current_personality != 1) {
912                 printstat(tcp, addr);
913                 return;
914         }
915 #endif
916
917         if (umove(tcp, addr, &statbuf) < 0) {
918                 tprints("{...}");
919                 return;
920         }
921
922         if (!abbrev(tcp)) {
923                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%llu, st_mode=%s, ",
924                         (unsigned long) major(statbuf.st_dev),
925                         (unsigned long) minor(statbuf.st_dev),
926                         (unsigned long long) statbuf.st_ino,
927                         sprintmode(statbuf.st_mode));
928                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
929                         (unsigned long) statbuf.st_nlink,
930                         (unsigned long) statbuf.st_uid,
931                         (unsigned long) statbuf.st_gid);
932 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
933                 tprintf("st_blksize=%lu, ",
934                         (unsigned long) statbuf.st_blksize);
935 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
936 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
937                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
938 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
939         }
940         else
941                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
942         switch (statbuf.st_mode & S_IFMT) {
943         case S_IFCHR: case S_IFBLK:
944 #ifdef HAVE_STRUCT_STAT_ST_RDEV
945                 tprintf("st_rdev=makedev(%lu, %lu), ",
946                         (unsigned long) major(statbuf.st_rdev),
947                         (unsigned long) minor(statbuf.st_rdev));
948 #else /* !HAVE_STRUCT_STAT_ST_RDEV */
949                 tprintf("st_size=makedev(%lu, %lu), ",
950                         (unsigned long) major(statbuf.st_size),
951                         (unsigned long) minor(statbuf.st_size));
952 #endif /* !HAVE_STRUCT_STAT_ST_RDEV */
953                 break;
954         default:
955                 tprintf("st_size=%llu, ", (unsigned long long) statbuf.st_size);
956                 break;
957         }
958         if (!abbrev(tcp)) {
959                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
960                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
961                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
962 #if HAVE_STRUCT_STAT_ST_FLAGS
963                 tprints(", st_flags=");
964                 printflags(fileflags, statbuf.st_flags, "UF_???");
965 #endif
966 #if HAVE_STRUCT_STAT_ST_ACLCNT
967                 tprintf(", st_aclcnt=%d", statbuf.st_aclcnt);
968 #endif
969 #if HAVE_STRUCT_STAT_ST_LEVEL
970                 tprintf(", st_level=%ld", statbuf.st_level);
971 #endif
972 #if HAVE_STRUCT_STAT_ST_FSTYPE
973                 tprintf(", st_fstype=%.*s",
974                         (int) sizeof statbuf.st_fstype, statbuf.st_fstype);
975 #endif
976 #if HAVE_STRUCT_STAT_ST_GEN
977                 tprintf(", st_gen=%u", statbuf.st_gen);
978 #endif
979                 tprints("}");
980         }
981         else
982                 tprints("...}");
983 }
984 #endif /* HAVE_STAT64 */
985
986 #if defined(HAVE_STRUCT___OLD_KERNEL_STAT)
987 static void
988 convertoldstat(const struct __old_kernel_stat *oldbuf, struct stat *newbuf)
989 {
990         newbuf->st_dev = oldbuf->st_dev;
991         newbuf->st_ino = oldbuf->st_ino;
992         newbuf->st_mode = oldbuf->st_mode;
993         newbuf->st_nlink = oldbuf->st_nlink;
994         newbuf->st_uid = oldbuf->st_uid;
995         newbuf->st_gid = oldbuf->st_gid;
996         newbuf->st_rdev = oldbuf->st_rdev;
997         newbuf->st_size = oldbuf->st_size;
998         newbuf->st_atime = oldbuf->st_atime;
999         newbuf->st_mtime = oldbuf->st_mtime;
1000         newbuf->st_ctime = oldbuf->st_ctime;
1001         newbuf->st_blksize = 0; /* not supported in old_stat */
1002         newbuf->st_blocks = 0; /* not supported in old_stat */
1003 }
1004
1005 static void
1006 printoldstat(struct tcb *tcp, long addr)
1007 {
1008         struct __old_kernel_stat statbuf;
1009         struct stat newstatbuf;
1010
1011         if (!addr) {
1012                 tprints("NULL");
1013                 return;
1014         }
1015         if (syserror(tcp) || !verbose(tcp)) {
1016                 tprintf("%#lx", addr);
1017                 return;
1018         }
1019
1020 # if defined(SPARC) || defined(SPARC64)
1021         if (current_personality == 1) {
1022                 printstatsol(tcp, addr);
1023                 return;
1024         }
1025 # endif
1026
1027         if (umove(tcp, addr, &statbuf) < 0) {
1028                 tprints("{...}");
1029                 return;
1030         }
1031
1032         convertoldstat(&statbuf, &newstatbuf);
1033         realprintstat(tcp, &newstatbuf);
1034 }
1035 #endif
1036
1037 int
1038 sys_stat(struct tcb *tcp)
1039 {
1040         if (entering(tcp)) {
1041                 printpath(tcp, tcp->u_arg[0]);
1042                 tprints(", ");
1043         } else {
1044                 printstat(tcp, tcp->u_arg[1]);
1045         }
1046         return 0;
1047 }
1048
1049 #ifdef X32
1050 static void
1051 printstat64_x32(struct tcb *tcp, long addr)
1052 {
1053         struct stat64 statbuf;
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 (umove(tcp, addr, &statbuf) < 0) {
1065                 tprints("{...}");
1066                 return;
1067         }
1068
1069         if (!abbrev(tcp)) {
1070                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%llu, st_mode=%s, ",
1071                         (unsigned long) major(statbuf.st_dev),
1072                         (unsigned long) minor(statbuf.st_dev),
1073                         (unsigned long long) statbuf.st_ino,
1074                         sprintmode(statbuf.st_mode));
1075                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
1076                         (unsigned long) statbuf.st_nlink,
1077                         (unsigned long) statbuf.st_uid,
1078                         (unsigned long) statbuf.st_gid);
1079                 tprintf("st_blksize=%lu, ",
1080                         (unsigned long) statbuf.st_blksize);
1081                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
1082         }
1083         else
1084                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
1085         switch (statbuf.st_mode & S_IFMT) {
1086         case S_IFCHR: case S_IFBLK:
1087                 tprintf("st_rdev=makedev(%lu, %lu), ",
1088                         (unsigned long) major(statbuf.st_rdev),
1089                         (unsigned long) minor(statbuf.st_rdev));
1090                 break;
1091         default:
1092                 tprintf("st_size=%llu, ", (unsigned long long) statbuf.st_size);
1093                 break;
1094         }
1095         if (!abbrev(tcp)) {
1096                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
1097                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
1098                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
1099                 tprints("}");
1100         }
1101         else
1102                 tprints("...}");
1103 }
1104 #endif /* X32 */
1105
1106 int
1107 sys_stat64(struct tcb *tcp)
1108 {
1109 #ifdef HAVE_STAT64
1110         if (entering(tcp)) {
1111                 printpath(tcp, tcp->u_arg[0]);
1112                 tprints(", ");
1113         } else {
1114 # ifdef X32
1115                 printstat64_x32(tcp, tcp->u_arg[1]);
1116 # else
1117                 printstat64(tcp, tcp->u_arg[1]);
1118 # endif
1119         }
1120         return 0;
1121 #else
1122         return printargs(tcp);
1123 #endif
1124 }
1125
1126 #ifndef AT_SYMLINK_NOFOLLOW
1127 # define AT_SYMLINK_NOFOLLOW    0x100
1128 #endif
1129 #ifndef AT_REMOVEDIR
1130 # define AT_REMOVEDIR           0x200
1131 #endif
1132 #ifndef AT_SYMLINK_FOLLOW
1133 # define AT_SYMLINK_FOLLOW      0x400
1134 #endif
1135 #ifndef AT_NO_AUTOMOUNT
1136 # define AT_NO_AUTOMOUNT        0x800
1137 #endif
1138 #ifndef AT_EMPTY_PATH
1139 # define AT_EMPTY_PATH          0x1000
1140 #endif
1141
1142 #include "xlat/at_flags.h"
1143
1144 int
1145 sys_newfstatat(struct tcb *tcp)
1146 {
1147         if (entering(tcp)) {
1148                 print_dirfd(tcp, tcp->u_arg[0]);
1149                 printpath(tcp, tcp->u_arg[1]);
1150                 tprints(", ");
1151         } else {
1152 #ifdef POWERPC64
1153                 if (current_personality == 0)
1154                         printstat(tcp, tcp->u_arg[2]);
1155                 else
1156                         printstat64(tcp, tcp->u_arg[2]);
1157 #elif defined HAVE_STAT64
1158                 printstat64(tcp, tcp->u_arg[2]);
1159 #else
1160                 printstat(tcp, tcp->u_arg[2]);
1161 #endif
1162                 tprints(", ");
1163                 printflags(at_flags, tcp->u_arg[3], "AT_???");
1164         }
1165         return 0;
1166 }
1167
1168 #if defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1169 int
1170 sys_oldstat(struct tcb *tcp)
1171 {
1172         if (entering(tcp)) {
1173                 printpath(tcp, tcp->u_arg[0]);
1174                 tprints(", ");
1175         } else {
1176                 printoldstat(tcp, tcp->u_arg[1]);
1177         }
1178         return 0;
1179 }
1180 #endif
1181
1182 int
1183 sys_fstat(struct tcb *tcp)
1184 {
1185         if (entering(tcp)) {
1186                 printfd(tcp, tcp->u_arg[0]);
1187                 tprints(", ");
1188         } else {
1189                 printstat(tcp, tcp->u_arg[1]);
1190         }
1191         return 0;
1192 }
1193
1194 int
1195 sys_fstat64(struct tcb *tcp)
1196 {
1197 #ifdef HAVE_STAT64
1198         if (entering(tcp)) {
1199                 printfd(tcp, tcp->u_arg[0]);
1200                 tprints(", ");
1201         } else {
1202 # ifdef X32
1203                 printstat64_x32(tcp, tcp->u_arg[1]);
1204 # else
1205                 printstat64(tcp, tcp->u_arg[1]);
1206 # endif
1207         }
1208         return 0;
1209 #else
1210         return printargs(tcp);
1211 #endif
1212 }
1213
1214 #if defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1215 int
1216 sys_oldfstat(struct tcb *tcp)
1217 {
1218         if (entering(tcp)) {
1219                 printfd(tcp, tcp->u_arg[0]);
1220                 tprints(", ");
1221         } else {
1222                 printoldstat(tcp, tcp->u_arg[1]);
1223         }
1224         return 0;
1225 }
1226 #endif
1227
1228 #if defined(SPARC) || defined(SPARC64)
1229
1230 int
1231 sys_xstat(struct tcb *tcp)
1232 {
1233         if (entering(tcp)) {
1234                 tprintf("%ld, ", tcp->u_arg[0]);
1235                 printpath(tcp, tcp->u_arg[1]);
1236                 tprints(", ");
1237         } else {
1238 # ifdef _STAT64_VER
1239                 if (tcp->u_arg[0] == _STAT64_VER)
1240                         printstat64(tcp, tcp->u_arg[2]);
1241                 else
1242 # endif
1243                 printstat(tcp, tcp->u_arg[2]);
1244         }
1245         return 0;
1246 }
1247
1248 int
1249 sys_fxstat(struct tcb *tcp)
1250 {
1251         if (entering(tcp))
1252                 tprintf("%ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1]);
1253         else {
1254 # ifdef _STAT64_VER
1255                 if (tcp->u_arg[0] == _STAT64_VER)
1256                         printstat64(tcp, tcp->u_arg[2]);
1257                 else
1258 # endif
1259                 printstat(tcp, tcp->u_arg[2]);
1260         }
1261         return 0;
1262 }
1263
1264 int
1265 sys_lxstat(struct tcb *tcp)
1266 {
1267         if (entering(tcp)) {
1268                 tprintf("%ld, ", tcp->u_arg[0]);
1269                 printpath(tcp, tcp->u_arg[1]);
1270                 tprints(", ");
1271         } else {
1272 # ifdef _STAT64_VER
1273                 if (tcp->u_arg[0] == _STAT64_VER)
1274                         printstat64(tcp, tcp->u_arg[2]);
1275                 else
1276 # endif
1277                 printstat(tcp, tcp->u_arg[2]);
1278         }
1279         return 0;
1280 }
1281
1282 int
1283 sys_xmknod(struct tcb *tcp)
1284 {
1285         int mode = tcp->u_arg[2];
1286
1287         if (entering(tcp)) {
1288                 tprintf("%ld, ", tcp->u_arg[0]);
1289                 printpath(tcp, tcp->u_arg[1]);
1290                 tprintf(", %s", sprintmode(mode));
1291                 switch (mode & S_IFMT) {
1292                 case S_IFCHR: case S_IFBLK:
1293                         tprintf(", makedev(%lu, %lu)",
1294                                 (unsigned long) ((tcp->u_arg[3] >> 18) & 0x3fff),
1295                                 (unsigned long) (tcp->u_arg[3] & 0x3ffff));
1296                         break;
1297                 default:
1298                         break;
1299                 }
1300         }
1301         return 0;
1302 }
1303
1304 #endif /* SPARC[64] */
1305
1306 /* directory */
1307 int
1308 sys_chdir(struct tcb *tcp)
1309 {
1310         if (entering(tcp)) {
1311                 printpath(tcp, tcp->u_arg[0]);
1312         }
1313         return 0;
1314 }
1315
1316 int
1317 sys_link(struct tcb *tcp)
1318 {
1319         if (entering(tcp)) {
1320                 printpath(tcp, tcp->u_arg[0]);
1321                 tprints(", ");
1322                 printpath(tcp, tcp->u_arg[1]);
1323         }
1324         return 0;
1325 }
1326
1327 int
1328 sys_linkat(struct tcb *tcp)
1329 {
1330         if (entering(tcp)) {
1331                 print_dirfd(tcp, tcp->u_arg[0]);
1332                 printpath(tcp, tcp->u_arg[1]);
1333                 tprints(", ");
1334                 print_dirfd(tcp, tcp->u_arg[2]);
1335                 printpath(tcp, tcp->u_arg[3]);
1336                 tprints(", ");
1337                 printflags(at_flags, tcp->u_arg[4], "AT_???");
1338         }
1339         return 0;
1340 }
1341
1342 int
1343 sys_unlinkat(struct tcb *tcp)
1344 {
1345         if (entering(tcp)) {
1346                 print_dirfd(tcp, tcp->u_arg[0]);
1347                 printpath(tcp, tcp->u_arg[1]);
1348                 tprints(", ");
1349                 printflags(at_flags, tcp->u_arg[2], "AT_???");
1350         }
1351         return 0;
1352 }
1353
1354 int
1355 sys_symlinkat(struct tcb *tcp)
1356 {
1357         if (entering(tcp)) {
1358                 printpath(tcp, tcp->u_arg[0]);
1359                 tprints(", ");
1360                 print_dirfd(tcp, tcp->u_arg[1]);
1361                 printpath(tcp, tcp->u_arg[2]);
1362         }
1363         return 0;
1364 }
1365
1366 static int
1367 decode_readlink(struct tcb *tcp, int offset)
1368 {
1369         if (entering(tcp)) {
1370                 printpath(tcp, tcp->u_arg[offset]);
1371                 tprints(", ");
1372         } else {
1373                 if (syserror(tcp))
1374                         tprintf("%#lx", tcp->u_arg[offset + 1]);
1375                 else
1376                         /* Used to use printpathn(), but readlink
1377                          * neither includes NUL in the returned count,
1378                          * nor actually writes it into memory.
1379                          * printpathn() would decide on printing
1380                          * "..." continuation based on garbage
1381                          * past return buffer's end.
1382                          */
1383                         printstr(tcp, tcp->u_arg[offset + 1], tcp->u_rval);
1384                 tprintf(", %lu", tcp->u_arg[offset + 2]);
1385         }
1386         return 0;
1387 }
1388
1389 int
1390 sys_readlink(struct tcb *tcp)
1391 {
1392         return decode_readlink(tcp, 0);
1393 }
1394
1395 int
1396 sys_readlinkat(struct tcb *tcp)
1397 {
1398         if (entering(tcp))
1399                 print_dirfd(tcp, tcp->u_arg[0]);
1400         return decode_readlink(tcp, 1);
1401 }
1402
1403 static void
1404 decode_renameat(struct tcb *tcp)
1405 {
1406         print_dirfd(tcp, tcp->u_arg[0]);
1407         printpath(tcp, tcp->u_arg[1]);
1408         tprints(", ");
1409         print_dirfd(tcp, tcp->u_arg[2]);
1410         printpath(tcp, tcp->u_arg[3]);
1411 }
1412
1413 int
1414 sys_renameat(struct tcb *tcp)
1415 {
1416         if (entering(tcp)) {
1417                 decode_renameat(tcp);
1418         }
1419         return 0;
1420 }
1421
1422 #include "xlat/rename_flags.h"
1423
1424 int
1425 sys_renameat2(struct tcb *tcp)
1426 {
1427         if (entering(tcp)) {
1428                 decode_renameat(tcp);
1429                 tprints(", ");
1430                 printflags(rename_flags, tcp->u_arg[4], "RENAME_??");
1431         }
1432         return 0;
1433 }
1434
1435 int
1436 sys_chown(struct tcb *tcp)
1437 {
1438         if (entering(tcp)) {
1439                 printpath(tcp, tcp->u_arg[0]);
1440                 printuid(", ", tcp->u_arg[1]);
1441                 printuid(", ", tcp->u_arg[2]);
1442         }
1443         return 0;
1444 }
1445
1446 int
1447 sys_fchownat(struct tcb *tcp)
1448 {
1449         if (entering(tcp)) {
1450                 print_dirfd(tcp, tcp->u_arg[0]);
1451                 printpath(tcp, tcp->u_arg[1]);
1452                 printuid(", ", tcp->u_arg[2]);
1453                 printuid(", ", tcp->u_arg[3]);
1454                 tprints(", ");
1455                 printflags(at_flags, tcp->u_arg[4], "AT_???");
1456         }
1457         return 0;
1458 }
1459
1460 int
1461 sys_fchown(struct tcb *tcp)
1462 {
1463         if (entering(tcp)) {
1464                 printfd(tcp, tcp->u_arg[0]);
1465                 printuid(", ", tcp->u_arg[1]);
1466                 printuid(", ", tcp->u_arg[2]);
1467         }
1468         return 0;
1469 }
1470
1471 static int
1472 decode_chmod(struct tcb *tcp, int offset)
1473 {
1474         if (entering(tcp)) {
1475                 printpath(tcp, tcp->u_arg[offset]);
1476                 tprintf(", %#lo", tcp->u_arg[offset + 1]);
1477         }
1478         return 0;
1479 }
1480
1481 int
1482 sys_chmod(struct tcb *tcp)
1483 {
1484         return decode_chmod(tcp, 0);
1485 }
1486
1487 int
1488 sys_fchmodat(struct tcb *tcp)
1489 {
1490         if (entering(tcp))
1491                 print_dirfd(tcp, tcp->u_arg[0]);
1492         return decode_chmod(tcp, 1);
1493 }
1494
1495 int
1496 sys_fchmod(struct tcb *tcp)
1497 {
1498         if (entering(tcp)) {
1499                 printfd(tcp, tcp->u_arg[0]);
1500                 tprintf(", %#lo", tcp->u_arg[1]);
1501         }
1502         return 0;
1503 }
1504
1505 #ifdef ALPHA
1506 int
1507 sys_osf_utimes(struct tcb *tcp)
1508 {
1509         if (entering(tcp)) {
1510                 printpath(tcp, tcp->u_arg[0]);
1511                 tprints(", ");
1512                 printtv_bitness(tcp, tcp->u_arg[1], BITNESS_32,  0);
1513         }
1514         return 0;
1515 }
1516 #endif
1517
1518 static int
1519 decode_utimes(struct tcb *tcp, int offset, int special)
1520 {
1521         if (entering(tcp)) {
1522                 printpath(tcp, tcp->u_arg[offset]);
1523                 tprints(", ");
1524                 if (tcp->u_arg[offset + 1] == 0)
1525                         tprints("NULL");
1526                 else {
1527                         tprints("{");
1528                         printtv_bitness(tcp, tcp->u_arg[offset + 1],
1529                                         BITNESS_CURRENT, special);
1530                         tprints(", ");
1531                         printtv_bitness(tcp, tcp->u_arg[offset + 1]
1532                                         + sizeof(struct timeval),
1533                                         BITNESS_CURRENT, special);
1534                         tprints("}");
1535                 }
1536         }
1537         return 0;
1538 }
1539
1540 int
1541 sys_utimes(struct tcb *tcp)
1542 {
1543         return decode_utimes(tcp, 0, 0);
1544 }
1545
1546 int
1547 sys_futimesat(struct tcb *tcp)
1548 {
1549         if (entering(tcp))
1550                 print_dirfd(tcp, tcp->u_arg[0]);
1551         return decode_utimes(tcp, 1, 0);
1552 }
1553
1554 int
1555 sys_utimensat(struct tcb *tcp)
1556 {
1557         if (entering(tcp)) {
1558                 print_dirfd(tcp, tcp->u_arg[0]);
1559                 decode_utimes(tcp, 1, 1);
1560                 tprints(", ");
1561                 printflags(at_flags, tcp->u_arg[3], "AT_???");
1562         }
1563         return 0;
1564 }
1565
1566 int
1567 sys_utime(struct tcb *tcp)
1568 {
1569         union {
1570                 long utl[2];
1571                 int uti[2];
1572                 long paranoia_for_huge_wordsize[4];
1573         } u;
1574         unsigned wordsize;
1575
1576         if (entering(tcp)) {
1577                 printpath(tcp, tcp->u_arg[0]);
1578                 tprints(", ");
1579
1580                 wordsize = current_wordsize;
1581                 if (!tcp->u_arg[1])
1582                         tprints("NULL");
1583                 else if (!verbose(tcp))
1584                         tprintf("%#lx", tcp->u_arg[1]);
1585                 else if (umoven(tcp, tcp->u_arg[1], 2 * wordsize, (char *) &u) < 0)
1586                         tprints("[?, ?]");
1587                 else if (wordsize == sizeof u.utl[0]) {
1588                         tprintf("[%s,", sprinttime(u.utl[0]));
1589                         tprintf(" %s]", sprinttime(u.utl[1]));
1590                 }
1591                 else if (wordsize == sizeof u.uti[0]) {
1592                         tprintf("[%s,", sprinttime(u.uti[0]));
1593                         tprintf(" %s]", sprinttime(u.uti[1]));
1594                 }
1595                 else
1596                         tprintf("<decode error: unsupported wordsize %d>",
1597                                 wordsize);
1598         }
1599         return 0;
1600 }
1601
1602 static int
1603 decode_mknod(struct tcb *tcp, int offset)
1604 {
1605         int mode = tcp->u_arg[offset + 1];
1606
1607         if (entering(tcp)) {
1608                 printpath(tcp, tcp->u_arg[offset]);
1609                 tprintf(", %s", sprintmode(mode));
1610                 switch (mode & S_IFMT) {
1611                 case S_IFCHR:
1612                 case S_IFBLK:
1613 #if defined(SPARC) || defined(SPARC64)
1614                         if (current_personality == 1)
1615                                 tprintf(", makedev(%lu, %lu)",
1616                                 (unsigned long) ((tcp->u_arg[offset + 2] >> 18) & 0x3fff),
1617                                 (unsigned long) (tcp->u_arg[offset + 2] & 0x3ffff));
1618                         else
1619 #endif
1620                                 tprintf(", makedev(%lu, %lu)",
1621                                 (unsigned long) major(tcp->u_arg[offset + 2]),
1622                                 (unsigned long) minor(tcp->u_arg[offset + 2]));
1623                         break;
1624                 default:
1625                         break;
1626                 }
1627         }
1628         return 0;
1629 }
1630
1631 int
1632 sys_mknod(struct tcb *tcp)
1633 {
1634         return decode_mknod(tcp, 0);
1635 }
1636
1637 int
1638 sys_mknodat(struct tcb *tcp)
1639 {
1640         if (entering(tcp))
1641                 print_dirfd(tcp, tcp->u_arg[0]);
1642         return decode_mknod(tcp, 1);
1643 }