1 //===-- SBDebugger.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 // C Includes 11 // C++ Includes 12 // Other libraries and framework includes 13 // Project includes 14 #include "lldb/API/SBDebugger.h" 15 16 #include "lldb/lldb-private.h" 17 18 #include "lldb/API/SBBroadcaster.h" 19 #include "lldb/API/SBCommandInterpreter.h" 20 #include "lldb/API/SBCommandReturnObject.h" 21 #include "lldb/API/SBError.h" 22 #include "lldb/API/SBEvent.h" 23 #include "lldb/API/SBFrame.h" 24 #include "lldb/API/SBListener.h" 25 #include "lldb/API/SBProcess.h" 26 #include "lldb/API/SBSourceManager.h" 27 #include "lldb/API/SBStream.h" 28 #include "lldb/API/SBStringList.h" 29 #include "lldb/API/SBStructuredData.h" 30 #include "lldb/API/SBTarget.h" 31 #include "lldb/API/SBThread.h" 32 #include "lldb/API/SBTypeCategory.h" 33 #include "lldb/API/SBTypeFilter.h" 34 #include "lldb/API/SBTypeFormat.h" 35 #include "lldb/API/SBTypeNameSpecifier.h" 36 #include "lldb/API/SBTypeSummary.h" 37 #include "lldb/API/SBTypeSynthetic.h" 38 #include "lldb/API/SystemInitializerFull.h" 39 40 #include "lldb/Core/Debugger.h" 41 #include "lldb/Core/PluginManager.h" 42 #include "lldb/Core/State.h" 43 #include "lldb/Core/StreamFile.h" 44 #include "lldb/Core/StructuredDataImpl.h" 45 #include "lldb/DataFormatters/DataVisualization.h" 46 #include "lldb/Initialization/SystemLifetimeManager.h" 47 #include "lldb/Interpreter/Args.h" 48 #include "lldb/Interpreter/CommandInterpreter.h" 49 #include "lldb/Interpreter/OptionGroupPlatform.h" 50 #include "lldb/Target/Process.h" 51 #include "lldb/Target/TargetList.h" 52 53 #include "llvm/ADT/STLExtras.h" 54 #include "llvm/ADT/StringRef.h" 55 #include "llvm/Support/DynamicLibrary.h" 56 #include "llvm/Support/ManagedStatic.h" 57 58 using namespace lldb; 59 using namespace lldb_private; 60 61 static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp, 62 const FileSpec &spec, 63 Status &error) { 64 llvm::sys::DynamicLibrary dynlib = 65 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str()); 66 if (dynlib.isValid()) { 67 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger); 68 69 lldb::SBDebugger debugger_sb(debugger_sp); 70 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger) 71 // function. 72 // TODO: mangle this differently for your system - on OSX, the first 73 // underscore needs to be removed and the second one stays 74 LLDBCommandPluginInit init_func = 75 (LLDBCommandPluginInit)dynlib.getAddressOfSymbol( 76 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE"); 77 if (init_func) { 78 if (init_func(debugger_sb)) 79 return dynlib; 80 else 81 error.SetErrorString("plug-in refused to load " 82 "(lldb::PluginInitialize(lldb::SBDebugger) " 83 "returned false)"); 84 } else { 85 error.SetErrorString("plug-in is missing the required initialization: " 86 "lldb::PluginInitialize(lldb::SBDebugger)"); 87 } 88 } else { 89 if (spec.Exists()) 90 error.SetErrorString("this file does not represent a loadable dylib"); 91 else 92 error.SetErrorString("no such file"); 93 } 94 return llvm::sys::DynamicLibrary(); 95 } 96 97 static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime; 98 99 SBError SBInputReader::Initialize( 100 lldb::SBDebugger &sb_debugger, 101 unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction, 102 char const *, unsigned long), 103 void *, lldb::InputReaderGranularity, char const *, char const *, bool) { 104 return SBError(); 105 } 106 107 void SBInputReader::SetIsDone(bool) {} 108 109 bool SBInputReader::IsActive() const { return false; } 110 111 SBDebugger::SBDebugger() = default; 112 113 SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp) 114 : m_opaque_sp(debugger_sp) {} 115 116 SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {} 117 118 SBDebugger::~SBDebugger() = default; 119 120 SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) { 121 if (this != &rhs) { 122 m_opaque_sp = rhs.m_opaque_sp; 123 } 124 return *this; 125 } 126 127 void SBDebugger::Initialize() { 128 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 129 130 if (log) 131 log->Printf("SBDebugger::Initialize ()"); 132 133 g_debugger_lifetime->Initialize(llvm::make_unique<SystemInitializerFull>(), 134 LoadPlugin); 135 } 136 137 void SBDebugger::Terminate() { g_debugger_lifetime->Terminate(); } 138 139 void SBDebugger::Clear() { 140 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 141 142 if (log) 143 log->Printf("SBDebugger(%p)::Clear ()", 144 static_cast<void *>(m_opaque_sp.get())); 145 146 if (m_opaque_sp) 147 m_opaque_sp->ClearIOHandlers(); 148 149 m_opaque_sp.reset(); 150 } 151 152 SBDebugger SBDebugger::Create() { 153 return SBDebugger::Create(false, nullptr, nullptr); 154 } 155 156 SBDebugger SBDebugger::Create(bool source_init_files) { 157 return SBDebugger::Create(source_init_files, nullptr, nullptr); 158 } 159 160 SBDebugger SBDebugger::Create(bool source_init_files, 161 lldb::LogOutputCallback callback, void *baton) 162 163 { 164 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 165 166 SBDebugger debugger; 167 168 // Currently we have issues if this function is called simultaneously on two 169 // different 170 // threads. The issues mainly revolve around the fact that the 171 // lldb_private::FormatManager 172 // uses global collections and having two threads parsing the .lldbinit files 173 // can cause 174 // mayhem. So to get around this for now we need to use a mutex to prevent bad 175 // things 176 // from happening. 177 static std::recursive_mutex g_mutex; 178 std::lock_guard<std::recursive_mutex> guard(g_mutex); 179 180 debugger.reset(Debugger::CreateInstance(callback, baton)); 181 182 if (log) { 183 SBStream sstr; 184 debugger.GetDescription(sstr); 185 log->Printf("SBDebugger::Create () => SBDebugger(%p): %s", 186 static_cast<void *>(debugger.m_opaque_sp.get()), 187 sstr.GetData()); 188 } 189 190 SBCommandInterpreter interp = debugger.GetCommandInterpreter(); 191 if (source_init_files) { 192 interp.get()->SkipLLDBInitFiles(false); 193 interp.get()->SkipAppInitFiles(false); 194 SBCommandReturnObject result; 195 interp.SourceInitFileInHomeDirectory(result); 196 } else { 197 interp.get()->SkipLLDBInitFiles(true); 198 interp.get()->SkipAppInitFiles(true); 199 } 200 return debugger; 201 } 202 203 void SBDebugger::Destroy(SBDebugger &debugger) { 204 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 205 206 if (log) { 207 SBStream sstr; 208 debugger.GetDescription(sstr); 209 log->Printf("SBDebugger::Destroy () => SBDebugger(%p): %s", 210 static_cast<void *>(debugger.m_opaque_sp.get()), 211 sstr.GetData()); 212 } 213 214 Debugger::Destroy(debugger.m_opaque_sp); 215 216 if (debugger.m_opaque_sp.get() != nullptr) 217 debugger.m_opaque_sp.reset(); 218 } 219 220 void SBDebugger::MemoryPressureDetected() { 221 // Since this function can be call asynchronously, we allow it to be 222 // non-mandatory. We have seen deadlocks with this function when called 223 // so we need to safeguard against this until we can determine what is 224 // causing the deadlocks. 225 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 226 227 const bool mandatory = false; 228 if (log) { 229 log->Printf("SBDebugger::MemoryPressureDetected (), mandatory = %d", 230 mandatory); 231 } 232 233 ModuleList::RemoveOrphanSharedModules(mandatory); 234 } 235 236 bool SBDebugger::IsValid() const { return m_opaque_sp.get() != nullptr; } 237 238 void SBDebugger::SetAsync(bool b) { 239 if (m_opaque_sp) 240 m_opaque_sp->SetAsyncExecution(b); 241 } 242 243 bool SBDebugger::GetAsync() { 244 return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false); 245 } 246 247 void SBDebugger::SkipLLDBInitFiles(bool b) { 248 if (m_opaque_sp) 249 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b); 250 } 251 252 void SBDebugger::SkipAppInitFiles(bool b) { 253 if (m_opaque_sp) 254 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b); 255 } 256 257 // Shouldn't really be settable after initialization as this could cause lots of 258 // problems; don't want users 259 // trying to switch modes in the middle of a debugging session. 260 void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) { 261 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 262 263 if (log) 264 log->Printf( 265 "SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)", 266 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh), 267 transfer_ownership); 268 269 if (m_opaque_sp) 270 m_opaque_sp->SetInputFileHandle(fh, transfer_ownership); 271 } 272 273 void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) { 274 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 275 276 if (log) 277 log->Printf( 278 "SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)", 279 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh), 280 transfer_ownership); 281 282 if (m_opaque_sp) 283 m_opaque_sp->SetOutputFileHandle(fh, transfer_ownership); 284 } 285 286 void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) { 287 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 288 289 if (log) 290 log->Printf( 291 "SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)", 292 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh), 293 transfer_ownership); 294 295 if (m_opaque_sp) 296 m_opaque_sp->SetErrorFileHandle(fh, transfer_ownership); 297 } 298 299 FILE *SBDebugger::GetInputFileHandle() { 300 if (m_opaque_sp) { 301 StreamFileSP stream_file_sp(m_opaque_sp->GetInputFile()); 302 if (stream_file_sp) 303 return stream_file_sp->GetFile().GetStream(); 304 } 305 return nullptr; 306 } 307 308 FILE *SBDebugger::GetOutputFileHandle() { 309 if (m_opaque_sp) { 310 StreamFileSP stream_file_sp(m_opaque_sp->GetOutputFile()); 311 if (stream_file_sp) 312 return stream_file_sp->GetFile().GetStream(); 313 } 314 return nullptr; 315 } 316 317 FILE *SBDebugger::GetErrorFileHandle() { 318 if (m_opaque_sp) { 319 StreamFileSP stream_file_sp(m_opaque_sp->GetErrorFile()); 320 if (stream_file_sp) 321 return stream_file_sp->GetFile().GetStream(); 322 } 323 return nullptr; 324 } 325 326 void SBDebugger::SaveInputTerminalState() { 327 if (m_opaque_sp) 328 m_opaque_sp->SaveInputTerminalState(); 329 } 330 331 void SBDebugger::RestoreInputTerminalState() { 332 if (m_opaque_sp) 333 m_opaque_sp->RestoreInputTerminalState(); 334 } 335 SBCommandInterpreter SBDebugger::GetCommandInterpreter() { 336 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 337 338 SBCommandInterpreter sb_interpreter; 339 if (m_opaque_sp) 340 sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter()); 341 342 if (log) 343 log->Printf( 344 "SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)", 345 static_cast<void *>(m_opaque_sp.get()), 346 static_cast<void *>(sb_interpreter.get())); 347 348 return sb_interpreter; 349 } 350 351 void SBDebugger::HandleCommand(const char *command) { 352 if (m_opaque_sp) { 353 TargetSP target_sp(m_opaque_sp->GetSelectedTarget()); 354 std::unique_lock<std::recursive_mutex> lock; 355 if (target_sp) 356 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex()); 357 358 SBCommandInterpreter sb_interpreter(GetCommandInterpreter()); 359 SBCommandReturnObject result; 360 361 sb_interpreter.HandleCommand(command, result, false); 362 363 if (GetErrorFileHandle() != nullptr) 364 result.PutError(GetErrorFileHandle()); 365 if (GetOutputFileHandle() != nullptr) 366 result.PutOutput(GetOutputFileHandle()); 367 368 if (!m_opaque_sp->GetAsyncExecution()) { 369 SBProcess process(GetCommandInterpreter().GetProcess()); 370 ProcessSP process_sp(process.GetSP()); 371 if (process_sp) { 372 EventSP event_sp; 373 ListenerSP lldb_listener_sp = m_opaque_sp->GetListener(); 374 while (lldb_listener_sp->GetEventForBroadcaster( 375 process_sp.get(), event_sp, std::chrono::seconds(0))) { 376 SBEvent event(event_sp); 377 HandleProcessEvent(process, event, GetOutputFileHandle(), 378 GetErrorFileHandle()); 379 } 380 } 381 } 382 } 383 } 384 385 SBListener SBDebugger::GetListener() { 386 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 387 388 SBListener sb_listener; 389 if (m_opaque_sp) 390 sb_listener.reset(m_opaque_sp->GetListener()); 391 392 if (log) 393 log->Printf("SBDebugger(%p)::GetListener () => SBListener(%p)", 394 static_cast<void *>(m_opaque_sp.get()), 395 static_cast<void *>(sb_listener.get())); 396 397 return sb_listener; 398 } 399 400 void SBDebugger::HandleProcessEvent(const SBProcess &process, 401 const SBEvent &event, FILE *out, 402 FILE *err) { 403 if (!process.IsValid()) 404 return; 405 406 TargetSP target_sp(process.GetTarget().GetSP()); 407 if (!target_sp) 408 return; 409 410 const uint32_t event_type = event.GetType(); 411 char stdio_buffer[1024]; 412 size_t len; 413 414 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 415 416 if (event_type & 417 (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) { 418 // Drain stdout when we stop just in case we have any bytes 419 while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0) 420 if (out != nullptr) 421 ::fwrite(stdio_buffer, 1, len, out); 422 } 423 424 if (event_type & 425 (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) { 426 // Drain stderr when we stop just in case we have any bytes 427 while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0) 428 if (err != nullptr) 429 ::fwrite(stdio_buffer, 1, len, err); 430 } 431 432 if (event_type & Process::eBroadcastBitStateChanged) { 433 StateType event_state = SBProcess::GetStateFromEvent(event); 434 435 if (event_state == eStateInvalid) 436 return; 437 438 bool is_stopped = StateIsStoppedState(event_state); 439 if (!is_stopped) 440 process.ReportEventState(event, out); 441 } 442 } 443 444 SBSourceManager SBDebugger::GetSourceManager() { 445 SBSourceManager sb_source_manager(*this); 446 return sb_source_manager; 447 } 448 449 bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) { 450 if (arch_name && arch_name_len) { 451 ArchSpec default_arch = Target::GetDefaultArchitecture(); 452 453 if (default_arch.IsValid()) { 454 const std::string &triple_str = default_arch.GetTriple().str(); 455 if (!triple_str.empty()) 456 ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str()); 457 else 458 ::snprintf(arch_name, arch_name_len, "%s", 459 default_arch.GetArchitectureName()); 460 return true; 461 } 462 } 463 if (arch_name && arch_name_len) 464 arch_name[0] = '\0'; 465 return false; 466 } 467 468 bool SBDebugger::SetDefaultArchitecture(const char *arch_name) { 469 if (arch_name) { 470 ArchSpec arch(arch_name); 471 if (arch.IsValid()) { 472 Target::SetDefaultArchitecture(arch); 473 return true; 474 } 475 } 476 return false; 477 } 478 479 ScriptLanguage 480 SBDebugger::GetScriptingLanguage(const char *script_language_name) { 481 if (!script_language_name) return eScriptLanguageDefault; 482 return Args::StringToScriptLanguage(llvm::StringRef(script_language_name), 483 eScriptLanguageDefault, nullptr); 484 } 485 486 const char *SBDebugger::GetVersionString() { 487 return lldb_private::GetVersion(); 488 } 489 490 const char *SBDebugger::StateAsCString(StateType state) { 491 return lldb_private::StateAsCString(state); 492 } 493 494 bool SBDebugger::StateIsRunningState(StateType state) { 495 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 496 497 const bool result = lldb_private::StateIsRunningState(state); 498 if (log) 499 log->Printf("SBDebugger::StateIsRunningState (state=%s) => %i", 500 StateAsCString(state), result); 501 502 return result; 503 } 504 505 bool SBDebugger::StateIsStoppedState(StateType state) { 506 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 507 508 const bool result = lldb_private::StateIsStoppedState(state, false); 509 if (log) 510 log->Printf("SBDebugger::StateIsStoppedState (state=%s) => %i", 511 StateAsCString(state), result); 512 513 return result; 514 } 515 516 lldb::SBTarget SBDebugger::CreateTarget(const char *filename, 517 const char *target_triple, 518 const char *platform_name, 519 bool add_dependent_modules, 520 lldb::SBError &sb_error) { 521 SBTarget sb_target; 522 TargetSP target_sp; 523 if (m_opaque_sp) { 524 sb_error.Clear(); 525 OptionGroupPlatform platform_options(false); 526 platform_options.SetPlatformName(platform_name); 527 528 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget( 529 *m_opaque_sp, filename, target_triple, add_dependent_modules, 530 &platform_options, target_sp); 531 532 if (sb_error.Success()) 533 sb_target.SetSP(target_sp); 534 } else { 535 sb_error.SetErrorString("invalid debugger"); 536 } 537 538 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 539 if (log) 540 log->Printf("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, " 541 "platform_name=%s, add_dependent_modules=%u, error=%s) => " 542 "SBTarget(%p)", 543 static_cast<void *>(m_opaque_sp.get()), filename, target_triple, 544 platform_name, add_dependent_modules, sb_error.GetCString(), 545 static_cast<void *>(target_sp.get())); 546 547 return sb_target; 548 } 549 550 SBTarget 551 SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename, 552 const char *target_triple) { 553 SBTarget sb_target; 554 TargetSP target_sp; 555 if (m_opaque_sp) { 556 const bool add_dependent_modules = true; 557 Status error(m_opaque_sp->GetTargetList().CreateTarget( 558 *m_opaque_sp, filename, target_triple, add_dependent_modules, nullptr, 559 target_sp)); 560 sb_target.SetSP(target_sp); 561 } 562 563 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 564 if (log) 565 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple " 566 "(filename=\"%s\", triple=%s) => SBTarget(%p)", 567 static_cast<void *>(m_opaque_sp.get()), filename, target_triple, 568 static_cast<void *>(target_sp.get())); 569 570 return sb_target; 571 } 572 573 SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename, 574 const char *arch_cstr) { 575 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 576 577 SBTarget sb_target; 578 TargetSP target_sp; 579 if (m_opaque_sp) { 580 Status error; 581 const bool add_dependent_modules = true; 582 583 error = m_opaque_sp->GetTargetList().CreateTarget( 584 *m_opaque_sp, filename, arch_cstr, add_dependent_modules, nullptr, 585 target_sp); 586 587 if (error.Success()) { 588 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get()); 589 sb_target.SetSP(target_sp); 590 } 591 } 592 593 if (log) 594 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", " 595 "arch=%s) => SBTarget(%p)", 596 static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr, 597 static_cast<void *>(target_sp.get())); 598 599 return sb_target; 600 } 601 602 SBTarget SBDebugger::CreateTarget(const char *filename) { 603 SBTarget sb_target; 604 TargetSP target_sp; 605 if (m_opaque_sp) { 606 Status error; 607 const bool add_dependent_modules = true; 608 error = m_opaque_sp->GetTargetList().CreateTarget( 609 *m_opaque_sp, filename, "", add_dependent_modules, nullptr, target_sp); 610 611 if (error.Success()) { 612 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get()); 613 sb_target.SetSP(target_sp); 614 } 615 } 616 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 617 if (log) 618 log->Printf( 619 "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)", 620 static_cast<void *>(m_opaque_sp.get()), filename, 621 static_cast<void *>(target_sp.get())); 622 return sb_target; 623 } 624 625 bool SBDebugger::DeleteTarget(lldb::SBTarget &target) { 626 bool result = false; 627 if (m_opaque_sp) { 628 TargetSP target_sp(target.GetSP()); 629 if (target_sp) { 630 // No need to lock, the target list is thread safe 631 result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp); 632 target_sp->Destroy(); 633 target.Clear(); 634 const bool mandatory = true; 635 ModuleList::RemoveOrphanSharedModules(mandatory); 636 } 637 } 638 639 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 640 if (log) 641 log->Printf("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i", 642 static_cast<void *>(m_opaque_sp.get()), 643 static_cast<void *>(target.m_opaque_sp.get()), result); 644 645 return result; 646 } 647 648 SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) { 649 SBTarget sb_target; 650 if (m_opaque_sp) { 651 // No need to lock, the target list is thread safe 652 sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx)); 653 } 654 return sb_target; 655 } 656 657 uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) { 658 659 lldb::TargetSP target_sp = target.GetSP(); 660 if (!target_sp) 661 return UINT32_MAX; 662 663 if (!m_opaque_sp) 664 return UINT32_MAX; 665 666 return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP()); 667 } 668 669 SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) { 670 SBTarget sb_target; 671 if (m_opaque_sp) { 672 // No need to lock, the target list is thread safe 673 sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid)); 674 } 675 return sb_target; 676 } 677 678 SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename, 679 const char *arch_name) { 680 SBTarget sb_target; 681 if (m_opaque_sp && filename && filename[0]) { 682 // No need to lock, the target list is thread safe 683 ArchSpec arch(arch_name, 684 m_opaque_sp->GetPlatformList().GetSelectedPlatform().get()); 685 TargetSP target_sp( 686 m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture( 687 FileSpec(filename, false), arch_name ? &arch : nullptr)); 688 sb_target.SetSP(target_sp); 689 } 690 return sb_target; 691 } 692 693 SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) { 694 SBTarget sb_target; 695 if (m_opaque_sp) { 696 // No need to lock, the target list is thread safe 697 sb_target.SetSP( 698 m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get())); 699 } 700 return sb_target; 701 } 702 703 uint32_t SBDebugger::GetNumTargets() { 704 if (m_opaque_sp) { 705 // No need to lock, the target list is thread safe 706 return m_opaque_sp->GetTargetList().GetNumTargets(); 707 } 708 return 0; 709 } 710 711 SBTarget SBDebugger::GetSelectedTarget() { 712 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 713 714 SBTarget sb_target; 715 TargetSP target_sp; 716 if (m_opaque_sp) { 717 // No need to lock, the target list is thread safe 718 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget(); 719 sb_target.SetSP(target_sp); 720 } 721 722 if (log) { 723 SBStream sstr; 724 sb_target.GetDescription(sstr, eDescriptionLevelBrief); 725 log->Printf("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s", 726 static_cast<void *>(m_opaque_sp.get()), 727 static_cast<void *>(target_sp.get()), sstr.GetData()); 728 } 729 730 return sb_target; 731 } 732 733 void SBDebugger::SetSelectedTarget(SBTarget &sb_target) { 734 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 735 736 TargetSP target_sp(sb_target.GetSP()); 737 if (m_opaque_sp) { 738 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get()); 739 } 740 if (log) { 741 SBStream sstr; 742 sb_target.GetDescription(sstr, eDescriptionLevelBrief); 743 log->Printf("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s", 744 static_cast<void *>(m_opaque_sp.get()), 745 static_cast<void *>(target_sp.get()), sstr.GetData()); 746 } 747 } 748 749 SBPlatform SBDebugger::GetSelectedPlatform() { 750 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 751 752 SBPlatform sb_platform; 753 DebuggerSP debugger_sp(m_opaque_sp); 754 if (debugger_sp) { 755 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform()); 756 } 757 if (log) 758 log->Printf("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s", 759 static_cast<void *>(m_opaque_sp.get()), 760 static_cast<void *>(sb_platform.GetSP().get()), 761 sb_platform.GetName()); 762 return sb_platform; 763 } 764 765 void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) { 766 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 767 768 DebuggerSP debugger_sp(m_opaque_sp); 769 if (debugger_sp) { 770 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP()); 771 } 772 773 if (log) 774 log->Printf("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)", 775 static_cast<void *>(m_opaque_sp.get()), 776 static_cast<void *>(sb_platform.GetSP().get()), 777 sb_platform.GetName()); 778 } 779 780 uint32_t SBDebugger::GetNumPlatforms() { 781 if (m_opaque_sp) { 782 // No need to lock, the platform list is thread safe 783 return m_opaque_sp->GetPlatformList().GetSize(); 784 } 785 return 0; 786 } 787 788 SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) { 789 SBPlatform sb_platform; 790 if (m_opaque_sp) { 791 // No need to lock, the platform list is thread safe 792 sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx)); 793 } 794 return sb_platform; 795 } 796 797 uint32_t SBDebugger::GetNumAvailablePlatforms() { 798 uint32_t idx = 0; 799 while (true) { 800 if (!PluginManager::GetPlatformPluginNameAtIndex(idx)) { 801 break; 802 } 803 ++idx; 804 } 805 // +1 for the host platform, which should always appear first in the list. 806 return idx + 1; 807 } 808 809 SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) { 810 SBStructuredData data; 811 auto platform_dict = llvm::make_unique<StructuredData::Dictionary>(); 812 llvm::StringRef name_str("name"), desc_str("description"); 813 814 if (idx == 0) { 815 PlatformSP host_platform_sp(Platform::GetHostPlatform()); 816 platform_dict->AddStringItem( 817 name_str, host_platform_sp->GetPluginName().GetStringRef()); 818 platform_dict->AddStringItem( 819 desc_str, llvm::StringRef(host_platform_sp->GetDescription())); 820 } else if (idx > 0) { 821 const char *plugin_name = 822 PluginManager::GetPlatformPluginNameAtIndex(idx - 1); 823 if (!plugin_name) { 824 return data; 825 } 826 platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name)); 827 828 const char *plugin_desc = 829 PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1); 830 if (!plugin_desc) { 831 return data; 832 } 833 platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc)); 834 } 835 836 data.m_impl_up->SetObjectSP( 837 StructuredData::ObjectSP(platform_dict.release())); 838 return data; 839 } 840 841 void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) { 842 DispatchInput(data, data_len); 843 } 844 845 void SBDebugger::DispatchInput(const void *data, size_t data_len) { 846 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 847 // 848 // if (log) 849 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\", 850 // size_t=%" PRIu64 ")", 851 // m_opaque_sp.get(), 852 // (int) data_len, 853 // (const char *) data, 854 // (uint64_t)data_len); 855 // 856 // if (m_opaque_sp) 857 // m_opaque_sp->DispatchInput ((const char *) data, data_len); 858 } 859 860 void SBDebugger::DispatchInputInterrupt() { 861 if (m_opaque_sp) 862 m_opaque_sp->DispatchInputInterrupt(); 863 } 864 865 void SBDebugger::DispatchInputEndOfFile() { 866 if (m_opaque_sp) 867 m_opaque_sp->DispatchInputEndOfFile(); 868 } 869 870 void SBDebugger::PushInputReader(SBInputReader &reader) {} 871 872 void SBDebugger::RunCommandInterpreter(bool auto_handle_events, 873 bool spawn_thread) { 874 if (m_opaque_sp) { 875 CommandInterpreterRunOptions options; 876 877 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter( 878 auto_handle_events, spawn_thread, options); 879 } 880 } 881 882 void SBDebugger::RunCommandInterpreter(bool auto_handle_events, 883 bool spawn_thread, 884 SBCommandInterpreterRunOptions &options, 885 int &num_errors, bool &quit_requested, 886 bool &stopped_for_crash) 887 888 { 889 if (m_opaque_sp) { 890 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter(); 891 interp.RunCommandInterpreter(auto_handle_events, spawn_thread, 892 options.ref()); 893 num_errors = interp.GetNumErrors(); 894 quit_requested = interp.GetQuitRequested(); 895 stopped_for_crash = interp.GetStoppedForCrash(); 896 } 897 } 898 899 SBError SBDebugger::RunREPL(lldb::LanguageType language, 900 const char *repl_options) { 901 SBError error; 902 if (m_opaque_sp) 903 error.ref() = m_opaque_sp->RunREPL(language, repl_options); 904 else 905 error.SetErrorString("invalid debugger"); 906 return error; 907 } 908 909 void SBDebugger::reset(const DebuggerSP &debugger_sp) { 910 m_opaque_sp = debugger_sp; 911 } 912 913 Debugger *SBDebugger::get() const { return m_opaque_sp.get(); } 914 915 Debugger &SBDebugger::ref() const { 916 assert(m_opaque_sp.get()); 917 return *m_opaque_sp; 918 } 919 920 const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; } 921 922 SBDebugger SBDebugger::FindDebuggerWithID(int id) { 923 // No need to lock, the debugger list is thread safe 924 SBDebugger sb_debugger; 925 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id); 926 if (debugger_sp) 927 sb_debugger.reset(debugger_sp); 928 return sb_debugger; 929 } 930 931 const char *SBDebugger::GetInstanceName() { 932 return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr); 933 } 934 935 SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value, 936 const char *debugger_instance_name) { 937 SBError sb_error; 938 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName( 939 ConstString(debugger_instance_name))); 940 Status error; 941 if (debugger_sp) { 942 ExecutionContext exe_ctx( 943 debugger_sp->GetCommandInterpreter().GetExecutionContext()); 944 error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign, 945 var_name, value); 946 } else { 947 error.SetErrorStringWithFormat("invalid debugger instance name '%s'", 948 debugger_instance_name); 949 } 950 if (error.Fail()) 951 sb_error.SetError(error); 952 return sb_error; 953 } 954 955 SBStringList 956 SBDebugger::GetInternalVariableValue(const char *var_name, 957 const char *debugger_instance_name) { 958 SBStringList ret_value; 959 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName( 960 ConstString(debugger_instance_name))); 961 Status error; 962 if (debugger_sp) { 963 ExecutionContext exe_ctx( 964 debugger_sp->GetCommandInterpreter().GetExecutionContext()); 965 lldb::OptionValueSP value_sp( 966 debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error)); 967 if (value_sp) { 968 StreamString value_strm; 969 value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue); 970 const std::string &value_str = value_strm.GetString(); 971 if (!value_str.empty()) { 972 StringList string_list; 973 string_list.SplitIntoLines(value_str); 974 return SBStringList(&string_list); 975 } 976 } 977 } 978 return SBStringList(); 979 } 980 981 uint32_t SBDebugger::GetTerminalWidth() const { 982 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0); 983 } 984 985 void SBDebugger::SetTerminalWidth(uint32_t term_width) { 986 if (m_opaque_sp) 987 m_opaque_sp->SetTerminalWidth(term_width); 988 } 989 990 const char *SBDebugger::GetPrompt() const { 991 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 992 993 if (log) 994 log->Printf("SBDebugger(%p)::GetPrompt () => \"%s\"", 995 static_cast<void *>(m_opaque_sp.get()), 996 (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : "")); 997 998 return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString() 999 : nullptr); 1000 } 1001 1002 void SBDebugger::SetPrompt(const char *prompt) { 1003 if (m_opaque_sp) 1004 m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt)); 1005 } 1006 1007 ScriptLanguage SBDebugger::GetScriptLanguage() const { 1008 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone); 1009 } 1010 1011 void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) { 1012 if (m_opaque_sp) { 1013 m_opaque_sp->SetScriptLanguage(script_lang); 1014 } 1015 } 1016 1017 bool SBDebugger::SetUseExternalEditor(bool value) { 1018 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false); 1019 } 1020 1021 bool SBDebugger::GetUseExternalEditor() { 1022 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false); 1023 } 1024 1025 bool SBDebugger::SetUseColor(bool value) { 1026 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false); 1027 } 1028 1029 bool SBDebugger::GetUseColor() const { 1030 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false); 1031 } 1032 1033 bool SBDebugger::GetDescription(SBStream &description) { 1034 Stream &strm = description.ref(); 1035 1036 if (m_opaque_sp) { 1037 const char *name = m_opaque_sp->GetInstanceName().AsCString(); 1038 user_id_t id = m_opaque_sp->GetID(); 1039 strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id); 1040 } else 1041 strm.PutCString("No value"); 1042 1043 return true; 1044 } 1045 1046 user_id_t SBDebugger::GetID() { 1047 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID); 1048 } 1049 1050 SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) { 1051 SBError sb_error; 1052 if (m_opaque_sp) { 1053 if (platform_name_cstr && platform_name_cstr[0]) { 1054 ConstString platform_name(platform_name_cstr); 1055 PlatformSP platform_sp(Platform::Find(platform_name)); 1056 1057 if (platform_sp) { 1058 // Already have a platform with this name, just select it 1059 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp); 1060 } else { 1061 // We don't have a platform by this name yet, create one 1062 platform_sp = Platform::Create(platform_name, sb_error.ref()); 1063 if (platform_sp) { 1064 // We created the platform, now append and select it 1065 bool make_selected = true; 1066 m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected); 1067 } 1068 } 1069 } else { 1070 sb_error.ref().SetErrorString("invalid platform name"); 1071 } 1072 } else { 1073 sb_error.ref().SetErrorString("invalid debugger"); 1074 } 1075 return sb_error; 1076 } 1077 1078 bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) { 1079 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1080 if (m_opaque_sp) { 1081 PlatformSP platform_sp( 1082 m_opaque_sp->GetPlatformList().GetSelectedPlatform()); 1083 1084 if (platform_sp) { 1085 if (log && sysroot) 1086 log->Printf("SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")", sysroot); 1087 platform_sp->SetSDKRootDirectory(ConstString(sysroot)); 1088 return true; 1089 } 1090 } 1091 return false; 1092 } 1093 1094 bool SBDebugger::GetCloseInputOnEOF() const { 1095 return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false); 1096 } 1097 1098 void SBDebugger::SetCloseInputOnEOF(bool b) { 1099 if (m_opaque_sp) 1100 m_opaque_sp->SetCloseInputOnEOF(b); 1101 } 1102 1103 SBTypeCategory SBDebugger::GetCategory(const char *category_name) { 1104 if (!category_name || *category_name == 0) 1105 return SBTypeCategory(); 1106 1107 TypeCategoryImplSP category_sp; 1108 1109 if (DataVisualization::Categories::GetCategory(ConstString(category_name), 1110 category_sp, false)) 1111 return SBTypeCategory(category_sp); 1112 else 1113 return SBTypeCategory(); 1114 } 1115 1116 SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) { 1117 TypeCategoryImplSP category_sp; 1118 if (DataVisualization::Categories::GetCategory(lang_type, category_sp)) 1119 return SBTypeCategory(category_sp); 1120 else 1121 return SBTypeCategory(); 1122 } 1123 1124 SBTypeCategory SBDebugger::CreateCategory(const char *category_name) { 1125 if (!category_name || *category_name == 0) 1126 return SBTypeCategory(); 1127 1128 TypeCategoryImplSP category_sp; 1129 1130 if (DataVisualization::Categories::GetCategory(ConstString(category_name), 1131 category_sp, true)) 1132 return SBTypeCategory(category_sp); 1133 else 1134 return SBTypeCategory(); 1135 } 1136 1137 bool SBDebugger::DeleteCategory(const char *category_name) { 1138 if (!category_name || *category_name == 0) 1139 return false; 1140 1141 return DataVisualization::Categories::Delete(ConstString(category_name)); 1142 } 1143 1144 uint32_t SBDebugger::GetNumCategories() { 1145 return DataVisualization::Categories::GetCount(); 1146 } 1147 1148 SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) { 1149 return SBTypeCategory( 1150 DataVisualization::Categories::GetCategoryAtIndex(index)); 1151 } 1152 1153 SBTypeCategory SBDebugger::GetDefaultCategory() { 1154 return GetCategory("default"); 1155 } 1156 1157 SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) { 1158 SBTypeCategory default_category_sb = GetDefaultCategory(); 1159 if (default_category_sb.GetEnabled()) 1160 return default_category_sb.GetFormatForType(type_name); 1161 return SBTypeFormat(); 1162 } 1163 1164 #ifndef LLDB_DISABLE_PYTHON 1165 SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) { 1166 if (!type_name.IsValid()) 1167 return SBTypeSummary(); 1168 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP())); 1169 } 1170 #endif // LLDB_DISABLE_PYTHON 1171 1172 SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) { 1173 if (!type_name.IsValid()) 1174 return SBTypeFilter(); 1175 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP())); 1176 } 1177 1178 #ifndef LLDB_DISABLE_PYTHON 1179 SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) { 1180 if (!type_name.IsValid()) 1181 return SBTypeSynthetic(); 1182 return SBTypeSynthetic( 1183 DataVisualization::GetSyntheticForType(type_name.GetSP())); 1184 } 1185 #endif // LLDB_DISABLE_PYTHON 1186 1187 static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) { 1188 if (categories == nullptr) 1189 return {}; 1190 size_t len = 0; 1191 while (categories[len] != nullptr) 1192 ++len; 1193 return llvm::makeArrayRef(categories, len); 1194 } 1195 1196 bool SBDebugger::EnableLog(const char *channel, const char **categories) { 1197 if (m_opaque_sp) { 1198 uint32_t log_options = 1199 LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME; 1200 std::string error; 1201 llvm::raw_string_ostream error_stream(error); 1202 return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "", 1203 log_options, error_stream); 1204 } else 1205 return false; 1206 } 1207 1208 void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback, 1209 void *baton) { 1210 if (m_opaque_sp) { 1211 return m_opaque_sp->SetLoggingCallback(log_callback, baton); 1212 } 1213 } 1214