1 //===-- CommandObjectBugreport.cpp ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "CommandObjectBugreport.h"
16 // Other libraries and framework includes
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 #include "lldb/Interpreter/CommandReturnObject.h"
21 #include "lldb/Interpreter/OptionGroupOutputFile.h"
22 #include "lldb/Target/Thread.h"
25 using namespace lldb_private;
27 //-------------------------------------------------------------------------
29 //-------------------------------------------------------------------------
31 class CommandObjectBugreportUnwind : public CommandObjectParsed {
33 CommandObjectBugreportUnwind(CommandInterpreter &interpreter)
34 : CommandObjectParsed(
35 interpreter, "bugreport unwind",
36 "Create a bugreport for a bug in the stack unwinding code.",
38 m_option_group(), m_outfile_options() {
39 m_option_group.Append(&m_outfile_options, LLDB_OPT_SET_ALL,
40 LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3);
41 m_option_group.Finalize();
44 ~CommandObjectBugreportUnwind() override {}
46 Options *GetOptions() override { return &m_option_group; }
49 bool DoExecute(Args &command, CommandReturnObject &result) override {
51 commands.AppendString("thread backtrace");
53 Thread *thread = m_exe_ctx.GetThreadPtr();
55 char command_buffer[256];
57 uint32_t frame_count = thread->GetStackFrameCount();
58 for (uint32_t i = 0; i < frame_count; ++i) {
59 StackFrameSP frame = thread->GetStackFrameAtIndex(i);
60 lldb::addr_t pc = frame->GetStackID().GetPC();
62 snprintf(command_buffer, sizeof(command_buffer),
63 "disassemble --bytes --address 0x%" PRIx64, pc);
64 commands.AppendString(command_buffer);
66 snprintf(command_buffer, sizeof(command_buffer),
67 "image show-unwind --address 0x%" PRIx64, pc);
68 commands.AppendString(command_buffer);
72 const FileSpec &outfile_spec =
73 m_outfile_options.GetFile().GetCurrentValue();
76 outfile_spec.GetPath(path, sizeof(path));
78 uint32_t open_options =
79 File::eOpenOptionWrite | File::eOpenOptionCanCreate |
80 File::eOpenOptionAppend | File::eOpenOptionCloseOnExec;
82 const bool append = m_outfile_options.GetAppend().GetCurrentValue();
84 open_options |= File::eOpenOptionTruncate;
86 StreamFileSP outfile_stream = std::make_shared<StreamFile>();
87 Error error = outfile_stream->GetFile().Open(path, open_options);
89 result.AppendErrorWithFormat("Failed to open file '%s' for %s: %s\n",
90 path, append ? "append" : "write",
92 result.SetStatus(eReturnStatusFailed);
96 result.SetImmediateOutputStream(outfile_stream);
99 CommandInterpreterRunOptions options;
100 options.SetStopOnError(false);
101 options.SetEchoCommands(true);
102 options.SetPrintResults(true);
103 options.SetAddToHistory(false);
104 m_interpreter.HandleCommands(commands, &m_exe_ctx, options, result);
106 return result.Succeeded();
110 OptionGroupOptions m_option_group;
111 OptionGroupOutputFile m_outfile_options;
114 #pragma mark CommandObjectMultiwordBugreport
116 //-------------------------------------------------------------------------
117 // CommandObjectMultiwordBugreport
118 //-------------------------------------------------------------------------
120 CommandObjectMultiwordBugreport::CommandObjectMultiwordBugreport(
121 CommandInterpreter &interpreter)
122 : CommandObjectMultiword(
123 interpreter, "bugreport",
124 "Commands for creating domain-specific bug reports.",
125 "bugreport <subcommand> [<subcommand-options>]") {
128 "unwind", CommandObjectSP(new CommandObjectBugreportUnwind(interpreter)));
131 CommandObjectMultiwordBugreport::~CommandObjectMultiwordBugreport() {}