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 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(auto_handle_events, spawn_thread); 976 } 977 978 void 979 SBDebugger::reset (const DebuggerSP &debugger_sp) 980 { 981 m_opaque_sp = debugger_sp; 982 } 983 984 Debugger * 985 SBDebugger::get () const 986 { 987 return m_opaque_sp.get(); 988 } 989 990 Debugger & 991 SBDebugger::ref () const 992 { 993 assert (m_opaque_sp.get()); 994 return *m_opaque_sp; 995 } 996 997 const lldb::DebuggerSP & 998 SBDebugger::get_sp () const 999 { 1000 return m_opaque_sp; 1001 } 1002 1003 SBDebugger 1004 SBDebugger::FindDebuggerWithID (int id) 1005 { 1006 // No need to lock, the debugger list is thread safe 1007 SBDebugger sb_debugger; 1008 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID (id); 1009 if (debugger_sp) 1010 sb_debugger.reset (debugger_sp); 1011 return sb_debugger; 1012 } 1013 1014 const char * 1015 SBDebugger::GetInstanceName() 1016 { 1017 if (m_opaque_sp) 1018 return m_opaque_sp->GetInstanceName().AsCString(); 1019 else 1020 return NULL; 1021 } 1022 1023 SBError 1024 SBDebugger::SetInternalVariable (const char *var_name, const char *value, const char *debugger_instance_name) 1025 { 1026 SBError sb_error; 1027 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name))); 1028 Error error; 1029 if (debugger_sp) 1030 { 1031 ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext()); 1032 error = debugger_sp->SetPropertyValue (&exe_ctx, 1033 eVarSetOperationAssign, 1034 var_name, 1035 value); 1036 } 1037 else 1038 { 1039 error.SetErrorStringWithFormat ("invalid debugger instance name '%s'", debugger_instance_name); 1040 } 1041 if (error.Fail()) 1042 sb_error.SetError(error); 1043 return sb_error; 1044 } 1045 1046 SBStringList 1047 SBDebugger::GetInternalVariableValue (const char *var_name, const char *debugger_instance_name) 1048 { 1049 SBStringList ret_value; 1050 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name))); 1051 Error error; 1052 if (debugger_sp) 1053 { 1054 ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext()); 1055 lldb::OptionValueSP value_sp (debugger_sp->GetPropertyValue (&exe_ctx, 1056 var_name, 1057 false, 1058 error)); 1059 if (value_sp) 1060 { 1061 StreamString value_strm; 1062 value_sp->DumpValue (&exe_ctx, value_strm, OptionValue::eDumpOptionValue); 1063 const std::string &value_str = value_strm.GetString(); 1064 if (!value_str.empty()) 1065 { 1066 StringList string_list; 1067 string_list.SplitIntoLines(value_str); 1068 return SBStringList(&string_list); 1069 } 1070 } 1071 } 1072 return SBStringList(); 1073 } 1074 1075 uint32_t 1076 SBDebugger::GetTerminalWidth () const 1077 { 1078 if (m_opaque_sp) 1079 return m_opaque_sp->GetTerminalWidth (); 1080 return 0; 1081 } 1082 1083 void 1084 SBDebugger::SetTerminalWidth (uint32_t term_width) 1085 { 1086 if (m_opaque_sp) 1087 m_opaque_sp->SetTerminalWidth (term_width); 1088 } 1089 1090 const char * 1091 SBDebugger::GetPrompt() const 1092 { 1093 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1094 1095 if (log) 1096 log->Printf ("SBDebugger(%p)::GetPrompt () => \"%s\"", 1097 static_cast<void*>(m_opaque_sp.get()), 1098 (m_opaque_sp ? m_opaque_sp->GetPrompt() : "")); 1099 1100 if (m_opaque_sp) 1101 return m_opaque_sp->GetPrompt (); 1102 return 0; 1103 } 1104 1105 void 1106 SBDebugger::SetPrompt (const char *prompt) 1107 { 1108 if (m_opaque_sp) 1109 m_opaque_sp->SetPrompt (prompt); 1110 } 1111 1112 1113 ScriptLanguage 1114 SBDebugger::GetScriptLanguage() const 1115 { 1116 if (m_opaque_sp) 1117 return m_opaque_sp->GetScriptLanguage (); 1118 return eScriptLanguageNone; 1119 } 1120 1121 void 1122 SBDebugger::SetScriptLanguage (ScriptLanguage script_lang) 1123 { 1124 if (m_opaque_sp) 1125 { 1126 m_opaque_sp->SetScriptLanguage (script_lang); 1127 } 1128 } 1129 1130 bool 1131 SBDebugger::SetUseExternalEditor (bool value) 1132 { 1133 if (m_opaque_sp) 1134 return m_opaque_sp->SetUseExternalEditor (value); 1135 return false; 1136 } 1137 1138 bool 1139 SBDebugger::GetUseExternalEditor () 1140 { 1141 if (m_opaque_sp) 1142 return m_opaque_sp->GetUseExternalEditor (); 1143 return false; 1144 } 1145 1146 bool 1147 SBDebugger::SetUseColor (bool value) 1148 { 1149 if (m_opaque_sp) 1150 return m_opaque_sp->SetUseColor (value); 1151 return false; 1152 } 1153 1154 bool 1155 SBDebugger::GetUseColor () const 1156 { 1157 if (m_opaque_sp) 1158 return m_opaque_sp->GetUseColor (); 1159 return false; 1160 } 1161 1162 bool 1163 SBDebugger::GetDescription (SBStream &description) 1164 { 1165 Stream &strm = description.ref(); 1166 1167 if (m_opaque_sp) 1168 { 1169 const char *name = m_opaque_sp->GetInstanceName().AsCString(); 1170 user_id_t id = m_opaque_sp->GetID(); 1171 strm.Printf ("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id); 1172 } 1173 else 1174 strm.PutCString ("No value"); 1175 1176 return true; 1177 } 1178 1179 user_id_t 1180 SBDebugger::GetID() 1181 { 1182 if (m_opaque_sp) 1183 return m_opaque_sp->GetID(); 1184 return LLDB_INVALID_UID; 1185 } 1186 1187 1188 SBError 1189 SBDebugger::SetCurrentPlatform (const char *platform_name) 1190 { 1191 SBError sb_error; 1192 if (m_opaque_sp) 1193 { 1194 PlatformSP platform_sp (Platform::Create (platform_name, sb_error.ref())); 1195 1196 if (platform_sp) 1197 { 1198 bool make_selected = true; 1199 m_opaque_sp->GetPlatformList().Append (platform_sp, make_selected); 1200 } 1201 } 1202 return sb_error; 1203 } 1204 1205 bool 1206 SBDebugger::SetCurrentPlatformSDKRoot (const char *sysroot) 1207 { 1208 if (m_opaque_sp) 1209 { 1210 PlatformSP platform_sp (m_opaque_sp->GetPlatformList().GetSelectedPlatform()); 1211 1212 if (platform_sp) 1213 { 1214 platform_sp->SetSDKRootDirectory (ConstString (sysroot)); 1215 return true; 1216 } 1217 } 1218 return false; 1219 } 1220 1221 bool 1222 SBDebugger::GetCloseInputOnEOF () const 1223 { 1224 if (m_opaque_sp) 1225 return m_opaque_sp->GetCloseInputOnEOF (); 1226 return false; 1227 } 1228 1229 void 1230 SBDebugger::SetCloseInputOnEOF (bool b) 1231 { 1232 if (m_opaque_sp) 1233 m_opaque_sp->SetCloseInputOnEOF (b); 1234 } 1235 1236 SBTypeCategory 1237 SBDebugger::GetCategory (const char* category_name) 1238 { 1239 if (!category_name || *category_name == 0) 1240 return SBTypeCategory(); 1241 1242 TypeCategoryImplSP category_sp; 1243 1244 if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, false)) 1245 return SBTypeCategory(category_sp); 1246 else 1247 return SBTypeCategory(); 1248 } 1249 1250 SBTypeCategory 1251 SBDebugger::CreateCategory (const char* category_name) 1252 { 1253 if (!category_name || *category_name == 0) 1254 return SBTypeCategory(); 1255 1256 TypeCategoryImplSP category_sp; 1257 1258 if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, true)) 1259 return SBTypeCategory(category_sp); 1260 else 1261 return SBTypeCategory(); 1262 } 1263 1264 bool 1265 SBDebugger::DeleteCategory (const char* category_name) 1266 { 1267 if (!category_name || *category_name == 0) 1268 return false; 1269 1270 return DataVisualization::Categories::Delete(ConstString(category_name)); 1271 } 1272 1273 uint32_t 1274 SBDebugger::GetNumCategories() 1275 { 1276 return DataVisualization::Categories::GetCount(); 1277 } 1278 1279 SBTypeCategory 1280 SBDebugger::GetCategoryAtIndex (uint32_t index) 1281 { 1282 return SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index)); 1283 } 1284 1285 SBTypeCategory 1286 SBDebugger::GetDefaultCategory() 1287 { 1288 return GetCategory("default"); 1289 } 1290 1291 SBTypeFormat 1292 SBDebugger::GetFormatForType (SBTypeNameSpecifier type_name) 1293 { 1294 SBTypeCategory default_category_sb = GetDefaultCategory(); 1295 if (default_category_sb.GetEnabled()) 1296 return default_category_sb.GetFormatForType(type_name); 1297 return SBTypeFormat(); 1298 } 1299 1300 #ifndef LLDB_DISABLE_PYTHON 1301 SBTypeSummary 1302 SBDebugger::GetSummaryForType (SBTypeNameSpecifier type_name) 1303 { 1304 if (type_name.IsValid() == false) 1305 return SBTypeSummary(); 1306 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP())); 1307 } 1308 #endif // LLDB_DISABLE_PYTHON 1309 1310 SBTypeFilter 1311 SBDebugger::GetFilterForType (SBTypeNameSpecifier type_name) 1312 { 1313 if (type_name.IsValid() == false) 1314 return SBTypeFilter(); 1315 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP())); 1316 } 1317 1318 #ifndef LLDB_DISABLE_PYTHON 1319 SBTypeSynthetic 1320 SBDebugger::GetSyntheticForType (SBTypeNameSpecifier type_name) 1321 { 1322 if (type_name.IsValid() == false) 1323 return SBTypeSynthetic(); 1324 return SBTypeSynthetic(DataVisualization::GetSyntheticForType(type_name.GetSP())); 1325 } 1326 #endif // LLDB_DISABLE_PYTHON 1327 1328 bool 1329 SBDebugger::EnableLog (const char *channel, const char **categories) 1330 { 1331 if (m_opaque_sp) 1332 { 1333 uint32_t log_options = LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME; 1334 StreamString errors; 1335 return m_opaque_sp->EnableLog (channel, categories, NULL, log_options, errors); 1336 1337 } 1338 else 1339 return false; 1340 } 1341 1342 void 1343 SBDebugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton) 1344 { 1345 if (m_opaque_sp) 1346 { 1347 return m_opaque_sp->SetLoggingCallback (log_callback, baton); 1348 } 1349 } 1350 1351 1352