]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/strace.c
Merge tag 'hppa64-fixes-pull-request' of https://github.com/hdeller/qemu-hppa into...
[mirror_qemu.git] / bsd-user / strace.c
CommitLineData
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
2231197c 19#include "qemu/osdep.h"
84778508 20#include <sys/select.h>
84778508 21#include <sys/syscall.h>
88dae46d 22#include <sys/ioccom.h>
84778508 23
88dae46d 24#include "qemu.h"
84778508 25
ea1ab4cf
SS
26#include "os-strace.h" /* OS dependent strace print functions */
27
88dae46d 28int do_strace;
84778508
BS
29
30/*
31 * Utility functions
32 */
fd5bec9a
WL
33static const char *
34get_comma(int last)
35{
36 return (last) ? "" : ",";
37}
38
39/*
40 * Prints out raw parameter using given format. Caller needs
41 * to do byte swapping if needed.
42 */
43static void
44print_raw_param(const char *fmt, abi_long param, int last)
45{
46 char format[64];
47
48 (void)snprintf(format, sizeof(format), "%s%s", fmt, get_comma(last));
49 gemu_log(format, param);
50}
84778508 51
80b34604
SB
52static void print_sysctl(const struct syscallname *name, abi_long arg1,
53 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
54 abi_long arg6)
55{
56 uint32_t i;
57 int32_t *namep;
58
59 gemu_log("%s({ ", name->name);
60 namep = lock_user(VERIFY_READ, arg1, sizeof(int32_t) * arg2, 1);
61 if (namep) {
62 int32_t *p = namep;
63
64 for (i = 0; i < (uint32_t)arg2; i++) {
65 gemu_log("%d ", tswap32(*p++));
66 }
67 unlock_user(namep, arg1, 0);
68 }
69 gemu_log("}, %u, 0x" TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ", 0x"
70 TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ")",
71 (uint32_t)arg2, arg3, arg4, arg5, arg6);
72}
73
88dae46d
SB
74static void print_execve(const struct syscallname *name, abi_long arg1,
75 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
76 abi_long arg6)
84778508
BS
77{
78 abi_ulong arg_ptr_addr;
79 char *s;
80
88dae46d
SB
81 s = lock_user_string(arg1);
82 if (s == NULL) {
84778508 83 return;
88dae46d 84 }
84778508
BS
85 gemu_log("%s(\"%s\",{", name->name, s);
86 unlock_user(s, arg1, 0);
87
88 for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) {
18c9a9c3 89 abi_ulong *arg_ptr, arg_addr;
84778508
BS
90
91 arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1);
88dae46d 92 if (!arg_ptr) {
84778508 93 return;
88dae46d 94 }
84778508
BS
95 arg_addr = tswapl(*arg_ptr);
96 unlock_user(arg_ptr, arg_ptr_addr, 0);
88dae46d 97 if (!arg_addr) {
84778508 98 break;
88dae46d 99 }
84778508
BS
100 if ((s = lock_user_string(arg_addr))) {
101 gemu_log("\"%s\",", s);
18c9a9c3 102 unlock_user(s, arg_addr, 0);
84778508
BS
103 }
104 }
84778508
BS
105 gemu_log("NULL})");
106}
107
b85159a3
SB
108static void print_ioctl(const struct syscallname *name,
109 abi_long arg1, abi_long arg2, abi_long arg3, abi_long arg4,
110 abi_long arg5, abi_long arg6)
111{
112 /* Decode the ioctl request */
113 gemu_log("%s(%d, 0x%0lx { IO%s%s GRP:0x%x('%c') CMD:%d LEN:%d }, 0x"
114 TARGET_ABI_FMT_lx ", ...)",
115 name->name,
116 (int)arg1,
117 (unsigned long)arg2,
118 arg2 & IOC_OUT ? "R" : "",
119 arg2 & IOC_IN ? "W" : "",
120 (unsigned)IOCGROUP(arg2),
121 isprint(IOCGROUP(arg2)) ? (char)IOCGROUP(arg2) : '?',
122 (int)arg2 & 0xFF,
123 (int)IOCPARM_LEN(arg2),
124 arg3);
125}
126
ea1ab4cf
SS
127static void print_sysarch(const struct syscallname *name, abi_long arg1,
128 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
129 abi_long arg6)
130{
131 /* This is os dependent. */
132 do_os_print_sysarch(name, arg1, arg2, arg3, arg4, arg5, arg6);
133}
134
84778508
BS
135/*
136 * Variants for the return value output function
137 */
138
88dae46d 139static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
84778508 140{
88dae46d 141 if (ret == -1) {
84778508
BS
142 gemu_log(" = -1 errno=%d (%s)\n", errno, strerror(errno));
143 } else {
144 gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
145 }
146}
147
84778508
BS
148/*
149 * An array of all of the syscalls we know about
150 */
151
152static const struct syscallname freebsd_scnames[] = {
153#include "freebsd/strace.list"
154};
155static const struct syscallname netbsd_scnames[] = {
156#include "netbsd/strace.list"
157};
158static const struct syscallname openbsd_scnames[] = {
159#include "openbsd/strace.list"
160};
161
88dae46d
SB
162static void print_syscall(int num, const struct syscallname *scnames,
163 unsigned int nscnames, abi_long arg1, abi_long arg2, abi_long arg3,
164 abi_long arg4, abi_long arg5, abi_long arg6)
84778508
BS
165{
166 unsigned int i;
167 const char *format="%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
168 TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
169 TARGET_ABI_FMT_ld ")";
170
171 gemu_log("%d ", getpid() );
172
88dae46d 173 for (i = 0; i < nscnames; i++) {
84778508
BS
174 if (scnames[i].nr == num) {
175 if (scnames[i].call != NULL) {
176 scnames[i].call(&scnames[i], arg1, arg2, arg3, arg4, arg5,
88dae46d 177 arg6);
84778508
BS
178 } else {
179 /* XXX: this format system is broken because it uses
180 host types and host pointers for strings */
88dae46d 181 if (scnames[i].format != NULL) {
84778508 182 format = scnames[i].format;
88dae46d
SB
183 }
184 gemu_log(format, scnames[i].name, arg1, arg2, arg3, arg4, arg5,
185 arg6);
84778508
BS
186 }
187 return;
188 }
88dae46d 189 }
84778508
BS
190 gemu_log("Unknown syscall %d\n", num);
191}
192
88dae46d
SB
193static void print_syscall_ret(int num, abi_long ret,
194 const struct syscallname *scnames, unsigned int nscnames)
84778508
BS
195{
196 unsigned int i;
197
88dae46d 198 for (i = 0; i < nscnames; i++) {
84778508
BS
199 if (scnames[i].nr == num) {
200 if (scnames[i].result != NULL) {
201 scnames[i].result(&scnames[i], ret);
202 } else {
88dae46d 203 if (ret < 0) {
84778508
BS
204 gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret,
205 strerror(-ret));
206 } else {
207 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
208 }
209 }
210 break;
211 }
88dae46d 212 }
84778508
BS
213}
214
215/*
216 * The public interface to this module.
217 */
88dae46d
SB
218void print_freebsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
219 abi_long arg4, abi_long arg5, abi_long arg6)
84778508 220{
88dae46d
SB
221
222 print_syscall(num, freebsd_scnames, ARRAY_SIZE(freebsd_scnames), arg1, arg2,
223 arg3, arg4, arg5, arg6);
84778508
BS
224}
225
88dae46d 226void print_freebsd_syscall_ret(int num, abi_long ret)
84778508 227{
88dae46d 228
84778508
BS
229 print_syscall_ret(num, ret, freebsd_scnames, ARRAY_SIZE(freebsd_scnames));
230}
231
88dae46d
SB
232void print_netbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
233 abi_long arg4, abi_long arg5, abi_long arg6)
84778508 234{
88dae46d 235
84778508
BS
236 print_syscall(num, netbsd_scnames, ARRAY_SIZE(netbsd_scnames),
237 arg1, arg2, arg3, arg4, arg5, arg6);
238}
239
88dae46d 240void print_netbsd_syscall_ret(int num, abi_long ret)
84778508 241{
88dae46d 242
84778508
BS
243 print_syscall_ret(num, ret, netbsd_scnames, ARRAY_SIZE(netbsd_scnames));
244}
245
88dae46d
SB
246void print_openbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
247 abi_long arg4, abi_long arg5, abi_long arg6)
84778508 248{
88dae46d
SB
249
250 print_syscall(num, openbsd_scnames, ARRAY_SIZE(openbsd_scnames), arg1, arg2,
251 arg3, arg4, arg5, arg6);
84778508
BS
252}
253
88dae46d 254void print_openbsd_syscall_ret(int num, abi_long ret)
84778508 255{
88dae46d 256
84778508
BS
257 print_syscall_ret(num, ret, openbsd_scnames, ARRAY_SIZE(openbsd_scnames));
258}
fd5bec9a
WL
259
260static void
261print_signal(abi_ulong arg, int last)
262{
263 const char *signal_name = NULL;
264 switch (arg) {
265 case TARGET_SIGHUP:
266 signal_name = "SIGHUP";
267 break;
268 case TARGET_SIGINT:
269 signal_name = "SIGINT";
270 break;
271 case TARGET_SIGQUIT:
272 signal_name = "SIGQUIT";
273 break;
274 case TARGET_SIGILL:
275 signal_name = "SIGILL";
276 break;
277 case TARGET_SIGABRT:
278 signal_name = "SIGABRT";
279 break;
280 case TARGET_SIGFPE:
281 signal_name = "SIGFPE";
282 break;
283 case TARGET_SIGKILL:
284 signal_name = "SIGKILL";
285 break;
286 case TARGET_SIGSEGV:
287 signal_name = "SIGSEGV";
288 break;
289 case TARGET_SIGPIPE:
290 signal_name = "SIGPIPE";
291 break;
292 case TARGET_SIGALRM:
293 signal_name = "SIGALRM";
294 break;
295 case TARGET_SIGTERM:
296 signal_name = "SIGTERM";
297 break;
298 case TARGET_SIGUSR1:
299 signal_name = "SIGUSR1";
300 break;
301 case TARGET_SIGUSR2:
302 signal_name = "SIGUSR2";
303 break;
304 case TARGET_SIGCHLD:
305 signal_name = "SIGCHLD";
306 break;
307 case TARGET_SIGCONT:
308 signal_name = "SIGCONT";
309 break;
310 case TARGET_SIGSTOP:
311 signal_name = "SIGSTOP";
312 break;
313 case TARGET_SIGTTIN:
314 signal_name = "SIGTTIN";
315 break;
316 case TARGET_SIGTTOU:
317 signal_name = "SIGTTOU";
318 break;
319 }
320 if (signal_name == NULL) {
321 print_raw_param("%ld", arg, last);
322 return;
323 }
324 gemu_log("%s%s", signal_name, get_comma(last));
325}
326
327void print_taken_signal(int target_signum, const target_siginfo_t *tinfo)
328{
329 /*
330 * Print the strace output for a signal being taken:
331 * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
332 */
333 gemu_log("%d ", getpid());
334 gemu_log("--- ");
335 print_signal(target_signum, 1);
336 gemu_log(" ---\n");
337}