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/lldb-python.h" 11 12 #include "lldb/API/SBDebugger.h" 13 14 #include "lldb/lldb-private.h" 15 16 #include "lldb/API/SBListener.h" 17 #include "lldb/API/SBBroadcaster.h" 18 #include "lldb/API/SBCommandInterpreter.h" 19 #include "lldb/API/SBCommandReturnObject.h" 20 #include "lldb/API/SBError.h" 21 #include "lldb/API/SBEvent.h" 22 #include "lldb/API/SBFrame.h" 23 #include "lldb/API/SBProcess.h" 24 #include "lldb/API/SBSourceManager.h" 25 #include "lldb/API/SBStream.h" 26 #include "lldb/API/SBStringList.h" 27 #include "lldb/API/SBTarget.h" 28 #include "lldb/API/SBThread.h" 29 #include "lldb/API/SBTypeCategory.h" 30 #include "lldb/API/SBTypeFormat.h" 31 #include "lldb/API/SBTypeFilter.h" 32 #include "lldb/API/SBTypeNameSpecifier.h" 33 #include "lldb/API/SBTypeSummary.h" 34 #include "lldb/API/SBTypeSynthetic.h" 35 36 37 #include "lldb/Core/Debugger.h" 38 #include "lldb/Core/State.h" 39 #include "lldb/Core/StreamFile.h" 40 #include "lldb/DataFormatters/DataVisualization.h" 41 #include "lldb/Interpreter/Args.h" 42 #include "lldb/Interpreter/CommandInterpreter.h" 43 #include "lldb/Interpreter/OptionGroupPlatform.h" 44 #include "lldb/Target/Process.h" 45 #include "lldb/Target/TargetList.h" 46 47 #include "llvm/Support/DynamicLibrary.h" 48 49 using namespace lldb; 50 using namespace lldb_private; 51 52 53 SBInputReader::SBInputReader() 54 { 55 } 56 SBInputReader::~SBInputReader() 57 { 58 } 59 60 SBError 61 SBInputReader::Initialize(lldb::SBDebugger& sb_debugger, unsigned long (*)(void*, lldb::SBInputReader*, lldb::InputReaderAction, char const*, unsigned long), void*, lldb::InputReaderGranularity, char const*, char const*, bool) 62 { 63 return SBError(); 64 } 65 66 void 67 SBInputReader::SetIsDone(bool) 68 { 69 } 70 bool 71 SBInputReader::IsActive() const 72 { 73 return false; 74 } 75 76 static llvm::sys::DynamicLibrary 77 LoadPlugin (const lldb::DebuggerSP &debugger_sp, const FileSpec& spec, Error& error) 78 { 79 llvm::sys::DynamicLibrary dynlib = llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str()); 80 if (dynlib.isValid()) 81 { 82 typedef bool (*LLDBCommandPluginInit) (lldb::SBDebugger& debugger); 83 84 lldb::SBDebugger debugger_sb(debugger_sp); 85 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger) function. 86 // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays 87 LLDBCommandPluginInit init_func = (LLDBCommandPluginInit)dynlib.getAddressOfSymbol("_ZN4lldb16PluginInitializeENS_10SBDebuggerE"); 88 if (init_func) 89 { 90 if (init_func(debugger_sb)) 91 return dynlib; 92 else 93 error.SetErrorString("plug-in refused to load (lldb::PluginInitialize(lldb::SBDebugger) returned false)"); 94 } 95 else 96 { 97 error.SetErrorString("plug-in is missing the required initialization: lldb::PluginInitialize(lldb::SBDebugger)"); 98 } 99 } 100 else 101 { 102 if (spec.Exists()) 103 error.SetErrorString("this file does not represent a loadable dylib"); 104 else 105 error.SetErrorString("no such file"); 106 } 107 return llvm::sys::DynamicLibrary(); 108 } 109 110 void 111 SBDebugger::Initialize () 112 { 113 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 114 115 if (log) 116 log->Printf ("SBDebugger::Initialize ()"); 117 118 SBCommandInterpreter::InitializeSWIG (); 119 120 Debugger::Initialize(LoadPlugin); 121 } 122 123 void 124 SBDebugger::Terminate () 125 { 126 Debugger::Terminate(); 127 } 128 129 void 130 SBDebugger::Clear () 131 { 132 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 133 134 if (log) 135 log->Printf ("SBDebugger(%p)::Clear ()", 136 static_cast<void*>(m_opaque_sp.get())); 137 138 if (m_opaque_sp) 139 m_opaque_sp->ClearIOHandlers (); 140 141 m_opaque_sp.reset(); 142 } 143 144 SBDebugger 145 SBDebugger::Create() 146 { 147 return SBDebugger::Create(false, NULL, NULL); 148 } 149 150 SBDebugger 151 SBDebugger::Create(bool source_init_files) 152 { 153 return SBDebugger::Create (source_init_files, NULL, NULL); 154 } 155 156 SBDebugger 157 SBDebugger::Create(bool source_init_files, 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 different 165 // threads. The issues mainly revolve around the fact that the lldb_private::FormatManager 166 // uses global collections and having two threads parsing the .lldbinit files can cause 167 // mayhem. So to get around this for now we need to use a mutex to prevent bad things 168 // from happening. 169 static Mutex g_mutex(Mutex::eMutexTypeRecursive); 170 Mutex::Locker locker(g_mutex); 171 172 debugger.reset(Debugger::CreateInstance(callback, baton)); 173 174 if (log) 175 { 176 SBStream sstr; 177 debugger.GetDescription (sstr); 178 log->Printf ("SBDebugger::Create () => SBDebugger(%p): %s", 179 static_cast<void*>(debugger.m_opaque_sp.get()), 180 sstr.GetData()); 181 } 182 183 SBCommandInterpreter interp = debugger.GetCommandInterpreter(); 184 if (source_init_files) 185 { 186 interp.get()->SkipLLDBInitFiles(false); 187 interp.get()->SkipAppInitFiles (false); 188 SBCommandReturnObject result; 189 interp.SourceInitFileInHomeDirectory(result); 190 } 191 else 192 { 193 interp.get()->SkipLLDBInitFiles(true); 194 interp.get()->SkipAppInitFiles (true); 195 } 196 return debugger; 197 } 198 199 void 200 SBDebugger::Destroy (SBDebugger &debugger) 201 { 202 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 203 204 if (log) 205 { 206 SBStream sstr; 207 debugger.GetDescription (sstr); 208 log->Printf ("SBDebugger::Destroy () => SBDebugger(%p): %s", 209 static_cast<void*>(debugger.m_opaque_sp.get()), 210 sstr.GetData()); 211 } 212 213 Debugger::Destroy (debugger.m_opaque_sp); 214 215 if (debugger.m_opaque_sp.get() != NULL) 216 debugger.m_opaque_sp.reset(); 217 } 218 219 void 220 SBDebugger::MemoryPressureDetected () 221 { 222 // Since this function can be call asynchronously, we allow it to be 223 // non-mandatory. We have seen deadlocks with this function when called 224 // so we need to safeguard against this until we can determine what is 225 // causing the deadlocks. 226 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 227 228 const bool mandatory = false; 229 if (log) 230 { 231 log->Printf ("SBDebugger::MemoryPressureDetected (), mandatory = %d", mandatory); 232 } 233 234 ModuleList::RemoveOrphanSharedModules(mandatory); 235 } 236 237 SBDebugger::SBDebugger () : 238 m_opaque_sp () 239 { 240 } 241 242 SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp) : 243 m_opaque_sp(debugger_sp) 244 { 245 } 246 247 SBDebugger::SBDebugger(const SBDebugger &rhs) : 248 m_opaque_sp (rhs.m_opaque_sp) 249 { 250 } 251 252 SBDebugger & 253 SBDebugger::operator = (const SBDebugger &rhs) 254 { 255 if (this != &rhs) 256 { 257 m_opaque_sp = rhs.m_opaque_sp; 258 } 259 return *this; 260 } 261 262 SBDebugger::~SBDebugger () 263 { 264 } 265 266 bool 267 SBDebugger::IsValid() const 268 { 269 return m_opaque_sp.get() != NULL; 270 } 271 272 273 void 274 SBDebugger::SetAsync (bool b) 275 { 276 if (m_opaque_sp) 277 m_opaque_sp->SetAsyncExecution(b); 278 } 279 280 bool 281 SBDebugger::GetAsync () 282 { 283 if (m_opaque_sp) 284 return m_opaque_sp->GetAsyncExecution(); 285 else 286 return false; 287 } 288 289 void 290 SBDebugger::SkipLLDBInitFiles (bool b) 291 { 292 if (m_opaque_sp) 293 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles (b); 294 } 295 296 void 297 SBDebugger::SkipAppInitFiles (bool b) 298 { 299 if (m_opaque_sp) 300 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles (b); 301 } 302 303 // Shouldn't really be settable after initialization as this could cause lots of problems; don't want users 304 // trying to switch modes in the middle of a debugging session. 305 void 306 SBDebugger::SetInputFileHandle (FILE *fh, bool transfer_ownership) 307 { 308 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 309 310 if (log) 311 log->Printf ("SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)", 312 static_cast<void*>(m_opaque_sp.get()), 313 static_cast<void*>(fh), transfer_ownership); 314 315 if (m_opaque_sp) 316 m_opaque_sp->SetInputFileHandle (fh, transfer_ownership); 317 } 318 319 void 320 SBDebugger::SetOutputFileHandle (FILE *fh, bool transfer_ownership) 321 { 322 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 323 324 325 if (log) 326 log->Printf ("SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)", 327 static_cast<void*>(m_opaque_sp.get()), 328 static_cast<void*>(fh), transfer_ownership); 329 330 if (m_opaque_sp) 331 m_opaque_sp->SetOutputFileHandle (fh, transfer_ownership); 332 } 333 334 void 335 SBDebugger::SetErrorFileHandle (FILE *fh, bool transfer_ownership) 336 { 337 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 338 339 340 if (log) 341 log->Printf ("SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)", 342 static_cast<void*>(m_opaque_sp.get()), 343 static_cast<void*>(fh), transfer_ownership); 344 345 if (m_opaque_sp) 346 m_opaque_sp->SetErrorFileHandle (fh, transfer_ownership); 347 } 348 349 FILE * 350 SBDebugger::GetInputFileHandle () 351 { 352 if (m_opaque_sp) 353 { 354 StreamFileSP stream_file_sp (m_opaque_sp->GetInputFile()); 355 if (stream_file_sp) 356 return stream_file_sp->GetFile().GetStream(); 357 } 358 return NULL; 359 } 360 361 FILE * 362 SBDebugger::GetOutputFileHandle () 363 { 364 if (m_opaque_sp) 365 { 366 StreamFileSP stream_file_sp (m_opaque_sp->GetOutputFile()); 367 if (stream_file_sp) 368 return stream_file_sp->GetFile().GetStream(); 369 } 370 return NULL; 371 } 372 373 FILE * 374 SBDebugger::GetErrorFileHandle () 375 { 376 if (m_opaque_sp) 377 if (m_opaque_sp) 378 { 379 StreamFileSP stream_file_sp (m_opaque_sp->GetErrorFile()); 380 if (stream_file_sp) 381 return stream_file_sp->GetFile().GetStream(); 382 } 383 return NULL; 384 } 385 386 void 387 SBDebugger::SaveInputTerminalState() 388 { 389 if (m_opaque_sp) 390 m_opaque_sp->SaveInputTerminalState(); 391 } 392 393 void 394 SBDebugger::RestoreInputTerminalState() 395 { 396 if (m_opaque_sp) 397 m_opaque_sp->RestoreInputTerminalState(); 398 399 } 400 SBCommandInterpreter 401 SBDebugger::GetCommandInterpreter () 402 { 403 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 404 405 SBCommandInterpreter sb_interpreter; 406 if (m_opaque_sp) 407 sb_interpreter.reset (&m_opaque_sp->GetCommandInterpreter()); 408 409 if (log) 410 log->Printf ("SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)", 411 static_cast<void*>(m_opaque_sp.get()), 412 static_cast<void*>(sb_interpreter.get())); 413 414 return sb_interpreter; 415 } 416 417 void 418 SBDebugger::HandleCommand (const char *command) 419 { 420 if (m_opaque_sp) 421 { 422 TargetSP target_sp (m_opaque_sp->GetSelectedTarget()); 423 Mutex::Locker api_locker; 424 if (target_sp) 425 api_locker.Lock(target_sp->GetAPIMutex()); 426 427 SBCommandInterpreter sb_interpreter(GetCommandInterpreter ()); 428 SBCommandReturnObject result; 429 430 sb_interpreter.HandleCommand (command, result, false); 431 432 if (GetErrorFileHandle() != NULL) 433 result.PutError (GetErrorFileHandle()); 434 if (GetOutputFileHandle() != NULL) 435 result.PutOutput (GetOutputFileHandle()); 436 437 if (m_opaque_sp->GetAsyncExecution() == false) 438 { 439 SBProcess process(GetCommandInterpreter().GetProcess ()); 440 ProcessSP process_sp (process.GetSP()); 441 if (process_sp) 442 { 443 EventSP event_sp; 444 Listener &lldb_listener = m_opaque_sp->GetListener(); 445 while (lldb_listener.GetNextEventForBroadcaster (process_sp.get(), event_sp)) 446 { 447 SBEvent event(event_sp); 448 HandleProcessEvent (process, event, GetOutputFileHandle(), GetErrorFileHandle()); 449 } 450 } 451 } 452 } 453 } 454 455 SBListener 456 SBDebugger::GetListener () 457 { 458 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 459 460 SBListener sb_listener; 461 if (m_opaque_sp) 462 sb_listener.reset(&m_opaque_sp->GetListener(), false); 463 464 if (log) 465 log->Printf ("SBDebugger(%p)::GetListener () => SBListener(%p)", 466 static_cast<void*>(m_opaque_sp.get()), 467 static_cast<void*>(sb_listener.get())); 468 469 return sb_listener; 470 } 471 472 void 473 SBDebugger::HandleProcessEvent (const SBProcess &process, const SBEvent &event, FILE *out, FILE *err) 474 { 475 if (!process.IsValid()) 476 return; 477 478 TargetSP target_sp (process.GetTarget().GetSP()); 479 if (!target_sp) 480 return; 481 482 const uint32_t event_type = event.GetType(); 483 char stdio_buffer[1024]; 484 size_t len; 485 486 Mutex::Locker api_locker (target_sp->GetAPIMutex()); 487 488 if (event_type & (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) 489 { 490 // Drain stdout when we stop just in case we have any bytes 491 while ((len = process.GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0) 492 if (out != NULL) 493 ::fwrite (stdio_buffer, 1, len, out); 494 } 495 496 if (event_type & (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) 497 { 498 // Drain stderr when we stop just in case we have any bytes 499 while ((len = process.GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0) 500 if (err != NULL) 501 ::fwrite (stdio_buffer, 1, len, err); 502 } 503 504 if (event_type & Process::eBroadcastBitStateChanged) 505 { 506 StateType event_state = SBProcess::GetStateFromEvent (event); 507 508 if (event_state == eStateInvalid) 509 return; 510 511 bool is_stopped = StateIsStoppedState (event_state); 512 if (!is_stopped) 513 process.ReportEventState (event, out); 514 } 515 } 516 517 SBSourceManager 518 SBDebugger::GetSourceManager () 519 { 520 SBSourceManager sb_source_manager (*this); 521 return sb_source_manager; 522 } 523 524 525 bool 526 SBDebugger::GetDefaultArchitecture (char *arch_name, size_t arch_name_len) 527 { 528 if (arch_name && arch_name_len) 529 { 530 ArchSpec default_arch = Target::GetDefaultArchitecture (); 531 532 if (default_arch.IsValid()) 533 { 534 const std::string &triple_str = default_arch.GetTriple().str(); 535 if (!triple_str.empty()) 536 ::snprintf (arch_name, arch_name_len, "%s", triple_str.c_str()); 537 else 538 ::snprintf (arch_name, arch_name_len, "%s", default_arch.GetArchitectureName()); 539 return true; 540 } 541 } 542 if (arch_name && arch_name_len) 543 arch_name[0] = '\0'; 544 return false; 545 } 546 547 548 bool 549 SBDebugger::SetDefaultArchitecture (const char *arch_name) 550 { 551 if (arch_name) 552 { 553 ArchSpec arch (arch_name); 554 if (arch.IsValid()) 555 { 556 Target::SetDefaultArchitecture (arch); 557 return true; 558 } 559 } 560 return false; 561 } 562 563 ScriptLanguage 564 SBDebugger::GetScriptingLanguage (const char *script_language_name) 565 { 566 567 return Args::StringToScriptLanguage (script_language_name, 568 eScriptLanguageDefault, 569 NULL); 570 } 571 572 const char * 573 SBDebugger::GetVersionString () 574 { 575 return lldb_private::GetVersion(); 576 } 577 578 const char * 579 SBDebugger::StateAsCString (StateType state) 580 { 581 return lldb_private::StateAsCString (state); 582 } 583 584 bool 585 SBDebugger::StateIsRunningState (StateType state) 586 { 587 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 588 589 const bool result = lldb_private::StateIsRunningState (state); 590 if (log) 591 log->Printf ("SBDebugger::StateIsRunningState (state=%s) => %i", 592 StateAsCString (state), result); 593 594 return result; 595 } 596 597 bool 598 SBDebugger::StateIsStoppedState (StateType state) 599 { 600 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 601 602 const bool result = lldb_private::StateIsStoppedState (state, false); 603 if (log) 604 log->Printf ("SBDebugger::StateIsStoppedState (state=%s) => %i", 605 StateAsCString (state), result); 606 607 return result; 608 } 609 610 lldb::SBTarget 611 SBDebugger::CreateTarget (const char *filename, 612 const char *target_triple, 613 const char *platform_name, 614 bool add_dependent_modules, 615 lldb::SBError& sb_error) 616 { 617 SBTarget sb_target; 618 TargetSP target_sp; 619 if (m_opaque_sp) 620 { 621 sb_error.Clear(); 622 OptionGroupPlatform platform_options (false); 623 platform_options.SetPlatformName (platform_name); 624 625 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp, 626 filename, 627 target_triple, 628 add_dependent_modules, 629 &platform_options, 630 target_sp); 631 632 if (sb_error.Success()) 633 sb_target.SetSP (target_sp); 634 } 635 else 636 { 637 sb_error.SetErrorString("invalid target"); 638 } 639 640 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 641 if (log) 642 log->Printf ("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, platform_name=%s, add_dependent_modules=%u, error=%s) => SBTarget(%p)", 643 static_cast<void*>(m_opaque_sp.get()), filename, 644 target_triple, platform_name, add_dependent_modules, 645 sb_error.GetCString(), static_cast<void*>(target_sp.get())); 646 647 return sb_target; 648 } 649 650 SBTarget 651 SBDebugger::CreateTargetWithFileAndTargetTriple (const char *filename, 652 const char *target_triple) 653 { 654 SBTarget sb_target; 655 TargetSP target_sp; 656 if (m_opaque_sp) 657 { 658 const bool add_dependent_modules = true; 659 Error error (m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp, 660 filename, 661 target_triple, 662 add_dependent_modules, 663 NULL, 664 target_sp)); 665 sb_target.SetSP (target_sp); 666 } 667 668 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 669 if (log) 670 log->Printf ("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple (filename=\"%s\", triple=%s) => SBTarget(%p)", 671 static_cast<void*>(m_opaque_sp.get()), filename, 672 target_triple, static_cast<void*>(target_sp.get())); 673 674 return sb_target; 675 } 676 677 SBTarget 678 SBDebugger::CreateTargetWithFileAndArch (const char *filename, const char *arch_cstr) 679 { 680 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 681 682 SBTarget sb_target; 683 TargetSP target_sp; 684 if (m_opaque_sp) 685 { 686 Error error; 687 const bool add_dependent_modules = true; 688 689 error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp, 690 filename, 691 arch_cstr, 692 add_dependent_modules, 693 NULL, 694 target_sp); 695 696 if (error.Success()) 697 { 698 m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get()); 699 sb_target.SetSP (target_sp); 700 } 701 } 702 703 if (log) 704 log->Printf ("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", arch=%s) => SBTarget(%p)", 705 static_cast<void*>(m_opaque_sp.get()), filename, arch_cstr, 706 static_cast<void*>(target_sp.get())); 707 708 return sb_target; 709 } 710 711 SBTarget 712 SBDebugger::CreateTarget (const char *filename) 713 { 714 SBTarget sb_target; 715 TargetSP target_sp; 716 if (m_opaque_sp) 717 { 718 ArchSpec arch = Target::GetDefaultArchitecture (); 719 Error error; 720 const bool add_dependent_modules = true; 721 722 PlatformSP platform_sp(m_opaque_sp->GetPlatformList().GetSelectedPlatform()); 723 error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp, 724 filename, 725 arch, 726 add_dependent_modules, 727 platform_sp, 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