root / linux-user / strace.c @ 579a97f7
History | View | Annotate | Download (7.4 kB)
1 |
#include <stdio.h> |
---|---|
2 |
#include <errno.h> |
3 |
#include <sys/ipc.h> |
4 |
#include <sys/msg.h> |
5 |
#include <sys/sem.h> |
6 |
#include <sys/shm.h> |
7 |
#include <sys/select.h> |
8 |
#include <sys/types.h> |
9 |
#include <unistd.h> |
10 |
#include "qemu.h" |
11 |
|
12 |
int do_strace=0; |
13 |
|
14 |
struct syscallname {
|
15 |
int nr;
|
16 |
char *name;
|
17 |
char *format;
|
18 |
void (*call)(struct syscallname *, |
19 |
target_long, target_long, target_long, |
20 |
target_long, target_long, target_long); |
21 |
void (*result)(struct syscallname *, target_long); |
22 |
}; |
23 |
|
24 |
/*
|
25 |
* Utility functions
|
26 |
*/
|
27 |
static void |
28 |
print_ipc_cmd(int cmd)
|
29 |
{ |
30 |
#define output_cmd(val) \
|
31 |
if( cmd == val ) { \
|
32 |
gemu_log(#val); \
|
33 |
return; \
|
34 |
} |
35 |
|
36 |
cmd &= 0xff;
|
37 |
|
38 |
/* General IPC commands */
|
39 |
output_cmd( IPC_RMID ); |
40 |
output_cmd( IPC_SET ); |
41 |
output_cmd( IPC_STAT ); |
42 |
output_cmd( IPC_INFO ); |
43 |
/* msgctl() commands */
|
44 |
#ifdef __USER_MISC
|
45 |
output_cmd( MSG_STAT ); |
46 |
output_cmd( MSG_INFO ); |
47 |
#endif
|
48 |
/* shmctl() commands */
|
49 |
output_cmd( SHM_LOCK ); |
50 |
output_cmd( SHM_UNLOCK ); |
51 |
output_cmd( SHM_STAT ); |
52 |
output_cmd( SHM_INFO ); |
53 |
/* semctl() commands */
|
54 |
output_cmd( GETPID ); |
55 |
output_cmd( GETVAL ); |
56 |
output_cmd( GETALL ); |
57 |
output_cmd( GETNCNT ); |
58 |
output_cmd( GETZCNT ); |
59 |
output_cmd( SETVAL ); |
60 |
output_cmd( SETALL ); |
61 |
output_cmd( SEM_STAT ); |
62 |
output_cmd( SEM_INFO ); |
63 |
output_cmd( IPC_RMID ); |
64 |
output_cmd( IPC_RMID ); |
65 |
output_cmd( IPC_RMID ); |
66 |
output_cmd( IPC_RMID ); |
67 |
output_cmd( IPC_RMID ); |
68 |
output_cmd( IPC_RMID ); |
69 |
output_cmd( IPC_RMID ); |
70 |
output_cmd( IPC_RMID ); |
71 |
output_cmd( IPC_RMID ); |
72 |
|
73 |
/* Some value we don't recognize */
|
74 |
gemu_log("%d",cmd);
|
75 |
} |
76 |
|
77 |
static void |
78 |
print_fdset(int n, target_ulong target_fds_addr)
|
79 |
{ |
80 |
int i;
|
81 |
|
82 |
gemu_log("[");
|
83 |
if( target_fds_addr ) {
|
84 |
abi_long *target_fds; |
85 |
|
86 |
target_fds = lock_user(VERIFY_READ, |
87 |
target_fds_addr, |
88 |
sizeof(*target_fds)*(n / TARGET_ABI_BITS + 1), |
89 |
1);
|
90 |
|
91 |
if (!target_fds)
|
92 |
return;
|
93 |
|
94 |
for (i=n; i>=0; i--) { |
95 |
if ((tswapl(target_fds[i / TARGET_ABI_BITS]) >> (i & (TARGET_ABI_BITS - 1))) & 1) |
96 |
gemu_log("%d,", i );
|
97 |
} |
98 |
unlock_user(target_fds, target_fds_addr, 0);
|
99 |
} |
100 |
gemu_log("]");
|
101 |
} |
102 |
|
103 |
static void |
104 |
print_timeval(target_ulong tv_addr) |
105 |
{ |
106 |
if( tv_addr ) {
|
107 |
struct target_timeval *tv;
|
108 |
|
109 |
tv = lock_user(VERIFY_READ, tv_addr, sizeof(*tv), 1); |
110 |
if (!tv)
|
111 |
return;
|
112 |
gemu_log("{" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "}", |
113 |
tv->tv_sec, tv->tv_usec); |
114 |
unlock_user(tv, tv_addr, 0);
|
115 |
} else
|
116 |
gemu_log("NULL");
|
117 |
} |
118 |
|
119 |
/*
|
120 |
* Sysycall specific output functions
|
121 |
*/
|
122 |
|
123 |
/* select */
|
124 |
static long newselect_arg1 = 0; |
125 |
static long newselect_arg2 = 0; |
126 |
static long newselect_arg3 = 0; |
127 |
static long newselect_arg4 = 0; |
128 |
static long newselect_arg5 = 0; |
129 |
|
130 |
static void |
131 |
print_newselect(struct syscallname *name,
|
132 |
target_long arg1, target_long arg2, target_long arg3, |
133 |
target_long arg4, target_long arg5, target_long arg6) |
134 |
{ |
135 |
gemu_log("%s(" TARGET_FMT_ld ",", name->name, arg1); |
136 |
print_fdset(arg1, arg2); |
137 |
gemu_log(",");
|
138 |
print_fdset(arg1, arg3); |
139 |
gemu_log(",");
|
140 |
print_fdset(arg1, arg4); |
141 |
gemu_log(",");
|
142 |
print_timeval(arg5); |
143 |
gemu_log(")");
|
144 |
|
145 |
/* save for use in the return output function below */
|
146 |
newselect_arg1=arg1; |
147 |
newselect_arg2=arg2; |
148 |
newselect_arg3=arg3; |
149 |
newselect_arg4=arg4; |
150 |
newselect_arg5=arg5; |
151 |
} |
152 |
|
153 |
static void |
154 |
print_semctl(struct syscallname *name,
|
155 |
target_long arg1, target_long arg2, target_long arg3, |
156 |
target_long arg4, target_long arg5, target_long arg6) |
157 |
{ |
158 |
gemu_log("%s(" TARGET_FMT_ld "," TARGET_FMT_ld ",", name->name, arg1, arg2); |
159 |
print_ipc_cmd(arg3); |
160 |
gemu_log(",0x" TARGET_FMT_lx ")", arg4); |
161 |
} |
162 |
|
163 |
static void |
164 |
print_execve(struct syscallname *name,
|
165 |
target_long arg1, target_long arg2, target_long arg3, |
166 |
target_long arg4, target_long arg5, target_long arg6) |
167 |
{ |
168 |
target_ulong arg_ptr_addr; |
169 |
char *s;
|
170 |
|
171 |
if (!(s = lock_user_string(arg1)))
|
172 |
return;
|
173 |
gemu_log("%s(\"%s\",{", name->name, s);
|
174 |
unlock_user(s, arg1, 0);
|
175 |
|
176 |
for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(target_ulong)) { |
177 |
target_ulong *arg_ptr, arg_addr, s_addr; |
178 |
|
179 |
arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(target_ulong), 1); |
180 |
if (!arg_ptr)
|
181 |
return;
|
182 |
arg_addr = tswapl(*arg_ptr); |
183 |
unlock_user(arg_ptr, arg_ptr_addr, 0);
|
184 |
if (!arg_addr)
|
185 |
break;
|
186 |
if ((s = lock_user_string(arg_addr))) {
|
187 |
gemu_log("\"%s\",", s);
|
188 |
unlock_user(s, s_addr, 0);
|
189 |
} |
190 |
} |
191 |
|
192 |
gemu_log("NULL})");
|
193 |
} |
194 |
|
195 |
static void |
196 |
print_ipc(struct syscallname *name,
|
197 |
target_long arg1, target_long arg2, target_long arg3, |
198 |
target_long arg4, target_long arg5, target_long arg6) |
199 |
{ |
200 |
switch(arg1) {
|
201 |
case IPCOP_semctl:
|
202 |
name->name = "semctl";
|
203 |
print_semctl(name,arg2,arg3,arg4,arg5,arg6,0);
|
204 |
break;
|
205 |
default:
|
206 |
gemu_log("%s(" TARGET_FMT_ld "," TARGET_FMT_ld "," TARGET_FMT_ld "," TARGET_FMT_ld ")", |
207 |
name->name, arg1, arg2, arg3, arg4); |
208 |
} |
209 |
} |
210 |
|
211 |
/*
|
212 |
* Variants for the return value output function
|
213 |
*/
|
214 |
|
215 |
static void |
216 |
print_syscall_ret_addr(struct syscallname *name, target_long ret)
|
217 |
{ |
218 |
if( ret == -1 ) { |
219 |
gemu_log(" = -1 errno=%d (%s)\n", errno, target_strerror(errno));
|
220 |
} else {
|
221 |
gemu_log(" = " TARGET_FMT_lx "\n", ret); |
222 |
} |
223 |
} |
224 |
|
225 |
#if 0 /* currently unused */
|
226 |
static void
|
227 |
print_syscall_ret_raw(struct syscallname *name, target_long ret)
|
228 |
{
|
229 |
gemu_log(" = " TARGET_FMT_lx "\n", ret);
|
230 |
}
|
231 |
#endif
|
232 |
|
233 |
#ifdef TARGET_NR__newselect
|
234 |
static void |
235 |
print_syscall_ret_newselect(struct syscallname *name, target_long ret)
|
236 |
{ |
237 |
gemu_log(" = " TARGET_FMT_lx " (", ret); |
238 |
print_fdset(newselect_arg1,newselect_arg2); |
239 |
gemu_log(",");
|
240 |
print_fdset(newselect_arg1,newselect_arg3); |
241 |
gemu_log(",");
|
242 |
print_fdset(newselect_arg1,newselect_arg4); |
243 |
gemu_log(",");
|
244 |
print_timeval(newselect_arg5); |
245 |
gemu_log(")\n");
|
246 |
} |
247 |
#endif
|
248 |
|
249 |
/*
|
250 |
* An array of all of the syscalls we know about
|
251 |
*/
|
252 |
|
253 |
static struct syscallname scnames[] = { |
254 |
#include "strace.list" |
255 |
}; |
256 |
|
257 |
static int nsyscalls = sizeof(scnames)/sizeof(struct syscallname); |
258 |
|
259 |
/*
|
260 |
* The public interface to this module.
|
261 |
*/
|
262 |
void
|
263 |
print_syscall(int num,
|
264 |
target_long arg1, target_long arg2, target_long arg3, |
265 |
target_long arg4, target_long arg5, target_long arg6) |
266 |
{ |
267 |
int i;
|
268 |
char *format="%s(%ld,%ld,%ld,%ld,%ld,%ld)"; |
269 |
|
270 |
gemu_log("%d ", getpid() );
|
271 |
|
272 |
for(i=0;i<nsyscalls;i++) |
273 |
if( scnames[i].nr == num ) {
|
274 |
if( scnames[i].call != NULL ) { |
275 |
scnames[i].call(&scnames[i],arg1,arg2,arg3,arg4,arg5,arg6); |
276 |
} else {
|
277 |
if( scnames[i].format != NULL ) |
278 |
format = scnames[i].format; |
279 |
gemu_log(format,scnames[i].name, arg1,arg2,arg3,arg4,arg5,arg6); |
280 |
} |
281 |
break;
|
282 |
} |
283 |
} |
284 |
|
285 |
|
286 |
void
|
287 |
print_syscall_ret(int num, target_long ret)
|
288 |
{ |
289 |
int i;
|
290 |
|
291 |
for(i=0;i<nsyscalls;i++) |
292 |
if( scnames[i].nr == num ) {
|
293 |
if( scnames[i].result != NULL ) { |
294 |
scnames[i].result(&scnames[i],ret); |
295 |
} else {
|
296 |
if( ret < 0 ) { |
297 |
gemu_log(" = -1 errno=%d (%s)\n", -ret, target_strerror(-ret));
|
298 |
} else {
|
299 |
gemu_log(" = %d\n", ret);
|
300 |
} |
301 |
} |
302 |
break;
|
303 |
} |
304 |
} |
305 |
|