aboutsummaryrefslogtreecommitdiff
path: root/source/Commands/CommandObjectBugreport.cpp
blob: db8c06c0e9e362e63d1c9d2a1b846dbdb3ee4eae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//===-- CommandObjectBugreport.cpp ------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "CommandObjectBugreport.h"

// C Includes
#include <cstdio>

// C++ Includes
// Other libraries and framework includes

// Project includes
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/OptionGroupOutputFile.h"
#include "lldb/Target/Thread.h"

using namespace lldb;
using namespace lldb_private;

//-------------------------------------------------------------------------
// "bugreport unwind"
//-------------------------------------------------------------------------

class CommandObjectBugreportUnwind : public CommandObjectParsed
{
public:
    CommandObjectBugreportUnwind(CommandInterpreter &interpreter) :
        CommandObjectParsed(interpreter,
                            "bugreport unwind",
                            "Create a bugreport for a bug in the stack unwinding code.",
                            nullptr),
        m_option_group(interpreter),
        m_outfile_options()
    {
        m_option_group.Append (&m_outfile_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3);
        m_option_group.Finalize();
    }

    ~CommandObjectBugreportUnwind() override
    {
    }

    Options *
    GetOptions() override
    {
        return &m_option_group;
    }

protected:
    bool
    DoExecute(Args& command, CommandReturnObject &result) override
    {
        StringList commands;
        commands.AppendString("thread backtrace");

        Thread *thread = m_exe_ctx.GetThreadPtr();
        if (thread)
        {
            char command_buffer[256];

            uint32_t frame_count = thread->GetStackFrameCount();
            for (uint32_t i = 0; i < frame_count; ++i)
            {
                StackFrameSP frame = thread->GetStackFrameAtIndex(i);
                lldb::addr_t pc = frame->GetStackID().GetPC();

                snprintf(command_buffer, sizeof(command_buffer), "disassemble --bytes --address 0x%" PRIx64, pc);
                commands.AppendString(command_buffer);

                snprintf(command_buffer, sizeof(command_buffer), "image show-unwind --address 0x%" PRIx64, pc);
                commands.AppendString(command_buffer);
            }
        }

        const FileSpec &outfile_spec = m_outfile_options.GetFile().GetCurrentValue();
        if (outfile_spec)
        {
            char path[PATH_MAX];
            outfile_spec.GetPath (path, sizeof(path));

            uint32_t open_options = File::eOpenOptionWrite |
                                    File::eOpenOptionCanCreate |
                                    File::eOpenOptionAppend |
                                    File::eOpenOptionCloseOnExec;

            const bool append = m_outfile_options.GetAppend().GetCurrentValue();
            if (!append)
                open_options |= File::eOpenOptionTruncate;
            
            StreamFileSP outfile_stream = std::make_shared<StreamFile>();
            Error error = outfile_stream->GetFile().Open(path, open_options);
            if (error.Fail())
            {
                result.AppendErrorWithFormat("Failed to open file '%s' for %s: %s\n",
                                             path,
                                             append ? "append" : "write",
                                             error.AsCString());
                result.SetStatus(eReturnStatusFailed);
                return false;
            }

            result.SetImmediateOutputStream(outfile_stream);
        }

        CommandInterpreterRunOptions options;
        options.SetStopOnError(false);
        options.SetEchoCommands(true);
        options.SetPrintResults(true);
        options.SetAddToHistory(false);
        m_interpreter.HandleCommands(commands, &m_exe_ctx, options, result);

        return result.Succeeded();
    }

private:
    OptionGroupOptions m_option_group;
    OptionGroupOutputFile m_outfile_options;
};

#pragma mark CommandObjectMultiwordBugreport

//-------------------------------------------------------------------------
// CommandObjectMultiwordBugreport
//-------------------------------------------------------------------------

CommandObjectMultiwordBugreport::CommandObjectMultiwordBugreport(CommandInterpreter &interpreter)
    : CommandObjectMultiword(interpreter, "bugreport", "Commands for creating domain-specific bug reports.",
                             "bugreport <subcommand> [<subcommand-options>]")
{

    LoadSubCommand("unwind", CommandObjectSP(new CommandObjectBugreportUnwind(interpreter)));
}

CommandObjectMultiwordBugreport::~CommandObjectMultiwordBugreport ()
{
}