blob: d4ca3cea8e9255c7f776ea7efb584d2f0ee54091 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBDebugger.cpp ------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Jonas Devliegherebaf56642019-03-06 00:06:00 +00009#include "SBReproducerPrivate.h"
Alex Langfordd17cd902018-05-25 20:28:16 +000010#include "SystemInitializerFull.h"
11
Eli Friedmanca93cc12010-06-09 07:37:52 +000012#include "lldb/API/SBDebugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
Greg Claytone0d378b2011-03-24 21:19:54 +000014#include "lldb/lldb-private.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Eli Friedmanca93cc12010-06-09 07:37:52 +000016#include "lldb/API/SBBroadcaster.h"
17#include "lldb/API/SBCommandInterpreter.h"
18#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton2289fa42011-04-30 01:09:13 +000019#include "lldb/API/SBError.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000020#include "lldb/API/SBEvent.h"
21#include "lldb/API/SBFrame.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000022#include "lldb/API/SBListener.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000023#include "lldb/API/SBProcess.h"
24#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000025#include "lldb/API/SBStream.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000026#include "lldb/API/SBStringList.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000027#include "lldb/API/SBStructuredData.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000028#include "lldb/API/SBTarget.h"
29#include "lldb/API/SBThread.h"
Enrico Granata061858c2012-02-15 02:34:21 +000030#include "lldb/API/SBTypeCategory.h"
Enrico Granata061858c2012-02-15 02:34:21 +000031#include "lldb/API/SBTypeFilter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000032#include "lldb/API/SBTypeFormat.h"
Enrico Granata061858c2012-02-15 02:34:21 +000033#include "lldb/API/SBTypeNameSpecifier.h"
34#include "lldb/API/SBTypeSummary.h"
35#include "lldb/API/SBTypeSynthetic.h"
36
Greg Clayton6eee5aa2010-10-11 01:05:37 +000037#include "lldb/Core/Debugger.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000038#include "lldb/Core/PluginManager.h"
Greg Clayton44d93782014-01-27 23:43:24 +000039#include "lldb/Core/StreamFile.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000040#include "lldb/Core/StructuredDataImpl.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000041#include "lldb/DataFormatters/DataVisualization.h"
Pavel Labathf1389e92018-02-19 15:06:28 +000042#include "lldb/Host/XML.h"
Zachary Turnere6e2bb32015-03-31 21:03:22 +000043#include "lldb/Initialization/SystemLifetimeManager.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000044#include "lldb/Interpreter/CommandInterpreter.h"
Pavel Labath47cbf4a2018-04-10 09:03:59 +000045#include "lldb/Interpreter/OptionArgParser.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000046#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000047#include "lldb/Target/Process.h"
48#include "lldb/Target/TargetList.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000049#include "lldb/Utility/Args.h"
Pavel Labathd821c992018-08-07 11:07:21 +000050#include "lldb/Utility/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051
Enrico Granatad717cc92015-10-20 04:50:09 +000052#include "llvm/ADT/STLExtras.h"
Zachary Turner7b2e5a32016-09-16 19:09:12 +000053#include "llvm/ADT/StringRef.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000054#include "llvm/Support/DynamicLibrary.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000055#include "llvm/Support/ManagedStatic.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000056
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057using namespace lldb;
58using namespace lldb_private;
59
Jonas Devlieghered77c2e02019-03-02 00:20:26 +000060/// Helper class for replaying commands through the reproducer.
61class CommandLoader {
62public:
63 CommandLoader(std::vector<std::string> files) : m_files(files) {}
64
65 static std::unique_ptr<CommandLoader> Create() {
66 repro::Loader *loader = repro::Reproducer::Instance().GetLoader();
67 if (!loader)
68 return {};
69
70 FileSpec file = loader->GetFile<repro::CommandInfo>();
71 if (!file)
72 return {};
73
74 auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath());
75 if (auto err = error_or_file.getError())
76 return {};
77
78 std::vector<std::string> files;
79 llvm::yaml::Input yin((*error_or_file)->getBuffer());
80 yin >> files;
81
82 if (auto err = yin.error())
83 return {};
84
85 return llvm::make_unique<CommandLoader>(std::move(files));
86 }
87
88 FILE *GetNextFile() {
89 if (m_index >= m_files.size())
90 return nullptr;
91 return FileSystem::Instance().Fopen(m_files[m_index++].c_str(), "r");
92 }
93
94private:
95 std::vector<std::string> m_files;
96 unsigned m_index = 0;
97};
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp,
100 const FileSpec &spec,
Zachary Turner97206d52017-05-12 04:51:55 +0000101 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 llvm::sys::DynamicLibrary dynlib =
103 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
104 if (dynlib.isValid()) {
105 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger);
106
107 lldb::SBDebugger debugger_sb(debugger_sp);
108 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
109 // function.
110 // TODO: mangle this differently for your system - on OSX, the first
111 // underscore needs to be removed and the second one stays
112 LLDBCommandPluginInit init_func =
Stephane Sezer22701b22017-10-24 23:46:00 +0000113 (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
115 if (init_func) {
116 if (init_func(debugger_sb))
117 return dynlib;
118 else
119 error.SetErrorString("plug-in refused to load "
120 "(lldb::PluginInitialize(lldb::SBDebugger) "
121 "returned false)");
122 } else {
123 error.SetErrorString("plug-in is missing the required initialization: "
124 "lldb::PluginInitialize(lldb::SBDebugger)");
Greg Clayton5fb8f792013-12-02 19:35:49 +0000125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 } else {
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000127 if (FileSystem::Instance().Exists(spec))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 error.SetErrorString("this file does not represent a loadable dylib");
Greg Clayton5fb8f792013-12-02 19:35:49 +0000129 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 error.SetErrorString("no such file");
131 }
132 return llvm::sys::DynamicLibrary();
Greg Clayton5fb8f792013-12-02 19:35:49 +0000133}
134
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000135static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137SBError SBInputReader::Initialize(
138 lldb::SBDebugger &sb_debugger,
139 unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction,
140 char const *, unsigned long),
141 void *, lldb::InputReaderGranularity, char const *, char const *, bool) {
142 return SBError();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000143}
144
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000145void SBInputReader::SetIsDone(bool b) {
146 LLDB_RECORD_METHOD(void, SBInputReader, SetIsDone, (bool), b);
147}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000148
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000149bool SBInputReader::IsActive() const {
150 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBInputReader, IsActive);
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000151
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000152 return false;
153}
154
155SBDebugger::SBDebugger() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBDebugger); }
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000158 : m_opaque_sp(debugger_sp) {
159 LLDB_RECORD_CONSTRUCTOR(SBDebugger, (const lldb::DebuggerSP &), debugger_sp);
160}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000161
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000162SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
163 LLDB_RECORD_CONSTRUCTOR(SBDebugger, (const lldb::SBDebugger &), rhs);
164}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000165
166SBDebugger::~SBDebugger() = default;
167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000169 LLDB_RECORD_METHOD(lldb::SBDebugger &,
170 SBDebugger, operator=,(const lldb::SBDebugger &), rhs);
171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 if (this != &rhs) {
173 m_opaque_sp = rhs.m_opaque_sp;
174 }
175 return *this;
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000176}
177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178void SBDebugger::Initialize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000179 LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, Initialize);
Jonas Devlieghere936c6242019-02-21 22:26:16 +0000180 SBError ignored = SBDebugger::InitializeWithErrorHandling();
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000181}
182
Jonas Devlieghere936c6242019-02-21 22:26:16 +0000183lldb::SBError SBDebugger::InitializeWithErrorHandling() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000184 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBError, SBDebugger,
185 InitializeWithErrorHandling);
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 if (log)
190 log->Printf("SBDebugger::Initialize ()");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000191
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000192 SBError error;
193 if (auto e = g_debugger_lifetime->Initialize(
Jonas Devlieghere936c6242019-02-21 22:26:16 +0000194 llvm::make_unique<SystemInitializerFull>(), LoadPlugin)) {
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000195 error.SetError(Status(std::move(e)));
196 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000197 return LLDB_RECORD_RESULT(error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198}
199
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000200void SBDebugger::Terminate() {
201 LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, Terminate);
202
203 g_debugger_lifetime->Terminate();
204}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205
206void SBDebugger::Clear() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000207 LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, Clear);
208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
210
211 if (log)
212 log->Printf("SBDebugger(%p)::Clear ()",
213 static_cast<void *>(m_opaque_sp.get()));
214
215 if (m_opaque_sp)
216 m_opaque_sp->ClearIOHandlers();
217
218 m_opaque_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219}
220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221SBDebugger SBDebugger::Create() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000222 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBDebugger, SBDebugger, Create);
223
224 return LLDB_RECORD_RESULT(SBDebugger::Create(false, nullptr, nullptr));
Greg Clayton48e42542010-07-30 20:12:55 +0000225}
226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227SBDebugger SBDebugger::Create(bool source_init_files) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000228 LLDB_RECORD_STATIC_METHOD(lldb::SBDebugger, SBDebugger, Create, (bool),
229 source_init_files);
230
231 return LLDB_RECORD_RESULT(
232 SBDebugger::Create(source_init_files, nullptr, nullptr));
Jim Ingham06942692011-08-13 00:22:20 +0000233}
234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235SBDebugger SBDebugger::Create(bool source_init_files,
236 lldb::LogOutputCallback callback, void *baton)
Jim Ingham228063c2012-02-21 02:23:08 +0000237
238{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 SBDebugger debugger;
Greg Claytoncb172b12014-05-19 20:42:14 +0000242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 // Currently we have issues if this function is called simultaneously on two
Adrian Prantl05097242018-04-30 16:49:04 +0000244 // different threads. The issues mainly revolve around the fact that the
245 // lldb_private::FormatManager uses global collections and having two threads
246 // parsing the .lldbinit files can cause mayhem. So to get around this for
247 // now we need to use a mutex to prevent bad things from happening.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 static std::recursive_mutex g_mutex;
249 std::lock_guard<std::recursive_mutex> guard(g_mutex);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 debugger.reset(Debugger::CreateInstance(callback, baton));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 if (log) {
254 SBStream sstr;
255 debugger.GetDescription(sstr);
256 log->Printf("SBDebugger::Create () => SBDebugger(%p): %s",
257 static_cast<void *>(debugger.m_opaque_sp.get()),
258 sstr.GetData());
259 }
260
261 SBCommandInterpreter interp = debugger.GetCommandInterpreter();
262 if (source_init_files) {
263 interp.get()->SkipLLDBInitFiles(false);
264 interp.get()->SkipAppInitFiles(false);
265 SBCommandReturnObject result;
266 interp.SourceInitFileInHomeDirectory(result);
267 } else {
268 interp.get()->SkipLLDBInitFiles(true);
269 interp.get()->SkipAppInitFiles(true);
270 }
271 return debugger;
Greg Clayton66111032010-06-23 01:19:29 +0000272}
273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274void SBDebugger::Destroy(SBDebugger &debugger) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000275 LLDB_RECORD_STATIC_METHOD(void, SBDebugger, Destroy, (lldb::SBDebugger &),
276 debugger);
277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 if (log) {
281 SBStream sstr;
282 debugger.GetDescription(sstr);
283 log->Printf("SBDebugger::Destroy () => SBDebugger(%p): %s",
284 static_cast<void *>(debugger.m_opaque_sp.get()),
285 sstr.GetData());
286 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 Debugger::Destroy(debugger.m_opaque_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000289
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 if (debugger.m_opaque_sp.get() != nullptr)
291 debugger.m_opaque_sp.reset();
Caroline Ticee02657b2011-01-22 01:02:07 +0000292}
293
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294void SBDebugger::MemoryPressureDetected() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000295 LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, MemoryPressureDetected);
296
Adrian Prantl05097242018-04-30 16:49:04 +0000297 // Since this function can be call asynchronously, we allow it to be non-
298 // mandatory. We have seen deadlocks with this function when called so we
299 // need to safeguard against this until we can determine what is causing the
300 // deadlocks.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
302
303 const bool mandatory = false;
304 if (log) {
305 log->Printf("SBDebugger::MemoryPressureDetected (), mandatory = %d",
306 mandatory);
307 }
308
309 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonf9322412011-12-15 04:38:41 +0000310}
311
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000312bool SBDebugger::IsValid() const {
313 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, IsValid);
314
315 return m_opaque_sp.get() != nullptr;
316}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317
318void SBDebugger::SetAsync(bool b) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000319 LLDB_RECORD_METHOD(void, SBDebugger, SetAsync, (bool), b);
320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 if (m_opaque_sp)
322 m_opaque_sp->SetAsyncExecution(b);
Greg Clayton66111032010-06-23 01:19:29 +0000323}
324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325bool SBDebugger::GetAsync() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000326 LLDB_RECORD_METHOD_NO_ARGS(bool, SBDebugger, GetAsync);
327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329}
330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331void SBDebugger::SkipLLDBInitFiles(bool b) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000332 LLDB_RECORD_METHOD(void, SBDebugger, SkipLLDBInitFiles, (bool), b);
333
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334 if (m_opaque_sp)
335 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b);
Jim Inghame64f0dc2011-09-13 23:25:31 +0000336}
337
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338void SBDebugger::SkipAppInitFiles(bool b) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000339 LLDB_RECORD_METHOD(void, SBDebugger, SkipAppInitFiles, (bool), b);
340
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 if (m_opaque_sp)
342 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b);
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000343}
344
Adrian Prantl05097242018-04-30 16:49:04 +0000345// Shouldn't really be settable after initialization as this could cause lots
346// of problems; don't want users trying to switch modes in the middle of a
347// debugging session.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000349 LLDB_RECORD_METHOD(void, SBDebugger, SetInputFileHandle, (FILE *, bool), fh,
350 transfer_ownership);
351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000353
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 if (log)
355 log->Printf(
356 "SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)",
357 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
358 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000359
Jonas Devlieghered77c2e02019-03-02 00:20:26 +0000360 if (!m_opaque_sp)
361 return;
362
363 repro::DataRecorder *recorder = nullptr;
364 if (repro::Generator *g = repro::Reproducer::Instance().GetGenerator())
365 recorder = g->GetOrCreate<repro::CommandProvider>().GetNewDataRecorder();
366
367 static std::unique_ptr<CommandLoader> loader = CommandLoader::Create();
368 if (loader)
369 fh = loader->GetNextFile();
370
371 m_opaque_sp->SetInputFileHandle(fh, transfer_ownership, recorder);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372}
373
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000375 LLDB_RECORD_METHOD(void, SBDebugger, SetOutputFileHandle, (FILE *, bool), fh,
376 transfer_ownership);
377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 if (log)
381 log->Printf(
382 "SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)",
383 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
384 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 if (m_opaque_sp)
387 m_opaque_sp->SetOutputFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388}
389
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000391 LLDB_RECORD_METHOD(void, SBDebugger, SetErrorFileHandle, (FILE *, bool), fh,
392 transfer_ownership);
393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 if (log)
397 log->Printf(
398 "SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)",
399 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
400 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 if (m_opaque_sp)
403 m_opaque_sp->SetErrorFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404}
405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406FILE *SBDebugger::GetInputFileHandle() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000407 LLDB_RECORD_METHOD_NO_ARGS(FILE *, SBDebugger, GetInputFileHandle);
408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 if (m_opaque_sp) {
410 StreamFileSP stream_file_sp(m_opaque_sp->GetInputFile());
411 if (stream_file_sp)
412 return stream_file_sp->GetFile().GetStream();
413 }
414 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415}
416
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417FILE *SBDebugger::GetOutputFileHandle() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000418 LLDB_RECORD_METHOD_NO_ARGS(FILE *, SBDebugger, GetOutputFileHandle);
419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 if (m_opaque_sp) {
421 StreamFileSP stream_file_sp(m_opaque_sp->GetOutputFile());
422 if (stream_file_sp)
423 return stream_file_sp->GetFile().GetStream();
424 }
425 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426}
427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428FILE *SBDebugger::GetErrorFileHandle() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000429 LLDB_RECORD_METHOD_NO_ARGS(FILE *, SBDebugger, GetErrorFileHandle);
430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 if (m_opaque_sp) {
432 StreamFileSP stream_file_sp(m_opaque_sp->GetErrorFile());
433 if (stream_file_sp)
434 return stream_file_sp->GetFile().GetStream();
435 }
436 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437}
438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439void SBDebugger::SaveInputTerminalState() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000440 LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, SaveInputTerminalState);
441
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 if (m_opaque_sp)
443 m_opaque_sp->SaveInputTerminalState();
Jim Inghamc5917d92012-11-30 20:23:19 +0000444}
445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446void SBDebugger::RestoreInputTerminalState() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000447 LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, RestoreInputTerminalState);
448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 if (m_opaque_sp)
450 m_opaque_sp->RestoreInputTerminalState();
Jim Inghamc5917d92012-11-30 20:23:19 +0000451}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452SBCommandInterpreter SBDebugger::GetCommandInterpreter() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000453 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBCommandInterpreter, SBDebugger,
454 GetCommandInterpreter);
455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 SBCommandInterpreter sb_interpreter;
459 if (m_opaque_sp)
460 sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000461
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 if (log)
463 log->Printf(
464 "SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)",
465 static_cast<void *>(m_opaque_sp.get()),
466 static_cast<void *>(sb_interpreter.get()));
Caroline Tice750cd172010-10-26 23:49:36 +0000467
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000468 return LLDB_RECORD_RESULT(sb_interpreter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469}
470
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471void SBDebugger::HandleCommand(const char *command) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000472 LLDB_RECORD_METHOD(void, SBDebugger, HandleCommand, (const char *), command);
473
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 if (m_opaque_sp) {
475 TargetSP target_sp(m_opaque_sp->GetSelectedTarget());
476 std::unique_lock<std::recursive_mutex> lock;
477 if (target_sp)
478 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +0000479
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480 SBCommandInterpreter sb_interpreter(GetCommandInterpreter());
481 SBCommandReturnObject result;
Greg Clayton66111032010-06-23 01:19:29 +0000482
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 sb_interpreter.HandleCommand(command, result, false);
Greg Clayton66111032010-06-23 01:19:29 +0000484
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 if (GetErrorFileHandle() != nullptr)
486 result.PutError(GetErrorFileHandle());
487 if (GetOutputFileHandle() != nullptr)
488 result.PutOutput(GetOutputFileHandle());
Greg Clayton66111032010-06-23 01:19:29 +0000489
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 if (!m_opaque_sp->GetAsyncExecution()) {
491 SBProcess process(GetCommandInterpreter().GetProcess());
492 ProcessSP process_sp(process.GetSP());
493 if (process_sp) {
494 EventSP event_sp;
495 ListenerSP lldb_listener_sp = m_opaque_sp->GetListener();
Pavel Labathd35031e12016-11-30 10:41:42 +0000496 while (lldb_listener_sp->GetEventForBroadcaster(
497 process_sp.get(), event_sp, std::chrono::seconds(0))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 SBEvent event(event_sp);
499 HandleProcessEvent(process, event, GetOutputFileHandle(),
500 GetErrorFileHandle());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505}
506
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507SBListener SBDebugger::GetListener() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000508 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBListener, SBDebugger, GetListener);
509
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000511
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 SBListener sb_listener;
513 if (m_opaque_sp)
514 sb_listener.reset(m_opaque_sp->GetListener());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 if (log)
517 log->Printf("SBDebugger(%p)::GetListener () => SBListener(%p)",
518 static_cast<void *>(m_opaque_sp.get()),
519 static_cast<void *>(sb_listener.get()));
Caroline Tice750cd172010-10-26 23:49:36 +0000520
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000521 return LLDB_RECORD_RESULT(sb_listener);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522}
523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524void SBDebugger::HandleProcessEvent(const SBProcess &process,
525 const SBEvent &event, FILE *out,
526 FILE *err) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000527 LLDB_RECORD_METHOD(
528 void, SBDebugger, HandleProcessEvent,
529 (const lldb::SBProcess &, const lldb::SBEvent &, FILE *, FILE *), process,
530 event, out, err);
531
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 if (!process.IsValid())
533 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 TargetSP target_sp(process.GetTarget().GetSP());
536 if (!target_sp)
537 return;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000538
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539 const uint32_t event_type = event.GetType();
540 char stdio_buffer[1024];
541 size_t len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 if (event_type &
546 (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) {
547 // Drain stdout when we stop just in case we have any bytes
548 while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0)
549 if (out != nullptr)
550 ::fwrite(stdio_buffer, 1, len, out);
551 }
Todd Fiala75930012016-08-19 04:21:48 +0000552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553 if (event_type &
554 (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) {
555 // Drain stderr when we stop just in case we have any bytes
556 while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0)
557 if (err != nullptr)
558 ::fwrite(stdio_buffer, 1, len, err);
559 }
560
561 if (event_type & Process::eBroadcastBitStateChanged) {
562 StateType event_state = SBProcess::GetStateFromEvent(event);
563
564 if (event_state == eStateInvalid)
565 return;
566
567 bool is_stopped = StateIsStoppedState(event_state);
568 if (!is_stopped)
569 process.ReportEventState(event, out);
570 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571}
572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573SBSourceManager SBDebugger::GetSourceManager() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000574 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSourceManager, SBDebugger,
575 GetSourceManager);
576
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 SBSourceManager sb_source_manager(*this);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000578 return LLDB_RECORD_RESULT(sb_source_manager);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579}
580
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000582 LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, GetDefaultArchitecture,
583 (char *, size_t), "", arch_name_len);
584
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 if (arch_name && arch_name_len) {
586 ArchSpec default_arch = Target::GetDefaultArchitecture();
Caroline Ticedaccaa92010-09-20 20:44:43 +0000587
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 if (default_arch.IsValid()) {
589 const std::string &triple_str = default_arch.GetTriple().str();
590 if (!triple_str.empty())
591 ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str());
592 else
593 ::snprintf(arch_name, arch_name_len, "%s",
594 default_arch.GetArchitectureName());
595 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 }
598 if (arch_name && arch_name_len)
599 arch_name[0] = '\0';
600 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601}
602
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603bool SBDebugger::SetDefaultArchitecture(const char *arch_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000604 LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, SetDefaultArchitecture,
605 (const char *), arch_name);
606
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 if (arch_name) {
608 ArchSpec arch(arch_name);
609 if (arch.IsValid()) {
610 Target::SetDefaultArchitecture(arch);
611 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613 }
614 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615}
616
617ScriptLanguage
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618SBDebugger::GetScriptingLanguage(const char *script_language_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000619 LLDB_RECORD_METHOD(lldb::ScriptLanguage, SBDebugger, GetScriptingLanguage,
620 (const char *), script_language_name);
621
Zachary Turner7b2e5a32016-09-16 19:09:12 +0000622 if (!script_language_name) return eScriptLanguageDefault;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000623 return OptionArgParser::ToScriptLanguage(
624 llvm::StringRef(script_language_name), eScriptLanguageDefault, nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627const char *SBDebugger::GetVersionString() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000628 LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBDebugger, GetVersionString);
629
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 return lldb_private::GetVersion();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631}
632
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633const char *SBDebugger::StateAsCString(StateType state) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000634 LLDB_RECORD_STATIC_METHOD(const char *, SBDebugger, StateAsCString,
635 (lldb::StateType), state);
636
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637 return lldb_private::StateAsCString(state);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638}
639
Pavel Labathf1389e92018-02-19 15:06:28 +0000640static void AddBoolConfigEntry(StructuredData::Dictionary &dict,
641 llvm::StringRef name, bool value,
642 llvm::StringRef description) {
643 auto entry_up = llvm::make_unique<StructuredData::Dictionary>();
644 entry_up->AddBooleanItem("value", value);
645 entry_up->AddStringItem("description", description);
646 dict.AddItem(name, std::move(entry_up));
647}
648
Pavel Labathabc0c6a2018-06-28 14:23:04 +0000649static void AddLLVMTargets(StructuredData::Dictionary &dict) {
650 auto array_up = llvm::make_unique<StructuredData::Array>();
651#define LLVM_TARGET(target) \
652 array_up->AddItem(llvm::make_unique<StructuredData::String>(#target));
653#include "llvm/Config/Targets.def"
654 auto entry_up = llvm::make_unique<StructuredData::Dictionary>();
655 entry_up->AddItem("value", std::move(array_up));
656 entry_up->AddStringItem("description", "A list of configured LLVM targets.");
657 dict.AddItem("targets", std::move(entry_up));
658}
659
Pavel Labathf1389e92018-02-19 15:06:28 +0000660SBStructuredData SBDebugger::GetBuildConfiguration() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000661 LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBStructuredData, SBDebugger,
662 GetBuildConfiguration);
663
Pavel Labathf1389e92018-02-19 15:06:28 +0000664 auto config_up = llvm::make_unique<StructuredData::Dictionary>();
665 AddBoolConfigEntry(
666 *config_up, "xml", XMLDocument::XMLEnabled(),
667 "A boolean value that indicates if XML support is enabled in LLDB");
Pavel Labathabc0c6a2018-06-28 14:23:04 +0000668 AddLLVMTargets(*config_up);
Pavel Labathf1389e92018-02-19 15:06:28 +0000669
670 SBStructuredData data;
671 data.m_impl_up->SetObjectSP(std::move(config_up));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000672 return LLDB_RECORD_RESULT(data);
Pavel Labathf1389e92018-02-19 15:06:28 +0000673}
674
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675bool SBDebugger::StateIsRunningState(StateType state) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000676 LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, StateIsRunningState,
677 (lldb::StateType), state);
678
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000680
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681 const bool result = lldb_private::StateIsRunningState(state);
682 if (log)
683 log->Printf("SBDebugger::StateIsRunningState (state=%s) => %i",
684 StateAsCString(state), result);
Greg Clayton48381312010-10-30 04:51:46 +0000685
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687}
688
Kate Stoneb9c1b512016-09-06 20:57:50 +0000689bool SBDebugger::StateIsStoppedState(StateType state) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000690 LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, StateIsStoppedState,
691 (lldb::StateType), state);
692
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000694
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695 const bool result = lldb_private::StateIsStoppedState(state, false);
696 if (log)
697 log->Printf("SBDebugger::StateIsStoppedState (state=%s) => %i",
698 StateAsCString(state), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000699
Kate Stoneb9c1b512016-09-06 20:57:50 +0000700 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701}
702
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703lldb::SBTarget SBDebugger::CreateTarget(const char *filename,
704 const char *target_triple,
705 const char *platform_name,
706 bool add_dependent_modules,
707 lldb::SBError &sb_error) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000708 LLDB_RECORD_METHOD(
709 lldb::SBTarget, SBDebugger, CreateTarget,
710 (const char *, const char *, const char *, bool, lldb::SBError &),
711 filename, target_triple, platform_name, add_dependent_modules, sb_error);
712
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713 SBTarget sb_target;
714 TargetSP target_sp;
715 if (m_opaque_sp) {
716 sb_error.Clear();
717 OptionGroupPlatform platform_options(false);
718 platform_options.SetPlatformName(platform_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000719
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000721 *m_opaque_sp, filename, target_triple,
722 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 &platform_options, target_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000724
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725 if (sb_error.Success())
726 sb_target.SetSP(target_sp);
727 } else {
728 sb_error.SetErrorString("invalid debugger");
729 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000730
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
732 if (log)
733 log->Printf("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, "
734 "platform_name=%s, add_dependent_modules=%u, error=%s) => "
735 "SBTarget(%p)",
736 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
737 platform_name, add_dependent_modules, sb_error.GetCString(),
738 static_cast<void *>(target_sp.get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000739
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000740 return LLDB_RECORD_RESULT(sb_target);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000741}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742
743SBTarget
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename,
745 const char *target_triple) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000746 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger,
747 CreateTargetWithFileAndTargetTriple,
748 (const char *, const char *), filename, target_triple);
749
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750 SBTarget sb_target;
751 TargetSP target_sp;
752 if (m_opaque_sp) {
753 const bool add_dependent_modules = true;
Zachary Turner97206d52017-05-12 04:51:55 +0000754 Status error(m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000755 *m_opaque_sp, filename, target_triple,
756 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 target_sp));
758 sb_target.SetSP(target_sp);
759 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000760
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
762 if (log)
763 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple "
764 "(filename=\"%s\", triple=%s) => SBTarget(%p)",
765 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
766 static_cast<void *>(target_sp.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000767
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000768 return LLDB_RECORD_RESULT(sb_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769}
770
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
772 const char *arch_cstr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000773 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, CreateTargetWithFileAndArch,
774 (const char *, const char *), filename, arch_cstr);
775
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 SBTarget sb_target;
779 TargetSP target_sp;
780 if (m_opaque_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000781 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782 const bool add_dependent_modules = true;
783
784 error = m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000785 *m_opaque_sp, filename, arch_cstr,
786 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787 target_sp);
788
789 if (error.Success()) {
790 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
791 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000792 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 }
794
795 if (log)
796 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
797 "arch=%s) => SBTarget(%p)",
798 static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr,
799 static_cast<void *>(target_sp.get()));
800
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000801 return LLDB_RECORD_RESULT(sb_target);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802}
803
804SBTarget SBDebugger::CreateTarget(const char *filename) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000805 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, CreateTarget, (const char *),
806 filename);
807
Kate Stoneb9c1b512016-09-06 20:57:50 +0000808 SBTarget sb_target;
809 TargetSP target_sp;
810 if (m_opaque_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000811 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 const bool add_dependent_modules = true;
813 error = m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000814 *m_opaque_sp, filename, "",
815 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
816 target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817
818 if (error.Success()) {
819 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
820 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000821 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 }
823 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
824 if (log)
825 log->Printf(
826 "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
827 static_cast<void *>(m_opaque_sp.get()), filename,
828 static_cast<void *>(target_sp.get()));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000829 return LLDB_RECORD_RESULT(sb_target);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830}
831
Jim Inghamb842f2e2017-09-14 20:22:49 +0000832SBTarget SBDebugger::GetDummyTarget() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000833 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTarget, SBDebugger, GetDummyTarget);
834
Jim Inghamb842f2e2017-09-14 20:22:49 +0000835 SBTarget sb_target;
836 if (m_opaque_sp) {
837 sb_target.SetSP(m_opaque_sp->GetDummyTarget()->shared_from_this());
838 }
839 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
840 if (log)
841 log->Printf(
842 "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)",
843 static_cast<void *>(m_opaque_sp.get()),
844 static_cast<void *>(sb_target.GetSP().get()));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000845 return LLDB_RECORD_RESULT(sb_target);
Jim Inghamb842f2e2017-09-14 20:22:49 +0000846}
847
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848bool SBDebugger::DeleteTarget(lldb::SBTarget &target) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000849 LLDB_RECORD_METHOD(bool, SBDebugger, DeleteTarget, (lldb::SBTarget &),
850 target);
851
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 bool result = false;
853 if (m_opaque_sp) {
854 TargetSP target_sp(target.GetSP());
855 if (target_sp) {
856 // No need to lock, the target list is thread safe
857 result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp);
858 target_sp->Destroy();
859 target.Clear();
860 const bool mandatory = true;
861 ModuleList::RemoveOrphanSharedModules(mandatory);
862 }
863 }
864
865 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
866 if (log)
867 log->Printf("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
868 static_cast<void *>(m_opaque_sp.get()),
869 static_cast<void *>(target.m_opaque_sp.get()), result);
870
871 return result;
872}
873
874SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000875 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, GetTargetAtIndex, (uint32_t),
876 idx);
877
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 SBTarget sb_target;
879 if (m_opaque_sp) {
880 // No need to lock, the target list is thread safe
881 sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx));
882 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000883 return LLDB_RECORD_RESULT(sb_target);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884}
885
886uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000887 LLDB_RECORD_METHOD(uint32_t, SBDebugger, GetIndexOfTarget, (lldb::SBTarget),
888 target);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889
890 lldb::TargetSP target_sp = target.GetSP();
891 if (!target_sp)
892 return UINT32_MAX;
893
894 if (!m_opaque_sp)
895 return UINT32_MAX;
896
897 return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP());
898}
899
900SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000901 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithProcessID,
902 (lldb::pid_t), pid);
903
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904 SBTarget sb_target;
905 if (m_opaque_sp) {
906 // No need to lock, the target list is thread safe
907 sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid));
908 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000909 return LLDB_RECORD_RESULT(sb_target);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910}
911
912SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename,
913 const char *arch_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000914 LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithFileAndArch,
915 (const char *, const char *), filename, arch_name);
916
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 SBTarget sb_target;
918 if (m_opaque_sp && filename && filename[0]) {
919 // No need to lock, the target list is thread safe
Pavel Labath7263f1b2017-10-31 10:56:03 +0000920 ArchSpec arch = Platform::GetAugmentedArchSpec(
921 m_opaque_sp->GetPlatformList().GetSelectedPlatform().get(), arch_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922 TargetSP target_sp(
923 m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000924 FileSpec(filename), arch_name ? &arch : nullptr));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 sb_target.SetSP(target_sp);
926 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000927 return LLDB_RECORD_RESULT(sb_target);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928}
929
930SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) {
931 SBTarget sb_target;
932 if (m_opaque_sp) {
933 // No need to lock, the target list is thread safe
934 sb_target.SetSP(
935 m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get()));
936 }
937 return sb_target;
938}
939
940uint32_t SBDebugger::GetNumTargets() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000941 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumTargets);
942
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 if (m_opaque_sp) {
944 // No need to lock, the target list is thread safe
945 return m_opaque_sp->GetTargetList().GetNumTargets();
946 }
947 return 0;
948}
949
950SBTarget SBDebugger::GetSelectedTarget() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000951 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTarget, SBDebugger, GetSelectedTarget);
952
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
954
955 SBTarget sb_target;
956 TargetSP target_sp;
957 if (m_opaque_sp) {
958 // No need to lock, the target list is thread safe
959 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget();
960 sb_target.SetSP(target_sp);
961 }
962
963 if (log) {
964 SBStream sstr;
965 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
966 log->Printf("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
967 static_cast<void *>(m_opaque_sp.get()),
968 static_cast<void *>(target_sp.get()), sstr.GetData());
969 }
970
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000971 return LLDB_RECORD_RESULT(sb_target);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972}
973
974void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000975 LLDB_RECORD_METHOD(void, SBDebugger, SetSelectedTarget, (lldb::SBTarget &),
976 sb_target);
977
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
979
980 TargetSP target_sp(sb_target.GetSP());
981 if (m_opaque_sp) {
982 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
983 }
984 if (log) {
985 SBStream sstr;
986 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
987 log->Printf("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
988 static_cast<void *>(m_opaque_sp.get()),
989 static_cast<void *>(target_sp.get()), sstr.GetData());
990 }
991}
992
993SBPlatform SBDebugger::GetSelectedPlatform() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000994 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBPlatform, SBDebugger, GetSelectedPlatform);
995
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
997
998 SBPlatform sb_platform;
999 DebuggerSP debugger_sp(m_opaque_sp);
1000 if (debugger_sp) {
1001 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
1002 }
1003 if (log)
1004 log->Printf("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
1005 static_cast<void *>(m_opaque_sp.get()),
1006 static_cast<void *>(sb_platform.GetSP().get()),
1007 sb_platform.GetName());
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001008 return LLDB_RECORD_RESULT(sb_platform);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009}
1010
1011void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001012 LLDB_RECORD_METHOD(void, SBDebugger, SetSelectedPlatform,
1013 (lldb::SBPlatform &), sb_platform);
1014
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1016
1017 DebuggerSP debugger_sp(m_opaque_sp);
1018 if (debugger_sp) {
1019 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
1020 }
1021
1022 if (log)
1023 log->Printf("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
1024 static_cast<void *>(m_opaque_sp.get()),
1025 static_cast<void *>(sb_platform.GetSP().get()),
1026 sb_platform.GetName());
1027}
1028
Vadim Macagonc10e34d2017-08-09 09:20:40 +00001029uint32_t SBDebugger::GetNumPlatforms() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001030 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumPlatforms);
1031
Vadim Macagonc10e34d2017-08-09 09:20:40 +00001032 if (m_opaque_sp) {
1033 // No need to lock, the platform list is thread safe
1034 return m_opaque_sp->GetPlatformList().GetSize();
1035 }
1036 return 0;
1037}
1038
1039SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001040 LLDB_RECORD_METHOD(lldb::SBPlatform, SBDebugger, GetPlatformAtIndex,
1041 (uint32_t), idx);
1042
Vadim Macagonc10e34d2017-08-09 09:20:40 +00001043 SBPlatform sb_platform;
1044 if (m_opaque_sp) {
1045 // No need to lock, the platform list is thread safe
1046 sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx));
1047 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001048 return LLDB_RECORD_RESULT(sb_platform);
Vadim Macagonc10e34d2017-08-09 09:20:40 +00001049}
1050
1051uint32_t SBDebugger::GetNumAvailablePlatforms() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001052 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumAvailablePlatforms);
1053
Vadim Macagonc10e34d2017-08-09 09:20:40 +00001054 uint32_t idx = 0;
1055 while (true) {
1056 if (!PluginManager::GetPlatformPluginNameAtIndex(idx)) {
1057 break;
1058 }
1059 ++idx;
1060 }
1061 // +1 for the host platform, which should always appear first in the list.
1062 return idx + 1;
1063}
1064
1065SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001066 LLDB_RECORD_METHOD(lldb::SBStructuredData, SBDebugger,
1067 GetAvailablePlatformInfoAtIndex, (uint32_t), idx);
1068
Vadim Macagonc10e34d2017-08-09 09:20:40 +00001069 SBStructuredData data;
1070 auto platform_dict = llvm::make_unique<StructuredData::Dictionary>();
1071 llvm::StringRef name_str("name"), desc_str("description");
1072
1073 if (idx == 0) {
1074 PlatformSP host_platform_sp(Platform::GetHostPlatform());
1075 platform_dict->AddStringItem(
1076 name_str, host_platform_sp->GetPluginName().GetStringRef());
1077 platform_dict->AddStringItem(
1078 desc_str, llvm::StringRef(host_platform_sp->GetDescription()));
1079 } else if (idx > 0) {
1080 const char *plugin_name =
1081 PluginManager::GetPlatformPluginNameAtIndex(idx - 1);
1082 if (!plugin_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001083 return LLDB_RECORD_RESULT(data);
Vadim Macagonc10e34d2017-08-09 09:20:40 +00001084 }
1085 platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name));
1086
1087 const char *plugin_desc =
1088 PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1);
1089 if (!plugin_desc) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001090 return LLDB_RECORD_RESULT(data);
Vadim Macagonc10e34d2017-08-09 09:20:40 +00001091 }
1092 platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc));
1093 }
1094
1095 data.m_impl_up->SetObjectSP(
1096 StructuredData::ObjectSP(platform_dict.release()));
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001097 return LLDB_RECORD_RESULT(data);
Vadim Macagonc10e34d2017-08-09 09:20:40 +00001098}
1099
Kate Stoneb9c1b512016-09-06 20:57:50 +00001100void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) {
1101 DispatchInput(data, data_len);
1102}
1103
1104void SBDebugger::DispatchInput(const void *data, size_t data_len) {
1105 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1106 //
1107 // if (log)
1108 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\",
1109 // size_t=%" PRIu64 ")",
1110 // m_opaque_sp.get(),
1111 // (int) data_len,
1112 // (const char *) data,
1113 // (uint64_t)data_len);
1114 //
1115 // if (m_opaque_sp)
1116 // m_opaque_sp->DispatchInput ((const char *) data, data_len);
1117}
1118
1119void SBDebugger::DispatchInputInterrupt() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001120 LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, DispatchInputInterrupt);
1121
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 if (m_opaque_sp)
1123 m_opaque_sp->DispatchInputInterrupt();
1124}
1125
1126void SBDebugger::DispatchInputEndOfFile() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001127 LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, DispatchInputEndOfFile);
1128
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129 if (m_opaque_sp)
1130 m_opaque_sp->DispatchInputEndOfFile();
1131}
1132
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001133void SBDebugger::PushInputReader(SBInputReader &reader) {
1134 LLDB_RECORD_METHOD(void, SBDebugger, PushInputReader, (lldb::SBInputReader &),
1135 reader);
1136}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137
1138void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
1139 bool spawn_thread) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001140 LLDB_RECORD_METHOD(void, SBDebugger, RunCommandInterpreter, (bool, bool),
1141 auto_handle_events, spawn_thread);
1142
Kate Stoneb9c1b512016-09-06 20:57:50 +00001143 if (m_opaque_sp) {
1144 CommandInterpreterRunOptions options;
1145
1146 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(
1147 auto_handle_events, spawn_thread, options);
1148 }
1149}
1150
1151void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
1152 bool spawn_thread,
1153 SBCommandInterpreterRunOptions &options,
1154 int &num_errors, bool &quit_requested,
1155 bool &stopped_for_crash)
1156
1157{
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001158 LLDB_RECORD_METHOD(void, SBDebugger, RunCommandInterpreter,
1159 (bool, bool, lldb::SBCommandInterpreterRunOptions &, int &,
1160 bool &, bool &),
1161 auto_handle_events, spawn_thread, options, num_errors,
1162 quit_requested, stopped_for_crash);
1163
Kate Stoneb9c1b512016-09-06 20:57:50 +00001164 if (m_opaque_sp) {
1165 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
1166 interp.RunCommandInterpreter(auto_handle_events, spawn_thread,
1167 options.ref());
1168 num_errors = interp.GetNumErrors();
1169 quit_requested = interp.GetQuitRequested();
1170 stopped_for_crash = interp.GetStoppedForCrash();
1171 }
1172}
1173
1174SBError SBDebugger::RunREPL(lldb::LanguageType language,
1175 const char *repl_options) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001176 LLDB_RECORD_METHOD(lldb::SBError, SBDebugger, RunREPL,
1177 (lldb::LanguageType, const char *), language,
1178 repl_options);
1179
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 SBError error;
1181 if (m_opaque_sp)
1182 error.ref() = m_opaque_sp->RunREPL(language, repl_options);
1183 else
1184 error.SetErrorString("invalid debugger");
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001185 return LLDB_RECORD_RESULT(error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186}
1187
1188void SBDebugger::reset(const DebuggerSP &debugger_sp) {
1189 m_opaque_sp = debugger_sp;
1190}
1191
1192Debugger *SBDebugger::get() const { return m_opaque_sp.get(); }
1193
1194Debugger &SBDebugger::ref() const {
1195 assert(m_opaque_sp.get());
1196 return *m_opaque_sp;
1197}
1198
1199const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; }
1200
1201SBDebugger SBDebugger::FindDebuggerWithID(int id) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001202 LLDB_RECORD_STATIC_METHOD(lldb::SBDebugger, SBDebugger, FindDebuggerWithID,
1203 (int), id);
1204
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205 // No need to lock, the debugger list is thread safe
1206 SBDebugger sb_debugger;
1207 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id);
1208 if (debugger_sp)
1209 sb_debugger.reset(debugger_sp);
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001210 return LLDB_RECORD_RESULT(sb_debugger);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001211}
1212
1213const char *SBDebugger::GetInstanceName() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001214 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBDebugger, GetInstanceName);
1215
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216 return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr);
1217}
1218
1219SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
1220 const char *debugger_instance_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001221 LLDB_RECORD_STATIC_METHOD(lldb::SBError, SBDebugger, SetInternalVariable,
1222 (const char *, const char *, const char *),
1223 var_name, value, debugger_instance_name);
1224
Kate Stoneb9c1b512016-09-06 20:57:50 +00001225 SBError sb_error;
1226 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
1227 ConstString(debugger_instance_name)));
Zachary Turner97206d52017-05-12 04:51:55 +00001228 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001229 if (debugger_sp) {
1230 ExecutionContext exe_ctx(
1231 debugger_sp->GetCommandInterpreter().GetExecutionContext());
1232 error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
1233 var_name, value);
1234 } else {
1235 error.SetErrorStringWithFormat("invalid debugger instance name '%s'",
1236 debugger_instance_name);
1237 }
1238 if (error.Fail())
1239 sb_error.SetError(error);
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001240 return LLDB_RECORD_RESULT(sb_error);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001241}
1242
Greg Clayton431ce672011-04-18 23:15:17 +00001243SBStringList
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244SBDebugger::GetInternalVariableValue(const char *var_name,
1245 const char *debugger_instance_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001246 LLDB_RECORD_STATIC_METHOD(
1247 lldb::SBStringList, SBDebugger, GetInternalVariableValue,
1248 (const char *, const char *), var_name, debugger_instance_name);
1249
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 SBStringList ret_value;
1251 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
1252 ConstString(debugger_instance_name)));
Zachary Turner97206d52017-05-12 04:51:55 +00001253 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 if (debugger_sp) {
1255 ExecutionContext exe_ctx(
1256 debugger_sp->GetCommandInterpreter().GetExecutionContext());
1257 lldb::OptionValueSP value_sp(
1258 debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error));
1259 if (value_sp) {
1260 StreamString value_strm;
1261 value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
1262 const std::string &value_str = value_strm.GetString();
1263 if (!value_str.empty()) {
1264 StringList string_list;
1265 string_list.SplitIntoLines(value_str);
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001266 return LLDB_RECORD_RESULT(SBStringList(&string_list));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267 }
1268 }
1269 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001270 return LLDB_RECORD_RESULT(SBStringList());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271}
1272
1273uint32_t SBDebugger::GetTerminalWidth() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001274 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBDebugger, GetTerminalWidth);
1275
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
1277}
1278
1279void SBDebugger::SetTerminalWidth(uint32_t term_width) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001280 LLDB_RECORD_METHOD(void, SBDebugger, SetTerminalWidth, (uint32_t),
1281 term_width);
1282
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 if (m_opaque_sp)
1284 m_opaque_sp->SetTerminalWidth(term_width);
1285}
1286
1287const char *SBDebugger::GetPrompt() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001288 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBDebugger, GetPrompt);
1289
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1291
1292 if (log)
1293 log->Printf("SBDebugger(%p)::GetPrompt () => \"%s\"",
1294 static_cast<void *>(m_opaque_sp.get()),
Zachary Turner514d8cd2016-09-23 18:06:53 +00001295 (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : ""));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296
Zachary Turner514d8cd2016-09-23 18:06:53 +00001297 return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
1298 : nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299}
1300
1301void SBDebugger::SetPrompt(const char *prompt) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001302 LLDB_RECORD_METHOD(void, SBDebugger, SetPrompt, (const char *), prompt);
1303
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304 if (m_opaque_sp)
Zachary Turner514d8cd2016-09-23 18:06:53 +00001305 m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306}
1307
Jonas Devlieghere9e046f02018-11-13 19:18:16 +00001308const char *SBDebugger::GetReproducerPath() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001309 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBDebugger, GetReproducerPath);
1310
Jonas Devlieghere9e046f02018-11-13 19:18:16 +00001311 return (m_opaque_sp
1312 ? ConstString(m_opaque_sp->GetReproducerPath()).GetCString()
1313 : nullptr);
1314}
1315
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316ScriptLanguage SBDebugger::GetScriptLanguage() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001317 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::ScriptLanguage, SBDebugger,
1318 GetScriptLanguage);
1319
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
1321}
1322
1323void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001324 LLDB_RECORD_METHOD(void, SBDebugger, SetScriptLanguage,
1325 (lldb::ScriptLanguage), script_lang);
1326
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327 if (m_opaque_sp) {
1328 m_opaque_sp->SetScriptLanguage(script_lang);
1329 }
1330}
1331
1332bool SBDebugger::SetUseExternalEditor(bool value) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001333 LLDB_RECORD_METHOD(bool, SBDebugger, SetUseExternalEditor, (bool), value);
1334
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
1336}
1337
1338bool SBDebugger::GetUseExternalEditor() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001339 LLDB_RECORD_METHOD_NO_ARGS(bool, SBDebugger, GetUseExternalEditor);
1340
Kate Stoneb9c1b512016-09-06 20:57:50 +00001341 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
1342}
1343
1344bool SBDebugger::SetUseColor(bool value) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001345 LLDB_RECORD_METHOD(bool, SBDebugger, SetUseColor, (bool), value);
1346
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
1348}
1349
1350bool SBDebugger::GetUseColor() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001351 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, GetUseColor);
1352
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
1354}
1355
1356bool SBDebugger::GetDescription(SBStream &description) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001357 LLDB_RECORD_METHOD(bool, SBDebugger, GetDescription, (lldb::SBStream &),
1358 description);
1359
Kate Stoneb9c1b512016-09-06 20:57:50 +00001360 Stream &strm = description.ref();
1361
1362 if (m_opaque_sp) {
1363 const char *name = m_opaque_sp->GetInstanceName().AsCString();
1364 user_id_t id = m_opaque_sp->GetID();
1365 strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1366 } else
1367 strm.PutCString("No value");
1368
1369 return true;
1370}
1371
1372user_id_t SBDebugger::GetID() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001373 LLDB_RECORD_METHOD_NO_ARGS(lldb::user_id_t, SBDebugger, GetID);
1374
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
1376}
1377
1378SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001379 LLDB_RECORD_METHOD(lldb::SBError, SBDebugger, SetCurrentPlatform,
1380 (const char *), platform_name_cstr);
1381
Kate Stoneb9c1b512016-09-06 20:57:50 +00001382 SBError sb_error;
1383 if (m_opaque_sp) {
1384 if (platform_name_cstr && platform_name_cstr[0]) {
1385 ConstString platform_name(platform_name_cstr);
1386 PlatformSP platform_sp(Platform::Find(platform_name));
1387
1388 if (platform_sp) {
1389 // Already have a platform with this name, just select it
1390 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
1391 } else {
1392 // We don't have a platform by this name yet, create one
1393 platform_sp = Platform::Create(platform_name, sb_error.ref());
1394 if (platform_sp) {
1395 // We created the platform, now append and select it
1396 bool make_selected = true;
1397 m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected);
Greg Clayton6920b522012-08-22 18:39:03 +00001398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 }
1400 } else {
1401 sb_error.ref().SetErrorString("invalid platform name");
Greg Clayton6920b522012-08-22 18:39:03 +00001402 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 } else {
1404 sb_error.ref().SetErrorString("invalid debugger");
1405 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001406 return LLDB_RECORD_RESULT(sb_error);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001407}
1408
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001410 LLDB_RECORD_METHOD(bool, SBDebugger, SetCurrentPlatformSDKRoot,
1411 (const char *), sysroot);
1412
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1414 if (m_opaque_sp) {
1415 PlatformSP platform_sp(
1416 m_opaque_sp->GetPlatformList().GetSelectedPlatform());
Greg Claytona7015092010-09-18 01:14:36 +00001417
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 if (platform_sp) {
1419 if (log && sysroot)
1420 log->Printf("SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")", sysroot);
1421 platform_sp->SetSDKRootDirectory(ConstString(sysroot));
1422 return true;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001423 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 }
1425 return false;
Greg Claytona7015092010-09-18 01:14:36 +00001426}
1427
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428bool SBDebugger::GetCloseInputOnEOF() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001429 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, GetCloseInputOnEOF);
1430
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false);
Jim Inghame40e4212010-08-30 19:44:40 +00001432}
1433
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434void SBDebugger::SetCloseInputOnEOF(bool b) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001435 LLDB_RECORD_METHOD(void, SBDebugger, SetCloseInputOnEOF, (bool), b);
1436
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 if (m_opaque_sp)
1438 m_opaque_sp->SetCloseInputOnEOF(b);
Jim Inghame40e4212010-08-30 19:44:40 +00001439}
1440
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441SBTypeCategory SBDebugger::GetCategory(const char *category_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001442 LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory,
1443 (const char *), category_name);
1444
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 if (!category_name || *category_name == 0)
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001446 return LLDB_RECORD_RESULT(SBTypeCategory());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001447
1448 TypeCategoryImplSP category_sp;
1449
1450 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001451 category_sp, false)) {
1452 return LLDB_RECORD_RESULT(SBTypeCategory(category_sp));
1453 } else {
1454 return LLDB_RECORD_RESULT(SBTypeCategory());
1455 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001456}
1457
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001459 LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory,
1460 (lldb::LanguageType), lang_type);
1461
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 TypeCategoryImplSP category_sp;
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001463 if (DataVisualization::Categories::GetCategory(lang_type, category_sp)) {
1464 return LLDB_RECORD_RESULT(SBTypeCategory(category_sp));
1465 } else {
1466 return LLDB_RECORD_RESULT(SBTypeCategory());
1467 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001468}
1469
Kate Stoneb9c1b512016-09-06 20:57:50 +00001470SBTypeCategory SBDebugger::CreateCategory(const char *category_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001471 LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, CreateCategory,
1472 (const char *), category_name);
1473
Kate Stoneb9c1b512016-09-06 20:57:50 +00001474 if (!category_name || *category_name == 0)
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001475 return LLDB_RECORD_RESULT(SBTypeCategory());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001476
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477 TypeCategoryImplSP category_sp;
1478
1479 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001480 category_sp, true)) {
1481 return LLDB_RECORD_RESULT(SBTypeCategory(category_sp));
1482 } else {
1483 return LLDB_RECORD_RESULT(SBTypeCategory());
1484 }
Caroline Ticedde9cff2010-09-20 05:20:02 +00001485}
Caroline Ticeefed6132010-11-19 20:47:54 +00001486
Kate Stoneb9c1b512016-09-06 20:57:50 +00001487bool SBDebugger::DeleteCategory(const char *category_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001488 LLDB_RECORD_METHOD(bool, SBDebugger, DeleteCategory, (const char *),
1489 category_name);
1490
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 if (!category_name || *category_name == 0)
1492 return false;
1493
1494 return DataVisualization::Categories::Delete(ConstString(category_name));
Caroline Ticeefed6132010-11-19 20:47:54 +00001495}
Greg Clayton2289fa42011-04-30 01:09:13 +00001496
Kate Stoneb9c1b512016-09-06 20:57:50 +00001497uint32_t SBDebugger::GetNumCategories() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001498 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumCategories);
1499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500 return DataVisualization::Categories::GetCount();
Greg Clayton2289fa42011-04-30 01:09:13 +00001501}
1502
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001504 LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategoryAtIndex,
1505 (uint32_t), index);
1506
1507 return LLDB_RECORD_RESULT(
1508 SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001509}
1510
1511SBTypeCategory SBDebugger::GetDefaultCategory() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001512 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTypeCategory, SBDebugger,
1513 GetDefaultCategory);
1514
1515 return LLDB_RECORD_RESULT(GetCategory("default"));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516}
1517
1518SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001519 LLDB_RECORD_METHOD(lldb::SBTypeFormat, SBDebugger, GetFormatForType,
1520 (lldb::SBTypeNameSpecifier), type_name);
1521
Kate Stoneb9c1b512016-09-06 20:57:50 +00001522 SBTypeCategory default_category_sb = GetDefaultCategory();
1523 if (default_category_sb.GetEnabled())
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001524 return LLDB_RECORD_RESULT(default_category_sb.GetFormatForType(type_name));
1525 return LLDB_RECORD_RESULT(SBTypeFormat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526}
1527
1528#ifndef LLDB_DISABLE_PYTHON
1529SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001530 LLDB_RECORD_METHOD(lldb::SBTypeSummary, SBDebugger, GetSummaryForType,
1531 (lldb::SBTypeNameSpecifier), type_name);
1532
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 if (!type_name.IsValid())
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001534 return LLDB_RECORD_RESULT(SBTypeSummary());
1535 return LLDB_RECORD_RESULT(
1536 SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP())));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537}
1538#endif // LLDB_DISABLE_PYTHON
1539
1540SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001541 LLDB_RECORD_METHOD(lldb::SBTypeFilter, SBDebugger, GetFilterForType,
1542 (lldb::SBTypeNameSpecifier), type_name);
1543
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544 if (!type_name.IsValid())
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001545 return LLDB_RECORD_RESULT(SBTypeFilter());
1546 return LLDB_RECORD_RESULT(
1547 SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP())));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001548}
1549
1550#ifndef LLDB_DISABLE_PYTHON
1551SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001552 LLDB_RECORD_METHOD(lldb::SBTypeSynthetic, SBDebugger, GetSyntheticForType,
1553 (lldb::SBTypeNameSpecifier), type_name);
1554
Kate Stoneb9c1b512016-09-06 20:57:50 +00001555 if (!type_name.IsValid())
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001556 return LLDB_RECORD_RESULT(SBTypeSynthetic());
1557 return LLDB_RECORD_RESULT(SBTypeSynthetic(
1558 DataVisualization::GetSyntheticForType(type_name.GetSP())));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001559}
1560#endif // LLDB_DISABLE_PYTHON
1561
Pavel Labath5e336902017-03-01 10:08:40 +00001562static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) {
1563 if (categories == nullptr)
1564 return {};
1565 size_t len = 0;
1566 while (categories[len] != nullptr)
1567 ++len;
1568 return llvm::makeArrayRef(categories, len);
1569}
1570
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571bool SBDebugger::EnableLog(const char *channel, const char **categories) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +00001572 LLDB_RECORD_METHOD(bool, SBDebugger, EnableLog, (const char *, const char **),
1573 channel, categories);
1574
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 if (m_opaque_sp) {
1576 uint32_t log_options =
1577 LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
Pavel Labath775588c2017-03-15 09:06:58 +00001578 std::string error;
1579 llvm::raw_string_ostream error_stream(error);
Pavel Labath5e336902017-03-01 10:08:40 +00001580 return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
Pavel Labath775588c2017-03-15 09:06:58 +00001581 log_options, error_stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001582 } else
Greg Claytonf3dd93c2011-06-17 03:31:01 +00001583 return false;
1584}
1585
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
1587 void *baton) {
1588 if (m_opaque_sp) {
1589 return m_opaque_sp->SetLoggingCallback(log_callback, baton);
1590 }
Jim Ingham4f02b222012-02-22 22:49:20 +00001591}