1 //===-- CommandReturnObject.h -----------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_CommandReturnObject_h_
10 #define liblldb_CommandReturnObject_h_
12 #include "lldb/Core/STLUtils.h"
13 #include "lldb/Core/StreamFile.h"
14 #include "lldb/Utility/StreamString.h"
15 #include "lldb/Utility/StreamTee.h"
16 #include "lldb/lldb-private.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/FormatVariadic.h"
23 namespace lldb_private {
25 class CommandReturnObject {
27 CommandReturnObject();
29 ~CommandReturnObject();
31 llvm::StringRef GetOutputData() {
32 lldb::StreamSP stream_sp(m_out_stream.GetStreamAtIndex(eStreamStringIndex));
34 return static_pointer_cast<StreamString>(stream_sp)->GetString();
35 return llvm::StringRef();
38 llvm::StringRef GetErrorData() {
39 lldb::StreamSP stream_sp(m_err_stream.GetStreamAtIndex(eStreamStringIndex));
41 return static_pointer_cast<StreamString>(stream_sp)->GetString();
42 return llvm::StringRef();
45 Stream &GetOutputStream() {
46 // Make sure we at least have our normal string stream output stream
47 lldb::StreamSP stream_sp(m_out_stream.GetStreamAtIndex(eStreamStringIndex));
49 stream_sp.reset(new StreamString());
50 m_out_stream.SetStreamAtIndex(eStreamStringIndex, stream_sp);
55 Stream &GetErrorStream() {
56 // Make sure we at least have our normal string stream output stream
57 lldb::StreamSP stream_sp(m_err_stream.GetStreamAtIndex(eStreamStringIndex));
59 stream_sp.reset(new StreamString());
60 m_err_stream.SetStreamAtIndex(eStreamStringIndex, stream_sp);
65 void SetImmediateOutputFile(FILE *fh, bool transfer_fh_ownership = false) {
66 lldb::StreamSP stream_sp(new StreamFile(fh, transfer_fh_ownership));
67 m_out_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
70 void SetImmediateErrorFile(FILE *fh, bool transfer_fh_ownership = false) {
71 lldb::StreamSP stream_sp(new StreamFile(fh, transfer_fh_ownership));
72 m_err_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
75 void SetImmediateOutputStream(const lldb::StreamSP &stream_sp) {
76 m_out_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
79 void SetImmediateErrorStream(const lldb::StreamSP &stream_sp) {
80 m_err_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
83 lldb::StreamSP GetImmediateOutputStream() {
84 return m_out_stream.GetStreamAtIndex(eImmediateStreamIndex);
87 lldb::StreamSP GetImmediateErrorStream() {
88 return m_err_stream.GetStreamAtIndex(eImmediateStreamIndex);
93 void AppendMessage(llvm::StringRef in_string);
95 void AppendMessageWithFormat(const char *format, ...)
96 __attribute__((format(printf, 2, 3)));
98 void AppendRawWarning(llvm::StringRef in_string);
100 void AppendWarning(llvm::StringRef in_string);
102 void AppendWarningWithFormat(const char *format, ...)
103 __attribute__((format(printf, 2, 3)));
105 void AppendError(llvm::StringRef in_string);
107 void AppendRawError(llvm::StringRef in_string);
109 void AppendErrorWithFormat(const char *format, ...)
110 __attribute__((format(printf, 2, 3)));
112 template <typename... Args>
113 void AppendMessageWithFormatv(const char *format, Args &&... args) {
114 AppendMessage(llvm::formatv(format, std::forward<Args>(args)...).str());
117 template <typename... Args>
118 void AppendWarningWithFormatv(const char *format, Args &&... args) {
119 AppendWarning(llvm::formatv(format, std::forward<Args>(args)...).str());
122 template <typename... Args>
123 void AppendErrorWithFormatv(const char *format, Args &&... args) {
124 AppendError(llvm::formatv(format, std::forward<Args>(args)...).str());
127 void SetError(const Status &error, const char *fallback_error_cstr = nullptr);
129 void SetError(llvm::StringRef error_cstr);
131 lldb::ReturnStatus GetStatus();
133 void SetStatus(lldb::ReturnStatus status);
139 bool GetDidChangeProcessState();
141 void SetDidChangeProcessState(bool b);
143 bool GetInteractive() const;
145 void SetInteractive(bool b);
147 bool GetAbnormalStopWasExpected() const {
148 return m_abnormal_stop_was_expected;
151 void SetAbnormalStopWasExpected(bool signal_was_expected) {
152 m_abnormal_stop_was_expected = signal_was_expected;
156 enum { eStreamStringIndex = 0, eImmediateStreamIndex = 1 };
158 StreamTee m_out_stream;
159 StreamTee m_err_stream;
161 lldb::ReturnStatus m_status;
162 bool m_did_change_process_state;
163 bool m_interactive; // If true, then the input handle from the debugger will
165 bool m_abnormal_stop_was_expected; // This is to support
166 // eHandleCommandFlagStopOnCrash vrs.
168 // The attach command often ends up with the process stopped due to a signal.
169 // Normally that would mean stop on crash should halt batch execution, but we
170 // obviously don't want that for attach. Using this flag, the attach command
171 // (and anything else for which this is relevant) can say that the signal is
172 // expected, and batch command execution can continue.
175 } // namespace lldb_private
177 #endif // liblldb_CommandReturnObject_h_