]> git.proxmox.com Git - mirror_qemu.git/blob - bsd-user/strace.c
bsd-user: Implement strace support for print_sysctl syscall
[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_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
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)
61 {
62 abi_ulong arg_ptr_addr;
63 char *s;
64
65 s = lock_user_string(arg1);
66 if (s == NULL) {
67 return;
68 }
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)) {
73 abi_ulong *arg_ptr, arg_addr;
74
75 arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1);
76 if (!arg_ptr) {
77 return;
78 }
79 arg_addr = tswapl(*arg_ptr);
80 unlock_user(arg_ptr, arg_ptr_addr, 0);
81 if (!arg_addr) {
82 break;
83 }
84 if ((s = lock_user_string(arg_addr))) {
85 gemu_log("\"%s\",", s);
86 unlock_user(s, arg_addr, 0);
87 }
88 }
89 gemu_log("NULL})");
90 }
91
92 /*
93 * Variants for the return value output function
94 */
95
96 static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
97 {
98 if (ret == -1) {
99 gemu_log(" = -1 errno=%d (%s)\n", errno, strerror(errno));
100 } else {
101 gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
102 }
103 }
104
105 #if 0 /* currently unused */
106 static void
107 print_syscall_ret_raw(struct syscallname *name, abi_long ret)
108 {
109 gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
110 }
111 #endif
112
113 /*
114 * An array of all of the syscalls we know about
115 */
116
117 static const struct syscallname freebsd_scnames[] = {
118 #include "freebsd/strace.list"
119 };
120 static const struct syscallname netbsd_scnames[] = {
121 #include "netbsd/strace.list"
122 };
123 static const struct syscallname openbsd_scnames[] = {
124 #include "openbsd/strace.list"
125 };
126
127 static void print_syscall(int num, const struct syscallname *scnames,
128 unsigned int nscnames, abi_long arg1, abi_long arg2, abi_long arg3,
129 abi_long arg4, abi_long arg5, abi_long arg6)
130 {
131 unsigned int i;
132 const char *format="%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
133 TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
134 TARGET_ABI_FMT_ld ")";
135
136 gemu_log("%d ", getpid() );
137
138 for (i = 0; i < nscnames; i++) {
139 if (scnames[i].nr == num) {
140 if (scnames[i].call != NULL) {
141 scnames[i].call(&scnames[i], arg1, arg2, arg3, arg4, arg5,
142 arg6);
143 } else {
144 /* XXX: this format system is broken because it uses
145 host types and host pointers for strings */
146 if (scnames[i].format != NULL) {
147 format = scnames[i].format;
148 }
149 gemu_log(format, scnames[i].name, arg1, arg2, arg3, arg4, arg5,
150 arg6);
151 }
152 return;
153 }
154 }
155 gemu_log("Unknown syscall %d\n", num);
156 }
157
158 static void print_syscall_ret(int num, abi_long ret,
159 const struct syscallname *scnames, unsigned int nscnames)
160 {
161 unsigned int i;
162
163 for (i = 0; i < nscnames; i++) {
164 if (scnames[i].nr == num) {
165 if (scnames[i].result != NULL) {
166 scnames[i].result(&scnames[i], ret);
167 } else {
168 if (ret < 0) {
169 gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret,
170 strerror(-ret));
171 } else {
172 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
173 }
174 }
175 break;
176 }
177 }
178 }
179
180 /*
181 * The public interface to this module.
182 */
183 void print_freebsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
184 abi_long arg4, abi_long arg5, abi_long arg6)
185 {
186
187 print_syscall(num, freebsd_scnames, ARRAY_SIZE(freebsd_scnames), arg1, arg2,
188 arg3, arg4, arg5, arg6);
189 }
190
191 void print_freebsd_syscall_ret(int num, abi_long ret)
192 {
193
194 print_syscall_ret(num, ret, freebsd_scnames, ARRAY_SIZE(freebsd_scnames));
195 }
196
197 void print_netbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
198 abi_long arg4, abi_long arg5, abi_long arg6)
199 {
200
201 print_syscall(num, netbsd_scnames, ARRAY_SIZE(netbsd_scnames),
202 arg1, arg2, arg3, arg4, arg5, arg6);
203 }
204
205 void print_netbsd_syscall_ret(int num, abi_long ret)
206 {
207
208 print_syscall_ret(num, ret, netbsd_scnames, ARRAY_SIZE(netbsd_scnames));
209 }
210
211 void print_openbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
212 abi_long arg4, abi_long arg5, abi_long arg6)
213 {
214
215 print_syscall(num, openbsd_scnames, ARRAY_SIZE(openbsd_scnames), arg1, arg2,
216 arg3, arg4, arg5, arg6);
217 }
218
219 void print_openbsd_syscall_ret(int num, abi_long ret)
220 {
221
222 print_syscall_ret(num, ret, openbsd_scnames, ARRAY_SIZE(openbsd_scnames));
223 }