CommandObjectReproducer.cpp
15.9 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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//===-- CommandObjectReproducer.cpp -----------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "CommandObjectReproducer.h"
#include "lldb/Host/OptionParser.h"
#include "lldb/Utility/GDBRemote.h"
#include "lldb/Utility/Reproducer.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/OptionArgParser.h"
#include <csignal>
using namespace lldb;
using namespace llvm;
using namespace lldb_private;
using namespace lldb_private::repro;
enum ReproducerProvider {
eReproducerProviderCommands,
eReproducerProviderFiles,
eReproducerProviderGDB,
eReproducerProviderVersion,
eReproducerProviderWorkingDirectory,
eReproducerProviderNone
};
static constexpr OptionEnumValueElement g_reproducer_provider_type[] = {
{
eReproducerProviderCommands,
"commands",
"Command Interpreter Commands",
},
{
eReproducerProviderFiles,
"files",
"Files",
},
{
eReproducerProviderGDB,
"gdb",
"GDB Remote Packets",
},
{
eReproducerProviderVersion,
"version",
"Version",
},
{
eReproducerProviderWorkingDirectory,
"cwd",
"Working Directory",
},
{
eReproducerProviderNone,
"none",
"None",
},
};
static constexpr OptionEnumValues ReproducerProviderType() {
return OptionEnumValues(g_reproducer_provider_type);
}
#define LLDB_OPTIONS_reproducer_dump
#include "CommandOptions.inc"
enum ReproducerCrashSignal {
eReproducerCrashSigill,
eReproducerCrashSigsegv,
};
static constexpr OptionEnumValueElement g_reproducer_signaltype[] = {
{
eReproducerCrashSigill,
"SIGILL",
"Illegal instruction",
},
{
eReproducerCrashSigsegv,
"SIGSEGV",
"Segmentation fault",
},
};
static constexpr OptionEnumValues ReproducerSignalType() {
return OptionEnumValues(g_reproducer_signaltype);
}
#define LLDB_OPTIONS_reproducer_xcrash
#include "CommandOptions.inc"
class CommandObjectReproducerGenerate : public CommandObjectParsed {
public:
CommandObjectReproducerGenerate(CommandInterpreter &interpreter)
: CommandObjectParsed(
interpreter, "reproducer generate",
"Generate reproducer on disk. When the debugger is in capture "
"mode, this command will output the reproducer to a directory on "
"disk and quit. In replay mode this command in a no-op.",
nullptr) {}
~CommandObjectReproducerGenerate() override = default;
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
if (!command.empty()) {
result.AppendErrorWithFormat("'%s' takes no arguments",
m_cmd_name.c_str());
return false;
}
auto &r = Reproducer::Instance();
if (auto generator = r.GetGenerator()) {
generator->Keep();
} else if (r.IsReplaying()) {
// Make this operation a NO-OP in replay mode.
result.SetStatus(eReturnStatusSuccessFinishNoResult);
return result.Succeeded();
} else {
result.AppendErrorWithFormat("Unable to get the reproducer generator");
result.SetStatus(eReturnStatusFailed);
return false;
}
result.GetOutputStream()
<< "Reproducer written to '" << r.GetReproducerPath() << "'\n";
result.GetOutputStream()
<< "Please have a look at the directory to assess if you're willing to "
"share the contained information.\n";
m_interpreter.BroadcastEvent(
CommandInterpreter::eBroadcastBitQuitCommandReceived);
result.SetStatus(eReturnStatusQuit);
return result.Succeeded();
}
};
class CommandObjectReproducerXCrash : public CommandObjectParsed {
public:
CommandObjectReproducerXCrash(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "reproducer xcrash",
"Intentionally force the debugger to crash in "
"order to trigger and test reproducer generation.",
nullptr) {}
~CommandObjectReproducerXCrash() override = default;
Options *GetOptions() override { return &m_options; }
class CommandOptions : public Options {
public:
CommandOptions() : Options() {}
~CommandOptions() override = default;
Status SetOptionValue(uint32_t option_idx, StringRef option_arg,
ExecutionContext *execution_context) override {
Status error;
const int short_option = m_getopt_table[option_idx].val;
switch (short_option) {
case 's':
signal = (ReproducerCrashSignal)OptionArgParser::ToOptionEnum(
option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
if (!error.Success())
error.SetErrorStringWithFormat("unrecognized value for signal '%s'",
option_arg.str().c_str());
break;
default:
llvm_unreachable("Unimplemented option");
}
return error;
}
void OptionParsingStarting(ExecutionContext *execution_context) override {
signal = eReproducerCrashSigsegv;
}
ArrayRef<OptionDefinition> GetDefinitions() override {
return makeArrayRef(g_reproducer_xcrash_options);
}
ReproducerCrashSignal signal = eReproducerCrashSigsegv;
};
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
if (!command.empty()) {
result.AppendErrorWithFormat("'%s' takes no arguments",
m_cmd_name.c_str());
return false;
}
auto &r = Reproducer::Instance();
if (!r.IsCapturing() && !r.IsReplaying()) {
result.SetError(
"forcing a crash is only supported when capturing a reproducer.");
result.SetStatus(eReturnStatusSuccessFinishNoResult);
return false;
}
switch (m_options.signal) {
case eReproducerCrashSigill:
std::raise(SIGILL);
break;
case eReproducerCrashSigsegv:
std::raise(SIGSEGV);
break;
}
result.SetStatus(eReturnStatusQuit);
return result.Succeeded();
}
private:
CommandOptions m_options;
};
class CommandObjectReproducerStatus : public CommandObjectParsed {
public:
CommandObjectReproducerStatus(CommandInterpreter &interpreter)
: CommandObjectParsed(
interpreter, "reproducer status",
"Show the current reproducer status. In capture mode the "
"debugger "
"is collecting all the information it needs to create a "
"reproducer. In replay mode the reproducer is replaying a "
"reproducer. When the reproducers are off, no data is collected "
"and no reproducer can be generated.",
nullptr) {}
~CommandObjectReproducerStatus() override = default;
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
if (!command.empty()) {
result.AppendErrorWithFormat("'%s' takes no arguments",
m_cmd_name.c_str());
return false;
}
auto &r = Reproducer::Instance();
if (r.IsCapturing()) {
result.GetOutputStream() << "Reproducer is in capture mode.\n";
} else if (r.IsReplaying()) {
result.GetOutputStream() << "Reproducer is in replay mode.\n";
} else {
result.GetOutputStream() << "Reproducer is off.\n";
}
result.SetStatus(eReturnStatusSuccessFinishResult);
return result.Succeeded();
}
};
static void SetError(CommandReturnObject &result, Error err) {
result.GetErrorStream().Printf("error: %s\n",
toString(std::move(err)).c_str());
result.SetStatus(eReturnStatusFailed);
}
class CommandObjectReproducerDump : public CommandObjectParsed {
public:
CommandObjectReproducerDump(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "reproducer dump",
"Dump the information contained in a reproducer. "
"If no reproducer is specified during replay, it "
"dumps the content of the current reproducer.",
nullptr) {}
~CommandObjectReproducerDump() override = default;
Options *GetOptions() override { return &m_options; }
class CommandOptions : public Options {
public:
CommandOptions() : Options(), file() {}
~CommandOptions() override = default;
Status SetOptionValue(uint32_t option_idx, StringRef option_arg,
ExecutionContext *execution_context) override {
Status error;
const int short_option = m_getopt_table[option_idx].val;
switch (short_option) {
case 'f':
file.SetFile(option_arg, FileSpec::Style::native);
FileSystem::Instance().Resolve(file);
break;
case 'p':
provider = (ReproducerProvider)OptionArgParser::ToOptionEnum(
option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
if (!error.Success())
error.SetErrorStringWithFormat("unrecognized value for provider '%s'",
option_arg.str().c_str());
break;
default:
llvm_unreachable("Unimplemented option");
}
return error;
}
void OptionParsingStarting(ExecutionContext *execution_context) override {
file.Clear();
provider = eReproducerProviderNone;
}
ArrayRef<OptionDefinition> GetDefinitions() override {
return makeArrayRef(g_reproducer_dump_options);
}
FileSpec file;
ReproducerProvider provider = eReproducerProviderNone;
};
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
if (!command.empty()) {
result.AppendErrorWithFormat("'%s' takes no arguments",
m_cmd_name.c_str());
return false;
}
// If no reproducer path is specified, use the loader currently used for
// replay. Otherwise create a new loader just for dumping.
llvm::Optional<Loader> loader_storage;
Loader *loader = nullptr;
if (!m_options.file) {
loader = Reproducer::Instance().GetLoader();
if (loader == nullptr) {
result.SetError(
"Not specifying a reproducer is only support during replay.");
result.SetStatus(eReturnStatusSuccessFinishNoResult);
return false;
}
} else {
loader_storage.emplace(m_options.file);
loader = &(*loader_storage);
if (Error err = loader->LoadIndex()) {
SetError(result, std::move(err));
return false;
}
}
// If we get here we should have a valid loader.
assert(loader);
switch (m_options.provider) {
case eReproducerProviderFiles: {
FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>();
// Read the VFS mapping.
ErrorOr<std::unique_ptr<MemoryBuffer>> buffer =
vfs::getRealFileSystem()->getBufferForFile(vfs_mapping.GetPath());
if (!buffer) {
SetError(result, errorCodeToError(buffer.getError()));
return false;
}
// Initialize a VFS from the given mapping.
IntrusiveRefCntPtr<vfs::FileSystem> vfs = vfs::getVFSFromYAML(
std::move(buffer.get()), nullptr, vfs_mapping.GetPath());
// Dump the VFS to a buffer.
std::string str;
raw_string_ostream os(str);
static_cast<vfs::RedirectingFileSystem &>(*vfs).dump(os);
os.flush();
// Return the string.
result.AppendMessage(str);
result.SetStatus(eReturnStatusSuccessFinishResult);
return true;
}
case eReproducerProviderVersion: {
Expected<std::string> version = loader->LoadBuffer<VersionProvider>();
if (!version) {
SetError(result, version.takeError());
return false;
}
result.AppendMessage(*version);
result.SetStatus(eReturnStatusSuccessFinishResult);
return true;
}
case eReproducerProviderWorkingDirectory: {
Expected<std::string> cwd =
loader->LoadBuffer<WorkingDirectoryProvider>();
if (!cwd) {
SetError(result, cwd.takeError());
return false;
}
result.AppendMessage(*cwd);
result.SetStatus(eReturnStatusSuccessFinishResult);
return true;
}
case eReproducerProviderCommands: {
std::unique_ptr<repro::MultiLoader<repro::CommandProvider>> multi_loader =
repro::MultiLoader<repro::CommandProvider>::Create(loader);
if (!multi_loader) {
SetError(result,
make_error<StringError>(llvm::inconvertibleErrorCode(),
"Unable to create command loader."));
return false;
}
// Iterate over the command files and dump them.
llvm::Optional<std::string> command_file;
while ((command_file = multi_loader->GetNextFile())) {
if (!command_file)
break;
auto command_buffer = llvm::MemoryBuffer::getFile(*command_file);
if (auto err = command_buffer.getError()) {
SetError(result, errorCodeToError(err));
return false;
}
result.AppendMessage((*command_buffer)->getBuffer());
}
result.SetStatus(eReturnStatusSuccessFinishResult);
return true;
}
case eReproducerProviderGDB: {
std::unique_ptr<repro::MultiLoader<repro::GDBRemoteProvider>>
multi_loader =
repro::MultiLoader<repro::GDBRemoteProvider>::Create(loader);
llvm::Optional<std::string> gdb_file;
while ((gdb_file = multi_loader->GetNextFile())) {
auto error_or_file = MemoryBuffer::getFile(*gdb_file);
if (auto err = error_or_file.getError()) {
SetError(result, errorCodeToError(err));
return false;
}
std::vector<GDBRemotePacket> packets;
yaml::Input yin((*error_or_file)->getBuffer());
yin >> packets;
if (auto err = yin.error()) {
SetError(result, errorCodeToError(err));
return false;
}
for (GDBRemotePacket &packet : packets) {
packet.Dump(result.GetOutputStream());
}
}
result.SetStatus(eReturnStatusSuccessFinishResult);
return true;
}
case eReproducerProviderNone:
result.SetError("No valid provider specified.");
return false;
}
result.SetStatus(eReturnStatusSuccessFinishNoResult);
return result.Succeeded();
}
private:
CommandOptions m_options;
};
CommandObjectReproducer::CommandObjectReproducer(
CommandInterpreter &interpreter)
: CommandObjectMultiword(
interpreter, "reproducer",
"Commands for manipulating reproducers. Reproducers make it "
"possible "
"to capture full debug sessions with all its dependencies. The "
"resulting reproducer is used to replay the debug session while "
"debugging the debugger.\n"
"Because reproducers need the whole the debug session from "
"beginning to end, you need to launch the debugger in capture or "
"replay mode, commonly though the command line driver.\n"
"Reproducers are unrelated record-replay debugging, as you cannot "
"interact with the debugger during replay.\n",
"reproducer <subcommand> [<subcommand-options>]") {
LoadSubCommand(
"generate",
CommandObjectSP(new CommandObjectReproducerGenerate(interpreter)));
LoadSubCommand("status", CommandObjectSP(
new CommandObjectReproducerStatus(interpreter)));
LoadSubCommand("dump",
CommandObjectSP(new CommandObjectReproducerDump(interpreter)));
LoadSubCommand("xcrash", CommandObjectSP(
new CommandObjectReproducerXCrash(interpreter)));
}
CommandObjectReproducer::~CommandObjectReproducer() = default;