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