1 //===-- SBDebugger.cpp ------------------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "SBReproducerPrivate.h" 10 #include "SystemInitializerFull.h" 11 12 #include "lldb/API/SBDebugger.h" 13 14 #include "lldb/lldb-private.h" 15 16 #include "lldb/API/SBBroadcaster.h" 17 #include "lldb/API/SBCommandInterpreter.h" 18 #include "lldb/API/SBCommandReturnObject.h" 19 #include "lldb/API/SBError.h" 20 #include "lldb/API/SBEvent.h" 21 #include "lldb/API/SBFrame.h" 22 #include "lldb/API/SBListener.h" 23 #include "lldb/API/SBProcess.h" 24 #include "lldb/API/SBSourceManager.h" 25 #include "lldb/API/SBStream.h" 26 #include "lldb/API/SBStringList.h" 27 #include "lldb/API/SBStructuredData.h" 28 #include "lldb/API/SBTarget.h" 29 #include "lldb/API/SBThread.h" 30 #include "lldb/API/SBTypeCategory.h" 31 #include "lldb/API/SBTypeFilter.h" 32 #include "lldb/API/SBTypeFormat.h" 33 #include "lldb/API/SBTypeNameSpecifier.h" 34 #include "lldb/API/SBTypeSummary.h" 35 #include "lldb/API/SBTypeSynthetic.h" 36 37 #include "lldb/Core/Debugger.h" 38 #include "lldb/Core/PluginManager.h" 39 #include "lldb/Core/StreamFile.h" 40 #include "lldb/Core/StructuredDataImpl.h" 41 #include "lldb/DataFormatters/DataVisualization.h" 42 #include "lldb/Host/XML.h" 43 #include "lldb/Initialization/SystemLifetimeManager.h" 44 #include "lldb/Interpreter/CommandInterpreter.h" 45 #include "lldb/Interpreter/OptionArgParser.h" 46 #include "lldb/Interpreter/OptionGroupPlatform.h" 47 #include "lldb/Target/Process.h" 48 #include "lldb/Target/TargetList.h" 49 #include "lldb/Utility/Args.h" 50 #include "lldb/Utility/State.h" 51 52 #include "llvm/ADT/STLExtras.h" 53 #include "llvm/ADT/StringRef.h" 54 #include "llvm/Support/DynamicLibrary.h" 55 #include "llvm/Support/ManagedStatic.h" 56 57 using namespace lldb; 58 using namespace lldb_private; 59 60 /// Helper class for replaying commands through the reproducer. 61 class CommandLoader { 62 public: 63 CommandLoader(std::vector<std::string> files) : m_files(files) {} 64 65 static std::unique_ptr<CommandLoader> Create() { 66 repro::Loader *loader = repro::Reproducer::Instance().GetLoader(); 67 if (!loader) 68 return {}; 69 70 FileSpec file = loader->GetFile<repro::CommandInfo>(); 71 if (!file) 72 return {}; 73 74 auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath()); 75 if (auto err = error_or_file.getError()) 76 return {}; 77 78 std::vector<std::string> files; 79 llvm::yaml::Input yin((*error_or_file)->getBuffer()); 80 yin >> files; 81 82 if (auto err = yin.error()) 83 return {}; 84 85 return llvm::make_unique<CommandLoader>(std::move(files)); 86 } 87 88 FILE *GetNextFile() { 89 if (m_index >= m_files.size()) 90 return nullptr; 91 return FileSystem::Instance().Fopen(m_files[m_index++].c_str(), "r"); 92 } 93 94 private: 95 std::vector<std::string> m_files; 96 unsigned m_index = 0; 97 }; 98 99 static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp, 100 const FileSpec &spec, 101 Status &error) { 102 llvm::sys::DynamicLibrary dynlib = 103 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str()); 104 if (dynlib.isValid()) { 105 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger); 106 107 lldb::SBDebugger debugger_sb(debugger_sp); 108 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger) 109 // function. 110 // TODO: mangle this differently for your system - on OSX, the first 111 // underscore needs to be removed and the second one stays 112 LLDBCommandPluginInit init_func = 113 (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol( 114 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE"); 115 if (init_func) { 116 if (init_func(debugger_sb)) 117 return dynlib; 118 else 119 error.SetErrorString("plug-in refused to load " 120 "(lldb::PluginInitialize(lldb::SBDebugger) " 121 "returned false)"); 122 } else { 123 error.SetErrorString("plug-in is missing the required initialization: " 124 "lldb::PluginInitialize(lldb::SBDebugger)"); 125 } 126 } else { 127 if (FileSystem::Instance().Exists(spec)) 128 error.SetErrorString("this file does not represent a loadable dylib"); 129 else 130 error.SetErrorString("no such file"); 131 } 132 return llvm::sys::DynamicLibrary(); 133 } 134 135 static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime; 136 137 SBError SBInputReader::Initialize( 138 lldb::SBDebugger &sb_debugger, 139 unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction, 140 char const *, unsigned long), 141 void *, lldb::InputReaderGranularity, char const *, char const *, bool) { 142 return SBError(); 143 } 144 145 void SBInputReader::SetIsDone(bool b) { 146 LLDB_RECORD_METHOD(void, SBInputReader, SetIsDone, (bool), b); 147 } 148 149 bool SBInputReader::IsActive() const { 150 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBInputReader, IsActive); 151 152 return false; 153 } 154 155 SBDebugger::SBDebugger() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBDebugger); } 156 157 SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp) 158 : m_opaque_sp(debugger_sp) { 159 LLDB_RECORD_CONSTRUCTOR(SBDebugger, (const lldb::DebuggerSP &), debugger_sp); 160 } 161 162 SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) { 163 LLDB_RECORD_CONSTRUCTOR(SBDebugger, (const lldb::SBDebugger &), rhs); 164 } 165 166 SBDebugger::~SBDebugger() = default; 167 168 SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) { 169 LLDB_RECORD_METHOD(lldb::SBDebugger &, 170 SBDebugger, operator=,(const lldb::SBDebugger &), rhs); 171 172 if (this != &rhs) { 173 m_opaque_sp = rhs.m_opaque_sp; 174 } 175 return *this; 176 } 177 178 void SBDebugger::Initialize() { 179 LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, Initialize); 180 SBError ignored = SBDebugger::InitializeWithErrorHandling(); 181 } 182 183 lldb::SBError SBDebugger::InitializeWithErrorHandling() { 184 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBError, SBDebugger, 185 InitializeWithErrorHandling); 186 187 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 188 189 if (log) 190 log->Printf("SBDebugger::Initialize ()"); 191 192 SBError error; 193 if (auto e = g_debugger_lifetime->Initialize( 194 llvm::make_unique<SystemInitializerFull>(), LoadPlugin)) { 195 error.SetError(Status(std::move(e))); 196 } 197 return LLDB_RECORD_RESULT(error); 198 } 199 200 void SBDebugger::Terminate() { 201 LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, Terminate); 202 203 g_debugger_lifetime->Terminate(); 204 } 205 206 void SBDebugger::Clear() { 207 LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, Clear); 208 209 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 210 211 if (log) 212 log->Printf("SBDebugger(%p)::Clear ()", 213 static_cast<void *>(m_opaque_sp.get())); 214 215 if (m_opaque_sp) 216 m_opaque_sp->ClearIOHandlers(); 217 218 m_opaque_sp.reset(); 219 } 220 221 SBDebugger SBDebugger::Create() { 222 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBDebugger, SBDebugger, Create); 223 224 return LLDB_RECORD_RESULT(SBDebugger::Create(false, nullptr, nullptr)); 225 } 226 227 SBDebugger SBDebugger::Create(bool source_init_files) { 228 LLDB_RECORD_STATIC_METHOD(lldb::SBDebugger, SBDebugger, Create, (bool), 229 source_init_files); 230 231 return LLDB_RECORD_RESULT( 232 SBDebugger::Create(source_init_files, nullptr, nullptr)); 233 } 234 235 SBDebugger SBDebugger::Create(bool source_init_files, 236 lldb::LogOutputCallback callback, void *baton) 237 238 { 239 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 240 241 SBDebugger debugger; 242 243 // Currently we have issues if this function is called simultaneously on two 244 // different threads. The issues mainly revolve around the fact that the 245 // lldb_private::FormatManager uses global collections and having two threads 246 // parsing the .lldbinit files can cause mayhem. So to get around this for 247 // now we need to use a mutex to prevent bad things from happening. 248 static std::recursive_mutex g_mutex; 249 std::lock_guard<std::recursive_mutex> guard(g_mutex); 250 251 debugger.reset(Debugger::CreateInstance(callback, baton)); 252 253 if (log) { 254 SBStream sstr; 255 debugger.GetDescription(sstr); 256 log->Printf("SBDebugger::Create () => SBDebugger(%p): %s", 257 static_cast<void *>(debugger.m_opaque_sp.get()), 258 sstr.GetData()); 259 } 260 261 SBCommandInterpreter interp = debugger.GetCommandInterpreter(); 262 if (source_init_files) { 263 interp.get()->SkipLLDBInitFiles(false); 264 interp.get()->SkipAppInitFiles(false); 265 SBCommandReturnObject result; 266 interp.SourceInitFileInHomeDirectory(result); 267 } else { 268 interp.get()->SkipLLDBInitFiles(true); 269 interp.get()->SkipAppInitFiles(true); 270 } 271 return debugger; 272 } 273 274 void SBDebugger::Destroy(SBDebugger &debugger) { 275 LLDB_RECORD_STATIC_METHOD(void, SBDebugger, Destroy, (lldb::SBDebugger &), 276 debugger); 277 278 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 279 280 if (log) { 281 SBStream sstr; 282 debugger.GetDescription(sstr); 283 log->Printf("SBDebugger::Destroy () => SBDebugger(%p): %s", 284 static_cast<void *>(debugger.m_opaque_sp.get()), 285 sstr.GetData()); 286 } 287 288 Debugger::Destroy(debugger.m_opaque_sp); 289 290 if (debugger.m_opaque_sp.get() != nullptr) 291 debugger.m_opaque_sp.reset(); 292 } 293 294 void SBDebugger::MemoryPressureDetected() { 295 LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, MemoryPressureDetected); 296 297 // Since this function can be call asynchronously, we allow it to be non- 298 // mandatory. We have seen deadlocks with this function when called so we 299 // need to safeguard against this until we can determine what is causing the 300 // deadlocks. 301 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 302 303 const bool mandatory = false; 304 if (log) { 305 log->Printf("SBDebugger::MemoryPressureDetected (), mandatory = %d", 306 mandatory); 307 } 308 309 ModuleList::RemoveOrphanSharedModules(mandatory); 310 } 311 312 bool SBDebugger::IsValid() const { 313 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, IsValid); 314 315 return m_opaque_sp.get() != nullptr; 316 } 317 318 void SBDebugger::SetAsync(bool b) { 319 LLDB_RECORD_METHOD(void, SBDebugger, SetAsync, (bool), b); 320 321 if (m_opaque_sp) 322 m_opaque_sp->SetAsyncExecution(b); 323 } 324 325 bool SBDebugger::GetAsync() { 326 LLDB_RECORD_METHOD_NO_ARGS(bool, SBDebugger, GetAsync); 327 328 return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false); 329 } 330 331 void SBDebugger::SkipLLDBInitFiles(bool b) { 332 LLDB_RECORD_METHOD(void, SBDebugger, SkipLLDBInitFiles, (bool), b); 333 334 if (m_opaque_sp) 335 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b); 336 } 337 338 void SBDebugger::SkipAppInitFiles(bool b) { 339 LLDB_RECORD_METHOD(void, SBDebugger, SkipAppInitFiles, (bool), b); 340 341 if (m_opaque_sp) 342 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b); 343 } 344 345 // Shouldn't really be settable after initialization as this could cause lots 346 // of problems; don't want users trying to switch modes in the middle of a 347 // debugging session. 348 void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) { 349 LLDB_RECORD_METHOD(void, SBDebugger, SetInputFileHandle, (FILE *, bool), fh, 350 transfer_ownership); 351 352 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 353 354 if (log) 355 log->Printf( 356 "SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)", 357 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh), 358 transfer_ownership); 359 360 if (!m_opaque_sp) 361 return; 362 363 repro::DataRecorder *recorder = nullptr; 364 if (repro::Generator *g = repro::Reproducer::Instance().GetGenerator()) 365 recorder = g->GetOrCreate<repro::CommandProvider>().GetNewDataRecorder(); 366 367 static std::unique_ptr<CommandLoader> loader = CommandLoader::Create(); 368 if (loader) 369 fh = loader->GetNextFile(); 370 371 m_opaque_sp->SetInputFileHandle(fh, transfer_ownership, recorder); 372 } 373 374 void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) { 375 LLDB_RECORD_METHOD(void, SBDebugger, SetOutputFileHandle, (FILE *, bool), fh, 376 transfer_ownership); 377 378 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 379 380 if (log) 381 log->Printf( 382 "SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)", 383 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh), 384 transfer_ownership); 385 386 if (m_opaque_sp) 387 m_opaque_sp->SetOutputFileHandle(fh, transfer_ownership); 388 } 389 390 void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) { 391 LLDB_RECORD_METHOD(void, SBDebugger, SetErrorFileHandle, (FILE *, bool), fh, 392 transfer_ownership); 393 394 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 395 396 if (log) 397 log->Printf( 398 "SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)", 399 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh), 400 transfer_ownership); 401 402 if (m_opaque_sp) 403 m_opaque_sp->SetErrorFileHandle(fh, transfer_ownership); 404 } 405 406 FILE *SBDebugger::GetInputFileHandle() { 407 LLDB_RECORD_METHOD_NO_ARGS(FILE *, SBDebugger, GetInputFileHandle); 408 409 if (m_opaque_sp) { 410 StreamFileSP stream_file_sp(m_opaque_sp->GetInputFile()); 411 if (stream_file_sp) 412 return stream_file_sp->GetFile().GetStream(); 413 } 414 return nullptr; 415 } 416 417 FILE *SBDebugger::GetOutputFileHandle() { 418 LLDB_RECORD_METHOD_NO_ARGS(FILE *, SBDebugger, GetOutputFileHandle); 419 420 if (m_opaque_sp) { 421 StreamFileSP stream_file_sp(m_opaque_sp->GetOutputFile()); 422 if (stream_file_sp) 423 return stream_file_sp->GetFile().GetStream(); 424 } 425 return nullptr; 426 } 427 428 FILE *SBDebugger::GetErrorFileHandle() { 429 LLDB_RECORD_METHOD_NO_ARGS(FILE *, SBDebugger, GetErrorFileHandle); 430 431 if (m_opaque_sp) { 432 StreamFileSP stream_file_sp(m_opaque_sp->GetErrorFile()); 433 if (stream_file_sp) 434 return stream_file_sp->GetFile().GetStream(); 435 } 436 return nullptr; 437 } 438 439 void SBDebugger::SaveInputTerminalState() { 440 LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, SaveInputTerminalState); 441 442 if (m_opaque_sp) 443 m_opaque_sp->SaveInputTerminalState(); 444 } 445 446 void SBDebugger::RestoreInputTerminalState() { 447 LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, RestoreInputTerminalState); 448 449 if (m_opaque_sp) 450 m_opaque_sp->RestoreInputTerminalState(); 451 } 452 SBCommandInterpreter SBDebugger::GetCommandInterpreter() { 453 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBCommandInterpreter, SBDebugger, 454 GetCommandInterpreter); 455 456 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 457 458 SBCommandInterpreter sb_interpreter; 459 if (m_opaque_sp) 460 sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter()); 461 462 if (log) 463 log->Printf( 464 "SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)", 465 static_cast<void *>(m_opaque_sp.get()), 466 static_cast<void *>(sb_interpreter.get())); 467 468 return LLDB_RECORD_RESULT(sb_interpreter); 469 } 470 471 void SBDebugger::HandleCommand(const char *command) { 472 LLDB_RECORD_METHOD(void, SBDebugger, HandleCommand, (const char *), command); 473 474 if (m_opaque_sp) { 475 TargetSP target_sp(m_opaque_sp->GetSelectedTarget()); 476 std::unique_lock<std::recursive_mutex> lock; 477 if (target_sp) 478 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex()); 479 480 SBCommandInterpreter sb_interpreter(GetCommandInterpreter()); 481 SBCommandReturnObject result; 482 483 sb_interpreter.HandleCommand(command, result, false); 484 485 if (GetErrorFileHandle() != nullptr) 486 result.PutError(GetErrorFileHandle()); 487 if (GetOutputFileHandle() != nullptr) 488 result.PutOutput(GetOutputFileHandle()); 489 490 if (!m_opaque_sp->GetAsyncExecution()) { 491 SBProcess process(GetCommandInterpreter().GetProcess()); 492 ProcessSP process_sp(process.GetSP()); 493 if (process_sp) { 494 EventSP event_sp; 495 ListenerSP lldb_listener_sp = m_opaque_sp->GetListener(); 496 while (lldb_listener_sp->GetEventForBroadcaster( 497 process_sp.get(), event_sp, std::chrono::seconds(0))) { 498 SBEvent event(event_sp); 499 HandleProcessEvent(process, event, GetOutputFileHandle(), 500 GetErrorFileHandle()); 501 } 502 } 503 } 504 } 505 } 506 507 SBListener SBDebugger::GetListener() { 508 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBListener, SBDebugger, GetListener); 509 510 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 511 512 SBListener sb_listener; 513 if (m_opaque_sp) 514 sb_listener.reset(m_opaque_sp->GetListener()); 515 516 if (log) 517 log->Printf("SBDebugger(%p)::GetListener () => SBListener(%p)", 518 static_cast<void *>(m_opaque_sp.get()), 519 static_cast<void *>(sb_listener.get())); 520 521 return LLDB_RECORD_RESULT(sb_listener); 522 } 523 524 void SBDebugger::HandleProcessEvent(const SBProcess &process, 525 const SBEvent &event, FILE *out, 526 FILE *err) { 527 LLDB_RECORD_METHOD( 528 void, SBDebugger, HandleProcessEvent, 529 (const lldb::SBProcess &, const lldb::SBEvent &, FILE *, FILE *), process, 530 event, out, err); 531 532 if (!process.IsValid()) 533 return; 534 535 TargetSP target_sp(process.GetTarget().GetSP()); 536 if (!target_sp) 537 return; 538 539 const uint32_t event_type = event.GetType(); 540 char stdio_buffer[1024]; 541 size_t len; 542 543 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 544 545 if (event_type & 546 (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) { 547 // Drain stdout when we stop just in case we have any bytes 548 while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0) 549 if (out != nullptr) 550 ::fwrite(stdio_buffer, 1, len, out); 551 } 552 553 if (event_type & 554 (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) { 555 // Drain stderr when we stop just in case we have any bytes 556 while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0) 557 if (err != nullptr) 558 ::fwrite(stdio_buffer, 1, len, err); 559 } 560 561 if (event_type & Process::eBroadcastBitStateChanged) { 562 StateType event_state = SBProcess::GetStateFromEvent(event); 563 564 if (event_state == eStateInvalid) 565 return; 566 567 bool is_stopped = StateIsStoppedState(event_state); 568 if (!is_stopped) 569 process.ReportEventState(event, out); 570 } 571 } 572 573 SBSourceManager SBDebugger::GetSourceManager() { 574 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSourceManager, SBDebugger, 575 GetSourceManager); 576 577 SBSourceManager sb_source_manager(*this); 578 return LLDB_RECORD_RESULT(sb_source_manager); 579 } 580 581 bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) { 582 LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, GetDefaultArchitecture, 583 (char *, size_t), "", arch_name_len); 584 585 if (arch_name && arch_name_len) { 586 ArchSpec default_arch = Target::GetDefaultArchitecture(); 587 588 if (default_arch.IsValid()) { 589 const std::string &triple_str = default_arch.GetTriple().str(); 590 if (!triple_str.empty()) 591 ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str()); 592 else 593 ::snprintf(arch_name, arch_name_len, "%s", 594 default_arch.GetArchitectureName()); 595 return true; 596 } 597 } 598 if (arch_name && arch_name_len) 599 arch_name[0] = '\0'; 600 return false; 601 } 602 603 bool SBDebugger::SetDefaultArchitecture(const char *arch_name) { 604 LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, SetDefaultArchitecture, 605 (const char *), arch_name); 606 607 if (arch_name) { 608 ArchSpec arch(arch_name); 609 if (arch.IsValid()) { 610 Target::SetDefaultArchitecture(arch); 611 return true; 612 } 613 } 614 return false; 615 } 616 617 ScriptLanguage 618 SBDebugger::GetScriptingLanguage(const char *script_language_name) { 619 LLDB_RECORD_METHOD(lldb::ScriptLanguage, SBDebugger, GetScriptingLanguage, 620 (const char *), script_language_name); 621 622 if (!script_language_name) return eScriptLanguageDefault; 623 return OptionArgParser::ToScriptLanguage( 624 llvm::StringRef(script_language_name), eScriptLanguageDefault, nullptr); 625 } 626 627 const char *SBDebugger::GetVersionString() { 628 LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBDebugger, GetVersionString); 629 630 return lldb_private::GetVersion(); 631 } 632 633 const char *SBDebugger::StateAsCString(StateType state) { 634 LLDB_RECORD_STATIC_METHOD(const char *, SBDebugger, StateAsCString, 635 (lldb::StateType), state); 636 637 return lldb_private::StateAsCString(state); 638 } 639 640 static void AddBoolConfigEntry(StructuredData::Dictionary &dict, 641 llvm::StringRef name, bool value, 642 llvm::StringRef description) { 643 auto entry_up = llvm::make_unique<StructuredData::Dictionary>(); 644 entry_up->AddBooleanItem("value", value); 645 entry_up->AddStringItem("description", description); 646 dict.AddItem(name, std::move(entry_up)); 647 } 648 649 static void AddLLVMTargets(StructuredData::Dictionary &dict) { 650 auto array_up = llvm::make_unique<StructuredData::Array>(); 651 #define LLVM_TARGET(target) \ 652 array_up->AddItem(llvm::make_unique<StructuredData::String>(#target)); 653 #include "llvm/Config/Targets.def" 654 auto entry_up = llvm::make_unique<StructuredData::Dictionary>(); 655 entry_up->AddItem("value", std::move(array_up)); 656 entry_up->AddStringItem("description", "A list of configured LLVM targets."); 657 dict.AddItem("targets", std::move(entry_up)); 658 } 659 660 SBStructuredData SBDebugger::GetBuildConfiguration() { 661 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBStructuredData, SBDebugger, 662 GetBuildConfiguration); 663 664 auto config_up = llvm::make_unique<StructuredData::Dictionary>(); 665 AddBoolConfigEntry( 666 *config_up, "xml", XMLDocument::XMLEnabled(), 667 "A boolean value that indicates if XML support is enabled in LLDB"); 668 AddLLVMTargets(*config_up); 669 670 SBStructuredData data; 671 data.m_impl_up->SetObjectSP(std::move(config_up)); 672 return LLDB_RECORD_RESULT(data); 673 } 674 675 bool SBDebugger::StateIsRunningState(StateType state) { 676 LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, StateIsRunningState, 677 (lldb::StateType), state); 678 679 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 680 681 const bool result = lldb_private::StateIsRunningState(state); 682 if (log) 683 log->Printf("SBDebugger::StateIsRunningState (state=%s) => %i", 684 StateAsCString(state), result); 685 686 return result; 687 } 688 689 bool SBDebugger::StateIsStoppedState(StateType state) { 690 LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, StateIsStoppedState, 691 (lldb::StateType), state); 692 693 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 694 695 const bool result = lldb_private::StateIsStoppedState(state, false); 696 if (log) 697 log->Printf("SBDebugger::StateIsStoppedState (state=%s) => %i", 698 StateAsCString(state), result); 699 700 return result; 701 } 702 703 lldb::SBTarget SBDebugger::CreateTarget(const char *filename, 704 const char *target_triple, 705 const char *platform_name, 706 bool add_dependent_modules, 707 lldb::SBError &sb_error) { 708 LLDB_RECORD_METHOD( 709 lldb::SBTarget, SBDebugger, CreateTarget, 710 (const char *, const char *, const char *, bool, lldb::SBError &), 711 filename, target_triple, platform_name, add_dependent_modules, sb_error); 712 713 SBTarget sb_target; 714 TargetSP target_sp; 715 if (m_opaque_sp) { 716 sb_error.Clear(); 717 OptionGroupPlatform platform_options(false); 718 platform_options.SetPlatformName(platform_name); 719 720 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget( 721 *m_opaque_sp, filename, target_triple, 722 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, 723 &platform_options, target_sp); 724 725 if (sb_error.Success()) 726 sb_target.SetSP(target_sp); 727 } else { 728 sb_error.SetErrorString("invalid debugger"); 729 } 730 731 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 732 if (log) 733 log->Printf("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, " 734 "platform_name=%s, add_dependent_modules=%u, error=%s) => " 735 "SBTarget(%p)", 736 static_cast<void *>(m_opaque_sp.get()), filename, target_triple, 737 platform_name, add_dependent_modules, sb_error.GetCString(), 738 static_cast<void *>(target_sp.get())); 739 740 return LLDB_RECORD_RESULT(sb_target); 741 } 742 743 SBTarget 744 SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename, 745 const char *target_triple) { 746 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, 747 CreateTargetWithFileAndTargetTriple, 748 (const char *, const char *), filename, target_triple); 749 750 SBTarget sb_target; 751 TargetSP target_sp; 752 if (m_opaque_sp) { 753 const bool add_dependent_modules = true; 754 Status error(m_opaque_sp->GetTargetList().CreateTarget( 755 *m_opaque_sp, filename, target_triple, 756 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr, 757 target_sp)); 758 sb_target.SetSP(target_sp); 759 } 760 761 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 762 if (log) 763 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple " 764 "(filename=\"%s\", triple=%s) => SBTarget(%p)", 765 static_cast<void *>(m_opaque_sp.get()), filename, target_triple, 766 static_cast<void *>(target_sp.get())); 767 768 return LLDB_RECORD_RESULT(sb_target); 769 } 770 771 SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename, 772 const char *arch_cstr) { 773 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, CreateTargetWithFileAndArch, 774 (const char *, const char *), filename, arch_cstr); 775 776 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 777 778 SBTarget sb_target; 779 TargetSP target_sp; 780 if (m_opaque_sp) { 781 Status error; 782 const bool add_dependent_modules = true; 783 784 error = m_opaque_sp->GetTargetList().CreateTarget( 785 *m_opaque_sp, filename, arch_cstr, 786 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr, 787 target_sp); 788 789 if (error.Success()) { 790 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get()); 791 sb_target.SetSP(target_sp); 792 } 793 } 794 795 if (log) 796 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", " 797 "arch=%s) => SBTarget(%p)", 798 static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr, 799 static_cast<void *>(target_sp.get())); 800 801 return LLDB_RECORD_RESULT(sb_target); 802 } 803 804 SBTarget SBDebugger::CreateTarget(const char *filename) { 805 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, CreateTarget, (const char *), 806 filename); 807 808 SBTarget sb_target; 809 TargetSP target_sp; 810 if (m_opaque_sp) { 811 Status error; 812 const bool add_dependent_modules = true; 813 error = m_opaque_sp->GetTargetList().CreateTarget( 814 *m_opaque_sp, filename, "", 815 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr, 816 target_sp); 817 818 if (error.Success()) { 819 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get()); 820 sb_target.SetSP(target_sp); 821 } 822 } 823 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 824 if (log) 825 log->Printf( 826 "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)", 827 static_cast<void *>(m_opaque_sp.get()), filename, 828 static_cast<void *>(target_sp.get())); 829 return LLDB_RECORD_RESULT(sb_target); 830 } 831 832 SBTarget SBDebugger::GetDummyTarget() { 833 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTarget, SBDebugger, GetDummyTarget); 834 835 SBTarget sb_target; 836 if (m_opaque_sp) { 837 sb_target.SetSP(m_opaque_sp->GetDummyTarget()->shared_from_this()); 838 } 839 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 840 if (log) 841 log->Printf( 842 "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)", 843 static_cast<void *>(m_opaque_sp.get()), 844 static_cast<void *>(sb_target.GetSP().get())); 845 return LLDB_RECORD_RESULT(sb_target); 846 } 847 848 bool SBDebugger::DeleteTarget(lldb::SBTarget &target) { 849 LLDB_RECORD_METHOD(bool, SBDebugger, DeleteTarget, (lldb::SBTarget &), 850 target); 851 852 bool result = false; 853 if (m_opaque_sp) { 854 TargetSP target_sp(target.GetSP()); 855 if (target_sp) { 856 // No need to lock, the target list is thread safe 857 result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp); 858 target_sp->Destroy(); 859 target.Clear(); 860 const bool mandatory = true; 861 ModuleList::RemoveOrphanSharedModules(mandatory); 862 } 863 } 864 865 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 866 if (log) 867 log->Printf("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i", 868 static_cast<void *>(m_opaque_sp.get()), 869 static_cast<void *>(target.m_opaque_sp.get()), result); 870 871 return result; 872 } 873 874 SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) { 875 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, GetTargetAtIndex, (uint32_t), 876 idx); 877 878 SBTarget sb_target; 879 if (m_opaque_sp) { 880 // No need to lock, the target list is thread safe 881 sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx)); 882 } 883 return LLDB_RECORD_RESULT(sb_target); 884 } 885 886 uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) { 887 LLDB_RECORD_METHOD(uint32_t, SBDebugger, GetIndexOfTarget, (lldb::SBTarget), 888 target); 889 890 lldb::TargetSP target_sp = target.GetSP(); 891 if (!target_sp) 892 return UINT32_MAX; 893 894 if (!m_opaque_sp) 895 return UINT32_MAX; 896 897 return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP()); 898 } 899 900 SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) { 901 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithProcessID, 902 (lldb::pid_t), pid); 903 904 SBTarget sb_target; 905 if (m_opaque_sp) { 906 // No need to lock, the target list is thread safe 907 sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid)); 908 } 909 return LLDB_RECORD_RESULT(sb_target); 910 } 911 912 SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename, 913 const char *arch_name) { 914 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithFileAndArch, 915 (const char *, const char *), filename, arch_name); 916 917 SBTarget sb_target; 918 if (m_opaque_sp && filename && filename[0]) { 919 // No need to lock, the target list is thread safe 920 ArchSpec arch = Platform::GetAugmentedArchSpec( 921 m_opaque_sp->GetPlatformList().GetSelectedPlatform().get(), arch_name); 922 TargetSP target_sp( 923 m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture( 924 FileSpec(filename), arch_name ? &arch : nullptr)); 925 sb_target.SetSP(target_sp); 926 } 927 return LLDB_RECORD_RESULT(sb_target); 928 } 929 930 SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) { 931 SBTarget sb_target; 932 if (m_opaque_sp) { 933 // No need to lock, the target list is thread safe 934 sb_target.SetSP( 935 m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get())); 936 } 937 return sb_target; 938 } 939 940 uint32_t SBDebugger::GetNumTargets() { 941 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumTargets); 942 943 if (m_opaque_sp) { 944 // No need to lock, the target list is thread safe 945 return m_opaque_sp->GetTargetList().GetNumTargets(); 946 } 947 return 0; 948 } 949 950 SBTarget SBDebugger::GetSelectedTarget() { 951 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTarget, SBDebugger, GetSelectedTarget); 952 953 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 954 955 SBTarget sb_target; 956 TargetSP target_sp; 957 if (m_opaque_sp) { 958 // No need to lock, the target list is thread safe 959 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget(); 960 sb_target.SetSP(target_sp); 961 } 962 963 if (log) { 964 SBStream sstr; 965 sb_target.GetDescription(sstr, eDescriptionLevelBrief); 966 log->Printf("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s", 967 static_cast<void *>(m_opaque_sp.get()), 968 static_cast<void *>(target_sp.get()), sstr.GetData()); 969 } 970 971 return LLDB_RECORD_RESULT(sb_target); 972 } 973 974 void SBDebugger::SetSelectedTarget(SBTarget &sb_target) { 975 LLDB_RECORD_METHOD(void, SBDebugger, SetSelectedTarget, (lldb::SBTarget &), 976 sb_target); 977 978 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 979 980 TargetSP target_sp(sb_target.GetSP()); 981 if (m_opaque_sp) { 982 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get()); 983 } 984 if (log) { 985 SBStream sstr; 986 sb_target.GetDescription(sstr, eDescriptionLevelBrief); 987 log->Printf("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s", 988 static_cast<void *>(m_opaque_sp.get()), 989 static_cast<void *>(target_sp.get()), sstr.GetData()); 990 } 991 } 992 993 SBPlatform SBDebugger::GetSelectedPlatform() { 994 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBPlatform, SBDebugger, GetSelectedPlatform); 995 996 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 997 998 SBPlatform sb_platform; 999 DebuggerSP debugger_sp(m_opaque_sp); 1000 if (debugger_sp) { 1001 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform()); 1002 } 1003 if (log) 1004 log->Printf("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s", 1005 static_cast<void *>(m_opaque_sp.get()), 1006 static_cast<void *>(sb_platform.GetSP().get()), 1007 sb_platform.GetName()); 1008 return LLDB_RECORD_RESULT(sb_platform); 1009 } 1010 1011 void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) { 1012 LLDB_RECORD_METHOD(void, SBDebugger, SetSelectedPlatform, 1013 (lldb::SBPlatform &), sb_platform); 1014 1015 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1016 1017 DebuggerSP debugger_sp(m_opaque_sp); 1018 if (debugger_sp) { 1019 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP()); 1020 } 1021 1022 if (log) 1023 log->Printf("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)", 1024 static_cast<void *>(m_opaque_sp.get()), 1025 static_cast<void *>(sb_platform.GetSP().get()), 1026 sb_platform.GetName()); 1027 } 1028 1029 uint32_t SBDebugger::GetNumPlatforms() { 1030 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumPlatforms); 1031 1032 if (m_opaque_sp) { 1033 // No need to lock, the platform list is thread safe 1034 return m_opaque_sp->GetPlatformList().GetSize(); 1035 } 1036 return 0; 1037 } 1038 1039 SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) { 1040 LLDB_RECORD_METHOD(lldb::SBPlatform, SBDebugger, GetPlatformAtIndex, 1041 (uint32_t), idx); 1042 1043 SBPlatform sb_platform; 1044 if (m_opaque_sp) { 1045 // No need to lock, the platform list is thread safe 1046 sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx)); 1047 } 1048 return LLDB_RECORD_RESULT(sb_platform); 1049 } 1050 1051 uint32_t SBDebugger::GetNumAvailablePlatforms() { 1052 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumAvailablePlatforms); 1053 1054 uint32_t idx = 0; 1055 while (true) { 1056 if (!PluginManager::GetPlatformPluginNameAtIndex(idx)) { 1057 break; 1058 } 1059 ++idx; 1060 } 1061 // +1 for the host platform, which should always appear first in the list. 1062 return idx + 1; 1063 } 1064 1065 SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) { 1066 LLDB_RECORD_METHOD(lldb::SBStructuredData, SBDebugger, 1067 GetAvailablePlatformInfoAtIndex, (uint32_t), idx); 1068 1069 SBStructuredData data; 1070 auto platform_dict = llvm::make_unique<StructuredData::Dictionary>(); 1071 llvm::StringRef name_str("name"), desc_str("description"); 1072 1073 if (idx == 0) { 1074 PlatformSP host_platform_sp(Platform::GetHostPlatform()); 1075 platform_dict->AddStringItem( 1076 name_str, host_platform_sp->GetPluginName().GetStringRef()); 1077 platform_dict->AddStringItem( 1078 desc_str, llvm::StringRef(host_platform_sp->GetDescription())); 1079 } else if (idx > 0) { 1080 const char *plugin_name = 1081 PluginManager::GetPlatformPluginNameAtIndex(idx - 1); 1082 if (!plugin_name) { 1083 return LLDB_RECORD_RESULT(data); 1084 } 1085 platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name)); 1086 1087 const char *plugin_desc = 1088 PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1); 1089 if (!plugin_desc) { 1090 return LLDB_RECORD_RESULT(data); 1091 } 1092 platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc)); 1093 } 1094 1095 data.m_impl_up->SetObjectSP( 1096 StructuredData::ObjectSP(platform_dict.release())); 1097 return LLDB_RECORD_RESULT(data); 1098 } 1099 1100 void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) { 1101 DispatchInput(data, data_len); 1102 } 1103 1104 void SBDebugger::DispatchInput(const void *data, size_t data_len) { 1105 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1106 // 1107 // if (log) 1108 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\", 1109 // size_t=%" PRIu64 ")", 1110 // m_opaque_sp.get(), 1111 // (int) data_len, 1112 // (const char *) data, 1113 // (uint64_t)data_len); 1114 // 1115 // if (m_opaque_sp) 1116 // m_opaque_sp->DispatchInput ((const char *) data, data_len); 1117 } 1118 1119 void SBDebugger::DispatchInputInterrupt() { 1120 LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, DispatchInputInterrupt); 1121 1122 if (m_opaque_sp) 1123 m_opaque_sp->DispatchInputInterrupt(); 1124 } 1125 1126 void SBDebugger::DispatchInputEndOfFile() { 1127 LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, DispatchInputEndOfFile); 1128 1129 if (m_opaque_sp) 1130 m_opaque_sp->DispatchInputEndOfFile(); 1131 } 1132 1133 void SBDebugger::PushInputReader(SBInputReader &reader) { 1134 LLDB_RECORD_METHOD(void, SBDebugger, PushInputReader, (lldb::SBInputReader &), 1135 reader); 1136 } 1137 1138 void SBDebugger::RunCommandInterpreter(bool auto_handle_events, 1139 bool spawn_thread) { 1140 LLDB_RECORD_METHOD(void, SBDebugger, RunCommandInterpreter, (bool, bool), 1141 auto_handle_events, spawn_thread); 1142 1143 if (m_opaque_sp) { 1144 CommandInterpreterRunOptions options; 1145 1146 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter( 1147 auto_handle_events, spawn_thread, options); 1148 } 1149 } 1150 1151 void SBDebugger::RunCommandInterpreter(bool auto_handle_events, 1152 bool spawn_thread, 1153 SBCommandInterpreterRunOptions &options, 1154 int &num_errors, bool &quit_requested, 1155 bool &stopped_for_crash) 1156 1157 { 1158 LLDB_RECORD_METHOD(void, SBDebugger, RunCommandInterpreter, 1159 (bool, bool, lldb::SBCommandInterpreterRunOptions &, int &, 1160 bool &, bool &), 1161 auto_handle_events, spawn_thread, options, num_errors, 1162 quit_requested, stopped_for_crash); 1163 1164 if (m_opaque_sp) { 1165 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter(); 1166 interp.RunCommandInterpreter(auto_handle_events, spawn_thread, 1167 options.ref()); 1168 num_errors = interp.GetNumErrors(); 1169 quit_requested = interp.GetQuitRequested(); 1170 stopped_for_crash = interp.GetStoppedForCrash(); 1171 } 1172 } 1173 1174 SBError SBDebugger::RunREPL(lldb::LanguageType language, 1175 const char *repl_options) { 1176 LLDB_RECORD_METHOD(lldb::SBError, SBDebugger, RunREPL, 1177 (lldb::LanguageType, const char *), language, 1178 repl_options); 1179 1180 SBError error; 1181 if (m_opaque_sp) 1182 error.ref() = m_opaque_sp->RunREPL(language, repl_options); 1183 else 1184 error.SetErrorString("invalid debugger"); 1185 return LLDB_RECORD_RESULT(error); 1186 } 1187 1188 void SBDebugger::reset(const DebuggerSP &debugger_sp) { 1189 m_opaque_sp = debugger_sp; 1190 } 1191 1192 Debugger *SBDebugger::get() const { return m_opaque_sp.get(); } 1193 1194 Debugger &SBDebugger::ref() const { 1195 assert(m_opaque_sp.get()); 1196 return *m_opaque_sp; 1197 } 1198 1199 const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; } 1200 1201 SBDebugger SBDebugger::FindDebuggerWithID(int id) { 1202 LLDB_RECORD_STATIC_METHOD(lldb::SBDebugger, SBDebugger, FindDebuggerWithID, 1203 (int), id); 1204 1205 // No need to lock, the debugger list is thread safe 1206 SBDebugger sb_debugger; 1207 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id); 1208 if (debugger_sp) 1209 sb_debugger.reset(debugger_sp); 1210 return LLDB_RECORD_RESULT(sb_debugger); 1211 } 1212 1213 const char *SBDebugger::GetInstanceName() { 1214 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBDebugger, GetInstanceName); 1215 1216 return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr); 1217 } 1218 1219 SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value, 1220 const char *debugger_instance_name) { 1221 LLDB_RECORD_STATIC_METHOD(lldb::SBError, SBDebugger, SetInternalVariable, 1222 (const char *, const char *, const char *), 1223 var_name, value, debugger_instance_name); 1224 1225 SBError sb_error; 1226 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName( 1227 ConstString(debugger_instance_name))); 1228 Status error; 1229 if (debugger_sp) { 1230 ExecutionContext exe_ctx( 1231 debugger_sp->GetCommandInterpreter().GetExecutionContext()); 1232 error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign, 1233 var_name, value); 1234 } else { 1235 error.SetErrorStringWithFormat("invalid debugger instance name '%s'", 1236 debugger_instance_name); 1237 } 1238 if (error.Fail()) 1239 sb_error.SetError(error); 1240 return LLDB_RECORD_RESULT(sb_error); 1241 } 1242 1243 SBStringList 1244 SBDebugger::GetInternalVariableValue(const char *var_name, 1245 const char *debugger_instance_name) { 1246 LLDB_RECORD_STATIC_METHOD( 1247 lldb::SBStringList, SBDebugger, GetInternalVariableValue, 1248 (const char *, const char *), var_name, debugger_instance_name); 1249 1250 SBStringList ret_value; 1251 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName( 1252 ConstString(debugger_instance_name))); 1253 Status error; 1254 if (debugger_sp) { 1255 ExecutionContext exe_ctx( 1256 debugger_sp->GetCommandInterpreter().GetExecutionContext()); 1257 lldb::OptionValueSP value_sp( 1258 debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error)); 1259 if (value_sp) { 1260 StreamString value_strm; 1261 value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue); 1262 const std::string &value_str = value_strm.GetString(); 1263 if (!value_str.empty()) { 1264 StringList string_list; 1265 string_list.SplitIntoLines(value_str); 1266 return LLDB_RECORD_RESULT(SBStringList(&string_list)); 1267 } 1268 } 1269 } 1270 return LLDB_RECORD_RESULT(SBStringList()); 1271 } 1272 1273 uint32_t SBDebugger::GetTerminalWidth() const { 1274 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBDebugger, GetTerminalWidth); 1275 1276 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0); 1277 } 1278 1279 void SBDebugger::SetTerminalWidth(uint32_t term_width) { 1280 LLDB_RECORD_METHOD(void, SBDebugger, SetTerminalWidth, (uint32_t), 1281 term_width); 1282 1283 if (m_opaque_sp) 1284 m_opaque_sp->SetTerminalWidth(term_width); 1285 } 1286 1287 const char *SBDebugger::GetPrompt() const { 1288 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBDebugger, GetPrompt); 1289 1290 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1291 1292 if (log) 1293 log->Printf("SBDebugger(%p)::GetPrompt () => \"%s\"", 1294 static_cast<void *>(m_opaque_sp.get()), 1295 (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : "")); 1296 1297 return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString() 1298 : nullptr); 1299 } 1300 1301 void SBDebugger::SetPrompt(const char *prompt) { 1302 LLDB_RECORD_METHOD(void, SBDebugger, SetPrompt, (const char *), prompt); 1303 1304 if (m_opaque_sp) 1305 m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt)); 1306 } 1307 1308 const char *SBDebugger::GetReproducerPath() const { 1309 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBDebugger, GetReproducerPath); 1310 1311 return (m_opaque_sp 1312 ? ConstString(m_opaque_sp->GetReproducerPath()).GetCString() 1313 : nullptr); 1314 } 1315 1316 ScriptLanguage SBDebugger::GetScriptLanguage() const { 1317 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::ScriptLanguage, SBDebugger, 1318 GetScriptLanguage); 1319 1320 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone); 1321 } 1322 1323 void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) { 1324 LLDB_RECORD_METHOD(void, SBDebugger, SetScriptLanguage, 1325 (lldb::ScriptLanguage), script_lang); 1326 1327 if (m_opaque_sp) { 1328 m_opaque_sp->SetScriptLanguage(script_lang); 1329 } 1330 } 1331 1332 bool SBDebugger::SetUseExternalEditor(bool value) { 1333 LLDB_RECORD_METHOD(bool, SBDebugger, SetUseExternalEditor, (bool), value); 1334 1335 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false); 1336 } 1337 1338 bool SBDebugger::GetUseExternalEditor() { 1339 LLDB_RECORD_METHOD_NO_ARGS(bool, SBDebugger, GetUseExternalEditor); 1340 1341 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false); 1342 } 1343 1344 bool SBDebugger::SetUseColor(bool value) { 1345 LLDB_RECORD_METHOD(bool, SBDebugger, SetUseColor, (bool), value); 1346 1347 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false); 1348 } 1349 1350 bool SBDebugger::GetUseColor() const { 1351 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, GetUseColor); 1352 1353 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false); 1354 } 1355 1356 bool SBDebugger::GetDescription(SBStream &description) { 1357 LLDB_RECORD_METHOD(bool, SBDebugger, GetDescription, (lldb::SBStream &), 1358 description); 1359 1360 Stream &strm = description.ref(); 1361 1362 if (m_opaque_sp) { 1363 const char *name = m_opaque_sp->GetInstanceName().AsCString(); 1364 user_id_t id = m_opaque_sp->GetID(); 1365 strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id); 1366 } else 1367 strm.PutCString("No value"); 1368 1369 return true; 1370 } 1371 1372 user_id_t SBDebugger::GetID() { 1373 LLDB_RECORD_METHOD_NO_ARGS(lldb::user_id_t, SBDebugger, GetID); 1374 1375 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID); 1376 } 1377 1378 SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) { 1379 LLDB_RECORD_METHOD(lldb::SBError, SBDebugger, SetCurrentPlatform, 1380 (const char *), platform_name_cstr); 1381 1382 SBError sb_error; 1383 if (m_opaque_sp) { 1384 if (platform_name_cstr && platform_name_cstr[0]) { 1385 ConstString platform_name(platform_name_cstr); 1386 PlatformSP platform_sp(Platform::Find(platform_name)); 1387 1388 if (platform_sp) { 1389 // Already have a platform with this name, just select it 1390 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp); 1391 } else { 1392 // We don't have a platform by this name yet, create one 1393 platform_sp = Platform::Create(platform_name, sb_error.ref()); 1394 if (platform_sp) { 1395 // We created the platform, now append and select it 1396 bool make_selected = true; 1397 m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected); 1398 } 1399 } 1400 } else { 1401 sb_error.ref().SetErrorString("invalid platform name"); 1402 } 1403 } else { 1404 sb_error.ref().SetErrorString("invalid debugger"); 1405 } 1406 return LLDB_RECORD_RESULT(sb_error); 1407 } 1408 1409 bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) { 1410 LLDB_RECORD_METHOD(bool, SBDebugger, SetCurrentPlatformSDKRoot, 1411 (const char *), sysroot); 1412 1413 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1414 if (m_opaque_sp) { 1415 PlatformSP platform_sp( 1416 m_opaque_sp->GetPlatformList().GetSelectedPlatform()); 1417 1418 if (platform_sp) { 1419 if (log && sysroot) 1420 log->Printf("SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")", sysroot); 1421 platform_sp->SetSDKRootDirectory(ConstString(sysroot)); 1422 return true; 1423 } 1424 } 1425 return false; 1426 } 1427 1428 bool SBDebugger::GetCloseInputOnEOF() const { 1429 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, GetCloseInputOnEOF); 1430 1431 return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false); 1432 } 1433 1434 void SBDebugger::SetCloseInputOnEOF(bool b) { 1435 LLDB_RECORD_METHOD(void, SBDebugger, SetCloseInputOnEOF, (bool), b); 1436 1437 if (m_opaque_sp) 1438 m_opaque_sp->SetCloseInputOnEOF(b); 1439 } 1440 1441 SBTypeCategory SBDebugger::GetCategory(const char *category_name) { 1442 LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory, 1443 (const char *), category_name); 1444 1445 if (!category_name || *category_name == 0) 1446 return LLDB_RECORD_RESULT(SBTypeCategory()); 1447 1448 TypeCategoryImplSP category_sp; 1449 1450 if (DataVisualization::Categories::GetCategory(ConstString(category_name), 1451 category_sp, false)) { 1452 return LLDB_RECORD_RESULT(SBTypeCategory(category_sp)); 1453 } else { 1454 return LLDB_RECORD_RESULT(SBTypeCategory()); 1455 } 1456 } 1457 1458 SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) { 1459 LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory, 1460 (lldb::LanguageType), lang_type); 1461 1462 TypeCategoryImplSP category_sp; 1463 if (DataVisualization::Categories::GetCategory(lang_type, category_sp)) { 1464 return LLDB_RECORD_RESULT(SBTypeCategory(category_sp)); 1465 } else { 1466 return LLDB_RECORD_RESULT(SBTypeCategory()); 1467 } 1468 } 1469 1470 SBTypeCategory SBDebugger::CreateCategory(const char *category_name) { 1471 LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, CreateCategory, 1472 (const char *), category_name); 1473 1474 if (!category_name || *category_name == 0) 1475 return LLDB_RECORD_RESULT(SBTypeCategory()); 1476 1477 TypeCategoryImplSP category_sp; 1478 1479 if (DataVisualization::Categories::GetCategory(ConstString(category_name), 1480 category_sp, true)) { 1481 return LLDB_RECORD_RESULT(SBTypeCategory(category_sp)); 1482 } else { 1483 return LLDB_RECORD_RESULT(SBTypeCategory()); 1484 } 1485 } 1486 1487 bool SBDebugger::DeleteCategory(const char *category_name) { 1488 LLDB_RECORD_METHOD(bool, SBDebugger, DeleteCategory, (const char *), 1489 category_name); 1490 1491 if (!category_name || *category_name == 0) 1492 return false; 1493 1494 return DataVisualization::Categories::Delete(ConstString(category_name)); 1495 } 1496 1497 uint32_t SBDebugger::GetNumCategories() { 1498 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumCategories); 1499 1500 return DataVisualization::Categories::GetCount(); 1501 } 1502 1503 SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) { 1504 LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategoryAtIndex, 1505 (uint32_t), index); 1506 1507 return LLDB_RECORD_RESULT( 1508 SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index))); 1509 } 1510 1511 SBTypeCategory SBDebugger::GetDefaultCategory() { 1512 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTypeCategory, SBDebugger, 1513 GetDefaultCategory); 1514 1515 return LLDB_RECORD_RESULT(GetCategory("default")); 1516 } 1517 1518 SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) { 1519 LLDB_RECORD_METHOD(lldb::SBTypeFormat, SBDebugger, GetFormatForType, 1520 (lldb::SBTypeNameSpecifier), type_name); 1521 1522 SBTypeCategory default_category_sb = GetDefaultCategory(); 1523 if (default_category_sb.GetEnabled()) 1524 return LLDB_RECORD_RESULT(default_category_sb.GetFormatForType(type_name)); 1525 return LLDB_RECORD_RESULT(SBTypeFormat()); 1526 } 1527 1528 #ifndef LLDB_DISABLE_PYTHON 1529 SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) { 1530 LLDB_RECORD_METHOD(lldb::SBTypeSummary, SBDebugger, GetSummaryForType, 1531 (lldb::SBTypeNameSpecifier), type_name); 1532 1533 if (!type_name.IsValid()) 1534 return LLDB_RECORD_RESULT(SBTypeSummary()); 1535 return LLDB_RECORD_RESULT( 1536 SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()))); 1537 } 1538 #endif // LLDB_DISABLE_PYTHON 1539 1540 SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) { 1541 LLDB_RECORD_METHOD(lldb::SBTypeFilter, SBDebugger, GetFilterForType, 1542 (lldb::SBTypeNameSpecifier), type_name); 1543 1544 if (!type_name.IsValid()) 1545 return LLDB_RECORD_RESULT(SBTypeFilter()); 1546 return LLDB_RECORD_RESULT( 1547 SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()))); 1548 } 1549 1550 #ifndef LLDB_DISABLE_PYTHON 1551 SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) { 1552 LLDB_RECORD_METHOD(lldb::SBTypeSynthetic, SBDebugger, GetSyntheticForType, 1553 (lldb::SBTypeNameSpecifier), type_name); 1554 1555 if (!type_name.IsValid()) 1556 return LLDB_RECORD_RESULT(SBTypeSynthetic()); 1557 return LLDB_RECORD_RESULT(SBTypeSynthetic( 1558 DataVisualization::GetSyntheticForType(type_name.GetSP()))); 1559 } 1560 #endif // LLDB_DISABLE_PYTHON 1561 1562 static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) { 1563 if (categories == nullptr) 1564 return {}; 1565 size_t len = 0; 1566 while (categories[len] != nullptr) 1567 ++len; 1568 return llvm::makeArrayRef(categories, len); 1569 } 1570 1571 bool SBDebugger::EnableLog(const char *channel, const char **categories) { 1572 LLDB_RECORD_METHOD(bool, SBDebugger, EnableLog, (const char *, const char **), 1573 channel, categories); 1574 1575 if (m_opaque_sp) { 1576 uint32_t log_options = 1577 LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME; 1578 std::string error; 1579 llvm::raw_string_ostream error_stream(error); 1580 return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "", 1581 log_options, error_stream); 1582 } else 1583 return false; 1584 } 1585 1586 void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback, 1587 void *baton) { 1588 if (m_opaque_sp) { 1589 return m_opaque_sp->SetLoggingCallback(log_callback, baton); 1590 } 1591 } 1592