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