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