docs: clarify memory region lifecycle
[qemu.git] / bsd-user / strace.c
blobe33dd4d48fea0dad5058464e1301b9561a1385af
1 /*
2 * System call tracing and debugging
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <stdio.h>
20 #include <errno.h>
21 #include <sys/select.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <sys/syscall.h>
25 #include <sys/ioccom.h>
26 #include <ctype.h>
28 #include "qemu.h"
30 int do_strace;
33 * Utility functions
36 static void print_sysctl(const struct syscallname *name, abi_long arg1,
37 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
38 abi_long arg6)
40 uint32_t i;
41 int32_t *namep;
43 gemu_log("%s({ ", name->name);
44 namep = lock_user(VERIFY_READ, arg1, sizeof(int32_t) * arg2, 1);
45 if (namep) {
46 int32_t *p = namep;
48 for (i = 0; i < (uint32_t)arg2; i++) {
49 gemu_log("%d ", tswap32(*p++));
51 unlock_user(namep, arg1, 0);
53 gemu_log("}, %u, 0x" TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ", 0x"
54 TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ")",
55 (uint32_t)arg2, arg3, arg4, arg5, arg6);
58 static void print_execve(const struct syscallname *name, abi_long arg1,
59 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
60 abi_long arg6)
62 abi_ulong arg_ptr_addr;
63 char *s;
65 s = lock_user_string(arg1);
66 if (s == NULL) {
67 return;
69 gemu_log("%s(\"%s\",{", name->name, s);
70 unlock_user(s, arg1, 0);
72 for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) {
73 abi_ulong *arg_ptr, arg_addr;
75 arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1);
76 if (!arg_ptr) {
77 return;
79 arg_addr = tswapl(*arg_ptr);
80 unlock_user(arg_ptr, arg_ptr_addr, 0);
81 if (!arg_addr) {
82 break;
84 if ((s = lock_user_string(arg_addr))) {
85 gemu_log("\"%s\",", s);
86 unlock_user(s, arg_addr, 0);
89 gemu_log("NULL})");
92 static void print_ioctl(const struct syscallname *name,
93 abi_long arg1, abi_long arg2, abi_long arg3, abi_long arg4,
94 abi_long arg5, abi_long arg6)
96 /* Decode the ioctl request */
97 gemu_log("%s(%d, 0x%0lx { IO%s%s GRP:0x%x('%c') CMD:%d LEN:%d }, 0x"
98 TARGET_ABI_FMT_lx ", ...)",
99 name->name,
100 (int)arg1,
101 (unsigned long)arg2,
102 arg2 & IOC_OUT ? "R" : "",
103 arg2 & IOC_IN ? "W" : "",
104 (unsigned)IOCGROUP(arg2),
105 isprint(IOCGROUP(arg2)) ? (char)IOCGROUP(arg2) : '?',
106 (int)arg2 & 0xFF,
107 (int)IOCPARM_LEN(arg2),
108 arg3);
112 * Variants for the return value output function
115 static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
117 if (ret == -1) {
118 gemu_log(" = -1 errno=%d (%s)\n", errno, strerror(errno));
119 } else {
120 gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
124 #if 0 /* currently unused */
125 static void
126 print_syscall_ret_raw(struct syscallname *name, abi_long ret)
128 gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
130 #endif
133 * An array of all of the syscalls we know about
136 static const struct syscallname freebsd_scnames[] = {
137 #include "freebsd/strace.list"
139 static const struct syscallname netbsd_scnames[] = {
140 #include "netbsd/strace.list"
142 static const struct syscallname openbsd_scnames[] = {
143 #include "openbsd/strace.list"
146 static void print_syscall(int num, const struct syscallname *scnames,
147 unsigned int nscnames, abi_long arg1, abi_long arg2, abi_long arg3,
148 abi_long arg4, abi_long arg5, abi_long arg6)
150 unsigned int i;
151 const char *format="%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
152 TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
153 TARGET_ABI_FMT_ld ")";
155 gemu_log("%d ", getpid() );
157 for (i = 0; i < nscnames; i++) {
158 if (scnames[i].nr == num) {
159 if (scnames[i].call != NULL) {
160 scnames[i].call(&scnames[i], arg1, arg2, arg3, arg4, arg5,
161 arg6);
162 } else {
163 /* XXX: this format system is broken because it uses
164 host types and host pointers for strings */
165 if (scnames[i].format != NULL) {
166 format = scnames[i].format;
168 gemu_log(format, scnames[i].name, arg1, arg2, arg3, arg4, arg5,
169 arg6);
171 return;
174 gemu_log("Unknown syscall %d\n", num);
177 static void print_syscall_ret(int num, abi_long ret,
178 const struct syscallname *scnames, unsigned int nscnames)
180 unsigned int i;
182 for (i = 0; i < nscnames; i++) {
183 if (scnames[i].nr == num) {
184 if (scnames[i].result != NULL) {
185 scnames[i].result(&scnames[i], ret);
186 } else {
187 if (ret < 0) {
188 gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret,
189 strerror(-ret));
190 } else {
191 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
194 break;
200 * The public interface to this module.
202 void print_freebsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
203 abi_long arg4, abi_long arg5, abi_long arg6)
206 print_syscall(num, freebsd_scnames, ARRAY_SIZE(freebsd_scnames), arg1, arg2,
207 arg3, arg4, arg5, arg6);
210 void print_freebsd_syscall_ret(int num, abi_long ret)
213 print_syscall_ret(num, ret, freebsd_scnames, ARRAY_SIZE(freebsd_scnames));
216 void print_netbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
217 abi_long arg4, abi_long arg5, abi_long arg6)
220 print_syscall(num, netbsd_scnames, ARRAY_SIZE(netbsd_scnames),
221 arg1, arg2, arg3, arg4, arg5, arg6);
224 void print_netbsd_syscall_ret(int num, abi_long ret)
227 print_syscall_ret(num, ret, netbsd_scnames, ARRAY_SIZE(netbsd_scnames));
230 void print_openbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
231 abi_long arg4, abi_long arg5, abi_long arg6)
234 print_syscall(num, openbsd_scnames, ARRAY_SIZE(openbsd_scnames), arg1, arg2,
235 arg3, arg4, arg5, arg6);
238 void print_openbsd_syscall_ret(int num, abi_long ret)
241 print_syscall_ret(num, ret, openbsd_scnames, ARRAY_SIZE(openbsd_scnames));