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