• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- 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 
10 #include "lldb/API/SBCommandReturnObject.h"
11 #include "lldb/API/SBError.h"
12 #include "lldb/API/SBStream.h"
13 
14 #include "lldb/Core/Error.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Interpreter/CommandReturnObject.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
SBCommandReturnObject()21 SBCommandReturnObject::SBCommandReturnObject () :
22     m_opaque_ap (new CommandReturnObject ())
23 {
24 }
25 
SBCommandReturnObject(const SBCommandReturnObject & rhs)26 SBCommandReturnObject::SBCommandReturnObject (const SBCommandReturnObject &rhs):
27     m_opaque_ap ()
28 {
29     if (rhs.m_opaque_ap.get())
30         m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
31 }
32 
SBCommandReturnObject(CommandReturnObject * ptr)33 SBCommandReturnObject::SBCommandReturnObject (CommandReturnObject *ptr) :
34     m_opaque_ap (ptr)
35 {
36 }
37 
38 CommandReturnObject *
Release()39 SBCommandReturnObject::Release ()
40 {
41     return m_opaque_ap.release();
42 }
43 
44 const SBCommandReturnObject &
operator =(const SBCommandReturnObject & rhs)45 SBCommandReturnObject::operator = (const SBCommandReturnObject &rhs)
46 {
47     if (this != &rhs)
48     {
49         if (rhs.m_opaque_ap.get())
50             m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
51         else
52             m_opaque_ap.reset();
53     }
54     return *this;
55 }
56 
57 
~SBCommandReturnObject()58 SBCommandReturnObject::~SBCommandReturnObject ()
59 {
60     // m_opaque_ap will automatically delete any pointer it owns
61 }
62 
63 bool
IsValid() const64 SBCommandReturnObject::IsValid() const
65 {
66     return m_opaque_ap.get() != NULL;
67 }
68 
69 
70 const char *
GetOutput()71 SBCommandReturnObject::GetOutput ()
72 {
73     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
74 
75     if (m_opaque_ap.get())
76     {
77         if (log)
78             log->Printf ("SBCommandReturnObject(%p)::GetOutput () => \"%s\"", m_opaque_ap.get(),
79                          m_opaque_ap->GetOutputData());
80 
81         return m_opaque_ap->GetOutputData();
82     }
83 
84     if (log)
85         log->Printf ("SBCommandReturnObject(%p)::GetOutput () => NULL", m_opaque_ap.get());
86 
87     return NULL;
88 }
89 
90 const char *
GetError()91 SBCommandReturnObject::GetError ()
92 {
93     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
94 
95     if (m_opaque_ap.get())
96     {
97         if (log)
98             log->Printf ("SBCommandReturnObject(%p)::GetError () => \"%s\"", m_opaque_ap.get(),
99                          m_opaque_ap->GetErrorData());
100 
101         return m_opaque_ap->GetErrorData();
102     }
103 
104     if (log)
105         log->Printf ("SBCommandReturnObject(%p)::GetError () => NULL", m_opaque_ap.get());
106 
107     return NULL;
108 }
109 
110 size_t
GetOutputSize()111 SBCommandReturnObject::GetOutputSize ()
112 {
113     if (m_opaque_ap.get())
114         return strlen (m_opaque_ap->GetOutputData());
115     return 0;
116 }
117 
118 size_t
GetErrorSize()119 SBCommandReturnObject::GetErrorSize ()
120 {
121     if (m_opaque_ap.get())
122         return strlen(m_opaque_ap->GetErrorData());
123     return 0;
124 }
125 
126 size_t
PutOutput(FILE * fh)127 SBCommandReturnObject::PutOutput (FILE *fh)
128 {
129     if (fh)
130     {
131         size_t num_bytes = GetOutputSize ();
132         if (num_bytes)
133             return ::fprintf (fh, "%s", GetOutput());
134     }
135     return 0;
136 }
137 
138 size_t
PutError(FILE * fh)139 SBCommandReturnObject::PutError (FILE *fh)
140 {
141     if (fh)
142     {
143         size_t num_bytes = GetErrorSize ();
144         if (num_bytes)
145             return ::fprintf (fh, "%s", GetError());
146     }
147     return 0;
148 }
149 
150 void
Clear()151 SBCommandReturnObject::Clear()
152 {
153     if (m_opaque_ap.get())
154         m_opaque_ap->Clear();
155 }
156 
157 lldb::ReturnStatus
GetStatus()158 SBCommandReturnObject::GetStatus()
159 {
160     if (m_opaque_ap.get())
161         return m_opaque_ap->GetStatus();
162     return lldb::eReturnStatusInvalid;
163 }
164 
165 void
SetStatus(lldb::ReturnStatus status)166 SBCommandReturnObject::SetStatus(lldb::ReturnStatus status)
167 {
168     if (m_opaque_ap.get())
169          m_opaque_ap->SetStatus(status);
170 }
171 
172 bool
Succeeded()173 SBCommandReturnObject::Succeeded ()
174 {
175     if (m_opaque_ap.get())
176         return m_opaque_ap->Succeeded();
177     return false;
178 }
179 
180 bool
HasResult()181 SBCommandReturnObject::HasResult ()
182 {
183     if (m_opaque_ap.get())
184         return m_opaque_ap->HasResult();
185     return false;
186 }
187 
188 void
AppendMessage(const char * message)189 SBCommandReturnObject::AppendMessage (const char *message)
190 {
191     if (m_opaque_ap.get())
192         m_opaque_ap->AppendMessage (message);
193 }
194 
195 void
AppendWarning(const char * message)196 SBCommandReturnObject::AppendWarning (const char *message)
197 {
198     if (m_opaque_ap.get())
199         m_opaque_ap->AppendWarning (message);
200 }
201 
202 CommandReturnObject *
operator ->() const203 SBCommandReturnObject::operator ->() const
204 {
205     return m_opaque_ap.get();
206 }
207 
208 CommandReturnObject *
get() const209 SBCommandReturnObject::get() const
210 {
211     return m_opaque_ap.get();
212 }
213 
214 CommandReturnObject &
operator *() const215 SBCommandReturnObject::operator *() const
216 {
217     assert(m_opaque_ap.get());
218     return *(m_opaque_ap.get());
219 }
220 
221 
222 CommandReturnObject &
ref() const223 SBCommandReturnObject::ref() const
224 {
225     assert(m_opaque_ap.get());
226     return *(m_opaque_ap.get());
227 }
228 
229 
230 void
SetLLDBObjectPtr(CommandReturnObject * ptr)231 SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
232 {
233     if (m_opaque_ap.get())
234         m_opaque_ap.reset (ptr);
235 }
236 
237 bool
GetDescription(SBStream & description)238 SBCommandReturnObject::GetDescription (SBStream &description)
239 {
240     Stream &strm = description.ref();
241 
242     if (m_opaque_ap.get())
243     {
244         description.Printf ("Status:  ");
245         lldb::ReturnStatus status = m_opaque_ap->GetStatus();
246         if (status == lldb::eReturnStatusStarted)
247             strm.PutCString ("Started");
248         else if (status == lldb::eReturnStatusInvalid)
249             strm.PutCString ("Invalid");
250         else if (m_opaque_ap->Succeeded())
251             strm.PutCString ("Success");
252         else
253             strm.PutCString ("Fail");
254 
255         if (GetOutputSize() > 0)
256             strm.Printf ("\nOutput Message:\n%s", GetOutput());
257 
258         if (GetErrorSize() > 0)
259             strm.Printf ("\nError Message:\n%s", GetError());
260     }
261     else
262         strm.PutCString ("No value");
263 
264     return true;
265 }
266 
267 void
SetImmediateOutputFile(FILE * fh)268 SBCommandReturnObject::SetImmediateOutputFile (FILE *fh)
269 {
270     if (m_opaque_ap.get())
271         m_opaque_ap->SetImmediateOutputFile (fh);
272 }
273 
274 void
SetImmediateErrorFile(FILE * fh)275 SBCommandReturnObject::SetImmediateErrorFile (FILE *fh)
276 {
277     if (m_opaque_ap.get())
278         m_opaque_ap->SetImmediateErrorFile (fh);
279 }
280 
281 void
PutCString(const char * string,int len)282 SBCommandReturnObject::PutCString(const char* string, int len)
283 {
284     if (m_opaque_ap.get())
285     {
286         if (len == 0 || string == NULL || *string == 0)
287         {
288             return;
289         }
290         else if (len > 0)
291         {
292             std::string buffer(string, len);
293             m_opaque_ap->AppendMessage(buffer.c_str());
294         }
295         else
296             m_opaque_ap->AppendMessage(string);
297     }
298 }
299 
300 const char *
GetOutput(bool only_if_no_immediate)301 SBCommandReturnObject::GetOutput (bool only_if_no_immediate)
302 {
303     if (!m_opaque_ap.get())
304         return NULL;
305     if (only_if_no_immediate == false || m_opaque_ap->GetImmediateOutputStream().get() == NULL)
306         return GetOutput();
307     return NULL;
308 }
309 
310 const char *
GetError(bool only_if_no_immediate)311 SBCommandReturnObject::GetError (bool only_if_no_immediate)
312 {
313     if (!m_opaque_ap.get())
314         return NULL;
315     if (only_if_no_immediate == false || m_opaque_ap->GetImmediateErrorStream().get() == NULL)
316         return GetError();
317     return NULL;
318 }
319 
320 size_t
Printf(const char * format,...)321 SBCommandReturnObject::Printf(const char* format, ...)
322 {
323     if (m_opaque_ap.get())
324     {
325         va_list args;
326         va_start (args, format);
327         size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
328         va_end (args);
329         return result;
330     }
331     return 0;
332 }
333 
334 void
SetError(lldb::SBError & error,const char * fallback_error_cstr)335 SBCommandReturnObject::SetError (lldb::SBError &error, const char *fallback_error_cstr)
336 {
337     if (m_opaque_ap.get())
338     {
339         if (error.IsValid())
340             m_opaque_ap->SetError(error.ref(), fallback_error_cstr);
341         else if (fallback_error_cstr)
342             m_opaque_ap->SetError(Error(), fallback_error_cstr);
343     }
344 }
345 
346 void
SetError(const char * error_cstr)347 SBCommandReturnObject::SetError (const char *error_cstr)
348 {
349     if (m_opaque_ap.get() && error_cstr)
350         m_opaque_ap->SetError(error_cstr);
351 }
352 
353