xref: /openbmc/qemu/linux-user/strace.c (revision 03feae73056ba3223151c31871860e30630645ac)
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 #ifdef AT_EACCESS
295     FLAG_GENERIC(AT_EACCESS),
296 #endif
297 #ifdef AT_SYMLINK_NOFOLLOW
298     FLAG_GENERIC(AT_SYMLINK_NOFOLLOW),
299 #endif
300     FLAG_END,
301 };
302 
303 UNUSED static struct flags unlinkat_flags[] = {
304 #ifdef AT_REMOVEDIR
305     FLAG_GENERIC(AT_REMOVEDIR),
306 #endif
307     FLAG_END,
308 };
309 
310 UNUSED static struct flags mode_flags[] = {
311     FLAG_GENERIC(S_IFSOCK),
312     FLAG_GENERIC(S_IFLNK),
313     FLAG_GENERIC(S_IFREG),
314     FLAG_GENERIC(S_IFBLK),
315     FLAG_GENERIC(S_IFDIR),
316     FLAG_GENERIC(S_IFCHR),
317     FLAG_GENERIC(S_IFIFO),
318     FLAG_END,
319 };
320 
321 UNUSED static struct flags open_access_flags[] = {
322     FLAG_TARGET(O_RDONLY),
323     FLAG_TARGET(O_WRONLY),
324     FLAG_TARGET(O_RDWR),
325     FLAG_END,
326 };
327 
328 UNUSED static struct flags open_flags[] = {
329     FLAG_TARGET(O_APPEND),
330     FLAG_TARGET(O_CREAT),
331     FLAG_TARGET(O_DIRECTORY),
332     FLAG_TARGET(O_EXCL),
333     FLAG_TARGET(O_LARGEFILE),
334     FLAG_TARGET(O_NOCTTY),
335     FLAG_TARGET(O_NOFOLLOW),
336     FLAG_TARGET(O_NONBLOCK),      /* also O_NDELAY */
337     FLAG_TARGET(O_SYNC),
338     FLAG_TARGET(O_TRUNC),
339 #ifdef O_DIRECT
340     FLAG_TARGET(O_DIRECT),
341 #endif
342     FLAG_END,
343 };
344 
345 UNUSED static struct flags mount_flags[] = {
346 #ifdef MS_BIND
347     FLAG_GENERIC(MS_BIND),
348 #endif
349 #ifdef MS_DIRSYNC
350     FLAG_GENERIC(MS_DIRSYNC),
351 #endif
352     FLAG_GENERIC(MS_MANDLOCK),
353 #ifdef MS_MOVE
354     FLAG_GENERIC(MS_MOVE),
355 #endif
356     FLAG_GENERIC(MS_NOATIME),
357     FLAG_GENERIC(MS_NODEV),
358     FLAG_GENERIC(MS_NODIRATIME),
359     FLAG_GENERIC(MS_NOEXEC),
360     FLAG_GENERIC(MS_NOSUID),
361     FLAG_GENERIC(MS_RDONLY),
362 #ifdef MS_RELATIME
363     FLAG_GENERIC(MS_RELATIME),
364 #endif
365     FLAG_GENERIC(MS_REMOUNT),
366     FLAG_GENERIC(MS_SYNCHRONOUS),
367     FLAG_END,
368 };
369 
370 UNUSED static struct flags umount2_flags[] = {
371 #ifdef MNT_FORCE
372     FLAG_GENERIC(MNT_FORCE),
373 #endif
374 #ifdef MNT_DETACH
375     FLAG_GENERIC(MNT_DETACH),
376 #endif
377 #ifdef MNT_EXPIRE
378     FLAG_GENERIC(MNT_EXPIRE),
379 #endif
380     FLAG_END,
381 };
382 
383 UNUSED static struct flags mmap_prot_flags[] = {
384     FLAG_GENERIC(PROT_NONE),
385     FLAG_GENERIC(PROT_EXEC),
386     FLAG_GENERIC(PROT_READ),
387     FLAG_GENERIC(PROT_WRITE),
388     FLAG_TARGET(PROT_SEM),
389     FLAG_GENERIC(PROT_GROWSDOWN),
390     FLAG_GENERIC(PROT_GROWSUP),
391     FLAG_END,
392 };
393 
394 UNUSED static struct flags mmap_flags[] = {
395     FLAG_TARGET(MAP_SHARED),
396     FLAG_TARGET(MAP_PRIVATE),
397     FLAG_TARGET(MAP_ANONYMOUS),
398     FLAG_TARGET(MAP_DENYWRITE),
399     FLAG_TARGET(MAP_FIXED),
400     FLAG_TARGET(MAP_GROWSDOWN),
401     FLAG_TARGET(MAP_EXECUTABLE),
402 #ifdef MAP_LOCKED
403     FLAG_TARGET(MAP_LOCKED),
404 #endif
405 #ifdef MAP_NONBLOCK
406     FLAG_TARGET(MAP_NONBLOCK),
407 #endif
408     FLAG_TARGET(MAP_NORESERVE),
409 #ifdef MAP_POPULATE
410     FLAG_TARGET(MAP_POPULATE),
411 #endif
412 #ifdef TARGET_MAP_UNINITIALIZED
413     FLAG_TARGET(MAP_UNINITIALIZED),
414 #endif
415     FLAG_END,
416 };
417 
418 UNUSED static struct flags fcntl_flags[] = {
419     FLAG_TARGET(F_DUPFD),
420     FLAG_TARGET(F_GETFD),
421     FLAG_TARGET(F_SETFD),
422     FLAG_TARGET(F_GETFL),
423     FLAG_TARGET(F_SETFL),
424     FLAG_TARGET(F_GETLK),
425     FLAG_TARGET(F_SETLK),
426     FLAG_TARGET(F_SETLKW),
427     FLAG_END,
428 };
429 
430 /*
431  * print_xxx utility functions.  These are used to print syscall
432  * parameters in certain format.  All of these have parameter
433  * named 'last'.  This parameter is used to add comma to output
434  * when last == 0.
435  */
436 
437 static const char *
438 get_comma(int last)
439 {
440     return ((last) ? "" : ",");
441 }
442 
443 static void
444 print_flags(const struct flags *f, abi_long flags, int last)
445 {
446     const char *sep = "";
447     int n;
448 
449     if ((flags == 0) && (f->f_value == 0)) {
450         gemu_log("%s%s", f->f_string, get_comma(last));
451         return;
452     }
453     for (n = 0; f->f_string != NULL; f++) {
454         if ((f->f_value != 0) && ((flags & f->f_value) == f->f_value)) {
455             gemu_log("%s%s", sep, f->f_string);
456             flags &= ~f->f_value;
457             sep = "|";
458             n++;
459         }
460     }
461 
462     if (n > 0) {
463         /* print rest of the flags as numeric */
464         if (flags != 0) {
465             gemu_log("%s%#x%s", sep, (unsigned int)flags, get_comma(last));
466         } else {
467             gemu_log("%s", get_comma(last));
468         }
469     } else {
470         /* no string version of flags found, print them in hex then */
471         gemu_log("%#x%s", (unsigned int)flags, get_comma(last));
472     }
473 }
474 
475 static void
476 print_at_dirfd(abi_long dirfd, int last)
477 {
478 #ifdef AT_FDCWD
479     if (dirfd == AT_FDCWD) {
480         gemu_log("AT_FDCWD%s", get_comma(last));
481         return;
482     }
483 #endif
484     gemu_log("%d%s", (int)dirfd, get_comma(last));
485 }
486 
487 static void
488 print_file_mode(abi_long mode, int last)
489 {
490     const char *sep = "";
491     const struct flags *m;
492 
493     for (m = &mode_flags[0]; m->f_string != NULL; m++) {
494         if ((m->f_value & mode) == m->f_value) {
495             gemu_log("%s%s", m->f_string, sep);
496             sep = "|";
497             mode &= ~m->f_value;
498             break;
499         }
500     }
501 
502     mode &= ~S_IFMT;
503     /* print rest of the mode as octal */
504     if (mode != 0)
505         gemu_log("%s%#o", sep, (unsigned int)mode);
506 
507     gemu_log("%s", get_comma(last));
508 }
509 
510 static void
511 print_open_flags(abi_long flags, int last)
512 {
513     print_flags(open_access_flags, flags & TARGET_O_ACCMODE, 1);
514     flags &= ~TARGET_O_ACCMODE;
515     if (flags == 0) {
516         gemu_log("%s", get_comma(last));
517         return;
518     }
519     gemu_log("|");
520     print_flags(open_flags, flags, last);
521 }
522 
523 static void
524 print_syscall_prologue(const struct syscallname *sc)
525 {
526     gemu_log("%s(", sc->name);
527 }
528 
529 /*ARGSUSED*/
530 static void
531 print_syscall_epilogue(const struct syscallname *sc)
532 {
533     (void)sc;
534     gemu_log(")");
535 }
536 
537 static void
538 print_string(abi_long addr, int last)
539 {
540     char *s;
541 
542     if ((s = lock_user_string(addr)) != NULL) {
543         gemu_log("\"%s\"%s", s, get_comma(last));
544         unlock_user(s, addr, 0);
545     } else {
546         /* can't get string out of it, so print it as pointer */
547         print_pointer(addr, last);
548     }
549 }
550 
551 /*
552  * Prints out raw parameter using given format.  Caller needs
553  * to do byte swapping if needed.
554  */
555 static void
556 print_raw_param(const char *fmt, abi_long param, int last)
557 {
558     char format[64];
559 
560     (void) snprintf(format, sizeof (format), "%s%s", fmt, get_comma(last));
561     gemu_log(format, param);
562 }
563 
564 static void
565 print_pointer(abi_long p, int last)
566 {
567     if (p == 0)
568         gemu_log("NULL%s", get_comma(last));
569     else
570         gemu_log("0x" TARGET_ABI_FMT_lx "%s", p, get_comma(last));
571 }
572 
573 /*
574  * Reads 32-bit (int) number from guest address space from
575  * address 'addr' and prints it.
576  */
577 static void
578 print_number(abi_long addr, int last)
579 {
580     if (addr == 0) {
581         gemu_log("NULL%s", get_comma(last));
582     } else {
583         int num;
584 
585         get_user_s32(num, addr);
586         gemu_log("[%d]%s", num, get_comma(last));
587     }
588 }
589 
590 static void
591 print_timeval(abi_ulong tv_addr, int last)
592 {
593     if( tv_addr ) {
594         struct target_timeval *tv;
595 
596         tv = lock_user(VERIFY_READ, tv_addr, sizeof(*tv), 1);
597         if (!tv)
598             return;
599         gemu_log("{" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "}%s",
600             tv->tv_sec, tv->tv_usec, get_comma(last));
601         unlock_user(tv, tv_addr, 0);
602     } else
603         gemu_log("NULL%s", get_comma(last));
604 }
605 
606 #undef UNUSED
607 
608 #ifdef TARGET_NR_accept
609 static void
610 print_accept(const struct syscallname *name,
611     abi_long arg0, abi_long arg1, abi_long arg2,
612     abi_long arg3, abi_long arg4, abi_long arg5)
613 {
614     print_syscall_prologue(name);
615     print_raw_param("%d", arg0, 0);
616     print_pointer(arg1, 0);
617     print_number(arg2, 1);
618     print_syscall_epilogue(name);
619 }
620 #endif
621 
622 #ifdef TARGET_NR_access
623 static void
624 print_access(const struct syscallname *name,
625     abi_long arg0, abi_long arg1, abi_long arg2,
626     abi_long arg3, abi_long arg4, abi_long arg5)
627 {
628     print_syscall_prologue(name);
629     print_string(arg0, 0);
630     print_flags(access_flags, arg1, 1);
631     print_syscall_epilogue(name);
632 }
633 #endif
634 
635 #ifdef TARGET_NR_brk
636 static void
637 print_brk(const struct syscallname *name,
638     abi_long arg0, abi_long arg1, abi_long arg2,
639     abi_long arg3, abi_long arg4, abi_long arg5)
640 {
641     print_syscall_prologue(name);
642     print_pointer(arg0, 1);
643     print_syscall_epilogue(name);
644 }
645 #endif
646 
647 #ifdef TARGET_NR_chdir
648 static void
649 print_chdir(const struct syscallname *name,
650     abi_long arg0, abi_long arg1, abi_long arg2,
651     abi_long arg3, abi_long arg4, abi_long arg5)
652 {
653     print_syscall_prologue(name);
654     print_string(arg0, 1);
655     print_syscall_epilogue(name);
656 }
657 #endif
658 
659 #ifdef TARGET_NR_chmod
660 static void
661 print_chmod(const struct syscallname *name,
662     abi_long arg0, abi_long arg1, abi_long arg2,
663     abi_long arg3, abi_long arg4, abi_long arg5)
664 {
665     print_syscall_prologue(name);
666     print_string(arg0, 0);
667     print_file_mode(arg1, 1);
668     print_syscall_epilogue(name);
669 }
670 #endif
671 
672 #ifdef TARGET_NR_creat
673 static void
674 print_creat(const struct syscallname *name,
675     abi_long arg0, abi_long arg1, abi_long arg2,
676     abi_long arg3, abi_long arg4, abi_long arg5)
677 {
678     print_syscall_prologue(name);
679     print_string(arg0, 0);
680     print_file_mode(arg1, 1);
681     print_syscall_epilogue(name);
682 }
683 #endif
684 
685 #ifdef TARGET_NR_execv
686 static void
687 print_execv(const struct syscallname *name,
688     abi_long arg0, abi_long arg1, abi_long arg2,
689     abi_long arg3, abi_long arg4, abi_long arg5)
690 {
691     print_syscall_prologue(name);
692     print_string(arg0, 0);
693     print_raw_param("0x" TARGET_ABI_FMT_lx, arg1, 1);
694     print_syscall_epilogue(name);
695 }
696 #endif
697 
698 #ifdef TARGET_NR_faccessat
699 static void
700 print_faccessat(const struct syscallname *name,
701     abi_long arg0, abi_long arg1, abi_long arg2,
702     abi_long arg3, abi_long arg4, abi_long arg5)
703 {
704     print_syscall_prologue(name);
705     print_at_dirfd(arg0, 0);
706     print_string(arg1, 0);
707     print_flags(access_flags, arg2, 0);
708     print_flags(at_file_flags, arg3, 1);
709     print_syscall_epilogue(name);
710 }
711 #endif
712 
713 #ifdef TARGET_NR_fchmodat
714 static void
715 print_fchmodat(const struct syscallname *name,
716     abi_long arg0, abi_long arg1, abi_long arg2,
717     abi_long arg3, abi_long arg4, abi_long arg5)
718 {
719     print_syscall_prologue(name);
720     print_at_dirfd(arg0, 0);
721     print_string(arg1, 0);
722     print_file_mode(arg2, 0);
723     print_flags(at_file_flags, arg3, 1);
724     print_syscall_epilogue(name);
725 }
726 #endif
727 
728 #ifdef TARGET_NR_fchownat
729 static void
730 print_fchownat(const struct syscallname *name,
731     abi_long arg0, abi_long arg1, abi_long arg2,
732     abi_long arg3, abi_long arg4, abi_long arg5)
733 {
734     print_syscall_prologue(name);
735     print_at_dirfd(arg0, 0);
736     print_string(arg1, 0);
737     print_raw_param("%d", arg2, 0);
738     print_raw_param("%d", arg3, 0);
739     print_flags(at_file_flags, arg4, 1);
740     print_syscall_epilogue(name);
741 }
742 #endif
743 
744 #if defined(TARGET_NR_fcntl) || defined(TARGET_NR_fcntl64)
745 static void
746 print_fcntl(const struct syscallname *name,
747     abi_long arg0, abi_long arg1, abi_long arg2,
748     abi_long arg3, abi_long arg4, abi_long arg5)
749 {
750     print_syscall_prologue(name);
751     print_raw_param("%d", arg0, 0);
752     print_flags(fcntl_flags, arg1, 0);
753     /*
754      * TODO: check flags and print following argument only
755      *       when needed.
756      */
757     print_pointer(arg2, 1);
758     print_syscall_epilogue(name);
759 }
760 #define print_fcntl64   print_fcntl
761 #endif
762 
763 
764 #ifdef TARGET_NR_futimesat
765 static void
766 print_futimesat(const struct syscallname *name,
767     abi_long arg0, abi_long arg1, abi_long arg2,
768     abi_long arg3, abi_long arg4, abi_long arg5)
769 {
770     print_syscall_prologue(name);
771     print_at_dirfd(arg0, 0);
772     print_string(arg1, 0);
773     print_timeval(arg2, 0);
774     print_timeval(arg2 + sizeof (struct target_timeval), 1);
775     print_syscall_epilogue(name);
776 }
777 #endif
778 
779 #ifdef TARGET_NR_link
780 static void
781 print_link(const struct syscallname *name,
782     abi_long arg0, abi_long arg1, abi_long arg2,
783     abi_long arg3, abi_long arg4, abi_long arg5)
784 {
785     print_syscall_prologue(name);
786     print_string(arg0, 0);
787     print_string(arg1, 1);
788     print_syscall_epilogue(name);
789 }
790 #endif
791 
792 #ifdef TARGET_NR_linkat
793 static void
794 print_linkat(const struct syscallname *name,
795     abi_long arg0, abi_long arg1, abi_long arg2,
796     abi_long arg3, abi_long arg4, abi_long arg5)
797 {
798     print_syscall_prologue(name);
799     print_at_dirfd(arg0, 0);
800     print_string(arg1, 0);
801     print_at_dirfd(arg2, 0);
802     print_string(arg3, 0);
803     print_flags(at_file_flags, arg4, 1);
804     print_syscall_epilogue(name);
805 }
806 #endif
807 
808 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) || \
809     defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64)
810 static void
811 print_stat(const struct syscallname *name,
812     abi_long arg0, abi_long arg1, abi_long arg2,
813     abi_long arg3, abi_long arg4, abi_long arg5)
814 {
815     print_syscall_prologue(name);
816     print_string(arg0, 0);
817     print_pointer(arg1, 1);
818     print_syscall_epilogue(name);
819 }
820 #define print_lstat     print_stat
821 #define print_stat64	print_stat
822 #define print_lstat64   print_stat
823 #endif
824 
825 #if defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64)
826 static void
827 print_fstat(const struct syscallname *name,
828     abi_long arg0, abi_long arg1, abi_long arg2,
829     abi_long arg3, abi_long arg4, abi_long arg5)
830 {
831     print_syscall_prologue(name);
832     print_raw_param("%d", arg0, 0);
833     print_pointer(arg1, 1);
834     print_syscall_epilogue(name);
835 }
836 #define print_fstat64     print_fstat
837 #endif
838 
839 #ifdef TARGET_NR_mkdir
840 static void
841 print_mkdir(const struct syscallname *name,
842     abi_long arg0, abi_long arg1, abi_long arg2,
843     abi_long arg3, abi_long arg4, abi_long arg5)
844 {
845     print_syscall_prologue(name);
846     print_string(arg0, 0);
847     print_file_mode(arg1, 1);
848     print_syscall_epilogue(name);
849 }
850 #endif
851 
852 #ifdef TARGET_NR_mkdirat
853 static void
854 print_mkdirat(const struct syscallname *name,
855     abi_long arg0, abi_long arg1, abi_long arg2,
856     abi_long arg3, abi_long arg4, abi_long arg5)
857 {
858     print_syscall_prologue(name);
859     print_at_dirfd(arg0, 0);
860     print_string(arg1, 0);
861     print_file_mode(arg2, 1);
862     print_syscall_epilogue(name);
863 }
864 #endif
865 
866 #ifdef TARGET_NR_rmdir
867 static void
868 print_rmdir(const struct syscallname *name,
869     abi_long arg0, abi_long arg1, abi_long arg2,
870     abi_long arg3, abi_long arg4, abi_long arg5)
871 {
872     print_syscall_prologue(name);
873     print_string(arg0, 0);
874     print_syscall_epilogue(name);
875 }
876 #endif
877 
878 #ifdef TARGET_NR_mknod
879 static void
880 print_mknod(const struct syscallname *name,
881     abi_long arg0, abi_long arg1, abi_long arg2,
882     abi_long arg3, abi_long arg4, abi_long arg5)
883 {
884     int hasdev = (arg1 & (S_IFCHR|S_IFBLK));
885 
886     print_syscall_prologue(name);
887     print_string(arg0, 0);
888     print_file_mode(arg1, (hasdev == 0));
889     if (hasdev) {
890         print_raw_param("makedev(%d", major(arg2), 0);
891         print_raw_param("%d)", minor(arg2), 1);
892     }
893     print_syscall_epilogue(name);
894 }
895 #endif
896 
897 #ifdef TARGET_NR_mknodat
898 static void
899 print_mknodat(const struct syscallname *name,
900     abi_long arg0, abi_long arg1, abi_long arg2,
901     abi_long arg3, abi_long arg4, abi_long arg5)
902 {
903     int hasdev = (arg2 & (S_IFCHR|S_IFBLK));
904 
905     print_syscall_prologue(name);
906     print_at_dirfd(arg0, 0);
907     print_string(arg1, 0);
908     print_file_mode(arg2, (hasdev == 0));
909     if (hasdev) {
910         print_raw_param("makedev(%d", major(arg3), 0);
911         print_raw_param("%d)", minor(arg3), 1);
912     }
913     print_syscall_epilogue(name);
914 }
915 #endif
916 
917 #ifdef TARGET_NR_mq_open
918 static void
919 print_mq_open(const struct syscallname *name,
920     abi_long arg0, abi_long arg1, abi_long arg2,
921     abi_long arg3, abi_long arg4, abi_long arg5)
922 {
923     int is_creat = (arg1 & TARGET_O_CREAT);
924 
925     print_syscall_prologue(name);
926     print_string(arg0, 0);
927     print_open_flags(arg1, (is_creat == 0));
928     if (is_creat) {
929         print_file_mode(arg2, 0);
930         print_pointer(arg3, 1);
931     }
932     print_syscall_epilogue(name);
933 }
934 #endif
935 
936 #ifdef TARGET_NR_open
937 static void
938 print_open(const struct syscallname *name,
939     abi_long arg0, abi_long arg1, abi_long arg2,
940     abi_long arg3, abi_long arg4, abi_long arg5)
941 {
942     int is_creat = (arg1 & TARGET_O_CREAT);
943 
944     print_syscall_prologue(name);
945     print_string(arg0, 0);
946     print_open_flags(arg1, (is_creat == 0));
947     if (is_creat)
948         print_file_mode(arg2, 1);
949     print_syscall_epilogue(name);
950 }
951 #endif
952 
953 #ifdef TARGET_NR_openat
954 static void
955 print_openat(const struct syscallname *name,
956     abi_long arg0, abi_long arg1, abi_long arg2,
957     abi_long arg3, abi_long arg4, abi_long arg5)
958 {
959     int is_creat = (arg2 & TARGET_O_CREAT);
960 
961     print_syscall_prologue(name);
962     print_at_dirfd(arg0, 0);
963     print_string(arg1, 0);
964     print_open_flags(arg2, (is_creat == 0));
965     if (is_creat)
966         print_file_mode(arg3, 1);
967     print_syscall_epilogue(name);
968 }
969 #endif
970 
971 #ifdef TARGET_NR_mq_unlink
972 static void
973 print_mq_unlink(const struct syscallname *name,
974     abi_long arg0, abi_long arg1, abi_long arg2,
975     abi_long arg3, abi_long arg4, abi_long arg5)
976 {
977     print_syscall_prologue(name);
978     print_string(arg0, 1);
979     print_syscall_epilogue(name);
980 }
981 #endif
982 
983 #if defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)
984 static void
985 print_fstatat64(const struct syscallname *name,
986     abi_long arg0, abi_long arg1, abi_long arg2,
987     abi_long arg3, abi_long arg4, abi_long arg5)
988 {
989     print_syscall_prologue(name);
990     print_at_dirfd(arg0, 0);
991     print_string(arg1, 0);
992     print_pointer(arg2, 0);
993     print_flags(at_file_flags, arg3, 1);
994     print_syscall_epilogue(name);
995 }
996 #define print_newfstatat    print_fstatat64
997 #endif
998 
999 #ifdef TARGET_NR_readlink
1000 static void
1001 print_readlink(const struct syscallname *name,
1002     abi_long arg0, abi_long arg1, abi_long arg2,
1003     abi_long arg3, abi_long arg4, abi_long arg5)
1004 {
1005     print_syscall_prologue(name);
1006     print_string(arg0, 0);
1007     print_pointer(arg1, 0);
1008     print_raw_param("%u", arg2, 1);
1009     print_syscall_epilogue(name);
1010 }
1011 #endif
1012 
1013 #ifdef TARGET_NR_readlinkat
1014 static void
1015 print_readlinkat(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_at_dirfd(arg0, 0);
1021     print_string(arg1, 0);
1022     print_pointer(arg2, 0);
1023     print_raw_param("%u", arg3, 1);
1024     print_syscall_epilogue(name);
1025 }
1026 #endif
1027 
1028 #ifdef TARGET_NR_rename
1029 static void
1030 print_rename(const struct syscallname *name,
1031     abi_long arg0, abi_long arg1, abi_long arg2,
1032     abi_long arg3, abi_long arg4, abi_long arg5)
1033 {
1034     print_syscall_prologue(name);
1035     print_string(arg0, 0);
1036     print_string(arg1, 1);
1037     print_syscall_epilogue(name);
1038 }
1039 #endif
1040 
1041 #ifdef TARGET_NR_renameat
1042 static void
1043 print_renameat(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_at_dirfd(arg0, 0);
1049     print_string(arg1, 0);
1050     print_at_dirfd(arg2, 0);
1051     print_string(arg3, 1);
1052     print_syscall_epilogue(name);
1053 }
1054 #endif
1055 
1056 #ifdef TARGET_NR_statfs
1057 static void
1058 print_statfs(const struct syscallname *name,
1059     abi_long arg0, abi_long arg1, abi_long arg2,
1060     abi_long arg3, abi_long arg4, abi_long arg5)
1061 {
1062     print_syscall_prologue(name);
1063     print_string(arg0, 0);
1064     print_pointer(arg1, 1);
1065     print_syscall_epilogue(name);
1066 }
1067 #define print_statfs64  print_statfs
1068 #endif
1069 
1070 #ifdef TARGET_NR_symlink
1071 static void
1072 print_symlink(const struct syscallname *name,
1073     abi_long arg0, abi_long arg1, abi_long arg2,
1074     abi_long arg3, abi_long arg4, abi_long arg5)
1075 {
1076     print_syscall_prologue(name);
1077     print_string(arg0, 0);
1078     print_string(arg1, 1);
1079     print_syscall_epilogue(name);
1080 }
1081 #endif
1082 
1083 #ifdef TARGET_NR_symlinkat
1084 static void
1085 print_symlinkat(const struct syscallname *name,
1086     abi_long arg0, abi_long arg1, abi_long arg2,
1087     abi_long arg3, abi_long arg4, abi_long arg5)
1088 {
1089     print_syscall_prologue(name);
1090     print_string(arg0, 0);
1091     print_at_dirfd(arg1, 0);
1092     print_string(arg2, 1);
1093     print_syscall_epilogue(name);
1094 }
1095 #endif
1096 
1097 #ifdef TARGET_NR_mount
1098 static void
1099 print_mount(const struct syscallname *name,
1100     abi_long arg0, abi_long arg1, abi_long arg2,
1101     abi_long arg3, abi_long arg4, abi_long arg5)
1102 {
1103     print_syscall_prologue(name);
1104     print_string(arg0, 0);
1105     print_string(arg1, 0);
1106     print_string(arg2, 0);
1107     print_flags(mount_flags, arg3, 0);
1108     print_pointer(arg4, 1);
1109     print_syscall_epilogue(name);
1110 }
1111 #endif
1112 
1113 #ifdef TARGET_NR_umount
1114 static void
1115 print_umount(const struct syscallname *name,
1116     abi_long arg0, abi_long arg1, abi_long arg2,
1117     abi_long arg3, abi_long arg4, abi_long arg5)
1118 {
1119     print_syscall_prologue(name);
1120     print_string(arg0, 1);
1121     print_syscall_epilogue(name);
1122 }
1123 #endif
1124 
1125 #ifdef TARGET_NR_umount2
1126 static void
1127 print_umount2(const struct syscallname *name,
1128     abi_long arg0, abi_long arg1, abi_long arg2,
1129     abi_long arg3, abi_long arg4, abi_long arg5)
1130 {
1131     print_syscall_prologue(name);
1132     print_string(arg0, 0);
1133     print_flags(umount2_flags, arg1, 1);
1134     print_syscall_epilogue(name);
1135 }
1136 #endif
1137 
1138 #ifdef TARGET_NR_unlink
1139 static void
1140 print_unlink(const struct syscallname *name,
1141     abi_long arg0, abi_long arg1, abi_long arg2,
1142     abi_long arg3, abi_long arg4, abi_long arg5)
1143 {
1144     print_syscall_prologue(name);
1145     print_string(arg0, 1);
1146     print_syscall_epilogue(name);
1147 }
1148 #endif
1149 
1150 #ifdef TARGET_NR_unlinkat
1151 static void
1152 print_unlinkat(const struct syscallname *name,
1153     abi_long arg0, abi_long arg1, abi_long arg2,
1154     abi_long arg3, abi_long arg4, abi_long arg5)
1155 {
1156     print_syscall_prologue(name);
1157     print_at_dirfd(arg0, 0);
1158     print_string(arg1, 0);
1159     print_flags(unlinkat_flags, arg2, 1);
1160     print_syscall_epilogue(name);
1161 }
1162 #endif
1163 
1164 #ifdef TARGET_NR_utime
1165 static void
1166 print_utime(const struct syscallname *name,
1167     abi_long arg0, abi_long arg1, abi_long arg2,
1168     abi_long arg3, abi_long arg4, abi_long arg5)
1169 {
1170     print_syscall_prologue(name);
1171     print_string(arg0, 0);
1172     print_pointer(arg1, 1);
1173     print_syscall_epilogue(name);
1174 }
1175 #endif
1176 
1177 #ifdef TARGET_NR_utimes
1178 static void
1179 print_utimes(const struct syscallname *name,
1180     abi_long arg0, abi_long arg1, abi_long arg2,
1181     abi_long arg3, abi_long arg4, abi_long arg5)
1182 {
1183     print_syscall_prologue(name);
1184     print_string(arg0, 0);
1185     print_pointer(arg1, 1);
1186     print_syscall_epilogue(name);
1187 }
1188 #endif
1189 
1190 #ifdef TARGET_NR_utimensat
1191 static void
1192 print_utimensat(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_at_dirfd(arg0, 0);
1198     print_string(arg1, 0);
1199     print_pointer(arg2, 0);
1200     print_flags(at_file_flags, arg3, 1);
1201     print_syscall_epilogue(name);
1202 }
1203 #endif
1204 
1205 #if defined(TARGET_NR_mmap) || defined(TARGET_NR_mmap2)
1206 static void
1207 print_mmap(const struct syscallname *name,
1208     abi_long arg0, abi_long arg1, abi_long arg2,
1209     abi_long arg3, abi_long arg4, abi_long arg5)
1210 {
1211     print_syscall_prologue(name);
1212     print_pointer(arg0, 0);
1213     print_raw_param("%d", arg1, 0);
1214     print_flags(mmap_prot_flags, arg2, 0);
1215     print_flags(mmap_flags, arg3, 0);
1216     print_raw_param("%d", arg4, 0);
1217     print_raw_param("%#x", arg5, 1);
1218     print_syscall_epilogue(name);
1219 }
1220 #define print_mmap2     print_mmap
1221 #endif
1222 
1223 #ifdef TARGET_NR_mprotect
1224 static void
1225 print_mprotect(const struct syscallname *name,
1226     abi_long arg0, abi_long arg1, abi_long arg2,
1227     abi_long arg3, abi_long arg4, abi_long arg5)
1228 {
1229     print_syscall_prologue(name);
1230     print_pointer(arg0, 0);
1231     print_raw_param("%d", arg1, 0);
1232     print_flags(mmap_prot_flags, arg2, 1);
1233     print_syscall_epilogue(name);
1234 }
1235 #endif
1236 
1237 #ifdef TARGET_NR_munmap
1238 static void
1239 print_munmap(const struct syscallname *name,
1240     abi_long arg0, abi_long arg1, abi_long arg2,
1241     abi_long arg3, abi_long arg4, abi_long arg5)
1242 {
1243     print_syscall_prologue(name);
1244     print_pointer(arg0, 0);
1245     print_raw_param("%d", arg1, 1);
1246     print_syscall_epilogue(name);
1247 }
1248 #endif
1249 
1250 #ifdef TARGET_NR_futex
1251 static void print_futex_op(abi_long tflag, int last)
1252 {
1253 #define print_op(val) \
1254 if( cmd == val ) { \
1255     gemu_log(#val); \
1256     return; \
1257 }
1258 
1259     int cmd = (int)tflag;
1260 #ifdef FUTEX_PRIVATE_FLAG
1261     if (cmd & FUTEX_PRIVATE_FLAG) {
1262         gemu_log("FUTEX_PRIVATE_FLAG|");
1263         cmd &= ~FUTEX_PRIVATE_FLAG;
1264     }
1265 #endif
1266     print_op(FUTEX_WAIT)
1267     print_op(FUTEX_WAKE)
1268     print_op(FUTEX_FD)
1269     print_op(FUTEX_REQUEUE)
1270     print_op(FUTEX_CMP_REQUEUE)
1271     print_op(FUTEX_WAKE_OP)
1272     print_op(FUTEX_LOCK_PI)
1273     print_op(FUTEX_UNLOCK_PI)
1274     print_op(FUTEX_TRYLOCK_PI)
1275 #ifdef FUTEX_WAIT_BITSET
1276     print_op(FUTEX_WAIT_BITSET)
1277 #endif
1278 #ifdef FUTEX_WAKE_BITSET
1279     print_op(FUTEX_WAKE_BITSET)
1280 #endif
1281     /* unknown values */
1282     gemu_log("%d",cmd);
1283 }
1284 
1285 static void
1286 print_futex(const struct syscallname *name,
1287     abi_long arg0, abi_long arg1, abi_long arg2,
1288     abi_long arg3, abi_long arg4, abi_long arg5)
1289 {
1290     print_syscall_prologue(name);
1291     print_pointer(arg0, 0);
1292     print_futex_op(arg1, 0);
1293     print_raw_param(",%d", arg2, 0);
1294     print_pointer(arg3, 0); /* struct timespec */
1295     print_pointer(arg4, 0);
1296     print_raw_param("%d", arg4, 1);
1297     print_syscall_epilogue(name);
1298 }
1299 #endif
1300 
1301 /*
1302  * An array of all of the syscalls we know about
1303  */
1304 
1305 static const struct syscallname scnames[] = {
1306 #include "strace.list"
1307 };
1308 
1309 static int nsyscalls = ARRAY_SIZE(scnames);
1310 
1311 /*
1312  * The public interface to this module.
1313  */
1314 void
1315 print_syscall(int num,
1316               abi_long arg1, abi_long arg2, abi_long arg3,
1317               abi_long arg4, abi_long arg5, abi_long arg6)
1318 {
1319     int i;
1320     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 ")";
1321 
1322     gemu_log("%d ", getpid() );
1323 
1324     for(i=0;i<nsyscalls;i++)
1325         if( scnames[i].nr == num ) {
1326             if( scnames[i].call != NULL ) {
1327                 scnames[i].call(&scnames[i],arg1,arg2,arg3,arg4,arg5,arg6);
1328             } else {
1329                 /* XXX: this format system is broken because it uses
1330                    host types and host pointers for strings */
1331                 if( scnames[i].format != NULL )
1332                     format = scnames[i].format;
1333                 gemu_log(format,scnames[i].name, arg1,arg2,arg3,arg4,arg5,arg6);
1334             }
1335             return;
1336         }
1337     gemu_log("Unknown syscall %d\n", num);
1338 }
1339 
1340 
1341 void
1342 print_syscall_ret(int num, abi_long ret)
1343 {
1344     int i;
1345 
1346     for(i=0;i<nsyscalls;i++)
1347         if( scnames[i].nr == num ) {
1348             if( scnames[i].result != NULL ) {
1349                 scnames[i].result(&scnames[i],ret);
1350             } else {
1351                 if( ret < 0 ) {
1352                     gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret, target_strerror(-ret));
1353                 } else {
1354                     gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
1355                 }
1356             }
1357             break;
1358         }
1359 }
1360