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