forked from rr-debugger/rr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDumpCommand.cc
291 lines (258 loc) · 8.97 KB
/
DumpCommand.cc
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
/* -*- Mode: C++; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
#include <inttypes.h>
#include <limits>
#include "preload/preload_interface.h"
#include "AddressSpace.h"
#include "Command.h"
#include "TraceStream.h"
#include "core.h"
#include "kernel_metadata.h"
#include "main.h"
#include "util.h"
using namespace std;
namespace rr {
class DumpCommand : public Command {
public:
virtual int run(vector<string>& args) override;
protected:
DumpCommand(const char* name, const char* help) : Command(name, help) {}
static DumpCommand singleton;
};
DumpCommand DumpCommand::singleton(
"dump",
" rr dump [OPTIONS] [<trace_dir>] [<event-spec>...]\n"
" Event specs can be either an event number like `127', or a range\n"
" like `1000-5000'. By default, all events are dumped.\n"
" -b, --syscallbuf dump syscallbuf contents\n"
" -m, --recorded-metadata dump recorded data metadata\n"
" -p, --mmaps dump mmap data\n"
" -r, --raw dump trace frames in a more easily\n"
" machine-parseable format instead of the\n"
" default human-readable format\n"
" -s, --statistics dump statistics about the trace\n"
" -t, --tid=<pid> dump events only for the specified tid\n");
struct DumpFlags {
bool dump_syscallbuf;
bool dump_recorded_data_metadata;
bool dump_mmaps;
bool raw_dump;
bool dump_statistics;
int only_tid;
DumpFlags()
: dump_syscallbuf(false),
dump_recorded_data_metadata(false),
dump_mmaps(false),
raw_dump(false),
dump_statistics(false),
only_tid(0) {}
};
static bool parse_dump_arg(vector<string>& args, DumpFlags& flags) {
if (parse_global_option(args)) {
return true;
}
static const OptionSpec options[] = {
{ 'b', "syscallbuf", NO_PARAMETER },
{ 'm', "recorded-metadata", NO_PARAMETER },
{ 'p', "mmaps", NO_PARAMETER },
{ 'r', "raw", NO_PARAMETER },
{ 's', "statistics", NO_PARAMETER },
{ 't', "tid", HAS_PARAMETER },
};
ParsedOption opt;
if (!Command::parse_option(args, options, &opt)) {
return false;
}
switch (opt.short_name) {
case 'b':
flags.dump_syscallbuf = true;
break;
case 'm':
flags.dump_recorded_data_metadata = true;
break;
case 'p':
flags.dump_mmaps = true;
break;
case 'r':
flags.raw_dump = true;
break;
case 's':
flags.dump_statistics = true;
break;
case 't':
if (!opt.verify_valid_int(1, INT32_MAX)) {
return false;
}
flags.only_tid = opt.int_value;
break;
default:
DEBUG_ASSERT(0 && "Unknown option");
}
return true;
}
static void dump_syscallbuf_data(TraceReader& trace, FILE* out,
const TraceFrame& frame) {
if (frame.event().type() != EV_SYSCALLBUF_FLUSH) {
return;
}
auto buf = trace.read_raw_data();
size_t bytes_remaining = buf.data.size() - sizeof(struct syscallbuf_hdr);
auto flush_hdr = reinterpret_cast<const syscallbuf_hdr*>(buf.data.data());
if (flush_hdr->num_rec_bytes > bytes_remaining) {
fprintf(stderr, "Malformed trace file (bad recorded-bytes count)\n");
notifying_abort();
}
bytes_remaining = flush_hdr->num_rec_bytes;
auto record_ptr = reinterpret_cast<const uint8_t*>(flush_hdr + 1);
auto end_ptr = record_ptr + bytes_remaining;
while (record_ptr < end_ptr) {
auto record = reinterpret_cast<const struct syscallbuf_record*>(record_ptr);
// Buffered syscalls always use the task arch
fprintf(out, " { syscall:'%s', ret:0x%lx, size:0x%lx }\n",
syscall_name(record->syscallno, frame.regs().arch()).c_str(),
(long)record->ret, (long)record->size);
if (record->size < sizeof(*record)) {
fprintf(stderr, "Malformed trace file (bad record size)\n");
notifying_abort();
}
record_ptr += stored_record_size(record->size);
}
}
/**
* Dump all events from the current to trace that match |spec| to
* |out|. |spec| has the following syntax: /\d+(-\d+)?/, expressing
* either a single event number of a range, and may be null to
* indicate "dump all events".
*
* This function is side-effect-y, in that the trace file isn't
* rewound in between matching each spec. Therefore specs should be
* constructed so as to match properly on a serial linear scan; that
* is, they should comprise disjoint and monotonically increasing
* event sets. No attempt is made to enforce this or normalize specs.
*/
static void dump_events_matching(TraceReader& trace, const DumpFlags& flags,
FILE* out, const string* spec) {
uint32_t start = 0, end = numeric_limits<uint32_t>::max();
// Try to parse the "range" syntax '[start]-[end]'.
if (spec && 2 > sscanf(spec->c_str(), "%u-%u", &start, &end)) {
// Fall back on assuming the spec is a single event
// number, however it parses out with atoi().
start = end = atoi(spec->c_str());
}
bool process_raw_data =
flags.dump_syscallbuf || flags.dump_recorded_data_metadata;
while (!trace.at_end()) {
auto frame = trace.read_frame();
if (end < frame.time()) {
return;
}
if (start <= frame.time() && frame.time() <= end &&
(!flags.only_tid || flags.only_tid == frame.tid())) {
if (flags.raw_dump) {
frame.dump_raw(out);
} else {
frame.dump(out);
}
if (flags.dump_syscallbuf) {
dump_syscallbuf_data(trace, out, frame);
}
while (true) {
TraceReader::MappedData data;
bool found;
KernelMapping km =
trace.read_mapped_region(&data, &found, TraceReader::DONT_VALIDATE);
if (!found) {
break;
}
if (flags.dump_mmaps) {
char prot_flags[] = "rwxp";
if (!(km.prot() & PROT_READ)) {
prot_flags[0] = '-';
}
if (!(km.prot() & PROT_WRITE)) {
prot_flags[1] = '-';
}
if (!(km.prot() & PROT_EXEC)) {
prot_flags[2] = '-';
}
if (km.flags() & MAP_SHARED) {
prot_flags[3] = 's';
}
const char* fsname = km.fsname().c_str();
if (data.source == TraceReader::SOURCE_ZERO) {
static const char source_zero[] = "<ZERO>";
fsname = source_zero;
}
fprintf(out, " { map_file:\"%s\", addr:%p, length:%p, "
"prot_flags:\"%s\", file_offset:0x%llx, "
"device:%lld, inode:%lld, "
"data_file:\"%s\", data_offset:0x%llx, "
"file_size:0x%llx }\n",
fsname, (void*)km.start().as_int(), (void*)km.size(),
prot_flags, (long long)km.file_offset_bytes(),
(long long)km.device(), (long long)km.inode(),
data.file_name.c_str(), (long long)data.data_offset_bytes,
(long long)data.file_size_bytes);
}
}
TraceReader::RawDataMetadata data;
while (process_raw_data && trace.read_raw_data_metadata_for_frame(data)) {
if (flags.dump_recorded_data_metadata) {
fprintf(out, " { tid:%d, addr:%p, length:%p }\n", data.rec_tid,
(void*)data.addr.as_int(), (void*)data.size);
}
}
if (!flags.raw_dump) {
fprintf(out, "}\n");
}
} else {
while (true) {
TraceReader::MappedData data;
KernelMapping km = trace.read_mapped_region(&data, nullptr,
TraceReader::DONT_VALIDATE);
if (km.size() == 0) {
break;
}
}
}
}
}
static void dump_statistics(const TraceReader& trace, FILE* out) {
uint64_t uncompressed = trace.uncompressed_bytes();
uint64_t compressed = trace.compressed_bytes();
fprintf(out, "// Uncompressed bytes %" PRIu64 ", compressed bytes %" PRIu64
", ratio %.2fx\n",
uncompressed, compressed, double(uncompressed) / compressed);
}
static void dump(const string& trace_dir, const DumpFlags& flags,
const vector<string>& specs, FILE* out) {
TraceReader trace(trace_dir);
if (flags.raw_dump) {
fprintf(out, "global_time tid reason ticks "
"hw_interrupts page_faults instructions "
"eax ebx ecx edx esi edi ebp orig_eax esp eip eflags\n");
}
if (specs.size() > 0) {
for (size_t i = 0; i < specs.size(); ++i) {
dump_events_matching(trace, flags, stdout, &specs[i]);
}
} else {
// No specs => dump all events.
dump_events_matching(trace, flags, stdout, nullptr /*all events*/);
}
if (flags.dump_statistics) {
dump_statistics(trace, stdout);
}
}
int DumpCommand::run(vector<string>& args) {
DumpFlags flags;
while (parse_dump_arg(args, flags)) {
}
string trace_dir;
if (!parse_optional_trace_dir(args, &trace_dir)) {
print_help(stderr);
return 1;
}
dump(trace_dir, flags, args, stdout);
return 0;
}
} // namespace rr