xref: /openbmc/qemu/linux-user/strace.c (revision eabfeb0c)
1 #include "qemu/osdep.h"
2 #include <sys/ipc.h>
3 #include <sys/msg.h>
4 #include <sys/sem.h>
5 #include <sys/shm.h>
6 #include <sys/select.h>
7 #include <sys/mount.h>
8 #include <arpa/inet.h>
9 #include <netinet/tcp.h>
10 #include <linux/if_packet.h>
11 #include <linux/netlink.h>
12 #include <sched.h>
13 #include "qemu.h"
14 
15 struct syscallname {
16     int nr;
17     const char *name;
18     const char *format;
19     void (*call)(const struct syscallname *,
20                  abi_long, abi_long, abi_long,
21                  abi_long, abi_long, abi_long);
22     void (*result)(const struct syscallname *, abi_long,
23                    abi_long, abi_long, abi_long,
24                    abi_long, abi_long, abi_long);
25 };
26 
27 #ifdef __GNUC__
28 /*
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.
33  */
34 #define UNUSED __attribute__ ((unused))
35 #else
36 #define UNUSED
37 #endif
38 
39 /*
40  * Structure used to translate flag values into strings.  This is
41  * similar that is in the actual strace tool.
42  */
43 struct flags {
44     abi_long    f_value;  /* flag */
45     const char  *f_string; /* stringified flag */
46 };
47 
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 }
54 
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_buf(abi_long addr, abi_long len, int last);
65 UNUSED static void print_raw_param(const char *, abi_long, int);
66 UNUSED static void print_timeval(abi_ulong, int);
67 UNUSED static void print_timezone(abi_ulong, int);
68 UNUSED static void print_number(abi_long, int);
69 UNUSED static void print_signal(abi_ulong, int);
70 UNUSED static void print_sockaddr(abi_ulong, abi_long, int);
71 UNUSED static void print_socket_domain(int domain);
72 UNUSED static void print_socket_type(int type);
73 UNUSED static void print_socket_protocol(int domain, int type, int protocol);
74 
75 /*
76  * Utility functions
77  */
78 static void
79 print_ipc_cmd(int cmd)
80 {
81 #define output_cmd(val) \
82 if( cmd == val ) { \
83     qemu_log(#val); \
84     return; \
85 }
86 
87     cmd &= 0xff;
88 
89     /* General IPC commands */
90     output_cmd( IPC_RMID );
91     output_cmd( IPC_SET );
92     output_cmd( IPC_STAT );
93     output_cmd( IPC_INFO );
94     /* msgctl() commands */
95     output_cmd( MSG_STAT );
96     output_cmd( MSG_INFO );
97     /* shmctl() commands */
98     output_cmd( SHM_LOCK );
99     output_cmd( SHM_UNLOCK );
100     output_cmd( SHM_STAT );
101     output_cmd( SHM_INFO );
102     /* semctl() commands */
103     output_cmd( GETPID );
104     output_cmd( GETVAL );
105     output_cmd( GETALL );
106     output_cmd( GETNCNT );
107     output_cmd( GETZCNT );
108     output_cmd( SETVAL );
109     output_cmd( SETALL );
110     output_cmd( SEM_STAT );
111     output_cmd( SEM_INFO );
112     output_cmd( IPC_RMID );
113     output_cmd( IPC_RMID );
114     output_cmd( IPC_RMID );
115     output_cmd( IPC_RMID );
116     output_cmd( IPC_RMID );
117     output_cmd( IPC_RMID );
118     output_cmd( IPC_RMID );
119     output_cmd( IPC_RMID );
120     output_cmd( IPC_RMID );
121 
122     /* Some value we don't recognize */
123     qemu_log("%d", cmd);
124 }
125 
126 static void
127 print_signal(abi_ulong arg, int last)
128 {
129     const char *signal_name = NULL;
130     switch(arg) {
131     case TARGET_SIGHUP: signal_name = "SIGHUP"; break;
132     case TARGET_SIGINT: signal_name = "SIGINT"; break;
133     case TARGET_SIGQUIT: signal_name = "SIGQUIT"; break;
134     case TARGET_SIGILL: signal_name = "SIGILL"; break;
135     case TARGET_SIGABRT: signal_name = "SIGABRT"; break;
136     case TARGET_SIGFPE: signal_name = "SIGFPE"; break;
137     case TARGET_SIGKILL: signal_name = "SIGKILL"; break;
138     case TARGET_SIGSEGV: signal_name = "SIGSEGV"; break;
139     case TARGET_SIGPIPE: signal_name = "SIGPIPE"; break;
140     case TARGET_SIGALRM: signal_name = "SIGALRM"; break;
141     case TARGET_SIGTERM: signal_name = "SIGTERM"; break;
142     case TARGET_SIGUSR1: signal_name = "SIGUSR1"; break;
143     case TARGET_SIGUSR2: signal_name = "SIGUSR2"; break;
144     case TARGET_SIGCHLD: signal_name = "SIGCHLD"; break;
145     case TARGET_SIGCONT: signal_name = "SIGCONT"; break;
146     case TARGET_SIGSTOP: signal_name = "SIGSTOP"; break;
147     case TARGET_SIGTTIN: signal_name = "SIGTTIN"; break;
148     case TARGET_SIGTTOU: signal_name = "SIGTTOU"; break;
149     }
150     if (signal_name == NULL) {
151         print_raw_param("%ld", arg, last);
152         return;
153     }
154     qemu_log("%s%s", signal_name, get_comma(last));
155 }
156 
157 static void print_si_code(int arg)
158 {
159     const char *codename = NULL;
160 
161     switch (arg) {
162     case SI_USER:
163         codename = "SI_USER";
164         break;
165     case SI_KERNEL:
166         codename = "SI_KERNEL";
167         break;
168     case SI_QUEUE:
169         codename = "SI_QUEUE";
170         break;
171     case SI_TIMER:
172         codename = "SI_TIMER";
173         break;
174     case SI_MESGQ:
175         codename = "SI_MESGQ";
176         break;
177     case SI_ASYNCIO:
178         codename = "SI_ASYNCIO";
179         break;
180     case SI_SIGIO:
181         codename = "SI_SIGIO";
182         break;
183     case SI_TKILL:
184         codename = "SI_TKILL";
185         break;
186     default:
187         qemu_log("%d", arg);
188         return;
189     }
190     qemu_log("%s", codename);
191 }
192 
193 static void get_target_siginfo(target_siginfo_t *tinfo,
194                                 const target_siginfo_t *info)
195 {
196     abi_ulong sival_ptr;
197 
198     int sig;
199     int si_errno;
200     int si_code;
201     int si_type;
202 
203     __get_user(sig, &info->si_signo);
204     __get_user(si_errno, &tinfo->si_errno);
205     __get_user(si_code, &info->si_code);
206 
207     tinfo->si_signo = sig;
208     tinfo->si_errno = si_errno;
209     tinfo->si_code = si_code;
210 
211     /* Ensure we don't leak random junk to the guest later */
212     memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad));
213 
214     /* This is awkward, because we have to use a combination of
215      * the si_code and si_signo to figure out which of the union's
216      * members are valid. (Within the host kernel it is always possible
217      * to tell, but the kernel carefully avoids giving userspace the
218      * high 16 bits of si_code, so we don't have the information to
219      * do this the easy way...) We therefore make our best guess,
220      * bearing in mind that a guest can spoof most of the si_codes
221      * via rt_sigqueueinfo() if it likes.
222      *
223      * Once we have made our guess, we record it in the top 16 bits of
224      * the si_code, so that print_siginfo() later can use it.
225      * print_siginfo() will strip these top bits out before printing
226      * the si_code.
227      */
228 
229     switch (si_code) {
230     case SI_USER:
231     case SI_TKILL:
232     case SI_KERNEL:
233         /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
234          * These are the only unspoofable si_code values.
235          */
236         __get_user(tinfo->_sifields._kill._pid, &info->_sifields._kill._pid);
237         __get_user(tinfo->_sifields._kill._uid, &info->_sifields._kill._uid);
238         si_type = QEMU_SI_KILL;
239         break;
240     default:
241         /* Everything else is spoofable. Make best guess based on signal */
242         switch (sig) {
243         case TARGET_SIGCHLD:
244             __get_user(tinfo->_sifields._sigchld._pid,
245                        &info->_sifields._sigchld._pid);
246             __get_user(tinfo->_sifields._sigchld._uid,
247                        &info->_sifields._sigchld._uid);
248             __get_user(tinfo->_sifields._sigchld._status,
249                        &info->_sifields._sigchld._status);
250             __get_user(tinfo->_sifields._sigchld._utime,
251                        &info->_sifields._sigchld._utime);
252             __get_user(tinfo->_sifields._sigchld._stime,
253                        &info->_sifields._sigchld._stime);
254             si_type = QEMU_SI_CHLD;
255             break;
256         case TARGET_SIGIO:
257             __get_user(tinfo->_sifields._sigpoll._band,
258                        &info->_sifields._sigpoll._band);
259             __get_user(tinfo->_sifields._sigpoll._fd,
260                        &info->_sifields._sigpoll._fd);
261             si_type = QEMU_SI_POLL;
262             break;
263         default:
264             /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
265             __get_user(tinfo->_sifields._rt._pid, &info->_sifields._rt._pid);
266             __get_user(tinfo->_sifields._rt._uid, &info->_sifields._rt._uid);
267             /* XXX: potential problem if 64 bit */
268             __get_user(sival_ptr, &info->_sifields._rt._sigval.sival_ptr);
269             tinfo->_sifields._rt._sigval.sival_ptr = sival_ptr;
270 
271             si_type = QEMU_SI_RT;
272             break;
273         }
274         break;
275     }
276 
277     tinfo->si_code = deposit32(si_code, 16, 16, si_type);
278 }
279 
280 static void print_siginfo(const target_siginfo_t *tinfo)
281 {
282     /* Print a target_siginfo_t in the format desired for printing
283      * signals being taken. We assume the target_siginfo_t is in the
284      * internal form where the top 16 bits of si_code indicate which
285      * part of the union is valid, rather than in the guest-visible
286      * form where the bottom 16 bits are sign-extended into the top 16.
287      */
288     int si_type = extract32(tinfo->si_code, 16, 16);
289     int si_code = sextract32(tinfo->si_code, 0, 16);
290 
291     qemu_log("{si_signo=");
292     print_signal(tinfo->si_signo, 1);
293     qemu_log(", si_code=");
294     print_si_code(si_code);
295 
296     switch (si_type) {
297     case QEMU_SI_KILL:
298         qemu_log(", si_pid=%u, si_uid=%u",
299                  (unsigned int)tinfo->_sifields._kill._pid,
300                  (unsigned int)tinfo->_sifields._kill._uid);
301         break;
302     case QEMU_SI_TIMER:
303         qemu_log(", si_timer1=%u, si_timer2=%u",
304                  tinfo->_sifields._timer._timer1,
305                  tinfo->_sifields._timer._timer2);
306         break;
307     case QEMU_SI_POLL:
308         qemu_log(", si_band=%d, si_fd=%d",
309                  tinfo->_sifields._sigpoll._band,
310                  tinfo->_sifields._sigpoll._fd);
311         break;
312     case QEMU_SI_FAULT:
313         qemu_log(", si_addr=");
314         print_pointer(tinfo->_sifields._sigfault._addr, 1);
315         break;
316     case QEMU_SI_CHLD:
317         qemu_log(", si_pid=%u, si_uid=%u, si_status=%d"
318                  ", si_utime=" TARGET_ABI_FMT_ld
319                  ", si_stime=" TARGET_ABI_FMT_ld,
320                  (unsigned int)(tinfo->_sifields._sigchld._pid),
321                  (unsigned int)(tinfo->_sifields._sigchld._uid),
322                  tinfo->_sifields._sigchld._status,
323                  tinfo->_sifields._sigchld._utime,
324                  tinfo->_sifields._sigchld._stime);
325         break;
326     case QEMU_SI_RT:
327         qemu_log(", si_pid=%u, si_uid=%u, si_sigval=" TARGET_ABI_FMT_ld,
328                  (unsigned int)tinfo->_sifields._rt._pid,
329                  (unsigned int)tinfo->_sifields._rt._uid,
330                  tinfo->_sifields._rt._sigval.sival_ptr);
331         break;
332     default:
333         g_assert_not_reached();
334     }
335     qemu_log("}");
336 }
337 
338 static void
339 print_sockaddr(abi_ulong addr, abi_long addrlen, int last)
340 {
341     struct target_sockaddr *sa;
342     int i;
343     int sa_family;
344 
345     sa = lock_user(VERIFY_READ, addr, addrlen, 1);
346     if (sa) {
347         sa_family = tswap16(sa->sa_family);
348         switch (sa_family) {
349         case AF_UNIX: {
350             struct target_sockaddr_un *un = (struct target_sockaddr_un *)sa;
351             int i;
352             qemu_log("{sun_family=AF_UNIX,sun_path=\"");
353             for (i = 0; i < addrlen -
354                             offsetof(struct target_sockaddr_un, sun_path) &&
355                  un->sun_path[i]; i++) {
356                 qemu_log("%c", un->sun_path[i]);
357             }
358             qemu_log("\"}");
359             break;
360         }
361         case AF_INET: {
362             struct target_sockaddr_in *in = (struct target_sockaddr_in *)sa;
363             uint8_t *c = (uint8_t *)&in->sin_addr.s_addr;
364             qemu_log("{sin_family=AF_INET,sin_port=htons(%d),",
365                      ntohs(in->sin_port));
366             qemu_log("sin_addr=inet_addr(\"%d.%d.%d.%d\")",
367                      c[0], c[1], c[2], c[3]);
368             qemu_log("}");
369             break;
370         }
371         case AF_PACKET: {
372             struct target_sockaddr_ll *ll = (struct target_sockaddr_ll *)sa;
373             uint8_t *c = (uint8_t *)&ll->sll_addr;
374             qemu_log("{sll_family=AF_PACKET,"
375                      "sll_protocol=htons(0x%04x),if%d,pkttype=",
376                      ntohs(ll->sll_protocol), ll->sll_ifindex);
377             switch (ll->sll_pkttype) {
378             case PACKET_HOST:
379                 qemu_log("PACKET_HOST");
380                 break;
381             case PACKET_BROADCAST:
382                 qemu_log("PACKET_BROADCAST");
383                 break;
384             case PACKET_MULTICAST:
385                 qemu_log("PACKET_MULTICAST");
386                 break;
387             case PACKET_OTHERHOST:
388                 qemu_log("PACKET_OTHERHOST");
389                 break;
390             case PACKET_OUTGOING:
391                 qemu_log("PACKET_OUTGOING");
392                 break;
393             default:
394                 qemu_log("%d", ll->sll_pkttype);
395                 break;
396             }
397             qemu_log(",sll_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
398                      c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]);
399             qemu_log("}");
400             break;
401         }
402         case AF_NETLINK: {
403             struct target_sockaddr_nl *nl = (struct target_sockaddr_nl *)sa;
404             qemu_log("{nl_family=AF_NETLINK,nl_pid=%u,nl_groups=%u}",
405                      tswap32(nl->nl_pid), tswap32(nl->nl_groups));
406             break;
407         }
408         default:
409             qemu_log("{sa_family=%d, sa_data={", sa->sa_family);
410             for (i = 0; i < 13; i++) {
411                 qemu_log("%02x, ", sa->sa_data[i]);
412             }
413             qemu_log("%02x}", sa->sa_data[i]);
414             qemu_log("}");
415             break;
416         }
417         unlock_user(sa, addr, 0);
418     } else {
419         print_raw_param("0x"TARGET_ABI_FMT_lx, addr, 0);
420     }
421     qemu_log(", "TARGET_ABI_FMT_ld"%s", addrlen, get_comma(last));
422 }
423 
424 static void
425 print_socket_domain(int domain)
426 {
427     switch (domain) {
428     case PF_UNIX:
429         qemu_log("PF_UNIX");
430         break;
431     case PF_INET:
432         qemu_log("PF_INET");
433         break;
434     case PF_NETLINK:
435         qemu_log("PF_NETLINK");
436         break;
437     case PF_PACKET:
438         qemu_log("PF_PACKET");
439         break;
440     default:
441         qemu_log("%d", domain);
442         break;
443     }
444 }
445 
446 static void
447 print_socket_type(int type)
448 {
449     switch (type & TARGET_SOCK_TYPE_MASK) {
450     case TARGET_SOCK_DGRAM:
451         qemu_log("SOCK_DGRAM");
452         break;
453     case TARGET_SOCK_STREAM:
454         qemu_log("SOCK_STREAM");
455         break;
456     case TARGET_SOCK_RAW:
457         qemu_log("SOCK_RAW");
458         break;
459     case TARGET_SOCK_RDM:
460         qemu_log("SOCK_RDM");
461         break;
462     case TARGET_SOCK_SEQPACKET:
463         qemu_log("SOCK_SEQPACKET");
464         break;
465     case TARGET_SOCK_PACKET:
466         qemu_log("SOCK_PACKET");
467         break;
468     }
469     if (type & TARGET_SOCK_CLOEXEC) {
470         qemu_log("|SOCK_CLOEXEC");
471     }
472     if (type & TARGET_SOCK_NONBLOCK) {
473         qemu_log("|SOCK_NONBLOCK");
474     }
475 }
476 
477 static void
478 print_socket_protocol(int domain, int type, int protocol)
479 {
480     if (domain == AF_PACKET ||
481         (domain == AF_INET && type == TARGET_SOCK_PACKET)) {
482         switch (protocol) {
483         case 0x0003:
484             qemu_log("ETH_P_ALL");
485             break;
486         default:
487             qemu_log("%d", protocol);
488         }
489         return;
490     }
491 
492     if (domain == PF_NETLINK) {
493         switch (protocol) {
494         case NETLINK_ROUTE:
495             qemu_log("NETLINK_ROUTE");
496             break;
497         case NETLINK_AUDIT:
498             qemu_log("NETLINK_AUDIT");
499             break;
500         case NETLINK_NETFILTER:
501             qemu_log("NETLINK_NETFILTER");
502             break;
503         case NETLINK_KOBJECT_UEVENT:
504             qemu_log("NETLINK_KOBJECT_UEVENT");
505             break;
506         case NETLINK_RDMA:
507             qemu_log("NETLINK_RDMA");
508             break;
509         case NETLINK_CRYPTO:
510             qemu_log("NETLINK_CRYPTO");
511             break;
512         default:
513             qemu_log("%d", protocol);
514             break;
515         }
516         return;
517     }
518 
519     switch (protocol) {
520     case IPPROTO_IP:
521         qemu_log("IPPROTO_IP");
522         break;
523     case IPPROTO_TCP:
524         qemu_log("IPPROTO_TCP");
525         break;
526     case IPPROTO_UDP:
527         qemu_log("IPPROTO_UDP");
528         break;
529     case IPPROTO_RAW:
530         qemu_log("IPPROTO_RAW");
531         break;
532     default:
533         qemu_log("%d", protocol);
534         break;
535     }
536 }
537 
538 
539 #ifdef TARGET_NR__newselect
540 static void
541 print_fdset(int n, abi_ulong target_fds_addr)
542 {
543     int i;
544 
545     qemu_log("[");
546     if( target_fds_addr ) {
547         abi_long *target_fds;
548 
549         target_fds = lock_user(VERIFY_READ,
550                                target_fds_addr,
551                                sizeof(*target_fds)*(n / TARGET_ABI_BITS + 1),
552                                1);
553 
554         if (!target_fds)
555             return;
556 
557         for (i=n; i>=0; i--) {
558             if ((tswapal(target_fds[i / TARGET_ABI_BITS]) >> (i & (TARGET_ABI_BITS - 1))) & 1)
559                 qemu_log("%d,", i);
560             }
561         unlock_user(target_fds, target_fds_addr, 0);
562     }
563     qemu_log("]");
564 }
565 #endif
566 
567 #ifdef TARGET_NR_clock_adjtime
568 /* IDs of the various system clocks */
569 #define TARGET_CLOCK_REALTIME              0
570 #define TARGET_CLOCK_MONOTONIC             1
571 #define TARGET_CLOCK_PROCESS_CPUTIME_ID    2
572 #define TARGET_CLOCK_THREAD_CPUTIME_ID     3
573 #define TARGET_CLOCK_MONOTONIC_RAW         4
574 #define TARGET_CLOCK_REALTIME_COARSE       5
575 #define TARGET_CLOCK_MONOTONIC_COARSE      6
576 #define TARGET_CLOCK_BOOTTIME              7
577 #define TARGET_CLOCK_REALTIME_ALARM        8
578 #define TARGET_CLOCK_BOOTTIME_ALARM        9
579 #define TARGET_CLOCK_SGI_CYCLE             10
580 #define TARGET_CLOCK_TAI                   11
581 
582 static void
583 print_clockid(int clockid, int last)
584 {
585     switch (clockid) {
586     case TARGET_CLOCK_REALTIME:
587         qemu_log("CLOCK_REALTIME");
588         break;
589     case TARGET_CLOCK_MONOTONIC:
590         qemu_log("CLOCK_MONOTONIC");
591         break;
592     case TARGET_CLOCK_PROCESS_CPUTIME_ID:
593         qemu_log("CLOCK_PROCESS_CPUTIME_ID");
594         break;
595     case TARGET_CLOCK_THREAD_CPUTIME_ID:
596         qemu_log("CLOCK_THREAD_CPUTIME_ID");
597         break;
598     case TARGET_CLOCK_MONOTONIC_RAW:
599         qemu_log("CLOCK_MONOTONIC_RAW");
600         break;
601     case TARGET_CLOCK_REALTIME_COARSE:
602         qemu_log("CLOCK_REALTIME_COARSE");
603         break;
604     case TARGET_CLOCK_MONOTONIC_COARSE:
605         qemu_log("CLOCK_MONOTONIC_COARSE");
606         break;
607     case TARGET_CLOCK_BOOTTIME:
608         qemu_log("CLOCK_BOOTTIME");
609         break;
610     case TARGET_CLOCK_REALTIME_ALARM:
611         qemu_log("CLOCK_REALTIME_ALARM");
612         break;
613     case TARGET_CLOCK_BOOTTIME_ALARM:
614         qemu_log("CLOCK_BOOTTIME_ALARM");
615         break;
616     case TARGET_CLOCK_SGI_CYCLE:
617         qemu_log("CLOCK_SGI_CYCLE");
618         break;
619     case TARGET_CLOCK_TAI:
620         qemu_log("CLOCK_TAI");
621         break;
622     default:
623         qemu_log("%d", clockid);
624         break;
625     }
626     qemu_log("%s", get_comma(last));
627 }
628 #endif
629 
630 /*
631  * Sysycall specific output functions
632  */
633 
634 /* select */
635 #ifdef TARGET_NR__newselect
636 static void
637 print_newselect(const struct syscallname *name,
638                 abi_long arg1, abi_long arg2, abi_long arg3,
639                 abi_long arg4, abi_long arg5, abi_long arg6)
640 {
641     print_syscall_prologue(name);
642     print_fdset(arg1, arg2);
643     qemu_log(",");
644     print_fdset(arg1, arg3);
645     qemu_log(",");
646     print_fdset(arg1, arg4);
647     qemu_log(",");
648     print_timeval(arg5, 1);
649     print_syscall_epilogue(name);
650 }
651 #endif
652 
653 #ifdef TARGET_NR_semctl
654 static void
655 print_semctl(const struct syscallname *name,
656              abi_long arg1, abi_long arg2, abi_long arg3,
657              abi_long arg4, abi_long arg5, abi_long arg6)
658 {
659     qemu_log("%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ",",
660              name->name, arg1, arg2);
661     print_ipc_cmd(arg3);
662     qemu_log(",0x" TARGET_ABI_FMT_lx ")", arg4);
663 }
664 #endif
665 
666 static void
667 print_execve(const struct syscallname *name,
668              abi_long arg1, abi_long arg2, abi_long arg3,
669              abi_long arg4, abi_long arg5, abi_long arg6)
670 {
671     abi_ulong arg_ptr_addr;
672     char *s;
673 
674     if (!(s = lock_user_string(arg1)))
675         return;
676     qemu_log("%s(\"%s\",{", name->name, s);
677     unlock_user(s, arg1, 0);
678 
679     for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) {
680         abi_ulong *arg_ptr, arg_addr;
681 
682         arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1);
683         if (!arg_ptr)
684             return;
685     arg_addr = tswapal(*arg_ptr);
686         unlock_user(arg_ptr, arg_ptr_addr, 0);
687         if (!arg_addr)
688             break;
689         if ((s = lock_user_string(arg_addr))) {
690             qemu_log("\"%s\",", s);
691             unlock_user(s, arg_addr, 0);
692         }
693     }
694 
695     qemu_log("NULL})");
696 }
697 
698 #ifdef TARGET_NR_ipc
699 static void
700 print_ipc(const struct syscallname *name,
701           abi_long arg1, abi_long arg2, abi_long arg3,
702           abi_long arg4, abi_long arg5, abi_long arg6)
703 {
704     switch(arg1) {
705     case IPCOP_semctl:
706         qemu_log("semctl(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ",",
707                  arg1, arg2);
708         print_ipc_cmd(arg3);
709         qemu_log(",0x" TARGET_ABI_FMT_lx ")", arg4);
710         break;
711     default:
712         qemu_log(("%s("
713                   TARGET_ABI_FMT_ld ","
714                   TARGET_ABI_FMT_ld ","
715                   TARGET_ABI_FMT_ld ","
716                   TARGET_ABI_FMT_ld
717                   ")"),
718                  name->name, arg1, arg2, arg3, arg4);
719     }
720 }
721 #endif
722 
723 /*
724  * Variants for the return value output function
725  */
726 
727 static bool
728 print_syscall_err(abi_long ret)
729 {
730     const char *errstr;
731 
732     qemu_log(" = ");
733     if (ret < 0) {
734         errstr = target_strerror(-ret);
735         if (errstr) {
736             qemu_log("-1 errno=%d (%s)", (int)-ret, errstr);
737             return true;
738         }
739     }
740     return false;
741 }
742 
743 static void
744 print_syscall_ret_addr(const struct syscallname *name, abi_long ret,
745                        abi_long arg0, abi_long arg1, abi_long arg2,
746                        abi_long arg3, abi_long arg4, abi_long arg5)
747 {
748     if (!print_syscall_err(ret)) {
749         qemu_log("0x" TARGET_ABI_FMT_lx, ret);
750     }
751     qemu_log("\n");
752 }
753 
754 #if 0 /* currently unused */
755 static void
756 print_syscall_ret_raw(struct syscallname *name, abi_long ret)
757 {
758         qemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
759 }
760 #endif
761 
762 #ifdef TARGET_NR__newselect
763 static void
764 print_syscall_ret_newselect(const struct syscallname *name, abi_long ret,
765                             abi_long arg0, abi_long arg1, abi_long arg2,
766                             abi_long arg3, abi_long arg4, abi_long arg5)
767 {
768     if (!print_syscall_err(ret)) {
769         qemu_log(" = 0x" TARGET_ABI_FMT_lx " (", ret);
770         print_fdset(arg0, arg1);
771         qemu_log(",");
772         print_fdset(arg0, arg2);
773         qemu_log(",");
774         print_fdset(arg0, arg3);
775         qemu_log(",");
776         print_timeval(arg4, 1);
777         qemu_log(")");
778     }
779 
780     qemu_log("\n");
781 }
782 #endif
783 
784 /* special meanings of adjtimex()' non-negative return values */
785 #define TARGET_TIME_OK       0   /* clock synchronized, no leap second */
786 #define TARGET_TIME_INS      1   /* insert leap second */
787 #define TARGET_TIME_DEL      2   /* delete leap second */
788 #define TARGET_TIME_OOP      3   /* leap second in progress */
789 #define TARGET_TIME_WAIT     4   /* leap second has occurred */
790 #define TARGET_TIME_ERROR    5   /* clock not synchronized */
791 #ifdef TARGET_NR_adjtimex
792 static void
793 print_syscall_ret_adjtimex(const struct syscallname *name, abi_long ret,
794                            abi_long arg0, abi_long arg1, abi_long arg2,
795                            abi_long arg3, abi_long arg4, abi_long arg5)
796 {
797     if (!print_syscall_err(ret)) {
798         qemu_log(TARGET_ABI_FMT_ld, ret);
799         switch (ret) {
800         case TARGET_TIME_OK:
801             qemu_log(" TIME_OK (clock synchronized, no leap second)");
802             break;
803         case TARGET_TIME_INS:
804             qemu_log(" TIME_INS (insert leap second)");
805             break;
806         case TARGET_TIME_DEL:
807             qemu_log(" TIME_DEL (delete leap second)");
808             break;
809         case TARGET_TIME_OOP:
810             qemu_log(" TIME_OOP (leap second in progress)");
811             break;
812         case TARGET_TIME_WAIT:
813             qemu_log(" TIME_WAIT (leap second has occurred)");
814             break;
815         case TARGET_TIME_ERROR:
816             qemu_log(" TIME_ERROR (clock not synchronized)");
817             break;
818         }
819     }
820 
821     qemu_log("\n");
822 }
823 #endif
824 
825 #if defined(TARGET_NR_listxattr) || defined(TARGET_NR_llistxattr) \
826  || defined(TARGGET_NR_flistxattr)
827 static void
828 print_syscall_ret_listxattr(const struct syscallname *name, abi_long ret,
829                             abi_long arg0, abi_long arg1, abi_long arg2,
830                             abi_long arg3, abi_long arg4, abi_long arg5)
831 {
832     if (!print_syscall_err(ret)) {
833         qemu_log(TARGET_ABI_FMT_ld, ret);
834         qemu_log(" (list = ");
835         if (arg1 != 0) {
836             abi_long attr = arg1;
837             while (ret) {
838                 if (attr != arg1) {
839                     qemu_log(",");
840                 }
841                 print_string(attr, 1);
842                 ret -= target_strlen(attr) + 1;
843                 attr += target_strlen(attr) + 1;
844             }
845         } else {
846             qemu_log("NULL");
847         }
848         qemu_log(")");
849     }
850 
851     qemu_log("\n");
852 }
853 #define print_syscall_ret_llistxattr     print_syscall_ret_listxattr
854 #define print_syscall_ret_flistxattr     print_syscall_ret_listxattr
855 #endif
856 
857 #ifdef TARGET_NR_ioctl
858 static void
859 print_syscall_ret_ioctl(const struct syscallname *name, abi_long ret,
860                         abi_long arg0, abi_long arg1, abi_long arg2,
861                         abi_long arg3, abi_long arg4, abi_long arg5)
862 {
863     if (!print_syscall_err(ret)) {
864         qemu_log(TARGET_ABI_FMT_ld, ret);
865 
866         const IOCTLEntry *ie;
867         const argtype *arg_type;
868         void *argptr;
869         int target_size;
870 
871         for (ie = ioctl_entries; ie->target_cmd != 0; ie++) {
872             if (ie->target_cmd == arg1) {
873                 break;
874             }
875         }
876 
877         if (ie->target_cmd == arg1 &&
878            (ie->access == IOC_R || ie->access == IOC_RW)) {
879             arg_type = ie->arg_type;
880             qemu_log(" (");
881             arg_type++;
882             target_size = thunk_type_size(arg_type, 0);
883             argptr = lock_user(VERIFY_READ, arg2, target_size, 1);
884             if (argptr) {
885                 thunk_print(argptr, arg_type);
886                 unlock_user(argptr, arg2, target_size);
887             } else {
888                 print_pointer(arg2, 1);
889             }
890             qemu_log(")");
891         }
892     }
893     qemu_log("\n");
894 }
895 #endif
896 
897 UNUSED static struct flags access_flags[] = {
898     FLAG_GENERIC(F_OK),
899     FLAG_GENERIC(R_OK),
900     FLAG_GENERIC(W_OK),
901     FLAG_GENERIC(X_OK),
902     FLAG_END,
903 };
904 
905 UNUSED static struct flags at_file_flags[] = {
906 #ifdef AT_EACCESS
907     FLAG_GENERIC(AT_EACCESS),
908 #endif
909 #ifdef AT_SYMLINK_NOFOLLOW
910     FLAG_GENERIC(AT_SYMLINK_NOFOLLOW),
911 #endif
912     FLAG_END,
913 };
914 
915 UNUSED static struct flags unlinkat_flags[] = {
916 #ifdef AT_REMOVEDIR
917     FLAG_GENERIC(AT_REMOVEDIR),
918 #endif
919     FLAG_END,
920 };
921 
922 UNUSED static struct flags mode_flags[] = {
923     FLAG_GENERIC(S_IFSOCK),
924     FLAG_GENERIC(S_IFLNK),
925     FLAG_GENERIC(S_IFREG),
926     FLAG_GENERIC(S_IFBLK),
927     FLAG_GENERIC(S_IFDIR),
928     FLAG_GENERIC(S_IFCHR),
929     FLAG_GENERIC(S_IFIFO),
930     FLAG_END,
931 };
932 
933 UNUSED static struct flags open_access_flags[] = {
934     FLAG_TARGET(O_RDONLY),
935     FLAG_TARGET(O_WRONLY),
936     FLAG_TARGET(O_RDWR),
937     FLAG_END,
938 };
939 
940 UNUSED static struct flags open_flags[] = {
941     FLAG_TARGET(O_APPEND),
942     FLAG_TARGET(O_CREAT),
943     FLAG_TARGET(O_DIRECTORY),
944     FLAG_TARGET(O_EXCL),
945     FLAG_TARGET(O_LARGEFILE),
946     FLAG_TARGET(O_NOCTTY),
947     FLAG_TARGET(O_NOFOLLOW),
948     FLAG_TARGET(O_NONBLOCK),      /* also O_NDELAY */
949     FLAG_TARGET(O_DSYNC),
950     FLAG_TARGET(__O_SYNC),
951     FLAG_TARGET(O_TRUNC),
952 #ifdef O_DIRECT
953     FLAG_TARGET(O_DIRECT),
954 #endif
955 #ifdef O_NOATIME
956     FLAG_TARGET(O_NOATIME),
957 #endif
958 #ifdef O_CLOEXEC
959     FLAG_TARGET(O_CLOEXEC),
960 #endif
961 #ifdef O_PATH
962     FLAG_TARGET(O_PATH),
963 #endif
964 #ifdef O_TMPFILE
965     FLAG_TARGET(O_TMPFILE),
966     FLAG_TARGET(__O_TMPFILE),
967 #endif
968     FLAG_END,
969 };
970 
971 UNUSED static struct flags mount_flags[] = {
972 #ifdef MS_BIND
973     FLAG_GENERIC(MS_BIND),
974 #endif
975 #ifdef MS_DIRSYNC
976     FLAG_GENERIC(MS_DIRSYNC),
977 #endif
978     FLAG_GENERIC(MS_MANDLOCK),
979 #ifdef MS_MOVE
980     FLAG_GENERIC(MS_MOVE),
981 #endif
982     FLAG_GENERIC(MS_NOATIME),
983     FLAG_GENERIC(MS_NODEV),
984     FLAG_GENERIC(MS_NODIRATIME),
985     FLAG_GENERIC(MS_NOEXEC),
986     FLAG_GENERIC(MS_NOSUID),
987     FLAG_GENERIC(MS_RDONLY),
988 #ifdef MS_RELATIME
989     FLAG_GENERIC(MS_RELATIME),
990 #endif
991     FLAG_GENERIC(MS_REMOUNT),
992     FLAG_GENERIC(MS_SYNCHRONOUS),
993     FLAG_END,
994 };
995 
996 UNUSED static struct flags umount2_flags[] = {
997 #ifdef MNT_FORCE
998     FLAG_GENERIC(MNT_FORCE),
999 #endif
1000 #ifdef MNT_DETACH
1001     FLAG_GENERIC(MNT_DETACH),
1002 #endif
1003 #ifdef MNT_EXPIRE
1004     FLAG_GENERIC(MNT_EXPIRE),
1005 #endif
1006     FLAG_END,
1007 };
1008 
1009 UNUSED static struct flags mmap_prot_flags[] = {
1010     FLAG_GENERIC(PROT_NONE),
1011     FLAG_GENERIC(PROT_EXEC),
1012     FLAG_GENERIC(PROT_READ),
1013     FLAG_GENERIC(PROT_WRITE),
1014     FLAG_TARGET(PROT_SEM),
1015     FLAG_GENERIC(PROT_GROWSDOWN),
1016     FLAG_GENERIC(PROT_GROWSUP),
1017     FLAG_END,
1018 };
1019 
1020 UNUSED static struct flags mmap_flags[] = {
1021     FLAG_TARGET(MAP_SHARED),
1022     FLAG_TARGET(MAP_PRIVATE),
1023     FLAG_TARGET(MAP_ANONYMOUS),
1024     FLAG_TARGET(MAP_DENYWRITE),
1025     FLAG_TARGET(MAP_FIXED),
1026     FLAG_TARGET(MAP_GROWSDOWN),
1027     FLAG_TARGET(MAP_EXECUTABLE),
1028 #ifdef MAP_LOCKED
1029     FLAG_TARGET(MAP_LOCKED),
1030 #endif
1031 #ifdef MAP_NONBLOCK
1032     FLAG_TARGET(MAP_NONBLOCK),
1033 #endif
1034     FLAG_TARGET(MAP_NORESERVE),
1035 #ifdef MAP_POPULATE
1036     FLAG_TARGET(MAP_POPULATE),
1037 #endif
1038 #ifdef TARGET_MAP_UNINITIALIZED
1039     FLAG_TARGET(MAP_UNINITIALIZED),
1040 #endif
1041     FLAG_END,
1042 };
1043 
1044 UNUSED static struct flags clone_flags[] = {
1045     FLAG_GENERIC(CLONE_VM),
1046     FLAG_GENERIC(CLONE_FS),
1047     FLAG_GENERIC(CLONE_FILES),
1048     FLAG_GENERIC(CLONE_SIGHAND),
1049     FLAG_GENERIC(CLONE_PTRACE),
1050     FLAG_GENERIC(CLONE_VFORK),
1051     FLAG_GENERIC(CLONE_PARENT),
1052     FLAG_GENERIC(CLONE_THREAD),
1053     FLAG_GENERIC(CLONE_NEWNS),
1054     FLAG_GENERIC(CLONE_SYSVSEM),
1055     FLAG_GENERIC(CLONE_SETTLS),
1056     FLAG_GENERIC(CLONE_PARENT_SETTID),
1057     FLAG_GENERIC(CLONE_CHILD_CLEARTID),
1058     FLAG_GENERIC(CLONE_DETACHED),
1059     FLAG_GENERIC(CLONE_UNTRACED),
1060     FLAG_GENERIC(CLONE_CHILD_SETTID),
1061 #if defined(CLONE_NEWUTS)
1062     FLAG_GENERIC(CLONE_NEWUTS),
1063 #endif
1064 #if defined(CLONE_NEWIPC)
1065     FLAG_GENERIC(CLONE_NEWIPC),
1066 #endif
1067 #if defined(CLONE_NEWUSER)
1068     FLAG_GENERIC(CLONE_NEWUSER),
1069 #endif
1070 #if defined(CLONE_NEWPID)
1071     FLAG_GENERIC(CLONE_NEWPID),
1072 #endif
1073 #if defined(CLONE_NEWNET)
1074     FLAG_GENERIC(CLONE_NEWNET),
1075 #endif
1076 #if defined(CLONE_IO)
1077     FLAG_GENERIC(CLONE_IO),
1078 #endif
1079     FLAG_END,
1080 };
1081 
1082 UNUSED static struct flags msg_flags[] = {
1083     /* send */
1084     FLAG_GENERIC(MSG_CONFIRM),
1085     FLAG_GENERIC(MSG_DONTROUTE),
1086     FLAG_GENERIC(MSG_DONTWAIT),
1087     FLAG_GENERIC(MSG_EOR),
1088     FLAG_GENERIC(MSG_MORE),
1089     FLAG_GENERIC(MSG_NOSIGNAL),
1090     FLAG_GENERIC(MSG_OOB),
1091     /* recv */
1092     FLAG_GENERIC(MSG_CMSG_CLOEXEC),
1093     FLAG_GENERIC(MSG_ERRQUEUE),
1094     FLAG_GENERIC(MSG_PEEK),
1095     FLAG_GENERIC(MSG_TRUNC),
1096     FLAG_GENERIC(MSG_WAITALL),
1097     /* recvmsg */
1098     FLAG_GENERIC(MSG_CTRUNC),
1099     FLAG_END,
1100 };
1101 
1102 UNUSED static struct flags statx_flags[] = {
1103 #ifdef AT_EMPTY_PATH
1104     FLAG_GENERIC(AT_EMPTY_PATH),
1105 #endif
1106 #ifdef AT_NO_AUTOMOUNT
1107     FLAG_GENERIC(AT_NO_AUTOMOUNT),
1108 #endif
1109 #ifdef AT_SYMLINK_NOFOLLOW
1110     FLAG_GENERIC(AT_SYMLINK_NOFOLLOW),
1111 #endif
1112 #ifdef AT_STATX_SYNC_AS_STAT
1113     FLAG_GENERIC(AT_STATX_SYNC_AS_STAT),
1114 #endif
1115 #ifdef AT_STATX_FORCE_SYNC
1116     FLAG_GENERIC(AT_STATX_FORCE_SYNC),
1117 #endif
1118 #ifdef AT_STATX_DONT_SYNC
1119     FLAG_GENERIC(AT_STATX_DONT_SYNC),
1120 #endif
1121     FLAG_END,
1122 };
1123 
1124 UNUSED static struct flags statx_mask[] = {
1125 /* This must come first, because it includes everything.  */
1126 #ifdef STATX_ALL
1127     FLAG_GENERIC(STATX_ALL),
1128 #endif
1129 /* This must come second; it includes everything except STATX_BTIME.  */
1130 #ifdef STATX_BASIC_STATS
1131     FLAG_GENERIC(STATX_BASIC_STATS),
1132 #endif
1133 #ifdef STATX_TYPE
1134     FLAG_GENERIC(STATX_TYPE),
1135 #endif
1136 #ifdef STATX_MODE
1137     FLAG_GENERIC(STATX_MODE),
1138 #endif
1139 #ifdef STATX_NLINK
1140     FLAG_GENERIC(STATX_NLINK),
1141 #endif
1142 #ifdef STATX_UID
1143     FLAG_GENERIC(STATX_UID),
1144 #endif
1145 #ifdef STATX_GID
1146     FLAG_GENERIC(STATX_GID),
1147 #endif
1148 #ifdef STATX_ATIME
1149     FLAG_GENERIC(STATX_ATIME),
1150 #endif
1151 #ifdef STATX_MTIME
1152     FLAG_GENERIC(STATX_MTIME),
1153 #endif
1154 #ifdef STATX_CTIME
1155     FLAG_GENERIC(STATX_CTIME),
1156 #endif
1157 #ifdef STATX_INO
1158     FLAG_GENERIC(STATX_INO),
1159 #endif
1160 #ifdef STATX_SIZE
1161     FLAG_GENERIC(STATX_SIZE),
1162 #endif
1163 #ifdef STATX_BLOCKS
1164     FLAG_GENERIC(STATX_BLOCKS),
1165 #endif
1166 #ifdef STATX_BTIME
1167     FLAG_GENERIC(STATX_BTIME),
1168 #endif
1169     FLAG_END,
1170 };
1171 
1172 UNUSED static struct flags falloc_flags[] = {
1173     FLAG_GENERIC(FALLOC_FL_KEEP_SIZE),
1174     FLAG_GENERIC(FALLOC_FL_PUNCH_HOLE),
1175 #ifdef FALLOC_FL_NO_HIDE_STALE
1176     FLAG_GENERIC(FALLOC_FL_NO_HIDE_STALE),
1177 #endif
1178 #ifdef FALLOC_FL_COLLAPSE_RANGE
1179     FLAG_GENERIC(FALLOC_FL_COLLAPSE_RANGE),
1180 #endif
1181 #ifdef FALLOC_FL_ZERO_RANGE
1182     FLAG_GENERIC(FALLOC_FL_ZERO_RANGE),
1183 #endif
1184 #ifdef FALLOC_FL_INSERT_RANGE
1185     FLAG_GENERIC(FALLOC_FL_INSERT_RANGE),
1186 #endif
1187 #ifdef FALLOC_FL_UNSHARE_RANGE
1188     FLAG_GENERIC(FALLOC_FL_UNSHARE_RANGE),
1189 #endif
1190 };
1191 
1192 /*
1193  * print_xxx utility functions.  These are used to print syscall
1194  * parameters in certain format.  All of these have parameter
1195  * named 'last'.  This parameter is used to add comma to output
1196  * when last == 0.
1197  */
1198 
1199 static const char *
1200 get_comma(int last)
1201 {
1202     return ((last) ? "" : ",");
1203 }
1204 
1205 static void
1206 print_flags(const struct flags *f, abi_long flags, int last)
1207 {
1208     const char *sep = "";
1209     int n;
1210 
1211     if ((flags == 0) && (f->f_value == 0)) {
1212         qemu_log("%s%s", f->f_string, get_comma(last));
1213         return;
1214     }
1215     for (n = 0; f->f_string != NULL; f++) {
1216         if ((f->f_value != 0) && ((flags & f->f_value) == f->f_value)) {
1217             qemu_log("%s%s", sep, f->f_string);
1218             flags &= ~f->f_value;
1219             sep = "|";
1220             n++;
1221         }
1222     }
1223 
1224     if (n > 0) {
1225         /* print rest of the flags as numeric */
1226         if (flags != 0) {
1227             qemu_log("%s%#x%s", sep, (unsigned int)flags, get_comma(last));
1228         } else {
1229             qemu_log("%s", get_comma(last));
1230         }
1231     } else {
1232         /* no string version of flags found, print them in hex then */
1233         qemu_log("%#x%s", (unsigned int)flags, get_comma(last));
1234     }
1235 }
1236 
1237 static void
1238 print_at_dirfd(abi_long dirfd, int last)
1239 {
1240 #ifdef AT_FDCWD
1241     if (dirfd == AT_FDCWD) {
1242         qemu_log("AT_FDCWD%s", get_comma(last));
1243         return;
1244     }
1245 #endif
1246     qemu_log("%d%s", (int)dirfd, get_comma(last));
1247 }
1248 
1249 static void
1250 print_file_mode(abi_long mode, int last)
1251 {
1252     const char *sep = "";
1253     const struct flags *m;
1254 
1255     for (m = &mode_flags[0]; m->f_string != NULL; m++) {
1256         if ((m->f_value & mode) == m->f_value) {
1257             qemu_log("%s%s", m->f_string, sep);
1258             sep = "|";
1259             mode &= ~m->f_value;
1260             break;
1261         }
1262     }
1263 
1264     mode &= ~S_IFMT;
1265     /* print rest of the mode as octal */
1266     if (mode != 0)
1267         qemu_log("%s%#o", sep, (unsigned int)mode);
1268 
1269     qemu_log("%s", get_comma(last));
1270 }
1271 
1272 static void
1273 print_open_flags(abi_long flags, int last)
1274 {
1275     print_flags(open_access_flags, flags & TARGET_O_ACCMODE, 1);
1276     flags &= ~TARGET_O_ACCMODE;
1277     if (flags == 0) {
1278         qemu_log("%s", get_comma(last));
1279         return;
1280     }
1281     qemu_log("|");
1282     print_flags(open_flags, flags, last);
1283 }
1284 
1285 static void
1286 print_syscall_prologue(const struct syscallname *sc)
1287 {
1288     qemu_log("%s(", sc->name);
1289 }
1290 
1291 /*ARGSUSED*/
1292 static void
1293 print_syscall_epilogue(const struct syscallname *sc)
1294 {
1295     (void)sc;
1296     qemu_log(")");
1297 }
1298 
1299 static void
1300 print_string(abi_long addr, int last)
1301 {
1302     char *s;
1303 
1304     if ((s = lock_user_string(addr)) != NULL) {
1305         qemu_log("\"%s\"%s", s, get_comma(last));
1306         unlock_user(s, addr, 0);
1307     } else {
1308         /* can't get string out of it, so print it as pointer */
1309         print_pointer(addr, last);
1310     }
1311 }
1312 
1313 #define MAX_PRINT_BUF 40
1314 static void
1315 print_buf(abi_long addr, abi_long len, int last)
1316 {
1317     uint8_t *s;
1318     int i;
1319 
1320     s = lock_user(VERIFY_READ, addr, len, 1);
1321     if (s) {
1322         qemu_log("\"");
1323         for (i = 0; i < MAX_PRINT_BUF && i < len; i++) {
1324             if (isprint(s[i])) {
1325                 qemu_log("%c", s[i]);
1326             } else {
1327                 qemu_log("\\%o", s[i]);
1328             }
1329         }
1330         qemu_log("\"");
1331         if (i != len) {
1332             qemu_log("...");
1333         }
1334         if (!last) {
1335             qemu_log(",");
1336         }
1337         unlock_user(s, addr, 0);
1338     } else {
1339         print_pointer(addr, last);
1340     }
1341 }
1342 
1343 /*
1344  * Prints out raw parameter using given format.  Caller needs
1345  * to do byte swapping if needed.
1346  */
1347 static void
1348 print_raw_param(const char *fmt, abi_long param, int last)
1349 {
1350     char format[64];
1351 
1352     (void) snprintf(format, sizeof (format), "%s%s", fmt, get_comma(last));
1353     qemu_log(format, param);
1354 }
1355 
1356 static void
1357 print_pointer(abi_long p, int last)
1358 {
1359     if (p == 0)
1360         qemu_log("NULL%s", get_comma(last));
1361     else
1362         qemu_log("0x" TARGET_ABI_FMT_lx "%s", p, get_comma(last));
1363 }
1364 
1365 /*
1366  * Reads 32-bit (int) number from guest address space from
1367  * address 'addr' and prints it.
1368  */
1369 static void
1370 print_number(abi_long addr, int last)
1371 {
1372     if (addr == 0) {
1373         qemu_log("NULL%s", get_comma(last));
1374     } else {
1375         int num;
1376 
1377         get_user_s32(num, addr);
1378         qemu_log("[%d]%s", num, get_comma(last));
1379     }
1380 }
1381 
1382 static void
1383 print_timeval(abi_ulong tv_addr, int last)
1384 {
1385     if( tv_addr ) {
1386         struct target_timeval *tv;
1387 
1388         tv = lock_user(VERIFY_READ, tv_addr, sizeof(*tv), 1);
1389         if (!tv) {
1390             print_pointer(tv_addr, last);
1391             return;
1392         }
1393         qemu_log("{" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "}%s",
1394             tswapal(tv->tv_sec), tswapal(tv->tv_usec), get_comma(last));
1395         unlock_user(tv, tv_addr, 0);
1396     } else
1397         qemu_log("NULL%s", get_comma(last));
1398 }
1399 
1400 static void
1401 print_timezone(abi_ulong tz_addr, int last)
1402 {
1403     if (tz_addr) {
1404         struct target_timezone *tz;
1405 
1406         tz = lock_user(VERIFY_READ, tz_addr, sizeof(*tz), 1);
1407         if (!tz) {
1408             print_pointer(tz_addr, last);
1409             return;
1410         }
1411         qemu_log("{%d,%d}%s", tswap32(tz->tz_minuteswest),
1412                  tswap32(tz->tz_dsttime), get_comma(last));
1413         unlock_user(tz, tz_addr, 0);
1414     } else {
1415         qemu_log("NULL%s", get_comma(last));
1416     }
1417 }
1418 
1419 #undef UNUSED
1420 
1421 #ifdef TARGET_NR_accept
1422 static void
1423 print_accept(const struct syscallname *name,
1424     abi_long arg0, abi_long arg1, abi_long arg2,
1425     abi_long arg3, abi_long arg4, abi_long arg5)
1426 {
1427     print_syscall_prologue(name);
1428     print_raw_param("%d", arg0, 0);
1429     print_pointer(arg1, 0);
1430     print_number(arg2, 1);
1431     print_syscall_epilogue(name);
1432 }
1433 #endif
1434 
1435 #ifdef TARGET_NR_access
1436 static void
1437 print_access(const struct syscallname *name,
1438     abi_long arg0, abi_long arg1, abi_long arg2,
1439     abi_long arg3, abi_long arg4, abi_long arg5)
1440 {
1441     print_syscall_prologue(name);
1442     print_string(arg0, 0);
1443     print_flags(access_flags, arg1, 1);
1444     print_syscall_epilogue(name);
1445 }
1446 #endif
1447 
1448 #ifdef TARGET_NR_acct
1449 static void
1450 print_acct(const struct syscallname *name,
1451     abi_long arg0, abi_long arg1, abi_long arg2,
1452     abi_long arg3, abi_long arg4, abi_long arg5)
1453 {
1454     print_syscall_prologue(name);
1455     print_string(arg0, 1);
1456     print_syscall_epilogue(name);
1457 }
1458 #endif
1459 
1460 #ifdef TARGET_NR_brk
1461 static void
1462 print_brk(const struct syscallname *name,
1463     abi_long arg0, abi_long arg1, abi_long arg2,
1464     abi_long arg3, abi_long arg4, abi_long arg5)
1465 {
1466     print_syscall_prologue(name);
1467     print_pointer(arg0, 1);
1468     print_syscall_epilogue(name);
1469 }
1470 #endif
1471 
1472 #ifdef TARGET_NR_chdir
1473 static void
1474 print_chdir(const struct syscallname *name,
1475     abi_long arg0, abi_long arg1, abi_long arg2,
1476     abi_long arg3, abi_long arg4, abi_long arg5)
1477 {
1478     print_syscall_prologue(name);
1479     print_string(arg0, 1);
1480     print_syscall_epilogue(name);
1481 }
1482 #endif
1483 
1484 #ifdef TARGET_NR_chroot
1485 static void
1486 print_chroot(const struct syscallname *name,
1487     abi_long arg0, abi_long arg1, abi_long arg2,
1488     abi_long arg3, abi_long arg4, abi_long arg5)
1489 {
1490     print_syscall_prologue(name);
1491     print_string(arg0, 1);
1492     print_syscall_epilogue(name);
1493 }
1494 #endif
1495 
1496 #ifdef TARGET_NR_chmod
1497 static void
1498 print_chmod(const struct syscallname *name,
1499     abi_long arg0, abi_long arg1, abi_long arg2,
1500     abi_long arg3, abi_long arg4, abi_long arg5)
1501 {
1502     print_syscall_prologue(name);
1503     print_string(arg0, 0);
1504     print_file_mode(arg1, 1);
1505     print_syscall_epilogue(name);
1506 }
1507 #endif
1508 
1509 #if defined(TARGET_NR_chown) || defined(TARGET_NR_lchown)
1510 static void
1511 print_chown(const struct syscallname *name,
1512     abi_long arg0, abi_long arg1, abi_long arg2,
1513     abi_long arg3, abi_long arg4, abi_long arg5)
1514 {
1515     print_syscall_prologue(name);
1516     print_string(arg0, 0);
1517     print_raw_param("%d", arg1, 0);
1518     print_raw_param("%d", arg2, 1);
1519     print_syscall_epilogue(name);
1520 }
1521 #define print_lchown     print_chown
1522 #endif
1523 
1524 #ifdef TARGET_NR_clock_adjtime
1525 static void
1526 print_clock_adjtime(const struct syscallname *name,
1527     abi_long arg0, abi_long arg1, abi_long arg2,
1528     abi_long arg3, abi_long arg4, abi_long arg5)
1529 {
1530     print_syscall_prologue(name);
1531     print_clockid(arg0, 0);
1532     print_pointer(arg1, 1);
1533     print_syscall_epilogue(name);
1534 }
1535 #endif
1536 
1537 #ifdef TARGET_NR_clone
1538 static void do_print_clone(unsigned int flags, abi_ulong newsp,
1539                            abi_ulong parent_tidptr, target_ulong newtls,
1540                            abi_ulong child_tidptr)
1541 {
1542     print_flags(clone_flags, flags, 0);
1543     print_raw_param("child_stack=0x" TARGET_ABI_FMT_lx, newsp, 0);
1544     print_raw_param("parent_tidptr=0x" TARGET_ABI_FMT_lx, parent_tidptr, 0);
1545     print_raw_param("tls=0x" TARGET_ABI_FMT_lx, newtls, 0);
1546     print_raw_param("child_tidptr=0x" TARGET_ABI_FMT_lx, child_tidptr, 1);
1547 }
1548 
1549 static void
1550 print_clone(const struct syscallname *name,
1551     abi_long arg1, abi_long arg2, abi_long arg3,
1552     abi_long arg4, abi_long arg5, abi_long arg6)
1553 {
1554     print_syscall_prologue(name);
1555 #if defined(TARGET_MICROBLAZE)
1556     do_print_clone(arg1, arg2, arg4, arg6, arg5);
1557 #elif defined(TARGET_CLONE_BACKWARDS)
1558     do_print_clone(arg1, arg2, arg3, arg4, arg5);
1559 #elif defined(TARGET_CLONE_BACKWARDS2)
1560     do_print_clone(arg2, arg1, arg3, arg5, arg4);
1561 #else
1562     do_print_clone(arg1, arg2, arg3, arg5, arg4);
1563 #endif
1564     print_syscall_epilogue(name);
1565 }
1566 #endif
1567 
1568 #ifdef TARGET_NR_creat
1569 static void
1570 print_creat(const struct syscallname *name,
1571     abi_long arg0, abi_long arg1, abi_long arg2,
1572     abi_long arg3, abi_long arg4, abi_long arg5)
1573 {
1574     print_syscall_prologue(name);
1575     print_string(arg0, 0);
1576     print_file_mode(arg1, 1);
1577     print_syscall_epilogue(name);
1578 }
1579 #endif
1580 
1581 #ifdef TARGET_NR_execv
1582 static void
1583 print_execv(const struct syscallname *name,
1584     abi_long arg0, abi_long arg1, abi_long arg2,
1585     abi_long arg3, abi_long arg4, abi_long arg5)
1586 {
1587     print_syscall_prologue(name);
1588     print_string(arg0, 0);
1589     print_raw_param("0x" TARGET_ABI_FMT_lx, arg1, 1);
1590     print_syscall_epilogue(name);
1591 }
1592 #endif
1593 
1594 #ifdef TARGET_NR_faccessat
1595 static void
1596 print_faccessat(const struct syscallname *name,
1597     abi_long arg0, abi_long arg1, abi_long arg2,
1598     abi_long arg3, abi_long arg4, abi_long arg5)
1599 {
1600     print_syscall_prologue(name);
1601     print_at_dirfd(arg0, 0);
1602     print_string(arg1, 0);
1603     print_flags(access_flags, arg2, 0);
1604     print_flags(at_file_flags, arg3, 1);
1605     print_syscall_epilogue(name);
1606 }
1607 #endif
1608 
1609 #ifdef TARGET_NR_fallocate
1610 static void
1611 print_fallocate(const struct syscallname *name,
1612     abi_long arg0, abi_long arg1, abi_long arg2,
1613     abi_long arg3, abi_long arg4, abi_long arg5)
1614 {
1615     print_syscall_prologue(name);
1616     print_raw_param("%d", arg0, 0);
1617     print_flags(falloc_flags, arg1, 0);
1618 #if TARGET_ABI_BITS == 32
1619     print_raw_param("%" PRIu64, target_offset64(arg2, arg3), 0);
1620     print_raw_param("%" PRIu64, target_offset64(arg4, arg5), 1);
1621 #else
1622     print_raw_param(TARGET_ABI_FMT_ld, arg2, 0);
1623     print_raw_param(TARGET_ABI_FMT_ld, arg3, 1);
1624 #endif
1625     print_syscall_epilogue(name);
1626 }
1627 #endif
1628 
1629 #ifdef TARGET_NR_fchmodat
1630 static void
1631 print_fchmodat(const struct syscallname *name,
1632     abi_long arg0, abi_long arg1, abi_long arg2,
1633     abi_long arg3, abi_long arg4, abi_long arg5)
1634 {
1635     print_syscall_prologue(name);
1636     print_at_dirfd(arg0, 0);
1637     print_string(arg1, 0);
1638     print_file_mode(arg2, 0);
1639     print_flags(at_file_flags, arg3, 1);
1640     print_syscall_epilogue(name);
1641 }
1642 #endif
1643 
1644 #ifdef TARGET_NR_fchownat
1645 static void
1646 print_fchownat(const struct syscallname *name,
1647     abi_long arg0, abi_long arg1, abi_long arg2,
1648     abi_long arg3, abi_long arg4, abi_long arg5)
1649 {
1650     print_syscall_prologue(name);
1651     print_at_dirfd(arg0, 0);
1652     print_string(arg1, 0);
1653     print_raw_param("%d", arg2, 0);
1654     print_raw_param("%d", arg3, 0);
1655     print_flags(at_file_flags, arg4, 1);
1656     print_syscall_epilogue(name);
1657 }
1658 #endif
1659 
1660 #if defined(TARGET_NR_fcntl) || defined(TARGET_NR_fcntl64)
1661 static void
1662 print_fcntl(const struct syscallname *name,
1663     abi_long arg0, abi_long arg1, abi_long arg2,
1664     abi_long arg3, abi_long arg4, abi_long arg5)
1665 {
1666     print_syscall_prologue(name);
1667     print_raw_param("%d", arg0, 0);
1668     switch(arg1) {
1669     case TARGET_F_DUPFD:
1670         qemu_log("F_DUPFD,");
1671         print_raw_param(TARGET_ABI_FMT_ld, arg2, 1);
1672         break;
1673     case TARGET_F_GETFD:
1674         qemu_log("F_GETFD");
1675         break;
1676     case TARGET_F_SETFD:
1677         qemu_log("F_SETFD,");
1678         print_raw_param(TARGET_ABI_FMT_ld, arg2, 1);
1679         break;
1680     case TARGET_F_GETFL:
1681         qemu_log("F_GETFL");
1682         break;
1683     case TARGET_F_SETFL:
1684         qemu_log("F_SETFL,");
1685         print_open_flags(arg2, 1);
1686         break;
1687     case TARGET_F_GETLK:
1688         qemu_log("F_GETLK,");
1689         print_pointer(arg2, 1);
1690         break;
1691     case TARGET_F_SETLK:
1692         qemu_log("F_SETLK,");
1693         print_pointer(arg2, 1);
1694         break;
1695     case TARGET_F_SETLKW:
1696         qemu_log("F_SETLKW,");
1697         print_pointer(arg2, 1);
1698         break;
1699     case TARGET_F_GETOWN:
1700         qemu_log("F_GETOWN");
1701         break;
1702     case TARGET_F_SETOWN:
1703         qemu_log("F_SETOWN,");
1704         print_raw_param(TARGET_ABI_FMT_ld, arg2, 0);
1705         break;
1706     case TARGET_F_GETSIG:
1707         qemu_log("F_GETSIG");
1708         break;
1709     case TARGET_F_SETSIG:
1710         qemu_log("F_SETSIG,");
1711         print_raw_param(TARGET_ABI_FMT_ld, arg2, 0);
1712         break;
1713 #if TARGET_ABI_BITS == 32
1714     case TARGET_F_GETLK64:
1715         qemu_log("F_GETLK64,");
1716         print_pointer(arg2, 1);
1717         break;
1718     case TARGET_F_SETLK64:
1719         qemu_log("F_SETLK64,");
1720         print_pointer(arg2, 1);
1721         break;
1722     case TARGET_F_SETLKW64:
1723         qemu_log("F_SETLKW64,");
1724         print_pointer(arg2, 1);
1725         break;
1726 #endif
1727     case TARGET_F_SETLEASE:
1728         qemu_log("F_SETLEASE,");
1729         print_raw_param(TARGET_ABI_FMT_ld, arg2, 0);
1730         break;
1731     case TARGET_F_GETLEASE:
1732         qemu_log("F_GETLEASE");
1733         break;
1734     case TARGET_F_SETPIPE_SZ:
1735         qemu_log("F_SETPIPE_SZ,");
1736         print_raw_param(TARGET_ABI_FMT_ld, arg2, 1);
1737         break;
1738     case TARGET_F_GETPIPE_SZ:
1739         qemu_log("F_GETPIPE_SZ");
1740         break;
1741     case TARGET_F_DUPFD_CLOEXEC:
1742         qemu_log("F_DUPFD_CLOEXEC,");
1743         print_raw_param(TARGET_ABI_FMT_ld, arg2, 1);
1744         break;
1745     case TARGET_F_NOTIFY:
1746         qemu_log("F_NOTIFY,");
1747         print_raw_param(TARGET_ABI_FMT_ld, arg2, 0);
1748         break;
1749     default:
1750         print_raw_param(TARGET_ABI_FMT_ld, arg1, 0);
1751         print_pointer(arg2, 1);
1752         break;
1753     }
1754     print_syscall_epilogue(name);
1755 }
1756 #define print_fcntl64   print_fcntl
1757 #endif
1758 
1759 #ifdef TARGET_NR_fgetxattr
1760 static void
1761 print_fgetxattr(const struct syscallname *name,
1762     abi_long arg0, abi_long arg1, abi_long arg2,
1763     abi_long arg3, abi_long arg4, abi_long arg5)
1764 {
1765     print_syscall_prologue(name);
1766     print_raw_param("%d", arg0, 0);
1767     print_string(arg1, 0);
1768     print_pointer(arg2, 0);
1769     print_raw_param(TARGET_FMT_lu, arg3, 1);
1770     print_syscall_epilogue(name);
1771 }
1772 #endif
1773 
1774 #ifdef TARGET_NR_flistxattr
1775 static void
1776 print_flistxattr(const struct syscallname *name,
1777     abi_long arg0, abi_long arg1, abi_long arg2,
1778     abi_long arg3, abi_long arg4, abi_long arg5)
1779 {
1780     print_syscall_prologue(name);
1781     print_raw_param("%d", arg0, 0);
1782     print_pointer(arg1, 0);
1783     print_raw_param(TARGET_FMT_lu, arg2, 1);
1784     print_syscall_epilogue(name);
1785 }
1786 #endif
1787 
1788 #if defined(TARGET_NR_getxattr) || defined(TARGET_NR_lgetxattr)
1789 static void
1790 print_getxattr(const struct syscallname *name,
1791     abi_long arg0, abi_long arg1, abi_long arg2,
1792     abi_long arg3, abi_long arg4, abi_long arg5)
1793 {
1794     print_syscall_prologue(name);
1795     print_string(arg0, 0);
1796     print_string(arg1, 0);
1797     print_pointer(arg2, 0);
1798     print_raw_param(TARGET_FMT_lu, arg3, 1);
1799     print_syscall_epilogue(name);
1800 }
1801 #define print_lgetxattr     print_getxattr
1802 #endif
1803 
1804 #if defined(TARGET_NR_listxattr) || defined(TARGET_NR_llistxattr)
1805 static void
1806 print_listxattr(const struct syscallname *name,
1807     abi_long arg0, abi_long arg1, abi_long arg2,
1808     abi_long arg3, abi_long arg4, abi_long arg5)
1809 {
1810     print_syscall_prologue(name);
1811     print_string(arg0, 0);
1812     print_pointer(arg1, 0);
1813     print_raw_param(TARGET_FMT_lu, arg2, 1);
1814     print_syscall_epilogue(name);
1815 }
1816 #define print_llistxattr     print_listxattr
1817 #endif
1818 
1819 #if defined(TARGET_NR_fremovexattr)
1820 static void
1821 print_fremovexattr(const struct syscallname *name,
1822     abi_long arg0, abi_long arg1, abi_long arg2,
1823     abi_long arg3, abi_long arg4, abi_long arg5)
1824 {
1825     print_syscall_prologue(name);
1826     print_raw_param("%d", arg0, 0);
1827     print_string(arg1, 1);
1828     print_syscall_epilogue(name);
1829 }
1830 #endif
1831 
1832 #if defined(TARGET_NR_removexattr) || defined(TARGET_NR_lremovexattr)
1833 static void
1834 print_removexattr(const struct syscallname *name,
1835     abi_long arg0, abi_long arg1, abi_long arg2,
1836     abi_long arg3, abi_long arg4, abi_long arg5)
1837 {
1838     print_syscall_prologue(name);
1839     print_string(arg0, 0);
1840     print_string(arg1, 1);
1841     print_syscall_epilogue(name);
1842 }
1843 #define print_lremovexattr     print_removexattr
1844 #endif
1845 
1846 #ifdef TARGET_NR_futimesat
1847 static void
1848 print_futimesat(const struct syscallname *name,
1849     abi_long arg0, abi_long arg1, abi_long arg2,
1850     abi_long arg3, abi_long arg4, abi_long arg5)
1851 {
1852     print_syscall_prologue(name);
1853     print_at_dirfd(arg0, 0);
1854     print_string(arg1, 0);
1855     print_timeval(arg2, 0);
1856     print_timeval(arg2 + sizeof (struct target_timeval), 1);
1857     print_syscall_epilogue(name);
1858 }
1859 #endif
1860 
1861 #ifdef TARGET_NR_settimeofday
1862 static void
1863 print_settimeofday(const struct syscallname *name,
1864                 abi_long arg0, abi_long arg1, abi_long arg2,
1865                 abi_long arg3, abi_long arg4, abi_long arg5)
1866 {
1867     print_syscall_prologue(name);
1868     print_timeval(arg0, 0);
1869     print_timezone(arg1, 1);
1870     print_syscall_epilogue(name);
1871 }
1872 #endif
1873 
1874 #ifdef TARGET_NR_link
1875 static void
1876 print_link(const struct syscallname *name,
1877     abi_long arg0, abi_long arg1, abi_long arg2,
1878     abi_long arg3, abi_long arg4, abi_long arg5)
1879 {
1880     print_syscall_prologue(name);
1881     print_string(arg0, 0);
1882     print_string(arg1, 1);
1883     print_syscall_epilogue(name);
1884 }
1885 #endif
1886 
1887 #ifdef TARGET_NR_linkat
1888 static void
1889 print_linkat(const struct syscallname *name,
1890     abi_long arg0, abi_long arg1, abi_long arg2,
1891     abi_long arg3, abi_long arg4, abi_long arg5)
1892 {
1893     print_syscall_prologue(name);
1894     print_at_dirfd(arg0, 0);
1895     print_string(arg1, 0);
1896     print_at_dirfd(arg2, 0);
1897     print_string(arg3, 0);
1898     print_flags(at_file_flags, arg4, 1);
1899     print_syscall_epilogue(name);
1900 }
1901 #endif
1902 
1903 #ifdef TARGET_NR__llseek
1904 static void
1905 print__llseek(const struct syscallname *name,
1906     abi_long arg0, abi_long arg1, abi_long arg2,
1907     abi_long arg3, abi_long arg4, abi_long arg5)
1908 {
1909     const char *whence = "UNKNOWN";
1910     print_syscall_prologue(name);
1911     print_raw_param("%d", arg0, 0);
1912     print_raw_param("%ld", arg1, 0);
1913     print_raw_param("%ld", arg2, 0);
1914     print_pointer(arg3, 0);
1915     switch(arg4) {
1916     case SEEK_SET: whence = "SEEK_SET"; break;
1917     case SEEK_CUR: whence = "SEEK_CUR"; break;
1918     case SEEK_END: whence = "SEEK_END"; break;
1919     }
1920     qemu_log("%s", whence);
1921     print_syscall_epilogue(name);
1922 }
1923 #endif
1924 
1925 #ifdef TARGET_NR_lseek
1926 static void
1927 print_lseek(const struct syscallname *name,
1928     abi_long arg0, abi_long arg1, abi_long arg2,
1929     abi_long arg3, abi_long arg4, abi_long arg5)
1930 {
1931     print_syscall_prologue(name);
1932     print_raw_param("%d", arg0, 0);
1933     print_raw_param(TARGET_ABI_FMT_ld, arg1, 0);
1934     switch (arg2) {
1935     case SEEK_SET:
1936         qemu_log("SEEK_SET"); break;
1937     case SEEK_CUR:
1938         qemu_log("SEEK_CUR"); break;
1939     case SEEK_END:
1940         qemu_log("SEEK_END"); break;
1941 #ifdef SEEK_DATA
1942     case SEEK_DATA:
1943         qemu_log("SEEK_DATA"); break;
1944 #endif
1945 #ifdef SEEK_HOLE
1946     case SEEK_HOLE:
1947         qemu_log("SEEK_HOLE"); break;
1948 #endif
1949     default:
1950         print_raw_param("%#x", arg2, 1);
1951     }
1952     print_syscall_epilogue(name);
1953 }
1954 #endif
1955 
1956 #if defined(TARGET_NR_socket)
1957 static void
1958 print_socket(const struct syscallname *name,
1959              abi_long arg0, abi_long arg1, abi_long arg2,
1960              abi_long arg3, abi_long arg4, abi_long arg5)
1961 {
1962     abi_ulong domain = arg0, type = arg1, protocol = arg2;
1963 
1964     print_syscall_prologue(name);
1965     print_socket_domain(domain);
1966     qemu_log(",");
1967     print_socket_type(type);
1968     qemu_log(",");
1969     if (domain == AF_PACKET ||
1970         (domain == AF_INET && type == TARGET_SOCK_PACKET)) {
1971         protocol = tswap16(protocol);
1972     }
1973     print_socket_protocol(domain, type, protocol);
1974     print_syscall_epilogue(name);
1975 }
1976 
1977 #endif
1978 
1979 #if defined(TARGET_NR_socketcall) || defined(TARGET_NR_bind)
1980 
1981 static void print_sockfd(abi_long sockfd, int last)
1982 {
1983     print_raw_param(TARGET_ABI_FMT_ld, sockfd, last);
1984 }
1985 
1986 #endif
1987 
1988 #if defined(TARGET_NR_socketcall)
1989 
1990 #define get_user_ualx(x, gaddr, idx) \
1991         get_user_ual(x, (gaddr) + (idx) * sizeof(abi_long))
1992 
1993 static void do_print_socket(const char *name, abi_long arg1)
1994 {
1995     abi_ulong domain, type, protocol;
1996 
1997     get_user_ualx(domain, arg1, 0);
1998     get_user_ualx(type, arg1, 1);
1999     get_user_ualx(protocol, arg1, 2);
2000     qemu_log("%s(", name);
2001     print_socket_domain(domain);
2002     qemu_log(",");
2003     print_socket_type(type);
2004     qemu_log(",");
2005     if (domain == AF_PACKET ||
2006         (domain == AF_INET && type == TARGET_SOCK_PACKET)) {
2007         protocol = tswap16(protocol);
2008     }
2009     print_socket_protocol(domain, type, protocol);
2010     qemu_log(")");
2011 }
2012 
2013 static void do_print_sockaddr(const char *name, abi_long arg1)
2014 {
2015     abi_ulong sockfd, addr, addrlen;
2016 
2017     get_user_ualx(sockfd, arg1, 0);
2018     get_user_ualx(addr, arg1, 1);
2019     get_user_ualx(addrlen, arg1, 2);
2020 
2021     qemu_log("%s(", name);
2022     print_sockfd(sockfd, 0);
2023     print_sockaddr(addr, addrlen, 0);
2024     qemu_log(")");
2025 }
2026 
2027 static void do_print_listen(const char *name, abi_long arg1)
2028 {
2029     abi_ulong sockfd, backlog;
2030 
2031     get_user_ualx(sockfd, arg1, 0);
2032     get_user_ualx(backlog, arg1, 1);
2033 
2034     qemu_log("%s(", name);
2035     print_sockfd(sockfd, 0);
2036     print_raw_param(TARGET_ABI_FMT_ld, backlog, 1);
2037     qemu_log(")");
2038 }
2039 
2040 static void do_print_socketpair(const char *name, abi_long arg1)
2041 {
2042     abi_ulong domain, type, protocol, tab;
2043 
2044     get_user_ualx(domain, arg1, 0);
2045     get_user_ualx(type, arg1, 1);
2046     get_user_ualx(protocol, arg1, 2);
2047     get_user_ualx(tab, arg1, 3);
2048 
2049     qemu_log("%s(", name);
2050     print_socket_domain(domain);
2051     qemu_log(",");
2052     print_socket_type(type);
2053     qemu_log(",");
2054     print_socket_protocol(domain, type, protocol);
2055     qemu_log(",");
2056     print_raw_param(TARGET_ABI_FMT_lx, tab, 1);
2057     qemu_log(")");
2058 }
2059 
2060 static void do_print_sendrecv(const char *name, abi_long arg1)
2061 {
2062     abi_ulong sockfd, msg, len, flags;
2063 
2064     get_user_ualx(sockfd, arg1, 0);
2065     get_user_ualx(msg, arg1, 1);
2066     get_user_ualx(len, arg1, 2);
2067     get_user_ualx(flags, arg1, 3);
2068 
2069     qemu_log("%s(", name);
2070     print_sockfd(sockfd, 0);
2071     print_buf(msg, len, 0);
2072     print_raw_param(TARGET_ABI_FMT_ld, len, 0);
2073     print_flags(msg_flags, flags, 1);
2074     qemu_log(")");
2075 }
2076 
2077 static void do_print_msgaddr(const char *name, abi_long arg1)
2078 {
2079     abi_ulong sockfd, msg, len, flags, addr, addrlen;
2080 
2081     get_user_ualx(sockfd, arg1, 0);
2082     get_user_ualx(msg, arg1, 1);
2083     get_user_ualx(len, arg1, 2);
2084     get_user_ualx(flags, arg1, 3);
2085     get_user_ualx(addr, arg1, 4);
2086     get_user_ualx(addrlen, arg1, 5);
2087 
2088     qemu_log("%s(", name);
2089     print_sockfd(sockfd, 0);
2090     print_buf(msg, len, 0);
2091     print_raw_param(TARGET_ABI_FMT_ld, len, 0);
2092     print_flags(msg_flags, flags, 0);
2093     print_sockaddr(addr, addrlen, 0);
2094     qemu_log(")");
2095 }
2096 
2097 static void do_print_shutdown(const char *name, abi_long arg1)
2098 {
2099     abi_ulong sockfd, how;
2100 
2101     get_user_ualx(sockfd, arg1, 0);
2102     get_user_ualx(how, arg1, 1);
2103 
2104     qemu_log("shutdown(");
2105     print_sockfd(sockfd, 0);
2106     switch (how) {
2107     case SHUT_RD:
2108         qemu_log("SHUT_RD");
2109         break;
2110     case SHUT_WR:
2111         qemu_log("SHUT_WR");
2112         break;
2113     case SHUT_RDWR:
2114         qemu_log("SHUT_RDWR");
2115         break;
2116     default:
2117         print_raw_param(TARGET_ABI_FMT_ld, how, 1);
2118         break;
2119     }
2120     qemu_log(")");
2121 }
2122 
2123 static void do_print_msg(const char *name, abi_long arg1)
2124 {
2125     abi_ulong sockfd, msg, flags;
2126 
2127     get_user_ualx(sockfd, arg1, 0);
2128     get_user_ualx(msg, arg1, 1);
2129     get_user_ualx(flags, arg1, 2);
2130 
2131     qemu_log("%s(", name);
2132     print_sockfd(sockfd, 0);
2133     print_pointer(msg, 0);
2134     print_flags(msg_flags, flags, 1);
2135     qemu_log(")");
2136 }
2137 
2138 static void do_print_sockopt(const char *name, abi_long arg1)
2139 {
2140     abi_ulong sockfd, level, optname, optval, optlen;
2141 
2142     get_user_ualx(sockfd, arg1, 0);
2143     get_user_ualx(level, arg1, 1);
2144     get_user_ualx(optname, arg1, 2);
2145     get_user_ualx(optval, arg1, 3);
2146     get_user_ualx(optlen, arg1, 4);
2147 
2148     qemu_log("%s(", name);
2149     print_sockfd(sockfd, 0);
2150     switch (level) {
2151     case SOL_TCP:
2152         qemu_log("SOL_TCP,");
2153         print_raw_param(TARGET_ABI_FMT_ld, optname, 0);
2154         print_pointer(optval, 0);
2155         break;
2156     case SOL_IP:
2157         qemu_log("SOL_IP,");
2158         print_raw_param(TARGET_ABI_FMT_ld, optname, 0);
2159         print_pointer(optval, 0);
2160         break;
2161     case SOL_RAW:
2162         qemu_log("SOL_RAW,");
2163         print_raw_param(TARGET_ABI_FMT_ld, optname, 0);
2164         print_pointer(optval, 0);
2165         break;
2166     case TARGET_SOL_SOCKET:
2167         qemu_log("SOL_SOCKET,");
2168         switch (optname) {
2169         case TARGET_SO_DEBUG:
2170             qemu_log("SO_DEBUG,");
2171 print_optint:
2172             print_number(optval, 0);
2173             break;
2174         case TARGET_SO_REUSEADDR:
2175             qemu_log("SO_REUSEADDR,");
2176             goto print_optint;
2177         case TARGET_SO_REUSEPORT:
2178             qemu_log("SO_REUSEPORT,");
2179             goto print_optint;
2180         case TARGET_SO_TYPE:
2181             qemu_log("SO_TYPE,");
2182             goto print_optint;
2183         case TARGET_SO_ERROR:
2184             qemu_log("SO_ERROR,");
2185             goto print_optint;
2186         case TARGET_SO_DONTROUTE:
2187             qemu_log("SO_DONTROUTE,");
2188             goto print_optint;
2189         case TARGET_SO_BROADCAST:
2190             qemu_log("SO_BROADCAST,");
2191             goto print_optint;
2192         case TARGET_SO_SNDBUF:
2193             qemu_log("SO_SNDBUF,");
2194             goto print_optint;
2195         case TARGET_SO_RCVBUF:
2196             qemu_log("SO_RCVBUF,");
2197             goto print_optint;
2198         case TARGET_SO_KEEPALIVE:
2199             qemu_log("SO_KEEPALIVE,");
2200             goto print_optint;
2201         case TARGET_SO_OOBINLINE:
2202             qemu_log("SO_OOBINLINE,");
2203             goto print_optint;
2204         case TARGET_SO_NO_CHECK:
2205             qemu_log("SO_NO_CHECK,");
2206             goto print_optint;
2207         case TARGET_SO_PRIORITY:
2208             qemu_log("SO_PRIORITY,");
2209             goto print_optint;
2210         case TARGET_SO_BSDCOMPAT:
2211             qemu_log("SO_BSDCOMPAT,");
2212             goto print_optint;
2213         case TARGET_SO_PASSCRED:
2214             qemu_log("SO_PASSCRED,");
2215             goto print_optint;
2216         case TARGET_SO_TIMESTAMP:
2217             qemu_log("SO_TIMESTAMP,");
2218             goto print_optint;
2219         case TARGET_SO_RCVLOWAT:
2220             qemu_log("SO_RCVLOWAT,");
2221             goto print_optint;
2222         case TARGET_SO_RCVTIMEO:
2223             qemu_log("SO_RCVTIMEO,");
2224             print_timeval(optval, 0);
2225             break;
2226         case TARGET_SO_SNDTIMEO:
2227             qemu_log("SO_SNDTIMEO,");
2228             print_timeval(optval, 0);
2229             break;
2230         case TARGET_SO_ATTACH_FILTER: {
2231             struct target_sock_fprog *fprog;
2232 
2233             qemu_log("SO_ATTACH_FILTER,");
2234 
2235             if (lock_user_struct(VERIFY_READ, fprog, optval,  0)) {
2236                 struct target_sock_filter *filter;
2237                 qemu_log("{");
2238                 if (lock_user_struct(VERIFY_READ, filter,
2239                                      tswapal(fprog->filter),  0)) {
2240                     int i;
2241                     for (i = 0; i < tswap16(fprog->len) - 1; i++) {
2242                         qemu_log("[%d]{0x%x,%d,%d,0x%x},",
2243                                  i, tswap16(filter[i].code),
2244                                  filter[i].jt, filter[i].jf,
2245                                  tswap32(filter[i].k));
2246                     }
2247                     qemu_log("[%d]{0x%x,%d,%d,0x%x}",
2248                              i, tswap16(filter[i].code),
2249                              filter[i].jt, filter[i].jf,
2250                              tswap32(filter[i].k));
2251                 } else {
2252                     qemu_log(TARGET_ABI_FMT_lx, tswapal(fprog->filter));
2253                 }
2254                 qemu_log(",%d},", tswap16(fprog->len));
2255                 unlock_user(fprog, optval, 0);
2256             } else {
2257                 print_pointer(optval, 0);
2258             }
2259             break;
2260         }
2261         default:
2262             print_raw_param(TARGET_ABI_FMT_ld, optname, 0);
2263             print_pointer(optval, 0);
2264             break;
2265         }
2266         break;
2267     default:
2268         print_raw_param(TARGET_ABI_FMT_ld, level, 0);
2269         print_raw_param(TARGET_ABI_FMT_ld, optname, 0);
2270         print_pointer(optval, 0);
2271         break;
2272     }
2273     print_raw_param(TARGET_ABI_FMT_ld, optlen, 1);
2274     qemu_log(")");
2275 }
2276 
2277 #define PRINT_SOCKOP(name, func) \
2278     [TARGET_SYS_##name] = { #name, func }
2279 
2280 static struct {
2281     const char *name;
2282     void (*print)(const char *, abi_long);
2283 } scall[] = {
2284     PRINT_SOCKOP(SOCKET, do_print_socket),
2285     PRINT_SOCKOP(BIND, do_print_sockaddr),
2286     PRINT_SOCKOP(CONNECT, do_print_sockaddr),
2287     PRINT_SOCKOP(LISTEN, do_print_listen),
2288     PRINT_SOCKOP(ACCEPT, do_print_sockaddr),
2289     PRINT_SOCKOP(GETSOCKNAME, do_print_sockaddr),
2290     PRINT_SOCKOP(GETPEERNAME, do_print_sockaddr),
2291     PRINT_SOCKOP(SOCKETPAIR, do_print_socketpair),
2292     PRINT_SOCKOP(SEND, do_print_sendrecv),
2293     PRINT_SOCKOP(RECV, do_print_sendrecv),
2294     PRINT_SOCKOP(SENDTO, do_print_msgaddr),
2295     PRINT_SOCKOP(RECVFROM, do_print_msgaddr),
2296     PRINT_SOCKOP(SHUTDOWN, do_print_shutdown),
2297     PRINT_SOCKOP(SETSOCKOPT, do_print_sockopt),
2298     PRINT_SOCKOP(GETSOCKOPT, do_print_sockopt),
2299     PRINT_SOCKOP(SENDMSG, do_print_msg),
2300     PRINT_SOCKOP(RECVMSG, do_print_msg),
2301     PRINT_SOCKOP(ACCEPT4, NULL),
2302     PRINT_SOCKOP(RECVMMSG, NULL),
2303     PRINT_SOCKOP(SENDMMSG, NULL),
2304 };
2305 
2306 static void
2307 print_socketcall(const struct syscallname *name,
2308                  abi_long arg0, abi_long arg1, abi_long arg2,
2309                  abi_long arg3, abi_long arg4, abi_long arg5)
2310 {
2311     if (arg0 >= 0 && arg0 < ARRAY_SIZE(scall) && scall[arg0].print) {
2312         scall[arg0].print(scall[arg0].name, arg1);
2313         return;
2314     }
2315     print_syscall_prologue(name);
2316     print_raw_param(TARGET_ABI_FMT_ld, arg0, 0);
2317     print_raw_param(TARGET_ABI_FMT_ld, arg1, 0);
2318     print_raw_param(TARGET_ABI_FMT_ld, arg2, 0);
2319     print_raw_param(TARGET_ABI_FMT_ld, arg3, 0);
2320     print_raw_param(TARGET_ABI_FMT_ld, arg4, 0);
2321     print_raw_param(TARGET_ABI_FMT_ld, arg5, 0);
2322     print_syscall_epilogue(name);
2323 }
2324 #endif
2325 
2326 #if defined(TARGET_NR_bind)
2327 static void
2328 print_bind(const struct syscallname *name,
2329            abi_long arg0, abi_long arg1, abi_long arg2,
2330            abi_long arg3, abi_long arg4, abi_long arg5)
2331 {
2332     print_syscall_prologue(name);
2333     print_sockfd(arg0, 0);
2334     print_sockaddr(arg1, arg2, 1);
2335     print_syscall_epilogue(name);
2336 }
2337 #endif
2338 
2339 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) || \
2340     defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64)
2341 static void
2342 print_stat(const struct syscallname *name,
2343     abi_long arg0, abi_long arg1, abi_long arg2,
2344     abi_long arg3, abi_long arg4, abi_long arg5)
2345 {
2346     print_syscall_prologue(name);
2347     print_string(arg0, 0);
2348     print_pointer(arg1, 1);
2349     print_syscall_epilogue(name);
2350 }
2351 #define print_lstat     print_stat
2352 #define print_stat64	print_stat
2353 #define print_lstat64   print_stat
2354 #endif
2355 
2356 #if defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64)
2357 static void
2358 print_fstat(const struct syscallname *name,
2359     abi_long arg0, abi_long arg1, abi_long arg2,
2360     abi_long arg3, abi_long arg4, abi_long arg5)
2361 {
2362     print_syscall_prologue(name);
2363     print_raw_param("%d", arg0, 0);
2364     print_pointer(arg1, 1);
2365     print_syscall_epilogue(name);
2366 }
2367 #define print_fstat64     print_fstat
2368 #endif
2369 
2370 #ifdef TARGET_NR_mkdir
2371 static void
2372 print_mkdir(const struct syscallname *name,
2373     abi_long arg0, abi_long arg1, abi_long arg2,
2374     abi_long arg3, abi_long arg4, abi_long arg5)
2375 {
2376     print_syscall_prologue(name);
2377     print_string(arg0, 0);
2378     print_file_mode(arg1, 1);
2379     print_syscall_epilogue(name);
2380 }
2381 #endif
2382 
2383 #ifdef TARGET_NR_mkdirat
2384 static void
2385 print_mkdirat(const struct syscallname *name,
2386     abi_long arg0, abi_long arg1, abi_long arg2,
2387     abi_long arg3, abi_long arg4, abi_long arg5)
2388 {
2389     print_syscall_prologue(name);
2390     print_at_dirfd(arg0, 0);
2391     print_string(arg1, 0);
2392     print_file_mode(arg2, 1);
2393     print_syscall_epilogue(name);
2394 }
2395 #endif
2396 
2397 #ifdef TARGET_NR_rmdir
2398 static void
2399 print_rmdir(const struct syscallname *name,
2400     abi_long arg0, abi_long arg1, abi_long arg2,
2401     abi_long arg3, abi_long arg4, abi_long arg5)
2402 {
2403     print_syscall_prologue(name);
2404     print_string(arg0, 0);
2405     print_syscall_epilogue(name);
2406 }
2407 #endif
2408 
2409 #ifdef TARGET_NR_rt_sigaction
2410 static void
2411 print_rt_sigaction(const struct syscallname *name,
2412     abi_long arg0, abi_long arg1, abi_long arg2,
2413     abi_long arg3, abi_long arg4, abi_long arg5)
2414 {
2415     print_syscall_prologue(name);
2416     print_signal(arg0, 0);
2417     print_pointer(arg1, 0);
2418     print_pointer(arg2, 1);
2419     print_syscall_epilogue(name);
2420 }
2421 #endif
2422 
2423 #ifdef TARGET_NR_rt_sigprocmask
2424 static void
2425 print_rt_sigprocmask(const struct syscallname *name,
2426     abi_long arg0, abi_long arg1, abi_long arg2,
2427     abi_long arg3, abi_long arg4, abi_long arg5)
2428 {
2429     const char *how = "UNKNOWN";
2430     print_syscall_prologue(name);
2431     switch(arg0) {
2432     case TARGET_SIG_BLOCK: how = "SIG_BLOCK"; break;
2433     case TARGET_SIG_UNBLOCK: how = "SIG_UNBLOCK"; break;
2434     case TARGET_SIG_SETMASK: how = "SIG_SETMASK"; break;
2435     }
2436     qemu_log("%s,", how);
2437     print_pointer(arg1, 0);
2438     print_pointer(arg2, 1);
2439     print_syscall_epilogue(name);
2440 }
2441 #endif
2442 
2443 #ifdef TARGET_NR_rt_sigqueueinfo
2444 static void
2445 print_rt_sigqueueinfo(const struct syscallname *name,
2446     abi_long arg0, abi_long arg1, abi_long arg2,
2447     abi_long arg3, abi_long arg4, abi_long arg5)
2448 {
2449     void *p;
2450     target_siginfo_t uinfo;
2451 
2452     print_syscall_prologue(name);
2453     print_raw_param("%d", arg0, 0);
2454     print_signal(arg1, 0);
2455     p = lock_user(VERIFY_READ, arg2, sizeof(target_siginfo_t), 1);
2456     if (p) {
2457         get_target_siginfo(&uinfo, p);
2458         print_siginfo(&uinfo);
2459 
2460         unlock_user(p, arg2, 0);
2461     } else {
2462         print_pointer(arg2, 1);
2463     }
2464     print_syscall_epilogue(name);
2465 }
2466 #endif
2467 
2468 #ifdef TARGET_NR_rt_tgsigqueueinfo
2469 static void
2470 print_rt_tgsigqueueinfo(const struct syscallname *name,
2471     abi_long arg0, abi_long arg1, abi_long arg2,
2472     abi_long arg3, abi_long arg4, abi_long arg5)
2473 {
2474     void *p;
2475     target_siginfo_t uinfo;
2476 
2477     print_syscall_prologue(name);
2478     print_raw_param("%d", arg0, 0);
2479     print_raw_param("%d", arg1, 0);
2480     print_signal(arg2, 0);
2481     p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
2482     if (p) {
2483         get_target_siginfo(&uinfo, p);
2484         print_siginfo(&uinfo);
2485 
2486         unlock_user(p, arg3, 0);
2487     } else {
2488         print_pointer(arg3, 1);
2489     }
2490     print_syscall_epilogue(name);
2491 }
2492 #endif
2493 
2494 #ifdef TARGET_NR_syslog
2495 static void
2496 print_syslog_action(abi_ulong arg, int last)
2497 {
2498     const char *type;
2499 
2500     switch (arg) {
2501         case TARGET_SYSLOG_ACTION_CLOSE: {
2502             type = "SYSLOG_ACTION_CLOSE";
2503             break;
2504         }
2505         case TARGET_SYSLOG_ACTION_OPEN: {
2506             type = "SYSLOG_ACTION_OPEN";
2507             break;
2508         }
2509         case TARGET_SYSLOG_ACTION_READ: {
2510             type = "SYSLOG_ACTION_READ";
2511             break;
2512         }
2513         case TARGET_SYSLOG_ACTION_READ_ALL: {
2514             type = "SYSLOG_ACTION_READ_ALL";
2515             break;
2516         }
2517         case TARGET_SYSLOG_ACTION_READ_CLEAR: {
2518             type = "SYSLOG_ACTION_READ_CLEAR";
2519             break;
2520         }
2521         case TARGET_SYSLOG_ACTION_CLEAR: {
2522             type = "SYSLOG_ACTION_CLEAR";
2523             break;
2524         }
2525         case TARGET_SYSLOG_ACTION_CONSOLE_OFF: {
2526             type = "SYSLOG_ACTION_CONSOLE_OFF";
2527             break;
2528         }
2529         case TARGET_SYSLOG_ACTION_CONSOLE_ON: {
2530             type = "SYSLOG_ACTION_CONSOLE_ON";
2531             break;
2532         }
2533         case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: {
2534             type = "SYSLOG_ACTION_CONSOLE_LEVEL";
2535             break;
2536         }
2537         case TARGET_SYSLOG_ACTION_SIZE_UNREAD: {
2538             type = "SYSLOG_ACTION_SIZE_UNREAD";
2539             break;
2540         }
2541         case TARGET_SYSLOG_ACTION_SIZE_BUFFER: {
2542             type = "SYSLOG_ACTION_SIZE_BUFFER";
2543             break;
2544         }
2545         default: {
2546             print_raw_param("%ld", arg, last);
2547             return;
2548         }
2549     }
2550     qemu_log("%s%s", type, get_comma(last));
2551 }
2552 
2553 static void
2554 print_syslog(const struct syscallname *name,
2555     abi_long arg0, abi_long arg1, abi_long arg2,
2556     abi_long arg3, abi_long arg4, abi_long arg5)
2557 {
2558     print_syscall_prologue(name);
2559     print_syslog_action(arg0, 0);
2560     print_pointer(arg1, 0);
2561     print_raw_param("%d", arg2, 1);
2562     print_syscall_epilogue(name);
2563 }
2564 #endif
2565 
2566 #ifdef TARGET_NR_mknod
2567 static void
2568 print_mknod(const struct syscallname *name,
2569     abi_long arg0, abi_long arg1, abi_long arg2,
2570     abi_long arg3, abi_long arg4, abi_long arg5)
2571 {
2572     int hasdev = (arg1 & (S_IFCHR|S_IFBLK));
2573 
2574     print_syscall_prologue(name);
2575     print_string(arg0, 0);
2576     print_file_mode(arg1, (hasdev == 0));
2577     if (hasdev) {
2578         print_raw_param("makedev(%d", major(arg2), 0);
2579         print_raw_param("%d)", minor(arg2), 1);
2580     }
2581     print_syscall_epilogue(name);
2582 }
2583 #endif
2584 
2585 #ifdef TARGET_NR_mknodat
2586 static void
2587 print_mknodat(const struct syscallname *name,
2588     abi_long arg0, abi_long arg1, abi_long arg2,
2589     abi_long arg3, abi_long arg4, abi_long arg5)
2590 {
2591     int hasdev = (arg2 & (S_IFCHR|S_IFBLK));
2592 
2593     print_syscall_prologue(name);
2594     print_at_dirfd(arg0, 0);
2595     print_string(arg1, 0);
2596     print_file_mode(arg2, (hasdev == 0));
2597     if (hasdev) {
2598         print_raw_param("makedev(%d", major(arg3), 0);
2599         print_raw_param("%d)", minor(arg3), 1);
2600     }
2601     print_syscall_epilogue(name);
2602 }
2603 #endif
2604 
2605 #ifdef TARGET_NR_mq_open
2606 static void
2607 print_mq_open(const struct syscallname *name,
2608     abi_long arg0, abi_long arg1, abi_long arg2,
2609     abi_long arg3, abi_long arg4, abi_long arg5)
2610 {
2611     int is_creat = (arg1 & TARGET_O_CREAT);
2612 
2613     print_syscall_prologue(name);
2614     print_string(arg0, 0);
2615     print_open_flags(arg1, (is_creat == 0));
2616     if (is_creat) {
2617         print_file_mode(arg2, 0);
2618         print_pointer(arg3, 1);
2619     }
2620     print_syscall_epilogue(name);
2621 }
2622 #endif
2623 
2624 #ifdef TARGET_NR_open
2625 static void
2626 print_open(const struct syscallname *name,
2627     abi_long arg0, abi_long arg1, abi_long arg2,
2628     abi_long arg3, abi_long arg4, abi_long arg5)
2629 {
2630     int is_creat = (arg1 & TARGET_O_CREAT);
2631 
2632     print_syscall_prologue(name);
2633     print_string(arg0, 0);
2634     print_open_flags(arg1, (is_creat == 0));
2635     if (is_creat)
2636         print_file_mode(arg2, 1);
2637     print_syscall_epilogue(name);
2638 }
2639 #endif
2640 
2641 #ifdef TARGET_NR_openat
2642 static void
2643 print_openat(const struct syscallname *name,
2644     abi_long arg0, abi_long arg1, abi_long arg2,
2645     abi_long arg3, abi_long arg4, abi_long arg5)
2646 {
2647     int is_creat = (arg2 & TARGET_O_CREAT);
2648 
2649     print_syscall_prologue(name);
2650     print_at_dirfd(arg0, 0);
2651     print_string(arg1, 0);
2652     print_open_flags(arg2, (is_creat == 0));
2653     if (is_creat)
2654         print_file_mode(arg3, 1);
2655     print_syscall_epilogue(name);
2656 }
2657 #endif
2658 
2659 #ifdef TARGET_NR_mq_unlink
2660 static void
2661 print_mq_unlink(const struct syscallname *name,
2662     abi_long arg0, abi_long arg1, abi_long arg2,
2663     abi_long arg3, abi_long arg4, abi_long arg5)
2664 {
2665     print_syscall_prologue(name);
2666     print_string(arg0, 1);
2667     print_syscall_epilogue(name);
2668 }
2669 #endif
2670 
2671 #if defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)
2672 static void
2673 print_fstatat64(const struct syscallname *name,
2674     abi_long arg0, abi_long arg1, abi_long arg2,
2675     abi_long arg3, abi_long arg4, abi_long arg5)
2676 {
2677     print_syscall_prologue(name);
2678     print_at_dirfd(arg0, 0);
2679     print_string(arg1, 0);
2680     print_pointer(arg2, 0);
2681     print_flags(at_file_flags, arg3, 1);
2682     print_syscall_epilogue(name);
2683 }
2684 #define print_newfstatat    print_fstatat64
2685 #endif
2686 
2687 #ifdef TARGET_NR_readlink
2688 static void
2689 print_readlink(const struct syscallname *name,
2690     abi_long arg0, abi_long arg1, abi_long arg2,
2691     abi_long arg3, abi_long arg4, abi_long arg5)
2692 {
2693     print_syscall_prologue(name);
2694     print_string(arg0, 0);
2695     print_pointer(arg1, 0);
2696     print_raw_param("%u", arg2, 1);
2697     print_syscall_epilogue(name);
2698 }
2699 #endif
2700 
2701 #ifdef TARGET_NR_readlinkat
2702 static void
2703 print_readlinkat(const struct syscallname *name,
2704     abi_long arg0, abi_long arg1, abi_long arg2,
2705     abi_long arg3, abi_long arg4, abi_long arg5)
2706 {
2707     print_syscall_prologue(name);
2708     print_at_dirfd(arg0, 0);
2709     print_string(arg1, 0);
2710     print_pointer(arg2, 0);
2711     print_raw_param("%u", arg3, 1);
2712     print_syscall_epilogue(name);
2713 }
2714 #endif
2715 
2716 #ifdef TARGET_NR_rename
2717 static void
2718 print_rename(const struct syscallname *name,
2719     abi_long arg0, abi_long arg1, abi_long arg2,
2720     abi_long arg3, abi_long arg4, abi_long arg5)
2721 {
2722     print_syscall_prologue(name);
2723     print_string(arg0, 0);
2724     print_string(arg1, 1);
2725     print_syscall_epilogue(name);
2726 }
2727 #endif
2728 
2729 #ifdef TARGET_NR_renameat
2730 static void
2731 print_renameat(const struct syscallname *name,
2732     abi_long arg0, abi_long arg1, abi_long arg2,
2733     abi_long arg3, abi_long arg4, abi_long arg5)
2734 {
2735     print_syscall_prologue(name);
2736     print_at_dirfd(arg0, 0);
2737     print_string(arg1, 0);
2738     print_at_dirfd(arg2, 0);
2739     print_string(arg3, 1);
2740     print_syscall_epilogue(name);
2741 }
2742 #endif
2743 
2744 #ifdef TARGET_NR_statfs
2745 static void
2746 print_statfs(const struct syscallname *name,
2747     abi_long arg0, abi_long arg1, abi_long arg2,
2748     abi_long arg3, abi_long arg4, abi_long arg5)
2749 {
2750     print_syscall_prologue(name);
2751     print_string(arg0, 0);
2752     print_pointer(arg1, 1);
2753     print_syscall_epilogue(name);
2754 }
2755 #endif
2756 
2757 #ifdef TARGET_NR_statfs64
2758 static void
2759 print_statfs64(const struct syscallname *name,
2760     abi_long arg0, abi_long arg1, abi_long arg2,
2761     abi_long arg3, abi_long arg4, abi_long arg5)
2762 {
2763     print_syscall_prologue(name);
2764     print_string(arg0, 0);
2765     print_pointer(arg1, 1);
2766     print_syscall_epilogue(name);
2767 }
2768 #endif
2769 
2770 #ifdef TARGET_NR_symlink
2771 static void
2772 print_symlink(const struct syscallname *name,
2773     abi_long arg0, abi_long arg1, abi_long arg2,
2774     abi_long arg3, abi_long arg4, abi_long arg5)
2775 {
2776     print_syscall_prologue(name);
2777     print_string(arg0, 0);
2778     print_string(arg1, 1);
2779     print_syscall_epilogue(name);
2780 }
2781 #endif
2782 
2783 #ifdef TARGET_NR_symlinkat
2784 static void
2785 print_symlinkat(const struct syscallname *name,
2786     abi_long arg0, abi_long arg1, abi_long arg2,
2787     abi_long arg3, abi_long arg4, abi_long arg5)
2788 {
2789     print_syscall_prologue(name);
2790     print_string(arg0, 0);
2791     print_at_dirfd(arg1, 0);
2792     print_string(arg2, 1);
2793     print_syscall_epilogue(name);
2794 }
2795 #endif
2796 
2797 #ifdef TARGET_NR_mount
2798 static void
2799 print_mount(const struct syscallname *name,
2800     abi_long arg0, abi_long arg1, abi_long arg2,
2801     abi_long arg3, abi_long arg4, abi_long arg5)
2802 {
2803     print_syscall_prologue(name);
2804     print_string(arg0, 0);
2805     print_string(arg1, 0);
2806     print_string(arg2, 0);
2807     print_flags(mount_flags, arg3, 0);
2808     print_pointer(arg4, 1);
2809     print_syscall_epilogue(name);
2810 }
2811 #endif
2812 
2813 #ifdef TARGET_NR_umount
2814 static void
2815 print_umount(const struct syscallname *name,
2816     abi_long arg0, abi_long arg1, abi_long arg2,
2817     abi_long arg3, abi_long arg4, abi_long arg5)
2818 {
2819     print_syscall_prologue(name);
2820     print_string(arg0, 1);
2821     print_syscall_epilogue(name);
2822 }
2823 #endif
2824 
2825 #ifdef TARGET_NR_umount2
2826 static void
2827 print_umount2(const struct syscallname *name,
2828     abi_long arg0, abi_long arg1, abi_long arg2,
2829     abi_long arg3, abi_long arg4, abi_long arg5)
2830 {
2831     print_syscall_prologue(name);
2832     print_string(arg0, 0);
2833     print_flags(umount2_flags, arg1, 1);
2834     print_syscall_epilogue(name);
2835 }
2836 #endif
2837 
2838 #ifdef TARGET_NR_unlink
2839 static void
2840 print_unlink(const struct syscallname *name,
2841     abi_long arg0, abi_long arg1, abi_long arg2,
2842     abi_long arg3, abi_long arg4, abi_long arg5)
2843 {
2844     print_syscall_prologue(name);
2845     print_string(arg0, 1);
2846     print_syscall_epilogue(name);
2847 }
2848 #endif
2849 
2850 #ifdef TARGET_NR_unlinkat
2851 static void
2852 print_unlinkat(const struct syscallname *name,
2853     abi_long arg0, abi_long arg1, abi_long arg2,
2854     abi_long arg3, abi_long arg4, abi_long arg5)
2855 {
2856     print_syscall_prologue(name);
2857     print_at_dirfd(arg0, 0);
2858     print_string(arg1, 0);
2859     print_flags(unlinkat_flags, arg2, 1);
2860     print_syscall_epilogue(name);
2861 }
2862 #endif
2863 
2864 #ifdef TARGET_NR_utime
2865 static void
2866 print_utime(const struct syscallname *name,
2867     abi_long arg0, abi_long arg1, abi_long arg2,
2868     abi_long arg3, abi_long arg4, abi_long arg5)
2869 {
2870     print_syscall_prologue(name);
2871     print_string(arg0, 0);
2872     print_pointer(arg1, 1);
2873     print_syscall_epilogue(name);
2874 }
2875 #endif
2876 
2877 #ifdef TARGET_NR_utimes
2878 static void
2879 print_utimes(const struct syscallname *name,
2880     abi_long arg0, abi_long arg1, abi_long arg2,
2881     abi_long arg3, abi_long arg4, abi_long arg5)
2882 {
2883     print_syscall_prologue(name);
2884     print_string(arg0, 0);
2885     print_pointer(arg1, 1);
2886     print_syscall_epilogue(name);
2887 }
2888 #endif
2889 
2890 #ifdef TARGET_NR_utimensat
2891 static void
2892 print_utimensat(const struct syscallname *name,
2893     abi_long arg0, abi_long arg1, abi_long arg2,
2894     abi_long arg3, abi_long arg4, abi_long arg5)
2895 {
2896     print_syscall_prologue(name);
2897     print_at_dirfd(arg0, 0);
2898     print_string(arg1, 0);
2899     print_pointer(arg2, 0);
2900     print_flags(at_file_flags, arg3, 1);
2901     print_syscall_epilogue(name);
2902 }
2903 #endif
2904 
2905 #if defined(TARGET_NR_mmap) || defined(TARGET_NR_mmap2)
2906 static void
2907 print_mmap(const struct syscallname *name,
2908     abi_long arg0, abi_long arg1, abi_long arg2,
2909     abi_long arg3, abi_long arg4, abi_long arg5)
2910 {
2911     print_syscall_prologue(name);
2912     print_pointer(arg0, 0);
2913     print_raw_param("%d", arg1, 0);
2914     print_flags(mmap_prot_flags, arg2, 0);
2915     print_flags(mmap_flags, arg3, 0);
2916     print_raw_param("%d", arg4, 0);
2917     print_raw_param("%#x", arg5, 1);
2918     print_syscall_epilogue(name);
2919 }
2920 #define print_mmap2     print_mmap
2921 #endif
2922 
2923 #ifdef TARGET_NR_mprotect
2924 static void
2925 print_mprotect(const struct syscallname *name,
2926     abi_long arg0, abi_long arg1, abi_long arg2,
2927     abi_long arg3, abi_long arg4, abi_long arg5)
2928 {
2929     print_syscall_prologue(name);
2930     print_pointer(arg0, 0);
2931     print_raw_param("%d", arg1, 0);
2932     print_flags(mmap_prot_flags, arg2, 1);
2933     print_syscall_epilogue(name);
2934 }
2935 #endif
2936 
2937 #ifdef TARGET_NR_munmap
2938 static void
2939 print_munmap(const struct syscallname *name,
2940     abi_long arg0, abi_long arg1, abi_long arg2,
2941     abi_long arg3, abi_long arg4, abi_long arg5)
2942 {
2943     print_syscall_prologue(name);
2944     print_pointer(arg0, 0);
2945     print_raw_param("%d", arg1, 1);
2946     print_syscall_epilogue(name);
2947 }
2948 #endif
2949 
2950 #ifdef TARGET_NR_futex
2951 static void print_futex_op(abi_long tflag, int last)
2952 {
2953 #define print_op(val) \
2954 if( cmd == val ) { \
2955     qemu_log(#val); \
2956     return; \
2957 }
2958 
2959     int cmd = (int)tflag;
2960 #ifdef FUTEX_PRIVATE_FLAG
2961     if (cmd & FUTEX_PRIVATE_FLAG) {
2962         qemu_log("FUTEX_PRIVATE_FLAG|");
2963         cmd &= ~FUTEX_PRIVATE_FLAG;
2964     }
2965 #endif
2966 #ifdef FUTEX_CLOCK_REALTIME
2967     if (cmd & FUTEX_CLOCK_REALTIME) {
2968         qemu_log("FUTEX_CLOCK_REALTIME|");
2969         cmd &= ~FUTEX_CLOCK_REALTIME;
2970     }
2971 #endif
2972     print_op(FUTEX_WAIT)
2973     print_op(FUTEX_WAKE)
2974     print_op(FUTEX_FD)
2975     print_op(FUTEX_REQUEUE)
2976     print_op(FUTEX_CMP_REQUEUE)
2977     print_op(FUTEX_WAKE_OP)
2978     print_op(FUTEX_LOCK_PI)
2979     print_op(FUTEX_UNLOCK_PI)
2980     print_op(FUTEX_TRYLOCK_PI)
2981 #ifdef FUTEX_WAIT_BITSET
2982     print_op(FUTEX_WAIT_BITSET)
2983 #endif
2984 #ifdef FUTEX_WAKE_BITSET
2985     print_op(FUTEX_WAKE_BITSET)
2986 #endif
2987     /* unknown values */
2988     qemu_log("%d", cmd);
2989 }
2990 
2991 static void
2992 print_futex(const struct syscallname *name,
2993     abi_long arg0, abi_long arg1, abi_long arg2,
2994     abi_long arg3, abi_long arg4, abi_long arg5)
2995 {
2996     print_syscall_prologue(name);
2997     print_pointer(arg0, 0);
2998     print_futex_op(arg1, 0);
2999     print_raw_param(",%d", arg2, 0);
3000     print_pointer(arg3, 0); /* struct timespec */
3001     print_pointer(arg4, 0);
3002     print_raw_param("%d", arg4, 1);
3003     print_syscall_epilogue(name);
3004 }
3005 #endif
3006 
3007 #ifdef TARGET_NR_kill
3008 static void
3009 print_kill(const struct syscallname *name,
3010     abi_long arg0, abi_long arg1, abi_long arg2,
3011     abi_long arg3, abi_long arg4, abi_long arg5)
3012 {
3013     print_syscall_prologue(name);
3014     print_raw_param("%d", arg0, 0);
3015     print_signal(arg1, 1);
3016     print_syscall_epilogue(name);
3017 }
3018 #endif
3019 
3020 #ifdef TARGET_NR_tkill
3021 static void
3022 print_tkill(const struct syscallname *name,
3023     abi_long arg0, abi_long arg1, abi_long arg2,
3024     abi_long arg3, abi_long arg4, abi_long arg5)
3025 {
3026     print_syscall_prologue(name);
3027     print_raw_param("%d", arg0, 0);
3028     print_signal(arg1, 1);
3029     print_syscall_epilogue(name);
3030 }
3031 #endif
3032 
3033 #ifdef TARGET_NR_tgkill
3034 static void
3035 print_tgkill(const struct syscallname *name,
3036     abi_long arg0, abi_long arg1, abi_long arg2,
3037     abi_long arg3, abi_long arg4, abi_long arg5)
3038 {
3039     print_syscall_prologue(name);
3040     print_raw_param("%d", arg0, 0);
3041     print_raw_param("%d", arg1, 0);
3042     print_signal(arg2, 1);
3043     print_syscall_epilogue(name);
3044 }
3045 #endif
3046 
3047 #ifdef TARGET_NR_statx
3048 static void
3049 print_statx(const struct syscallname *name,
3050             abi_long arg0, abi_long arg1, abi_long arg2,
3051             abi_long arg3, abi_long arg4, abi_long arg5)
3052 {
3053     print_syscall_prologue(name);
3054     print_at_dirfd(arg0, 0);
3055     print_string(arg1, 0);
3056     print_flags(statx_flags, arg2, 0);
3057     print_flags(statx_mask, arg3, 0);
3058     print_pointer(arg4, 1);
3059     print_syscall_epilogue(name);
3060 }
3061 #endif
3062 
3063 #ifdef TARGET_NR_ioctl
3064 static void
3065 print_ioctl(const struct syscallname *name,
3066             abi_long arg0, abi_long arg1, abi_long arg2,
3067             abi_long arg3, abi_long arg4, abi_long arg5)
3068 {
3069     print_syscall_prologue(name);
3070     print_raw_param("%d", arg0, 0);
3071 
3072     const IOCTLEntry *ie;
3073     const argtype *arg_type;
3074     void *argptr;
3075     int target_size;
3076 
3077     for (ie = ioctl_entries; ie->target_cmd != 0; ie++) {
3078         if (ie->target_cmd == arg1) {
3079             break;
3080         }
3081     }
3082 
3083     if (ie->target_cmd == 0) {
3084         print_raw_param("%#x", arg1, 0);
3085         print_raw_param("%#x", arg2, 1);
3086     } else {
3087         qemu_log("%s", ie->name);
3088         arg_type = ie->arg_type;
3089 
3090         if (arg_type[0] != TYPE_NULL) {
3091             qemu_log(",");
3092 
3093             switch (arg_type[0]) {
3094             case TYPE_PTRVOID:
3095                 print_pointer(arg2, 1);
3096                 break;
3097             case TYPE_CHAR:
3098             case TYPE_SHORT:
3099             case TYPE_INT:
3100                 print_raw_param("%d", arg2, 1);
3101                 break;
3102             case TYPE_LONG:
3103                 print_raw_param(TARGET_ABI_FMT_ld, arg2, 1);
3104                 break;
3105             case TYPE_ULONG:
3106                 print_raw_param(TARGET_ABI_FMT_lu, arg2, 1);
3107                 break;
3108             case TYPE_PTR:
3109                 switch (ie->access) {
3110                 case IOC_R:
3111                     print_pointer(arg2, 1);
3112                     break;
3113                 case IOC_W:
3114                 case IOC_RW:
3115                     arg_type++;
3116                     target_size = thunk_type_size(arg_type, 0);
3117                     argptr = lock_user(VERIFY_READ, arg2, target_size, 1);
3118                     if (argptr) {
3119                         thunk_print(argptr, arg_type);
3120                         unlock_user(argptr, arg2, target_size);
3121                     } else {
3122                         print_pointer(arg2, 1);
3123                     }
3124                     break;
3125                 }
3126                 break;
3127             default:
3128                 g_assert_not_reached();
3129             }
3130         }
3131     }
3132     print_syscall_epilogue(name);
3133 }
3134 #endif
3135 
3136 /*
3137  * An array of all of the syscalls we know about
3138  */
3139 
3140 static const struct syscallname scnames[] = {
3141 #include "strace.list"
3142 };
3143 
3144 static int nsyscalls = ARRAY_SIZE(scnames);
3145 
3146 /*
3147  * The public interface to this module.
3148  */
3149 void
3150 print_syscall(int num,
3151               abi_long arg1, abi_long arg2, abi_long arg3,
3152               abi_long arg4, abi_long arg5, abi_long arg6)
3153 {
3154     int i;
3155     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 ")";
3156 
3157     qemu_log("%d ", getpid());
3158 
3159     for(i=0;i<nsyscalls;i++)
3160         if( scnames[i].nr == num ) {
3161             if( scnames[i].call != NULL ) {
3162                 scnames[i].call(
3163                     &scnames[i], arg1, arg2, arg3, arg4, arg5, arg6);
3164             } else {
3165                 /* XXX: this format system is broken because it uses
3166                    host types and host pointers for strings */
3167                 if( scnames[i].format != NULL )
3168                     format = scnames[i].format;
3169                 qemu_log(format,
3170                          scnames[i].name, arg1, arg2, arg3, arg4, arg5, arg6);
3171             }
3172             return;
3173         }
3174     qemu_log("Unknown syscall %d\n", num);
3175 }
3176 
3177 
3178 void
3179 print_syscall_ret(int num, abi_long ret,
3180                   abi_long arg1, abi_long arg2, abi_long arg3,
3181                   abi_long arg4, abi_long arg5, abi_long arg6)
3182 {
3183     int i;
3184 
3185     for(i=0;i<nsyscalls;i++)
3186         if( scnames[i].nr == num ) {
3187             if( scnames[i].result != NULL ) {
3188                 scnames[i].result(&scnames[i], ret,
3189                                   arg1, arg2, arg3,
3190                                   arg4, arg5, arg6);
3191             } else {
3192                 if (!print_syscall_err(ret)) {
3193                     qemu_log(TARGET_ABI_FMT_ld, ret);
3194                 }
3195                 qemu_log("\n");
3196             }
3197             break;
3198         }
3199 }
3200 
3201 void print_taken_signal(int target_signum, const target_siginfo_t *tinfo)
3202 {
3203     /* Print the strace output for a signal being taken:
3204      * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
3205      */
3206     qemu_log("--- ");
3207     print_signal(target_signum, 1);
3208     qemu_log(" ");
3209     print_siginfo(tinfo);
3210     qemu_log(" ---\n");
3211 }
3212