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 Error error; 719 const bool add_dependent_modules = true; 720 error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp, 721 filename, 722 NULL, 723 add_dependent_modules, 724 NULL, 725 target_sp); 726 727 if (error.Success()) 728 { 729 m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get()); 730 sb_target.SetSP (target_sp); 731 } 732 } 733 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 734 if (log) 735 log->Printf ("SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)", 736 static_cast<void*>(m_opaque_sp.get()), filename, 737 static_cast<void*>(target_sp.get())); 738 return sb_target; 739 } 740 741 bool 742 SBDebugger::DeleteTarget (lldb::SBTarget &target) 743 { 744 bool result = false; 745 if (m_opaque_sp) 746 { 747 TargetSP target_sp(target.GetSP()); 748 if (target_sp) 749 { 750 // No need to lock, the target list is thread safe 751 result = m_opaque_sp->GetTargetList().DeleteTarget (target_sp); 752 target_sp->Destroy(); 753 target.Clear(); 754 const bool mandatory = true; 755 ModuleList::RemoveOrphanSharedModules(mandatory); 756 } 757 } 758 759 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 760 if (log) 761 log->Printf ("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i", 762 static_cast<void*>(m_opaque_sp.get()), 763 static_cast<void*>(target.m_opaque_sp.get()), result); 764 765 return result; 766 } 767 SBTarget 768 SBDebugger::GetTargetAtIndex (uint32_t idx) 769 { 770 SBTarget sb_target; 771 if (m_opaque_sp) 772 { 773 // No need to lock, the target list is thread safe 774 sb_target.SetSP (m_opaque_sp->GetTargetList().GetTargetAtIndex (idx)); 775 } 776 return sb_target; 777 } 778 779 uint32_t 780 SBDebugger::GetIndexOfTarget (lldb::SBTarget target) 781 { 782 783 lldb::TargetSP target_sp = target.GetSP(); 784 if (!target_sp) 785 return UINT32_MAX; 786 787 if (!m_opaque_sp) 788 return UINT32_MAX; 789 790 return m_opaque_sp->GetTargetList().GetIndexOfTarget (target.GetSP()); 791 } 792 793 SBTarget 794 SBDebugger::FindTargetWithProcessID (lldb::pid_t pid) 795 { 796 SBTarget sb_target; 797 if (m_opaque_sp) 798 { 799 // No need to lock, the target list is thread safe 800 sb_target.SetSP (m_opaque_sp->GetTargetList().FindTargetWithProcessID (pid)); 801 } 802 return sb_target; 803 } 804 805 SBTarget 806 SBDebugger::FindTargetWithFileAndArch (const char *filename, const char *arch_name) 807 { 808 SBTarget sb_target; 809 if (m_opaque_sp && filename && filename[0]) 810 { 811 // No need to lock, the target list is thread safe 812 ArchSpec arch (arch_name, m_opaque_sp->GetPlatformList().GetSelectedPlatform().get()); 813 TargetSP target_sp (m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture (FileSpec(filename, false), arch_name ? &arch : NULL)); 814 sb_target.SetSP (target_sp); 815 } 816 return sb_target; 817 } 818 819 SBTarget 820 SBDebugger::FindTargetWithLLDBProcess (const ProcessSP &process_sp) 821 { 822 SBTarget sb_target; 823 if (m_opaque_sp) 824 { 825 // No need to lock, the target list is thread safe 826 sb_target.SetSP (m_opaque_sp->GetTargetList().FindTargetWithProcess (process_sp.get())); 827 } 828 return sb_target; 829 } 830 831 832 uint32_t 833 SBDebugger::GetNumTargets () 834 { 835 if (m_opaque_sp) 836 { 837 // No need to lock, the target list is thread safe 838 return m_opaque_sp->GetTargetList().GetNumTargets (); 839 } 840 return 0; 841 } 842 843 SBTarget 844 SBDebugger::GetSelectedTarget () 845 { 846 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 847 848 SBTarget sb_target; 849 TargetSP target_sp; 850 if (m_opaque_sp) 851 { 852 // No need to lock, the target list is thread safe 853 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget (); 854 sb_target.SetSP (target_sp); 855 } 856 857 if (log) 858 { 859 SBStream sstr; 860 sb_target.GetDescription (sstr, eDescriptionLevelBrief); 861 log->Printf ("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s", 862 static_cast<void*>(m_opaque_sp.get()), 863 static_cast<void*>(target_sp.get()), sstr.GetData()); 864 } 865 866 return sb_target; 867 } 868 869 void 870 SBDebugger::SetSelectedTarget (SBTarget &sb_target) 871 { 872 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 873 874 TargetSP target_sp (sb_target.GetSP()); 875 if (m_opaque_sp) 876 { 877 m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get()); 878 } 879 if (log) 880 { 881 SBStream sstr; 882 sb_target.GetDescription (sstr, eDescriptionLevelBrief); 883 log->Printf ("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s", 884 static_cast<void*>(m_opaque_sp.get()), 885 static_cast<void*>(target_sp.get()), sstr.GetData()); 886 } 887 } 888 889 SBPlatform 890 SBDebugger::GetSelectedPlatform() 891 { 892 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 893 894 SBPlatform sb_platform; 895 DebuggerSP debugger_sp(m_opaque_sp); 896 if (debugger_sp) 897 { 898 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform()); 899 } 900 if (log) 901 log->Printf ("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s", 902 static_cast<void*>(m_opaque_sp.get()), 903 static_cast<void*>(sb_platform.GetSP().get()), 904 sb_platform.GetName()); 905 return sb_platform; 906 } 907 908 void 909 SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) 910 { 911 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 912 913 DebuggerSP debugger_sp(m_opaque_sp); 914 if (debugger_sp) 915 { 916 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP()); 917 } 918 919 if (log) 920 log->Printf ("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)", 921 static_cast<void*>(m_opaque_sp.get()), 922 static_cast<void*>(sb_platform.GetSP().get()), 923 sb_platform.GetName()); 924 } 925 926 void 927 SBDebugger::DispatchInput (void* baton, const void *data, size_t data_len) 928 { 929 DispatchInput (data,data_len); 930 } 931 932 void 933 SBDebugger::DispatchInput (const void *data, size_t data_len) 934 { 935 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 936 // 937 // if (log) 938 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\", size_t=%" PRIu64 ")", 939 // m_opaque_sp.get(), 940 // (int) data_len, 941 // (const char *) data, 942 // (uint64_t)data_len); 943 // 944 // if (m_opaque_sp) 945 // m_opaque_sp->DispatchInput ((const char *) data, data_len); 946 } 947 948 void 949 SBDebugger::DispatchInputInterrupt () 950 { 951 if (m_opaque_sp) 952 m_opaque_sp->DispatchInputInterrupt (); 953 } 954 955 void 956 SBDebugger::DispatchInputEndOfFile () 957 { 958 if (m_opaque_sp) 959 m_opaque_sp->DispatchInputEndOfFile (); 960 } 961 962 void 963 SBDebugger::PushInputReader (SBInputReader &reader) 964 { 965 } 966 967 void 968 SBDebugger::RunCommandInterpreter (bool auto_handle_events, 969 bool spawn_thread) 970 { 971 if (m_opaque_sp) 972 { 973 CommandInterpreterRunOptions options; 974 975 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(auto_handle_events, 976 spawn_thread, 977 options); 978 } 979 } 980 981 void 982 SBDebugger::RunCommandInterpreter (bool auto_handle_events, 983 bool spawn_thread, 984 SBCommandInterpreterRunOptions &options, 985 int &num_errors, 986 bool &quit_requested, 987 bool &stopped_for_crash) 988 989 { 990 if (m_opaque_sp) 991 { 992 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter(); 993 interp.RunCommandInterpreter(auto_handle_events, spawn_thread, options.ref()); 994 num_errors = interp.GetNumErrors(); 995 quit_requested = interp.GetQuitRequested(); 996 stopped_for_crash = interp.GetStoppedForCrash(); 997 } 998 } 999 1000 void 1001 SBDebugger::reset (const DebuggerSP &debugger_sp) 1002 { 1003 m_opaque_sp = debugger_sp; 1004 } 1005 1006 Debugger * 1007 SBDebugger::get () const 1008 { 1009 return m_opaque_sp.get(); 1010 } 1011 1012 Debugger & 1013 SBDebugger::ref () const 1014 { 1015 assert (m_opaque_sp.get()); 1016 return *m_opaque_sp; 1017 } 1018 1019 const lldb::DebuggerSP & 1020 SBDebugger::get_sp () const 1021 { 1022 return m_opaque_sp; 1023 } 1024 1025 SBDebugger 1026 SBDebugger::FindDebuggerWithID (int id) 1027 { 1028 // No need to lock, the debugger list is thread safe 1029 SBDebugger sb_debugger; 1030 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID (id); 1031 if (debugger_sp) 1032 sb_debugger.reset (debugger_sp); 1033 return sb_debugger; 1034 } 1035 1036 const char * 1037 SBDebugger::GetInstanceName() 1038 { 1039 if (m_opaque_sp) 1040 return m_opaque_sp->GetInstanceName().AsCString(); 1041 else 1042 return NULL; 1043 } 1044 1045 SBError 1046 SBDebugger::SetInternalVariable (const char *var_name, const char *value, const char *debugger_instance_name) 1047 { 1048 SBError sb_error; 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 error = debugger_sp->SetPropertyValue (&exe_ctx, 1055 eVarSetOperationAssign, 1056 var_name, 1057 value); 1058 } 1059 else 1060 { 1061 error.SetErrorStringWithFormat ("invalid debugger instance name '%s'", debugger_instance_name); 1062 } 1063 if (error.Fail()) 1064 sb_error.SetError(error); 1065 return sb_error; 1066 } 1067 1068 SBStringList 1069 SBDebugger::GetInternalVariableValue (const char *var_name, const char *debugger_instance_name) 1070 { 1071 SBStringList ret_value; 1072 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name))); 1073 Error error; 1074 if (debugger_sp) 1075 { 1076 ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext()); 1077 lldb::OptionValueSP value_sp (debugger_sp->GetPropertyValue (&exe_ctx, 1078 var_name, 1079 false, 1080 error)); 1081 if (value_sp) 1082 { 1083 StreamString value_strm; 1084 value_sp->DumpValue (&exe_ctx, value_strm, OptionValue::eDumpOptionValue); 1085 const std::string &value_str = value_strm.GetString(); 1086 if (!value_str.empty()) 1087 { 1088 StringList string_list; 1089 string_list.SplitIntoLines(value_str); 1090 return SBStringList(&string_list); 1091 } 1092 } 1093 } 1094 return SBStringList(); 1095 } 1096 1097 uint32_t 1098 SBDebugger::GetTerminalWidth () const 1099 { 1100 if (m_opaque_sp) 1101 return m_opaque_sp->GetTerminalWidth (); 1102 return 0; 1103 } 1104 1105 void 1106 SBDebugger::SetTerminalWidth (uint32_t term_width) 1107 { 1108 if (m_opaque_sp) 1109 m_opaque_sp->SetTerminalWidth (term_width); 1110 } 1111 1112 const char * 1113 SBDebugger::GetPrompt() const 1114 { 1115 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1116 1117 if (log) 1118 log->Printf ("SBDebugger(%p)::GetPrompt () => \"%s\"", 1119 static_cast<void*>(m_opaque_sp.get()), 1120 (m_opaque_sp ? m_opaque_sp->GetPrompt() : "")); 1121 1122 if (m_opaque_sp) 1123 return m_opaque_sp->GetPrompt (); 1124 return 0; 1125 } 1126 1127 void 1128 SBDebugger::SetPrompt (const char *prompt) 1129 { 1130 if (m_opaque_sp) 1131 m_opaque_sp->SetPrompt (prompt); 1132 } 1133 1134 1135 ScriptLanguage 1136 SBDebugger::GetScriptLanguage() const 1137 { 1138 if (m_opaque_sp) 1139 return m_opaque_sp->GetScriptLanguage (); 1140 return eScriptLanguageNone; 1141 } 1142 1143 void 1144 SBDebugger::SetScriptLanguage (ScriptLanguage script_lang) 1145 { 1146 if (m_opaque_sp) 1147 { 1148 m_opaque_sp->SetScriptLanguage (script_lang); 1149 } 1150 } 1151 1152 bool 1153 SBDebugger::SetUseExternalEditor (bool value) 1154 { 1155 if (m_opaque_sp) 1156 return m_opaque_sp->SetUseExternalEditor (value); 1157 return false; 1158 } 1159 1160 bool 1161 SBDebugger::GetUseExternalEditor () 1162 { 1163 if (m_opaque_sp) 1164 return m_opaque_sp->GetUseExternalEditor (); 1165 return false; 1166 } 1167 1168 bool 1169 SBDebugger::SetUseColor (bool value) 1170 { 1171 if (m_opaque_sp) 1172 return m_opaque_sp->SetUseColor (value); 1173 return false; 1174 } 1175 1176 bool 1177 SBDebugger::GetUseColor () const 1178 { 1179 if (m_opaque_sp) 1180 return m_opaque_sp->GetUseColor (); 1181 return false; 1182 } 1183 1184 bool 1185 SBDebugger::GetDescription (SBStream &description) 1186 { 1187 Stream &strm = description.ref(); 1188 1189 if (m_opaque_sp) 1190 { 1191 const char *name = m_opaque_sp->GetInstanceName().AsCString(); 1192 user_id_t id = m_opaque_sp->GetID(); 1193 strm.Printf ("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id); 1194 } 1195 else 1196 strm.PutCString ("No value"); 1197 1198 return true; 1199 } 1200 1201 user_id_t 1202 SBDebugger::GetID() 1203 { 1204 if (m_opaque_sp) 1205 return m_opaque_sp->GetID(); 1206 return LLDB_INVALID_UID; 1207 } 1208 1209 1210 SBError 1211 SBDebugger::SetCurrentPlatform (const char *platform_name_cstr) 1212 { 1213 SBError sb_error; 1214 if (m_opaque_sp) 1215 { 1216 if (platform_name_cstr && platform_name_cstr[0]) 1217 { 1218 ConstString platform_name (platform_name_cstr); 1219 PlatformSP platform_sp (Platform::Find (platform_name)); 1220 1221 if (platform_sp) 1222 { 1223 // Already have a platform with this name, just select it 1224 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp); 1225 } 1226 else 1227 { 1228 // We don't have a platform by this name yet, create one 1229 platform_sp = Platform::Create (platform_name, sb_error.ref()); 1230 if (platform_sp) 1231 { 1232 // We created the platform, now append and select it 1233 bool make_selected = true; 1234 m_opaque_sp->GetPlatformList().Append (platform_sp, make_selected); 1235 } 1236 } 1237 } 1238 else 1239 { 1240 sb_error.ref().SetErrorString("invalid platform name"); 1241 } 1242 } 1243 else 1244 { 1245 sb_error.ref().SetErrorString("invalid debugger"); 1246 } 1247 return sb_error; 1248 } 1249 1250 bool 1251 SBDebugger::SetCurrentPlatformSDKRoot (const char *sysroot) 1252 { 1253 if (m_opaque_sp) 1254 { 1255 PlatformSP platform_sp (m_opaque_sp->GetPlatformList().GetSelectedPlatform()); 1256 1257 if (platform_sp) 1258 { 1259 platform_sp->SetSDKRootDirectory (ConstString (sysroot)); 1260 return true; 1261 } 1262 } 1263 return false; 1264 } 1265 1266 bool 1267 SBDebugger::GetCloseInputOnEOF () const 1268 { 1269 if (m_opaque_sp) 1270 return m_opaque_sp->GetCloseInputOnEOF (); 1271 return false; 1272 } 1273 1274 void 1275 SBDebugger::SetCloseInputOnEOF (bool b) 1276 { 1277 if (m_opaque_sp) 1278 m_opaque_sp->SetCloseInputOnEOF (b); 1279 } 1280 1281 SBTypeCategory 1282 SBDebugger::GetCategory (const char* category_name) 1283 { 1284 if (!category_name || *category_name == 0) 1285 return SBTypeCategory(); 1286 1287 TypeCategoryImplSP category_sp; 1288 1289 if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, false)) 1290 return SBTypeCategory(category_sp); 1291 else 1292 return SBTypeCategory(); 1293 } 1294 1295 SBTypeCategory 1296 SBDebugger::CreateCategory (const char* category_name) 1297 { 1298 if (!category_name || *category_name == 0) 1299 return SBTypeCategory(); 1300 1301 TypeCategoryImplSP category_sp; 1302 1303 if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, true)) 1304 return SBTypeCategory(category_sp); 1305 else 1306 return SBTypeCategory(); 1307 } 1308 1309 bool 1310 SBDebugger::DeleteCategory (const char* category_name) 1311 { 1312 if (!category_name || *category_name == 0) 1313 return false; 1314 1315 return DataVisualization::Categories::Delete(ConstString(category_name)); 1316 } 1317 1318 uint32_t 1319 SBDebugger::GetNumCategories() 1320 { 1321 return DataVisualization::Categories::GetCount(); 1322 } 1323 1324 SBTypeCategory 1325 SBDebugger::GetCategoryAtIndex (uint32_t index) 1326 { 1327 return SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index)); 1328 } 1329 1330 SBTypeCategory 1331 SBDebugger::GetDefaultCategory() 1332 { 1333 return GetCategory("default"); 1334 } 1335 1336 SBTypeFormat 1337 SBDebugger::GetFormatForType (SBTypeNameSpecifier type_name) 1338 { 1339 SBTypeCategory default_category_sb = GetDefaultCategory(); 1340 if (default_category_sb.GetEnabled()) 1341 return default_category_sb.GetFormatForType(type_name); 1342 return SBTypeFormat(); 1343 } 1344 1345 #ifndef LLDB_DISABLE_PYTHON 1346 SBTypeSummary 1347 SBDebugger::GetSummaryForType (SBTypeNameSpecifier type_name) 1348 { 1349 if (type_name.IsValid() == false) 1350 return SBTypeSummary(); 1351 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP())); 1352 } 1353 #endif // LLDB_DISABLE_PYTHON 1354 1355 SBTypeFilter 1356 SBDebugger::GetFilterForType (SBTypeNameSpecifier type_name) 1357 { 1358 if (type_name.IsValid() == false) 1359 return SBTypeFilter(); 1360 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP())); 1361 } 1362 1363 #ifndef LLDB_DISABLE_PYTHON 1364 SBTypeSynthetic 1365 SBDebugger::GetSyntheticForType (SBTypeNameSpecifier type_name) 1366 { 1367 if (type_name.IsValid() == false) 1368 return SBTypeSynthetic(); 1369 return SBTypeSynthetic(DataVisualization::GetSyntheticForType(type_name.GetSP())); 1370 } 1371 #endif // LLDB_DISABLE_PYTHON 1372 1373 bool 1374 SBDebugger::EnableLog (const char *channel, const char **categories) 1375 { 1376 if (m_opaque_sp) 1377 { 1378 uint32_t log_options = LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME; 1379 StreamString errors; 1380 return m_opaque_sp->EnableLog (channel, categories, NULL, log_options, errors); 1381 1382 } 1383 else 1384 return false; 1385 } 1386 1387 void 1388 SBDebugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton) 1389 { 1390 if (m_opaque_sp) 1391 { 1392 return m_opaque_sp->SetLoggingCallback (log_callback, baton); 1393 } 1394 } 1395 1396 1397