1 //===-- CommandReturnObject.h -----------------------------------*- 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 #ifndef liblldb_CommandReturnObject_h_
11 #define liblldb_CommandReturnObject_h_
15 // Other libraries and framework 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"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/FormatVariadic.h"
28 namespace lldb_private {
30 class CommandReturnObject {
32 CommandReturnObject();
34 ~CommandReturnObject();
36 llvm::StringRef GetOutputData() {
37 lldb::StreamSP stream_sp(m_out_stream.GetStreamAtIndex(eStreamStringIndex));
39 return static_pointer_cast<StreamString>(stream_sp)->GetString();
40 return llvm::StringRef();
43 llvm::StringRef GetErrorData() {
44 lldb::StreamSP stream_sp(m_err_stream.GetStreamAtIndex(eStreamStringIndex));
46 return static_pointer_cast<StreamString>(stream_sp)->GetString();
47 return llvm::StringRef();
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));
54 stream_sp.reset(new StreamString());
55 m_out_stream.SetStreamAtIndex(eStreamStringIndex, stream_sp);
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));
64 stream_sp.reset(new StreamString());
65 m_err_stream.SetStreamAtIndex(eStreamStringIndex, stream_sp);
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);
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);
80 void SetImmediateOutputStream(const lldb::StreamSP &stream_sp) {
81 m_out_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
84 void SetImmediateErrorStream(const lldb::StreamSP &stream_sp) {
85 m_err_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
88 lldb::StreamSP GetImmediateOutputStream() {
89 return m_out_stream.GetStreamAtIndex(eImmediateStreamIndex);
92 lldb::StreamSP GetImmediateErrorStream() {
93 return m_err_stream.GetStreamAtIndex(eImmediateStreamIndex);
98 void AppendMessage(llvm::StringRef in_string);
100 void AppendMessageWithFormat(const char *format, ...)
101 __attribute__((format(printf, 2, 3)));
103 void AppendRawWarning(llvm::StringRef in_string);
105 void AppendWarning(llvm::StringRef in_string);
107 void AppendWarningWithFormat(const char *format, ...)
108 __attribute__((format(printf, 2, 3)));
110 void AppendError(llvm::StringRef in_string);
112 void AppendRawError(llvm::StringRef in_string);
114 void AppendErrorWithFormat(const char *format, ...)
115 __attribute__((format(printf, 2, 3)));
117 template <typename... Args>
118 void AppendMessageWithFormatv(const char *format, Args &&... args) {
119 AppendMessage(llvm::formatv(format, std::forward<Args>(args)...).str());
122 template <typename... Args>
123 void AppendWarningWithFormatv(const char *format, Args &&... args) {
124 AppendWarning(llvm::formatv(format, std::forward<Args>(args)...).str());
127 template <typename... Args>
128 void AppendErrorWithFormatv(const char *format, Args &&... args) {
129 AppendError(llvm::formatv(format, std::forward<Args>(args)...).str());
132 void SetError(const Status &error, const char *fallback_error_cstr = nullptr);
134 void SetError(llvm::StringRef error_cstr);
136 lldb::ReturnStatus GetStatus();
138 void SetStatus(lldb::ReturnStatus status);
144 bool GetDidChangeProcessState();
146 void SetDidChangeProcessState(bool b);
148 bool GetInteractive() const;
150 void SetInteractive(bool b);
152 bool GetAbnormalStopWasExpected() const {
153 return m_abnormal_stop_was_expected;
156 void SetAbnormalStopWasExpected(bool signal_was_expected) {
157 m_abnormal_stop_was_expected = signal_was_expected;
161 enum { eStreamStringIndex = 0, eImmediateStreamIndex = 1 };
163 StreamTee m_out_stream;
164 StreamTee m_err_stream;
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
170 bool m_abnormal_stop_was_expected; // This is to support
171 // eHandleCommandFlagStopOnCrash vrs.
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.
180 } // namespace lldb_private
182 #endif // liblldb_CommandReturnObject_h_