]>
Commit | Line | Data |
---|---|---|
88dae46d SB |
1 | /* |
2 | * System call tracing and debugging | |
3 | * | |
4 | * | |
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. | |
9 | * | |
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. | |
14 | * | |
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/>. | |
17 | */ | |
18 | ||
84778508 BS |
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> | |
88dae46d SB |
25 | #include <sys/ioccom.h> |
26 | #include <ctype.h> | |
84778508 | 27 | |
88dae46d | 28 | #include "qemu.h" |
84778508 | 29 | |
88dae46d | 30 | int do_strace; |
84778508 BS |
31 | |
32 | /* | |
33 | * Utility functions | |
34 | */ | |
35 | ||
80b34604 SB |
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) | |
39 | { | |
40 | uint32_t i; | |
41 | int32_t *namep; | |
42 | ||
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; | |
47 | ||
48 | for (i = 0; i < (uint32_t)arg2; i++) { | |
49 | gemu_log("%d ", tswap32(*p++)); | |
50 | } | |
51 | unlock_user(namep, arg1, 0); | |
52 | } | |
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); | |
56 | } | |
57 | ||
88dae46d SB |
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) | |
84778508 BS |
61 | { |
62 | abi_ulong arg_ptr_addr; | |
63 | char *s; | |
64 | ||
88dae46d SB |
65 | s = lock_user_string(arg1); |
66 | if (s == NULL) { | |
84778508 | 67 | return; |
88dae46d | 68 | } |
84778508 BS |
69 | gemu_log("%s(\"%s\",{", name->name, s); |
70 | unlock_user(s, arg1, 0); | |
71 | ||
72 | for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) { | |
18c9a9c3 | 73 | abi_ulong *arg_ptr, arg_addr; |
84778508 BS |
74 | |
75 | arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1); | |
88dae46d | 76 | if (!arg_ptr) { |
84778508 | 77 | return; |
88dae46d | 78 | } |
84778508 BS |
79 | arg_addr = tswapl(*arg_ptr); |
80 | unlock_user(arg_ptr, arg_ptr_addr, 0); | |
88dae46d | 81 | if (!arg_addr) { |
84778508 | 82 | break; |
88dae46d | 83 | } |
84778508 BS |
84 | if ((s = lock_user_string(arg_addr))) { |
85 | gemu_log("\"%s\",", s); | |
18c9a9c3 | 86 | unlock_user(s, arg_addr, 0); |
84778508 BS |
87 | } |
88 | } | |
84778508 BS |
89 | gemu_log("NULL})"); |
90 | } | |
91 | ||
b85159a3 SB |
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) | |
95 | { | |
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); | |
109 | } | |
110 | ||
84778508 BS |
111 | /* |
112 | * Variants for the return value output function | |
113 | */ | |
114 | ||
88dae46d | 115 | static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret) |
84778508 | 116 | { |
88dae46d | 117 | if (ret == -1) { |
84778508 BS |
118 | gemu_log(" = -1 errno=%d (%s)\n", errno, strerror(errno)); |
119 | } else { | |
120 | gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret); | |
121 | } | |
122 | } | |
123 | ||
124 | #if 0 /* currently unused */ | |
125 | static void | |
126 | print_syscall_ret_raw(struct syscallname *name, abi_long ret) | |
127 | { | |
128 | gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret); | |
129 | } | |
130 | #endif | |
131 | ||
132 | /* | |
133 | * An array of all of the syscalls we know about | |
134 | */ | |
135 | ||
136 | static const struct syscallname freebsd_scnames[] = { | |
137 | #include "freebsd/strace.list" | |
138 | }; | |
139 | static const struct syscallname netbsd_scnames[] = { | |
140 | #include "netbsd/strace.list" | |
141 | }; | |
142 | static const struct syscallname openbsd_scnames[] = { | |
143 | #include "openbsd/strace.list" | |
144 | }; | |
145 | ||
88dae46d SB |
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) | |
84778508 BS |
149 | { |
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 ")"; | |
154 | ||
155 | gemu_log("%d ", getpid() ); | |
156 | ||
88dae46d | 157 | for (i = 0; i < nscnames; i++) { |
84778508 BS |
158 | if (scnames[i].nr == num) { |
159 | if (scnames[i].call != NULL) { | |
160 | scnames[i].call(&scnames[i], arg1, arg2, arg3, arg4, arg5, | |
88dae46d | 161 | arg6); |
84778508 BS |
162 | } else { |
163 | /* XXX: this format system is broken because it uses | |
164 | host types and host pointers for strings */ | |
88dae46d | 165 | if (scnames[i].format != NULL) { |
84778508 | 166 | format = scnames[i].format; |
88dae46d SB |
167 | } |
168 | gemu_log(format, scnames[i].name, arg1, arg2, arg3, arg4, arg5, | |
169 | arg6); | |
84778508 BS |
170 | } |
171 | return; | |
172 | } | |
88dae46d | 173 | } |
84778508 BS |
174 | gemu_log("Unknown syscall %d\n", num); |
175 | } | |
176 | ||
88dae46d SB |
177 | static void print_syscall_ret(int num, abi_long ret, |
178 | const struct syscallname *scnames, unsigned int nscnames) | |
84778508 BS |
179 | { |
180 | unsigned int i; | |
181 | ||
88dae46d | 182 | for (i = 0; i < nscnames; i++) { |
84778508 BS |
183 | if (scnames[i].nr == num) { |
184 | if (scnames[i].result != NULL) { | |
185 | scnames[i].result(&scnames[i], ret); | |
186 | } else { | |
88dae46d | 187 | if (ret < 0) { |
84778508 BS |
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); | |
192 | } | |
193 | } | |
194 | break; | |
195 | } | |
88dae46d | 196 | } |
84778508 BS |
197 | } |
198 | ||
199 | /* | |
200 | * The public interface to this module. | |
201 | */ | |
88dae46d SB |
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) | |
84778508 | 204 | { |
88dae46d SB |
205 | |
206 | print_syscall(num, freebsd_scnames, ARRAY_SIZE(freebsd_scnames), arg1, arg2, | |
207 | arg3, arg4, arg5, arg6); | |
84778508 BS |
208 | } |
209 | ||
88dae46d | 210 | void print_freebsd_syscall_ret(int num, abi_long ret) |
84778508 | 211 | { |
88dae46d | 212 | |
84778508 BS |
213 | print_syscall_ret(num, ret, freebsd_scnames, ARRAY_SIZE(freebsd_scnames)); |
214 | } | |
215 | ||
88dae46d SB |
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) | |
84778508 | 218 | { |
88dae46d | 219 | |
84778508 BS |
220 | print_syscall(num, netbsd_scnames, ARRAY_SIZE(netbsd_scnames), |
221 | arg1, arg2, arg3, arg4, arg5, arg6); | |
222 | } | |
223 | ||
88dae46d | 224 | void print_netbsd_syscall_ret(int num, abi_long ret) |
84778508 | 225 | { |
88dae46d | 226 | |
84778508 BS |
227 | print_syscall_ret(num, ret, netbsd_scnames, ARRAY_SIZE(netbsd_scnames)); |
228 | } | |
229 | ||
88dae46d SB |
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) | |
84778508 | 232 | { |
88dae46d SB |
233 | |
234 | print_syscall(num, openbsd_scnames, ARRAY_SIZE(openbsd_scnames), arg1, arg2, | |
235 | arg3, arg4, arg5, arg6); | |
84778508 BS |
236 | } |
237 | ||
88dae46d | 238 | void print_openbsd_syscall_ret(int num, abi_long ret) |
84778508 | 239 | { |
88dae46d | 240 | |
84778508 BS |
241 | print_syscall_ret(num, ret, openbsd_scnames, ARRAY_SIZE(openbsd_scnames)); |
242 | } |