1 //===-- SBDebugger.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/API/SBDebugger.h"
16 #include "lldb/lldb-private.h"
18 #include "lldb/API/SBBroadcaster.h"
19 #include "lldb/API/SBCommandInterpreter.h"
20 #include "lldb/API/SBCommandReturnObject.h"
21 #include "lldb/API/SBError.h"
22 #include "lldb/API/SBEvent.h"
23 #include "lldb/API/SBFrame.h"
24 #include "lldb/API/SBListener.h"
25 #include "lldb/API/SBProcess.h"
26 #include "lldb/API/SBSourceManager.h"
27 #include "lldb/API/SBStream.h"
28 #include "lldb/API/SBStringList.h"
29 #include "lldb/API/SBStructuredData.h"
30 #include "lldb/API/SBTarget.h"
31 #include "lldb/API/SBThread.h"
32 #include "lldb/API/SBTypeCategory.h"
33 #include "lldb/API/SBTypeFilter.h"
34 #include "lldb/API/SBTypeFormat.h"
35 #include "lldb/API/SBTypeNameSpecifier.h"
36 #include "lldb/API/SBTypeSummary.h"
37 #include "lldb/API/SBTypeSynthetic.h"
38 #include "lldb/API/SystemInitializerFull.h"
40 #include "lldb/Core/Debugger.h"
41 #include "lldb/Core/PluginManager.h"
42 #include "lldb/Core/State.h"
43 #include "lldb/Core/StreamFile.h"
44 #include "lldb/Core/StructuredDataImpl.h"
45 #include "lldb/DataFormatters/DataVisualization.h"
46 #include "lldb/Initialization/SystemLifetimeManager.h"
47 #include "lldb/Interpreter/Args.h"
48 #include "lldb/Interpreter/CommandInterpreter.h"
49 #include "lldb/Interpreter/OptionGroupPlatform.h"
50 #include "lldb/Target/Process.h"
51 #include "lldb/Target/TargetList.h"
53 #include "llvm/ADT/STLExtras.h"
54 #include "llvm/ADT/StringRef.h"
55 #include "llvm/Support/DynamicLibrary.h"
56 #include "llvm/Support/ManagedStatic.h"
59 using namespace lldb_private;
61 static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp,
64 llvm::sys::DynamicLibrary dynlib =
65 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
66 if (dynlib.isValid()) {
67 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger);
69 lldb::SBDebugger debugger_sb(debugger_sp);
70 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
72 // TODO: mangle this differently for your system - on OSX, the first
73 // underscore needs to be removed and the second one stays
74 LLDBCommandPluginInit init_func =
75 (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
76 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
78 if (init_func(debugger_sb))
81 error.SetErrorString("plug-in refused to load "
82 "(lldb::PluginInitialize(lldb::SBDebugger) "
85 error.SetErrorString("plug-in is missing the required initialization: "
86 "lldb::PluginInitialize(lldb::SBDebugger)");
90 error.SetErrorString("this file does not represent a loadable dylib");
92 error.SetErrorString("no such file");
94 return llvm::sys::DynamicLibrary();
97 static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;
99 SBError SBInputReader::Initialize(
100 lldb::SBDebugger &sb_debugger,
101 unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction,
102 char const *, unsigned long),
103 void *, lldb::InputReaderGranularity, char const *, char const *, bool) {
107 void SBInputReader::SetIsDone(bool) {}
109 bool SBInputReader::IsActive() const { return false; }
111 SBDebugger::SBDebugger() = default;
113 SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp)
114 : m_opaque_sp(debugger_sp) {}
116 SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
118 SBDebugger::~SBDebugger() = default;
120 SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) {
122 m_opaque_sp = rhs.m_opaque_sp;
127 void SBDebugger::Initialize() {
128 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
131 log->Printf("SBDebugger::Initialize ()");
133 g_debugger_lifetime->Initialize(llvm::make_unique<SystemInitializerFull>(),
137 void SBDebugger::Terminate() { g_debugger_lifetime->Terminate(); }
139 void SBDebugger::Clear() {
140 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
143 log->Printf("SBDebugger(%p)::Clear ()",
144 static_cast<void *>(m_opaque_sp.get()));
147 m_opaque_sp->ClearIOHandlers();
152 SBDebugger SBDebugger::Create() {
153 return SBDebugger::Create(false, nullptr, nullptr);
156 SBDebugger SBDebugger::Create(bool source_init_files) {
157 return SBDebugger::Create(source_init_files, nullptr, nullptr);
160 SBDebugger SBDebugger::Create(bool source_init_files,
161 lldb::LogOutputCallback callback, void *baton)
164 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
168 // Currently we have issues if this function is called simultaneously on two
170 // threads. The issues mainly revolve around the fact that the
171 // lldb_private::FormatManager
172 // uses global collections and having two threads parsing the .lldbinit files
174 // mayhem. So to get around this for now we need to use a mutex to prevent bad
177 static std::recursive_mutex g_mutex;
178 std::lock_guard<std::recursive_mutex> guard(g_mutex);
180 debugger.reset(Debugger::CreateInstance(callback, baton));
184 debugger.GetDescription(sstr);
185 log->Printf("SBDebugger::Create () => SBDebugger(%p): %s",
186 static_cast<void *>(debugger.m_opaque_sp.get()),
190 SBCommandInterpreter interp = debugger.GetCommandInterpreter();
191 if (source_init_files) {
192 interp.get()->SkipLLDBInitFiles(false);
193 interp.get()->SkipAppInitFiles(false);
194 SBCommandReturnObject result;
195 interp.SourceInitFileInHomeDirectory(result);
197 interp.get()->SkipLLDBInitFiles(true);
198 interp.get()->SkipAppInitFiles(true);
203 void SBDebugger::Destroy(SBDebugger &debugger) {
204 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
208 debugger.GetDescription(sstr);
209 log->Printf("SBDebugger::Destroy () => SBDebugger(%p): %s",
210 static_cast<void *>(debugger.m_opaque_sp.get()),
214 Debugger::Destroy(debugger.m_opaque_sp);
216 if (debugger.m_opaque_sp.get() != nullptr)
217 debugger.m_opaque_sp.reset();
220 void SBDebugger::MemoryPressureDetected() {
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));
227 const bool mandatory = false;
229 log->Printf("SBDebugger::MemoryPressureDetected (), mandatory = %d",
233 ModuleList::RemoveOrphanSharedModules(mandatory);
236 bool SBDebugger::IsValid() const { return m_opaque_sp.get() != nullptr; }
238 void SBDebugger::SetAsync(bool b) {
240 m_opaque_sp->SetAsyncExecution(b);
243 bool SBDebugger::GetAsync() {
244 return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false);
247 void SBDebugger::SkipLLDBInitFiles(bool b) {
249 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b);
252 void SBDebugger::SkipAppInitFiles(bool b) {
254 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b);
257 // Shouldn't really be settable after initialization as this could cause lots of
258 // problems; don't want users
259 // trying to switch modes in the middle of a debugging session.
260 void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) {
261 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
265 "SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)",
266 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
270 m_opaque_sp->SetInputFileHandle(fh, transfer_ownership);
273 void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) {
274 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
278 "SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)",
279 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
283 m_opaque_sp->SetOutputFileHandle(fh, transfer_ownership);
286 void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) {
287 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
291 "SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)",
292 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
296 m_opaque_sp->SetErrorFileHandle(fh, transfer_ownership);
299 FILE *SBDebugger::GetInputFileHandle() {
301 StreamFileSP stream_file_sp(m_opaque_sp->GetInputFile());
303 return stream_file_sp->GetFile().GetStream();
308 FILE *SBDebugger::GetOutputFileHandle() {
310 StreamFileSP stream_file_sp(m_opaque_sp->GetOutputFile());
312 return stream_file_sp->GetFile().GetStream();
317 FILE *SBDebugger::GetErrorFileHandle() {
319 StreamFileSP stream_file_sp(m_opaque_sp->GetErrorFile());
321 return stream_file_sp->GetFile().GetStream();
326 void SBDebugger::SaveInputTerminalState() {
328 m_opaque_sp->SaveInputTerminalState();
331 void SBDebugger::RestoreInputTerminalState() {
333 m_opaque_sp->RestoreInputTerminalState();
335 SBCommandInterpreter SBDebugger::GetCommandInterpreter() {
336 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
338 SBCommandInterpreter sb_interpreter;
340 sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter());
344 "SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)",
345 static_cast<void *>(m_opaque_sp.get()),
346 static_cast<void *>(sb_interpreter.get()));
348 return sb_interpreter;
351 void SBDebugger::HandleCommand(const char *command) {
353 TargetSP target_sp(m_opaque_sp->GetSelectedTarget());
354 std::unique_lock<std::recursive_mutex> lock;
356 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
358 SBCommandInterpreter sb_interpreter(GetCommandInterpreter());
359 SBCommandReturnObject result;
361 sb_interpreter.HandleCommand(command, result, false);
363 if (GetErrorFileHandle() != nullptr)
364 result.PutError(GetErrorFileHandle());
365 if (GetOutputFileHandle() != nullptr)
366 result.PutOutput(GetOutputFileHandle());
368 if (!m_opaque_sp->GetAsyncExecution()) {
369 SBProcess process(GetCommandInterpreter().GetProcess());
370 ProcessSP process_sp(process.GetSP());
373 ListenerSP lldb_listener_sp = m_opaque_sp->GetListener();
374 while (lldb_listener_sp->GetEventForBroadcaster(
375 process_sp.get(), event_sp, std::chrono::seconds(0))) {
376 SBEvent event(event_sp);
377 HandleProcessEvent(process, event, GetOutputFileHandle(),
378 GetErrorFileHandle());
385 SBListener SBDebugger::GetListener() {
386 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
388 SBListener sb_listener;
390 sb_listener.reset(m_opaque_sp->GetListener());
393 log->Printf("SBDebugger(%p)::GetListener () => SBListener(%p)",
394 static_cast<void *>(m_opaque_sp.get()),
395 static_cast<void *>(sb_listener.get()));
400 void SBDebugger::HandleProcessEvent(const SBProcess &process,
401 const SBEvent &event, FILE *out,
403 if (!process.IsValid())
406 TargetSP target_sp(process.GetTarget().GetSP());
410 const uint32_t event_type = event.GetType();
411 char stdio_buffer[1024];
414 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
417 (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) {
418 // Drain stdout when we stop just in case we have any bytes
419 while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0)
421 ::fwrite(stdio_buffer, 1, len, out);
425 (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) {
426 // Drain stderr when we stop just in case we have any bytes
427 while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0)
429 ::fwrite(stdio_buffer, 1, len, err);
432 if (event_type & Process::eBroadcastBitStateChanged) {
433 StateType event_state = SBProcess::GetStateFromEvent(event);
435 if (event_state == eStateInvalid)
438 bool is_stopped = StateIsStoppedState(event_state);
440 process.ReportEventState(event, out);
444 SBSourceManager SBDebugger::GetSourceManager() {
445 SBSourceManager sb_source_manager(*this);
446 return sb_source_manager;
449 bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) {
450 if (arch_name && arch_name_len) {
451 ArchSpec default_arch = Target::GetDefaultArchitecture();
453 if (default_arch.IsValid()) {
454 const std::string &triple_str = default_arch.GetTriple().str();
455 if (!triple_str.empty())
456 ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str());
458 ::snprintf(arch_name, arch_name_len, "%s",
459 default_arch.GetArchitectureName());
463 if (arch_name && arch_name_len)
468 bool SBDebugger::SetDefaultArchitecture(const char *arch_name) {
470 ArchSpec arch(arch_name);
471 if (arch.IsValid()) {
472 Target::SetDefaultArchitecture(arch);
480 SBDebugger::GetScriptingLanguage(const char *script_language_name) {
481 if (!script_language_name) return eScriptLanguageDefault;
482 return Args::StringToScriptLanguage(llvm::StringRef(script_language_name),
483 eScriptLanguageDefault, nullptr);
486 const char *SBDebugger::GetVersionString() {
487 return lldb_private::GetVersion();
490 const char *SBDebugger::StateAsCString(StateType state) {
491 return lldb_private::StateAsCString(state);
494 bool SBDebugger::StateIsRunningState(StateType state) {
495 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
497 const bool result = lldb_private::StateIsRunningState(state);
499 log->Printf("SBDebugger::StateIsRunningState (state=%s) => %i",
500 StateAsCString(state), result);
505 bool SBDebugger::StateIsStoppedState(StateType state) {
506 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
508 const bool result = lldb_private::StateIsStoppedState(state, false);
510 log->Printf("SBDebugger::StateIsStoppedState (state=%s) => %i",
511 StateAsCString(state), result);
516 lldb::SBTarget SBDebugger::CreateTarget(const char *filename,
517 const char *target_triple,
518 const char *platform_name,
519 bool add_dependent_modules,
520 lldb::SBError &sb_error) {
525 OptionGroupPlatform platform_options(false);
526 platform_options.SetPlatformName(platform_name);
528 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget(
529 *m_opaque_sp, filename, target_triple, add_dependent_modules,
530 &platform_options, target_sp);
532 if (sb_error.Success())
533 sb_target.SetSP(target_sp);
535 sb_error.SetErrorString("invalid debugger");
538 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
540 log->Printf("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, "
541 "platform_name=%s, add_dependent_modules=%u, error=%s) => "
543 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
544 platform_name, add_dependent_modules, sb_error.GetCString(),
545 static_cast<void *>(target_sp.get()));
551 SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename,
552 const char *target_triple) {
556 const bool add_dependent_modules = true;
557 Status error(m_opaque_sp->GetTargetList().CreateTarget(
558 *m_opaque_sp, filename, target_triple, add_dependent_modules, nullptr,
560 sb_target.SetSP(target_sp);
563 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
565 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple "
566 "(filename=\"%s\", triple=%s) => SBTarget(%p)",
567 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
568 static_cast<void *>(target_sp.get()));
573 SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
574 const char *arch_cstr) {
575 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
581 const bool add_dependent_modules = true;
583 error = m_opaque_sp->GetTargetList().CreateTarget(
584 *m_opaque_sp, filename, arch_cstr, add_dependent_modules, nullptr,
587 if (error.Success()) {
588 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
589 sb_target.SetSP(target_sp);
594 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
595 "arch=%s) => SBTarget(%p)",
596 static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr,
597 static_cast<void *>(target_sp.get()));
602 SBTarget SBDebugger::CreateTarget(const char *filename) {
607 const bool add_dependent_modules = true;
608 error = m_opaque_sp->GetTargetList().CreateTarget(
609 *m_opaque_sp, filename, "", add_dependent_modules, nullptr, target_sp);
611 if (error.Success()) {
612 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
613 sb_target.SetSP(target_sp);
616 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
619 "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
620 static_cast<void *>(m_opaque_sp.get()), filename,
621 static_cast<void *>(target_sp.get()));
625 SBTarget SBDebugger::GetDummyTarget() {
628 sb_target.SetSP(m_opaque_sp->GetDummyTarget()->shared_from_this());
630 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
633 "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)",
634 static_cast<void *>(m_opaque_sp.get()),
635 static_cast<void *>(sb_target.GetSP().get()));
639 bool SBDebugger::DeleteTarget(lldb::SBTarget &target) {
642 TargetSP target_sp(target.GetSP());
644 // No need to lock, the target list is thread safe
645 result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp);
646 target_sp->Destroy();
648 const bool mandatory = true;
649 ModuleList::RemoveOrphanSharedModules(mandatory);
653 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
655 log->Printf("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
656 static_cast<void *>(m_opaque_sp.get()),
657 static_cast<void *>(target.m_opaque_sp.get()), result);
662 SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) {
665 // No need to lock, the target list is thread safe
666 sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx));
671 uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) {
673 lldb::TargetSP target_sp = target.GetSP();
680 return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP());
683 SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) {
686 // No need to lock, the target list is thread safe
687 sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid));
692 SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename,
693 const char *arch_name) {
695 if (m_opaque_sp && filename && filename[0]) {
696 // No need to lock, the target list is thread safe
697 ArchSpec arch = Platform::GetAugmentedArchSpec(
698 m_opaque_sp->GetPlatformList().GetSelectedPlatform().get(), arch_name);
700 m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(
701 FileSpec(filename, false), arch_name ? &arch : nullptr));
702 sb_target.SetSP(target_sp);
707 SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) {
710 // No need to lock, the target list is thread safe
712 m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get()));
717 uint32_t SBDebugger::GetNumTargets() {
719 // No need to lock, the target list is thread safe
720 return m_opaque_sp->GetTargetList().GetNumTargets();
725 SBTarget SBDebugger::GetSelectedTarget() {
726 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
731 // No need to lock, the target list is thread safe
732 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget();
733 sb_target.SetSP(target_sp);
738 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
739 log->Printf("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
740 static_cast<void *>(m_opaque_sp.get()),
741 static_cast<void *>(target_sp.get()), sstr.GetData());
747 void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
748 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
750 TargetSP target_sp(sb_target.GetSP());
752 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
756 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
757 log->Printf("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
758 static_cast<void *>(m_opaque_sp.get()),
759 static_cast<void *>(target_sp.get()), sstr.GetData());
763 SBPlatform SBDebugger::GetSelectedPlatform() {
764 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
766 SBPlatform sb_platform;
767 DebuggerSP debugger_sp(m_opaque_sp);
769 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
772 log->Printf("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
773 static_cast<void *>(m_opaque_sp.get()),
774 static_cast<void *>(sb_platform.GetSP().get()),
775 sb_platform.GetName());
779 void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) {
780 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
782 DebuggerSP debugger_sp(m_opaque_sp);
784 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
788 log->Printf("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
789 static_cast<void *>(m_opaque_sp.get()),
790 static_cast<void *>(sb_platform.GetSP().get()),
791 sb_platform.GetName());
794 uint32_t SBDebugger::GetNumPlatforms() {
796 // No need to lock, the platform list is thread safe
797 return m_opaque_sp->GetPlatformList().GetSize();
802 SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) {
803 SBPlatform sb_platform;
805 // No need to lock, the platform list is thread safe
806 sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx));
811 uint32_t SBDebugger::GetNumAvailablePlatforms() {
814 if (!PluginManager::GetPlatformPluginNameAtIndex(idx)) {
819 // +1 for the host platform, which should always appear first in the list.
823 SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) {
824 SBStructuredData data;
825 auto platform_dict = llvm::make_unique<StructuredData::Dictionary>();
826 llvm::StringRef name_str("name"), desc_str("description");
829 PlatformSP host_platform_sp(Platform::GetHostPlatform());
830 platform_dict->AddStringItem(
831 name_str, host_platform_sp->GetPluginName().GetStringRef());
832 platform_dict->AddStringItem(
833 desc_str, llvm::StringRef(host_platform_sp->GetDescription()));
834 } else if (idx > 0) {
835 const char *plugin_name =
836 PluginManager::GetPlatformPluginNameAtIndex(idx - 1);
840 platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name));
842 const char *plugin_desc =
843 PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1);
847 platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc));
850 data.m_impl_up->SetObjectSP(
851 StructuredData::ObjectSP(platform_dict.release()));
855 void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) {
856 DispatchInput(data, data_len);
859 void SBDebugger::DispatchInput(const void *data, size_t data_len) {
860 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
863 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\",
864 // size_t=%" PRIu64 ")",
865 // m_opaque_sp.get(),
867 // (const char *) data,
868 // (uint64_t)data_len);
871 // m_opaque_sp->DispatchInput ((const char *) data, data_len);
874 void SBDebugger::DispatchInputInterrupt() {
876 m_opaque_sp->DispatchInputInterrupt();
879 void SBDebugger::DispatchInputEndOfFile() {
881 m_opaque_sp->DispatchInputEndOfFile();
884 void SBDebugger::PushInputReader(SBInputReader &reader) {}
886 void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
889 CommandInterpreterRunOptions options;
891 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(
892 auto_handle_events, spawn_thread, options);
896 void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
898 SBCommandInterpreterRunOptions &options,
899 int &num_errors, bool &quit_requested,
900 bool &stopped_for_crash)
904 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
905 interp.RunCommandInterpreter(auto_handle_events, spawn_thread,
907 num_errors = interp.GetNumErrors();
908 quit_requested = interp.GetQuitRequested();
909 stopped_for_crash = interp.GetStoppedForCrash();
913 SBError SBDebugger::RunREPL(lldb::LanguageType language,
914 const char *repl_options) {
917 error.ref() = m_opaque_sp->RunREPL(language, repl_options);
919 error.SetErrorString("invalid debugger");
923 void SBDebugger::reset(const DebuggerSP &debugger_sp) {
924 m_opaque_sp = debugger_sp;
927 Debugger *SBDebugger::get() const { return m_opaque_sp.get(); }
929 Debugger &SBDebugger::ref() const {
930 assert(m_opaque_sp.get());
934 const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; }
936 SBDebugger SBDebugger::FindDebuggerWithID(int id) {
937 // No need to lock, the debugger list is thread safe
938 SBDebugger sb_debugger;
939 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id);
941 sb_debugger.reset(debugger_sp);
945 const char *SBDebugger::GetInstanceName() {
946 return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr);
949 SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
950 const char *debugger_instance_name) {
952 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
953 ConstString(debugger_instance_name)));
956 ExecutionContext exe_ctx(
957 debugger_sp->GetCommandInterpreter().GetExecutionContext());
958 error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
961 error.SetErrorStringWithFormat("invalid debugger instance name '%s'",
962 debugger_instance_name);
965 sb_error.SetError(error);
970 SBDebugger::GetInternalVariableValue(const char *var_name,
971 const char *debugger_instance_name) {
972 SBStringList ret_value;
973 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
974 ConstString(debugger_instance_name)));
977 ExecutionContext exe_ctx(
978 debugger_sp->GetCommandInterpreter().GetExecutionContext());
979 lldb::OptionValueSP value_sp(
980 debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error));
982 StreamString value_strm;
983 value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
984 const std::string &value_str = value_strm.GetString();
985 if (!value_str.empty()) {
986 StringList string_list;
987 string_list.SplitIntoLines(value_str);
988 return SBStringList(&string_list);
992 return SBStringList();
995 uint32_t SBDebugger::GetTerminalWidth() const {
996 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
999 void SBDebugger::SetTerminalWidth(uint32_t term_width) {
1001 m_opaque_sp->SetTerminalWidth(term_width);
1004 const char *SBDebugger::GetPrompt() const {
1005 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1008 log->Printf("SBDebugger(%p)::GetPrompt () => \"%s\"",
1009 static_cast<void *>(m_opaque_sp.get()),
1010 (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : ""));
1012 return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
1016 void SBDebugger::SetPrompt(const char *prompt) {
1018 m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt));
1021 ScriptLanguage SBDebugger::GetScriptLanguage() const {
1022 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
1025 void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) {
1027 m_opaque_sp->SetScriptLanguage(script_lang);
1031 bool SBDebugger::SetUseExternalEditor(bool value) {
1032 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
1035 bool SBDebugger::GetUseExternalEditor() {
1036 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
1039 bool SBDebugger::SetUseColor(bool value) {
1040 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
1043 bool SBDebugger::GetUseColor() const {
1044 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
1047 bool SBDebugger::GetDescription(SBStream &description) {
1048 Stream &strm = description.ref();
1051 const char *name = m_opaque_sp->GetInstanceName().AsCString();
1052 user_id_t id = m_opaque_sp->GetID();
1053 strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1055 strm.PutCString("No value");
1060 user_id_t SBDebugger::GetID() {
1061 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
1064 SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
1067 if (platform_name_cstr && platform_name_cstr[0]) {
1068 ConstString platform_name(platform_name_cstr);
1069 PlatformSP platform_sp(Platform::Find(platform_name));
1072 // Already have a platform with this name, just select it
1073 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
1075 // We don't have a platform by this name yet, create one
1076 platform_sp = Platform::Create(platform_name, sb_error.ref());
1078 // We created the platform, now append and select it
1079 bool make_selected = true;
1080 m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected);
1084 sb_error.ref().SetErrorString("invalid platform name");
1087 sb_error.ref().SetErrorString("invalid debugger");
1092 bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) {
1093 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1095 PlatformSP platform_sp(
1096 m_opaque_sp->GetPlatformList().GetSelectedPlatform());
1100 log->Printf("SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")", sysroot);
1101 platform_sp->SetSDKRootDirectory(ConstString(sysroot));
1108 bool SBDebugger::GetCloseInputOnEOF() const {
1109 return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false);
1112 void SBDebugger::SetCloseInputOnEOF(bool b) {
1114 m_opaque_sp->SetCloseInputOnEOF(b);
1117 SBTypeCategory SBDebugger::GetCategory(const char *category_name) {
1118 if (!category_name || *category_name == 0)
1119 return SBTypeCategory();
1121 TypeCategoryImplSP category_sp;
1123 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1124 category_sp, false))
1125 return SBTypeCategory(category_sp);
1127 return SBTypeCategory();
1130 SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) {
1131 TypeCategoryImplSP category_sp;
1132 if (DataVisualization::Categories::GetCategory(lang_type, category_sp))
1133 return SBTypeCategory(category_sp);
1135 return SBTypeCategory();
1138 SBTypeCategory SBDebugger::CreateCategory(const char *category_name) {
1139 if (!category_name || *category_name == 0)
1140 return SBTypeCategory();
1142 TypeCategoryImplSP category_sp;
1144 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1146 return SBTypeCategory(category_sp);
1148 return SBTypeCategory();
1151 bool SBDebugger::DeleteCategory(const char *category_name) {
1152 if (!category_name || *category_name == 0)
1155 return DataVisualization::Categories::Delete(ConstString(category_name));
1158 uint32_t SBDebugger::GetNumCategories() {
1159 return DataVisualization::Categories::GetCount();
1162 SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) {
1163 return SBTypeCategory(
1164 DataVisualization::Categories::GetCategoryAtIndex(index));
1167 SBTypeCategory SBDebugger::GetDefaultCategory() {
1168 return GetCategory("default");
1171 SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) {
1172 SBTypeCategory default_category_sb = GetDefaultCategory();
1173 if (default_category_sb.GetEnabled())
1174 return default_category_sb.GetFormatForType(type_name);
1175 return SBTypeFormat();
1178 #ifndef LLDB_DISABLE_PYTHON
1179 SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) {
1180 if (!type_name.IsValid())
1181 return SBTypeSummary();
1182 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1184 #endif // LLDB_DISABLE_PYTHON
1186 SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) {
1187 if (!type_name.IsValid())
1188 return SBTypeFilter();
1189 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1192 #ifndef LLDB_DISABLE_PYTHON
1193 SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) {
1194 if (!type_name.IsValid())
1195 return SBTypeSynthetic();
1196 return SBTypeSynthetic(
1197 DataVisualization::GetSyntheticForType(type_name.GetSP()));
1199 #endif // LLDB_DISABLE_PYTHON
1201 static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) {
1202 if (categories == nullptr)
1205 while (categories[len] != nullptr)
1207 return llvm::makeArrayRef(categories, len);
1210 bool SBDebugger::EnableLog(const char *channel, const char **categories) {
1212 uint32_t log_options =
1213 LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1215 llvm::raw_string_ostream error_stream(error);
1216 return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
1217 log_options, error_stream);
1222 void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
1225 return m_opaque_sp->SetLoggingCallback(log_callback, baton);