xref: /openbmc/qemu/bsd-user/strace.c (revision bfb27e60)
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 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 
111 /*
112  * Variants for the return value output function
113  */
114 
115 static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
116 {
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);
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 
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)
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 
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;
167                 }
168                 gemu_log(format, scnames[i].name, arg1, arg2, arg3, arg4, arg5,
169                         arg6);
170             }
171             return;
172         }
173     }
174     gemu_log("Unknown syscall %d\n", num);
175 }
176 
177 static void print_syscall_ret(int num, abi_long ret,
178         const struct syscallname *scnames, unsigned int nscnames)
179 {
180     unsigned int i;
181 
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);
192                 }
193             }
194             break;
195         }
196     }
197 }
198 
199 /*
200  * The public interface to this module.
201  */
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)
204 {
205 
206     print_syscall(num, freebsd_scnames, ARRAY_SIZE(freebsd_scnames), arg1, arg2,
207             arg3, arg4, arg5, arg6);
208 }
209 
210 void print_freebsd_syscall_ret(int num, abi_long ret)
211 {
212 
213     print_syscall_ret(num, ret, freebsd_scnames, ARRAY_SIZE(freebsd_scnames));
214 }
215 
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)
218 {
219 
220     print_syscall(num, netbsd_scnames, ARRAY_SIZE(netbsd_scnames),
221                   arg1, arg2, arg3, arg4, arg5, arg6);
222 }
223 
224 void print_netbsd_syscall_ret(int num, abi_long ret)
225 {
226 
227     print_syscall_ret(num, ret, netbsd_scnames, ARRAY_SIZE(netbsd_scnames));
228 }
229 
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)
232 {
233 
234     print_syscall(num, openbsd_scnames, ARRAY_SIZE(openbsd_scnames), arg1, arg2,
235             arg3, arg4, arg5, arg6);
236 }
237 
238 void print_openbsd_syscall_ret(int num, abi_long ret)
239 {
240 
241     print_syscall_ret(num, ret, openbsd_scnames, ARRAY_SIZE(openbsd_scnames));
242 }
243