LLDB  mainline
CommandObjectDiagnostics.cpp
Go to the documentation of this file.
1 //===-- CommandObjectDiagnostics.cpp --------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "lldb/Host/OptionParser.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 #define LLDB_OPTIONS_diagnostics_dump
23 #include "CommandOptions.inc"
24 
26 public:
27  // Constructors and Destructors
29  : CommandObjectParsed(interpreter, "diagnostics dump",
30  "Dump diagnostics to disk", nullptr) {}
31 
32  ~CommandObjectDiagnosticsDump() override = default;
33 
34  class CommandOptions : public Options {
35  public:
36  CommandOptions() = default;
37 
38  ~CommandOptions() override = default;
39 
40  Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
41  ExecutionContext *execution_context) override {
42  Status error;
43  const int short_option = m_getopt_table[option_idx].val;
44 
45  switch (short_option) {
46  case 'd':
47  directory.SetDirectory(option_arg);
48  break;
49  default:
50  llvm_unreachable("Unimplemented option");
51  }
52  return error;
53  }
54 
55  void OptionParsingStarting(ExecutionContext *execution_context) override {
56  directory.Clear();
57  }
58 
59  llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
60  return llvm::ArrayRef(g_diagnostics_dump_options);
61  }
62 
64  };
65 
66  Options *GetOptions() override { return &m_options; }
67 
68 protected:
69  llvm::Expected<FileSpec> GetDirectory() {
70  if (m_options.directory) {
71  auto ec =
72  llvm::sys::fs::create_directories(m_options.directory.GetPath());
73  if (ec)
74  return llvm::errorCodeToError(ec);
75  return m_options.directory;
76  }
77  return Diagnostics::CreateUniqueDirectory();
78  }
79 
80  bool DoExecute(Args &args, CommandReturnObject &result) override {
81  llvm::Expected<FileSpec> directory = GetDirectory();
82 
83  if (!directory) {
84  result.AppendError(llvm::toString(directory.takeError()));
85  return result.Succeeded();
86  }
87 
88  llvm::Error error = Diagnostics::Instance().Create(*directory);
89  if (error) {
90  result.AppendErrorWithFormat("failed to write diagnostics to %s",
91  directory->GetPath().c_str());
92  result.AppendError(llvm::toString(std::move(error)));
93  return result.Succeeded();
94  }
95 
96  result.GetOutputStream() << "diagnostics written to " << *directory << '\n';
97 
99  return result.Succeeded();
100  }
101 
103 };
104 
105 CommandObjectDiagnostics::CommandObjectDiagnostics(
106  CommandInterpreter &interpreter)
107  : CommandObjectMultiword(interpreter, "diagnostics",
108  "Commands controlling LLDB diagnostics.",
109  "diagnostics <subcommand> [<command-options>]") {
111  "dump", CommandObjectSP(new CommandObjectDiagnosticsDump(interpreter)));
112 }
113 
lldb_private::toString
const char * toString(AppleArm64ExceptionClass EC)
Definition: AppleArm64ExceptionClass.h:38
lldb_private::CommandObjectParsed
Definition: CommandObject.h:391
CommandObjectDiagnosticsDump::CommandOptions::directory
FileSpec directory
Definition: CommandObjectDiagnostics.cpp:63
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
OptionValueUInt64.h
OptionArgParser.h
lldb_private::Args
Definition: Args.h:33
lldb_private::CommandReturnObject::Succeeded
bool Succeeded() const
Definition: CommandReturnObject.cpp:131
CommandObjectDiagnosticsDump::CommandOptions::OptionParsingStarting
void OptionParsingStarting(ExecutionContext *execution_context) override
Definition: CommandObjectDiagnostics.cpp:55
CommandReturnObject.h
CommandObjectDiagnosticsDump::DoExecute
bool DoExecute(Args &args, CommandReturnObject &result) override
Definition: CommandObjectDiagnostics.cpp:80
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::CommandObjectMultiword
Definition: CommandObjectMultiword.h:20
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
Options.h
lldb_private::Options
Definition: Options.h:57
CommandObjectDiagnosticsDump::CommandOptions::GetDefinitions
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
Definition: CommandObjectDiagnostics.cpp:59
OptionValueEnumeration.h
CommandObjectDiagnosticsDump::CommandOptions
Definition: CommandObjectDiagnostics.cpp:34
lldb_private::CommandObjectMultiword::LoadSubCommand
bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj) override
Definition: CommandObjectMultiword.cpp:81
lldb_private::CommandReturnObject::SetStatus
void SetStatus(lldb::ReturnStatus status)
Definition: CommandReturnObject.cpp:127
lldb_private::CommandReturnObject::GetOutputStream
Stream & GetOutputStream()
Definition: CommandReturnObject.h:46
lldb_private::CommandObjectDiagnostics::~CommandObjectDiagnostics
~CommandObjectDiagnostics() override
CommandObjectDiagnosticsDump::GetOptions
Options * GetOptions() override
Definition: CommandObjectDiagnostics.cpp:66
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:215
Diagnostics.h
lldb_private::Status
Definition: Status.h:44
CommandObjectDiagnosticsDump::m_options
CommandOptions m_options
Definition: CommandObjectDiagnostics.cpp:102
lldb_private::CommandReturnObject
Definition: CommandReturnObject.h:26
uint32_t
lldb::eReturnStatusSuccessFinishResult
@ eReturnStatusSuccessFinishResult
Definition: lldb-enumerations.h:262
CommandObjectDiagnosticsDump::CommandObjectDiagnosticsDump
CommandObjectDiagnosticsDump(CommandInterpreter &interpreter)
Definition: CommandObjectDiagnostics.cpp:28
lldb_private::CommandReturnObject::AppendErrorWithFormat
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
Definition: CommandReturnObject.cpp:46
OptionParser.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:35
CommandObjectDiagnostics.h
CommandOptionArgumentTable.h
CommandObjectDiagnosticsDump
Definition: CommandObjectDiagnostics.cpp:25
lldb_private::CommandReturnObject::AppendError
void void AppendError(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:100
lldb
Definition: SBAddress.h:15
CommandObjectDiagnosticsDump::GetDirectory
llvm::Expected< FileSpec > GetDirectory()
Definition: CommandObjectDiagnostics.cpp:69
CommandObjectDiagnosticsDump::CommandOptions::SetOptionValue
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
Definition: CommandObjectDiagnostics.cpp:40