]> git.proxmox.com Git - mirror_qemu.git/blob - bsd-user/strace.c
bsd-user: GPL v2 attribution update and style
[mirror_qemu.git] / bsd-user / strace.c
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
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>
27
28 #include "qemu.h"
29
30 int do_strace;
31
32 /*
33 * Utility functions
34 */
35
36 static void print_execve(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 abi_ulong arg_ptr_addr;
41 char *s;
42
43 s = lock_user_string(arg1);
44 if (s == NULL) {
45 return;
46 }
47 gemu_log("%s(\"%s\",{", name->name, s);
48 unlock_user(s, arg1, 0);
49
50 for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) {
51 abi_ulong *arg_ptr, arg_addr;
52
53 arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1);
54 if (!arg_ptr) {
55 return;
56 }
57 arg_addr = tswapl(*arg_ptr);
58 unlock_user(arg_ptr, arg_ptr_addr, 0);
59 if (!arg_addr) {
60 break;
61 }
62 if ((s = lock_user_string(arg_addr))) {
63 gemu_log("\"%s\",", s);
64 unlock_user(s, arg_addr, 0);
65 }
66 }
67 gemu_log("NULL})");
68 }
69
70 /*
71 * Variants for the return value output function
72 */
73
74 static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
75 {
76 if (ret == -1) {
77 gemu_log(" = -1 errno=%d (%s)\n", errno, strerror(errno));
78 } else {
79 gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
80 }
81 }
82
83 #if 0 /* currently unused */
84 static void
85 print_syscall_ret_raw(struct syscallname *name, abi_long ret)
86 {
87 gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
88 }
89 #endif
90
91 /*
92 * An array of all of the syscalls we know about
93 */
94
95 static const struct syscallname freebsd_scnames[] = {
96 #include "freebsd/strace.list"
97 };
98 static const struct syscallname netbsd_scnames[] = {
99 #include "netbsd/strace.list"
100 };
101 static const struct syscallname openbsd_scnames[] = {
102 #include "openbsd/strace.list"
103 };
104
105 static void print_syscall(int num, const struct syscallname *scnames,
106 unsigned int nscnames, abi_long arg1, abi_long arg2, abi_long arg3,
107 abi_long arg4, abi_long arg5, abi_long arg6)
108 {
109 unsigned int i;
110 const char *format="%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
111 TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
112 TARGET_ABI_FMT_ld ")";
113
114 gemu_log("%d ", getpid() );
115
116 for (i = 0; i < nscnames; i++) {
117 if (scnames[i].nr == num) {
118 if (scnames[i].call != NULL) {
119 scnames[i].call(&scnames[i], arg1, arg2, arg3, arg4, arg5,
120 arg6);
121 } else {
122 /* XXX: this format system is broken because it uses
123 host types and host pointers for strings */
124 if (scnames[i].format != NULL) {
125 format = scnames[i].format;
126 }
127 gemu_log(format, scnames[i].name, arg1, arg2, arg3, arg4, arg5,
128 arg6);
129 }
130 return;
131 }
132 }
133 gemu_log("Unknown syscall %d\n", num);
134 }
135
136 static void print_syscall_ret(int num, abi_long ret,
137 const struct syscallname *scnames, unsigned int nscnames)
138 {
139 unsigned int i;
140
141 for (i = 0; i < nscnames; i++) {
142 if (scnames[i].nr == num) {
143 if (scnames[i].result != NULL) {
144 scnames[i].result(&scnames[i], ret);
145 } else {
146 if (ret < 0) {
147 gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret,
148 strerror(-ret));
149 } else {
150 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
151 }
152 }
153 break;
154 }
155 }
156 }
157
158 /*
159 * The public interface to this module.
160 */
161 void print_freebsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
162 abi_long arg4, abi_long arg5, abi_long arg6)
163 {
164
165 print_syscall(num, freebsd_scnames, ARRAY_SIZE(freebsd_scnames), arg1, arg2,
166 arg3, arg4, arg5, arg6);
167 }
168
169 void print_freebsd_syscall_ret(int num, abi_long ret)
170 {
171
172 print_syscall_ret(num, ret, freebsd_scnames, ARRAY_SIZE(freebsd_scnames));
173 }
174
175 void print_netbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
176 abi_long arg4, abi_long arg5, abi_long arg6)
177 {
178
179 print_syscall(num, netbsd_scnames, ARRAY_SIZE(netbsd_scnames),
180 arg1, arg2, arg3, arg4, arg5, arg6);
181 }
182
183 void print_netbsd_syscall_ret(int num, abi_long ret)
184 {
185
186 print_syscall_ret(num, ret, netbsd_scnames, ARRAY_SIZE(netbsd_scnames));
187 }
188
189 void print_openbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
190 abi_long arg4, abi_long arg5, abi_long arg6)
191 {
192
193 print_syscall(num, openbsd_scnames, ARRAY_SIZE(openbsd_scnames), arg1, arg2,
194 arg3, arg4, arg5, arg6);
195 }
196
197 void print_openbsd_syscall_ret(int num, abi_long ret)
198 {
199
200 print_syscall_ret(num, ret, openbsd_scnames, ARRAY_SIZE(openbsd_scnames));
201 }