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