raw-posix: Remove O_RDWR when attempting to open a file read-only
[qemu-kvm/amd-iommu.git] / linux-user / strace.c
blob5ea9acb082e0dd80a67854460a465d543bf08649
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 <linux/futex.h>
12 #include <unistd.h>
13 #include "qemu.h"
15 int do_strace=0;
17 struct syscallname {
18 int nr;
19 const char *name;
20 const char *format;
21 void (*call)(const struct syscallname *,
22 abi_long, abi_long, abi_long,
23 abi_long, abi_long, abi_long);
24 void (*result)(const struct syscallname *, abi_long);
27 #ifdef __GNUC__
29 * It is possible that target doesn't have syscall that uses
30 * following flags but we don't want the compiler to warn
31 * us about them being unused. Same applies to utility print
32 * functions. It is ok to keep them while not used.
34 #define UNUSED __attribute__ ((unused))
35 #else
36 #define UNUSED
37 #endif
40 * Structure used to translate flag values into strings. This is
41 * similar that is in the actual strace tool.
43 struct flags {
44 abi_long f_value; /* flag */
45 const char *f_string; /* stringified flag */
48 /* common flags for all architectures */
49 #define FLAG_GENERIC(name) { name, #name }
50 /* target specific flags (syscall_defs.h has TARGET_<flag>) */
51 #define FLAG_TARGET(name) { TARGET_ ## name, #name }
52 /* end of flags array */
53 #define FLAG_END { 0, NULL }
55 UNUSED static const char *get_comma(int);
56 UNUSED static void print_pointer(abi_long, int);
57 UNUSED static void print_flags(const struct flags *, abi_long, int);
58 UNUSED static void print_at_dirfd(abi_long, int);
59 UNUSED static void print_file_mode(abi_long, int);
60 UNUSED static void print_open_flags(abi_long, int);
61 UNUSED static void print_syscall_prologue(const struct syscallname *);
62 UNUSED static void print_syscall_epilogue(const struct syscallname *);
63 UNUSED static void print_string(abi_long, int);
64 UNUSED static void print_raw_param(const char *, abi_long, int);
65 UNUSED static void print_timeval(abi_ulong, int);
66 UNUSED static void print_number(abi_long, int);
69 * Utility functions
71 static void
72 print_ipc_cmd(int cmd)
74 #define output_cmd(val) \
75 if( cmd == val ) { \
76 gemu_log(#val); \
77 return; \
80 cmd &= 0xff;
82 /* General IPC commands */
83 output_cmd( IPC_RMID );
84 output_cmd( IPC_SET );
85 output_cmd( IPC_STAT );
86 output_cmd( IPC_INFO );
87 /* msgctl() commands */
88 #ifdef __USER_MISC
89 output_cmd( MSG_STAT );
90 output_cmd( MSG_INFO );
91 #endif
92 /* shmctl() commands */
93 output_cmd( SHM_LOCK );
94 output_cmd( SHM_UNLOCK );
95 output_cmd( SHM_STAT );
96 output_cmd( SHM_INFO );
97 /* semctl() commands */
98 output_cmd( GETPID );
99 output_cmd( GETVAL );
100 output_cmd( GETALL );
101 output_cmd( GETNCNT );
102 output_cmd( GETZCNT );
103 output_cmd( SETVAL );
104 output_cmd( SETALL );
105 output_cmd( SEM_STAT );
106 output_cmd( SEM_INFO );
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 output_cmd( IPC_RMID );
117 /* Some value we don't recognize */
118 gemu_log("%d",cmd);
121 #ifdef TARGET_NR__newselect
122 static void
123 print_fdset(int n, abi_ulong target_fds_addr)
125 int i;
127 gemu_log("[");
128 if( target_fds_addr ) {
129 abi_long *target_fds;
131 target_fds = lock_user(VERIFY_READ,
132 target_fds_addr,
133 sizeof(*target_fds)*(n / TARGET_ABI_BITS + 1),
136 if (!target_fds)
137 return;
139 for (i=n; i>=0; i--) {
140 if ((tswapl(target_fds[i / TARGET_ABI_BITS]) >> (i & (TARGET_ABI_BITS - 1))) & 1)
141 gemu_log("%d,", i );
143 unlock_user(target_fds, target_fds_addr, 0);
145 gemu_log("]");
147 #endif
150 * Sysycall specific output functions
153 /* select */
154 #ifdef TARGET_NR__newselect
155 static long newselect_arg1 = 0;
156 static long newselect_arg2 = 0;
157 static long newselect_arg3 = 0;
158 static long newselect_arg4 = 0;
159 static long newselect_arg5 = 0;
161 static void
162 print_newselect(const struct syscallname *name,
163 abi_long arg1, abi_long arg2, abi_long arg3,
164 abi_long arg4, abi_long arg5, abi_long arg6)
166 gemu_log("%s(" TARGET_ABI_FMT_ld ",", name->name, arg1);
167 print_fdset(arg1, arg2);
168 gemu_log(",");
169 print_fdset(arg1, arg3);
170 gemu_log(",");
171 print_fdset(arg1, arg4);
172 gemu_log(",");
173 print_timeval(arg5, 1);
174 gemu_log(")");
176 /* save for use in the return output function below */
177 newselect_arg1=arg1;
178 newselect_arg2=arg2;
179 newselect_arg3=arg3;
180 newselect_arg4=arg4;
181 newselect_arg5=arg5;
183 #endif
185 #ifdef TARGET_NR_semctl
186 static void
187 print_semctl(const struct syscallname *name,
188 abi_long arg1, abi_long arg2, abi_long arg3,
189 abi_long arg4, abi_long arg5, abi_long arg6)
191 gemu_log("%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ",", name->name, arg1, arg2);
192 print_ipc_cmd(arg3);
193 gemu_log(",0x" TARGET_ABI_FMT_lx ")", arg4);
195 #endif
197 static void
198 print_execve(const struct syscallname *name,
199 abi_long arg1, abi_long arg2, abi_long arg3,
200 abi_long arg4, abi_long arg5, abi_long arg6)
202 abi_ulong arg_ptr_addr;
203 char *s;
205 if (!(s = lock_user_string(arg1)))
206 return;
207 gemu_log("%s(\"%s\",{", name->name, s);
208 unlock_user(s, arg1, 0);
210 for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) {
211 abi_ulong *arg_ptr, arg_addr;
213 arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1);
214 if (!arg_ptr)
215 return;
216 arg_addr = tswapl(*arg_ptr);
217 unlock_user(arg_ptr, arg_ptr_addr, 0);
218 if (!arg_addr)
219 break;
220 if ((s = lock_user_string(arg_addr))) {
221 gemu_log("\"%s\",", s);
222 unlock_user(s, arg_addr, 0);
226 gemu_log("NULL})");
229 #ifdef TARGET_NR_ipc
230 static void
231 print_ipc(const struct syscallname *name,
232 abi_long arg1, abi_long arg2, abi_long arg3,
233 abi_long arg4, abi_long arg5, abi_long arg6)
235 switch(arg1) {
236 case IPCOP_semctl:
237 gemu_log("semctl(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ",", arg1, arg2);
238 print_ipc_cmd(arg3);
239 gemu_log(",0x" TARGET_ABI_FMT_lx ")", arg4);
240 break;
241 default:
242 gemu_log("%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ")",
243 name->name, arg1, arg2, arg3, arg4);
246 #endif
249 * Variants for the return value output function
252 static void
253 print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
255 if( ret == -1 ) {
256 gemu_log(" = -1 errno=%d (%s)\n", errno, target_strerror(errno));
257 } else {
258 gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
262 #if 0 /* currently unused */
263 static void
264 print_syscall_ret_raw(struct syscallname *name, abi_long ret)
266 gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
268 #endif
270 #ifdef TARGET_NR__newselect
271 static void
272 print_syscall_ret_newselect(const struct syscallname *name, abi_long ret)
274 gemu_log(" = 0x" TARGET_ABI_FMT_lx " (", ret);
275 print_fdset(newselect_arg1,newselect_arg2);
276 gemu_log(",");
277 print_fdset(newselect_arg1,newselect_arg3);
278 gemu_log(",");
279 print_fdset(newselect_arg1,newselect_arg4);
280 gemu_log(",");
281 print_timeval(newselect_arg5, 1);
282 gemu_log(")\n");
284 #endif
286 UNUSED static struct flags access_flags[] = {
287 FLAG_GENERIC(F_OK),
288 FLAG_GENERIC(R_OK),
289 FLAG_GENERIC(W_OK),
290 FLAG_GENERIC(X_OK),
291 FLAG_END,
294 UNUSED static struct flags at_file_flags[] = {
295 #ifdef AT_EACCESS
296 FLAG_GENERIC(AT_EACCESS),
297 #endif
298 #ifdef AT_SYMLINK_NOFOLLOW
299 FLAG_GENERIC(AT_SYMLINK_NOFOLLOW),
300 #endif
301 FLAG_END,
304 UNUSED static struct flags unlinkat_flags[] = {
305 #ifdef AT_REMOVEDIR
306 FLAG_GENERIC(AT_REMOVEDIR),
307 #endif
308 FLAG_END,
311 UNUSED static struct flags mode_flags[] = {
312 FLAG_GENERIC(S_IFSOCK),
313 FLAG_GENERIC(S_IFLNK),
314 FLAG_GENERIC(S_IFREG),
315 FLAG_GENERIC(S_IFBLK),
316 FLAG_GENERIC(S_IFDIR),
317 FLAG_GENERIC(S_IFCHR),
318 FLAG_GENERIC(S_IFIFO),
319 FLAG_END,
322 UNUSED static struct flags open_access_flags[] = {
323 FLAG_TARGET(O_RDONLY),
324 FLAG_TARGET(O_WRONLY),
325 FLAG_TARGET(O_RDWR),
326 FLAG_END,
329 UNUSED static struct flags open_flags[] = {
330 FLAG_TARGET(O_APPEND),
331 FLAG_TARGET(O_CREAT),
332 FLAG_TARGET(O_DIRECTORY),
333 FLAG_TARGET(O_EXCL),
334 FLAG_TARGET(O_LARGEFILE),
335 FLAG_TARGET(O_NOCTTY),
336 FLAG_TARGET(O_NOFOLLOW),
337 FLAG_TARGET(O_NONBLOCK), /* also O_NDELAY */
338 FLAG_TARGET(O_SYNC),
339 FLAG_TARGET(O_TRUNC),
340 #ifdef O_DIRECT
341 FLAG_TARGET(O_DIRECT),
342 #endif
343 FLAG_END,
346 UNUSED static struct flags mount_flags[] = {
347 #ifdef MS_BIND
348 FLAG_GENERIC(MS_BIND),
349 #endif
350 #ifdef MS_DIRSYNC
351 FLAG_GENERIC(MS_DIRSYNC),
352 #endif
353 FLAG_GENERIC(MS_MANDLOCK),
354 #ifdef MS_MOVE
355 FLAG_GENERIC(MS_MOVE),
356 #endif
357 FLAG_GENERIC(MS_NOATIME),
358 FLAG_GENERIC(MS_NODEV),
359 FLAG_GENERIC(MS_NODIRATIME),
360 FLAG_GENERIC(MS_NOEXEC),
361 FLAG_GENERIC(MS_NOSUID),
362 FLAG_GENERIC(MS_RDONLY),
363 #ifdef MS_RELATIME
364 FLAG_GENERIC(MS_RELATIME),
365 #endif
366 FLAG_GENERIC(MS_REMOUNT),
367 FLAG_GENERIC(MS_SYNCHRONOUS),
368 FLAG_END,
371 UNUSED static struct flags umount2_flags[] = {
372 #ifdef MNT_FORCE
373 FLAG_GENERIC(MNT_FORCE),
374 #endif
375 #ifdef MNT_DETACH
376 FLAG_GENERIC(MNT_DETACH),
377 #endif
378 #ifdef MNT_EXPIRE
379 FLAG_GENERIC(MNT_EXPIRE),
380 #endif
381 FLAG_END,
384 UNUSED static struct flags mmap_prot_flags[] = {
385 FLAG_GENERIC(PROT_NONE),
386 FLAG_GENERIC(PROT_EXEC),
387 FLAG_GENERIC(PROT_READ),
388 FLAG_GENERIC(PROT_WRITE),
389 FLAG_END,
392 UNUSED static struct flags mmap_flags[] = {
393 FLAG_TARGET(MAP_SHARED),
394 FLAG_TARGET(MAP_PRIVATE),
395 FLAG_TARGET(MAP_ANONYMOUS),
396 FLAG_TARGET(MAP_DENYWRITE),
397 FLAG_TARGET(MAP_FIXED),
398 FLAG_TARGET(MAP_GROWSDOWN),
399 #ifdef MAP_LOCKED
400 FLAG_TARGET(MAP_LOCKED),
401 #endif
402 #ifdef MAP_NONBLOCK
403 FLAG_TARGET(MAP_NONBLOCK),
404 #endif
405 FLAG_TARGET(MAP_NORESERVE),
406 #ifdef MAP_POPULATE
407 FLAG_TARGET(MAP_POPULATE),
408 #endif
409 FLAG_END,
412 UNUSED static struct flags fcntl_flags[] = {
413 FLAG_TARGET(F_DUPFD),
414 FLAG_TARGET(F_GETFD),
415 FLAG_TARGET(F_SETFD),
416 FLAG_TARGET(F_GETFL),
417 FLAG_TARGET(F_SETFL),
418 FLAG_TARGET(F_GETLK),
419 FLAG_TARGET(F_SETLK),
420 FLAG_TARGET(F_SETLKW),
421 FLAG_END,
425 * print_xxx utility functions. These are used to print syscall
426 * parameters in certain format. All of these have parameter
427 * named 'last'. This parameter is used to add comma to output
428 * when last == 0.
431 static const char *
432 get_comma(int last)
434 return ((last) ? "" : ",");
437 static void
438 print_flags(const struct flags *f, abi_long tflags, int last)
440 const char *sep = "";
441 int flags;
442 int n;
444 flags = (int)tswap32(tflags);
446 if ((flags == 0) && (f->f_value == 0)) {
447 gemu_log("%s%s", f->f_string, get_comma(last));
448 return;
450 for (n = 0; f->f_string != NULL; f++) {
451 if ((f->f_value != 0) && ((flags & f->f_value) == f->f_value)) {
452 gemu_log("%s%s", sep, f->f_string);
453 flags &= ~f->f_value;
454 sep = "|";
455 n++;
459 if (n > 0) {
460 /* print rest of the flags as numeric */
461 if (flags != 0) {
462 gemu_log("%s%#x%s", sep, flags, get_comma(last));
463 } else {
464 gemu_log("%s", get_comma(last));
466 } else {
467 /* no string version of flags found, print them in hex then */
468 gemu_log("%#x%s", flags, get_comma(last));
472 static void
473 print_at_dirfd(abi_long tdirfd, int last)
475 int dirfd = tswap32(tdirfd);
477 #ifdef AT_FDCWD
478 if (dirfd == AT_FDCWD) {
479 gemu_log("AT_FDCWD%s", get_comma(last));
480 return;
482 #endif
483 gemu_log("%d%s", dirfd, get_comma(last));
486 static void
487 print_file_mode(abi_long tmode, int last)
489 const char *sep = "";
490 const struct flags *m;
491 mode_t mode = (mode_t)tswap32(tmode);
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;
502 mode &= ~S_IFMT;
503 /* print rest of the mode as octal */
504 if (mode != 0)
505 gemu_log("%s%#o", sep, mode);
507 gemu_log("%s", get_comma(last));
510 static void
511 print_open_flags(abi_long tflags, int last)
513 int flags = tswap32(tflags);
515 print_flags(open_access_flags, flags & TARGET_O_ACCMODE, 1);
516 flags &= ~TARGET_O_ACCMODE;
517 if (flags == 0) {
518 gemu_log("%s", get_comma(last));
519 return;
521 gemu_log("|");
522 print_flags(open_flags, flags, last);
525 static void
526 print_syscall_prologue(const struct syscallname *sc)
528 gemu_log("%s(", sc->name);
531 /*ARGSUSED*/
532 static void
533 print_syscall_epilogue(const struct syscallname *sc)
535 (void)sc;
536 gemu_log(")");
539 static void
540 print_string(abi_long addr, int last)
542 char *s;
544 if ((s = lock_user_string(addr)) != NULL) {
545 gemu_log("\"%s\"%s", s, get_comma(last));
546 unlock_user(s, addr, 0);
547 } else {
548 /* can't get string out of it, so print it as pointer */
549 print_pointer(addr, last);
554 * Prints out raw parameter using given format. Caller needs
555 * to do byte swapping if needed.
557 static void
558 print_raw_param(const char *fmt, abi_long param, int last)
560 char format[64];
562 (void) snprintf(format, sizeof (format), "%s%s", fmt, get_comma(last));
563 gemu_log(format, param);
566 static void
567 print_pointer(abi_long p, int last)
569 if (p == 0)
570 gemu_log("NULL%s", get_comma(last));
571 else
572 gemu_log("0x" TARGET_ABI_FMT_lx "%s", p, get_comma(last));
576 * Reads 32-bit (int) number from guest address space from
577 * address 'addr' and prints it.
579 static void
580 print_number(abi_long addr, int last)
582 if (addr == 0) {
583 gemu_log("NULL%s", get_comma(last));
584 } else {
585 int num;
587 get_user_s32(num, addr);
588 gemu_log("[%d]%s", num, get_comma(last));
592 static void
593 print_timeval(abi_ulong tv_addr, int last)
595 if( tv_addr ) {
596 struct target_timeval *tv;
598 tv = lock_user(VERIFY_READ, tv_addr, sizeof(*tv), 1);
599 if (!tv)
600 return;
601 gemu_log("{" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "}%s",
602 tv->tv_sec, tv->tv_usec, get_comma(last));
603 unlock_user(tv, tv_addr, 0);
604 } else
605 gemu_log("NULL%s", get_comma(last));
608 #undef UNUSED
610 #ifdef TARGET_NR_accept
611 static void
612 print_accept(const struct syscallname *name,
613 abi_long arg0, abi_long arg1, abi_long arg2,
614 abi_long arg3, abi_long arg4, abi_long arg5)
616 print_syscall_prologue(name);
617 print_raw_param("%d", tswap32(arg0), 0);
618 print_pointer(arg1, 0);
619 print_number(arg2, 1);
620 print_syscall_epilogue(name);
622 #endif
624 #ifdef TARGET_NR_access
625 static void
626 print_access(const struct syscallname *name,
627 abi_long arg0, abi_long arg1, abi_long arg2,
628 abi_long arg3, abi_long arg4, abi_long arg5)
630 print_syscall_prologue(name);
631 print_string(arg0, 0);
632 print_flags(access_flags, arg1, 1);
633 print_syscall_epilogue(name);
635 #endif
637 #ifdef TARGET_NR_brk
638 static void
639 print_brk(const struct syscallname *name,
640 abi_long arg0, abi_long arg1, abi_long arg2,
641 abi_long arg3, abi_long arg4, abi_long arg5)
643 print_syscall_prologue(name);
644 print_pointer(arg0, 1);
645 print_syscall_epilogue(name);
647 #endif
649 #ifdef TARGET_NR_chdir
650 static void
651 print_chdir(const struct syscallname *name,
652 abi_long arg0, abi_long arg1, abi_long arg2,
653 abi_long arg3, abi_long arg4, abi_long arg5)
655 print_syscall_prologue(name);
656 print_string(arg0, 1);
657 print_syscall_epilogue(name);
659 #endif
661 #ifdef TARGET_NR_chmod
662 static void
663 print_chmod(const struct syscallname *name,
664 abi_long arg0, abi_long arg1, abi_long arg2,
665 abi_long arg3, abi_long arg4, abi_long arg5)
667 print_syscall_prologue(name);
668 print_string(arg0, 0);
669 print_file_mode(arg1, 1);
670 print_syscall_epilogue(name);
672 #endif
674 #ifdef TARGET_NR_creat
675 static void
676 print_creat(const struct syscallname *name,
677 abi_long arg0, abi_long arg1, abi_long arg2,
678 abi_long arg3, abi_long arg4, abi_long arg5)
680 print_syscall_prologue(name);
681 print_string(arg0, 0);
682 print_file_mode(arg1, 1);
683 print_syscall_epilogue(name);
685 #endif
687 #ifdef TARGET_NR_execv
688 static void
689 print_execv(const struct syscallname *name,
690 abi_long arg0, abi_long arg1, abi_long arg2,
691 abi_long arg3, abi_long arg4, abi_long arg5)
693 print_syscall_prologue(name);
694 print_string(arg0, 0);
695 print_raw_param("0x" TARGET_ABI_FMT_lx, tswapl(arg1), 1);
696 print_syscall_epilogue(name);
698 #endif
700 #ifdef TARGET_NR_faccessat
701 static void
702 print_faccessat(const struct syscallname *name,
703 abi_long arg0, abi_long arg1, abi_long arg2,
704 abi_long arg3, abi_long arg4, abi_long arg5)
706 print_syscall_prologue(name);
707 print_at_dirfd(arg0, 0);
708 print_string(arg1, 0);
709 print_flags(access_flags, arg2, 0);
710 print_flags(at_file_flags, arg3, 1);
711 print_syscall_epilogue(name);
713 #endif
715 #ifdef TARGET_NR_fchmodat
716 static void
717 print_fchmodat(const struct syscallname *name,
718 abi_long arg0, abi_long arg1, abi_long arg2,
719 abi_long arg3, abi_long arg4, abi_long arg5)
721 print_syscall_prologue(name);
722 print_at_dirfd(arg0, 0);
723 print_string(arg1, 0);
724 print_file_mode(arg2, 0);
725 print_flags(at_file_flags, arg3, 1);
726 print_syscall_epilogue(name);
728 #endif
730 #ifdef TARGET_NR_fchownat
731 static void
732 print_fchownat(const struct syscallname *name,
733 abi_long arg0, abi_long arg1, abi_long arg2,
734 abi_long arg3, abi_long arg4, abi_long arg5)
736 print_syscall_prologue(name);
737 print_at_dirfd(arg0, 0);
738 print_string(arg1, 0);
739 #ifdef USE_UID16
740 print_raw_param("%d", tswap16(arg2), 0);
741 print_raw_param("%d", tswap16(arg3), 0);
742 #else
743 print_raw_param("%d", tswap32(arg2), 0);
744 print_raw_param("%d", tswap32(arg3), 0);
745 #endif
746 print_flags(at_file_flags, arg4, 1);
747 print_syscall_epilogue(name);
749 #endif
751 #if defined(TARGET_NR_fcntl) || defined(TARGET_NR_fcntl64)
752 static void
753 print_fcntl(const struct syscallname *name,
754 abi_long arg0, abi_long arg1, abi_long arg2,
755 abi_long arg3, abi_long arg4, abi_long arg5)
757 print_syscall_prologue(name);
758 print_raw_param("%d", tswap32(arg0), 0);
759 print_flags(fcntl_flags, arg1, 0);
761 * TODO: check flags and print following argument only
762 * when needed.
764 print_pointer(arg2, 1);
765 print_syscall_epilogue(name);
767 #define print_fcntl64 print_fcntl
768 #endif
771 #ifdef TARGET_NR_futimesat
772 static void
773 print_futimesat(const struct syscallname *name,
774 abi_long arg0, abi_long arg1, abi_long arg2,
775 abi_long arg3, abi_long arg4, abi_long arg5)
777 print_syscall_prologue(name);
778 print_at_dirfd(arg0, 0);
779 print_string(arg1, 0);
780 print_timeval(arg2, 0);
781 print_timeval(arg2 + sizeof (struct target_timeval), 1);
782 print_syscall_epilogue(name);
784 #endif
786 #ifdef TARGET_NR_link
787 static void
788 print_link(const struct syscallname *name,
789 abi_long arg0, abi_long arg1, abi_long arg2,
790 abi_long arg3, abi_long arg4, abi_long arg5)
792 print_syscall_prologue(name);
793 print_string(arg0, 0);
794 print_string(arg1, 1);
795 print_syscall_epilogue(name);
797 #endif
799 #ifdef TARGET_NR_linkat
800 static void
801 print_linkat(const struct syscallname *name,
802 abi_long arg0, abi_long arg1, abi_long arg2,
803 abi_long arg3, abi_long arg4, abi_long arg5)
805 print_syscall_prologue(name);
806 print_at_dirfd(arg0, 0);
807 print_string(arg1, 0);
808 print_at_dirfd(arg2, 0);
809 print_string(arg3, 0);
810 print_flags(at_file_flags, arg4, 1);
811 print_syscall_epilogue(name);
813 #endif
815 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) || \
816 defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64)
817 static void
818 print_stat(const struct syscallname *name,
819 abi_long arg0, abi_long arg1, abi_long arg2,
820 abi_long arg3, abi_long arg4, abi_long arg5)
822 print_syscall_prologue(name);
823 print_string(arg0, 0);
824 print_pointer(arg1, 1);
825 print_syscall_epilogue(name);
827 #define print_lstat print_stat
828 #define print_stat64 print_stat
829 #define print_lstat64 print_stat
830 #endif
832 #if defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64)
833 static void
834 print_fstat(const struct syscallname *name,
835 abi_long arg0, abi_long arg1, abi_long arg2,
836 abi_long arg3, abi_long arg4, abi_long arg5)
838 print_syscall_prologue(name);
839 print_raw_param("%d", tswap32(arg0), 0);
840 print_pointer(arg1, 1);
841 print_syscall_epilogue(name);
843 #define print_fstat64 print_fstat
844 #endif
846 #ifdef TARGET_NR_mkdir
847 static void
848 print_mkdir(const struct syscallname *name,
849 abi_long arg0, abi_long arg1, abi_long arg2,
850 abi_long arg3, abi_long arg4, abi_long arg5)
852 print_syscall_prologue(name);
853 print_string(arg0, 0);
854 print_file_mode(arg1, 1);
855 print_syscall_epilogue(name);
857 #endif
859 #ifdef TARGET_NR_mkdirat
860 static void
861 print_mkdirat(const struct syscallname *name,
862 abi_long arg0, abi_long arg1, abi_long arg2,
863 abi_long arg3, abi_long arg4, abi_long arg5)
865 print_syscall_prologue(name);
866 print_at_dirfd(arg0, 0);
867 print_string(arg1, 0);
868 print_file_mode(arg2, 1);
869 print_syscall_epilogue(name);
871 #endif
873 #ifdef TARGET_NR_mknod
874 static void
875 print_mknod(const struct syscallname *name,
876 abi_long arg0, abi_long arg1, abi_long arg2,
877 abi_long arg3, abi_long arg4, abi_long arg5)
879 int hasdev = (tswapl(arg1) & (S_IFCHR|S_IFBLK));
881 print_syscall_prologue(name);
882 print_string(arg0, 0);
883 print_file_mode(arg1, (hasdev == 0));
884 if (hasdev) {
885 print_raw_param("makedev(%d", major(tswapl(arg2)), 0);
886 print_raw_param("%d)", minor(tswapl(arg2)), 1);
888 print_syscall_epilogue(name);
890 #endif
892 #ifdef TARGET_NR_mknodat
893 static void
894 print_mknodat(const struct syscallname *name,
895 abi_long arg0, abi_long arg1, abi_long arg2,
896 abi_long arg3, abi_long arg4, abi_long arg5)
898 int hasdev = (tswapl(arg2) & (S_IFCHR|S_IFBLK));
900 print_syscall_prologue(name);
901 print_at_dirfd(arg0, 0);
902 print_string(arg1, 0);
903 print_file_mode(arg2, (hasdev == 0));
904 if (hasdev) {
905 print_raw_param("makedev(%d", major(tswapl(arg3)), 0);
906 print_raw_param("%d)", minor(tswapl(arg3)), 1);
908 print_syscall_epilogue(name);
910 #endif
912 #ifdef TARGET_NR_mq_open
913 static void
914 print_mq_open(const struct syscallname *name,
915 abi_long arg0, abi_long arg1, abi_long arg2,
916 abi_long arg3, abi_long arg4, abi_long arg5)
918 int is_creat = (tswapl(arg1) & TARGET_O_CREAT);
920 print_syscall_prologue(name);
921 print_string(arg0, 0);
922 print_open_flags(arg1, (is_creat == 0));
923 if (is_creat) {
924 print_file_mode(arg2, 0);
925 print_pointer(arg3, 1);
927 print_syscall_epilogue(name);
929 #endif
931 #ifdef TARGET_NR_open
932 static void
933 print_open(const struct syscallname *name,
934 abi_long arg0, abi_long arg1, abi_long arg2,
935 abi_long arg3, abi_long arg4, abi_long arg5)
937 int is_creat = (tswap32(arg1) & TARGET_O_CREAT);
939 print_syscall_prologue(name);
940 print_string(arg0, 0);
941 print_open_flags(arg1, (is_creat == 0));
942 if (is_creat)
943 print_file_mode(arg2, 1);
944 print_syscall_epilogue(name);
946 #endif
948 #ifdef TARGET_NR_openat
949 static void
950 print_openat(const struct syscallname *name,
951 abi_long arg0, abi_long arg1, abi_long arg2,
952 abi_long arg3, abi_long arg4, abi_long arg5)
954 int is_creat = (tswap32(arg2) & TARGET_O_CREAT);
956 print_syscall_prologue(name);
957 print_at_dirfd(arg0, 0);
958 print_string(arg1, 0);
959 print_open_flags(arg2, (is_creat == 0));
960 if (is_creat)
961 print_file_mode(arg3, 1);
962 print_syscall_epilogue(name);
964 #endif
966 #ifdef TARGET_NR_mq_unlink
967 static void
968 print_mq_unlink(const struct syscallname *name,
969 abi_long arg0, abi_long arg1, abi_long arg2,
970 abi_long arg3, abi_long arg4, abi_long arg5)
972 print_syscall_prologue(name);
973 print_string(arg0, 1);
974 print_syscall_epilogue(name);
976 #endif
978 #if defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)
979 static void
980 print_fstatat64(const struct syscallname *name,
981 abi_long arg0, abi_long arg1, abi_long arg2,
982 abi_long arg3, abi_long arg4, abi_long arg5)
984 print_syscall_prologue(name);
985 print_at_dirfd(arg0, 0);
986 print_string(arg1, 0);
987 print_pointer(arg2, 0);
988 print_flags(at_file_flags, arg3, 1);
989 print_syscall_epilogue(name);
991 #define print_newfstatat print_fstatat64
992 #endif
994 #ifdef TARGET_NR_readlink
995 static void
996 print_readlink(const struct syscallname *name,
997 abi_long arg0, abi_long arg1, abi_long arg2,
998 abi_long arg3, abi_long arg4, abi_long arg5)
1000 print_syscall_prologue(name);
1001 print_string(arg0, 0);
1002 print_pointer(arg1, 0);
1003 print_raw_param("%u", tswapl(arg2), 1);
1004 print_syscall_epilogue(name);
1006 #endif
1008 #ifdef TARGET_NR_readlinkat
1009 static void
1010 print_readlinkat(const struct syscallname *name,
1011 abi_long arg0, abi_long arg1, abi_long arg2,
1012 abi_long arg3, abi_long arg4, abi_long arg5)
1014 print_syscall_prologue(name);
1015 print_at_dirfd(arg0, 0);
1016 print_string(arg1, 0);
1017 print_pointer(arg2, 0);
1018 print_raw_param("%u", tswapl(arg3), 1);
1019 print_syscall_epilogue(name);
1021 #endif
1023 #ifdef TARGET_NR_rename
1024 static void
1025 print_rename(const struct syscallname *name,
1026 abi_long arg0, abi_long arg1, abi_long arg2,
1027 abi_long arg3, abi_long arg4, abi_long arg5)
1029 print_syscall_prologue(name);
1030 print_string(arg0, 0);
1031 print_string(arg1, 1);
1032 print_syscall_epilogue(name);
1034 #endif
1036 #ifdef TARGET_NR_renameat
1037 static void
1038 print_renameat(const struct syscallname *name,
1039 abi_long arg0, abi_long arg1, abi_long arg2,
1040 abi_long arg3, abi_long arg4, abi_long arg5)
1042 print_syscall_prologue(name);
1043 print_at_dirfd(arg0, 0);
1044 print_string(arg1, 0);
1045 print_at_dirfd(arg2, 0);
1046 print_string(arg3, 1);
1047 print_syscall_epilogue(name);
1049 #endif
1051 #ifdef TARGET_NR_statfs
1052 static void
1053 print_statfs(const struct syscallname *name,
1054 abi_long arg0, abi_long arg1, abi_long arg2,
1055 abi_long arg3, abi_long arg4, abi_long arg5)
1057 print_syscall_prologue(name);
1058 print_string(arg0, 0);
1059 print_pointer(arg1, 1);
1060 print_syscall_epilogue(name);
1062 #define print_statfs64 print_statfs
1063 #endif
1065 #ifdef TARGET_NR_symlink
1066 static void
1067 print_symlink(const struct syscallname *name,
1068 abi_long arg0, abi_long arg1, abi_long arg2,
1069 abi_long arg3, abi_long arg4, abi_long arg5)
1071 print_syscall_prologue(name);
1072 print_string(arg0, 0);
1073 print_string(arg1, 1);
1074 print_syscall_epilogue(name);
1076 #endif
1078 #ifdef TARGET_NR_symlinkat
1079 static void
1080 print_symlinkat(const struct syscallname *name,
1081 abi_long arg0, abi_long arg1, abi_long arg2,
1082 abi_long arg3, abi_long arg4, abi_long arg5)
1084 print_syscall_prologue(name);
1085 print_string(arg0, 0);
1086 print_at_dirfd(arg1, 0);
1087 print_string(arg2, 1);
1088 print_syscall_epilogue(name);
1090 #endif
1092 #ifdef TARGET_NR_mount
1093 static void
1094 print_mount(const struct syscallname *name,
1095 abi_long arg0, abi_long arg1, abi_long arg2,
1096 abi_long arg3, abi_long arg4, abi_long arg5)
1098 print_syscall_prologue(name);
1099 print_string(arg0, 0);
1100 print_string(arg1, 0);
1101 print_string(arg2, 0);
1102 print_flags(mount_flags, arg3, 0);
1103 print_pointer(arg4, 1);
1104 print_syscall_epilogue(name);
1106 #endif
1108 #ifdef TARGET_NR_umount
1109 static void
1110 print_umount(const struct syscallname *name,
1111 abi_long arg0, abi_long arg1, abi_long arg2,
1112 abi_long arg3, abi_long arg4, abi_long arg5)
1114 print_syscall_prologue(name);
1115 print_string(arg0, 1);
1116 print_syscall_epilogue(name);
1118 #endif
1120 #ifdef TARGET_NR_umount2
1121 static void
1122 print_umount2(const struct syscallname *name,
1123 abi_long arg0, abi_long arg1, abi_long arg2,
1124 abi_long arg3, abi_long arg4, abi_long arg5)
1126 print_syscall_prologue(name);
1127 print_string(arg0, 0);
1128 print_flags(umount2_flags, arg1, 1);
1129 print_syscall_epilogue(name);
1131 #endif
1133 #ifdef TARGET_NR_unlink
1134 static void
1135 print_unlink(const struct syscallname *name,
1136 abi_long arg0, abi_long arg1, abi_long arg2,
1137 abi_long arg3, abi_long arg4, abi_long arg5)
1139 print_syscall_prologue(name);
1140 print_string(arg0, 1);
1141 print_syscall_epilogue(name);
1143 #endif
1145 #ifdef TARGET_NR_unlinkat
1146 static void
1147 print_unlinkat(const struct syscallname *name,
1148 abi_long arg0, abi_long arg1, abi_long arg2,
1149 abi_long arg3, abi_long arg4, abi_long arg5)
1151 print_syscall_prologue(name);
1152 print_at_dirfd(arg0, 0);
1153 print_string(arg1, 0);
1154 print_flags(unlinkat_flags, arg2, 1);
1155 print_syscall_epilogue(name);
1157 #endif
1159 #ifdef TARGET_NR_utime
1160 static void
1161 print_utime(const struct syscallname *name,
1162 abi_long arg0, abi_long arg1, abi_long arg2,
1163 abi_long arg3, abi_long arg4, abi_long arg5)
1165 print_syscall_prologue(name);
1166 print_string(arg0, 0);
1167 print_pointer(arg1, 1);
1168 print_syscall_epilogue(name);
1170 #endif
1172 #ifdef TARGET_NR_utimes
1173 static void
1174 print_utimes(const struct syscallname *name,
1175 abi_long arg0, abi_long arg1, abi_long arg2,
1176 abi_long arg3, abi_long arg4, abi_long arg5)
1178 print_syscall_prologue(name);
1179 print_string(arg0, 0);
1180 print_pointer(arg1, 1);
1181 print_syscall_epilogue(name);
1183 #endif
1185 #ifdef TARGET_NR_utimensat
1186 static void
1187 print_utimensat(const struct syscallname *name,
1188 abi_long arg0, abi_long arg1, abi_long arg2,
1189 abi_long arg3, abi_long arg4, abi_long arg5)
1191 print_syscall_prologue(name);
1192 print_at_dirfd(arg0, 0);
1193 print_string(arg1, 0);
1194 print_pointer(arg2, 0);
1195 print_flags(at_file_flags, arg3, 1);
1196 print_syscall_epilogue(name);
1198 #endif
1200 #ifdef TARGET_NR_mmap
1201 static void
1202 print_mmap(const struct syscallname *name,
1203 abi_long arg0, abi_long arg1, abi_long arg2,
1204 abi_long arg3, abi_long arg4, abi_long arg5)
1206 print_syscall_prologue(name);
1207 print_pointer(arg0, 0);
1208 print_raw_param("%d", tswapl(arg1), 0);
1209 print_flags(mmap_prot_flags, arg2, 0);
1210 print_flags(mmap_flags, arg3, 0);
1211 print_raw_param("%d", tswapl(arg4), 0);
1212 print_raw_param("%#x", tswapl(arg5), 1);
1213 print_syscall_epilogue(name);
1215 #define print_mmap2 print_mmap
1216 #endif
1218 #ifdef TARGET_NR_mprotect
1219 static void
1220 print_mprotect(const struct syscallname *name,
1221 abi_long arg0, abi_long arg1, abi_long arg2,
1222 abi_long arg3, abi_long arg4, abi_long arg5)
1224 print_syscall_prologue(name);
1225 print_pointer(arg0, 0);
1226 print_raw_param("%d", tswapl(arg1), 0);
1227 print_flags(mmap_prot_flags, arg2, 1);
1228 print_syscall_epilogue(name);
1230 #endif
1232 #ifdef TARGET_NR_munmap
1233 static void
1234 print_munmap(const struct syscallname *name,
1235 abi_long arg0, abi_long arg1, abi_long arg2,
1236 abi_long arg3, abi_long arg4, abi_long arg5)
1238 print_syscall_prologue(name);
1239 print_pointer(arg0, 0);
1240 print_raw_param("%d", tswapl(arg1), 1);
1241 print_syscall_epilogue(name);
1243 #endif
1245 #ifdef TARGET_NR_futex
1246 static void print_futex_op(abi_long tflag, int last)
1248 #define print_op(val) \
1249 if( cmd == val ) { \
1250 gemu_log(#val); \
1251 return; \
1254 int cmd = (int)tswap32(tflag);
1255 #ifdef FUTEX_PRIVATE_FLAG
1256 if (cmd == FUTEX_PRIVATE_FLAG)
1257 gemu_log("FUTEX_PRIVATE_FLAG|");
1258 #endif
1259 print_op(FUTEX_WAIT)
1260 print_op(FUTEX_WAKE)
1261 print_op(FUTEX_FD)
1262 print_op(FUTEX_REQUEUE)
1263 print_op(FUTEX_CMP_REQUEUE)
1264 print_op(FUTEX_WAKE_OP)
1265 print_op(FUTEX_LOCK_PI)
1266 print_op(FUTEX_UNLOCK_PI)
1267 print_op(FUTEX_TRYLOCK_PI)
1268 #ifdef FUTEX_WAIT_BITSET
1269 print_op(FUTEX_WAIT_BITSET)
1270 #endif
1271 #ifdef FUTEX_WAKE_BITSET
1272 print_op(FUTEX_WAKE_BITSET)
1273 #endif
1274 /* unknown values */
1275 gemu_log("%d",cmd);
1278 static void
1279 print_futex(const struct syscallname *name,
1280 abi_long arg0, abi_long arg1, abi_long arg2,
1281 abi_long arg3, abi_long arg4, abi_long arg5)
1283 print_syscall_prologue(name);
1284 print_pointer(arg0, 0);
1285 print_futex_op(arg1, 0);
1286 print_raw_param(",%d", tswapl(arg2), 0);
1287 print_pointer(arg3, 0); /* struct timespec */
1288 print_pointer(arg4, 0);
1289 print_raw_param("%d", tswapl(arg4), 1);
1290 print_syscall_epilogue(name);
1292 #endif
1295 * An array of all of the syscalls we know about
1298 static const struct syscallname scnames[] = {
1299 #include "strace.list"
1302 static int nsyscalls = ARRAY_SIZE(scnames);
1305 * The public interface to this module.
1307 void
1308 print_syscall(int num,
1309 abi_long arg1, abi_long arg2, abi_long arg3,
1310 abi_long arg4, abi_long arg5, abi_long arg6)
1312 int i;
1313 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 ")";
1315 gemu_log("%d ", getpid() );
1317 for(i=0;i<nsyscalls;i++)
1318 if( scnames[i].nr == num ) {
1319 if( scnames[i].call != NULL ) {
1320 scnames[i].call(&scnames[i],arg1,arg2,arg3,arg4,arg5,arg6);
1321 } else {
1322 /* XXX: this format system is broken because it uses
1323 host types and host pointers for strings */
1324 if( scnames[i].format != NULL )
1325 format = scnames[i].format;
1326 gemu_log(format,scnames[i].name, arg1,arg2,arg3,arg4,arg5,arg6);
1328 return;
1330 gemu_log("Unknown syscall %d\n", num);
1334 void
1335 print_syscall_ret(int num, abi_long ret)
1337 int i;
1339 for(i=0;i<nsyscalls;i++)
1340 if( scnames[i].nr == num ) {
1341 if( scnames[i].result != NULL ) {
1342 scnames[i].result(&scnames[i],ret);
1343 } else {
1344 if( ret < 0 ) {
1345 gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret, target_strerror(-ret));
1346 } else {
1347 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
1350 break;