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