-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqemu-etrace.c
465 lines (418 loc) · 10.9 KB
/
qemu-etrace.c
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* QEMU etrace post-processing tool.
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "config.h"
#include "trace-open.h"
#include "coverage.h"
#include "syms.h"
#include "trace.h"
#include "etrace.h"
#include "trace-hex.h"
#include "util.h"
#include "run.h"
#include <bfd.h>
#include "trace-qemu-simple.h"
struct format_map {
const char *str;
int val;
};
struct format_map cov_fmt_map[] = {
{ "none", NONE },
{ "etrace", ETRACE },
{ "cachegrind", CACHEGRIND },
{ "gcov-bad", GCOV },
{ "qcov", QCOV },
{ "lcov", LCOV },
{ NULL, NONE },
};
struct format_map trace_fmt_map[] = {
{ "none", TRACE_NONE },
{ "etrace", TRACE_ETRACE },
{ "human", TRACE_HUMAN },
{ "vcd", TRACE_VCD },
{ "ascii-hex", TRACE_ASCII_HEX },
{ "ascii-hex-le16", TRACE_ASCII_HEX_LE16 },
{ "ascii-hex-le32", TRACE_ASCII_HEX_LE32 },
{ "ascii-hex-le64", TRACE_ASCII_HEX_LE64 },
{ "ascii-hex-be16", TRACE_ASCII_HEX_BE16 },
{ "ascii-hex-be32", TRACE_ASCII_HEX_BE32 },
{ "ascii-hex-be64", TRACE_ASCII_HEX_BE64 },
{ "qemu-simple", TRACE_QEMU_SIMPLE },
{ NULL, TRACE_NONE },
};
struct
{
char *trace_filename;
char *trace_output;
char *elf;
char *exclude;
char *addr2line;
char *dwarfdump;
char *nm;
char *objdump;
char *guest_objdump;
char *machine;
char *guest_machine;
enum trace_format trace_in_format;
enum trace_format trace_out_format;
enum cov_format coverage_format;
bool server;
char *coverage_output;
char *gcov_strip;
char *gcov_prefix;
} args = {
.trace_filename = NULL,
.trace_output = "-",
.trace_in_format = TRACE_ETRACE,
.trace_out_format = TRACE_HUMAN,
.elf = NULL,
.exclude = NULL,
.addr2line = "/usr/bin/addr2line",
.dwarfdump = "/usr/bin/dwarfdump",
.nm = "/usr/bin/nm",
.objdump = "/usr/bin/objdump",
.guest_objdump = "objdump",
.machine = NULL,
.guest_machine = NULL,
.coverage_format = NONE,
.coverage_output = NULL,
.gcov_strip = NULL,
.gcov_prefix = NULL,
.server = true,
};
static const char etrace_usagestr[] = \
"qemu-etrace " PACKAGE_VERSION "\n"
"-h | --help Help.\n"
"--trace Trace filename.\n"
"--trace-in-format Trace input format .\n"
"--trace-out-format Trace output format .\n"
"--trace-output Decoded trace output filename.\n"
"--elf Elf file of traced app.\n"
"--exclude Excludes description file.\n"
"--addr2line Path to addr2line binary.\n"
"--dwarfdump Path to dwarfdump binary.\n"
"--nm Path to nm binary.\n"
"--objdump Path to objdump. \n"
"--machine Host machine name. See objdump --help.\n"
"--guest-objdump Path to guest objdump.\n"
"--guest-machine Guest machine name. See objdump --help.\n"
"--gcov-strip Strip the specified prefix.\n"
"--gcov-prefix Prefix with the specified prefix.\n"
"--coverage-format Kind of coverage.\n"
"--coverage-output Coverage filename (if applicable).\n"
"\n";
void usage(void)
{
unsigned int i;
puts(etrace_usagestr);
printf("\nSupported trace formats:\n");
i = 0;
while (trace_fmt_map[i].str) {
printf("%s%s", i == 0 ? "" : ",", trace_fmt_map[i].str);
i++;
}
printf("\n");
printf("\nSupported coverage formats:\n");
i = 0;
while (cov_fmt_map[i].str) {
printf("%s%s", i == 0 ? "" : ",", cov_fmt_map[i].str);
i++;
}
printf("\n\n");
printf("The exclude fileformat is a list of filenames:line-numbers.\n"
"One per line. For example:\n"
"filename1.c:22\n"
"filename2.c:38\n\n");
}
int map_format(struct format_map *map, const char *s)
{
int i = 0;
while (map[i].str) {
if (strcmp(map[i].str, s) == 0)
return map[i].val;
i++;
}
fprintf(stderr, "Invalid coverage format %s\n", s);
usage();
exit(EXIT_FAILURE);
}
static inline enum cov_format map_covformat(const char *s)
{
return map_format(cov_fmt_map, s);
}
static inline enum trace_format map_traceformat(const char *s)
{
return map_format(trace_fmt_map, s);
}
static void parse_arguments(int argc, char **argv)
{
int c;
while (1) {
static struct option long_options[] = {
{"help", no_argument, 0, 'h' },
{"server", required_argument, 0, 's' },
{"trace", required_argument, 0, 't' },
{"trace-in-format", required_argument, 0, 'y' },
{"trace-out-format", required_argument, 0, 'q' },
{"trace-output", required_argument, 0, 'o' },
{"elf", required_argument, 0, 'e' },
{"exclude", required_argument, 0, 'l' },
{"addr2line", required_argument, 0, 'a' },
{"dwarfdump", required_argument, 0, 'w' },
{"nm", required_argument, 0, 'n' },
{"objdump", required_argument, 0, 'd' },
{"machine", required_argument, 0, 'm' },
{"guest-objdump", required_argument, 0, 'x' },
{"guest-machine", required_argument, 0, 'g' },
{"coverage-format", required_argument, 0, 'f' },
{"coverage-output", required_argument, 0, 'c' },
{"gcov-strip", required_argument, 0, 'z' },
{"gcov-prefix", required_argument, 0, 'p' },
{0, 0, 0, 0 }
};
int option_index = 0;
c = getopt_long(argc, argv, "bc:t:e:m:g:n:o:x:s:l:w:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'h':
usage();
exit(EXIT_SUCCESS);
break;
case 'c':
args.coverage_output = optarg;
break;
case 'f':
args.coverage_format = map_covformat(optarg);
break;
case 't':
args.trace_filename = optarg;
break;
case 'o':
args.trace_output = optarg;
break;
case 'e':
args.elf = optarg;
break;
case 'l':
args.exclude = optarg;
break;
case 'a':
args.addr2line = optarg;
break;
case 'w':
args.dwarfdump = optarg;
break;
case 'n':
args.nm = optarg;
break;
case 'd':
args.objdump = optarg;
break;
case 'x':
args.guest_objdump = optarg;
break;
case 'm':
args.machine = optarg;
break;
case 'g':
args.guest_machine = optarg;
break;
case 's':
args.server = strtol(optarg, NULL, 0);
break;
case 'z':
args.gcov_strip = optarg;
break;
case 'p':
args.gcov_prefix = optarg;
break;
case 'y':
args.trace_in_format = map_traceformat(optarg);
break;
case 'q':
args.trace_out_format = map_traceformat(optarg);
break;
default:
usage();
exit(EXIT_FAILURE);
break;
}
}
}
FILE *open_trace_output(const char *outname)
{
char *buf;
int fd;
FILE *fp;
/* None is special for output. If you want a file name none,
just use ./none as the name. */
if (!strcmp(outname, "none"))
return NULL;
fd = trace_open(outname, true);
if (fd < 0) {
perror(outname);
exit(1);
}
fp = fdopen(fd, "w");
if (!fp) {
perror(outname);
exit(1);
}
#define BUFSIZE (32 * 1024)
buf = safe_malloc(BUFSIZE);
setvbuf(fp, buf, _IOFBF, BUFSIZE);
#undef BUFSIZE
return fp;
}
void validate_arguments(void)
{
if (!args.trace_filename) {
fprintf(stderr, "No tracefile selected (--trace)\n");
exit(EXIT_FAILURE);
}
if (!args.coverage_output
&& (args.coverage_format != NONE
&& args.coverage_format != GCOV
&& args.coverage_format != QCOV)) {
fprintf(stderr, "A coverage format that needs an output filechosen\n"
" but no output file!\n"
"Try using the --coverage-output option.\n\n");
usage();
exit(EXIT_FAILURE);
}
}
sig_atomic_t got_sigint = false;
sig_atomic_t block_sigint_exit = false;
void sigint_handler(int s) {
got_sigint = true;
if (!block_sigint_exit) {
exit(EXIT_SUCCESS);
}
}
void trace_show(int fd, FILE *fp_out,
const char *objdump, const char *machine,
const char *guest_objdump, const char *guest_machine,
void **sym_tree, enum cov_format cov_fmt,
enum trace_format trace_in_fmt,
enum trace_format trace_out_fmt)
{
switch (trace_in_fmt) {
case TRACE_ETRACE:
etrace_show(fd, fp_out,
objdump, machine,
guest_objdump, guest_machine,
sym_tree, cov_fmt,
trace_in_fmt, trace_out_fmt);
break;
case TRACE_ASCII_HEX:
case TRACE_ASCII_HEX_LE16:
case TRACE_ASCII_HEX_LE32:
case TRACE_ASCII_HEX_LE64:
case TRACE_ASCII_HEX_BE16:
case TRACE_ASCII_HEX_BE32:
case TRACE_ASCII_HEX_BE64:
hextrace_show(fd, fp_out,
objdump, machine,
guest_objdump, guest_machine,
sym_tree, cov_fmt,
trace_in_fmt, trace_out_fmt);
break;
case TRACE_QEMU_SIMPLE:
qemu_simple_trace_show(fd, fp_out,
objdump, machine,
guest_objdump, guest_machine,
sym_tree, cov_fmt,
trace_in_fmt, trace_out_fmt);
break;
default:
fprintf(stderr, "Unsupported trace format\n");
usage();
exit(1);
break;
}
}
int main(int argc, char **argv)
{
FILE *trace_out;
void *sym_tree = NULL;
int fd;
bfd_init();
parse_arguments(argc, argv);
validate_arguments();
if (args.elf) {
sym_read_from_elf(&sym_tree, args.nm, args.elf);
if (args.coverage_format != NONE)
sym_build_linemap(&sym_tree, args.dwarfdump, args.elf);
}
coverage_init(&sym_tree, args.coverage_output, args.coverage_format,
args.gcov_strip, args.gcov_prefix);
trace_out = open_trace_output(args.trace_output);
{
struct sigaction shandler;
shandler.sa_handler = sigint_handler;
sigemptyset(&shandler.sa_mask);
shandler.sa_flags = 0;
sigaction(SIGINT, &shandler, NULL);
}
do {
fd = trace_open(args.trace_filename, false);
if (fd < 0) {
perror(args.trace_filename);
if (block_sigint_exit) {
break;
}
exit(EXIT_FAILURE);
}
/* If we are dealing with sockets, we want to handle
* SIGINT synchronously with a post process of the
* stats prior to exit().
*/
if (fd >=0 && fd_is_socket(fd)) {
block_sigint_exit = true;
}
trace_show(fd, trace_out,
args.objdump, args.machine,
args.guest_objdump, args.guest_machine,
&sym_tree, args.coverage_format,
args.trace_in_format,
args.trace_out_format);
} while (fd_is_socket(fd) && !got_sigint && args.server);
sym_show_stats(&sym_tree);
if (args.coverage_format != NONE)
coverage_emit(&sym_tree, args.coverage_output,
args.coverage_format,
args.gcov_strip, args.gcov_prefix,
args.exclude);
return EXIT_SUCCESS;
}