611958637431030251c0a13a612bec7c0d685e10
[qemu] / linux-user / strace.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <sys/ipc.h>
4 #include <sys/msg.h>
5 #include <sys/sem.h>
6 #include <sys/shm.h>
7 #include <sys/select.h>
8 #include <sys/types.h>
9 #include <sys/mount.h>
10 #include <sys/mman.h>
11 #include <unistd.h>
12 #include "qemu.h"
13
14 int do_strace=0;
15
16 struct syscallname {
17     int nr;
18     const char *name;
19     const char *format;
20     void (*call)(const struct syscallname *,
21                  abi_long, abi_long, abi_long,
22                  abi_long, abi_long, abi_long);
23     void (*result)(const struct syscallname *, abi_long);
24 };
25
26 #ifdef __GNUC__
27 /*
28  * It is possible that target doesn't have syscall that uses
29  * following flags but we don't want the compiler to warn
30  * us about them being unused.  Same applies to utility print
31  * functions.  It is ok to keep them while not used.
32  */
33 #define UNUSED __attribute__ ((unused))
34 #else
35 #define UNUSED
36 #endif
37
38 /*
39  * Structure used to translate flag values into strings.  This is
40  * similar that is in the actual strace tool.
41  */
42 struct flags {
43     abi_long    f_value;  /* flag */
44     const char  *f_string; /* stringified flag */
45 };
46
47 /* common flags for all architectures */
48 #define FLAG_GENERIC(name) { name, #name }
49 /* target specific flags (syscall_defs.h has TARGET_<flag>) */
50 #define FLAG_TARGET(name)  { TARGET_ ## name, #name }
51 /* end of flags array */
52 #define FLAG_END           { 0, NULL }
53
54 UNUSED static const char *get_comma(int);
55 UNUSED static void print_pointer(abi_long, int);
56 UNUSED static void print_flags(const struct flags *, abi_long, int);
57 UNUSED static void print_at_dirfd(abi_long, int);
58 UNUSED static void print_file_mode(abi_long, int);
59 UNUSED static void print_open_flags(abi_long, int);
60 UNUSED static void print_syscall_prologue(const struct syscallname *);
61 UNUSED static void print_syscall_epilogue(const struct syscallname *);
62 UNUSED static void print_string(abi_long, int);
63 UNUSED static void print_raw_param(const char *, abi_long, int);
64 UNUSED static void print_timeval(abi_ulong, int);
65 UNUSED static void print_number(abi_long, int);
66
67 /*
68  * Utility functions
69  */
70 static void
71 print_ipc_cmd(int cmd)
72 {
73 #define output_cmd(val) \
74 if( cmd == val ) { \
75     gemu_log(#val); \
76     return; \
77 }
78
79     cmd &= 0xff;
80
81     /* General IPC commands */
82     output_cmd( IPC_RMID );
83     output_cmd( IPC_SET );
84     output_cmd( IPC_STAT );
85     output_cmd( IPC_INFO );
86     /* msgctl() commands */
87     #ifdef __USER_MISC
88     output_cmd( MSG_STAT );
89     output_cmd( MSG_INFO );
90     #endif
91     /* shmctl() commands */
92     output_cmd( SHM_LOCK );
93     output_cmd( SHM_UNLOCK );
94     output_cmd( SHM_STAT );
95     output_cmd( SHM_INFO );
96     /* semctl() commands */
97     output_cmd( GETPID );
98     output_cmd( GETVAL );
99     output_cmd( GETALL );
100     output_cmd( GETNCNT );
101     output_cmd( GETZCNT );
102     output_cmd( SETVAL );
103     output_cmd( SETALL );
104     output_cmd( SEM_STAT );
105     output_cmd( SEM_INFO );
106     output_cmd( IPC_RMID );
107     output_cmd( IPC_RMID );
108     output_cmd( IPC_RMID );
109     output_cmd( IPC_RMID );
110     output_cmd( IPC_RMID );
111     output_cmd( IPC_RMID );
112     output_cmd( IPC_RMID );
113     output_cmd( IPC_RMID );
114     output_cmd( IPC_RMID );
115
116     /* Some value we don't recognize */
117     gemu_log("%d",cmd);
118 }
119
120 #ifdef TARGET_NR__newselect
121 static void
122 print_fdset(int n, abi_ulong target_fds_addr)
123 {
124     int i;
125
126     gemu_log("[");
127     if( target_fds_addr ) {
128         abi_long *target_fds;
129
130         target_fds = lock_user(VERIFY_READ,
131                                target_fds_addr,
132                                sizeof(*target_fds)*(n / TARGET_ABI_BITS + 1),
133                                1);
134
135         if (!target_fds)
136             return;
137
138         for (i=n; i>=0; i--) {
139             if ((tswapl(target_fds[i / TARGET_ABI_BITS]) >> (i & (TARGET_ABI_BITS - 1))) & 1)
140                 gemu_log("%d,", i );
141             }
142         unlock_user(target_fds, target_fds_addr, 0);
143     }
144     gemu_log("]");
145 }
146 #endif
147
148 /*
149  * Sysycall specific output functions
150  */
151
152 /* select */
153 #ifdef TARGET_NR__newselect
154 static long newselect_arg1 = 0;
155 static long newselect_arg2 = 0;
156 static long newselect_arg3 = 0;
157 static long newselect_arg4 = 0;
158 static long newselect_arg5 = 0;
159
160 static void
161 print_newselect(const struct syscallname *name,
162                 abi_long arg1, abi_long arg2, abi_long arg3,
163                 abi_long arg4, abi_long arg5, abi_long arg6)
164 {
165     gemu_log("%s(" TARGET_ABI_FMT_ld ",", name->name, arg1);
166     print_fdset(arg1, arg2);
167     gemu_log(",");
168     print_fdset(arg1, arg3);
169     gemu_log(",");
170     print_fdset(arg1, arg4);
171     gemu_log(",");
172     print_timeval(arg5, 1);
173     gemu_log(")");
174
175     /* save for use in the return output function below */
176     newselect_arg1=arg1;
177     newselect_arg2=arg2;
178     newselect_arg3=arg3;
179     newselect_arg4=arg4;
180     newselect_arg5=arg5;
181 }
182 #endif
183
184 #ifdef TARGET_NR_semctl
185 static void
186 print_semctl(const struct syscallname *name,
187              abi_long arg1, abi_long arg2, abi_long arg3,
188              abi_long arg4, abi_long arg5, abi_long arg6)
189 {
190     gemu_log("%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ",", name->name, arg1, arg2);
191     print_ipc_cmd(arg3);
192     gemu_log(",0x" TARGET_ABI_FMT_lx ")", arg4);
193 }
194 #endif
195
196 static void
197 print_execve(const struct syscallname *name,
198              abi_long arg1, abi_long arg2, abi_long arg3,
199              abi_long arg4, abi_long arg5, abi_long arg6)
200 {
201     abi_ulong arg_ptr_addr;
202     char *s;
203
204     if (!(s = lock_user_string(arg1)))
205         return;
206     gemu_log("%s(\"%s\",{", name->name, s);
207     unlock_user(s, arg1, 0);
208
209     for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) {
210         abi_ulong *arg_ptr, arg_addr;
211
212         arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1);
213         if (!arg_ptr)
214             return;
215         arg_addr = tswapl(*arg_ptr);
216         unlock_user(arg_ptr, arg_ptr_addr, 0);
217         if (!arg_addr)
218             break;
219         if ((s = lock_user_string(arg_addr))) {
220             gemu_log("\"%s\",", s);
221             unlock_user(s, arg_addr, 0);
222         }
223     }
224
225     gemu_log("NULL})");
226 }
227
228 #ifdef TARGET_NR_ipc
229 static void
230 print_ipc(const struct syscallname *name,
231           abi_long arg1, abi_long arg2, abi_long arg3,
232           abi_long arg4, abi_long arg5, abi_long arg6)
233 {
234     switch(arg1) {
235     case IPCOP_semctl:
236         gemu_log("semctl(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ",", arg1, arg2);
237         print_ipc_cmd(arg3);
238         gemu_log(",0x" TARGET_ABI_FMT_lx ")", arg4);
239         break;
240     default:
241         gemu_log("%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ")",
242                  name->name, arg1, arg2, arg3, arg4);
243     }
244 }
245 #endif
246
247 /*
248  * Variants for the return value output function
249  */
250
251 static void
252 print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
253 {
254 if( ret == -1 ) {
255         gemu_log(" = -1 errno=%d (%s)\n", errno, target_strerror(errno));
256     } else {
257         gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
258     }
259 }
260
261 #if 0 /* currently unused */
262 static void
263 print_syscall_ret_raw(struct syscallname *name, abi_long ret)
264 {
265         gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
266 }
267 #endif
268
269 #ifdef TARGET_NR__newselect
270 static void
271 print_syscall_ret_newselect(const struct syscallname *name, abi_long ret)
272 {
273     gemu_log(" = 0x" TARGET_ABI_FMT_lx " (", ret);
274     print_fdset(newselect_arg1,newselect_arg2);
275     gemu_log(",");
276     print_fdset(newselect_arg1,newselect_arg3);
277     gemu_log(",");
278     print_fdset(newselect_arg1,newselect_arg4);
279     gemu_log(",");
280     print_timeval(newselect_arg5, 1);
281     gemu_log(")\n");
282 }
283 #endif
284
285 UNUSED static struct flags access_flags[] = {
286     FLAG_GENERIC(F_OK),
287     FLAG_GENERIC(R_OK),
288     FLAG_GENERIC(W_OK),
289     FLAG_GENERIC(X_OK),
290     FLAG_END,
291 };
292
293 UNUSED static struct flags at_file_flags[] = {
294     FLAG_GENERIC(AT_EACCESS),
295     FLAG_GENERIC(AT_SYMLINK_NOFOLLOW),
296     FLAG_END,
297 };
298
299 UNUSED static struct flags unlinkat_flags[] = {
300     FLAG_GENERIC(AT_REMOVEDIR),
301     FLAG_END,
302 };
303
304 UNUSED static struct flags mode_flags[] = {
305     FLAG_GENERIC(S_IFSOCK),
306     FLAG_GENERIC(S_IFLNK),
307     FLAG_GENERIC(S_IFREG),
308     FLAG_GENERIC(S_IFBLK),
309     FLAG_GENERIC(S_IFDIR),
310     FLAG_GENERIC(S_IFCHR),
311     FLAG_GENERIC(S_IFIFO),
312     FLAG_END,
313 };
314
315 UNUSED static struct flags open_access_flags[] = {
316     FLAG_TARGET(O_RDONLY),
317     FLAG_TARGET(O_WRONLY),
318     FLAG_TARGET(O_RDWR),
319     FLAG_END,
320 };
321
322 UNUSED static struct flags open_flags[] = {
323     FLAG_TARGET(O_APPEND),
324     FLAG_TARGET(O_CREAT),
325     FLAG_TARGET(O_DIRECTORY),
326     FLAG_TARGET(O_EXCL),
327     FLAG_TARGET(O_LARGEFILE),
328     FLAG_TARGET(O_NOCTTY),
329     FLAG_TARGET(O_NOFOLLOW),
330     FLAG_TARGET(O_NONBLOCK),      /* also O_NDELAY */
331     FLAG_TARGET(O_SYNC),
332     FLAG_TARGET(O_TRUNC),
333 #ifdef O_DIRECT
334     FLAG_TARGET(O_DIRECT),
335 #endif
336     FLAG_END,
337 };
338
339 UNUSED static struct flags mount_flags[] = {
340 #ifdef MS_BIND
341     FLAG_GENERIC(MS_BIND),
342 #endif
343 #ifdef MS_DIRSYNC
344     FLAG_GENERIC(MS_DIRSYNC),
345 #endif
346     FLAG_GENERIC(MS_MANDLOCK),
347 #ifdef MS_MOVE
348     FLAG_GENERIC(MS_MOVE),
349 #endif
350     FLAG_GENERIC(MS_NOATIME),
351     FLAG_GENERIC(MS_NODEV),
352     FLAG_GENERIC(MS_NODIRATIME),
353     FLAG_GENERIC(MS_NOEXEC),
354     FLAG_GENERIC(MS_NOSUID),
355     FLAG_GENERIC(MS_RDONLY),
356 #ifdef MS_RELATIME
357     FLAG_GENERIC(MS_RELATIME),
358 #endif
359     FLAG_GENERIC(MS_REMOUNT),
360     FLAG_GENERIC(MS_SYNCHRONOUS),
361     FLAG_END,
362 };
363
364 UNUSED static struct flags umount2_flags[] = {
365 #ifdef MNT_FORCE
366     FLAG_GENERIC(MNT_FORCE),
367 #endif
368 #ifdef MNT_DETACH
369     FLAG_GENERIC(MNT_DETACH),
370 #endif
371 #ifdef MNT_EXPIRE
372     FLAG_GENERIC(MNT_EXPIRE),
373 #endif
374     FLAG_END,
375 };
376
377 UNUSED static struct flags mmap_prot_flags[] = {
378     FLAG_GENERIC(PROT_NONE),
379     FLAG_GENERIC(PROT_EXEC),
380     FLAG_GENERIC(PROT_READ),
381     FLAG_GENERIC(PROT_WRITE),
382     FLAG_END,
383 };
384
385 UNUSED static struct flags mmap_flags[] = {
386     FLAG_TARGET(MAP_SHARED),
387     FLAG_TARGET(MAP_PRIVATE),
388     FLAG_TARGET(MAP_ANONYMOUS),
389     FLAG_TARGET(MAP_DENYWRITE),
390     FLAG_TARGET(MAP_FIXED),
391     FLAG_TARGET(MAP_GROWSDOWN),
392 #ifdef MAP_LOCKED
393     FLAG_TARGET(MAP_LOCKED),
394 #endif
395 #ifdef MAP_NONBLOCK
396     FLAG_TARGET(MAP_NONBLOCK),
397 #endif
398     FLAG_TARGET(MAP_NORESERVE),
399 #ifdef MAP_POPULATE
400     FLAG_TARGET(MAP_POPULATE),
401 #endif
402     FLAG_END,
403 };
404
405 UNUSED static struct flags fcntl_flags[] = {
406     FLAG_TARGET(F_DUPFD),
407     FLAG_TARGET(F_GETFD),
408     FLAG_TARGET(F_SETFD),
409     FLAG_TARGET(F_GETFL),
410     FLAG_TARGET(F_SETFL),
411     FLAG_TARGET(F_GETLK),
412     FLAG_TARGET(F_SETLK),
413     FLAG_TARGET(F_SETLKW),
414     FLAG_END,
415 };
416
417 /*
418  * print_xxx utility functions.  These are used to print syscall
419  * parameters in certain format.  All of these have parameter
420  * named 'last'.  This parameter is used to add comma to output
421  * when last == 0.
422  */
423
424 static const char *
425 get_comma(int last)
426 {
427     return ((last) ? "" : ",");
428 }
429
430 static void
431 print_flags(const struct flags *f, abi_long tflags, int last)
432 {
433     const char *sep = "";
434     int flags;
435     int n;
436
437     flags = (int)tswap32(tflags);
438
439     if ((flags == 0) && (f->f_value == 0)) {
440         gemu_log("%s%s", f->f_string, get_comma(last));
441         return;
442     }
443     for (n = 0; f->f_string != NULL; f++) {
444         if ((f->f_value != 0) && ((flags & f->f_value) == f->f_value)) {
445             gemu_log("%s%s", sep, f->f_string);
446             flags &= ~f->f_value;
447             sep = "|";
448             n++;
449         }
450     }
451
452     if (n > 0) {
453         /* print rest of the flags as numeric */
454         if (flags != 0) {
455             gemu_log("%s%#x%s", sep, flags, get_comma(last));
456         } else {
457             gemu_log("%s", get_comma(last));
458         }
459     } else {
460         /* no string version of flags found, print them in hex then */
461         gemu_log("%#x%s", flags, get_comma(last));
462     }
463 }
464
465 static void
466 print_at_dirfd(abi_long tdirfd, int last)
467 {
468     int dirfd = tswap32(tdirfd);
469
470     if (dirfd == AT_FDCWD)
471         gemu_log("AT_FDCWD%s", get_comma(last));
472     else
473         gemu_log("%d%s", dirfd, get_comma(last));
474 }
475
476 static void
477 print_file_mode(abi_long tmode, int last)
478 {
479     const char *sep = "";
480     const struct flags *m;
481     mode_t mode = (mode_t)tswap32(tmode);
482
483     for (m = &mode_flags[0]; m->f_string != NULL; m++) {
484         if ((m->f_value & mode) == m->f_value) {
485             gemu_log("%s%s", m->f_string, sep);
486             sep = "|";
487             mode &= ~m->f_value;
488             break;
489         }
490     }
491
492     mode &= ~S_IFMT;
493     /* print rest of the mode as octal */
494     if (mode != 0)
495         gemu_log("%s%#o", sep, mode);
496
497     gemu_log("%s", get_comma(last));
498 }
499
500 static void
501 print_open_flags(abi_long tflags, int last)
502 {
503     int flags = tswap32(tflags);
504
505     print_flags(open_access_flags, flags & TARGET_O_ACCMODE, 1);
506     flags &= ~TARGET_O_ACCMODE;
507     if (flags == 0) {
508         gemu_log("%s", get_comma(last));
509         return;
510     }
511     gemu_log("|");
512     print_flags(open_flags, flags, last);
513 }
514
515 static void
516 print_syscall_prologue(const struct syscallname *sc)
517 {
518     gemu_log("%s(", sc->name);
519 }
520
521 /*ARGSUSED*/
522 static void
523 print_syscall_epilogue(const struct syscallname *sc)
524 {
525     (void)sc;
526     gemu_log(")");
527 }
528
529 static void
530 print_string(abi_long addr, int last)
531 {
532     char *s;
533
534     if ((s = lock_user_string(addr)) != NULL) {
535         gemu_log("\"%s\"%s", s, get_comma(last));
536         unlock_user(s, addr, 0);
537     } else {
538         /* can't get string out of it, so print it as pointer */
539         print_pointer(addr, last);
540     }
541 }
542
543 /*
544  * Prints out raw parameter using given format.  Caller needs
545  * to do byte swapping if needed.
546  */
547 static void
548 print_raw_param(const char *fmt, abi_long param, int last)
549 {
550     char format[64];
551
552     (void) snprintf(format, sizeof (format), "%s%s", fmt, get_comma(last));
553     gemu_log(format, param);
554 }
555
556 static void
557 print_pointer(abi_long p, int last)
558 {
559     if (p == 0)
560         gemu_log("NULL%s", get_comma(last));
561     else
562         gemu_log("0x" TARGET_ABI_FMT_lx "%s", p, get_comma(last));
563 }
564
565 /*
566  * Reads 32-bit (int) number from guest address space from
567  * address 'addr' and prints it.
568  */
569 static void
570 print_number(abi_long addr, int last)
571 {
572     if (addr == 0) {
573         gemu_log("NULL%s", get_comma(last));
574     } else {
575         int num;
576
577         get_user_s32(num, addr);
578         gemu_log("[%d]%s", num, get_comma(last));
579     }
580 }
581
582 static void
583 print_timeval(abi_ulong tv_addr, int last)
584 {
585     if( tv_addr ) {
586         struct target_timeval *tv;
587
588         tv = lock_user(VERIFY_READ, tv_addr, sizeof(*tv), 1);
589         if (!tv)
590             return;
591         gemu_log("{" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "}%s",
592             tv->tv_sec, tv->tv_usec, get_comma(last));
593         unlock_user(tv, tv_addr, 0);
594     } else
595         gemu_log("NULL%s", get_comma(last));
596 }
597
598 #undef UNUSED
599
600 #ifdef TARGET_NR_accept
601 static void
602 print_accept(const struct syscallname *name,
603     abi_long arg0, abi_long arg1, abi_long arg2,
604     abi_long arg3, abi_long arg4, abi_long arg5)
605 {
606     print_syscall_prologue(name);
607     print_raw_param("%d", tswap32(arg0), 0);
608     print_pointer(arg1, 0);
609     print_number(arg2, 1);
610     print_syscall_epilogue(name);
611 }
612 #endif
613
614 #ifdef TARGET_NR_access
615 static void
616 print_access(const struct syscallname *name,
617     abi_long arg0, abi_long arg1, abi_long arg2,
618     abi_long arg3, abi_long arg4, abi_long arg5)
619 {
620     print_syscall_prologue(name);
621     print_string(arg0, 0);
622     print_flags(access_flags, arg1, 1);
623     print_syscall_epilogue(name);
624 }
625 #endif
626
627 #ifdef TARGET_NR_brk
628 static void
629 print_brk(const struct syscallname *name,
630     abi_long arg0, abi_long arg1, abi_long arg2,
631     abi_long arg3, abi_long arg4, abi_long arg5)
632 {
633     print_syscall_prologue(name);
634     print_pointer(arg0, 1);
635     print_syscall_epilogue(name);
636 }
637 #endif
638
639 #ifdef TARGET_NR_chdir
640 static void
641 print_chdir(const struct syscallname *name,
642     abi_long arg0, abi_long arg1, abi_long arg2,
643     abi_long arg3, abi_long arg4, abi_long arg5)
644 {
645     print_syscall_prologue(name);
646     print_string(arg0, 1);
647     print_syscall_epilogue(name);
648 }
649 #endif
650
651 #ifdef TARGET_NR_chmod
652 static void
653 print_chmod(const struct syscallname *name,
654     abi_long arg0, abi_long arg1, abi_long arg2,
655     abi_long arg3, abi_long arg4, abi_long arg5)
656 {
657     print_syscall_prologue(name);
658     print_string(arg0, 0);
659     print_file_mode(arg1, 1);
660     print_syscall_epilogue(name);
661 }
662 #endif
663
664 #ifdef TARGET_NR_creat
665 static void
666 print_creat(const struct syscallname *name,
667     abi_long arg0, abi_long arg1, abi_long arg2,
668     abi_long arg3, abi_long arg4, abi_long arg5)
669 {
670     print_syscall_prologue(name);
671     print_string(arg0, 0);
672     print_file_mode(arg1, 1);
673     print_syscall_epilogue(name);
674 }
675 #endif
676
677 #ifdef TARGET_NR_execv
678 static void
679 print_execv(const struct syscallname *name,
680     abi_long arg0, abi_long arg1, abi_long arg2,
681     abi_long arg3, abi_long arg4, abi_long arg5)
682 {
683     print_syscall_prologue(name);
684     print_string(arg0, 0);
685     print_raw_param("0x" TARGET_ABI_FMT_lx, tswapl(arg1), 1);
686     print_syscall_epilogue(name);
687 }
688 #endif
689
690 #ifdef TARGET_NR_faccessat
691 static void
692 print_faccessat(const struct syscallname *name,
693     abi_long arg0, abi_long arg1, abi_long arg2,
694     abi_long arg3, abi_long arg4, abi_long arg5)
695 {
696     print_syscall_prologue(name);
697     print_at_dirfd(arg0, 0);
698     print_string(arg1, 0);
699     print_flags(access_flags, arg2, 0);
700     print_flags(at_file_flags, arg3, 1);
701     print_syscall_epilogue(name);
702 }
703 #endif
704
705 #ifdef TARGET_NR_fchmodat
706 static void
707 print_fchmodat(const struct syscallname *name,
708     abi_long arg0, abi_long arg1, abi_long arg2,
709     abi_long arg3, abi_long arg4, abi_long arg5)
710 {
711     print_syscall_prologue(name);
712     print_at_dirfd(arg0, 0);
713     print_string(arg1, 0);
714     print_file_mode(arg2, 0);
715     print_flags(at_file_flags, arg3, 1);
716     print_syscall_epilogue(name);
717 }
718 #endif
719
720 #ifdef TARGET_NR_fchownat
721 static void
722 print_fchownat(const struct syscallname *name,
723     abi_long arg0, abi_long arg1, abi_long arg2,
724     abi_long arg3, abi_long arg4, abi_long arg5)
725 {
726     print_syscall_prologue(name);
727     print_at_dirfd(arg0, 0);
728     print_string(arg1, 0);
729 #ifdef USE_UID16
730     print_raw_param("%d", tswap16(arg2), 0);
731     print_raw_param("%d", tswap16(arg3), 0);
732 #else
733     print_raw_param("%d", tswap32(arg2), 0);
734     print_raw_param("%d", tswap32(arg3), 0);
735 #endif
736     print_flags(at_file_flags, arg4, 1);
737     print_syscall_epilogue(name);
738 }
739 #endif
740
741 #if defined(TARGET_NR_fcntl) || defined(TARGET_NR_fcntl64)
742 static void
743 print_fcntl(const struct syscallname *name,
744     abi_long arg0, abi_long arg1, abi_long arg2,
745     abi_long arg3, abi_long arg4, abi_long arg5)
746 {
747     print_syscall_prologue(name);
748     print_raw_param("%d", tswap32(arg0), 0);
749     print_flags(fcntl_flags, arg1, 0);
750     /*
751      * TODO: check flags and print following argument only
752      *       when needed.
753      */
754     print_pointer(arg2, 1);
755     print_syscall_epilogue(name);
756 }
757 #define print_fcntl64   print_fcntl
758 #endif
759
760
761 #ifdef TARGET_NR_futimesat
762 static void
763 print_futimesat(const struct syscallname *name,
764     abi_long arg0, abi_long arg1, abi_long arg2,
765     abi_long arg3, abi_long arg4, abi_long arg5)
766 {
767     print_syscall_prologue(name);
768     print_at_dirfd(arg0, 0);
769     print_string(arg1, 0);
770     print_timeval(arg2, 0);
771     print_timeval(arg2 + sizeof (struct target_timeval), 1);
772     print_syscall_epilogue(name);
773 }
774 #endif
775
776 #ifdef TARGET_NR_link
777 static void
778 print_link(const struct syscallname *name,
779     abi_long arg0, abi_long arg1, abi_long arg2,
780     abi_long arg3, abi_long arg4, abi_long arg5)
781 {
782     print_syscall_prologue(name);
783     print_string(arg0, 0);
784     print_string(arg1, 1);
785     print_syscall_epilogue(name);
786 }
787 #endif
788
789 #ifdef TARGET_NR_linkat
790 static void
791 print_linkat(const struct syscallname *name,
792     abi_long arg0, abi_long arg1, abi_long arg2,
793     abi_long arg3, abi_long arg4, abi_long arg5)
794 {
795     print_syscall_prologue(name);
796     print_at_dirfd(arg0, 0);
797     print_string(arg1, 0);
798     print_at_dirfd(arg2, 0);
799     print_string(arg3, 0);
800     print_flags(at_file_flags, arg4, 1);
801     print_syscall_epilogue(name);
802 }
803 #endif
804
805 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) || \
806     defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64)
807 static void
808 print_stat(const struct syscallname *name,
809     abi_long arg0, abi_long arg1, abi_long arg2,
810     abi_long arg3, abi_long arg4, abi_long arg5)
811 {
812     print_syscall_prologue(name);
813     print_string(arg0, 0);
814     print_pointer(arg1, 1);
815     print_syscall_epilogue(name);
816 }
817 #define print_lstat     print_stat
818 #define print_stat64    print_stat
819 #define print_lstat64   print_stat
820 #endif
821
822 #if defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64)
823 static void
824 print_fstat(const struct syscallname *name,
825     abi_long arg0, abi_long arg1, abi_long arg2,
826     abi_long arg3, abi_long arg4, abi_long arg5)
827 {
828     print_syscall_prologue(name);
829     print_raw_param("%d", tswap32(arg0), 0);
830     print_pointer(arg1, 1);
831     print_syscall_epilogue(name);
832 }
833 #define print_fstat64     print_fstat
834 #endif
835
836 #ifdef TARGET_NR_mkdir
837 static void
838 print_mkdir(const struct syscallname *name,
839     abi_long arg0, abi_long arg1, abi_long arg2,
840     abi_long arg3, abi_long arg4, abi_long arg5)
841 {
842     print_syscall_prologue(name);
843     print_string(arg0, 0);
844     print_file_mode(arg1, 1);
845     print_syscall_epilogue(name);
846 }
847 #endif
848
849 #ifdef TARGET_NR_mkdirat
850 static void
851 print_mkdirat(const struct syscallname *name,
852     abi_long arg0, abi_long arg1, abi_long arg2,
853     abi_long arg3, abi_long arg4, abi_long arg5)
854 {
855     print_syscall_prologue(name);
856     print_at_dirfd(arg0, 0);
857     print_string(arg1, 0);
858     print_file_mode(arg2, 1);
859     print_syscall_epilogue(name);
860 }
861 #endif
862
863 #ifdef TARGET_NR_mknod
864 static void
865 print_mknod(const struct syscallname *name,
866     abi_long arg0, abi_long arg1, abi_long arg2,
867     abi_long arg3, abi_long arg4, abi_long arg5)
868 {
869     int hasdev = (tswapl(arg1) & (S_IFCHR|S_IFBLK));
870
871     print_syscall_prologue(name);
872     print_string(arg0, 0);
873     print_file_mode(arg1, (hasdev == 0));
874     if (hasdev) {
875         print_raw_param("makedev(%d", major(tswapl(arg2)), 0);
876         print_raw_param("%d)", minor(tswapl(arg2)), 1);
877     }
878     print_syscall_epilogue(name);
879 }
880 #endif
881
882 #ifdef TARGET_NR_mknodat
883 static void
884 print_mknodat(const struct syscallname *name,
885     abi_long arg0, abi_long arg1, abi_long arg2,
886     abi_long arg3, abi_long arg4, abi_long arg5)
887 {
888     int hasdev = (tswapl(arg2) & (S_IFCHR|S_IFBLK));
889
890     print_syscall_prologue(name);
891     print_at_dirfd(arg0, 0);
892     print_string(arg1, 0);
893     print_file_mode(arg2, (hasdev == 0));
894     if (hasdev) {
895         print_raw_param("makedev(%d", major(tswapl(arg3)), 0);
896         print_raw_param("%d)", minor(tswapl(arg3)), 1);
897     }
898     print_syscall_epilogue(name);
899 }
900 #endif
901
902 #ifdef TARGET_NR_mq_open
903 static void
904 print_mq_open(const struct syscallname *name,
905     abi_long arg0, abi_long arg1, abi_long arg2,
906     abi_long arg3, abi_long arg4, abi_long arg5)
907 {
908     int is_creat = (tswapl(arg1) & TARGET_O_CREAT);
909
910     print_syscall_prologue(name);
911     print_string(arg0, 0);
912     print_open_flags(arg1, (is_creat == 0));
913     if (is_creat) {
914         print_file_mode(arg2, 0);
915         print_pointer(arg3, 1);
916     }
917     print_syscall_epilogue(name);
918 }
919 #endif
920
921 #ifdef TARGET_NR_open
922 static void
923 print_open(const struct syscallname *name,
924     abi_long arg0, abi_long arg1, abi_long arg2,
925     abi_long arg3, abi_long arg4, abi_long arg5)
926 {
927     int is_creat = (tswap32(arg1) & TARGET_O_CREAT);
928
929     print_syscall_prologue(name);
930     print_string(arg0, 0);
931     print_open_flags(arg1, (is_creat == 0));
932     if (is_creat)
933         print_file_mode(arg2, 1);
934     print_syscall_epilogue(name);
935 }
936 #endif
937
938 #ifdef TARGET_NR_openat
939 static void
940 print_openat(const struct syscallname *name,
941     abi_long arg0, abi_long arg1, abi_long arg2,
942     abi_long arg3, abi_long arg4, abi_long arg5)
943 {
944     int is_creat = (tswap32(arg2) & TARGET_O_CREAT);
945
946     print_syscall_prologue(name);
947     print_at_dirfd(arg0, 0);
948     print_string(arg1, 0);
949     print_open_flags(arg2, (is_creat == 0));
950     if (is_creat)
951         print_file_mode(arg3, 1);
952     print_syscall_epilogue(name);
953 }
954 #endif
955
956 #ifdef TARGET_NR_mq_unlink
957 static void
958 print_mq_unlink(const struct syscallname *name,
959     abi_long arg0, abi_long arg1, abi_long arg2,
960     abi_long arg3, abi_long arg4, abi_long arg5)
961 {
962     print_syscall_prologue(name);
963     print_string(arg0, 1);
964     print_syscall_epilogue(name);
965 }
966 #endif
967
968 #if defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)
969 static void
970 print_fstatat64(const struct syscallname *name,
971     abi_long arg0, abi_long arg1, abi_long arg2,
972     abi_long arg3, abi_long arg4, abi_long arg5)
973 {
974     print_syscall_prologue(name);
975     print_at_dirfd(arg0, 0);
976     print_string(arg1, 0);
977     print_pointer(arg2, 0);
978     print_flags(at_file_flags, arg3, 1);
979     print_syscall_epilogue(name);
980 }
981 #define print_newfstatat    print_fstatat64
982 #endif
983
984 #ifdef TARGET_NR_readlink
985 static void
986 print_readlink(const struct syscallname *name,
987     abi_long arg0, abi_long arg1, abi_long arg2,
988     abi_long arg3, abi_long arg4, abi_long arg5)
989 {
990     print_syscall_prologue(name);
991     print_string(arg0, 0);
992     print_pointer(arg1, 0);
993     print_raw_param("%u", tswapl(arg2), 1);
994     print_syscall_epilogue(name);
995 }
996 #endif
997
998 #ifdef TARGET_NR_readlinkat
999 static void
1000 print_readlinkat(const struct syscallname *name,
1001     abi_long arg0, abi_long arg1, abi_long arg2,
1002     abi_long arg3, abi_long arg4, abi_long arg5)
1003 {
1004     print_syscall_prologue(name);
1005     print_at_dirfd(arg0, 0);
1006     print_string(arg1, 0);
1007     print_pointer(arg2, 0);
1008     print_raw_param("%u", tswapl(arg3), 1);
1009     print_syscall_epilogue(name);
1010 }
1011 #endif
1012
1013 #ifdef TARGET_NR_rename
1014 static void
1015 print_rename(const struct syscallname *name,
1016     abi_long arg0, abi_long arg1, abi_long arg2,
1017     abi_long arg3, abi_long arg4, abi_long arg5)
1018 {
1019     print_syscall_prologue(name);
1020     print_string(arg0, 0);
1021     print_string(arg1, 1);
1022     print_syscall_epilogue(name);
1023 }
1024 #endif
1025
1026 #ifdef TARGET_NR_renameat
1027 static void
1028 print_renameat(const struct syscallname *name,
1029     abi_long arg0, abi_long arg1, abi_long arg2,
1030     abi_long arg3, abi_long arg4, abi_long arg5)
1031 {
1032     print_syscall_prologue(name);
1033     print_at_dirfd(arg0, 0);
1034     print_string(arg1, 0);
1035     print_at_dirfd(arg2, 0);
1036     print_string(arg3, 1);
1037     print_syscall_epilogue(name);
1038 }
1039 #endif
1040
1041 #ifdef TARGET_NR_statfs
1042 static void
1043 print_statfs(const struct syscallname *name,
1044     abi_long arg0, abi_long arg1, abi_long arg2,
1045     abi_long arg3, abi_long arg4, abi_long arg5)
1046 {
1047     print_syscall_prologue(name);
1048     print_string(arg0, 0);
1049     print_pointer(arg1, 1);
1050     print_syscall_epilogue(name);
1051 }
1052 #define print_statfs64  print_statfs
1053 #endif
1054
1055 #ifdef TARGET_NR_symlink
1056 static void
1057 print_symlink(const struct syscallname *name,
1058     abi_long arg0, abi_long arg1, abi_long arg2,
1059     abi_long arg3, abi_long arg4, abi_long arg5)
1060 {
1061     print_syscall_prologue(name);
1062     print_string(arg0, 0);
1063     print_string(arg1, 1);
1064     print_syscall_epilogue(name);
1065 }
1066 #endif
1067
1068 #ifdef TARGET_NR_symlinkat
1069 static void
1070 print_symlinkat(const struct syscallname *name,
1071     abi_long arg0, abi_long arg1, abi_long arg2,
1072     abi_long arg3, abi_long arg4, abi_long arg5)
1073 {
1074     print_syscall_prologue(name);
1075     print_string(arg0, 0);
1076     print_at_dirfd(arg1, 0);
1077     print_string(arg2, 1);
1078     print_syscall_epilogue(name);
1079 }
1080 #endif
1081
1082 #ifdef TARGET_NR_mount
1083 static void
1084 print_mount(const struct syscallname *name,
1085     abi_long arg0, abi_long arg1, abi_long arg2,
1086     abi_long arg3, abi_long arg4, abi_long arg5)
1087 {
1088     print_syscall_prologue(name);
1089     print_string(arg0, 0);
1090     print_string(arg1, 0);
1091     print_string(arg2, 0);
1092     print_flags(mount_flags, arg3, 0);
1093     print_pointer(arg4, 1);
1094     print_syscall_epilogue(name);
1095 }
1096 #endif
1097
1098 #ifdef TARGET_NR_umount
1099 static void
1100 print_umount(const struct syscallname *name,
1101     abi_long arg0, abi_long arg1, abi_long arg2,
1102     abi_long arg3, abi_long arg4, abi_long arg5)
1103 {
1104     print_syscall_prologue(name);
1105     print_string(arg0, 1);
1106     print_syscall_epilogue(name);
1107 }
1108 #endif
1109
1110 #ifdef TARGET_NR_umount2
1111 static void
1112 print_umount2(const struct syscallname *name,
1113     abi_long arg0, abi_long arg1, abi_long arg2,
1114     abi_long arg3, abi_long arg4, abi_long arg5)
1115 {
1116     print_syscall_prologue(name);
1117     print_string(arg0, 0);
1118     print_flags(umount2_flags, arg1, 1);
1119     print_syscall_epilogue(name);
1120 }
1121 #endif
1122
1123 #ifdef TARGET_NR_unlink
1124 static void
1125 print_unlink(const struct syscallname *name,
1126     abi_long arg0, abi_long arg1, abi_long arg2,
1127     abi_long arg3, abi_long arg4, abi_long arg5)
1128 {
1129     print_syscall_prologue(name);
1130     print_string(arg0, 1);
1131     print_syscall_epilogue(name);
1132 }
1133 #endif
1134
1135 #ifdef TARGET_NR_unlinkat
1136 static void
1137 print_unlinkat(const struct syscallname *name,
1138     abi_long arg0, abi_long arg1, abi_long arg2,
1139     abi_long arg3, abi_long arg4, abi_long arg5)
1140 {
1141     print_syscall_prologue(name);
1142     print_at_dirfd(arg0, 0);
1143     print_string(arg1, 0);
1144     print_flags(unlinkat_flags, arg2, 1);
1145     print_syscall_epilogue(name);
1146 }
1147 #endif
1148
1149 #ifdef TARGET_NR_utime
1150 static void
1151 print_utime(const struct syscallname *name,
1152     abi_long arg0, abi_long arg1, abi_long arg2,
1153     abi_long arg3, abi_long arg4, abi_long arg5)
1154 {
1155     print_syscall_prologue(name);
1156     print_string(arg0, 0);
1157     print_pointer(arg1, 1);
1158     print_syscall_epilogue(name);
1159 }
1160 #endif
1161
1162 #ifdef TARGET_NR_utimes
1163 static void
1164 print_utimes(const struct syscallname *name,
1165     abi_long arg0, abi_long arg1, abi_long arg2,
1166     abi_long arg3, abi_long arg4, abi_long arg5)
1167 {
1168     print_syscall_prologue(name);
1169     print_string(arg0, 0);
1170     print_pointer(arg1, 1);
1171     print_syscall_epilogue(name);
1172 }
1173 #endif
1174
1175 #ifdef TARGET_NR_utimensat
1176 static void
1177 print_utimensat(const struct syscallname *name,
1178     abi_long arg0, abi_long arg1, abi_long arg2,
1179     abi_long arg3, abi_long arg4, abi_long arg5)
1180 {
1181     print_syscall_prologue(name);
1182     print_at_dirfd(arg0, 0);
1183     print_string(arg1, 0);
1184     print_pointer(arg2, 0);
1185     print_flags(at_file_flags, arg3, 1);
1186     print_syscall_epilogue(name);
1187 }
1188 #endif
1189
1190 #ifdef TARGET_NR_mmap
1191 static void
1192 print_mmap(const struct syscallname *name,
1193     abi_long arg0, abi_long arg1, abi_long arg2,
1194     abi_long arg3, abi_long arg4, abi_long arg5)
1195 {
1196     print_syscall_prologue(name);
1197     print_pointer(arg0, 0);
1198     print_raw_param("%d", tswapl(arg1), 0);
1199     print_flags(mmap_prot_flags, arg2, 0);
1200     print_flags(mmap_flags, arg3, 0);
1201     print_raw_param("%d", tswapl(arg4), 0);
1202     print_raw_param("%#x", tswapl(arg5), 1);
1203     print_syscall_epilogue(name);
1204 }
1205 #define print_mmap2     print_mmap
1206 #endif
1207
1208 #ifdef TARGET_NR_mprotect
1209 static void
1210 print_mprotect(const struct syscallname *name,
1211     abi_long arg0, abi_long arg1, abi_long arg2,
1212     abi_long arg3, abi_long arg4, abi_long arg5)
1213 {
1214     print_syscall_prologue(name);
1215     print_pointer(arg0, 0);
1216     print_raw_param("%d", tswapl(arg1), 0);
1217     print_flags(mmap_prot_flags, arg2, 1);
1218     print_syscall_epilogue(name);
1219 }
1220 #endif
1221
1222 #ifdef TARGET_NR_munmap
1223 static void
1224 print_munmap(const struct syscallname *name,
1225     abi_long arg0, abi_long arg1, abi_long arg2,
1226     abi_long arg3, abi_long arg4, abi_long arg5)
1227 {
1228     print_syscall_prologue(name);
1229     print_pointer(arg0, 0);
1230     print_raw_param("%d", tswapl(arg1), 1);
1231     print_syscall_epilogue(name);
1232 }
1233 #endif
1234
1235 /*
1236  * An array of all of the syscalls we know about
1237  */
1238
1239 static const struct syscallname scnames[] = {
1240 #include "strace.list"
1241 };
1242
1243 static int nsyscalls = ARRAY_SIZE(scnames);
1244
1245 /*
1246  * The public interface to this module.
1247  */
1248 void
1249 print_syscall(int num,
1250               abi_long arg1, abi_long arg2, abi_long arg3,
1251               abi_long arg4, abi_long arg5, abi_long arg6)
1252 {
1253     int i;
1254     const char *format="%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ")";
1255
1256     gemu_log("%d ", getpid() );
1257
1258     for(i=0;i<nsyscalls;i++)
1259         if( scnames[i].nr == num ) {
1260             if( scnames[i].call != NULL ) {
1261                 scnames[i].call(&scnames[i],arg1,arg2,arg3,arg4,arg5,arg6);
1262             } else {
1263                 /* XXX: this format system is broken because it uses
1264                    host types and host pointers for strings */
1265                 if( scnames[i].format != NULL )
1266                     format = scnames[i].format;
1267                 gemu_log(format,scnames[i].name, arg1,arg2,arg3,arg4,arg5,arg6);
1268             }
1269             return;
1270         }
1271     gemu_log("Unknown syscall %d\n", num);
1272 }
1273
1274
1275 void
1276 print_syscall_ret(int num, abi_long ret)
1277 {
1278     int i;
1279
1280     for(i=0;i<nsyscalls;i++)
1281         if( scnames[i].nr == num ) {
1282             if( scnames[i].result != NULL ) {
1283                 scnames[i].result(&scnames[i],ret);
1284             } else {
1285                 if( ret < 0 ) {
1286                     gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret, target_strerror(-ret));
1287                 } else {
1288                     gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
1289                 }
1290             }
1291             break;
1292         }
1293 }