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