Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 1 | //===-- CommandObjectReproducer.cpp -----------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "CommandObjectReproducer.h" |
| 10 | |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 11 | #include "lldb/Host/OptionParser.h" |
Jonas Devlieghere | 8fc8d3f | 2019-09-16 23:31:06 +0000 | [diff] [blame] | 12 | #include "lldb/Utility/GDBRemote.h" |
Jonas Devlieghere | f4f1201 | 2019-10-17 00:02:00 +0000 | [diff] [blame^] | 13 | #include "lldb/Utility/Reproducer.h" |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 14 | |
Jonas Devlieghere | df14b94 | 2018-11-15 01:05:40 +0000 | [diff] [blame] | 15 | #include "lldb/Interpreter/CommandInterpreter.h" |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 17 | #include "lldb/Interpreter/OptionArgParser.h" |
| 18 | #include "lldb/Interpreter/OptionGroupBoolean.h" |
| 19 | |
| 20 | using namespace lldb; |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 22 | using namespace lldb_private; |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 23 | using namespace lldb_private::repro; |
| 24 | |
| 25 | enum ReproducerProvider { |
| 26 | eReproducerProviderCommands, |
| 27 | eReproducerProviderFiles, |
| 28 | eReproducerProviderGDB, |
| 29 | eReproducerProviderVersion, |
Jonas Devlieghere | f4f1201 | 2019-10-17 00:02:00 +0000 | [diff] [blame^] | 30 | eReproducerProviderWorkingDirectory, |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 31 | eReproducerProviderNone |
| 32 | }; |
| 33 | |
| 34 | static constexpr OptionEnumValueElement g_reproducer_provider_type[] = { |
| 35 | { |
| 36 | eReproducerProviderCommands, |
| 37 | "commands", |
| 38 | "Command Interpreter Commands", |
| 39 | }, |
| 40 | { |
| 41 | eReproducerProviderFiles, |
| 42 | "files", |
| 43 | "Files", |
| 44 | }, |
| 45 | { |
| 46 | eReproducerProviderGDB, |
| 47 | "gdb", |
| 48 | "GDB Remote Packets", |
| 49 | }, |
| 50 | { |
| 51 | eReproducerProviderVersion, |
| 52 | "version", |
| 53 | "Version", |
| 54 | }, |
| 55 | { |
Jonas Devlieghere | f4f1201 | 2019-10-17 00:02:00 +0000 | [diff] [blame^] | 56 | eReproducerProviderWorkingDirectory, |
| 57 | "cwd", |
| 58 | "Working Directory", |
| 59 | }, |
| 60 | { |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 61 | eReproducerProviderNone, |
| 62 | "none", |
| 63 | "None", |
| 64 | }, |
| 65 | }; |
| 66 | |
| 67 | static constexpr OptionEnumValues ReproducerProviderType() { |
| 68 | return OptionEnumValues(g_reproducer_provider_type); |
| 69 | } |
| 70 | |
| 71 | #define LLDB_OPTIONS_reproducer |
| 72 | #include "CommandOptions.inc" |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 73 | |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 74 | class CommandObjectReproducerGenerate : public CommandObjectParsed { |
| 75 | public: |
| 76 | CommandObjectReproducerGenerate(CommandInterpreter &interpreter) |
Jonas Devlieghere | 973d66e | 2019-05-03 00:10:31 +0000 | [diff] [blame] | 77 | : CommandObjectParsed( |
| 78 | interpreter, "reproducer generate", |
| 79 | "Generate reproducer on disk. When the debugger is in capture " |
| 80 | "mode, this command will output the reproducer to a directory on " |
| 81 | "disk. In replay mode this command in a no-op.", |
| 82 | nullptr) {} |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 83 | |
| 84 | ~CommandObjectReproducerGenerate() override = default; |
| 85 | |
| 86 | protected: |
| 87 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 88 | if (!command.empty()) { |
| 89 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 90 | m_cmd_name.c_str()); |
| 91 | return false; |
| 92 | } |
| 93 | |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 94 | auto &r = Reproducer::Instance(); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 95 | if (auto generator = r.GetGenerator()) { |
| 96 | generator->Keep(); |
Jonas Devlieghere | 865cd09 | 2019-10-09 21:47:49 +0000 | [diff] [blame] | 97 | } else if (r.IsReplaying()) { |
Jonas Devlieghere | 2dca653 | 2019-02-27 17:47:06 +0000 | [diff] [blame] | 98 | // Make this operation a NOP in replay mode. |
| 99 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 100 | return result.Succeeded(); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 101 | } else { |
| 102 | result.AppendErrorWithFormat("Unable to get the reproducer generator"); |
Jonas Devlieghere | 2dca653 | 2019-02-27 17:47:06 +0000 | [diff] [blame] | 103 | result.SetStatus(eReturnStatusFailed); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | |
| 107 | result.GetOutputStream() |
| 108 | << "Reproducer written to '" << r.GetReproducerPath() << "'\n"; |
Jonas Devlieghere | 1c5250a | 2019-04-02 18:23:16 +0000 | [diff] [blame] | 109 | result.GetOutputStream() |
| 110 | << "Please have a look at the directory to assess if you're willing to " |
| 111 | "share the contained information.\n"; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 112 | |
| 113 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 114 | return result.Succeeded(); |
| 115 | } |
| 116 | }; |
| 117 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 118 | class CommandObjectReproducerStatus : public CommandObjectParsed { |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 119 | public: |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 120 | CommandObjectReproducerStatus(CommandInterpreter &interpreter) |
Jonas Devlieghere | 973d66e | 2019-05-03 00:10:31 +0000 | [diff] [blame] | 121 | : CommandObjectParsed( |
| 122 | interpreter, "reproducer status", |
| 123 | "Show the current reproducer status. In capture mode the debugger " |
| 124 | "is collecting all the information it needs to create a " |
| 125 | "reproducer. In replay mode the reproducer is replaying a " |
| 126 | "reproducer. When the reproducers are off, no data is collected " |
| 127 | "and no reproducer can be generated.", |
| 128 | nullptr) {} |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 129 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 130 | ~CommandObjectReproducerStatus() override = default; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 131 | |
| 132 | protected: |
| 133 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 134 | if (!command.empty()) { |
| 135 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 136 | m_cmd_name.c_str()); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 140 | auto &r = Reproducer::Instance(); |
Jonas Devlieghere | 865cd09 | 2019-10-09 21:47:49 +0000 | [diff] [blame] | 141 | if (r.IsCapturing()) { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 142 | result.GetOutputStream() << "Reproducer is in capture mode.\n"; |
Jonas Devlieghere | 865cd09 | 2019-10-09 21:47:49 +0000 | [diff] [blame] | 143 | } else if (r.IsReplaying()) { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 144 | result.GetOutputStream() << "Reproducer is in replay mode.\n"; |
| 145 | } else { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 146 | result.GetOutputStream() << "Reproducer is off.\n"; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 149 | result.SetStatus(eReturnStatusSuccessFinishResult); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 150 | return result.Succeeded(); |
| 151 | } |
| 152 | }; |
| 153 | |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 154 | static void SetError(CommandReturnObject &result, Error err) { |
| 155 | result.GetErrorStream().Printf("error: %s\n", |
| 156 | toString(std::move(err)).c_str()); |
| 157 | result.SetStatus(eReturnStatusFailed); |
| 158 | } |
| 159 | |
| 160 | class CommandObjectReproducerDump : public CommandObjectParsed { |
| 161 | public: |
| 162 | CommandObjectReproducerDump(CommandInterpreter &interpreter) |
| 163 | : CommandObjectParsed(interpreter, "reproducer dump", |
| 164 | "Dump the information contained in a reproducer.", |
| 165 | nullptr) {} |
| 166 | |
| 167 | ~CommandObjectReproducerDump() override = default; |
| 168 | |
| 169 | Options *GetOptions() override { return &m_options; } |
| 170 | |
| 171 | class CommandOptions : public Options { |
| 172 | public: |
| 173 | CommandOptions() : Options(), file() {} |
| 174 | |
| 175 | ~CommandOptions() override = default; |
| 176 | |
| 177 | Status SetOptionValue(uint32_t option_idx, StringRef option_arg, |
| 178 | ExecutionContext *execution_context) override { |
| 179 | Status error; |
| 180 | const int short_option = m_getopt_table[option_idx].val; |
| 181 | |
| 182 | switch (short_option) { |
| 183 | case 'f': |
| 184 | file.SetFile(option_arg, FileSpec::Style::native); |
| 185 | FileSystem::Instance().Resolve(file); |
| 186 | break; |
| 187 | case 'p': |
| 188 | provider = (ReproducerProvider)OptionArgParser::ToOptionEnum( |
| 189 | option_arg, GetDefinitions()[option_idx].enum_values, 0, error); |
| 190 | if (!error.Success()) |
| 191 | error.SetErrorStringWithFormat("unrecognized value for provider '%s'", |
| 192 | option_arg.str().c_str()); |
| 193 | break; |
| 194 | default: |
| 195 | llvm_unreachable("Unimplemented option"); |
| 196 | } |
| 197 | |
| 198 | return error; |
| 199 | } |
| 200 | |
| 201 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 202 | file.Clear(); |
| 203 | provider = eReproducerProviderNone; |
| 204 | } |
| 205 | |
| 206 | ArrayRef<OptionDefinition> GetDefinitions() override { |
| 207 | return makeArrayRef(g_reproducer_options); |
| 208 | } |
| 209 | |
| 210 | FileSpec file; |
| 211 | ReproducerProvider provider = eReproducerProviderNone; |
| 212 | }; |
| 213 | |
| 214 | protected: |
| 215 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 216 | if (!command.empty()) { |
| 217 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 218 | m_cmd_name.c_str()); |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | // If no reproducer path is specified, use the loader currently used for |
| 223 | // replay. Otherwise create a new loader just for dumping. |
| 224 | llvm::Optional<Loader> loader_storage; |
| 225 | Loader *loader = nullptr; |
| 226 | if (!m_options.file) { |
| 227 | loader = Reproducer::Instance().GetLoader(); |
| 228 | if (loader == nullptr) { |
| 229 | result.SetError( |
| 230 | "Not specifying a reproducer is only support during replay."); |
| 231 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 232 | return false; |
| 233 | } |
| 234 | } else { |
| 235 | loader_storage.emplace(m_options.file); |
| 236 | loader = &(*loader_storage); |
| 237 | if (Error err = loader->LoadIndex()) { |
| 238 | SetError(result, std::move(err)); |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // If we get here we should have a valid loader. |
| 244 | assert(loader); |
| 245 | |
| 246 | switch (m_options.provider) { |
| 247 | case eReproducerProviderFiles: { |
| 248 | FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>(); |
| 249 | |
| 250 | // Read the VFS mapping. |
| 251 | ErrorOr<std::unique_ptr<MemoryBuffer>> buffer = |
| 252 | vfs::getRealFileSystem()->getBufferForFile(vfs_mapping.GetPath()); |
| 253 | if (!buffer) { |
| 254 | SetError(result, errorCodeToError(buffer.getError())); |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | // Initialize a VFS from the given mapping. |
| 259 | IntrusiveRefCntPtr<vfs::FileSystem> vfs = vfs::getVFSFromYAML( |
| 260 | std::move(buffer.get()), nullptr, vfs_mapping.GetPath()); |
| 261 | |
| 262 | // Dump the VFS to a buffer. |
| 263 | std::string str; |
| 264 | raw_string_ostream os(str); |
| 265 | static_cast<vfs::RedirectingFileSystem &>(*vfs).dump(os); |
| 266 | os.flush(); |
| 267 | |
| 268 | // Return the string. |
| 269 | result.AppendMessage(str); |
| 270 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 271 | return true; |
| 272 | } |
| 273 | case eReproducerProviderVersion: { |
Jonas Devlieghere | b2575da | 2019-10-17 00:01:57 +0000 | [diff] [blame] | 274 | Expected<std::string> version = loader->LoadBuffer<VersionProvider>(); |
| 275 | if (!version) { |
| 276 | SetError(result, version.takeError()); |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 277 | return false; |
| 278 | } |
Jonas Devlieghere | b2575da | 2019-10-17 00:01:57 +0000 | [diff] [blame] | 279 | result.AppendMessage(*version); |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 280 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 281 | return true; |
| 282 | } |
Jonas Devlieghere | f4f1201 | 2019-10-17 00:02:00 +0000 | [diff] [blame^] | 283 | case eReproducerProviderWorkingDirectory: { |
| 284 | Expected<std::string> cwd = |
| 285 | loader->LoadBuffer<WorkingDirectoryProvider>(); |
| 286 | if (!cwd) { |
| 287 | SetError(result, cwd.takeError()); |
| 288 | return false; |
| 289 | } |
| 290 | result.AppendMessage(*cwd); |
| 291 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 292 | return true; |
| 293 | } |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 294 | case eReproducerProviderCommands: { |
| 295 | // Create a new command loader. |
| 296 | std::unique_ptr<repro::CommandLoader> command_loader = |
| 297 | repro::CommandLoader::Create(loader); |
| 298 | if (!command_loader) { |
| 299 | SetError(result, |
| 300 | make_error<StringError>(llvm::inconvertibleErrorCode(), |
| 301 | "Unable to create command loader.")); |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | // Iterate over the command files and dump them. |
| 306 | while (true) { |
| 307 | llvm::Optional<std::string> command_file = |
| 308 | command_loader->GetNextFile(); |
| 309 | if (!command_file) |
| 310 | break; |
| 311 | |
| 312 | auto command_buffer = llvm::MemoryBuffer::getFile(*command_file); |
| 313 | if (auto err = command_buffer.getError()) { |
| 314 | SetError(result, errorCodeToError(err)); |
| 315 | return false; |
| 316 | } |
| 317 | result.AppendMessage((*command_buffer)->getBuffer()); |
| 318 | } |
| 319 | |
| 320 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 321 | return true; |
| 322 | } |
| 323 | case eReproducerProviderGDB: { |
Jonas Devlieghere | 8fc8d3f | 2019-09-16 23:31:06 +0000 | [diff] [blame] | 324 | FileSpec gdb_file = loader->GetFile<ProcessGDBRemoteProvider::Info>(); |
| 325 | auto error_or_file = MemoryBuffer::getFile(gdb_file.GetPath()); |
| 326 | if (auto err = error_or_file.getError()) { |
| 327 | SetError(result, errorCodeToError(err)); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | std::vector<GDBRemotePacket> packets; |
| 332 | yaml::Input yin((*error_or_file)->getBuffer()); |
| 333 | yin >> packets; |
| 334 | |
| 335 | if (auto err = yin.error()) { |
| 336 | SetError(result, errorCodeToError(err)); |
| 337 | return false; |
| 338 | } |
| 339 | |
Jonas Devlieghere | f4f1201 | 2019-10-17 00:02:00 +0000 | [diff] [blame^] | 340 | for (GDBRemotePacket &packet : packets) { |
Jonas Devlieghere | 8fc8d3f | 2019-09-16 23:31:06 +0000 | [diff] [blame] | 341 | packet.Dump(result.GetOutputStream()); |
| 342 | } |
| 343 | |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 344 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 345 | return true; |
| 346 | } |
| 347 | case eReproducerProviderNone: |
| 348 | result.SetError("No valid provider specified."); |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 353 | return result.Succeeded(); |
| 354 | } |
| 355 | |
| 356 | private: |
| 357 | CommandOptions m_options; |
| 358 | }; |
| 359 | |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 360 | CommandObjectReproducer::CommandObjectReproducer( |
| 361 | CommandInterpreter &interpreter) |
Jonas Devlieghere | 973d66e | 2019-05-03 00:10:31 +0000 | [diff] [blame] | 362 | : CommandObjectMultiword( |
| 363 | interpreter, "reproducer", |
Jonas Devlieghere | 130ec06 | 2019-07-30 18:06:38 +0000 | [diff] [blame] | 364 | "Commands for manipulate the reproducer functionality.", |
| 365 | "reproducer <subcommand> [<subcommand-options>]") { |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 366 | LoadSubCommand( |
| 367 | "generate", |
| 368 | CommandObjectSP(new CommandObjectReproducerGenerate(interpreter))); |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 369 | LoadSubCommand("status", CommandObjectSP( |
| 370 | new CommandObjectReproducerStatus(interpreter))); |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 371 | LoadSubCommand("dump", |
| 372 | CommandObjectSP(new CommandObjectReproducerDump(interpreter))); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | CommandObjectReproducer::~CommandObjectReproducer() = default; |