blob: b0533e132db38e74131dacd41ebe898f884ba8ba [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBCommandReturnObject.cpp -------------------------------*- 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
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000010#include "lldb/API/SBCommandReturnObject.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000011#include "lldb/API/SBStream.h"
12
Caroline Tice7826c882010-10-26 03:11:13 +000013#include "lldb/Core/Log.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000014#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015
16using namespace lldb;
Greg Clayton63094e02010-06-23 01:19:29 +000017using namespace lldb_private;
Chris Lattner24943d22010-06-08 16:52:24 +000018
19SBCommandReturnObject::SBCommandReturnObject () :
Greg Clayton63094e02010-06-23 01:19:29 +000020 m_opaque_ap (new CommandReturnObject ())
Chris Lattner24943d22010-06-08 16:52:24 +000021{
22}
23
Greg Clayton538eb822010-11-05 23:17:00 +000024SBCommandReturnObject::SBCommandReturnObject (const SBCommandReturnObject &rhs):
25 m_opaque_ap ()
26{
27 if (rhs.m_opaque_ap.get())
28 m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
29}
30
Greg Claytond8b9b062011-08-19 23:06:38 +000031SBCommandReturnObject::SBCommandReturnObject (CommandReturnObject *ptr) :
32 m_opaque_ap (ptr)
33{
34}
35
36CommandReturnObject *
37SBCommandReturnObject::Release ()
38{
39 return m_opaque_ap.release();
40}
41
Greg Clayton538eb822010-11-05 23:17:00 +000042const SBCommandReturnObject &
43SBCommandReturnObject::operator = (const SBCommandReturnObject &rhs)
44{
45 if (this != &rhs)
46 {
47 if (rhs.m_opaque_ap.get())
48 m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
49 else
50 m_opaque_ap.reset();
51 }
52 return *this;
53}
54
55
Chris Lattner24943d22010-06-08 16:52:24 +000056SBCommandReturnObject::~SBCommandReturnObject ()
57{
Greg Clayton63094e02010-06-23 01:19:29 +000058 // m_opaque_ap will automatically delete any pointer it owns
Chris Lattner24943d22010-06-08 16:52:24 +000059}
60
61bool
62SBCommandReturnObject::IsValid() const
63{
Greg Clayton63094e02010-06-23 01:19:29 +000064 return m_opaque_ap.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000065}
66
67
68const char *
69SBCommandReturnObject::GetOutput ()
70{
Greg Clayton952e9dc2013-03-27 23:08:40 +000071 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000072
Greg Clayton63094e02010-06-23 01:19:29 +000073 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +000074 {
75 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +000076 log->Printf ("SBCommandReturnObject(%p)::GetOutput () => \"%s\"", m_opaque_ap.get(),
Jim Ingham2e8cb8a2011-02-19 02:53:09 +000077 m_opaque_ap->GetOutputData());
Caroline Tice61ba7ec2010-10-26 23:49:36 +000078
Jim Ingham2e8cb8a2011-02-19 02:53:09 +000079 return m_opaque_ap->GetOutputData();
Caroline Tice7826c882010-10-26 03:11:13 +000080 }
81
82 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +000083 log->Printf ("SBCommandReturnObject(%p)::GetOutput () => NULL", m_opaque_ap.get());
Caroline Tice7826c882010-10-26 03:11:13 +000084
Chris Lattner24943d22010-06-08 16:52:24 +000085 return NULL;
86}
87
88const char *
89SBCommandReturnObject::GetError ()
90{
Greg Clayton952e9dc2013-03-27 23:08:40 +000091 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000092
Greg Clayton63094e02010-06-23 01:19:29 +000093 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +000094 {
95 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +000096 log->Printf ("SBCommandReturnObject(%p)::GetError () => \"%s\"", m_opaque_ap.get(),
Jim Ingham2e8cb8a2011-02-19 02:53:09 +000097 m_opaque_ap->GetErrorData());
Caroline Tice61ba7ec2010-10-26 23:49:36 +000098
Jim Ingham2e8cb8a2011-02-19 02:53:09 +000099 return m_opaque_ap->GetErrorData();
Caroline Tice7826c882010-10-26 03:11:13 +0000100 }
101
102 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +0000103 log->Printf ("SBCommandReturnObject(%p)::GetError () => NULL", m_opaque_ap.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000104
Chris Lattner24943d22010-06-08 16:52:24 +0000105 return NULL;
106}
107
108size_t
109SBCommandReturnObject::GetOutputSize ()
110{
Greg Clayton63094e02010-06-23 01:19:29 +0000111 if (m_opaque_ap.get())
Jim Ingham2e8cb8a2011-02-19 02:53:09 +0000112 return strlen (m_opaque_ap->GetOutputData());
Chris Lattner24943d22010-06-08 16:52:24 +0000113 return 0;
114}
115
116size_t
117SBCommandReturnObject::GetErrorSize ()
118{
Greg Clayton63094e02010-06-23 01:19:29 +0000119 if (m_opaque_ap.get())
Jim Ingham2e8cb8a2011-02-19 02:53:09 +0000120 return strlen(m_opaque_ap->GetErrorData());
Chris Lattner24943d22010-06-08 16:52:24 +0000121 return 0;
122}
123
124size_t
125SBCommandReturnObject::PutOutput (FILE *fh)
126{
127 if (fh)
128 {
129 size_t num_bytes = GetOutputSize ();
130 if (num_bytes)
131 return ::fprintf (fh, "%s", GetOutput());
132 }
133 return 0;
134}
135
136size_t
137SBCommandReturnObject::PutError (FILE *fh)
138{
139 if (fh)
140 {
141 size_t num_bytes = GetErrorSize ();
142 if (num_bytes)
143 return ::fprintf (fh, "%s", GetError());
144 }
145 return 0;
146}
147
148void
149SBCommandReturnObject::Clear()
150{
Greg Clayton63094e02010-06-23 01:19:29 +0000151 if (m_opaque_ap.get())
152 m_opaque_ap->Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000153}
154
155lldb::ReturnStatus
156SBCommandReturnObject::GetStatus()
157{
Greg Clayton63094e02010-06-23 01:19:29 +0000158 if (m_opaque_ap.get())
159 return m_opaque_ap->GetStatus();
Chris Lattner24943d22010-06-08 16:52:24 +0000160 return lldb::eReturnStatusInvalid;
161}
162
Jim Ingham839de392012-06-27 17:25:36 +0000163void
164SBCommandReturnObject::SetStatus(lldb::ReturnStatus status)
165{
166 if (m_opaque_ap.get())
167 m_opaque_ap->SetStatus(status);
168}
169
Chris Lattner24943d22010-06-08 16:52:24 +0000170bool
171SBCommandReturnObject::Succeeded ()
172{
Greg Clayton63094e02010-06-23 01:19:29 +0000173 if (m_opaque_ap.get())
174 return m_opaque_ap->Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000175 return false;
176}
177
178bool
179SBCommandReturnObject::HasResult ()
180{
Greg Clayton63094e02010-06-23 01:19:29 +0000181 if (m_opaque_ap.get())
182 return m_opaque_ap->HasResult();
Chris Lattner24943d22010-06-08 16:52:24 +0000183 return false;
184}
185
186void
187SBCommandReturnObject::AppendMessage (const char *message)
188{
Greg Clayton63094e02010-06-23 01:19:29 +0000189 if (m_opaque_ap.get())
190 m_opaque_ap->AppendMessage (message);
Chris Lattner24943d22010-06-08 16:52:24 +0000191}
192
Enrico Granata6d101882012-09-28 23:57:51 +0000193void
194SBCommandReturnObject::AppendWarning (const char *message)
195{
196 if (m_opaque_ap.get())
197 m_opaque_ap->AppendWarning (message);
198}
199
Greg Clayton63094e02010-06-23 01:19:29 +0000200CommandReturnObject *
201SBCommandReturnObject::operator ->() const
Chris Lattner24943d22010-06-08 16:52:24 +0000202{
Greg Clayton63094e02010-06-23 01:19:29 +0000203 return m_opaque_ap.get();
204}
205
206CommandReturnObject *
207SBCommandReturnObject::get() const
208{
209 return m_opaque_ap.get();
210}
211
212CommandReturnObject &
213SBCommandReturnObject::operator *() const
214{
215 assert(m_opaque_ap.get());
216 return *(m_opaque_ap.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000217}
218
219
Greg Clayton63094e02010-06-23 01:19:29 +0000220CommandReturnObject &
221SBCommandReturnObject::ref() const
Chris Lattner24943d22010-06-08 16:52:24 +0000222{
Greg Clayton63094e02010-06-23 01:19:29 +0000223 assert(m_opaque_ap.get());
224 return *(m_opaque_ap.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000225}
226
227
228void
Greg Clayton63094e02010-06-23 01:19:29 +0000229SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000230{
Greg Clayton63094e02010-06-23 01:19:29 +0000231 if (m_opaque_ap.get())
232 m_opaque_ap.reset (ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000233}
234
Caroline Tice98f930f2010-09-20 05:20:02 +0000235bool
236SBCommandReturnObject::GetDescription (SBStream &description)
237{
Greg Clayton96154be2011-11-13 06:57:31 +0000238 Stream &strm = description.ref();
239
Caroline Tice98f930f2010-09-20 05:20:02 +0000240 if (m_opaque_ap.get())
241 {
242 description.Printf ("Status: ");
243 lldb::ReturnStatus status = m_opaque_ap->GetStatus();
244 if (status == lldb::eReturnStatusStarted)
Greg Clayton96154be2011-11-13 06:57:31 +0000245 strm.PutCString ("Started");
Caroline Tice98f930f2010-09-20 05:20:02 +0000246 else if (status == lldb::eReturnStatusInvalid)
Greg Clayton96154be2011-11-13 06:57:31 +0000247 strm.PutCString ("Invalid");
Caroline Tice98f930f2010-09-20 05:20:02 +0000248 else if (m_opaque_ap->Succeeded())
Greg Clayton96154be2011-11-13 06:57:31 +0000249 strm.PutCString ("Success");
Caroline Tice98f930f2010-09-20 05:20:02 +0000250 else
Greg Clayton96154be2011-11-13 06:57:31 +0000251 strm.PutCString ("Fail");
Caroline Tice98f930f2010-09-20 05:20:02 +0000252
253 if (GetOutputSize() > 0)
Greg Clayton96154be2011-11-13 06:57:31 +0000254 strm.Printf ("\nOutput Message:\n%s", GetOutput());
Caroline Tice98f930f2010-09-20 05:20:02 +0000255
256 if (GetErrorSize() > 0)
Greg Clayton96154be2011-11-13 06:57:31 +0000257 strm.Printf ("\nError Message:\n%s", GetError());
Caroline Tice98f930f2010-09-20 05:20:02 +0000258 }
259 else
Greg Clayton96154be2011-11-13 06:57:31 +0000260 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +0000261
262 return true;
263}
Jim Ingham2e8cb8a2011-02-19 02:53:09 +0000264
265void
266SBCommandReturnObject::SetImmediateOutputFile (FILE *fh)
267{
268 if (m_opaque_ap.get())
269 m_opaque_ap->SetImmediateOutputFile (fh);
270}
Enrico Granatac2bc7942012-10-16 20:57:12 +0000271
Jim Ingham2e8cb8a2011-02-19 02:53:09 +0000272void
273SBCommandReturnObject::SetImmediateErrorFile (FILE *fh)
274{
275 if (m_opaque_ap.get())
276 m_opaque_ap->SetImmediateErrorFile (fh);
277}
Enrico Granata6b1596d2011-08-16 23:24:13 +0000278
279void
280SBCommandReturnObject::PutCString(const char* string, int len)
281{
282 if (m_opaque_ap.get())
283 {
Enrico Granata78280ba2013-05-02 17:29:04 +0000284 if (len == 0 || string == NULL || *string == 0)
Jim Inghambb04be12012-12-15 02:40:54 +0000285 {
286 return;
287 }
288 else if (len > 0)
289 {
290 std::string buffer(string, len);
291 m_opaque_ap->AppendMessage(buffer.c_str());
292 }
293 else
294 m_opaque_ap->AppendMessage(string);
Enrico Granata6b1596d2011-08-16 23:24:13 +0000295 }
296}
297
Enrico Granatac2bc7942012-10-16 20:57:12 +0000298const char *
299SBCommandReturnObject::GetOutput (bool only_if_no_immediate)
300{
301 if (!m_opaque_ap.get())
302 return NULL;
303 if (only_if_no_immediate == false || m_opaque_ap->GetImmediateOutputStream().get() == NULL)
304 return GetOutput();
305 return NULL;
306}
307
308const char *
309SBCommandReturnObject::GetError (bool only_if_no_immediate)
310{
311 if (!m_opaque_ap.get())
312 return NULL;
313 if (only_if_no_immediate == false || m_opaque_ap->GetImmediateErrorStream().get() == NULL)
314 return GetError();
315 return NULL;
316}
317
318size_t
Enrico Granata6b1596d2011-08-16 23:24:13 +0000319SBCommandReturnObject::Printf(const char* format, ...)
320{
321 if (m_opaque_ap.get())
322 {
323 va_list args;
324 va_start (args, format);
325 size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
326 va_end (args);
327 return result;
328 }
329 return 0;
330}
331