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/SystemInitializerFull.h"
19 #include "lldb/API/SBListener.h"
20 #include "lldb/API/SBBroadcaster.h"
21 #include "lldb/API/SBCommandInterpreter.h"
22 #include "lldb/API/SBCommandReturnObject.h"
23 #include "lldb/API/SBError.h"
24 #include "lldb/API/SBEvent.h"
25 #include "lldb/API/SBFrame.h"
26 #include "lldb/API/SBProcess.h"
27 #include "lldb/API/SBSourceManager.h"
28 #include "lldb/API/SBStream.h"
29 #include "lldb/API/SBStringList.h"
30 #include "lldb/API/SBTarget.h"
31 #include "lldb/API/SBThread.h"
32 #include "lldb/API/SBTypeCategory.h"
33 #include "lldb/API/SBTypeFormat.h"
34 #include "lldb/API/SBTypeFilter.h"
35 #include "lldb/API/SBTypeNameSpecifier.h"
36 #include "lldb/API/SBTypeSummary.h"
37 #include "lldb/API/SBTypeSynthetic.h"
39 #include "lldb/Core/Debugger.h"
40 #include "lldb/Core/State.h"
41 #include "lldb/Core/StreamFile.h"
42 #include "lldb/DataFormatters/DataVisualization.h"
43 #include "lldb/Initialization/SystemLifetimeManager.h"
44 #include "lldb/Interpreter/Args.h"
45 #include "lldb/Interpreter/CommandInterpreter.h"
46 #include "lldb/Interpreter/OptionGroupPlatform.h"
47 #include "lldb/Target/Process.h"
48 #include "lldb/Target/TargetList.h"
50 #include "llvm/ADT/STLExtras.h"
51 #include "llvm/Support/ManagedStatic.h"
52 #include "llvm/Support/DynamicLibrary.h"
55 using namespace lldb_private;
57 static llvm::sys::DynamicLibrary
58 LoadPlugin (const lldb::DebuggerSP &debugger_sp, const FileSpec& spec, Error& error)
60 llvm::sys::DynamicLibrary dynlib = llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
63 typedef bool (*LLDBCommandPluginInit) (lldb::SBDebugger& debugger);
65 lldb::SBDebugger debugger_sb(debugger_sp);
66 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger) function.
67 // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays
68 LLDBCommandPluginInit init_func = (LLDBCommandPluginInit)dynlib.getAddressOfSymbol("_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
71 if (init_func(debugger_sb))
74 error.SetErrorString("plug-in refused to load (lldb::PluginInitialize(lldb::SBDebugger) returned false)");
78 error.SetErrorString("plug-in is missing the required initialization: lldb::PluginInitialize(lldb::SBDebugger)");
84 error.SetErrorString("this file does not represent a loadable dylib");
86 error.SetErrorString("no such file");
88 return llvm::sys::DynamicLibrary();
91 static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;
94 SBInputReader::Initialize(lldb::SBDebugger &sb_debugger,
95 unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction, char const *,
97 void *, lldb::InputReaderGranularity, char const *, char const *, bool)
103 SBInputReader::SetIsDone(bool)
108 SBInputReader::IsActive() const
113 SBDebugger::SBDebugger() = default;
115 SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp) :
116 m_opaque_sp(debugger_sp)
120 SBDebugger::SBDebugger(const SBDebugger &rhs) :
121 m_opaque_sp (rhs.m_opaque_sp)
125 SBDebugger::~SBDebugger() = default;
128 SBDebugger::operator = (const SBDebugger &rhs)
132 m_opaque_sp = rhs.m_opaque_sp;
138 SBDebugger::Initialize ()
140 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
143 log->Printf ("SBDebugger::Initialize ()");
145 g_debugger_lifetime->Initialize(llvm::make_unique<SystemInitializerFull>(), LoadPlugin);
149 SBDebugger::Terminate ()
151 g_debugger_lifetime->Terminate();
157 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
160 log->Printf ("SBDebugger(%p)::Clear ()",
161 static_cast<void*>(m_opaque_sp.get()));
164 m_opaque_sp->ClearIOHandlers ();
172 return SBDebugger::Create(false, nullptr, nullptr);
176 SBDebugger::Create(bool source_init_files)
178 return SBDebugger::Create (source_init_files, nullptr, nullptr);
182 SBDebugger::Create(bool source_init_files, lldb::LogOutputCallback callback, void *baton)
185 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
189 // Currently we have issues if this function is called simultaneously on two different
190 // threads. The issues mainly revolve around the fact that the lldb_private::FormatManager
191 // uses global collections and having two threads parsing the .lldbinit files can cause
192 // mayhem. So to get around this for now we need to use a mutex to prevent bad things
194 static std::recursive_mutex g_mutex;
195 std::lock_guard<std::recursive_mutex> guard(g_mutex);
197 debugger.reset(Debugger::CreateInstance(callback, baton));
202 debugger.GetDescription (sstr);
203 log->Printf ("SBDebugger::Create () => SBDebugger(%p): %s",
204 static_cast<void*>(debugger.m_opaque_sp.get()),
208 SBCommandInterpreter interp = debugger.GetCommandInterpreter();
209 if (source_init_files)
211 interp.get()->SkipLLDBInitFiles(false);
212 interp.get()->SkipAppInitFiles (false);
213 SBCommandReturnObject result;
214 interp.SourceInitFileInHomeDirectory(result);
218 interp.get()->SkipLLDBInitFiles(true);
219 interp.get()->SkipAppInitFiles (true);
225 SBDebugger::Destroy (SBDebugger &debugger)
227 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
232 debugger.GetDescription (sstr);
233 log->Printf ("SBDebugger::Destroy () => SBDebugger(%p): %s",
234 static_cast<void*>(debugger.m_opaque_sp.get()),
238 Debugger::Destroy (debugger.m_opaque_sp);
240 if (debugger.m_opaque_sp.get() != nullptr)
241 debugger.m_opaque_sp.reset();
245 SBDebugger::MemoryPressureDetected ()
247 // Since this function can be call asynchronously, we allow it to be
248 // non-mandatory. We have seen deadlocks with this function when called
249 // so we need to safeguard against this until we can determine what is
250 // causing the deadlocks.
251 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
253 const bool mandatory = false;
256 log->Printf ("SBDebugger::MemoryPressureDetected (), mandatory = %d", mandatory);
259 ModuleList::RemoveOrphanSharedModules(mandatory);
263 SBDebugger::IsValid() const
265 return m_opaque_sp.get() != nullptr;
269 SBDebugger::SetAsync (bool b)
272 m_opaque_sp->SetAsyncExecution(b);
276 SBDebugger::GetAsync()
278 return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false);
282 SBDebugger::SkipLLDBInitFiles (bool b)
285 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles (b);
289 SBDebugger::SkipAppInitFiles (bool b)
292 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles (b);
295 // Shouldn't really be settable after initialization as this could cause lots of problems; don't want users
296 // trying to switch modes in the middle of a debugging session.
298 SBDebugger::SetInputFileHandle (FILE *fh, bool transfer_ownership)
300 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
303 log->Printf ("SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)",
304 static_cast<void*>(m_opaque_sp.get()),
305 static_cast<void*>(fh), transfer_ownership);
308 m_opaque_sp->SetInputFileHandle (fh, transfer_ownership);
312 SBDebugger::SetOutputFileHandle (FILE *fh, bool transfer_ownership)
314 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
318 log->Printf ("SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)",
319 static_cast<void*>(m_opaque_sp.get()),
320 static_cast<void*>(fh), transfer_ownership);
323 m_opaque_sp->SetOutputFileHandle (fh, transfer_ownership);
327 SBDebugger::SetErrorFileHandle (FILE *fh, bool transfer_ownership)
329 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
333 log->Printf ("SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)",
334 static_cast<void*>(m_opaque_sp.get()),
335 static_cast<void*>(fh), transfer_ownership);
338 m_opaque_sp->SetErrorFileHandle (fh, transfer_ownership);
342 SBDebugger::GetInputFileHandle ()
346 StreamFileSP stream_file_sp (m_opaque_sp->GetInputFile());
348 return stream_file_sp->GetFile().GetStream();
354 SBDebugger::GetOutputFileHandle ()
358 StreamFileSP stream_file_sp (m_opaque_sp->GetOutputFile());
360 return stream_file_sp->GetFile().GetStream();
366 SBDebugger::GetErrorFileHandle ()
370 StreamFileSP stream_file_sp(m_opaque_sp->GetErrorFile());
372 return stream_file_sp->GetFile().GetStream();
378 SBDebugger::SaveInputTerminalState()
381 m_opaque_sp->SaveInputTerminalState();
385 SBDebugger::RestoreInputTerminalState()
388 m_opaque_sp->RestoreInputTerminalState();
392 SBDebugger::GetCommandInterpreter ()
394 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
396 SBCommandInterpreter sb_interpreter;
398 sb_interpreter.reset (&m_opaque_sp->GetCommandInterpreter());
401 log->Printf ("SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)",
402 static_cast<void*>(m_opaque_sp.get()),
403 static_cast<void*>(sb_interpreter.get()));
405 return sb_interpreter;
409 SBDebugger::HandleCommand (const char *command)
413 TargetSP target_sp (m_opaque_sp->GetSelectedTarget());
414 std::unique_lock<std::recursive_mutex> lock;
416 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
418 SBCommandInterpreter sb_interpreter(GetCommandInterpreter ());
419 SBCommandReturnObject result;
421 sb_interpreter.HandleCommand (command, result, false);
423 if (GetErrorFileHandle() != nullptr)
424 result.PutError (GetErrorFileHandle());
425 if (GetOutputFileHandle() != nullptr)
426 result.PutOutput (GetOutputFileHandle());
428 if (!m_opaque_sp->GetAsyncExecution())
430 SBProcess process(GetCommandInterpreter().GetProcess ());
431 ProcessSP process_sp (process.GetSP());
435 ListenerSP lldb_listener_sp = m_opaque_sp->GetListener();
436 while (lldb_listener_sp->GetNextEventForBroadcaster (process_sp.get(), event_sp))
438 SBEvent event(event_sp);
439 HandleProcessEvent (process, event, GetOutputFileHandle(), GetErrorFileHandle());
447 SBDebugger::GetListener ()
449 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
451 SBListener sb_listener;
453 sb_listener.reset(m_opaque_sp->GetListener());
456 log->Printf ("SBDebugger(%p)::GetListener () => SBListener(%p)",
457 static_cast<void*>(m_opaque_sp.get()),
458 static_cast<void*>(sb_listener.get()));
464 SBDebugger::HandleProcessEvent (const SBProcess &process, const SBEvent &event, FILE *out, FILE *err)
466 if (!process.IsValid())
469 TargetSP target_sp (process.GetTarget().GetSP());
473 const uint32_t event_type = event.GetType();
474 char stdio_buffer[1024];
477 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
479 if (event_type & (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged))
481 // Drain stdout when we stop just in case we have any bytes
482 while ((len = process.GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0)
484 ::fwrite (stdio_buffer, 1, len, out);
487 if (event_type & (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged))
489 // Drain stderr when we stop just in case we have any bytes
490 while ((len = process.GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0)
492 ::fwrite (stdio_buffer, 1, len, err);
495 if (event_type & Process::eBroadcastBitStateChanged)
497 StateType event_state = SBProcess::GetStateFromEvent (event);
499 if (event_state == eStateInvalid)
502 bool is_stopped = StateIsStoppedState (event_state);
504 process.ReportEventState (event, out);
509 SBDebugger::GetSourceManager ()
511 SBSourceManager sb_source_manager (*this);
512 return sb_source_manager;
516 SBDebugger::GetDefaultArchitecture (char *arch_name, size_t arch_name_len)
518 if (arch_name && arch_name_len)
520 ArchSpec default_arch = Target::GetDefaultArchitecture ();
522 if (default_arch.IsValid())
524 const std::string &triple_str = default_arch.GetTriple().str();
525 if (!triple_str.empty())
526 ::snprintf (arch_name, arch_name_len, "%s", triple_str.c_str());
528 ::snprintf (arch_name, arch_name_len, "%s", default_arch.GetArchitectureName());
532 if (arch_name && arch_name_len)
538 SBDebugger::SetDefaultArchitecture (const char *arch_name)
542 ArchSpec arch (arch_name);
545 Target::SetDefaultArchitecture (arch);
553 SBDebugger::GetScriptingLanguage(const char *script_language_name)
555 return Args::StringToScriptLanguage(script_language_name,
556 eScriptLanguageDefault,
561 SBDebugger::GetVersionString ()
563 return lldb_private::GetVersion();
567 SBDebugger::StateAsCString (StateType state)
569 return lldb_private::StateAsCString (state);
573 SBDebugger::StateIsRunningState (StateType state)
575 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
577 const bool result = lldb_private::StateIsRunningState (state);
579 log->Printf ("SBDebugger::StateIsRunningState (state=%s) => %i",
580 StateAsCString (state), result);
586 SBDebugger::StateIsStoppedState (StateType state)
588 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
590 const bool result = lldb_private::StateIsStoppedState (state, false);
592 log->Printf ("SBDebugger::StateIsStoppedState (state=%s) => %i",
593 StateAsCString (state), result);
599 SBDebugger::CreateTarget (const char *filename,
600 const char *target_triple,
601 const char *platform_name,
602 bool add_dependent_modules,
603 lldb::SBError& sb_error)
610 OptionGroupPlatform platform_options (false);
611 platform_options.SetPlatformName (platform_name);
613 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
616 add_dependent_modules,
620 if (sb_error.Success())
621 sb_target.SetSP (target_sp);
625 sb_error.SetErrorString("invalid debugger");
628 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
630 log->Printf ("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, platform_name=%s, add_dependent_modules=%u, error=%s) => SBTarget(%p)",
631 static_cast<void*>(m_opaque_sp.get()), filename,
632 target_triple, platform_name, add_dependent_modules,
633 sb_error.GetCString(), static_cast<void*>(target_sp.get()));
639 SBDebugger::CreateTargetWithFileAndTargetTriple (const char *filename,
640 const char *target_triple)
646 const bool add_dependent_modules = true;
647 Error error (m_opaque_sp->GetTargetList().CreateTarget(*m_opaque_sp,
650 add_dependent_modules,
653 sb_target.SetSP (target_sp);
656 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
658 log->Printf ("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple (filename=\"%s\", triple=%s) => SBTarget(%p)",
659 static_cast<void*>(m_opaque_sp.get()), filename,
660 target_triple, static_cast<void*>(target_sp.get()));
666 SBDebugger::CreateTargetWithFileAndArch (const char *filename, const char *arch_cstr)
668 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
675 const bool add_dependent_modules = true;
677 error = m_opaque_sp->GetTargetList().CreateTarget(*m_opaque_sp,
680 add_dependent_modules,
686 m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
687 sb_target.SetSP (target_sp);
692 log->Printf ("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", arch=%s) => SBTarget(%p)",
693 static_cast<void*>(m_opaque_sp.get()), filename, arch_cstr,
694 static_cast<void*>(target_sp.get()));
700 SBDebugger::CreateTarget (const char *filename)
707 const bool add_dependent_modules = true;
708 error = m_opaque_sp->GetTargetList().CreateTarget(*m_opaque_sp,
711 add_dependent_modules,
717 m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
718 sb_target.SetSP (target_sp);
721 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
723 log->Printf ("SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
724 static_cast<void*>(m_opaque_sp.get()), filename,
725 static_cast<void*>(target_sp.get()));
730 SBDebugger::DeleteTarget (lldb::SBTarget &target)
735 TargetSP target_sp(target.GetSP());
738 // No need to lock, the target list is thread safe
739 result = m_opaque_sp->GetTargetList().DeleteTarget (target_sp);
740 target_sp->Destroy();
742 const bool mandatory = true;
743 ModuleList::RemoveOrphanSharedModules(mandatory);
747 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
749 log->Printf ("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
750 static_cast<void*>(m_opaque_sp.get()),
751 static_cast<void*>(target.m_opaque_sp.get()), result);
757 SBDebugger::GetTargetAtIndex (uint32_t idx)
762 // No need to lock, the target list is thread safe
763 sb_target.SetSP (m_opaque_sp->GetTargetList().GetTargetAtIndex (idx));
769 SBDebugger::GetIndexOfTarget (lldb::SBTarget target)
772 lldb::TargetSP target_sp = target.GetSP();
779 return m_opaque_sp->GetTargetList().GetIndexOfTarget (target.GetSP());
783 SBDebugger::FindTargetWithProcessID (lldb::pid_t pid)
788 // No need to lock, the target list is thread safe
789 sb_target.SetSP (m_opaque_sp->GetTargetList().FindTargetWithProcessID (pid));
795 SBDebugger::FindTargetWithFileAndArch (const char *filename, const char *arch_name)
798 if (m_opaque_sp && filename && filename[0])
800 // No need to lock, the target list is thread safe
801 ArchSpec arch (arch_name, m_opaque_sp->GetPlatformList().GetSelectedPlatform().get());
802 TargetSP target_sp (m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(FileSpec(filename, false), arch_name ? &arch : nullptr));
803 sb_target.SetSP (target_sp);
809 SBDebugger::FindTargetWithLLDBProcess (const ProcessSP &process_sp)
814 // No need to lock, the target list is thread safe
815 sb_target.SetSP (m_opaque_sp->GetTargetList().FindTargetWithProcess (process_sp.get()));
821 SBDebugger::GetNumTargets ()
825 // No need to lock, the target list is thread safe
826 return m_opaque_sp->GetTargetList().GetNumTargets ();
832 SBDebugger::GetSelectedTarget ()
834 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
840 // No need to lock, the target list is thread safe
841 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget ();
842 sb_target.SetSP (target_sp);
848 sb_target.GetDescription (sstr, eDescriptionLevelBrief);
849 log->Printf ("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
850 static_cast<void*>(m_opaque_sp.get()),
851 static_cast<void*>(target_sp.get()), sstr.GetData());
858 SBDebugger::SetSelectedTarget (SBTarget &sb_target)
860 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
862 TargetSP target_sp (sb_target.GetSP());
865 m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
870 sb_target.GetDescription (sstr, eDescriptionLevelBrief);
871 log->Printf ("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
872 static_cast<void*>(m_opaque_sp.get()),
873 static_cast<void*>(target_sp.get()), sstr.GetData());
878 SBDebugger::GetSelectedPlatform()
880 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
882 SBPlatform sb_platform;
883 DebuggerSP debugger_sp(m_opaque_sp);
886 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
889 log->Printf ("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
890 static_cast<void*>(m_opaque_sp.get()),
891 static_cast<void*>(sb_platform.GetSP().get()),
892 sb_platform.GetName());
897 SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform)
899 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
901 DebuggerSP debugger_sp(m_opaque_sp);
904 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
908 log->Printf ("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
909 static_cast<void*>(m_opaque_sp.get()),
910 static_cast<void*>(sb_platform.GetSP().get()),
911 sb_platform.GetName());
915 SBDebugger::DispatchInput (void* baton, const void *data, size_t data_len)
917 DispatchInput (data,data_len);
921 SBDebugger::DispatchInput (const void *data, size_t data_len)
923 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
926 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\", size_t=%" PRIu64 ")",
927 // m_opaque_sp.get(),
929 // (const char *) data,
930 // (uint64_t)data_len);
933 // m_opaque_sp->DispatchInput ((const char *) data, data_len);
937 SBDebugger::DispatchInputInterrupt ()
940 m_opaque_sp->DispatchInputInterrupt ();
944 SBDebugger::DispatchInputEndOfFile ()
947 m_opaque_sp->DispatchInputEndOfFile ();
951 SBDebugger::PushInputReader (SBInputReader &reader)
956 SBDebugger::RunCommandInterpreter (bool auto_handle_events,
961 CommandInterpreterRunOptions options;
963 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(auto_handle_events,
970 SBDebugger::RunCommandInterpreter (bool auto_handle_events,
972 SBCommandInterpreterRunOptions &options,
974 bool &quit_requested,
975 bool &stopped_for_crash)
980 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
981 interp.RunCommandInterpreter(auto_handle_events, spawn_thread, options.ref());
982 num_errors = interp.GetNumErrors();
983 quit_requested = interp.GetQuitRequested();
984 stopped_for_crash = interp.GetStoppedForCrash();
989 SBDebugger::RunREPL (lldb::LanguageType language, const char *repl_options)
993 error.ref() = m_opaque_sp->RunREPL(language, repl_options);
995 error.SetErrorString ("invalid debugger");
1000 SBDebugger::reset (const DebuggerSP &debugger_sp)
1002 m_opaque_sp = debugger_sp;
1006 SBDebugger::get () const
1008 return m_opaque_sp.get();
1012 SBDebugger::ref () const
1014 assert (m_opaque_sp.get());
1015 return *m_opaque_sp;
1018 const lldb::DebuggerSP &
1019 SBDebugger::get_sp () const
1025 SBDebugger::FindDebuggerWithID (int id)
1027 // No need to lock, the debugger list is thread safe
1028 SBDebugger sb_debugger;
1029 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID (id);
1031 sb_debugger.reset (debugger_sp);
1036 SBDebugger::GetInstanceName()
1038 return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr);
1042 SBDebugger::SetInternalVariable (const char *var_name, const char *value, const char *debugger_instance_name)
1045 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name)));
1049 ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext());
1050 error = debugger_sp->SetPropertyValue (&exe_ctx,
1051 eVarSetOperationAssign,
1057 error.SetErrorStringWithFormat ("invalid debugger instance name '%s'", debugger_instance_name);
1060 sb_error.SetError(error);
1065 SBDebugger::GetInternalVariableValue (const char *var_name, const char *debugger_instance_name)
1067 SBStringList ret_value;
1068 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name)));
1072 ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext());
1073 lldb::OptionValueSP value_sp (debugger_sp->GetPropertyValue (&exe_ctx,
1079 StreamString value_strm;
1080 value_sp->DumpValue (&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
1081 const std::string &value_str = value_strm.GetString();
1082 if (!value_str.empty())
1084 StringList string_list;
1085 string_list.SplitIntoLines(value_str);
1086 return SBStringList(&string_list);
1090 return SBStringList();
1094 SBDebugger::GetTerminalWidth() const
1096 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
1100 SBDebugger::SetTerminalWidth (uint32_t term_width)
1103 m_opaque_sp->SetTerminalWidth (term_width);
1107 SBDebugger::GetPrompt() const
1109 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1112 log->Printf ("SBDebugger(%p)::GetPrompt () => \"%s\"",
1113 static_cast<void*>(m_opaque_sp.get()),
1114 (m_opaque_sp ? m_opaque_sp->GetPrompt() : ""));
1116 return (m_opaque_sp ? m_opaque_sp->GetPrompt() : nullptr);
1120 SBDebugger::SetPrompt (const char *prompt)
1123 m_opaque_sp->SetPrompt (prompt);
1127 SBDebugger::GetScriptLanguage() const
1129 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
1133 SBDebugger::SetScriptLanguage (ScriptLanguage script_lang)
1137 m_opaque_sp->SetScriptLanguage (script_lang);
1142 SBDebugger::SetUseExternalEditor(bool value)
1144 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
1148 SBDebugger::GetUseExternalEditor()
1150 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
1154 SBDebugger::SetUseColor(bool value)
1156 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
1160 SBDebugger::GetUseColor() const
1162 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
1166 SBDebugger::GetDescription (SBStream &description)
1168 Stream &strm = description.ref();
1172 const char *name = m_opaque_sp->GetInstanceName().AsCString();
1173 user_id_t id = m_opaque_sp->GetID();
1174 strm.Printf ("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1177 strm.PutCString ("No value");
1185 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
1189 SBDebugger::SetCurrentPlatform (const char *platform_name_cstr)
1194 if (platform_name_cstr && platform_name_cstr[0])
1196 ConstString platform_name (platform_name_cstr);
1197 PlatformSP platform_sp (Platform::Find (platform_name));
1201 // Already have a platform with this name, just select it
1202 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
1206 // We don't have a platform by this name yet, create one
1207 platform_sp = Platform::Create (platform_name, sb_error.ref());
1210 // We created the platform, now append and select it
1211 bool make_selected = true;
1212 m_opaque_sp->GetPlatformList().Append (platform_sp, make_selected);
1218 sb_error.ref().SetErrorString("invalid platform name");
1223 sb_error.ref().SetErrorString("invalid debugger");
1229 SBDebugger::SetCurrentPlatformSDKRoot (const char *sysroot)
1233 PlatformSP platform_sp (m_opaque_sp->GetPlatformList().GetSelectedPlatform());
1237 platform_sp->SetSDKRootDirectory (ConstString (sysroot));
1245 SBDebugger::GetCloseInputOnEOF() const
1247 return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false);
1251 SBDebugger::SetCloseInputOnEOF (bool b)
1254 m_opaque_sp->SetCloseInputOnEOF (b);
1258 SBDebugger::GetCategory (const char* category_name)
1260 if (!category_name || *category_name == 0)
1261 return SBTypeCategory();
1263 TypeCategoryImplSP category_sp;
1265 if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, false))
1266 return SBTypeCategory(category_sp);
1268 return SBTypeCategory();
1272 SBDebugger::GetCategory (lldb::LanguageType lang_type)
1274 TypeCategoryImplSP category_sp;
1275 if (DataVisualization::Categories::GetCategory(lang_type, category_sp))
1276 return SBTypeCategory(category_sp);
1278 return SBTypeCategory();
1282 SBDebugger::CreateCategory (const char* category_name)
1284 if (!category_name || *category_name == 0)
1285 return SBTypeCategory();
1287 TypeCategoryImplSP category_sp;
1289 if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, true))
1290 return SBTypeCategory(category_sp);
1292 return SBTypeCategory();
1296 SBDebugger::DeleteCategory (const char* category_name)
1298 if (!category_name || *category_name == 0)
1301 return DataVisualization::Categories::Delete(ConstString(category_name));
1305 SBDebugger::GetNumCategories()
1307 return DataVisualization::Categories::GetCount();
1311 SBDebugger::GetCategoryAtIndex (uint32_t index)
1313 return SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index));
1317 SBDebugger::GetDefaultCategory()
1319 return GetCategory("default");
1323 SBDebugger::GetFormatForType (SBTypeNameSpecifier type_name)
1325 SBTypeCategory default_category_sb = GetDefaultCategory();
1326 if (default_category_sb.GetEnabled())
1327 return default_category_sb.GetFormatForType(type_name);
1328 return SBTypeFormat();
1331 #ifndef LLDB_DISABLE_PYTHON
1333 SBDebugger::GetSummaryForType (SBTypeNameSpecifier type_name)
1335 if (!type_name.IsValid())
1336 return SBTypeSummary();
1337 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1339 #endif // LLDB_DISABLE_PYTHON
1342 SBDebugger::GetFilterForType (SBTypeNameSpecifier type_name)
1344 if (!type_name.IsValid())
1345 return SBTypeFilter();
1346 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1349 #ifndef LLDB_DISABLE_PYTHON
1351 SBDebugger::GetSyntheticForType (SBTypeNameSpecifier type_name)
1353 if (!type_name.IsValid())
1354 return SBTypeSynthetic();
1355 return SBTypeSynthetic(DataVisualization::GetSyntheticForType(type_name.GetSP()));
1357 #endif // LLDB_DISABLE_PYTHON
1360 SBDebugger::EnableLog (const char *channel, const char **categories)
1364 uint32_t log_options = LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1365 StreamString errors;
1366 return m_opaque_sp->EnableLog(channel, categories, nullptr, log_options, errors);
1373 SBDebugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1377 return m_opaque_sp->SetLoggingCallback (log_callback, baton);