blob: 2797f3c0a9f038ae93c74d1c4f1ac716f2d79e86 [file] [log] [blame]
Jonas Devlieghere9e046f02018-11-13 19:18:16 +00001//===-- CommandObjectReproducer.cpp -----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jonas Devlieghere9e046f02018-11-13 19:18:16 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "CommandObjectReproducer.h"
10
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +000011#include "lldb/Host/OptionParser.h"
Jonas Devlieghere8fc8d3f2019-09-16 23:31:06 +000012#include "lldb/Utility/GDBRemote.h"
Jonas Devliegheref4f12012019-10-17 00:02:00 +000013#include "lldb/Utility/Reproducer.h"
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000014
Jonas Devliegheredf14b942018-11-15 01:05:40 +000015#include "lldb/Interpreter/CommandInterpreter.h"
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000016#include "lldb/Interpreter/CommandReturnObject.h"
17#include "lldb/Interpreter/OptionArgParser.h"
18#include "lldb/Interpreter/OptionGroupBoolean.h"
19
Jonas Devliegherec8dfe902019-11-19 17:28:46 -080020#include <csignal>
21
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000022using namespace lldb;
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +000023using namespace llvm;
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000024using namespace lldb_private;
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +000025using namespace lldb_private::repro;
26
27enum ReproducerProvider {
28 eReproducerProviderCommands,
29 eReproducerProviderFiles,
30 eReproducerProviderGDB,
31 eReproducerProviderVersion,
Jonas Devliegheref4f12012019-10-17 00:02:00 +000032 eReproducerProviderWorkingDirectory,
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +000033 eReproducerProviderNone
34};
35
36static constexpr OptionEnumValueElement g_reproducer_provider_type[] = {
37 {
38 eReproducerProviderCommands,
39 "commands",
40 "Command Interpreter Commands",
41 },
42 {
43 eReproducerProviderFiles,
44 "files",
45 "Files",
46 },
47 {
48 eReproducerProviderGDB,
49 "gdb",
50 "GDB Remote Packets",
51 },
52 {
53 eReproducerProviderVersion,
54 "version",
55 "Version",
56 },
57 {
Jonas Devliegheref4f12012019-10-17 00:02:00 +000058 eReproducerProviderWorkingDirectory,
59 "cwd",
60 "Working Directory",
61 },
62 {
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +000063 eReproducerProviderNone,
64 "none",
65 "None",
66 },
67};
68
69static constexpr OptionEnumValues ReproducerProviderType() {
70 return OptionEnumValues(g_reproducer_provider_type);
71}
72
Jonas Devlieghere36eea5c2019-11-19 16:18:19 -080073#define LLDB_OPTIONS_reproducer_dump
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +000074#include "CommandOptions.inc"
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000075
Jonas Devliegherec8dfe902019-11-19 17:28:46 -080076enum ReproducerCrashSignal {
Jonas Devliegherec8dfe902019-11-19 17:28:46 -080077 eReproducerCrashSigill,
78 eReproducerCrashSigsegv,
79};
80
81static constexpr OptionEnumValueElement g_reproducer_signaltype[] = {
82 {
Jonas Devliegherec8dfe902019-11-19 17:28:46 -080083 eReproducerCrashSigill,
84 "SIGILL",
85 "Illegal instruction",
86 },
87 {
88 eReproducerCrashSigsegv,
89 "SIGSEGV",
90 "Segmentation fault",
91 },
92};
93
94static constexpr OptionEnumValues ReproducerSignalType() {
95 return OptionEnumValues(g_reproducer_signaltype);
96}
97
98#define LLDB_OPTIONS_reproducer_xcrash
99#include "CommandOptions.inc"
100
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000101class CommandObjectReproducerGenerate : public CommandObjectParsed {
102public:
103 CommandObjectReproducerGenerate(CommandInterpreter &interpreter)
Jonas Devlieghere973d66e2019-05-03 00:10:31 +0000104 : CommandObjectParsed(
105 interpreter, "reproducer generate",
106 "Generate reproducer on disk. When the debugger is in capture "
107 "mode, this command will output the reproducer to a directory on "
Jonas Devlieghere0cf86da2019-11-11 14:16:52 -0800108 "disk and quit. In replay mode this command in a no-op.",
Jonas Devlieghere973d66e2019-05-03 00:10:31 +0000109 nullptr) {}
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000110
111 ~CommandObjectReproducerGenerate() override = default;
112
113protected:
114 bool DoExecute(Args &command, CommandReturnObject &result) override {
115 if (!command.empty()) {
116 result.AppendErrorWithFormat("'%s' takes no arguments",
117 m_cmd_name.c_str());
118 return false;
119 }
120
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +0000121 auto &r = Reproducer::Instance();
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000122 if (auto generator = r.GetGenerator()) {
123 generator->Keep();
Jonas Devlieghere865cd092019-10-09 21:47:49 +0000124 } else if (r.IsReplaying()) {
Jonas Devlieghere2dca6532019-02-27 17:47:06 +0000125 // Make this operation a NOP in replay mode.
126 result.SetStatus(eReturnStatusSuccessFinishNoResult);
127 return result.Succeeded();
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000128 } else {
129 result.AppendErrorWithFormat("Unable to get the reproducer generator");
Jonas Devlieghere2dca6532019-02-27 17:47:06 +0000130 result.SetStatus(eReturnStatusFailed);
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000131 return false;
132 }
133
134 result.GetOutputStream()
135 << "Reproducer written to '" << r.GetReproducerPath() << "'\n";
Jonas Devlieghere1c5250a2019-04-02 18:23:16 +0000136 result.GetOutputStream()
137 << "Please have a look at the directory to assess if you're willing to "
138 "share the contained information.\n";
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000139
Jonas Devlieghere0cf86da2019-11-11 14:16:52 -0800140 m_interpreter.BroadcastEvent(
141 CommandInterpreter::eBroadcastBitQuitCommandReceived);
142 result.SetStatus(eReturnStatusQuit);
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000143 return result.Succeeded();
144 }
145};
146
Jonas Devliegherec8dfe902019-11-19 17:28:46 -0800147class CommandObjectReproducerXCrash : public CommandObjectParsed {
148public:
149 CommandObjectReproducerXCrash(CommandInterpreter &interpreter)
150 : CommandObjectParsed(interpreter, "reproducer xcrash",
151 "Intentionally force the debugger to crash in "
152 "order to trigger and test reproducer generation.",
153 nullptr) {}
154
155 ~CommandObjectReproducerXCrash() override = default;
156
157 Options *GetOptions() override { return &m_options; }
158
159 class CommandOptions : public Options {
160 public:
161 CommandOptions() : Options() {}
162
163 ~CommandOptions() override = default;
164
165 Status SetOptionValue(uint32_t option_idx, StringRef option_arg,
166 ExecutionContext *execution_context) override {
167 Status error;
168 const int short_option = m_getopt_table[option_idx].val;
169
170 switch (short_option) {
171 case 's':
172 signal = (ReproducerCrashSignal)OptionArgParser::ToOptionEnum(
173 option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
174 if (!error.Success())
175 error.SetErrorStringWithFormat("unrecognized value for signal '%s'",
176 option_arg.str().c_str());
177 break;
178 default:
179 llvm_unreachable("Unimplemented option");
180 }
181
182 return error;
183 }
184
185 void OptionParsingStarting(ExecutionContext *execution_context) override {
186 signal = eReproducerCrashSigsegv;
187 }
188
189 ArrayRef<OptionDefinition> GetDefinitions() override {
190 return makeArrayRef(g_reproducer_xcrash_options);
191 }
192
193 ReproducerCrashSignal signal = eReproducerCrashSigsegv;
194 };
195
196protected:
197 bool DoExecute(Args &command, CommandReturnObject &result) override {
198 if (!command.empty()) {
199 result.AppendErrorWithFormat("'%s' takes no arguments",
200 m_cmd_name.c_str());
201 return false;
202 }
203
204 auto &r = Reproducer::Instance();
205 if (!r.IsCapturing()) {
206 result.SetError(
207 "forcing a crash is only supported when capturing a reproducer.");
208 result.SetStatus(eReturnStatusSuccessFinishNoResult);
209 return false;
210 }
211
212 switch (m_options.signal) {
213 case eReproducerCrashSigill:
214 std::raise(SIGILL);
215 break;
Jonas Devliegherec8dfe902019-11-19 17:28:46 -0800216 case eReproducerCrashSigsegv:
217 std::raise(SIGSEGV);
218 break;
219 }
220
221 result.SetStatus(eReturnStatusQuit);
222 return result.Succeeded();
223 }
224
225private:
226 CommandOptions m_options;
227};
228
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000229class CommandObjectReproducerStatus : public CommandObjectParsed {
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000230public:
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000231 CommandObjectReproducerStatus(CommandInterpreter &interpreter)
Jonas Devlieghere973d66e2019-05-03 00:10:31 +0000232 : CommandObjectParsed(
233 interpreter, "reproducer status",
Jonas Devliegherec8dfe902019-11-19 17:28:46 -0800234 "Show the current reproducer status. In capture mode the "
235 "debugger "
Jonas Devlieghere973d66e2019-05-03 00:10:31 +0000236 "is collecting all the information it needs to create a "
237 "reproducer. In replay mode the reproducer is replaying a "
238 "reproducer. When the reproducers are off, no data is collected "
239 "and no reproducer can be generated.",
240 nullptr) {}
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000241
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000242 ~CommandObjectReproducerStatus() override = default;
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000243
244protected:
245 bool DoExecute(Args &command, CommandReturnObject &result) override {
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000246 if (!command.empty()) {
247 result.AppendErrorWithFormat("'%s' takes no arguments",
248 m_cmd_name.c_str());
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000249 return false;
250 }
251
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +0000252 auto &r = Reproducer::Instance();
Jonas Devlieghere865cd092019-10-09 21:47:49 +0000253 if (r.IsCapturing()) {
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000254 result.GetOutputStream() << "Reproducer is in capture mode.\n";
Jonas Devlieghere865cd092019-10-09 21:47:49 +0000255 } else if (r.IsReplaying()) {
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000256 result.GetOutputStream() << "Reproducer is in replay mode.\n";
257 } else {
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000258 result.GetOutputStream() << "Reproducer is off.\n";
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000259 }
260
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000261 result.SetStatus(eReturnStatusSuccessFinishResult);
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000262 return result.Succeeded();
263 }
264};
265
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +0000266static void SetError(CommandReturnObject &result, Error err) {
267 result.GetErrorStream().Printf("error: %s\n",
268 toString(std::move(err)).c_str());
269 result.SetStatus(eReturnStatusFailed);
270}
271
272class CommandObjectReproducerDump : public CommandObjectParsed {
273public:
274 CommandObjectReproducerDump(CommandInterpreter &interpreter)
275 : CommandObjectParsed(interpreter, "reproducer dump",
Jonas Devlieghere64b7d952019-10-18 21:47:31 +0000276 "Dump the information contained in a reproducer. "
277 "If no reproducer is specified during replay, it "
278 "dumps the content of the current reproducer.",
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +0000279 nullptr) {}
280
281 ~CommandObjectReproducerDump() override = default;
282
283 Options *GetOptions() override { return &m_options; }
284
285 class CommandOptions : public Options {
286 public:
287 CommandOptions() : Options(), file() {}
288
289 ~CommandOptions() override = default;
290
291 Status SetOptionValue(uint32_t option_idx, StringRef option_arg,
292 ExecutionContext *execution_context) override {
293 Status error;
294 const int short_option = m_getopt_table[option_idx].val;
295
296 switch (short_option) {
297 case 'f':
298 file.SetFile(option_arg, FileSpec::Style::native);
299 FileSystem::Instance().Resolve(file);
300 break;
301 case 'p':
302 provider = (ReproducerProvider)OptionArgParser::ToOptionEnum(
303 option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
304 if (!error.Success())
305 error.SetErrorStringWithFormat("unrecognized value for provider '%s'",
306 option_arg.str().c_str());
307 break;
308 default:
309 llvm_unreachable("Unimplemented option");
310 }
311
312 return error;
313 }
314
315 void OptionParsingStarting(ExecutionContext *execution_context) override {
316 file.Clear();
317 provider = eReproducerProviderNone;
318 }
319
320 ArrayRef<OptionDefinition> GetDefinitions() override {
Jonas Devlieghere36eea5c2019-11-19 16:18:19 -0800321 return makeArrayRef(g_reproducer_dump_options);
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +0000322 }
323
324 FileSpec file;
325 ReproducerProvider provider = eReproducerProviderNone;
326 };
327
328protected:
329 bool DoExecute(Args &command, CommandReturnObject &result) override {
330 if (!command.empty()) {
331 result.AppendErrorWithFormat("'%s' takes no arguments",
332 m_cmd_name.c_str());
333 return false;
334 }
335
336 // If no reproducer path is specified, use the loader currently used for
337 // replay. Otherwise create a new loader just for dumping.
338 llvm::Optional<Loader> loader_storage;
339 Loader *loader = nullptr;
340 if (!m_options.file) {
341 loader = Reproducer::Instance().GetLoader();
342 if (loader == nullptr) {
343 result.SetError(
344 "Not specifying a reproducer is only support during replay.");
345 result.SetStatus(eReturnStatusSuccessFinishNoResult);
346 return false;
347 }
348 } else {
349 loader_storage.emplace(m_options.file);
350 loader = &(*loader_storage);
351 if (Error err = loader->LoadIndex()) {
352 SetError(result, std::move(err));
353 return false;
354 }
355 }
356
357 // If we get here we should have a valid loader.
358 assert(loader);
359
360 switch (m_options.provider) {
361 case eReproducerProviderFiles: {
362 FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>();
363
364 // Read the VFS mapping.
365 ErrorOr<std::unique_ptr<MemoryBuffer>> buffer =
366 vfs::getRealFileSystem()->getBufferForFile(vfs_mapping.GetPath());
367 if (!buffer) {
368 SetError(result, errorCodeToError(buffer.getError()));
369 return false;
370 }
371
372 // Initialize a VFS from the given mapping.
373 IntrusiveRefCntPtr<vfs::FileSystem> vfs = vfs::getVFSFromYAML(
374 std::move(buffer.get()), nullptr, vfs_mapping.GetPath());
375
376 // Dump the VFS to a buffer.
377 std::string str;
378 raw_string_ostream os(str);
379 static_cast<vfs::RedirectingFileSystem &>(*vfs).dump(os);
380 os.flush();
381
382 // Return the string.
383 result.AppendMessage(str);
384 result.SetStatus(eReturnStatusSuccessFinishResult);
385 return true;
386 }
387 case eReproducerProviderVersion: {
Jonas Devlieghereb2575da2019-10-17 00:01:57 +0000388 Expected<std::string> version = loader->LoadBuffer<VersionProvider>();
389 if (!version) {
390 SetError(result, version.takeError());
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +0000391 return false;
392 }
Jonas Devlieghereb2575da2019-10-17 00:01:57 +0000393 result.AppendMessage(*version);
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +0000394 result.SetStatus(eReturnStatusSuccessFinishResult);
395 return true;
396 }
Jonas Devliegheref4f12012019-10-17 00:02:00 +0000397 case eReproducerProviderWorkingDirectory: {
398 Expected<std::string> cwd =
399 loader->LoadBuffer<WorkingDirectoryProvider>();
400 if (!cwd) {
401 SetError(result, cwd.takeError());
402 return false;
403 }
404 result.AppendMessage(*cwd);
405 result.SetStatus(eReturnStatusSuccessFinishResult);
406 return true;
407 }
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +0000408 case eReproducerProviderCommands: {
409 // Create a new command loader.
410 std::unique_ptr<repro::CommandLoader> command_loader =
411 repro::CommandLoader::Create(loader);
412 if (!command_loader) {
413 SetError(result,
414 make_error<StringError>(llvm::inconvertibleErrorCode(),
415 "Unable to create command loader."));
416 return false;
417 }
418
419 // Iterate over the command files and dump them.
420 while (true) {
421 llvm::Optional<std::string> command_file =
422 command_loader->GetNextFile();
423 if (!command_file)
424 break;
425
426 auto command_buffer = llvm::MemoryBuffer::getFile(*command_file);
427 if (auto err = command_buffer.getError()) {
428 SetError(result, errorCodeToError(err));
429 return false;
430 }
431 result.AppendMessage((*command_buffer)->getBuffer());
432 }
433
434 result.SetStatus(eReturnStatusSuccessFinishResult);
435 return true;
436 }
437 case eReproducerProviderGDB: {
Jonas Devlieghere8fc8d3f2019-09-16 23:31:06 +0000438 FileSpec gdb_file = loader->GetFile<ProcessGDBRemoteProvider::Info>();
439 auto error_or_file = MemoryBuffer::getFile(gdb_file.GetPath());
440 if (auto err = error_or_file.getError()) {
441 SetError(result, errorCodeToError(err));
442 return false;
443 }
444
445 std::vector<GDBRemotePacket> packets;
446 yaml::Input yin((*error_or_file)->getBuffer());
447 yin >> packets;
448
449 if (auto err = yin.error()) {
450 SetError(result, errorCodeToError(err));
451 return false;
452 }
453
Jonas Devliegheref4f12012019-10-17 00:02:00 +0000454 for (GDBRemotePacket &packet : packets) {
Jonas Devlieghere8fc8d3f2019-09-16 23:31:06 +0000455 packet.Dump(result.GetOutputStream());
456 }
457
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +0000458 result.SetStatus(eReturnStatusSuccessFinishResult);
459 return true;
460 }
461 case eReproducerProviderNone:
462 result.SetError("No valid provider specified.");
463 return false;
464 }
465
466 result.SetStatus(eReturnStatusSuccessFinishNoResult);
467 return result.Succeeded();
468 }
469
470private:
471 CommandOptions m_options;
472};
473
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000474CommandObjectReproducer::CommandObjectReproducer(
475 CommandInterpreter &interpreter)
Jonas Devlieghere973d66e2019-05-03 00:10:31 +0000476 : CommandObjectMultiword(
477 interpreter, "reproducer",
Jonas Devliegherec8dfe902019-11-19 17:28:46 -0800478 "Commands for manipulating reproducers. Reproducers make it "
479 "possible "
Jonas Devlieghere64b7d952019-10-18 21:47:31 +0000480 "to capture full debug sessions with all its dependencies. The "
481 "resulting reproducer is used to replay the debug session while "
482 "debugging the debugger.\n"
483 "Because reproducers need the whole the debug session from "
484 "beginning to end, you need to launch the debugger in capture or "
485 "replay mode, commonly though the command line driver.\n"
486 "Reproducers are unrelated record-replay debugging, as you cannot "
487 "interact with the debugger during replay.\n",
Jonas Devlieghere130ec062019-07-30 18:06:38 +0000488 "reproducer <subcommand> [<subcommand-options>]") {
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000489 LoadSubCommand(
490 "generate",
491 CommandObjectSP(new CommandObjectReproducerGenerate(interpreter)));
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000492 LoadSubCommand("status", CommandObjectSP(
493 new CommandObjectReproducerStatus(interpreter)));
Jonas Devlieghere97fc8eb2019-09-13 23:27:31 +0000494 LoadSubCommand("dump",
495 CommandObjectSP(new CommandObjectReproducerDump(interpreter)));
Jonas Devliegherec8dfe902019-11-19 17:28:46 -0800496 LoadSubCommand("xcrash", CommandObjectSP(
497 new CommandObjectReproducerXCrash(interpreter)));
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000498}
499
500CommandObjectReproducer::~CommandObjectReproducer() = default;