LLDB  mainline
CommandObjectBugreport.cpp
Go to the documentation of this file.
1 //===-- CommandObjectBugreport.cpp ------------------------------*- C++ -*-===//
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 
11 #include <cstdio>
12 
13 
17 #include "lldb/Target/Thread.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 // "bugreport unwind"
23 
25 public:
28  interpreter, "bugreport unwind",
29  "Create a bugreport for a bug in the stack unwinding code.",
30  nullptr),
31  m_option_group(), m_outfile_options() {
32  m_option_group.Append(&m_outfile_options, LLDB_OPT_SET_ALL,
34  m_option_group.Finalize();
35  }
36 
38 
39  Options *GetOptions() override { return &m_option_group; }
40 
41 protected:
42  bool DoExecute(Args &command, CommandReturnObject &result) override {
43  StringList commands;
44  commands.AppendString("thread backtrace");
45 
46  Thread *thread = m_exe_ctx.GetThreadPtr();
47  if (thread) {
48  char command_buffer[256];
49 
50  uint32_t frame_count = thread->GetStackFrameCount();
51  for (uint32_t i = 0; i < frame_count; ++i) {
52  StackFrameSP frame = thread->GetStackFrameAtIndex(i);
53  lldb::addr_t pc = frame->GetStackID().GetPC();
54 
55  snprintf(command_buffer, sizeof(command_buffer),
56  "disassemble --bytes --address 0x%" PRIx64, pc);
57  commands.AppendString(command_buffer);
58 
59  snprintf(command_buffer, sizeof(command_buffer),
60  "image show-unwind --address 0x%" PRIx64, pc);
61  commands.AppendString(command_buffer);
62  }
63  }
64 
65  const FileSpec &outfile_spec =
66  m_outfile_options.GetFile().GetCurrentValue();
67  if (outfile_spec) {
68 
69  uint32_t open_options =
70  File::eOpenOptionWrite | File::eOpenOptionCanCreate |
71  File::eOpenOptionAppend | File::eOpenOptionCloseOnExec;
72 
73  const bool append = m_outfile_options.GetAppend().GetCurrentValue();
74  if (!append)
75  open_options |= File::eOpenOptionTruncate;
76 
77  StreamFileSP outfile_stream = std::make_shared<StreamFile>();
78  File &file = outfile_stream->GetFile();
79  Status error =
80  FileSystem::Instance().Open(file, outfile_spec, open_options);
81  if (error.Fail()) {
82  auto path = outfile_spec.GetPath();
83  result.AppendErrorWithFormat("Failed to open file '%s' for %s: %s\n",
84  path.c_str(), append ? "append" : "write",
85  error.AsCString());
87  return false;
88  }
89 
90  result.SetImmediateOutputStream(outfile_stream);
91  }
92 
94  options.SetStopOnError(false);
95  options.SetEchoCommands(true);
96  options.SetPrintResults(true);
97  options.SetAddToHistory(false);
98  m_interpreter.HandleCommands(commands, &m_exe_ctx, options, result);
99 
100  return result.Succeeded();
101  }
102 
103 private:
104  OptionGroupOptions m_option_group;
105  OptionGroupOutputFile m_outfile_options;
106 };
107 
108 #pragma mark CommandObjectMultiwordBugreport
109 
110 // CommandObjectMultiwordBugreport
111 
112 CommandObjectMultiwordBugreport::CommandObjectMultiwordBugreport(
113  CommandInterpreter &interpreter)
115  interpreter, "bugreport",
116  "Commands for creating domain-specific bug reports.",
117  "bugreport <subcommand> [<subcommand-options>]") {
118 
120  "unwind", CommandObjectSP(new CommandObjectBugreportUnwind(interpreter)));
121 }
122 
A command line argument class.
Definition: Args.h:32
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj) override
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
A file utility class.
Definition: FileSpec.h:55
A file class.
Definition: File.h:29
#define LLDB_OPT_SET_3
Definition: lldb-defines.h:113
bool DoExecute(Args &command, CommandReturnObject &result) override
void SetImmediateOutputStream(const lldb::StreamSP &stream_sp)
#define LLDB_OPT_SET_2
Definition: lldb-defines.h:112
A command line option parsing protocol class.
Definition: Options.h:62
uint64_t addr_t
Definition: lldb-types.h:83
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
Definition: SBAddress.h:15
#define LLDB_OPT_SET_ALL
Definition: lldb-defines.h:110
#define LLDB_OPT_SET_1
Definition: lldb-defines.h:111
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:395
virtual uint32_t GetStackFrameCount()
Definition: Thread.h:391
void AppendString(const std::string &s)
Definition: StringList.cpp:43
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:376
void SetStatus(lldb::ReturnStatus status)
CommandObjectBugreportUnwind(CommandInterpreter &interpreter)
An error handling class.
Definition: Status.h:44