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
34 CommandObjectBugreportUnwind(CommandInterpreter &interpreter) :
35 CommandObjectParsed(interpreter,
37 "Create a bugreport for a bug in the stack unwinding code.",
39 m_option_group(interpreter),
42 m_option_group.Append (&m_outfile_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3);
43 m_option_group.Finalize();
46 ~CommandObjectBugreportUnwind() override
53 return &m_option_group;
58 DoExecute(Args& command, CommandReturnObject &result) override
61 commands.AppendString("thread backtrace");
63 Thread *thread = m_exe_ctx.GetThreadPtr();
66 char command_buffer[256];
68 uint32_t frame_count = thread->GetStackFrameCount();
69 for (uint32_t i = 0; i < frame_count; ++i)
71 StackFrameSP frame = thread->GetStackFrameAtIndex(i);
72 lldb::addr_t pc = frame->GetStackID().GetPC();
74 snprintf(command_buffer, sizeof(command_buffer), "disassemble --bytes --address 0x%" PRIx64, pc);
75 commands.AppendString(command_buffer);
77 snprintf(command_buffer, sizeof(command_buffer), "image show-unwind --address 0x%" PRIx64, pc);
78 commands.AppendString(command_buffer);
82 const FileSpec &outfile_spec = m_outfile_options.GetFile().GetCurrentValue();
86 outfile_spec.GetPath (path, sizeof(path));
88 uint32_t open_options = File::eOpenOptionWrite |
89 File::eOpenOptionCanCreate |
90 File::eOpenOptionAppend |
91 File::eOpenOptionCloseOnExec;
93 const bool append = m_outfile_options.GetAppend().GetCurrentValue();
95 open_options |= File::eOpenOptionTruncate;
97 StreamFileSP outfile_stream = std::make_shared<StreamFile>();
98 Error error = outfile_stream->GetFile().Open(path, open_options);
101 result.AppendErrorWithFormat("Failed to open file '%s' for %s: %s\n",
103 append ? "append" : "write",
105 result.SetStatus(eReturnStatusFailed);
109 result.SetImmediateOutputStream(outfile_stream);
112 CommandInterpreterRunOptions options;
113 options.SetStopOnError(false);
114 options.SetEchoCommands(true);
115 options.SetPrintResults(true);
116 options.SetAddToHistory(false);
117 m_interpreter.HandleCommands(commands, &m_exe_ctx, options, result);
119 return result.Succeeded();
123 OptionGroupOptions m_option_group;
124 OptionGroupOutputFile m_outfile_options;
127 #pragma mark CommandObjectMultiwordBugreport
129 //-------------------------------------------------------------------------
130 // CommandObjectMultiwordBugreport
131 //-------------------------------------------------------------------------
133 CommandObjectMultiwordBugreport::CommandObjectMultiwordBugreport(CommandInterpreter &interpreter)
134 : CommandObjectMultiword(interpreter, "bugreport", "Commands for creating domain-specific bug reports.",
135 "bugreport <subcommand> [<subcommand-options>]")
138 LoadSubCommand("unwind", CommandObjectSP(new CommandObjectBugreportUnwind(interpreter)));
141 CommandObjectMultiwordBugreport::~CommandObjectMultiwordBugreport ()