]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Interpreter/CommandReturnObject.h
MFV r336944: 9286 want refreservation=auto
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Interpreter / CommandReturnObject.h
1 //===-- CommandReturnObject.h -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_CommandReturnObject_h_
11 #define liblldb_CommandReturnObject_h_
12
13 // C Includes
14 // C++ Includes
15 // Other libraries and framework includes
16 // Project includes
17 #include "lldb/Core/STLUtils.h"
18 #include "lldb/Core/StreamFile.h"
19 #include "lldb/Utility/StreamString.h"
20 #include "lldb/Utility/StreamTee.h"
21 #include "lldb/lldb-private.h"
22
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/FormatVariadic.h"
25
26 #include <memory>
27
28 namespace lldb_private {
29
30 class CommandReturnObject {
31 public:
32   CommandReturnObject();
33
34   ~CommandReturnObject();
35
36   llvm::StringRef GetOutputData() {
37     lldb::StreamSP stream_sp(m_out_stream.GetStreamAtIndex(eStreamStringIndex));
38     if (stream_sp)
39       return static_pointer_cast<StreamString>(stream_sp)->GetString();
40     return llvm::StringRef();
41   }
42
43   llvm::StringRef GetErrorData() {
44     lldb::StreamSP stream_sp(m_err_stream.GetStreamAtIndex(eStreamStringIndex));
45     if (stream_sp)
46       return static_pointer_cast<StreamString>(stream_sp)->GetString();
47     return llvm::StringRef();
48   }
49
50   Stream &GetOutputStream() {
51     // Make sure we at least have our normal string stream output stream
52     lldb::StreamSP stream_sp(m_out_stream.GetStreamAtIndex(eStreamStringIndex));
53     if (!stream_sp) {
54       stream_sp.reset(new StreamString());
55       m_out_stream.SetStreamAtIndex(eStreamStringIndex, stream_sp);
56     }
57     return m_out_stream;
58   }
59
60   Stream &GetErrorStream() {
61     // Make sure we at least have our normal string stream output stream
62     lldb::StreamSP stream_sp(m_err_stream.GetStreamAtIndex(eStreamStringIndex));
63     if (!stream_sp) {
64       stream_sp.reset(new StreamString());
65       m_err_stream.SetStreamAtIndex(eStreamStringIndex, stream_sp);
66     }
67     return m_err_stream;
68   }
69
70   void SetImmediateOutputFile(FILE *fh, bool transfer_fh_ownership = false) {
71     lldb::StreamSP stream_sp(new StreamFile(fh, transfer_fh_ownership));
72     m_out_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
73   }
74
75   void SetImmediateErrorFile(FILE *fh, bool transfer_fh_ownership = false) {
76     lldb::StreamSP stream_sp(new StreamFile(fh, transfer_fh_ownership));
77     m_err_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
78   }
79
80   void SetImmediateOutputStream(const lldb::StreamSP &stream_sp) {
81     m_out_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
82   }
83
84   void SetImmediateErrorStream(const lldb::StreamSP &stream_sp) {
85     m_err_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
86   }
87
88   lldb::StreamSP GetImmediateOutputStream() {
89     return m_out_stream.GetStreamAtIndex(eImmediateStreamIndex);
90   }
91
92   lldb::StreamSP GetImmediateErrorStream() {
93     return m_err_stream.GetStreamAtIndex(eImmediateStreamIndex);
94   }
95
96   void Clear();
97
98   void AppendMessage(llvm::StringRef in_string);
99
100   void AppendMessageWithFormat(const char *format, ...)
101       __attribute__((format(printf, 2, 3)));
102
103   void AppendRawWarning(llvm::StringRef in_string);
104
105   void AppendWarning(llvm::StringRef in_string);
106
107   void AppendWarningWithFormat(const char *format, ...)
108       __attribute__((format(printf, 2, 3)));
109
110   void AppendError(llvm::StringRef in_string);
111
112   void AppendRawError(llvm::StringRef in_string);
113
114   void AppendErrorWithFormat(const char *format, ...)
115       __attribute__((format(printf, 2, 3)));
116
117   template <typename... Args>
118   void AppendMessageWithFormatv(const char *format, Args &&... args) {
119     AppendMessage(llvm::formatv(format, std::forward<Args>(args)...).str());
120   }
121
122   template <typename... Args>
123   void AppendWarningWithFormatv(const char *format, Args &&... args) {
124     AppendWarning(llvm::formatv(format, std::forward<Args>(args)...).str());
125   }
126
127   template <typename... Args>
128   void AppendErrorWithFormatv(const char *format, Args &&... args) {
129     AppendError(llvm::formatv(format, std::forward<Args>(args)...).str());
130   }
131
132   void SetError(const Status &error, const char *fallback_error_cstr = nullptr);
133
134   void SetError(llvm::StringRef error_cstr);
135
136   lldb::ReturnStatus GetStatus();
137
138   void SetStatus(lldb::ReturnStatus status);
139
140   bool Succeeded();
141
142   bool HasResult();
143
144   bool GetDidChangeProcessState();
145
146   void SetDidChangeProcessState(bool b);
147
148   bool GetInteractive() const;
149
150   void SetInteractive(bool b);
151
152   bool GetAbnormalStopWasExpected() const {
153     return m_abnormal_stop_was_expected;
154   }
155
156   void SetAbnormalStopWasExpected(bool signal_was_expected) {
157     m_abnormal_stop_was_expected = signal_was_expected;
158   }
159
160 private:
161   enum { eStreamStringIndex = 0, eImmediateStreamIndex = 1 };
162
163   StreamTee m_out_stream;
164   StreamTee m_err_stream;
165
166   lldb::ReturnStatus m_status;
167   bool m_did_change_process_state;
168   bool m_interactive; // If true, then the input handle from the debugger will
169                       // be hooked up
170   bool m_abnormal_stop_was_expected; // This is to support
171                                      // eHandleCommandFlagStopOnCrash vrs.
172                                      // attach.
173   // The attach command often ends up with the process stopped due to a signal.
174   // Normally that would mean stop on crash should halt batch execution, but we
175   // obviously don't want that for attach.  Using this flag, the attach command
176   // (and anything else for which this is relevant) can say that the signal is
177   // expected, and batch command execution can continue.
178 };
179
180 } // namespace lldb_private
181
182 #endif // liblldb_CommandReturnObject_h_