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