-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrace_creader.c
More file actions
executable file
·198 lines (175 loc) · 5.4 KB
/
trace_creader.c
File metadata and controls
executable file
·198 lines (175 loc) · 5.4 KB
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
/*
* a tool to read an execution trace and output its information
*
* Copyright (C) 2013 Juan Caballero <juan.caballero@imdea.org>
* All rights reserved.
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <inttypes.h>
#include <limits.h>
#include <xed-interface.h>
#include <trace60.h>
#include <trace_interface.h>
#include <trace_print.h>
#define OUTPUT_BUF 1024*1024*4
/* How often to report on trace progress */
#define PROGRESS_GAP 100000
/* Maximum file path length */
#define MAX_FILE_PATH_SIZE 128
static FILE * out_stream;
static char trace_file[MAX_FILE_PATH_SIZE] = {0};
static char output_file[MAX_FILE_PATH_SIZE] = {0};
static char outbuf[OUTPUT_BUF] = {0};
static int create_index = 0;
static int intel_format = 0;
static int print_header = 0;
static int print_counter = 0;
static int verbose = 0;
static unsigned long long last = ULLONG_MAX;
static unsigned long long first = 1LL;
void print_usage() {
fprintf(stderr, "Usage: trace_creader\n"
" -count <> Display instruction counter\n"
" -createindex <> Create trace index (no other output)\n"
" -first <int64> First instruction to read\n"
" -header <> Print trace header information and exit\n"
" -intel <> Use intel format insted of ATT\n"
" -last <int64> Last instruction to read\n"
" -out <string> Output file (default is stdout)\n"
" -trace <string> Name of input trace file\n"
" -v <> Verbose. Prints more info per instruction\n"
"\n"
);
}
/* Function to apply to all instruction that need printing */
int process_insn(x86_inst_t * insn, void * ctx) {
trace_interface_t * trace = (trace_interface_t *) ctx;
/* Print analysis progress */
if ((insn->insn_ctr % PROGRESS_GAP) == 0) {
int percent = (trace_get_curr_filepos(trace) * 101) /
trace->trace_byte_size;
print_percent(stderr, percent);
}
/* Print instruction */
print_insn(out_stream, insn, intel_format, verbose, print_counter);
return 0;
}
int main(int argc, char ** argv)
{
int opt = -1;
int long_index =0;
char *endptr = NULL;
size_t len;
static struct option long_options[] =
{
{"count", no_argument, 0, 'c'},
{"createindex", no_argument, 0, 'd'},
{"first", required_argument, 0, 'f'},
{"header", no_argument, 0, 'h'},
{"intel", no_argument, 0, 'i'},
{"last", required_argument, 0, 'l'},
{"out", required_argument, 0, 'o'},
{"trace", required_argument, 0, 't'},
{"verbose", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
while ((opt = getopt_long_only(argc, argv, "",
long_options, &long_index )) != -1)
{
switch (opt) {
case 'c':
print_counter = 1;
break;
case 'd':
create_index = 1;
break;
case 'f':
first = strtoull(optarg, &endptr, 0);
len = strlen(optarg);
if (endptr != optarg + len) {
fprintf(stderr, "error : invalid -first <int64> argument.\n");
return -1;
}
break;
case 'h':
print_header = 1;
break;
case 'i':
intel_format = 1;
break;
case 'l':
last = strtoull(optarg, &endptr, 0);
len = strlen(optarg);
if (endptr != optarg + len) {
fprintf(stderr, "error : invalid -last <int64> argument.\n");
return -1;
}
break;
case 'o':
sprintf(output_file, "%s", optarg);
break;
case 't':
sprintf(trace_file, "%s", optarg);
break;
case 'v':
verbose = 1;
break;
default:
print_usage();
return -1;
}
}
if(trace_file[0] == 0) {
print_usage();
return -1;
}
if(output_file[0] != 0 && (out_stream = fopen(output_file, "w")) == NULL) {
fprintf(stderr, "error : output file not accessible.\n");
return -1;
}
if(out_stream == NULL) {
out_stream = stdout;
}
setbuf(out_stream, outbuf);
/* If create index requested, loop over instructions and exit */
if (create_index) {
return trace_create_index(trace_file);
}
/* Open trace */
trace_interface_t * trace_iface = trace_open(trace_file);
if (trace_iface == NULL) {
return -1;
}
/* If requested, print header and exit */
if (print_header) {
print_trace_header(stdout, trace_iface, verbose);
goto cleanup;
}
/* Loop over instructions */
trace_fold(trace_iface, first, last, 0, process_insn, trace_iface);
/* Update progress */
print_percent(stderr, 100);
cleanup:
/* Close files */
trace_close(trace_iface);
fclose(out_stream);
return 0;
}