[lldb][dwarf] Compute fully qualified names on simplified template names with DWARFT...
[llvm-project.git] / lldb / source / API / SBDebugger.cpp
blob4efec747aacff1781f52fa36c0afd3c4a54197e4
1 //===-- SBDebugger.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBDebugger.h"
10 #include "SystemInitializerFull.h"
11 #include "lldb/Utility/Instrumentation.h"
12 #include "lldb/Utility/LLDBLog.h"
14 #include "lldb/API/SBBroadcaster.h"
15 #include "lldb/API/SBCommandInterpreter.h"
16 #include "lldb/API/SBCommandInterpreterRunOptions.h"
17 #include "lldb/API/SBCommandReturnObject.h"
18 #include "lldb/API/SBError.h"
19 #include "lldb/API/SBEvent.h"
20 #include "lldb/API/SBFile.h"
21 #include "lldb/API/SBFrame.h"
22 #include "lldb/API/SBListener.h"
23 #include "lldb/API/SBProcess.h"
24 #include "lldb/API/SBSourceManager.h"
25 #include "lldb/API/SBStream.h"
26 #include "lldb/API/SBStringList.h"
27 #include "lldb/API/SBStructuredData.h"
28 #include "lldb/API/SBTarget.h"
29 #include "lldb/API/SBThread.h"
30 #include "lldb/API/SBTrace.h"
31 #include "lldb/API/SBTypeCategory.h"
32 #include "lldb/API/SBTypeFilter.h"
33 #include "lldb/API/SBTypeFormat.h"
34 #include "lldb/API/SBTypeNameSpecifier.h"
35 #include "lldb/API/SBTypeSummary.h"
36 #include "lldb/API/SBTypeSynthetic.h"
38 #include "lldb/Core/Debugger.h"
39 #include "lldb/Core/DebuggerEvents.h"
40 #include "lldb/Core/PluginManager.h"
41 #include "lldb/Core/Progress.h"
42 #include "lldb/Core/StructuredDataImpl.h"
43 #include "lldb/DataFormatters/DataVisualization.h"
44 #include "lldb/Host/Config.h"
45 #include "lldb/Host/StreamFile.h"
46 #include "lldb/Host/XML.h"
47 #include "lldb/Initialization/SystemLifetimeManager.h"
48 #include "lldb/Interpreter/CommandInterpreter.h"
49 #include "lldb/Interpreter/OptionArgParser.h"
50 #include "lldb/Interpreter/OptionGroupPlatform.h"
51 #include "lldb/Target/Process.h"
52 #include "lldb/Target/TargetList.h"
53 #include "lldb/Utility/Args.h"
54 #include "lldb/Utility/Diagnostics.h"
55 #include "lldb/Utility/State.h"
56 #include "lldb/Version/Version.h"
58 #include "llvm/ADT/STLExtras.h"
59 #include "llvm/ADT/StringRef.h"
60 #include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_CURL
61 #include "llvm/Support/DynamicLibrary.h"
62 #include "llvm/Support/ManagedStatic.h"
63 #include "llvm/Support/PrettyStackTrace.h"
64 #include "llvm/Support/Signals.h"
66 using namespace lldb;
67 using namespace lldb_private;
69 static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;
71 SBError SBInputReader::Initialize(
72 lldb::SBDebugger &sb_debugger,
73 unsigned long (*callback)(void *, lldb::SBInputReader *,
74 lldb::InputReaderAction, char const *,
75 unsigned long),
76 void *a, lldb::InputReaderGranularity b, char const *c, char const *d,
77 bool e) {
78 LLDB_INSTRUMENT_VA(this, sb_debugger, callback, a, b, c, d, e);
80 return SBError();
83 void SBInputReader::SetIsDone(bool b) { LLDB_INSTRUMENT_VA(this, b); }
85 bool SBInputReader::IsActive() const {
86 LLDB_INSTRUMENT_VA(this);
88 return false;
91 SBDebugger::SBDebugger() { LLDB_INSTRUMENT_VA(this); }
93 SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp)
94 : m_opaque_sp(debugger_sp) {
95 LLDB_INSTRUMENT_VA(this, debugger_sp);
98 SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
99 LLDB_INSTRUMENT_VA(this, rhs);
102 SBDebugger::~SBDebugger() = default;
104 SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) {
105 LLDB_INSTRUMENT_VA(this, rhs);
107 if (this != &rhs) {
108 m_opaque_sp = rhs.m_opaque_sp;
110 return *this;
113 const char *SBDebugger::GetBroadcasterClass() {
114 LLDB_INSTRUMENT();
116 return ConstString(Debugger::GetStaticBroadcasterClass()).AsCString();
119 const char *SBDebugger::GetProgressFromEvent(const lldb::SBEvent &event,
120 uint64_t &progress_id,
121 uint64_t &completed,
122 uint64_t &total,
123 bool &is_debugger_specific) {
124 LLDB_INSTRUMENT_VA(event);
126 const ProgressEventData *progress_data =
127 ProgressEventData::GetEventDataFromEvent(event.get());
128 if (progress_data == nullptr)
129 return nullptr;
130 progress_id = progress_data->GetID();
131 completed = progress_data->GetCompleted();
132 total = progress_data->GetTotal();
133 is_debugger_specific = progress_data->IsDebuggerSpecific();
134 ConstString message(progress_data->GetMessage());
135 return message.AsCString();
138 lldb::SBStructuredData
139 SBDebugger::GetProgressDataFromEvent(const lldb::SBEvent &event) {
140 LLDB_INSTRUMENT_VA(event);
142 StructuredData::DictionarySP dictionary_sp =
143 ProgressEventData::GetAsStructuredData(event.get());
145 if (!dictionary_sp)
146 return {};
148 SBStructuredData data;
149 data.m_impl_up->SetObjectSP(std::move(dictionary_sp));
150 return data;
153 lldb::SBStructuredData
154 SBDebugger::GetDiagnosticFromEvent(const lldb::SBEvent &event) {
155 LLDB_INSTRUMENT_VA(event);
157 StructuredData::DictionarySP dictionary_sp =
158 DiagnosticEventData::GetAsStructuredData(event.get());
160 if (!dictionary_sp)
161 return {};
163 SBStructuredData data;
164 data.m_impl_up->SetObjectSP(std::move(dictionary_sp));
165 return data;
168 SBBroadcaster SBDebugger::GetBroadcaster() {
169 LLDB_INSTRUMENT_VA(this);
170 SBBroadcaster broadcaster(&m_opaque_sp->GetBroadcaster(), false);
171 return broadcaster;
174 void SBDebugger::Initialize() {
175 LLDB_INSTRUMENT();
176 SBError ignored = SBDebugger::InitializeWithErrorHandling();
179 lldb::SBError SBDebugger::InitializeWithErrorHandling() {
180 LLDB_INSTRUMENT();
182 auto LoadPlugin = [](const lldb::DebuggerSP &debugger_sp,
183 const FileSpec &spec,
184 Status &error) -> llvm::sys::DynamicLibrary {
185 llvm::sys::DynamicLibrary dynlib =
186 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
187 if (dynlib.isValid()) {
188 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger);
190 lldb::SBDebugger debugger_sb(debugger_sp);
191 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
192 // function.
193 // TODO: mangle this differently for your system - on OSX, the first
194 // underscore needs to be removed and the second one stays
195 LLDBCommandPluginInit init_func =
196 (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
197 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
198 if (init_func) {
199 if (init_func(debugger_sb))
200 return dynlib;
201 else
202 error = Status::FromErrorString(
203 "plug-in refused to load "
204 "(lldb::PluginInitialize(lldb::SBDebugger) "
205 "returned false)");
206 } else {
207 error = Status::FromErrorString(
208 "plug-in is missing the required initialization: "
209 "lldb::PluginInitialize(lldb::SBDebugger)");
211 } else {
212 if (FileSystem::Instance().Exists(spec))
213 error = Status::FromErrorString(
214 "this file does not represent a loadable dylib");
215 else
216 error = Status::FromErrorString("no such file");
218 return llvm::sys::DynamicLibrary();
221 SBError error;
222 if (auto e = g_debugger_lifetime->Initialize(
223 std::make_unique<SystemInitializerFull>(), LoadPlugin)) {
224 error.SetError(Status::FromError(std::move(e)));
226 return error;
229 void SBDebugger::PrintStackTraceOnError() {
230 LLDB_INSTRUMENT();
232 llvm::EnablePrettyStackTrace();
233 static std::string executable =
234 llvm::sys::fs::getMainExecutable(nullptr, nullptr);
235 llvm::sys::PrintStackTraceOnErrorSignal(executable);
238 static void DumpDiagnostics(void *cookie) {
239 Diagnostics::Instance().Dump(llvm::errs());
242 void SBDebugger::PrintDiagnosticsOnError() {
243 LLDB_INSTRUMENT();
245 llvm::sys::AddSignalHandler(&DumpDiagnostics, nullptr);
248 void SBDebugger::Terminate() {
249 LLDB_INSTRUMENT();
251 g_debugger_lifetime->Terminate();
254 void SBDebugger::Clear() {
255 LLDB_INSTRUMENT_VA(this);
257 if (m_opaque_sp)
258 m_opaque_sp->ClearIOHandlers();
260 m_opaque_sp.reset();
263 SBDebugger SBDebugger::Create() {
264 LLDB_INSTRUMENT();
266 return SBDebugger::Create(false, nullptr, nullptr);
269 SBDebugger SBDebugger::Create(bool source_init_files) {
270 LLDB_INSTRUMENT_VA(source_init_files);
272 return SBDebugger::Create(source_init_files, nullptr, nullptr);
275 SBDebugger SBDebugger::Create(bool source_init_files,
276 lldb::LogOutputCallback callback, void *baton)
279 LLDB_INSTRUMENT_VA(source_init_files, callback, baton);
281 SBDebugger debugger;
283 // Currently we have issues if this function is called simultaneously on two
284 // different threads. The issues mainly revolve around the fact that the
285 // lldb_private::FormatManager uses global collections and having two threads
286 // parsing the .lldbinit files can cause mayhem. So to get around this for
287 // now we need to use a mutex to prevent bad things from happening.
288 static std::recursive_mutex g_mutex;
289 std::lock_guard<std::recursive_mutex> guard(g_mutex);
291 debugger.reset(Debugger::CreateInstance(callback, baton));
293 SBCommandInterpreter interp = debugger.GetCommandInterpreter();
294 if (source_init_files) {
295 interp.get()->SkipLLDBInitFiles(false);
296 interp.get()->SkipAppInitFiles(false);
297 SBCommandReturnObject result;
298 interp.SourceInitFileInGlobalDirectory(result);
299 interp.SourceInitFileInHomeDirectory(result, false);
300 } else {
301 interp.get()->SkipLLDBInitFiles(true);
302 interp.get()->SkipAppInitFiles(true);
304 return debugger;
307 void SBDebugger::Destroy(SBDebugger &debugger) {
308 LLDB_INSTRUMENT_VA(debugger);
310 Debugger::Destroy(debugger.m_opaque_sp);
312 if (debugger.m_opaque_sp.get() != nullptr)
313 debugger.m_opaque_sp.reset();
316 void SBDebugger::MemoryPressureDetected() {
317 LLDB_INSTRUMENT();
319 // Since this function can be call asynchronously, we allow it to be non-
320 // mandatory. We have seen deadlocks with this function when called so we
321 // need to safeguard against this until we can determine what is causing the
322 // deadlocks.
324 const bool mandatory = false;
326 ModuleList::RemoveOrphanSharedModules(mandatory);
329 bool SBDebugger::IsValid() const {
330 LLDB_INSTRUMENT_VA(this);
331 return this->operator bool();
333 SBDebugger::operator bool() const {
334 LLDB_INSTRUMENT_VA(this);
336 return m_opaque_sp.get() != nullptr;
339 void SBDebugger::SetAsync(bool b) {
340 LLDB_INSTRUMENT_VA(this, b);
342 if (m_opaque_sp)
343 m_opaque_sp->SetAsyncExecution(b);
346 bool SBDebugger::GetAsync() {
347 LLDB_INSTRUMENT_VA(this);
349 return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false);
352 void SBDebugger::SkipLLDBInitFiles(bool b) {
353 LLDB_INSTRUMENT_VA(this, b);
355 if (m_opaque_sp)
356 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b);
359 void SBDebugger::SkipAppInitFiles(bool b) {
360 LLDB_INSTRUMENT_VA(this, b);
362 if (m_opaque_sp)
363 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b);
366 void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) {
367 LLDB_INSTRUMENT_VA(this, fh, transfer_ownership);
368 if (m_opaque_sp)
369 m_opaque_sp->SetInputFile(
370 (FileSP)std::make_shared<NativeFile>(fh, transfer_ownership));
373 SBError SBDebugger::SetInputString(const char *data) {
374 LLDB_INSTRUMENT_VA(this, data);
375 SBError sb_error;
376 if (data == nullptr) {
377 sb_error = Status::FromErrorString("String data is null");
378 return sb_error;
381 size_t size = strlen(data);
382 if (size == 0) {
383 sb_error = Status::FromErrorString("String data is empty");
384 return sb_error;
387 if (!m_opaque_sp) {
388 sb_error = Status::FromErrorString("invalid debugger");
389 return sb_error;
392 sb_error.SetError(m_opaque_sp->SetInputString(data));
393 return sb_error;
396 // Shouldn't really be settable after initialization as this could cause lots
397 // of problems; don't want users trying to switch modes in the middle of a
398 // debugging session.
399 SBError SBDebugger::SetInputFile(SBFile file) {
400 LLDB_INSTRUMENT_VA(this, file);
402 SBError error;
403 if (!m_opaque_sp) {
404 error.ref() = Status::FromErrorString("invalid debugger");
405 return error;
407 if (!file) {
408 error.ref() = Status::FromErrorString("invalid file");
409 return error;
411 m_opaque_sp->SetInputFile(file.m_opaque_sp);
412 return error;
415 SBError SBDebugger::SetInputFile(FileSP file_sp) {
416 LLDB_INSTRUMENT_VA(this, file_sp);
417 return SetInputFile(SBFile(file_sp));
420 SBError SBDebugger::SetOutputFile(FileSP file_sp) {
421 LLDB_INSTRUMENT_VA(this, file_sp);
422 return SetOutputFile(SBFile(file_sp));
425 void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) {
426 LLDB_INSTRUMENT_VA(this, fh, transfer_ownership);
427 SetOutputFile((FileSP)std::make_shared<NativeFile>(fh, transfer_ownership));
430 SBError SBDebugger::SetOutputFile(SBFile file) {
431 LLDB_INSTRUMENT_VA(this, file);
432 SBError error;
433 if (!m_opaque_sp) {
434 error.ref() = Status::FromErrorString("invalid debugger");
435 return error;
437 if (!file) {
438 error.ref() = Status::FromErrorString("invalid file");
439 return error;
441 m_opaque_sp->SetOutputFile(file.m_opaque_sp);
442 return error;
445 void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) {
446 LLDB_INSTRUMENT_VA(this, fh, transfer_ownership);
447 SetErrorFile((FileSP)std::make_shared<NativeFile>(fh, transfer_ownership));
450 SBError SBDebugger::SetErrorFile(FileSP file_sp) {
451 LLDB_INSTRUMENT_VA(this, file_sp);
452 return SetErrorFile(SBFile(file_sp));
455 SBError SBDebugger::SetErrorFile(SBFile file) {
456 LLDB_INSTRUMENT_VA(this, file);
457 SBError error;
458 if (!m_opaque_sp) {
459 error.ref() = Status::FromErrorString("invalid debugger");
460 return error;
462 if (!file) {
463 error.ref() = Status::FromErrorString("invalid file");
464 return error;
466 m_opaque_sp->SetErrorFile(file.m_opaque_sp);
467 return error;
470 lldb::SBStructuredData SBDebugger::GetSetting(const char *setting) {
471 LLDB_INSTRUMENT_VA(this, setting);
473 SBStructuredData data;
474 if (!m_opaque_sp)
475 return data;
477 StreamString json_strm;
478 ExecutionContext exe_ctx(
479 m_opaque_sp->GetCommandInterpreter().GetExecutionContext());
480 if (setting && strlen(setting) > 0)
481 m_opaque_sp->DumpPropertyValue(&exe_ctx, json_strm, setting,
482 /*dump_mask*/ 0,
483 /*is_json*/ true);
484 else
485 m_opaque_sp->DumpAllPropertyValues(&exe_ctx, json_strm, /*dump_mask*/ 0,
486 /*is_json*/ true);
488 data.m_impl_up->SetObjectSP(StructuredData::ParseJSON(json_strm.GetString()));
489 return data;
492 FILE *SBDebugger::GetInputFileHandle() {
493 LLDB_INSTRUMENT_VA(this);
494 if (m_opaque_sp) {
495 File &file_sp = m_opaque_sp->GetInputFile();
496 return file_sp.GetStream();
498 return nullptr;
501 SBFile SBDebugger::GetInputFile() {
502 LLDB_INSTRUMENT_VA(this);
503 if (m_opaque_sp) {
504 return SBFile(m_opaque_sp->GetInputFileSP());
506 return SBFile();
509 FILE *SBDebugger::GetOutputFileHandle() {
510 LLDB_INSTRUMENT_VA(this);
511 if (m_opaque_sp) {
512 StreamFile &stream_file = m_opaque_sp->GetOutputStream();
513 return stream_file.GetFile().GetStream();
515 return nullptr;
518 SBFile SBDebugger::GetOutputFile() {
519 LLDB_INSTRUMENT_VA(this);
520 if (m_opaque_sp) {
521 SBFile file(m_opaque_sp->GetOutputStream().GetFileSP());
522 return file;
524 return SBFile();
527 FILE *SBDebugger::GetErrorFileHandle() {
528 LLDB_INSTRUMENT_VA(this);
530 if (m_opaque_sp) {
531 StreamFile &stream_file = m_opaque_sp->GetErrorStream();
532 return stream_file.GetFile().GetStream();
534 return nullptr;
537 SBFile SBDebugger::GetErrorFile() {
538 LLDB_INSTRUMENT_VA(this);
539 SBFile file;
540 if (m_opaque_sp) {
541 SBFile file(m_opaque_sp->GetErrorStream().GetFileSP());
542 return file;
544 return SBFile();
547 void SBDebugger::SaveInputTerminalState() {
548 LLDB_INSTRUMENT_VA(this);
550 if (m_opaque_sp)
551 m_opaque_sp->SaveInputTerminalState();
554 void SBDebugger::RestoreInputTerminalState() {
555 LLDB_INSTRUMENT_VA(this);
557 if (m_opaque_sp)
558 m_opaque_sp->RestoreInputTerminalState();
560 SBCommandInterpreter SBDebugger::GetCommandInterpreter() {
561 LLDB_INSTRUMENT_VA(this);
563 SBCommandInterpreter sb_interpreter;
564 if (m_opaque_sp)
565 sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter());
567 return sb_interpreter;
570 void SBDebugger::HandleCommand(const char *command) {
571 LLDB_INSTRUMENT_VA(this, command);
573 if (m_opaque_sp) {
574 TargetSP target_sp(m_opaque_sp->GetSelectedTarget());
575 std::unique_lock<std::recursive_mutex> lock;
576 if (target_sp)
577 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
579 SBCommandInterpreter sb_interpreter(GetCommandInterpreter());
580 SBCommandReturnObject result;
582 sb_interpreter.HandleCommand(command, result, false);
584 result.PutError(m_opaque_sp->GetErrorStream().GetFileSP());
585 result.PutOutput(m_opaque_sp->GetOutputStream().GetFileSP());
587 if (!m_opaque_sp->GetAsyncExecution()) {
588 SBProcess process(GetCommandInterpreter().GetProcess());
589 ProcessSP process_sp(process.GetSP());
590 if (process_sp) {
591 EventSP event_sp;
592 ListenerSP lldb_listener_sp = m_opaque_sp->GetListener();
593 while (lldb_listener_sp->GetEventForBroadcaster(
594 process_sp.get(), event_sp, std::chrono::seconds(0))) {
595 SBEvent event(event_sp);
596 HandleProcessEvent(process, event, GetOutputFile(), GetErrorFile());
603 SBListener SBDebugger::GetListener() {
604 LLDB_INSTRUMENT_VA(this);
606 SBListener sb_listener;
607 if (m_opaque_sp)
608 sb_listener.reset(m_opaque_sp->GetListener());
610 return sb_listener;
613 void SBDebugger::HandleProcessEvent(const SBProcess &process,
614 const SBEvent &event, SBFile out,
615 SBFile err) {
616 LLDB_INSTRUMENT_VA(this, process, event, out, err);
618 return HandleProcessEvent(process, event, out.m_opaque_sp, err.m_opaque_sp);
621 void SBDebugger::HandleProcessEvent(const SBProcess &process,
622 const SBEvent &event, FILE *out,
623 FILE *err) {
624 LLDB_INSTRUMENT_VA(this, process, event, out, err);
626 FileSP outfile = std::make_shared<NativeFile>(out, false);
627 FileSP errfile = std::make_shared<NativeFile>(err, false);
628 return HandleProcessEvent(process, event, outfile, errfile);
631 void SBDebugger::HandleProcessEvent(const SBProcess &process,
632 const SBEvent &event, FileSP out_sp,
633 FileSP err_sp) {
635 LLDB_INSTRUMENT_VA(this, process, event, out_sp, err_sp);
637 if (!process.IsValid())
638 return;
640 TargetSP target_sp(process.GetTarget().GetSP());
641 if (!target_sp)
642 return;
644 const uint32_t event_type = event.GetType();
645 char stdio_buffer[1024];
646 size_t len;
648 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
650 if (event_type &
651 (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) {
652 // Drain stdout when we stop just in case we have any bytes
653 while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0)
654 if (out_sp)
655 out_sp->Write(stdio_buffer, len);
658 if (event_type &
659 (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) {
660 // Drain stderr when we stop just in case we have any bytes
661 while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0)
662 if (err_sp)
663 err_sp->Write(stdio_buffer, len);
666 if (event_type & Process::eBroadcastBitStateChanged) {
667 StateType event_state = SBProcess::GetStateFromEvent(event);
669 if (event_state == eStateInvalid)
670 return;
672 bool is_stopped = StateIsStoppedState(event_state);
673 if (!is_stopped)
674 process.ReportEventState(event, out_sp);
678 SBSourceManager SBDebugger::GetSourceManager() {
679 LLDB_INSTRUMENT_VA(this);
681 SBSourceManager sb_source_manager(*this);
682 return sb_source_manager;
685 bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) {
686 LLDB_INSTRUMENT_VA(arch_name, arch_name_len);
688 if (arch_name && arch_name_len) {
689 ArchSpec default_arch = Target::GetDefaultArchitecture();
691 if (default_arch.IsValid()) {
692 const std::string &triple_str = default_arch.GetTriple().str();
693 if (!triple_str.empty())
694 ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str());
695 else
696 ::snprintf(arch_name, arch_name_len, "%s",
697 default_arch.GetArchitectureName());
698 return true;
701 if (arch_name && arch_name_len)
702 arch_name[0] = '\0';
703 return false;
706 bool SBDebugger::SetDefaultArchitecture(const char *arch_name) {
707 LLDB_INSTRUMENT_VA(arch_name);
709 if (arch_name) {
710 ArchSpec arch(arch_name);
711 if (arch.IsValid()) {
712 Target::SetDefaultArchitecture(arch);
713 return true;
716 return false;
719 ScriptLanguage
720 SBDebugger::GetScriptingLanguage(const char *script_language_name) {
721 LLDB_INSTRUMENT_VA(this, script_language_name);
723 if (!script_language_name)
724 return eScriptLanguageDefault;
725 return OptionArgParser::ToScriptLanguage(
726 llvm::StringRef(script_language_name), eScriptLanguageDefault, nullptr);
729 SBStructuredData
730 SBDebugger::GetScriptInterpreterInfo(lldb::ScriptLanguage language) {
731 LLDB_INSTRUMENT_VA(this, language);
732 SBStructuredData data;
733 if (m_opaque_sp) {
734 lldb_private::ScriptInterpreter *interp =
735 m_opaque_sp->GetScriptInterpreter(language);
736 if (interp) {
737 data.m_impl_up->SetObjectSP(interp->GetInterpreterInfo());
740 return data;
743 const char *SBDebugger::GetVersionString() {
744 LLDB_INSTRUMENT();
746 return lldb_private::GetVersion();
749 const char *SBDebugger::StateAsCString(StateType state) {
750 LLDB_INSTRUMENT_VA(state);
752 return lldb_private::StateAsCString(state);
755 static void AddBoolConfigEntry(StructuredData::Dictionary &dict,
756 llvm::StringRef name, bool value,
757 llvm::StringRef description) {
758 auto entry_up = std::make_unique<StructuredData::Dictionary>();
759 entry_up->AddBooleanItem("value", value);
760 entry_up->AddStringItem("description", description);
761 dict.AddItem(name, std::move(entry_up));
764 static void AddLLVMTargets(StructuredData::Dictionary &dict) {
765 auto array_up = std::make_unique<StructuredData::Array>();
766 #define LLVM_TARGET(target) \
767 array_up->AddItem(std::make_unique<StructuredData::String>(#target));
768 #include "llvm/Config/Targets.def"
769 auto entry_up = std::make_unique<StructuredData::Dictionary>();
770 entry_up->AddItem("value", std::move(array_up));
771 entry_up->AddStringItem("description", "A list of configured LLVM targets.");
772 dict.AddItem("targets", std::move(entry_up));
775 SBStructuredData SBDebugger::GetBuildConfiguration() {
776 LLDB_INSTRUMENT();
778 auto config_up = std::make_unique<StructuredData::Dictionary>();
779 AddBoolConfigEntry(
780 *config_up, "xml", XMLDocument::XMLEnabled(),
781 "A boolean value that indicates if XML support is enabled in LLDB");
782 AddBoolConfigEntry(
783 *config_up, "curl", LLVM_ENABLE_CURL,
784 "A boolean value that indicates if CURL support is enabled in LLDB");
785 AddBoolConfigEntry(
786 *config_up, "curses", LLDB_ENABLE_CURSES,
787 "A boolean value that indicates if curses support is enabled in LLDB");
788 AddBoolConfigEntry(
789 *config_up, "editline", LLDB_ENABLE_LIBEDIT,
790 "A boolean value that indicates if editline support is enabled in LLDB");
791 AddBoolConfigEntry(*config_up, "editline_wchar", LLDB_EDITLINE_USE_WCHAR,
792 "A boolean value that indicates if editline wide "
793 "characters support is enabled in LLDB");
794 AddBoolConfigEntry(
795 *config_up, "lzma", LLDB_ENABLE_LZMA,
796 "A boolean value that indicates if lzma support is enabled in LLDB");
797 AddBoolConfigEntry(
798 *config_up, "python", LLDB_ENABLE_PYTHON,
799 "A boolean value that indicates if python support is enabled in LLDB");
800 AddBoolConfigEntry(
801 *config_up, "lua", LLDB_ENABLE_LUA,
802 "A boolean value that indicates if lua support is enabled in LLDB");
803 AddBoolConfigEntry(*config_up, "fbsdvmcore", LLDB_ENABLE_FBSDVMCORE,
804 "A boolean value that indicates if fbsdvmcore support is "
805 "enabled in LLDB");
806 AddLLVMTargets(*config_up);
808 SBStructuredData data;
809 data.m_impl_up->SetObjectSP(std::move(config_up));
810 return data;
813 bool SBDebugger::StateIsRunningState(StateType state) {
814 LLDB_INSTRUMENT_VA(state);
816 const bool result = lldb_private::StateIsRunningState(state);
818 return result;
821 bool SBDebugger::StateIsStoppedState(StateType state) {
822 LLDB_INSTRUMENT_VA(state);
824 const bool result = lldb_private::StateIsStoppedState(state, false);
826 return result;
829 lldb::SBTarget SBDebugger::CreateTarget(const char *filename,
830 const char *target_triple,
831 const char *platform_name,
832 bool add_dependent_modules,
833 lldb::SBError &sb_error) {
834 LLDB_INSTRUMENT_VA(this, filename, target_triple, platform_name,
835 add_dependent_modules, sb_error);
837 SBTarget sb_target;
838 TargetSP target_sp;
839 if (m_opaque_sp) {
840 sb_error.Clear();
841 OptionGroupPlatform platform_options(false);
842 platform_options.SetPlatformName(platform_name);
844 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget(
845 *m_opaque_sp, filename, target_triple,
846 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo,
847 &platform_options, target_sp);
849 if (sb_error.Success())
850 sb_target.SetSP(target_sp);
851 } else {
852 sb_error = Status::FromErrorString("invalid debugger");
855 Log *log = GetLog(LLDBLog::API);
856 LLDB_LOGF(log,
857 "SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, "
858 "platform_name=%s, add_dependent_modules=%u, error=%s) => "
859 "SBTarget(%p)",
860 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
861 platform_name, add_dependent_modules, sb_error.GetCString(),
862 static_cast<void *>(target_sp.get()));
864 return sb_target;
867 SBTarget
868 SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename,
869 const char *target_triple) {
870 LLDB_INSTRUMENT_VA(this, filename, target_triple);
872 SBTarget sb_target;
873 TargetSP target_sp;
874 if (m_opaque_sp) {
875 const bool add_dependent_modules = true;
876 Status error(m_opaque_sp->GetTargetList().CreateTarget(
877 *m_opaque_sp, filename, target_triple,
878 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
879 target_sp));
880 sb_target.SetSP(target_sp);
883 Log *log = GetLog(LLDBLog::API);
884 LLDB_LOGF(log,
885 "SBDebugger(%p)::CreateTargetWithFileAndTargetTriple "
886 "(filename=\"%s\", triple=%s) => SBTarget(%p)",
887 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
888 static_cast<void *>(target_sp.get()));
890 return sb_target;
893 SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
894 const char *arch_cstr) {
895 LLDB_INSTRUMENT_VA(this, filename, arch_cstr);
897 Log *log = GetLog(LLDBLog::API);
899 SBTarget sb_target;
900 TargetSP target_sp;
901 if (m_opaque_sp) {
902 Status error;
903 if (arch_cstr == nullptr) {
904 // The version of CreateTarget that takes an ArchSpec won't accept an
905 // empty ArchSpec, so when the arch hasn't been specified, we need to
906 // call the target triple version.
907 error = m_opaque_sp->GetTargetList().CreateTarget(
908 *m_opaque_sp, filename, arch_cstr, eLoadDependentsYes, nullptr,
909 target_sp);
910 } else {
911 PlatformSP platform_sp =
912 m_opaque_sp->GetPlatformList().GetSelectedPlatform();
913 ArchSpec arch =
914 Platform::GetAugmentedArchSpec(platform_sp.get(), arch_cstr);
915 if (arch.IsValid())
916 error = m_opaque_sp->GetTargetList().CreateTarget(
917 *m_opaque_sp, filename, arch, eLoadDependentsYes, platform_sp,
918 target_sp);
919 else
920 error = Status::FromErrorStringWithFormat("invalid arch_cstr: %s",
921 arch_cstr);
923 if (error.Success())
924 sb_target.SetSP(target_sp);
927 LLDB_LOGF(log,
928 "SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
929 "arch=%s) => SBTarget(%p)",
930 static_cast<void *>(m_opaque_sp.get()),
931 filename ? filename : "<unspecified>",
932 arch_cstr ? arch_cstr : "<unspecified>",
933 static_cast<void *>(target_sp.get()));
935 return sb_target;
938 SBTarget SBDebugger::CreateTarget(const char *filename) {
939 LLDB_INSTRUMENT_VA(this, filename);
941 SBTarget sb_target;
942 TargetSP target_sp;
943 if (m_opaque_sp) {
944 Status error;
945 const bool add_dependent_modules = true;
946 error = m_opaque_sp->GetTargetList().CreateTarget(
947 *m_opaque_sp, filename, "",
948 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
949 target_sp);
951 if (error.Success())
952 sb_target.SetSP(target_sp);
954 Log *log = GetLog(LLDBLog::API);
955 LLDB_LOGF(log,
956 "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
957 static_cast<void *>(m_opaque_sp.get()), filename,
958 static_cast<void *>(target_sp.get()));
959 return sb_target;
962 SBTarget SBDebugger::GetDummyTarget() {
963 LLDB_INSTRUMENT_VA(this);
965 SBTarget sb_target;
966 if (m_opaque_sp) {
967 sb_target.SetSP(m_opaque_sp->GetDummyTarget().shared_from_this());
969 Log *log = GetLog(LLDBLog::API);
970 LLDB_LOGF(log, "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)",
971 static_cast<void *>(m_opaque_sp.get()),
972 static_cast<void *>(sb_target.GetSP().get()));
973 return sb_target;
976 bool SBDebugger::DeleteTarget(lldb::SBTarget &target) {
977 LLDB_INSTRUMENT_VA(this, target);
979 bool result = false;
980 if (m_opaque_sp) {
981 TargetSP target_sp(target.GetSP());
982 if (target_sp) {
983 // No need to lock, the target list is thread safe
984 result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp);
985 target_sp->Destroy();
986 target.Clear();
990 Log *log = GetLog(LLDBLog::API);
991 LLDB_LOGF(log, "SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
992 static_cast<void *>(m_opaque_sp.get()),
993 static_cast<void *>(target.m_opaque_sp.get()), result);
995 return result;
998 SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) {
999 LLDB_INSTRUMENT_VA(this, idx);
1001 SBTarget sb_target;
1002 if (m_opaque_sp) {
1003 // No need to lock, the target list is thread safe
1004 sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx));
1006 return sb_target;
1009 uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) {
1010 LLDB_INSTRUMENT_VA(this, target);
1012 lldb::TargetSP target_sp = target.GetSP();
1013 if (!target_sp)
1014 return UINT32_MAX;
1016 if (!m_opaque_sp)
1017 return UINT32_MAX;
1019 return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP());
1022 SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) {
1023 LLDB_INSTRUMENT_VA(this, pid);
1025 SBTarget sb_target;
1026 if (m_opaque_sp) {
1027 // No need to lock, the target list is thread safe
1028 sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid));
1030 return sb_target;
1033 SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename,
1034 const char *arch_name) {
1035 LLDB_INSTRUMENT_VA(this, filename, arch_name);
1037 SBTarget sb_target;
1038 if (m_opaque_sp && filename && filename[0]) {
1039 // No need to lock, the target list is thread safe
1040 ArchSpec arch = Platform::GetAugmentedArchSpec(
1041 m_opaque_sp->GetPlatformList().GetSelectedPlatform().get(), arch_name);
1042 TargetSP target_sp(
1043 m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(
1044 FileSpec(filename), arch_name ? &arch : nullptr));
1045 sb_target.SetSP(target_sp);
1047 return sb_target;
1050 SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) {
1051 SBTarget sb_target;
1052 if (m_opaque_sp) {
1053 // No need to lock, the target list is thread safe
1054 sb_target.SetSP(
1055 m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get()));
1057 return sb_target;
1060 uint32_t SBDebugger::GetNumTargets() {
1061 LLDB_INSTRUMENT_VA(this);
1063 if (m_opaque_sp) {
1064 // No need to lock, the target list is thread safe
1065 return m_opaque_sp->GetTargetList().GetNumTargets();
1067 return 0;
1070 SBTarget SBDebugger::GetSelectedTarget() {
1071 LLDB_INSTRUMENT_VA(this);
1073 Log *log = GetLog(LLDBLog::API);
1075 SBTarget sb_target;
1076 TargetSP target_sp;
1077 if (m_opaque_sp) {
1078 // No need to lock, the target list is thread safe
1079 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget();
1080 sb_target.SetSP(target_sp);
1083 if (log) {
1084 SBStream sstr;
1085 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
1086 LLDB_LOGF(log, "SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
1087 static_cast<void *>(m_opaque_sp.get()),
1088 static_cast<void *>(target_sp.get()), sstr.GetData());
1091 return sb_target;
1094 void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
1095 LLDB_INSTRUMENT_VA(this, sb_target);
1097 Log *log = GetLog(LLDBLog::API);
1099 TargetSP target_sp(sb_target.GetSP());
1100 if (m_opaque_sp) {
1101 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp);
1103 if (log) {
1104 SBStream sstr;
1105 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
1106 LLDB_LOGF(log, "SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
1107 static_cast<void *>(m_opaque_sp.get()),
1108 static_cast<void *>(target_sp.get()), sstr.GetData());
1112 SBPlatform SBDebugger::GetSelectedPlatform() {
1113 LLDB_INSTRUMENT_VA(this);
1115 Log *log = GetLog(LLDBLog::API);
1117 SBPlatform sb_platform;
1118 DebuggerSP debugger_sp(m_opaque_sp);
1119 if (debugger_sp) {
1120 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
1122 LLDB_LOGF(log, "SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
1123 static_cast<void *>(m_opaque_sp.get()),
1124 static_cast<void *>(sb_platform.GetSP().get()),
1125 sb_platform.GetName());
1126 return sb_platform;
1129 void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) {
1130 LLDB_INSTRUMENT_VA(this, sb_platform);
1132 Log *log = GetLog(LLDBLog::API);
1134 DebuggerSP debugger_sp(m_opaque_sp);
1135 if (debugger_sp) {
1136 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
1139 LLDB_LOGF(log, "SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
1140 static_cast<void *>(m_opaque_sp.get()),
1141 static_cast<void *>(sb_platform.GetSP().get()),
1142 sb_platform.GetName());
1145 uint32_t SBDebugger::GetNumPlatforms() {
1146 LLDB_INSTRUMENT_VA(this);
1148 if (m_opaque_sp) {
1149 // No need to lock, the platform list is thread safe
1150 return m_opaque_sp->GetPlatformList().GetSize();
1152 return 0;
1155 SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) {
1156 LLDB_INSTRUMENT_VA(this, idx);
1158 SBPlatform sb_platform;
1159 if (m_opaque_sp) {
1160 // No need to lock, the platform list is thread safe
1161 sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx));
1163 return sb_platform;
1166 uint32_t SBDebugger::GetNumAvailablePlatforms() {
1167 LLDB_INSTRUMENT_VA(this);
1169 uint32_t idx = 0;
1170 while (true) {
1171 if (PluginManager::GetPlatformPluginNameAtIndex(idx).empty()) {
1172 break;
1174 ++idx;
1176 // +1 for the host platform, which should always appear first in the list.
1177 return idx + 1;
1180 SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) {
1181 LLDB_INSTRUMENT_VA(this, idx);
1183 SBStructuredData data;
1184 auto platform_dict = std::make_unique<StructuredData::Dictionary>();
1185 llvm::StringRef name_str("name"), desc_str("description");
1187 if (idx == 0) {
1188 PlatformSP host_platform_sp(Platform::GetHostPlatform());
1189 platform_dict->AddStringItem(name_str, host_platform_sp->GetPluginName());
1190 platform_dict->AddStringItem(
1191 desc_str, llvm::StringRef(host_platform_sp->GetDescription()));
1192 } else if (idx > 0) {
1193 llvm::StringRef plugin_name =
1194 PluginManager::GetPlatformPluginNameAtIndex(idx - 1);
1195 if (plugin_name.empty()) {
1196 return data;
1198 platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name));
1200 llvm::StringRef plugin_desc =
1201 PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1);
1202 platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc));
1205 data.m_impl_up->SetObjectSP(
1206 StructuredData::ObjectSP(platform_dict.release()));
1207 return data;
1210 void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) {
1211 LLDB_INSTRUMENT_VA(this, baton, data, data_len);
1213 DispatchInput(data, data_len);
1216 void SBDebugger::DispatchInput(const void *data, size_t data_len) {
1217 LLDB_INSTRUMENT_VA(this, data, data_len);
1219 // Log *log(GetLog (LLDBLog::API));
1221 // if (log)
1222 // LLDB_LOGF(log, "SBDebugger(%p)::DispatchInput (data=\"%.*s\",
1223 // size_t=%" PRIu64 ")",
1224 // m_opaque_sp.get(),
1225 // (int) data_len,
1226 // (const char *) data,
1227 // (uint64_t)data_len);
1229 // if (m_opaque_sp)
1230 // m_opaque_sp->DispatchInput ((const char *) data, data_len);
1233 void SBDebugger::DispatchInputInterrupt() {
1234 LLDB_INSTRUMENT_VA(this);
1236 if (m_opaque_sp)
1237 m_opaque_sp->DispatchInputInterrupt();
1240 void SBDebugger::DispatchInputEndOfFile() {
1241 LLDB_INSTRUMENT_VA(this);
1243 if (m_opaque_sp)
1244 m_opaque_sp->DispatchInputEndOfFile();
1247 void SBDebugger::PushInputReader(SBInputReader &reader) {
1248 LLDB_INSTRUMENT_VA(this, reader);
1251 void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
1252 bool spawn_thread) {
1253 LLDB_INSTRUMENT_VA(this, auto_handle_events, spawn_thread);
1255 if (m_opaque_sp) {
1256 CommandInterpreterRunOptions options;
1257 options.SetAutoHandleEvents(auto_handle_events);
1258 options.SetSpawnThread(spawn_thread);
1259 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(options);
1263 void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
1264 bool spawn_thread,
1265 SBCommandInterpreterRunOptions &options,
1266 int &num_errors, bool &quit_requested,
1267 bool &stopped_for_crash)
1270 LLDB_INSTRUMENT_VA(this, auto_handle_events, spawn_thread, options,
1271 num_errors, quit_requested, stopped_for_crash);
1273 if (m_opaque_sp) {
1274 options.SetAutoHandleEvents(auto_handle_events);
1275 options.SetSpawnThread(spawn_thread);
1276 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
1277 CommandInterpreterRunResult result =
1278 interp.RunCommandInterpreter(options.ref());
1279 num_errors = result.GetNumErrors();
1280 quit_requested =
1281 result.IsResult(lldb::eCommandInterpreterResultQuitRequested);
1282 stopped_for_crash =
1283 result.IsResult(lldb::eCommandInterpreterResultInferiorCrash);
1287 SBCommandInterpreterRunResult SBDebugger::RunCommandInterpreter(
1288 const SBCommandInterpreterRunOptions &options) {
1289 LLDB_INSTRUMENT_VA(this, options);
1291 if (!m_opaque_sp)
1292 return SBCommandInterpreterRunResult();
1294 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
1295 CommandInterpreterRunResult result =
1296 interp.RunCommandInterpreter(options.ref());
1298 return SBCommandInterpreterRunResult(result);
1301 SBError SBDebugger::RunREPL(lldb::LanguageType language,
1302 const char *repl_options) {
1303 LLDB_INSTRUMENT_VA(this, language, repl_options);
1305 SBError error;
1306 if (m_opaque_sp)
1307 error.ref() = m_opaque_sp->RunREPL(language, repl_options);
1308 else
1309 error = Status::FromErrorString("invalid debugger");
1310 return error;
1313 void SBDebugger::reset(const DebuggerSP &debugger_sp) {
1314 m_opaque_sp = debugger_sp;
1317 Debugger *SBDebugger::get() const { return m_opaque_sp.get(); }
1319 Debugger &SBDebugger::ref() const {
1320 assert(m_opaque_sp.get());
1321 return *m_opaque_sp;
1324 const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; }
1326 SBDebugger SBDebugger::FindDebuggerWithID(int id) {
1327 LLDB_INSTRUMENT_VA(id);
1329 // No need to lock, the debugger list is thread safe
1330 SBDebugger sb_debugger;
1331 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id);
1332 if (debugger_sp)
1333 sb_debugger.reset(debugger_sp);
1334 return sb_debugger;
1337 const char *SBDebugger::GetInstanceName() {
1338 LLDB_INSTRUMENT_VA(this);
1340 if (!m_opaque_sp)
1341 return nullptr;
1343 return ConstString(m_opaque_sp->GetInstanceName()).AsCString();
1346 SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
1347 const char *debugger_instance_name) {
1348 LLDB_INSTRUMENT_VA(var_name, value, debugger_instance_name);
1350 SBError sb_error;
1351 DebuggerSP debugger_sp(
1352 Debugger::FindDebuggerWithInstanceName(debugger_instance_name));
1353 Status error;
1354 if (debugger_sp) {
1355 ExecutionContext exe_ctx(
1356 debugger_sp->GetCommandInterpreter().GetExecutionContext());
1357 error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
1358 var_name, value);
1359 } else {
1360 error = Status::FromErrorStringWithFormat(
1361 "invalid debugger instance name '%s'", debugger_instance_name);
1363 if (error.Fail())
1364 sb_error.SetError(std::move(error));
1365 return sb_error;
1368 SBStringList
1369 SBDebugger::GetInternalVariableValue(const char *var_name,
1370 const char *debugger_instance_name) {
1371 LLDB_INSTRUMENT_VA(var_name, debugger_instance_name);
1373 DebuggerSP debugger_sp(
1374 Debugger::FindDebuggerWithInstanceName(debugger_instance_name));
1375 Status error;
1376 if (debugger_sp) {
1377 ExecutionContext exe_ctx(
1378 debugger_sp->GetCommandInterpreter().GetExecutionContext());
1379 lldb::OptionValueSP value_sp(
1380 debugger_sp->GetPropertyValue(&exe_ctx, var_name, error));
1381 if (value_sp) {
1382 StreamString value_strm;
1383 value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
1384 const std::string &value_str = std::string(value_strm.GetString());
1385 if (!value_str.empty()) {
1386 StringList string_list;
1387 string_list.SplitIntoLines(value_str);
1388 return SBStringList(&string_list);
1392 return SBStringList();
1395 uint32_t SBDebugger::GetTerminalWidth() const {
1396 LLDB_INSTRUMENT_VA(this);
1398 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
1401 void SBDebugger::SetTerminalWidth(uint32_t term_width) {
1402 LLDB_INSTRUMENT_VA(this, term_width);
1404 if (m_opaque_sp)
1405 m_opaque_sp->SetTerminalWidth(term_width);
1408 const char *SBDebugger::GetPrompt() const {
1409 LLDB_INSTRUMENT_VA(this);
1411 Log *log = GetLog(LLDBLog::API);
1413 LLDB_LOG(log, "SBDebugger({0:x})::GetPrompt () => \"{1}\"",
1414 static_cast<void *>(m_opaque_sp.get()),
1415 (m_opaque_sp ? m_opaque_sp->GetPrompt() : ""));
1417 return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
1418 : nullptr);
1421 void SBDebugger::SetPrompt(const char *prompt) {
1422 LLDB_INSTRUMENT_VA(this, prompt);
1424 if (m_opaque_sp)
1425 m_opaque_sp->SetPrompt(llvm::StringRef(prompt));
1428 const char *SBDebugger::GetReproducerPath() const {
1429 LLDB_INSTRUMENT_VA(this);
1431 return "GetReproducerPath has been deprecated";
1434 ScriptLanguage SBDebugger::GetScriptLanguage() const {
1435 LLDB_INSTRUMENT_VA(this);
1437 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
1440 void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) {
1441 LLDB_INSTRUMENT_VA(this, script_lang);
1443 if (m_opaque_sp) {
1444 m_opaque_sp->SetScriptLanguage(script_lang);
1448 LanguageType SBDebugger::GetREPLLanguage() const {
1449 LLDB_INSTRUMENT_VA(this);
1451 return (m_opaque_sp ? m_opaque_sp->GetREPLLanguage() : eLanguageTypeUnknown);
1454 void SBDebugger::SetREPLLanguage(LanguageType repl_lang) {
1455 LLDB_INSTRUMENT_VA(this, repl_lang);
1457 if (m_opaque_sp) {
1458 m_opaque_sp->SetREPLLanguage(repl_lang);
1462 bool SBDebugger::SetUseExternalEditor(bool value) {
1463 LLDB_INSTRUMENT_VA(this, value);
1465 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
1468 bool SBDebugger::GetUseExternalEditor() {
1469 LLDB_INSTRUMENT_VA(this);
1471 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
1474 bool SBDebugger::SetUseColor(bool value) {
1475 LLDB_INSTRUMENT_VA(this, value);
1477 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
1480 bool SBDebugger::GetUseColor() const {
1481 LLDB_INSTRUMENT_VA(this);
1483 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
1486 bool SBDebugger::SetShowInlineDiagnostics(bool value) {
1487 LLDB_INSTRUMENT_VA(this, value);
1489 return (m_opaque_sp ? m_opaque_sp->SetShowInlineDiagnostics(value) : false);
1492 bool SBDebugger::SetUseSourceCache(bool value) {
1493 LLDB_INSTRUMENT_VA(this, value);
1495 return (m_opaque_sp ? m_opaque_sp->SetUseSourceCache(value) : false);
1498 bool SBDebugger::GetUseSourceCache() const {
1499 LLDB_INSTRUMENT_VA(this);
1501 return (m_opaque_sp ? m_opaque_sp->GetUseSourceCache() : false);
1504 bool SBDebugger::GetDescription(SBStream &description) {
1505 LLDB_INSTRUMENT_VA(this, description);
1507 Stream &strm = description.ref();
1509 if (m_opaque_sp) {
1510 const char *name = m_opaque_sp->GetInstanceName().c_str();
1511 user_id_t id = m_opaque_sp->GetID();
1512 strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1513 } else
1514 strm.PutCString("No value");
1516 return true;
1519 user_id_t SBDebugger::GetID() {
1520 LLDB_INSTRUMENT_VA(this);
1522 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
1525 SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
1526 LLDB_INSTRUMENT_VA(this, platform_name_cstr);
1528 SBError sb_error;
1529 if (m_opaque_sp) {
1530 if (platform_name_cstr && platform_name_cstr[0]) {
1531 PlatformList &platforms = m_opaque_sp->GetPlatformList();
1532 if (PlatformSP platform_sp = platforms.GetOrCreate(platform_name_cstr))
1533 platforms.SetSelectedPlatform(platform_sp);
1534 else
1535 sb_error.ref() = Status::FromErrorString("platform not found");
1536 } else {
1537 sb_error.ref() = Status::FromErrorString("invalid platform name");
1539 } else {
1540 sb_error.ref() = Status::FromErrorString("invalid debugger");
1542 return sb_error;
1545 bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) {
1546 LLDB_INSTRUMENT_VA(this, sysroot);
1548 if (SBPlatform platform = GetSelectedPlatform()) {
1549 platform.SetSDKRoot(sysroot);
1550 return true;
1552 return false;
1555 bool SBDebugger::GetCloseInputOnEOF() const {
1556 LLDB_INSTRUMENT_VA(this);
1558 return false;
1561 void SBDebugger::SetCloseInputOnEOF(bool b) {
1562 LLDB_INSTRUMENT_VA(this, b);
1565 SBTypeCategory SBDebugger::GetCategory(const char *category_name) {
1566 LLDB_INSTRUMENT_VA(this, category_name);
1568 if (!category_name || *category_name == 0)
1569 return SBTypeCategory();
1571 TypeCategoryImplSP category_sp;
1573 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1574 category_sp, false)) {
1575 return SBTypeCategory(category_sp);
1576 } else {
1577 return SBTypeCategory();
1581 SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) {
1582 LLDB_INSTRUMENT_VA(this, lang_type);
1584 TypeCategoryImplSP category_sp;
1585 if (DataVisualization::Categories::GetCategory(lang_type, category_sp)) {
1586 return SBTypeCategory(category_sp);
1587 } else {
1588 return SBTypeCategory();
1592 SBTypeCategory SBDebugger::CreateCategory(const char *category_name) {
1593 LLDB_INSTRUMENT_VA(this, category_name);
1595 if (!category_name || *category_name == 0)
1596 return SBTypeCategory();
1598 TypeCategoryImplSP category_sp;
1600 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1601 category_sp, true)) {
1602 return SBTypeCategory(category_sp);
1603 } else {
1604 return SBTypeCategory();
1608 bool SBDebugger::DeleteCategory(const char *category_name) {
1609 LLDB_INSTRUMENT_VA(this, category_name);
1611 if (!category_name || *category_name == 0)
1612 return false;
1614 return DataVisualization::Categories::Delete(ConstString(category_name));
1617 uint32_t SBDebugger::GetNumCategories() {
1618 LLDB_INSTRUMENT_VA(this);
1620 return DataVisualization::Categories::GetCount();
1623 SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) {
1624 LLDB_INSTRUMENT_VA(this, index);
1626 return SBTypeCategory(
1627 DataVisualization::Categories::GetCategoryAtIndex(index));
1630 SBTypeCategory SBDebugger::GetDefaultCategory() {
1631 LLDB_INSTRUMENT_VA(this);
1633 return GetCategory("default");
1636 SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) {
1637 LLDB_INSTRUMENT_VA(this, type_name);
1639 SBTypeCategory default_category_sb = GetDefaultCategory();
1640 if (default_category_sb.GetEnabled())
1641 return default_category_sb.GetFormatForType(type_name);
1642 return SBTypeFormat();
1645 SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) {
1646 LLDB_INSTRUMENT_VA(this, type_name);
1648 if (!type_name.IsValid())
1649 return SBTypeSummary();
1650 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1653 SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) {
1654 LLDB_INSTRUMENT_VA(this, type_name);
1656 if (!type_name.IsValid())
1657 return SBTypeFilter();
1658 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1661 SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) {
1662 LLDB_INSTRUMENT_VA(this, type_name);
1664 if (!type_name.IsValid())
1665 return SBTypeSynthetic();
1666 return SBTypeSynthetic(
1667 DataVisualization::GetSyntheticForType(type_name.GetSP()));
1670 void SBDebugger::ResetStatistics() {
1671 LLDB_INSTRUMENT_VA(this);
1672 if (m_opaque_sp)
1673 DebuggerStats::ResetStatistics(*m_opaque_sp, nullptr);
1676 static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) {
1677 if (categories == nullptr)
1678 return {};
1679 size_t len = 0;
1680 while (categories[len] != nullptr)
1681 ++len;
1682 return llvm::ArrayRef(categories, len);
1685 bool SBDebugger::EnableLog(const char *channel, const char **categories) {
1686 LLDB_INSTRUMENT_VA(this, channel, categories);
1688 if (m_opaque_sp) {
1689 uint32_t log_options =
1690 LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1691 std::string error;
1692 llvm::raw_string_ostream error_stream(error);
1693 return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
1694 log_options, /*buffer_size=*/0,
1695 eLogHandlerStream, error_stream);
1696 } else
1697 return false;
1700 void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
1701 void *baton) {
1702 LLDB_INSTRUMENT_VA(this, log_callback, baton);
1704 if (m_opaque_sp) {
1705 return m_opaque_sp->SetLoggingCallback(log_callback, baton);
1709 void SBDebugger::SetDestroyCallback(
1710 lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) {
1711 LLDB_INSTRUMENT_VA(this, destroy_callback, baton);
1712 if (m_opaque_sp) {
1713 return m_opaque_sp->SetDestroyCallback(
1714 destroy_callback, baton);
1718 lldb::callback_token_t
1719 SBDebugger::AddDestroyCallback(lldb::SBDebuggerDestroyCallback destroy_callback,
1720 void *baton) {
1721 LLDB_INSTRUMENT_VA(this, destroy_callback, baton);
1723 if (m_opaque_sp)
1724 return m_opaque_sp->AddDestroyCallback(destroy_callback, baton);
1726 return LLDB_INVALID_CALLBACK_TOKEN;
1729 bool SBDebugger::RemoveDestroyCallback(lldb::callback_token_t token) {
1730 LLDB_INSTRUMENT_VA(this, token);
1732 if (m_opaque_sp)
1733 return m_opaque_sp->RemoveDestroyCallback(token);
1735 return false;
1738 SBTrace
1739 SBDebugger::LoadTraceFromFile(SBError &error,
1740 const SBFileSpec &trace_description_file) {
1741 LLDB_INSTRUMENT_VA(this, error, trace_description_file);
1742 return SBTrace::LoadTraceFromFile(error, *this, trace_description_file);
1745 void SBDebugger::RequestInterrupt() {
1746 LLDB_INSTRUMENT_VA(this);
1748 if (m_opaque_sp)
1749 m_opaque_sp->RequestInterrupt();
1751 void SBDebugger::CancelInterruptRequest() {
1752 LLDB_INSTRUMENT_VA(this);
1754 if (m_opaque_sp)
1755 m_opaque_sp->CancelInterruptRequest();
1758 bool SBDebugger::InterruptRequested() {
1759 LLDB_INSTRUMENT_VA(this);
1761 if (m_opaque_sp)
1762 return m_opaque_sp->InterruptRequested();
1763 return false;
1766 bool SBDebugger::SupportsLanguage(lldb::LanguageType language) {
1767 return TypeSystem::SupportsLanguageStatic(language);