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