1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
*/
/*
* Virtual hardware for bridging the FUSE kernel module
* in the emulated OS and outside file system
*/
#include "qemu_file.h"
#include "goldfish_trace.h"
#include "sysemu.h"
#include "android-trace.h"
#ifdef CONFIG_MEMCHECK
#include "memcheck/memcheck.h"
#include "memcheck/memcheck_util.h"
#endif // CONFIG_MEMCHECK
/* Set to 1 to debug tracing */
#define DEBUG 0
#if DEBUG
# define D(...) printf(__VA_ARGS__), fflush(stdout)
#else
# define D(...) ((void)0)
#endif
/* Set to 1 to debug PID tracking */
#define DEBUG_PID 0
#if DEBUG_PID
# define DPID(...) printf(__VA_ARGS__), fflush(stdout)
#else
# define DPID(...) ((void)0)
#endif
extern void cpu_loop_exit(void);
extern int tracing;
extern const char *trace_filename;
/* for execve */
static char exec_path[CLIENT_PAGE_SIZE];
static char exec_arg[CLIENT_PAGE_SIZE];
static unsigned long vstart; // VM start
static unsigned long vend; // VM end
static unsigned long eoff; // offset in EXE file
static unsigned cmdlen; // cmdline length
static unsigned pid; // PID (really thread id)
static unsigned tgid; // thread group id (really process id)
static unsigned tid; // current thread id (same as pid, most of the time)
static unsigned long dsaddr; // dynamic symbol address
static unsigned long unmap_start; // start address to unmap
/* for context switch */
//static unsigned long cs_pid; // context switch PID
/* I/O write */
static void trace_dev_write(void *opaque, target_phys_addr_t offset, uint32_t value)
{
trace_dev_state *s = (trace_dev_state *)opaque;
(void)s;
switch (offset >> 2) {
case TRACE_DEV_REG_SWITCH: // context switch, switch to pid
DPID("QEMU.trace: context switch tid=%u\n", value);
if (trace_filename != NULL) {
trace_switch(value);
D("QEMU.trace: kernel, context switch %u\n", value);
}
#ifdef CONFIG_MEMCHECK
if (memcheck_enabled) {
memcheck_switch(value);
}
#endif // CONFIG_MEMCHECK
tid = (unsigned) value;
break;
case TRACE_DEV_REG_TGID: // save the tgid for the following fork/clone
DPID("QEMU.trace: tgid=%u\n", value);
tgid = value;
if (trace_filename != NULL) {
D("QEMU.trace: kernel, tgid %u\n", value);
}
break;
case TRACE_DEV_REG_FORK: // fork, fork new pid
DPID("QEMU.trace: fork (pid=%d tgid=%d value=%d)\n", pid, tgid, value);
if (trace_filename != NULL) {
trace_fork(tgid, value);
D("QEMU.trace: kernel, fork %u\n", value);
}
#ifdef CONFIG_MEMCHECK
if (memcheck_enabled) {
memcheck_fork(tgid, value);
}
#endif // CONFIG_MEMCHECK
break;
case TRACE_DEV_REG_CLONE: // fork, clone new pid (i.e. thread)
DPID("QEMU.trace: clone (pid=%d tgid=%d value=%d)\n", pid, tgid, value);
if (trace_filename != NULL) {
trace_clone(tgid, value);
D("QEMU.trace: kernel, clone %u\n", value);
}
#ifdef CONFIG_MEMCHECK
if (memcheck_enabled) {
memcheck_clone(tgid, value);
}
#endif // CONFIG_MEMCHECK
break;
case TRACE_DEV_REG_EXECVE_VMSTART: // execve, vstart
vstart = value;
break;
case TRACE_DEV_REG_EXECVE_VMEND: // execve, vend
vend = value;
break;
case TRACE_DEV_REG_EXECVE_OFFSET: // execve, offset in EXE
eoff = value;
break;
case TRACE_DEV_REG_EXECVE_EXEPATH: // init exec, path of EXE
vstrcpy(value, exec_path, CLIENT_PAGE_SIZE);
if (trace_filename != NULL) {
trace_init_exec(vstart, vend, eoff, exec_path);
D("QEMU.trace: kernel, init exec [%lx,%lx]@%lx [%s]\n",
vstart, vend, eoff, exec_path);
}
#ifdef CONFIG_MEMCHECK
if (memcheck_enabled) {
if (exec_path[0] == '\0') {
// vstrcpy may fail to copy path. In this case lets do it
// differently.
memcheck_get_guest_kernel_string(exec_path, value, CLIENT_PAGE_SIZE);
}
memcheck_mmap_exepath(vstart, vend, eoff, exec_path);
}
#endif // CONFIG_MEMCHECK
exec_path[0] = 0;
break;
case TRACE_DEV_REG_CMDLINE_LEN: // execve, process cmdline length
cmdlen = value;
break;
case TRACE_DEV_REG_CMDLINE: // execve, process cmdline
cpu_memory_rw_debug(cpu_single_env, value, (uint8_t*)exec_arg, cmdlen, 0);
if (trace_filename != NULL) {
trace_execve(exec_arg, cmdlen);
}
#ifdef CONFIG_MEMCHECK
if (memcheck_enabled) {
memcheck_set_cmd_line(exec_arg, cmdlen);
}
#endif // CONFIG_MEMCHECK
#if DEBUG || DEBUG_PID
if (trace_filename != NULL) {
int i;
for (i = 0; i < cmdlen; i ++)
if (i != cmdlen - 1 && exec_arg[i] == 0)
exec_arg[i] = ' ';
printf("QEMU.trace: kernel, execve %s[%d]\n", exec_arg, cmdlen);
exec_arg[0] = 0;
}
#endif
break;
case TRACE_DEV_REG_EXIT: // exit, exit current process with exit code
DPID("QEMU.trace: exit tid=%u\n", value);
if (trace_filename != NULL) {
trace_exit(value);
D("QEMU.trace: kernel, exit %x\n", value);
}
#ifdef CONFIG_MEMCHECK
if (memcheck_enabled) {
memcheck_exit(value);
}
#endif // CONFIG_MEMCHECK
break;
case TRACE_DEV_REG_NAME: // record thread name
vstrcpy(value, exec_path, CLIENT_PAGE_SIZE);
DPID("QEMU.trace: thread name=%s\n", exec_path);
// Remove the trailing newline if it exists
int len = strlen(exec_path);
if (exec_path[len - 1] == '\n') {
exec_path[len - 1] = 0;
}
if (trace_filename != NULL) {
trace_name(exec_path);
D("QEMU.trace: kernel, name %s\n", exec_path);
}
break;
case TRACE_DEV_REG_MMAP_EXEPATH: // mmap, path of EXE, the others are same as execve
vstrcpy(value, exec_path, CLIENT_PAGE_SIZE);
DPID("QEMU.trace: mmap exe=%s\n", exec_path);
if (trace_filename != NULL) {
trace_mmap(vstart, vend, eoff, exec_path);
D("QEMU.trace: kernel, mmap [%lx,%lx]@%lx [%s]\n", vstart, vend, eoff, exec_path);
}
#ifdef CONFIG_MEMCHECK
if (memcheck_enabled) {
if (exec_path[0] == '\0') {
// vstrcpy may fail to copy path. In this case lets do it
// differently.
memcheck_get_guest_kernel_string(exec_path, value, CLIENT_PAGE_SIZE);
}
memcheck_mmap_exepath(vstart, vend, eoff, exec_path);
}
#endif // CONFIG_MEMCHECK
exec_path[0] = 0;
break;
case TRACE_DEV_REG_INIT_PID: // init, name the pid that starts before device registered
pid = value;
DPID("QEMU.trace: pid=%d\n", value);
#ifdef CONFIG_MEMCHECK
if (memcheck_enabled) {
memcheck_init_pid(value);
}
#endif // CONFIG_MEMCHECK
break;
case TRACE_DEV_REG_INIT_NAME: // init, the comm of the init pid
vstrcpy(value, exec_path, CLIENT_PAGE_SIZE);
DPID("QEMU.trace: tgid=%d pid=%d name=%s\n", tgid, pid, exec_path);
if (trace_filename != NULL) {
trace_init_name(tgid, pid, exec_path);
D("QEMU.trace: kernel, init name %u [%s]\n", pid, exec_path);
}
exec_path[0] = 0;
break;
case TRACE_DEV_REG_DYN_SYM_ADDR: // dynamic symbol address
dsaddr = value;
break;
case TRACE_DEV_REG_DYN_SYM: // add dynamic symbol
vstrcpy(value, exec_arg, CLIENT_PAGE_SIZE);
if (trace_filename != NULL) {
trace_dynamic_symbol_add(dsaddr, exec_arg);
D("QEMU.trace: dynamic symbol %lx:%s\n", dsaddr, exec_arg);
}
exec_arg[0] = 0;
break;
case TRACE_DEV_REG_REMOVE_ADDR: // remove dynamic symbol addr
if (trace_filename != NULL) {
trace_dynamic_symbol_remove(value);
D("QEMU.trace: dynamic symbol remove %lx\n", dsaddr);
}
break;
case TRACE_DEV_REG_PRINT_STR: // print string
vstrcpy(value, exec_arg, CLIENT_PAGE_SIZE);
printf("%s", exec_arg);
exec_arg[0] = 0;
break;
case TRACE_DEV_REG_PRINT_NUM_DEC: // print number in decimal
printf("%d", value);
break;
case TRACE_DEV_REG_PRINT_NUM_HEX: // print number in hexical
printf("%x", value);
break;
case TRACE_DEV_REG_STOP_EMU: // stop the VM execution
if (trace_filename != NULL) {
// To ensure that the number of instructions executed in this
// block is correct, we pretend that there was an exception.
trace_exception(0);
}
cpu_single_env->exception_index = EXCP_HLT;
cpu_single_env->halted = 1;
qemu_system_shutdown_request();
cpu_loop_exit();
break;
case TRACE_DEV_REG_ENABLE: // tracing enable: 0 = stop, 1 = start
if (value == 1) {
if (trace_filename != NULL) {
start_tracing();
}
}
else if (value == 0) {
if (trace_filename != NULL) {
stop_tracing();
// To ensure that the number of instructions executed in this
// block is correct, we pretend that there was an exception.
trace_exception(0);
}
}
break;
case TRACE_DEV_REG_UNMAP_START:
unmap_start = value;
break;
case TRACE_DEV_REG_UNMAP_END:
if (trace_filename != NULL) {
trace_munmap(unmap_start, value);
}
#ifdef CONFIG_MEMCHECK
if (memcheck_enabled) {
memcheck_unmap(unmap_start, value);
}
#endif // CONFIG_MEMCHECK
break;
case TRACE_DEV_REG_METHOD_ENTRY:
case TRACE_DEV_REG_METHOD_EXIT:
case TRACE_DEV_REG_METHOD_EXCEPTION:
case TRACE_DEV_REG_NATIVE_ENTRY:
case TRACE_DEV_REG_NATIVE_EXIT:
case TRACE_DEV_REG_NATIVE_EXCEPTION:
if (trace_filename != NULL) {
if (tracing) {
int call_type = (offset - 4096) >> 2;
trace_interpreted_method(value, call_type);
}
}
break;
#ifdef CONFIG_MEMCHECK
case TRACE_DEV_REG_MALLOC:
if (memcheck_enabled) {
memcheck_guest_alloc(value);
}
break;
case TRACE_DEV_REG_FREE_PTR:
if (memcheck_enabled) {
memcheck_guest_free(value);
}
break;
case TRACE_DEV_REG_QUERY_MALLOC:
if (memcheck_enabled) {
memcheck_guest_query_malloc(value);
}
break;
case TRACE_DEV_REG_LIBC_INIT:
if (memcheck_enabled) {
memcheck_guest_libc_initialized(value);
}
break;
case TRACE_DEV_REG_PRINT_USER_STR:
if (memcheck_enabled) {
memcheck_guest_print_str(value);
}
break;
#endif // CONFIG_MEMCHECK
default:
if (offset < 4096) {
cpu_abort(cpu_single_env, "trace_dev_write: Bad offset %x\n", offset);
} else {
D("%s: offset=%d (0x%x) value=%d (0x%x)\n", __FUNCTION__, offset,
offset, value, value);
}
break;
}
}
/* I/O read */
static uint32_t trace_dev_read(void *opaque, target_phys_addr_t offset)
{
trace_dev_state *s = (trace_dev_state *)opaque;
(void)s;
switch (offset >> 2) {
case TRACE_DEV_REG_ENABLE: // tracing enable
return tracing;
default:
if (offset < 4096) {
cpu_abort(cpu_single_env, "trace_dev_read: Bad offset %x\n", offset);
} else {
D("%s: offset=%d (0x%x)\n", __FUNCTION__, offset, offset);
}
return 0;
}
return 0;
}
static CPUReadMemoryFunc *trace_dev_readfn[] = {
trace_dev_read,
trace_dev_read,
trace_dev_read
};
static CPUWriteMemoryFunc *trace_dev_writefn[] = {
trace_dev_write,
trace_dev_write,
trace_dev_write
};
/* initialize the trace device */
void trace_dev_init()
{
trace_dev_state *s;
s = (trace_dev_state *)qemu_mallocz(sizeof(trace_dev_state));
s->dev.name = "qemu_trace";
s->dev.id = -1;
s->dev.base = 0; // will be allocated dynamically
s->dev.size = 0x2000;
s->dev.irq = 0;
s->dev.irq_count = 0;
goldfish_device_add(&s->dev, trace_dev_readfn, trace_dev_writefn, s);
exec_path[0] = exec_arg[0] = '\0';
}
|