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 //===----------------------------------------------------------------------===//
8 
9 #include "SBReproducerPrivate.h"
10 #include "SystemInitializerFull.h"
11 
12 #include "lldb/API/SBDebugger.h"
13 
14 #include "lldb/lldb-private.h"
15 
16 #include "lldb/API/SBBroadcaster.h"
17 #include "lldb/API/SBCommandInterpreter.h"
18 #include "lldb/API/SBCommandInterpreterRunOptions.h"
19 #include "lldb/API/SBCommandReturnObject.h"
20 #include "lldb/API/SBError.h"
21 #include "lldb/API/SBEvent.h"
22 #include "lldb/API/SBFile.h"
23 #include "lldb/API/SBFrame.h"
24 #include "lldb/API/SBListener.h"
25 #include "lldb/API/SBProcess.h"
26 #include "lldb/API/SBSourceManager.h"
27 #include "lldb/API/SBStream.h"
28 #include "lldb/API/SBStringList.h"
29 #include "lldb/API/SBStructuredData.h"
30 #include "lldb/API/SBTarget.h"
31 #include "lldb/API/SBThread.h"
32 #include "lldb/API/SBTypeCategory.h"
33 #include "lldb/API/SBTypeFilter.h"
34 #include "lldb/API/SBTypeFormat.h"
35 #include "lldb/API/SBTypeNameSpecifier.h"
36 #include "lldb/API/SBTypeSummary.h"
37 #include "lldb/API/SBTypeSynthetic.h"
38 
39 #include "lldb/Core/Debugger.h"
40 #include "lldb/Core/PluginManager.h"
41 #include "lldb/Core/StreamFile.h"
42 #include "lldb/Core/StructuredDataImpl.h"
43 #include "lldb/DataFormatters/DataVisualization.h"
44 #include "lldb/Host/Config.h"
45 #include "lldb/Host/XML.h"
46 #include "lldb/Initialization/SystemLifetimeManager.h"
47 #include "lldb/Interpreter/CommandInterpreter.h"
48 #include "lldb/Interpreter/OptionArgParser.h"
49 #include "lldb/Interpreter/OptionGroupPlatform.h"
50 #include "lldb/Target/Process.h"
51 #include "lldb/Target/TargetList.h"
52 #include "lldb/Utility/Args.h"
53 #include "lldb/Utility/State.h"
54 
55 #include "llvm/ADT/STLExtras.h"
56 #include "llvm/ADT/StringRef.h"
57 #include "llvm/Support/DynamicLibrary.h"
58 #include "llvm/Support/ManagedStatic.h"
59 
60 using namespace lldb;
61 using namespace lldb_private;
62 
LoadPlugin(const lldb::DebuggerSP & debugger_sp,const FileSpec & spec,Status & error)63 static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp,
64                                             const FileSpec &spec,
65                                             Status &error) {
66   llvm::sys::DynamicLibrary dynlib =
67       llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
68   if (dynlib.isValid()) {
69     typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger);
70 
71     lldb::SBDebugger debugger_sb(debugger_sp);
72     // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
73     // function.
74     // TODO: mangle this differently for your system - on OSX, the first
75     // underscore needs to be removed and the second one stays
76     LLDBCommandPluginInit init_func =
77         (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
78             "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
79     if (init_func) {
80       if (init_func(debugger_sb))
81         return dynlib;
82       else
83         error.SetErrorString("plug-in refused to load "
84                              "(lldb::PluginInitialize(lldb::SBDebugger) "
85                              "returned false)");
86     } else {
87       error.SetErrorString("plug-in is missing the required initialization: "
88                            "lldb::PluginInitialize(lldb::SBDebugger)");
89     }
90   } else {
91     if (FileSystem::Instance().Exists(spec))
92       error.SetErrorString("this file does not represent a loadable dylib");
93     else
94       error.SetErrorString("no such file");
95   }
96   return llvm::sys::DynamicLibrary();
97 }
98 
99 static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;
100 
Initialize(lldb::SBDebugger & sb_debugger,unsigned long (* callback)(void *,lldb::SBInputReader *,lldb::InputReaderAction,char const *,unsigned long),void * a,lldb::InputReaderGranularity b,char const * c,char const * d,bool e)101 SBError SBInputReader::Initialize(
102     lldb::SBDebugger &sb_debugger,
103     unsigned long (*callback)(void *, lldb::SBInputReader *,
104                               lldb::InputReaderAction, char const *,
105                               unsigned long),
106     void *a, lldb::InputReaderGranularity b, char const *c, char const *d,
107     bool e) {
108   LLDB_RECORD_DUMMY(
109       lldb::SBError, SBInputReader, Initialize,
110       (lldb::SBDebugger &,
111        unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction,
112                          const char *, unsigned long),
113        void *, lldb::InputReaderGranularity, const char *, const char *, bool),
114       sb_debugger, callback, a, b, c, d, e);
115 
116   return SBError();
117 }
118 
SetIsDone(bool b)119 void SBInputReader::SetIsDone(bool b) {
120   LLDB_RECORD_METHOD(void, SBInputReader, SetIsDone, (bool), b);
121 }
122 
IsActive() const123 bool SBInputReader::IsActive() const {
124   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBInputReader, IsActive);
125 
126   return false;
127 }
128 
SBDebugger()129 SBDebugger::SBDebugger() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBDebugger); }
130 
SBDebugger(const lldb::DebuggerSP & debugger_sp)131 SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp)
132     : m_opaque_sp(debugger_sp) {
133   LLDB_RECORD_CONSTRUCTOR(SBDebugger, (const lldb::DebuggerSP &), debugger_sp);
134 }
135 
SBDebugger(const SBDebugger & rhs)136 SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
137   LLDB_RECORD_CONSTRUCTOR(SBDebugger, (const lldb::SBDebugger &), rhs);
138 }
139 
140 SBDebugger::~SBDebugger() = default;
141 
operator =(const SBDebugger & rhs)142 SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) {
143   LLDB_RECORD_METHOD(lldb::SBDebugger &,
144                      SBDebugger, operator=,(const lldb::SBDebugger &), rhs);
145 
146   if (this != &rhs) {
147     m_opaque_sp = rhs.m_opaque_sp;
148   }
149   return LLDB_RECORD_RESULT(*this);
150 }
151 
Initialize()152 void SBDebugger::Initialize() {
153   LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, Initialize);
154   SBError ignored = SBDebugger::InitializeWithErrorHandling();
155 }
156 
InitializeWithErrorHandling()157 lldb::SBError SBDebugger::InitializeWithErrorHandling() {
158   LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBError, SBDebugger,
159                                     InitializeWithErrorHandling);
160 
161   SBError error;
162   if (auto e = g_debugger_lifetime->Initialize(
163           std::make_unique<SystemInitializerFull>(), LoadPlugin)) {
164     error.SetError(Status(std::move(e)));
165   }
166   return LLDB_RECORD_RESULT(error);
167 }
168 
Terminate()169 void SBDebugger::Terminate() {
170   LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, Terminate);
171 
172   g_debugger_lifetime->Terminate();
173 }
174 
Clear()175 void SBDebugger::Clear() {
176   LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, Clear);
177 
178   if (m_opaque_sp)
179     m_opaque_sp->ClearIOHandlers();
180 
181   m_opaque_sp.reset();
182 }
183 
Create()184 SBDebugger SBDebugger::Create() {
185   LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBDebugger, SBDebugger, Create);
186 
187   return LLDB_RECORD_RESULT(SBDebugger::Create(false, nullptr, nullptr));
188 }
189 
Create(bool source_init_files)190 SBDebugger SBDebugger::Create(bool source_init_files) {
191   LLDB_RECORD_STATIC_METHOD(lldb::SBDebugger, SBDebugger, Create, (bool),
192                             source_init_files);
193 
194   return LLDB_RECORD_RESULT(
195       SBDebugger::Create(source_init_files, nullptr, nullptr));
196 }
197 
Create(bool source_init_files,lldb::LogOutputCallback callback,void * baton)198 SBDebugger SBDebugger::Create(bool source_init_files,
199                               lldb::LogOutputCallback callback, void *baton)
200 
201 {
202   LLDB_RECORD_DUMMY(lldb::SBDebugger, SBDebugger, Create,
203                     (bool, lldb::LogOutputCallback, void *), source_init_files,
204                     callback, baton);
205 
206   SBDebugger debugger;
207 
208   // Currently we have issues if this function is called simultaneously on two
209   // different threads. The issues mainly revolve around the fact that the
210   // lldb_private::FormatManager uses global collections and having two threads
211   // parsing the .lldbinit files can cause mayhem. So to get around this for
212   // now we need to use a mutex to prevent bad things from happening.
213   static std::recursive_mutex g_mutex;
214   std::lock_guard<std::recursive_mutex> guard(g_mutex);
215 
216   debugger.reset(Debugger::CreateInstance(callback, baton));
217 
218   SBCommandInterpreter interp = debugger.GetCommandInterpreter();
219   if (source_init_files) {
220     interp.get()->SkipLLDBInitFiles(false);
221     interp.get()->SkipAppInitFiles(false);
222     SBCommandReturnObject result;
223     interp.SourceInitFileInHomeDirectory(result, false);
224   } else {
225     interp.get()->SkipLLDBInitFiles(true);
226     interp.get()->SkipAppInitFiles(true);
227   }
228   return debugger;
229 }
230 
Destroy(SBDebugger & debugger)231 void SBDebugger::Destroy(SBDebugger &debugger) {
232   LLDB_RECORD_STATIC_METHOD(void, SBDebugger, Destroy, (lldb::SBDebugger &),
233                             debugger);
234 
235   Debugger::Destroy(debugger.m_opaque_sp);
236 
237   if (debugger.m_opaque_sp.get() != nullptr)
238     debugger.m_opaque_sp.reset();
239 }
240 
MemoryPressureDetected()241 void SBDebugger::MemoryPressureDetected() {
242   LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, MemoryPressureDetected);
243 
244   // Since this function can be call asynchronously, we allow it to be non-
245   // mandatory. We have seen deadlocks with this function when called so we
246   // need to safeguard against this until we can determine what is causing the
247   // deadlocks.
248 
249   const bool mandatory = false;
250 
251   ModuleList::RemoveOrphanSharedModules(mandatory);
252 }
253 
IsValid() const254 bool SBDebugger::IsValid() const {
255   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, IsValid);
256   return this->operator bool();
257 }
operator bool() const258 SBDebugger::operator bool() const {
259   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, operator bool);
260 
261   return m_opaque_sp.get() != nullptr;
262 }
263 
SetAsync(bool b)264 void SBDebugger::SetAsync(bool b) {
265   LLDB_RECORD_METHOD(void, SBDebugger, SetAsync, (bool), b);
266 
267   if (m_opaque_sp)
268     m_opaque_sp->SetAsyncExecution(b);
269 }
270 
GetAsync()271 bool SBDebugger::GetAsync() {
272   LLDB_RECORD_METHOD_NO_ARGS(bool, SBDebugger, GetAsync);
273 
274   return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false);
275 }
276 
SkipLLDBInitFiles(bool b)277 void SBDebugger::SkipLLDBInitFiles(bool b) {
278   LLDB_RECORD_METHOD(void, SBDebugger, SkipLLDBInitFiles, (bool), b);
279 
280   if (m_opaque_sp)
281     m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b);
282 }
283 
SkipAppInitFiles(bool b)284 void SBDebugger::SkipAppInitFiles(bool b) {
285   LLDB_RECORD_METHOD(void, SBDebugger, SkipAppInitFiles, (bool), b);
286 
287   if (m_opaque_sp)
288     m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b);
289 }
290 
SetInputFileHandle(FILE * fh,bool transfer_ownership)291 void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) {
292   LLDB_RECORD_METHOD(void, SBDebugger, SetInputFileHandle, (FILE *, bool), fh,
293                      transfer_ownership);
294   SetInputFile((FileSP)std::make_shared<NativeFile>(fh, transfer_ownership));
295 }
296 
SetInputFile(FileSP file_sp)297 SBError SBDebugger::SetInputFile(FileSP file_sp) {
298   LLDB_RECORD_METHOD(SBError, SBDebugger, SetInputFile, (FileSP), file_sp);
299   return LLDB_RECORD_RESULT(SetInputFile(SBFile(file_sp)));
300 }
301 
302 // Shouldn't really be settable after initialization as this could cause lots
303 // of problems; don't want users trying to switch modes in the middle of a
304 // debugging session.
SetInputFile(SBFile file)305 SBError SBDebugger::SetInputFile(SBFile file) {
306   LLDB_RECORD_METHOD(SBError, SBDebugger, SetInputFile, (SBFile), file);
307 
308   SBError error;
309   if (!m_opaque_sp) {
310     error.ref().SetErrorString("invalid debugger");
311     return LLDB_RECORD_RESULT(error);
312   }
313 
314   repro::DataRecorder *recorder = nullptr;
315   if (repro::Generator *g = repro::Reproducer::Instance().GetGenerator())
316     recorder = g->GetOrCreate<repro::CommandProvider>().GetNewRecorder();
317 
318   FileSP file_sp = file.m_opaque_sp;
319 
320   static std::unique_ptr<repro::MultiLoader<repro::CommandProvider>> loader =
321       repro::MultiLoader<repro::CommandProvider>::Create(
322           repro::Reproducer::Instance().GetLoader());
323   if (loader) {
324     llvm::Optional<std::string> nextfile = loader->GetNextFile();
325     FILE *fh = nextfile ? FileSystem::Instance().Fopen(nextfile->c_str(), "r")
326                         : nullptr;
327     // FIXME Jonas Devlieghere: shouldn't this error be propagated out to the
328     // reproducer somehow if fh is NULL?
329     if (fh) {
330       file_sp = std::make_shared<NativeFile>(fh, true);
331     }
332   }
333 
334   if (!file_sp || !file_sp->IsValid()) {
335     error.ref().SetErrorString("invalid file");
336     return LLDB_RECORD_RESULT(error);
337   }
338 
339   m_opaque_sp->SetInputFile(file_sp, recorder);
340   return LLDB_RECORD_RESULT(error);
341 }
342 
SetOutputFile(FileSP file_sp)343 SBError SBDebugger::SetOutputFile(FileSP file_sp) {
344   LLDB_RECORD_METHOD(SBError, SBDebugger, SetOutputFile, (FileSP), file_sp);
345   return LLDB_RECORD_RESULT(SetOutputFile(SBFile(file_sp)));
346 }
347 
SetOutputFileHandle(FILE * fh,bool transfer_ownership)348 void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) {
349   LLDB_RECORD_METHOD(void, SBDebugger, SetOutputFileHandle, (FILE *, bool), fh,
350                      transfer_ownership);
351   SetOutputFile((FileSP)std::make_shared<NativeFile>(fh, transfer_ownership));
352 }
353 
SetOutputFile(SBFile file)354 SBError SBDebugger::SetOutputFile(SBFile file) {
355   LLDB_RECORD_METHOD(SBError, SBDebugger, SetOutputFile, (SBFile file), file);
356   SBError error;
357   if (!m_opaque_sp) {
358     error.ref().SetErrorString("invalid debugger");
359     return LLDB_RECORD_RESULT(error);
360   }
361   if (!file) {
362     error.ref().SetErrorString("invalid file");
363     return LLDB_RECORD_RESULT(error);
364   }
365   m_opaque_sp->SetOutputFile(file.m_opaque_sp);
366   return LLDB_RECORD_RESULT(error);
367 }
368 
SetErrorFileHandle(FILE * fh,bool transfer_ownership)369 void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) {
370   LLDB_RECORD_METHOD(void, SBDebugger, SetErrorFileHandle, (FILE *, bool), fh,
371                      transfer_ownership);
372   SetErrorFile((FileSP)std::make_shared<NativeFile>(fh, transfer_ownership));
373 }
374 
SetErrorFile(FileSP file_sp)375 SBError SBDebugger::SetErrorFile(FileSP file_sp) {
376   LLDB_RECORD_METHOD(SBError, SBDebugger, SetErrorFile, (FileSP), file_sp);
377   return LLDB_RECORD_RESULT(SetErrorFile(SBFile(file_sp)));
378 }
379 
SetErrorFile(SBFile file)380 SBError SBDebugger::SetErrorFile(SBFile file) {
381   LLDB_RECORD_METHOD(SBError, SBDebugger, SetErrorFile, (SBFile file), file);
382   SBError error;
383   if (!m_opaque_sp) {
384     error.ref().SetErrorString("invalid debugger");
385     return LLDB_RECORD_RESULT(error);
386   }
387   if (!file) {
388     error.ref().SetErrorString("invalid file");
389     return LLDB_RECORD_RESULT(error);
390   }
391   m_opaque_sp->SetErrorFile(file.m_opaque_sp);
392   return LLDB_RECORD_RESULT(error);
393 }
394 
GetInputFileHandle()395 FILE *SBDebugger::GetInputFileHandle() {
396   LLDB_RECORD_METHOD_NO_ARGS(FILE *, SBDebugger, GetInputFileHandle);
397   if (m_opaque_sp) {
398     File &file_sp = m_opaque_sp->GetInputFile();
399     return LLDB_RECORD_RESULT(file_sp.GetStream());
400   }
401   return LLDB_RECORD_RESULT(nullptr);
402 }
403 
GetInputFile()404 SBFile SBDebugger::GetInputFile() {
405   LLDB_RECORD_METHOD_NO_ARGS(SBFile, SBDebugger, GetInputFile);
406   if (m_opaque_sp) {
407     return LLDB_RECORD_RESULT(SBFile(m_opaque_sp->GetInputFileSP()));
408   }
409   return LLDB_RECORD_RESULT(SBFile());
410 }
411 
GetOutputFileHandle()412 FILE *SBDebugger::GetOutputFileHandle() {
413   LLDB_RECORD_METHOD_NO_ARGS(FILE *, SBDebugger, GetOutputFileHandle);
414   if (m_opaque_sp) {
415     StreamFile &stream_file = m_opaque_sp->GetOutputStream();
416     return LLDB_RECORD_RESULT(stream_file.GetFile().GetStream());
417   }
418   return LLDB_RECORD_RESULT(nullptr);
419 }
420 
GetOutputFile()421 SBFile SBDebugger::GetOutputFile() {
422   LLDB_RECORD_METHOD_NO_ARGS(SBFile, SBDebugger, GetOutputFile);
423   if (m_opaque_sp) {
424     SBFile file(m_opaque_sp->GetOutputStream().GetFileSP());
425     return LLDB_RECORD_RESULT(file);
426   }
427   return LLDB_RECORD_RESULT(SBFile());
428 }
429 
GetErrorFileHandle()430 FILE *SBDebugger::GetErrorFileHandle() {
431   LLDB_RECORD_METHOD_NO_ARGS(FILE *, SBDebugger, GetErrorFileHandle);
432 
433   if (m_opaque_sp) {
434     StreamFile &stream_file = m_opaque_sp->GetErrorStream();
435     return LLDB_RECORD_RESULT(stream_file.GetFile().GetStream());
436   }
437   return LLDB_RECORD_RESULT(nullptr);
438 }
439 
GetErrorFile()440 SBFile SBDebugger::GetErrorFile() {
441   LLDB_RECORD_METHOD_NO_ARGS(SBFile, SBDebugger, GetErrorFile);
442   SBFile file;
443   if (m_opaque_sp) {
444     SBFile file(m_opaque_sp->GetErrorStream().GetFileSP());
445     return LLDB_RECORD_RESULT(file);
446   }
447   return LLDB_RECORD_RESULT(SBFile());
448 }
449 
SaveInputTerminalState()450 void SBDebugger::SaveInputTerminalState() {
451   LLDB_RECORD_DUMMY_NO_ARGS(void, SBDebugger, SaveInputTerminalState);
452 
453   if (m_opaque_sp)
454     m_opaque_sp->SaveInputTerminalState();
455 }
456 
RestoreInputTerminalState()457 void SBDebugger::RestoreInputTerminalState() {
458   LLDB_RECORD_DUMMY_NO_ARGS(void, SBDebugger, RestoreInputTerminalState);
459 
460   if (m_opaque_sp)
461     m_opaque_sp->RestoreInputTerminalState();
462 }
GetCommandInterpreter()463 SBCommandInterpreter SBDebugger::GetCommandInterpreter() {
464   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBCommandInterpreter, SBDebugger,
465                              GetCommandInterpreter);
466 
467   SBCommandInterpreter sb_interpreter;
468   if (m_opaque_sp)
469     sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter());
470 
471   return LLDB_RECORD_RESULT(sb_interpreter);
472 }
473 
HandleCommand(const char * command)474 void SBDebugger::HandleCommand(const char *command) {
475   LLDB_RECORD_METHOD(void, SBDebugger, HandleCommand, (const char *), command);
476 
477   if (m_opaque_sp) {
478     TargetSP target_sp(m_opaque_sp->GetSelectedTarget());
479     std::unique_lock<std::recursive_mutex> lock;
480     if (target_sp)
481       lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
482 
483     SBCommandInterpreter sb_interpreter(GetCommandInterpreter());
484     SBCommandReturnObject result;
485 
486     sb_interpreter.HandleCommand(command, result, false);
487 
488     result.PutError(m_opaque_sp->GetErrorStream().GetFileSP());
489     result.PutOutput(m_opaque_sp->GetOutputStream().GetFileSP());
490 
491     if (!m_opaque_sp->GetAsyncExecution()) {
492       SBProcess process(GetCommandInterpreter().GetProcess());
493       ProcessSP process_sp(process.GetSP());
494       if (process_sp) {
495         EventSP event_sp;
496         ListenerSP lldb_listener_sp = m_opaque_sp->GetListener();
497         while (lldb_listener_sp->GetEventForBroadcaster(
498             process_sp.get(), event_sp, std::chrono::seconds(0))) {
499           SBEvent event(event_sp);
500           HandleProcessEvent(process, event, GetOutputFile(), GetErrorFile());
501         }
502       }
503     }
504   }
505 }
506 
GetListener()507 SBListener SBDebugger::GetListener() {
508   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBListener, SBDebugger, GetListener);
509 
510   SBListener sb_listener;
511   if (m_opaque_sp)
512     sb_listener.reset(m_opaque_sp->GetListener());
513 
514   return LLDB_RECORD_RESULT(sb_listener);
515 }
516 
HandleProcessEvent(const SBProcess & process,const SBEvent & event,SBFile out,SBFile err)517 void SBDebugger::HandleProcessEvent(const SBProcess &process,
518                                     const SBEvent &event, SBFile out,
519                                     SBFile err) {
520   LLDB_RECORD_METHOD(
521       void, SBDebugger, HandleProcessEvent,
522       (const lldb::SBProcess &, const lldb::SBEvent &, SBFile, SBFile), process,
523       event, out, err);
524 
525   return HandleProcessEvent(process, event, out.m_opaque_sp, err.m_opaque_sp);
526 }
527 
HandleProcessEvent(const SBProcess & process,const SBEvent & event,FILE * out,FILE * err)528 void SBDebugger::HandleProcessEvent(const SBProcess &process,
529                                     const SBEvent &event, FILE *out,
530                                     FILE *err) {
531   LLDB_RECORD_METHOD(
532       void, SBDebugger, HandleProcessEvent,
533       (const lldb::SBProcess &, const lldb::SBEvent &, FILE *, FILE *), process,
534       event, out, err);
535 
536   FileSP outfile = std::make_shared<NativeFile>(out, false);
537   FileSP errfile = std::make_shared<NativeFile>(err, false);
538   return HandleProcessEvent(process, event, outfile, errfile);
539 }
540 
HandleProcessEvent(const SBProcess & process,const SBEvent & event,FileSP out_sp,FileSP err_sp)541 void SBDebugger::HandleProcessEvent(const SBProcess &process,
542                                     const SBEvent &event, FileSP out_sp,
543                                     FileSP err_sp) {
544 
545   LLDB_RECORD_METHOD(
546       void, SBDebugger, HandleProcessEvent,
547       (const lldb::SBProcess &, const lldb::SBEvent &, FileSP, FileSP), process,
548       event, out_sp, err_sp);
549 
550   if (!process.IsValid())
551     return;
552 
553   TargetSP target_sp(process.GetTarget().GetSP());
554   if (!target_sp)
555     return;
556 
557   const uint32_t event_type = event.GetType();
558   char stdio_buffer[1024];
559   size_t len;
560 
561   std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
562 
563   if (event_type &
564       (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) {
565     // Drain stdout when we stop just in case we have any bytes
566     while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0)
567       if (out_sp)
568         out_sp->Write(stdio_buffer, len);
569   }
570 
571   if (event_type &
572       (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) {
573     // Drain stderr when we stop just in case we have any bytes
574     while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0)
575       if (err_sp)
576         err_sp->Write(stdio_buffer, len);
577   }
578 
579   if (event_type & Process::eBroadcastBitStateChanged) {
580     StateType event_state = SBProcess::GetStateFromEvent(event);
581 
582     if (event_state == eStateInvalid)
583       return;
584 
585     bool is_stopped = StateIsStoppedState(event_state);
586     if (!is_stopped)
587       process.ReportEventState(event, out_sp);
588   }
589 }
590 
GetSourceManager()591 SBSourceManager SBDebugger::GetSourceManager() {
592   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSourceManager, SBDebugger,
593                              GetSourceManager);
594 
595   SBSourceManager sb_source_manager(*this);
596   return LLDB_RECORD_RESULT(sb_source_manager);
597 }
598 
GetDefaultArchitecture(char * arch_name,size_t arch_name_len)599 bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) {
600   LLDB_RECORD_CHAR_PTR_STATIC_METHOD(bool, SBDebugger, GetDefaultArchitecture,
601                                      (char *, size_t), arch_name, "",
602                                      arch_name_len);
603 
604   if (arch_name && arch_name_len) {
605     ArchSpec default_arch = Target::GetDefaultArchitecture();
606 
607     if (default_arch.IsValid()) {
608       const std::string &triple_str = default_arch.GetTriple().str();
609       if (!triple_str.empty())
610         ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str());
611       else
612         ::snprintf(arch_name, arch_name_len, "%s",
613                    default_arch.GetArchitectureName());
614       return true;
615     }
616   }
617   if (arch_name && arch_name_len)
618     arch_name[0] = '\0';
619   return false;
620 }
621 
SetDefaultArchitecture(const char * arch_name)622 bool SBDebugger::SetDefaultArchitecture(const char *arch_name) {
623   LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, SetDefaultArchitecture,
624                             (const char *), arch_name);
625 
626   if (arch_name) {
627     ArchSpec arch(arch_name);
628     if (arch.IsValid()) {
629       Target::SetDefaultArchitecture(arch);
630       return true;
631     }
632   }
633   return false;
634 }
635 
636 ScriptLanguage
GetScriptingLanguage(const char * script_language_name)637 SBDebugger::GetScriptingLanguage(const char *script_language_name) {
638   LLDB_RECORD_METHOD(lldb::ScriptLanguage, SBDebugger, GetScriptingLanguage,
639                      (const char *), script_language_name);
640 
641   if (!script_language_name)
642     return eScriptLanguageDefault;
643   return OptionArgParser::ToScriptLanguage(
644       llvm::StringRef(script_language_name), eScriptLanguageDefault, nullptr);
645 }
646 
GetVersionString()647 const char *SBDebugger::GetVersionString() {
648   LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBDebugger, GetVersionString);
649 
650   return lldb_private::GetVersion();
651 }
652 
StateAsCString(StateType state)653 const char *SBDebugger::StateAsCString(StateType state) {
654   LLDB_RECORD_STATIC_METHOD(const char *, SBDebugger, StateAsCString,
655                             (lldb::StateType), state);
656 
657   return lldb_private::StateAsCString(state);
658 }
659 
AddBoolConfigEntry(StructuredData::Dictionary & dict,llvm::StringRef name,bool value,llvm::StringRef description)660 static void AddBoolConfigEntry(StructuredData::Dictionary &dict,
661                                llvm::StringRef name, bool value,
662                                llvm::StringRef description) {
663   auto entry_up = std::make_unique<StructuredData::Dictionary>();
664   entry_up->AddBooleanItem("value", value);
665   entry_up->AddStringItem("description", description);
666   dict.AddItem(name, std::move(entry_up));
667 }
668 
AddLLVMTargets(StructuredData::Dictionary & dict)669 static void AddLLVMTargets(StructuredData::Dictionary &dict) {
670   auto array_up = std::make_unique<StructuredData::Array>();
671 #define LLVM_TARGET(target)                                                    \
672   array_up->AddItem(std::make_unique<StructuredData::String>(#target));
673 #include "llvm/Config/Targets.def"
674   auto entry_up = std::make_unique<StructuredData::Dictionary>();
675   entry_up->AddItem("value", std::move(array_up));
676   entry_up->AddStringItem("description", "A list of configured LLVM targets.");
677   dict.AddItem("targets", std::move(entry_up));
678 }
679 
GetBuildConfiguration()680 SBStructuredData SBDebugger::GetBuildConfiguration() {
681   LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBStructuredData, SBDebugger,
682                                     GetBuildConfiguration);
683 
684   auto config_up = std::make_unique<StructuredData::Dictionary>();
685   AddBoolConfigEntry(
686       *config_up, "xml", XMLDocument::XMLEnabled(),
687       "A boolean value that indicates if XML support is enabled in LLDB");
688   AddBoolConfigEntry(
689       *config_up, "curses", LLDB_ENABLE_CURSES,
690       "A boolean value that indicates if curses support is enabled in LLDB");
691   AddBoolConfigEntry(
692       *config_up, "editline", LLDB_ENABLE_LIBEDIT,
693       "A boolean value that indicates if editline support is enabled in LLDB");
694   AddBoolConfigEntry(
695       *config_up, "lzma", LLDB_ENABLE_LZMA,
696       "A boolean value that indicates if lzma support is enabled in LLDB");
697   AddBoolConfigEntry(
698       *config_up, "python", LLDB_ENABLE_PYTHON,
699       "A boolean value that indicates if python support is enabled in LLDB");
700   AddBoolConfigEntry(
701       *config_up, "lua", LLDB_ENABLE_LUA,
702       "A boolean value that indicates if lua support is enabled in LLDB");
703   AddLLVMTargets(*config_up);
704 
705   SBStructuredData data;
706   data.m_impl_up->SetObjectSP(std::move(config_up));
707   return LLDB_RECORD_RESULT(data);
708 }
709 
StateIsRunningState(StateType state)710 bool SBDebugger::StateIsRunningState(StateType state) {
711   LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, StateIsRunningState,
712                             (lldb::StateType), state);
713 
714   const bool result = lldb_private::StateIsRunningState(state);
715 
716   return result;
717 }
718 
StateIsStoppedState(StateType state)719 bool SBDebugger::StateIsStoppedState(StateType state) {
720   LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, StateIsStoppedState,
721                             (lldb::StateType), state);
722 
723   const bool result = lldb_private::StateIsStoppedState(state, false);
724 
725   return result;
726 }
727 
CreateTarget(const char * filename,const char * target_triple,const char * platform_name,bool add_dependent_modules,lldb::SBError & sb_error)728 lldb::SBTarget SBDebugger::CreateTarget(const char *filename,
729                                         const char *target_triple,
730                                         const char *platform_name,
731                                         bool add_dependent_modules,
732                                         lldb::SBError &sb_error) {
733   LLDB_RECORD_METHOD(
734       lldb::SBTarget, SBDebugger, CreateTarget,
735       (const char *, const char *, const char *, bool, lldb::SBError &),
736       filename, target_triple, platform_name, add_dependent_modules, sb_error);
737 
738   SBTarget sb_target;
739   TargetSP target_sp;
740   if (m_opaque_sp) {
741     sb_error.Clear();
742     OptionGroupPlatform platform_options(false);
743     platform_options.SetPlatformName(platform_name);
744 
745     sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget(
746         *m_opaque_sp, filename, target_triple,
747         add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo,
748         &platform_options, target_sp);
749 
750     if (sb_error.Success())
751       sb_target.SetSP(target_sp);
752   } else {
753     sb_error.SetErrorString("invalid debugger");
754   }
755 
756   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
757   LLDB_LOGF(log,
758             "SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, "
759             "platform_name=%s, add_dependent_modules=%u, error=%s) => "
760             "SBTarget(%p)",
761             static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
762             platform_name, add_dependent_modules, sb_error.GetCString(),
763             static_cast<void *>(target_sp.get()));
764 
765   return LLDB_RECORD_RESULT(sb_target);
766 }
767 
768 SBTarget
CreateTargetWithFileAndTargetTriple(const char * filename,const char * target_triple)769 SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename,
770                                                 const char *target_triple) {
771   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger,
772                      CreateTargetWithFileAndTargetTriple,
773                      (const char *, const char *), filename, target_triple);
774 
775   SBTarget sb_target;
776   TargetSP target_sp;
777   if (m_opaque_sp) {
778     const bool add_dependent_modules = true;
779     Status error(m_opaque_sp->GetTargetList().CreateTarget(
780         *m_opaque_sp, filename, target_triple,
781         add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
782         target_sp));
783     sb_target.SetSP(target_sp);
784   }
785 
786   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
787   LLDB_LOGF(log,
788             "SBDebugger(%p)::CreateTargetWithFileAndTargetTriple "
789             "(filename=\"%s\", triple=%s) => SBTarget(%p)",
790             static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
791             static_cast<void *>(target_sp.get()));
792 
793   return LLDB_RECORD_RESULT(sb_target);
794 }
795 
CreateTargetWithFileAndArch(const char * filename,const char * arch_cstr)796 SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
797                                                  const char *arch_cstr) {
798   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, CreateTargetWithFileAndArch,
799                      (const char *, const char *), filename, arch_cstr);
800 
801   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
802 
803   SBTarget sb_target;
804   TargetSP target_sp;
805   if (m_opaque_sp) {
806     Status error;
807     const bool add_dependent_modules = true;
808 
809     error = m_opaque_sp->GetTargetList().CreateTarget(
810         *m_opaque_sp, filename, arch_cstr,
811         add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
812         target_sp);
813 
814     if (error.Success()) {
815       m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
816       sb_target.SetSP(target_sp);
817     }
818   }
819 
820   LLDB_LOGF(log,
821             "SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
822             "arch=%s) => SBTarget(%p)",
823             static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr,
824             static_cast<void *>(target_sp.get()));
825 
826   return LLDB_RECORD_RESULT(sb_target);
827 }
828 
CreateTarget(const char * filename)829 SBTarget SBDebugger::CreateTarget(const char *filename) {
830   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, CreateTarget, (const char *),
831                      filename);
832 
833   SBTarget sb_target;
834   TargetSP target_sp;
835   if (m_opaque_sp) {
836     Status error;
837     const bool add_dependent_modules = true;
838     error = m_opaque_sp->GetTargetList().CreateTarget(
839         *m_opaque_sp, filename, "",
840         add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
841         target_sp);
842 
843     if (error.Success()) {
844       m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
845       sb_target.SetSP(target_sp);
846     }
847   }
848   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
849   LLDB_LOGF(log,
850             "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
851             static_cast<void *>(m_opaque_sp.get()), filename,
852             static_cast<void *>(target_sp.get()));
853   return LLDB_RECORD_RESULT(sb_target);
854 }
855 
GetDummyTarget()856 SBTarget SBDebugger::GetDummyTarget() {
857   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTarget, SBDebugger, GetDummyTarget);
858 
859   SBTarget sb_target;
860   if (m_opaque_sp) {
861     sb_target.SetSP(m_opaque_sp->GetDummyTarget().shared_from_this());
862   }
863   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
864   LLDB_LOGF(log, "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)",
865             static_cast<void *>(m_opaque_sp.get()),
866             static_cast<void *>(sb_target.GetSP().get()));
867   return LLDB_RECORD_RESULT(sb_target);
868 }
869 
DeleteTarget(lldb::SBTarget & target)870 bool SBDebugger::DeleteTarget(lldb::SBTarget &target) {
871   LLDB_RECORD_METHOD(bool, SBDebugger, DeleteTarget, (lldb::SBTarget &),
872                      target);
873 
874   bool result = false;
875   if (m_opaque_sp) {
876     TargetSP target_sp(target.GetSP());
877     if (target_sp) {
878       // No need to lock, the target list is thread safe
879       result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp);
880       target_sp->Destroy();
881       target.Clear();
882     }
883   }
884 
885   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
886   LLDB_LOGF(log, "SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
887             static_cast<void *>(m_opaque_sp.get()),
888             static_cast<void *>(target.m_opaque_sp.get()), result);
889 
890   return result;
891 }
892 
GetTargetAtIndex(uint32_t idx)893 SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) {
894   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, GetTargetAtIndex, (uint32_t),
895                      idx);
896 
897   SBTarget sb_target;
898   if (m_opaque_sp) {
899     // No need to lock, the target list is thread safe
900     sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx));
901   }
902   return LLDB_RECORD_RESULT(sb_target);
903 }
904 
GetIndexOfTarget(lldb::SBTarget target)905 uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) {
906   LLDB_RECORD_METHOD(uint32_t, SBDebugger, GetIndexOfTarget, (lldb::SBTarget),
907                      target);
908 
909   lldb::TargetSP target_sp = target.GetSP();
910   if (!target_sp)
911     return UINT32_MAX;
912 
913   if (!m_opaque_sp)
914     return UINT32_MAX;
915 
916   return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP());
917 }
918 
FindTargetWithProcessID(lldb::pid_t pid)919 SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) {
920   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithProcessID,
921                      (lldb::pid_t), pid);
922 
923   SBTarget sb_target;
924   if (m_opaque_sp) {
925     // No need to lock, the target list is thread safe
926     sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid));
927   }
928   return LLDB_RECORD_RESULT(sb_target);
929 }
930 
FindTargetWithFileAndArch(const char * filename,const char * arch_name)931 SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename,
932                                                const char *arch_name) {
933   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithFileAndArch,
934                      (const char *, const char *), filename, arch_name);
935 
936   SBTarget sb_target;
937   if (m_opaque_sp && filename && filename[0]) {
938     // No need to lock, the target list is thread safe
939     ArchSpec arch = Platform::GetAugmentedArchSpec(
940         m_opaque_sp->GetPlatformList().GetSelectedPlatform().get(), arch_name);
941     TargetSP target_sp(
942         m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(
943             FileSpec(filename), arch_name ? &arch : nullptr));
944     sb_target.SetSP(target_sp);
945   }
946   return LLDB_RECORD_RESULT(sb_target);
947 }
948 
FindTargetWithLLDBProcess(const ProcessSP & process_sp)949 SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) {
950   SBTarget sb_target;
951   if (m_opaque_sp) {
952     // No need to lock, the target list is thread safe
953     sb_target.SetSP(
954         m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get()));
955   }
956   return sb_target;
957 }
958 
GetNumTargets()959 uint32_t SBDebugger::GetNumTargets() {
960   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumTargets);
961 
962   if (m_opaque_sp) {
963     // No need to lock, the target list is thread safe
964     return m_opaque_sp->GetTargetList().GetNumTargets();
965   }
966   return 0;
967 }
968 
GetSelectedTarget()969 SBTarget SBDebugger::GetSelectedTarget() {
970   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTarget, SBDebugger, GetSelectedTarget);
971 
972   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
973 
974   SBTarget sb_target;
975   TargetSP target_sp;
976   if (m_opaque_sp) {
977     // No need to lock, the target list is thread safe
978     target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget();
979     sb_target.SetSP(target_sp);
980   }
981 
982   if (log) {
983     SBStream sstr;
984     sb_target.GetDescription(sstr, eDescriptionLevelBrief);
985     LLDB_LOGF(log, "SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
986               static_cast<void *>(m_opaque_sp.get()),
987               static_cast<void *>(target_sp.get()), sstr.GetData());
988   }
989 
990   return LLDB_RECORD_RESULT(sb_target);
991 }
992 
SetSelectedTarget(SBTarget & sb_target)993 void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
994   LLDB_RECORD_METHOD(void, SBDebugger, SetSelectedTarget, (lldb::SBTarget &),
995                      sb_target);
996 
997   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
998 
999   TargetSP target_sp(sb_target.GetSP());
1000   if (m_opaque_sp) {
1001     m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
1002   }
1003   if (log) {
1004     SBStream sstr;
1005     sb_target.GetDescription(sstr, eDescriptionLevelBrief);
1006     LLDB_LOGF(log, "SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
1007               static_cast<void *>(m_opaque_sp.get()),
1008               static_cast<void *>(target_sp.get()), sstr.GetData());
1009   }
1010 }
1011 
GetSelectedPlatform()1012 SBPlatform SBDebugger::GetSelectedPlatform() {
1013   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBPlatform, SBDebugger, GetSelectedPlatform);
1014 
1015   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1016 
1017   SBPlatform sb_platform;
1018   DebuggerSP debugger_sp(m_opaque_sp);
1019   if (debugger_sp) {
1020     sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
1021   }
1022   LLDB_LOGF(log, "SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
1023             static_cast<void *>(m_opaque_sp.get()),
1024             static_cast<void *>(sb_platform.GetSP().get()),
1025             sb_platform.GetName());
1026   return LLDB_RECORD_RESULT(sb_platform);
1027 }
1028 
SetSelectedPlatform(SBPlatform & sb_platform)1029 void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) {
1030   LLDB_RECORD_METHOD(void, SBDebugger, SetSelectedPlatform,
1031                      (lldb::SBPlatform &), sb_platform);
1032 
1033   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1034 
1035   DebuggerSP debugger_sp(m_opaque_sp);
1036   if (debugger_sp) {
1037     debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
1038   }
1039 
1040   LLDB_LOGF(log, "SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
1041             static_cast<void *>(m_opaque_sp.get()),
1042             static_cast<void *>(sb_platform.GetSP().get()),
1043             sb_platform.GetName());
1044 }
1045 
GetNumPlatforms()1046 uint32_t SBDebugger::GetNumPlatforms() {
1047   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumPlatforms);
1048 
1049   if (m_opaque_sp) {
1050     // No need to lock, the platform list is thread safe
1051     return m_opaque_sp->GetPlatformList().GetSize();
1052   }
1053   return 0;
1054 }
1055 
GetPlatformAtIndex(uint32_t idx)1056 SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) {
1057   LLDB_RECORD_METHOD(lldb::SBPlatform, SBDebugger, GetPlatformAtIndex,
1058                      (uint32_t), idx);
1059 
1060   SBPlatform sb_platform;
1061   if (m_opaque_sp) {
1062     // No need to lock, the platform list is thread safe
1063     sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx));
1064   }
1065   return LLDB_RECORD_RESULT(sb_platform);
1066 }
1067 
GetNumAvailablePlatforms()1068 uint32_t SBDebugger::GetNumAvailablePlatforms() {
1069   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumAvailablePlatforms);
1070 
1071   uint32_t idx = 0;
1072   while (true) {
1073     if (!PluginManager::GetPlatformPluginNameAtIndex(idx)) {
1074       break;
1075     }
1076     ++idx;
1077   }
1078   // +1 for the host platform, which should always appear first in the list.
1079   return idx + 1;
1080 }
1081 
GetAvailablePlatformInfoAtIndex(uint32_t idx)1082 SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) {
1083   LLDB_RECORD_METHOD(lldb::SBStructuredData, SBDebugger,
1084                      GetAvailablePlatformInfoAtIndex, (uint32_t), idx);
1085 
1086   SBStructuredData data;
1087   auto platform_dict = std::make_unique<StructuredData::Dictionary>();
1088   llvm::StringRef name_str("name"), desc_str("description");
1089 
1090   if (idx == 0) {
1091     PlatformSP host_platform_sp(Platform::GetHostPlatform());
1092     platform_dict->AddStringItem(
1093         name_str, host_platform_sp->GetPluginName().GetStringRef());
1094     platform_dict->AddStringItem(
1095         desc_str, llvm::StringRef(host_platform_sp->GetDescription()));
1096   } else if (idx > 0) {
1097     const char *plugin_name =
1098         PluginManager::GetPlatformPluginNameAtIndex(idx - 1);
1099     if (!plugin_name) {
1100       return LLDB_RECORD_RESULT(data);
1101     }
1102     platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name));
1103 
1104     const char *plugin_desc =
1105         PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1);
1106     if (!plugin_desc) {
1107       return LLDB_RECORD_RESULT(data);
1108     }
1109     platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc));
1110   }
1111 
1112   data.m_impl_up->SetObjectSP(
1113       StructuredData::ObjectSP(platform_dict.release()));
1114   return LLDB_RECORD_RESULT(data);
1115 }
1116 
DispatchInput(void * baton,const void * data,size_t data_len)1117 void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) {
1118   LLDB_RECORD_DUMMY(void, SBDebugger, DispatchInput,
1119                     (void *, const void *, size_t), baton, data, data_len);
1120 
1121   DispatchInput(data, data_len);
1122 }
1123 
DispatchInput(const void * data,size_t data_len)1124 void SBDebugger::DispatchInput(const void *data, size_t data_len) {
1125   LLDB_RECORD_DUMMY(void, SBDebugger, DispatchInput, (const void *, size_t),
1126                     data, data_len);
1127 
1128   //    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1129   //
1130   //    if (log)
1131   //        LLDB_LOGF(log, "SBDebugger(%p)::DispatchInput (data=\"%.*s\",
1132   //        size_t=%" PRIu64 ")",
1133   //                     m_opaque_sp.get(),
1134   //                     (int) data_len,
1135   //                     (const char *) data,
1136   //                     (uint64_t)data_len);
1137   //
1138   //    if (m_opaque_sp)
1139   //        m_opaque_sp->DispatchInput ((const char *) data, data_len);
1140 }
1141 
DispatchInputInterrupt()1142 void SBDebugger::DispatchInputInterrupt() {
1143   LLDB_RECORD_DUMMY_NO_ARGS(void, SBDebugger, DispatchInputInterrupt);
1144 
1145   if (m_opaque_sp)
1146     m_opaque_sp->DispatchInputInterrupt();
1147 }
1148 
DispatchInputEndOfFile()1149 void SBDebugger::DispatchInputEndOfFile() {
1150   LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, DispatchInputEndOfFile);
1151 
1152   if (m_opaque_sp)
1153     m_opaque_sp->DispatchInputEndOfFile();
1154 }
1155 
PushInputReader(SBInputReader & reader)1156 void SBDebugger::PushInputReader(SBInputReader &reader) {
1157   LLDB_RECORD_METHOD(void, SBDebugger, PushInputReader, (lldb::SBInputReader &),
1158                      reader);
1159 }
1160 
RunCommandInterpreter(bool auto_handle_events,bool spawn_thread)1161 void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
1162                                        bool spawn_thread) {
1163   LLDB_RECORD_METHOD(void, SBDebugger, RunCommandInterpreter, (bool, bool),
1164                      auto_handle_events, spawn_thread);
1165 
1166   if (m_opaque_sp) {
1167     CommandInterpreterRunOptions options;
1168     options.SetAutoHandleEvents(auto_handle_events);
1169     options.SetSpawnThread(spawn_thread);
1170     m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(options);
1171   }
1172 }
1173 
RunCommandInterpreter(bool auto_handle_events,bool spawn_thread,SBCommandInterpreterRunOptions & options,int & num_errors,bool & quit_requested,bool & stopped_for_crash)1174 void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
1175                                        bool spawn_thread,
1176                                        SBCommandInterpreterRunOptions &options,
1177                                        int &num_errors, bool &quit_requested,
1178                                        bool &stopped_for_crash)
1179 
1180 {
1181   LLDB_RECORD_METHOD(void, SBDebugger, RunCommandInterpreter,
1182                      (bool, bool, lldb::SBCommandInterpreterRunOptions &, int &,
1183                       bool &, bool &),
1184                      auto_handle_events, spawn_thread, options, num_errors,
1185                      quit_requested, stopped_for_crash);
1186 
1187   if (m_opaque_sp) {
1188     options.SetAutoHandleEvents(auto_handle_events);
1189     options.SetSpawnThread(spawn_thread);
1190     CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
1191     CommandInterpreterRunResult result =
1192         interp.RunCommandInterpreter(options.ref());
1193     num_errors = result.GetNumErrors();
1194     quit_requested =
1195         result.IsResult(lldb::eCommandInterpreterResultQuitRequested);
1196     stopped_for_crash =
1197         result.IsResult(lldb::eCommandInterpreterResultInferiorCrash);
1198   }
1199 }
1200 
RunCommandInterpreter(const SBCommandInterpreterRunOptions & options)1201 SBCommandInterpreterRunResult SBDebugger::RunCommandInterpreter(
1202     const SBCommandInterpreterRunOptions &options) {
1203   LLDB_RECORD_METHOD(lldb::SBCommandInterpreterRunResult, SBDebugger,
1204                      RunCommandInterpreter,
1205                      (const lldb::SBCommandInterpreterRunOptions &), options);
1206 
1207   if (!m_opaque_sp)
1208     return LLDB_RECORD_RESULT(SBCommandInterpreterRunResult());
1209 
1210   CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
1211   CommandInterpreterRunResult result =
1212       interp.RunCommandInterpreter(options.ref());
1213 
1214   return LLDB_RECORD_RESULT(SBCommandInterpreterRunResult(result));
1215 }
1216 
RunREPL(lldb::LanguageType language,const char * repl_options)1217 SBError SBDebugger::RunREPL(lldb::LanguageType language,
1218                             const char *repl_options) {
1219   LLDB_RECORD_METHOD(lldb::SBError, SBDebugger, RunREPL,
1220                      (lldb::LanguageType, const char *), language,
1221                      repl_options);
1222 
1223   SBError error;
1224   if (m_opaque_sp)
1225     error.ref() = m_opaque_sp->RunREPL(language, repl_options);
1226   else
1227     error.SetErrorString("invalid debugger");
1228   return LLDB_RECORD_RESULT(error);
1229 }
1230 
reset(const DebuggerSP & debugger_sp)1231 void SBDebugger::reset(const DebuggerSP &debugger_sp) {
1232   m_opaque_sp = debugger_sp;
1233 }
1234 
get() const1235 Debugger *SBDebugger::get() const { return m_opaque_sp.get(); }
1236 
ref() const1237 Debugger &SBDebugger::ref() const {
1238   assert(m_opaque_sp.get());
1239   return *m_opaque_sp;
1240 }
1241 
get_sp() const1242 const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; }
1243 
FindDebuggerWithID(int id)1244 SBDebugger SBDebugger::FindDebuggerWithID(int id) {
1245   LLDB_RECORD_STATIC_METHOD(lldb::SBDebugger, SBDebugger, FindDebuggerWithID,
1246                             (int), id);
1247 
1248   // No need to lock, the debugger list is thread safe
1249   SBDebugger sb_debugger;
1250   DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id);
1251   if (debugger_sp)
1252     sb_debugger.reset(debugger_sp);
1253   return LLDB_RECORD_RESULT(sb_debugger);
1254 }
1255 
GetInstanceName()1256 const char *SBDebugger::GetInstanceName() {
1257   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBDebugger, GetInstanceName);
1258 
1259   return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr);
1260 }
1261 
SetInternalVariable(const char * var_name,const char * value,const char * debugger_instance_name)1262 SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
1263                                         const char *debugger_instance_name) {
1264   LLDB_RECORD_STATIC_METHOD(lldb::SBError, SBDebugger, SetInternalVariable,
1265                             (const char *, const char *, const char *),
1266                             var_name, value, debugger_instance_name);
1267 
1268   SBError sb_error;
1269   DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
1270       ConstString(debugger_instance_name)));
1271   Status error;
1272   if (debugger_sp) {
1273     ExecutionContext exe_ctx(
1274         debugger_sp->GetCommandInterpreter().GetExecutionContext());
1275     error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
1276                                           var_name, value);
1277   } else {
1278     error.SetErrorStringWithFormat("invalid debugger instance name '%s'",
1279                                    debugger_instance_name);
1280   }
1281   if (error.Fail())
1282     sb_error.SetError(error);
1283   return LLDB_RECORD_RESULT(sb_error);
1284 }
1285 
1286 SBStringList
GetInternalVariableValue(const char * var_name,const char * debugger_instance_name)1287 SBDebugger::GetInternalVariableValue(const char *var_name,
1288                                      const char *debugger_instance_name) {
1289   LLDB_RECORD_STATIC_METHOD(
1290       lldb::SBStringList, SBDebugger, GetInternalVariableValue,
1291       (const char *, const char *), var_name, debugger_instance_name);
1292 
1293   SBStringList ret_value;
1294   DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
1295       ConstString(debugger_instance_name)));
1296   Status error;
1297   if (debugger_sp) {
1298     ExecutionContext exe_ctx(
1299         debugger_sp->GetCommandInterpreter().GetExecutionContext());
1300     lldb::OptionValueSP value_sp(
1301         debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error));
1302     if (value_sp) {
1303       StreamString value_strm;
1304       value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
1305       const std::string &value_str = std::string(value_strm.GetString());
1306       if (!value_str.empty()) {
1307         StringList string_list;
1308         string_list.SplitIntoLines(value_str);
1309         return LLDB_RECORD_RESULT(SBStringList(&string_list));
1310       }
1311     }
1312   }
1313   return LLDB_RECORD_RESULT(SBStringList());
1314 }
1315 
GetTerminalWidth() const1316 uint32_t SBDebugger::GetTerminalWidth() const {
1317   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBDebugger, GetTerminalWidth);
1318 
1319   return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
1320 }
1321 
SetTerminalWidth(uint32_t term_width)1322 void SBDebugger::SetTerminalWidth(uint32_t term_width) {
1323   LLDB_RECORD_DUMMY(void, SBDebugger, SetTerminalWidth, (uint32_t), term_width);
1324 
1325   if (m_opaque_sp)
1326     m_opaque_sp->SetTerminalWidth(term_width);
1327 }
1328 
GetPrompt() const1329 const char *SBDebugger::GetPrompt() const {
1330   LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBDebugger, GetPrompt);
1331 
1332   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1333 
1334   LLDB_LOGF(log, "SBDebugger(%p)::GetPrompt () => \"%s\"",
1335             static_cast<void *>(m_opaque_sp.get()),
1336             (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : ""));
1337 
1338   return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
1339                       : nullptr);
1340 }
1341 
SetPrompt(const char * prompt)1342 void SBDebugger::SetPrompt(const char *prompt) {
1343   LLDB_RECORD_METHOD(void, SBDebugger, SetPrompt, (const char *), prompt);
1344 
1345   if (m_opaque_sp)
1346     m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt));
1347 }
1348 
GetReproducerPath() const1349 const char *SBDebugger::GetReproducerPath() const {
1350   LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBDebugger, GetReproducerPath);
1351 
1352   return (m_opaque_sp
1353               ? ConstString(m_opaque_sp->GetReproducerPath()).GetCString()
1354               : nullptr);
1355 }
1356 
GetScriptLanguage() const1357 ScriptLanguage SBDebugger::GetScriptLanguage() const {
1358   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::ScriptLanguage, SBDebugger,
1359                                    GetScriptLanguage);
1360 
1361   return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
1362 }
1363 
SetScriptLanguage(ScriptLanguage script_lang)1364 void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) {
1365   LLDB_RECORD_METHOD(void, SBDebugger, SetScriptLanguage,
1366                      (lldb::ScriptLanguage), script_lang);
1367 
1368   if (m_opaque_sp) {
1369     m_opaque_sp->SetScriptLanguage(script_lang);
1370   }
1371 }
1372 
SetUseExternalEditor(bool value)1373 bool SBDebugger::SetUseExternalEditor(bool value) {
1374   LLDB_RECORD_METHOD(bool, SBDebugger, SetUseExternalEditor, (bool), value);
1375 
1376   return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
1377 }
1378 
GetUseExternalEditor()1379 bool SBDebugger::GetUseExternalEditor() {
1380   LLDB_RECORD_METHOD_NO_ARGS(bool, SBDebugger, GetUseExternalEditor);
1381 
1382   return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
1383 }
1384 
SetUseColor(bool value)1385 bool SBDebugger::SetUseColor(bool value) {
1386   LLDB_RECORD_METHOD(bool, SBDebugger, SetUseColor, (bool), value);
1387 
1388   return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
1389 }
1390 
GetUseColor() const1391 bool SBDebugger::GetUseColor() const {
1392   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, GetUseColor);
1393 
1394   return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
1395 }
1396 
SetUseSourceCache(bool value)1397 bool SBDebugger::SetUseSourceCache(bool value) {
1398   LLDB_RECORD_METHOD(bool, SBDebugger, SetUseSourceCache, (bool), value);
1399 
1400   return (m_opaque_sp ? m_opaque_sp->SetUseSourceCache(value) : false);
1401 }
1402 
GetUseSourceCache() const1403 bool SBDebugger::GetUseSourceCache() const {
1404   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, GetUseSourceCache);
1405 
1406   return (m_opaque_sp ? m_opaque_sp->GetUseSourceCache() : false);
1407 }
1408 
GetDescription(SBStream & description)1409 bool SBDebugger::GetDescription(SBStream &description) {
1410   LLDB_RECORD_METHOD(bool, SBDebugger, GetDescription, (lldb::SBStream &),
1411                      description);
1412 
1413   Stream &strm = description.ref();
1414 
1415   if (m_opaque_sp) {
1416     const char *name = m_opaque_sp->GetInstanceName().AsCString();
1417     user_id_t id = m_opaque_sp->GetID();
1418     strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1419   } else
1420     strm.PutCString("No value");
1421 
1422   return true;
1423 }
1424 
GetID()1425 user_id_t SBDebugger::GetID() {
1426   LLDB_RECORD_METHOD_NO_ARGS(lldb::user_id_t, SBDebugger, GetID);
1427 
1428   return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
1429 }
1430 
SetCurrentPlatform(const char * platform_name_cstr)1431 SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
1432   LLDB_RECORD_METHOD(lldb::SBError, SBDebugger, SetCurrentPlatform,
1433                      (const char *), platform_name_cstr);
1434 
1435   SBError sb_error;
1436   if (m_opaque_sp) {
1437     if (platform_name_cstr && platform_name_cstr[0]) {
1438       ConstString platform_name(platform_name_cstr);
1439       PlatformSP platform_sp(Platform::Find(platform_name));
1440 
1441       if (platform_sp) {
1442         // Already have a platform with this name, just select it
1443         m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
1444       } else {
1445         // We don't have a platform by this name yet, create one
1446         platform_sp = Platform::Create(platform_name, sb_error.ref());
1447         if (platform_sp) {
1448           // We created the platform, now append and select it
1449           bool make_selected = true;
1450           m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected);
1451         }
1452       }
1453     } else {
1454       sb_error.ref().SetErrorString("invalid platform name");
1455     }
1456   } else {
1457     sb_error.ref().SetErrorString("invalid debugger");
1458   }
1459   return LLDB_RECORD_RESULT(sb_error);
1460 }
1461 
SetCurrentPlatformSDKRoot(const char * sysroot)1462 bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) {
1463   LLDB_RECORD_METHOD(bool, SBDebugger, SetCurrentPlatformSDKRoot,
1464                      (const char *), sysroot);
1465 
1466   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1467   if (m_opaque_sp) {
1468     PlatformSP platform_sp(
1469         m_opaque_sp->GetPlatformList().GetSelectedPlatform());
1470 
1471     if (platform_sp) {
1472       if (log && sysroot)
1473         LLDB_LOGF(log, "SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")",
1474                   sysroot);
1475       platform_sp->SetSDKRootDirectory(ConstString(sysroot));
1476       return true;
1477     }
1478   }
1479   return false;
1480 }
1481 
GetCloseInputOnEOF() const1482 bool SBDebugger::GetCloseInputOnEOF() const {
1483   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, GetCloseInputOnEOF);
1484 
1485   return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false);
1486 }
1487 
SetCloseInputOnEOF(bool b)1488 void SBDebugger::SetCloseInputOnEOF(bool b) {
1489   LLDB_RECORD_METHOD(void, SBDebugger, SetCloseInputOnEOF, (bool), b);
1490 
1491   if (m_opaque_sp)
1492     m_opaque_sp->SetCloseInputOnEOF(b);
1493 }
1494 
GetCategory(const char * category_name)1495 SBTypeCategory SBDebugger::GetCategory(const char *category_name) {
1496   LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory,
1497                      (const char *), category_name);
1498 
1499   if (!category_name || *category_name == 0)
1500     return LLDB_RECORD_RESULT(SBTypeCategory());
1501 
1502   TypeCategoryImplSP category_sp;
1503 
1504   if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1505                                                  category_sp, false)) {
1506     return LLDB_RECORD_RESULT(SBTypeCategory(category_sp));
1507   } else {
1508     return LLDB_RECORD_RESULT(SBTypeCategory());
1509   }
1510 }
1511 
GetCategory(lldb::LanguageType lang_type)1512 SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) {
1513   LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory,
1514                      (lldb::LanguageType), lang_type);
1515 
1516   TypeCategoryImplSP category_sp;
1517   if (DataVisualization::Categories::GetCategory(lang_type, category_sp)) {
1518     return LLDB_RECORD_RESULT(SBTypeCategory(category_sp));
1519   } else {
1520     return LLDB_RECORD_RESULT(SBTypeCategory());
1521   }
1522 }
1523 
CreateCategory(const char * category_name)1524 SBTypeCategory SBDebugger::CreateCategory(const char *category_name) {
1525   LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, CreateCategory,
1526                      (const char *), category_name);
1527 
1528   if (!category_name || *category_name == 0)
1529     return LLDB_RECORD_RESULT(SBTypeCategory());
1530 
1531   TypeCategoryImplSP category_sp;
1532 
1533   if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1534                                                  category_sp, true)) {
1535     return LLDB_RECORD_RESULT(SBTypeCategory(category_sp));
1536   } else {
1537     return LLDB_RECORD_RESULT(SBTypeCategory());
1538   }
1539 }
1540 
DeleteCategory(const char * category_name)1541 bool SBDebugger::DeleteCategory(const char *category_name) {
1542   LLDB_RECORD_METHOD(bool, SBDebugger, DeleteCategory, (const char *),
1543                      category_name);
1544 
1545   if (!category_name || *category_name == 0)
1546     return false;
1547 
1548   return DataVisualization::Categories::Delete(ConstString(category_name));
1549 }
1550 
GetNumCategories()1551 uint32_t SBDebugger::GetNumCategories() {
1552   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumCategories);
1553 
1554   return DataVisualization::Categories::GetCount();
1555 }
1556 
GetCategoryAtIndex(uint32_t index)1557 SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) {
1558   LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategoryAtIndex,
1559                      (uint32_t), index);
1560 
1561   return LLDB_RECORD_RESULT(
1562       SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index)));
1563 }
1564 
GetDefaultCategory()1565 SBTypeCategory SBDebugger::GetDefaultCategory() {
1566   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTypeCategory, SBDebugger,
1567                              GetDefaultCategory);
1568 
1569   return LLDB_RECORD_RESULT(GetCategory("default"));
1570 }
1571 
GetFormatForType(SBTypeNameSpecifier type_name)1572 SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) {
1573   LLDB_RECORD_METHOD(lldb::SBTypeFormat, SBDebugger, GetFormatForType,
1574                      (lldb::SBTypeNameSpecifier), type_name);
1575 
1576   SBTypeCategory default_category_sb = GetDefaultCategory();
1577   if (default_category_sb.GetEnabled())
1578     return LLDB_RECORD_RESULT(default_category_sb.GetFormatForType(type_name));
1579   return LLDB_RECORD_RESULT(SBTypeFormat());
1580 }
1581 
GetSummaryForType(SBTypeNameSpecifier type_name)1582 SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) {
1583   LLDB_RECORD_METHOD(lldb::SBTypeSummary, SBDebugger, GetSummaryForType,
1584                      (lldb::SBTypeNameSpecifier), type_name);
1585 
1586   if (!type_name.IsValid())
1587     return LLDB_RECORD_RESULT(SBTypeSummary());
1588   return LLDB_RECORD_RESULT(
1589       SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP())));
1590 }
1591 
GetFilterForType(SBTypeNameSpecifier type_name)1592 SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) {
1593   LLDB_RECORD_METHOD(lldb::SBTypeFilter, SBDebugger, GetFilterForType,
1594                      (lldb::SBTypeNameSpecifier), type_name);
1595 
1596   if (!type_name.IsValid())
1597     return LLDB_RECORD_RESULT(SBTypeFilter());
1598   return LLDB_RECORD_RESULT(
1599       SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP())));
1600 }
1601 
GetSyntheticForType(SBTypeNameSpecifier type_name)1602 SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) {
1603   LLDB_RECORD_METHOD(lldb::SBTypeSynthetic, SBDebugger, GetSyntheticForType,
1604                      (lldb::SBTypeNameSpecifier), type_name);
1605 
1606   if (!type_name.IsValid())
1607     return LLDB_RECORD_RESULT(SBTypeSynthetic());
1608   return LLDB_RECORD_RESULT(SBTypeSynthetic(
1609       DataVisualization::GetSyntheticForType(type_name.GetSP())));
1610 }
1611 
GetCategoryArray(const char ** categories)1612 static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) {
1613   if (categories == nullptr)
1614     return {};
1615   size_t len = 0;
1616   while (categories[len] != nullptr)
1617     ++len;
1618   return llvm::makeArrayRef(categories, len);
1619 }
1620 
EnableLog(const char * channel,const char ** categories)1621 bool SBDebugger::EnableLog(const char *channel, const char **categories) {
1622   LLDB_RECORD_METHOD(bool, SBDebugger, EnableLog, (const char *, const char **),
1623                      channel, categories);
1624 
1625   if (m_opaque_sp) {
1626     uint32_t log_options =
1627         LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1628     std::string error;
1629     llvm::raw_string_ostream error_stream(error);
1630     return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
1631                                   log_options, error_stream);
1632   } else
1633     return false;
1634 }
1635 
SetLoggingCallback(lldb::LogOutputCallback log_callback,void * baton)1636 void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
1637                                     void *baton) {
1638   LLDB_RECORD_DUMMY(void, SBDebugger, SetLoggingCallback,
1639                     (lldb::LogOutputCallback, void *), log_callback, baton);
1640 
1641   if (m_opaque_sp) {
1642     return m_opaque_sp->SetLoggingCallback(log_callback, baton);
1643   }
1644 }
1645 
1646 namespace lldb_private {
1647 namespace repro {
1648 
RegisterMethods(Registry & R)1649 template <> void RegisterMethods<SBInputReader>(Registry &R) {
1650   LLDB_REGISTER_METHOD(void, SBInputReader, SetIsDone, (bool));
1651   LLDB_REGISTER_METHOD_CONST(bool, SBInputReader, IsActive, ());
1652 }
1653 
SetFileHandleRedirect(SBDebugger *,FILE *,bool)1654 static void SetFileHandleRedirect(SBDebugger *, FILE *, bool) {
1655   // Do nothing.
1656 }
1657 
SetFileRedirect(SBDebugger *,SBFile file)1658 static SBError SetFileRedirect(SBDebugger *, SBFile file) { return SBError(); }
1659 
SetFileRedirect(SBDebugger *,FileSP file)1660 static SBError SetFileRedirect(SBDebugger *, FileSP file) { return SBError(); }
1661 
RegisterMethods(Registry & R)1662 template <> void RegisterMethods<SBDebugger>(Registry &R) {
1663   // Custom implementation.
1664   R.Register(&invoke<void (SBDebugger::*)(FILE *, bool)>::method<
1665                  &SBDebugger::SetErrorFileHandle>::record,
1666              &SetFileHandleRedirect);
1667   R.Register(&invoke<void (SBDebugger::*)(FILE *, bool)>::method<
1668                  &SBDebugger::SetOutputFileHandle>::record,
1669              &SetFileHandleRedirect);
1670 
1671   R.Register(&invoke<SBError (SBDebugger::*)(
1672                  SBFile)>::method<&SBDebugger::SetInputFile>::record,
1673              &SetFileRedirect);
1674   R.Register(&invoke<SBError (SBDebugger::*)(
1675                  SBFile)>::method<&SBDebugger::SetOutputFile>::record,
1676              &SetFileRedirect);
1677   R.Register(&invoke<SBError (SBDebugger::*)(
1678                  SBFile)>::method<&SBDebugger::SetErrorFile>::record,
1679              &SetFileRedirect);
1680 
1681   R.Register(&invoke<SBError (SBDebugger::*)(
1682                  FileSP)>::method<&SBDebugger::SetInputFile>::record,
1683              &SetFileRedirect);
1684   R.Register(&invoke<SBError (SBDebugger::*)(
1685                  FileSP)>::method<&SBDebugger::SetOutputFile>::record,
1686              &SetFileRedirect);
1687   R.Register(&invoke<SBError (SBDebugger::*)(
1688                  FileSP)>::method<&SBDebugger::SetErrorFile>::record,
1689              &SetFileRedirect);
1690 
1691   LLDB_REGISTER_CHAR_PTR_METHOD_STATIC(bool, SBDebugger,
1692                                        GetDefaultArchitecture);
1693 
1694   LLDB_REGISTER_CONSTRUCTOR(SBDebugger, ());
1695   LLDB_REGISTER_CONSTRUCTOR(SBDebugger, (const lldb::DebuggerSP &));
1696   LLDB_REGISTER_CONSTRUCTOR(SBDebugger, (const lldb::SBDebugger &));
1697   LLDB_REGISTER_METHOD(lldb::SBDebugger &,
1698                        SBDebugger, operator=,(const lldb::SBDebugger &));
1699   LLDB_REGISTER_STATIC_METHOD(void, SBDebugger, Initialize, ());
1700   LLDB_REGISTER_STATIC_METHOD(lldb::SBError, SBDebugger,
1701                               InitializeWithErrorHandling, ());
1702   LLDB_REGISTER_STATIC_METHOD(void, SBDebugger, Terminate, ());
1703   LLDB_REGISTER_METHOD(void, SBDebugger, Clear, ());
1704   LLDB_REGISTER_STATIC_METHOD(lldb::SBDebugger, SBDebugger, Create, ());
1705   LLDB_REGISTER_STATIC_METHOD(lldb::SBDebugger, SBDebugger, Create, (bool));
1706   LLDB_REGISTER_STATIC_METHOD(void, SBDebugger, Destroy, (lldb::SBDebugger &));
1707   LLDB_REGISTER_STATIC_METHOD(void, SBDebugger, MemoryPressureDetected, ());
1708   LLDB_REGISTER_METHOD_CONST(bool, SBDebugger, IsValid, ());
1709   LLDB_REGISTER_METHOD_CONST(bool, SBDebugger, operator bool,());
1710   LLDB_REGISTER_METHOD(void, SBDebugger, SetAsync, (bool));
1711   LLDB_REGISTER_METHOD(bool, SBDebugger, GetAsync, ());
1712   LLDB_REGISTER_METHOD(void, SBDebugger, SkipLLDBInitFiles, (bool));
1713   LLDB_REGISTER_METHOD(void, SBDebugger, SkipAppInitFiles, (bool));
1714   LLDB_REGISTER_METHOD(void, SBDebugger, SetInputFileHandle, (FILE *, bool));
1715   LLDB_REGISTER_METHOD(FILE *, SBDebugger, GetInputFileHandle, ());
1716   LLDB_REGISTER_METHOD(FILE *, SBDebugger, GetOutputFileHandle, ());
1717   LLDB_REGISTER_METHOD(FILE *, SBDebugger, GetErrorFileHandle, ());
1718   LLDB_REGISTER_METHOD(SBFile, SBDebugger, GetInputFile, ());
1719   LLDB_REGISTER_METHOD(SBFile, SBDebugger, GetOutputFile, ());
1720   LLDB_REGISTER_METHOD(SBFile, SBDebugger, GetErrorFile, ());
1721   LLDB_REGISTER_METHOD(void, SBDebugger, SaveInputTerminalState, ());
1722   LLDB_REGISTER_METHOD(void, SBDebugger, RestoreInputTerminalState, ());
1723   LLDB_REGISTER_METHOD(lldb::SBCommandInterpreter, SBDebugger,
1724                        GetCommandInterpreter, ());
1725   LLDB_REGISTER_METHOD(void, SBDebugger, HandleCommand, (const char *));
1726   LLDB_REGISTER_METHOD(lldb::SBListener, SBDebugger, GetListener, ());
1727   LLDB_REGISTER_METHOD(
1728       void, SBDebugger, HandleProcessEvent,
1729       (const lldb::SBProcess &, const lldb::SBEvent &, FILE *, FILE *));
1730   LLDB_REGISTER_METHOD(
1731       void, SBDebugger, HandleProcessEvent,
1732       (const lldb::SBProcess &, const lldb::SBEvent &, SBFile, SBFile));
1733   LLDB_REGISTER_METHOD(
1734       void, SBDebugger, HandleProcessEvent,
1735       (const lldb::SBProcess &, const lldb::SBEvent &, FileSP, FileSP));
1736   LLDB_REGISTER_METHOD(lldb::SBSourceManager, SBDebugger, GetSourceManager, ());
1737   LLDB_REGISTER_STATIC_METHOD(bool, SBDebugger, SetDefaultArchitecture,
1738                               (const char *));
1739   LLDB_REGISTER_METHOD(lldb::ScriptLanguage, SBDebugger, GetScriptingLanguage,
1740                        (const char *));
1741   LLDB_REGISTER_STATIC_METHOD(const char *, SBDebugger, GetVersionString, ());
1742   LLDB_REGISTER_STATIC_METHOD(const char *, SBDebugger, StateAsCString,
1743                               (lldb::StateType));
1744   LLDB_REGISTER_STATIC_METHOD(lldb::SBStructuredData, SBDebugger,
1745                               GetBuildConfiguration, ());
1746   LLDB_REGISTER_STATIC_METHOD(bool, SBDebugger, StateIsRunningState,
1747                               (lldb::StateType));
1748   LLDB_REGISTER_STATIC_METHOD(bool, SBDebugger, StateIsStoppedState,
1749                               (lldb::StateType));
1750   LLDB_REGISTER_METHOD(
1751       lldb::SBTarget, SBDebugger, CreateTarget,
1752       (const char *, const char *, const char *, bool, lldb::SBError &));
1753   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger,
1754                        CreateTargetWithFileAndTargetTriple,
1755                        (const char *, const char *));
1756   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, CreateTargetWithFileAndArch,
1757                        (const char *, const char *));
1758   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, CreateTarget,
1759                        (const char *));
1760   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, GetDummyTarget, ());
1761   LLDB_REGISTER_METHOD(bool, SBDebugger, DeleteTarget, (lldb::SBTarget &));
1762   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, GetTargetAtIndex,
1763                        (uint32_t));
1764   LLDB_REGISTER_METHOD(uint32_t, SBDebugger, GetIndexOfTarget,
1765                        (lldb::SBTarget));
1766   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithProcessID,
1767                        (lldb::pid_t));
1768   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithFileAndArch,
1769                        (const char *, const char *));
1770   LLDB_REGISTER_METHOD(uint32_t, SBDebugger, GetNumTargets, ());
1771   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, GetSelectedTarget, ());
1772   LLDB_REGISTER_METHOD(void, SBDebugger, SetSelectedTarget, (lldb::SBTarget &));
1773   LLDB_REGISTER_METHOD(lldb::SBPlatform, SBDebugger, GetSelectedPlatform, ());
1774   LLDB_REGISTER_METHOD(void, SBDebugger, SetSelectedPlatform,
1775                        (lldb::SBPlatform &));
1776   LLDB_REGISTER_METHOD(uint32_t, SBDebugger, GetNumPlatforms, ());
1777   LLDB_REGISTER_METHOD(lldb::SBPlatform, SBDebugger, GetPlatformAtIndex,
1778                        (uint32_t));
1779   LLDB_REGISTER_METHOD(uint32_t, SBDebugger, GetNumAvailablePlatforms, ());
1780   LLDB_REGISTER_METHOD(lldb::SBStructuredData, SBDebugger,
1781                        GetAvailablePlatformInfoAtIndex, (uint32_t));
1782   LLDB_REGISTER_METHOD(void, SBDebugger, DispatchInputInterrupt, ());
1783   LLDB_REGISTER_METHOD(void, SBDebugger, DispatchInputEndOfFile, ());
1784   LLDB_REGISTER_METHOD(void, SBDebugger, PushInputReader,
1785                        (lldb::SBInputReader &));
1786   LLDB_REGISTER_METHOD(void, SBDebugger, RunCommandInterpreter, (bool, bool));
1787   LLDB_REGISTER_METHOD(void, SBDebugger, RunCommandInterpreter,
1788                        (bool, bool, lldb::SBCommandInterpreterRunOptions &,
1789                         int &, bool &, bool &));
1790   LLDB_REGISTER_METHOD(lldb::SBError, SBDebugger, RunREPL,
1791                        (lldb::LanguageType, const char *));
1792   LLDB_REGISTER_STATIC_METHOD(lldb::SBDebugger, SBDebugger, FindDebuggerWithID,
1793                               (int));
1794   LLDB_REGISTER_METHOD(const char *, SBDebugger, GetInstanceName, ());
1795   LLDB_REGISTER_STATIC_METHOD(lldb::SBError, SBDebugger, SetInternalVariable,
1796                               (const char *, const char *, const char *));
1797   LLDB_REGISTER_STATIC_METHOD(lldb::SBStringList, SBDebugger,
1798                               GetInternalVariableValue,
1799                               (const char *, const char *));
1800   LLDB_REGISTER_METHOD_CONST(uint32_t, SBDebugger, GetTerminalWidth, ());
1801   LLDB_REGISTER_METHOD(void, SBDebugger, SetTerminalWidth, (uint32_t));
1802   LLDB_REGISTER_METHOD_CONST(const char *, SBDebugger, GetPrompt, ());
1803   LLDB_REGISTER_METHOD(void, SBDebugger, SetPrompt, (const char *));
1804   LLDB_REGISTER_METHOD_CONST(const char *, SBDebugger, GetReproducerPath, ());
1805   LLDB_REGISTER_METHOD_CONST(lldb::ScriptLanguage, SBDebugger,
1806                              GetScriptLanguage, ());
1807   LLDB_REGISTER_METHOD(void, SBDebugger, SetScriptLanguage,
1808                        (lldb::ScriptLanguage));
1809   LLDB_REGISTER_METHOD(bool, SBDebugger, SetUseExternalEditor, (bool));
1810   LLDB_REGISTER_METHOD(bool, SBDebugger, GetUseExternalEditor, ());
1811   LLDB_REGISTER_METHOD(bool, SBDebugger, SetUseColor, (bool));
1812   LLDB_REGISTER_METHOD_CONST(bool, SBDebugger, GetUseColor, ());
1813   LLDB_REGISTER_METHOD(bool, SBDebugger, GetDescription, (lldb::SBStream &));
1814   LLDB_REGISTER_METHOD(lldb::user_id_t, SBDebugger, GetID, ());
1815   LLDB_REGISTER_METHOD(lldb::SBError, SBDebugger, SetCurrentPlatform,
1816                        (const char *));
1817   LLDB_REGISTER_METHOD(bool, SBDebugger, SetCurrentPlatformSDKRoot,
1818                        (const char *));
1819   LLDB_REGISTER_METHOD_CONST(bool, SBDebugger, GetCloseInputOnEOF, ());
1820   LLDB_REGISTER_METHOD(void, SBDebugger, SetCloseInputOnEOF, (bool));
1821   LLDB_REGISTER_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory,
1822                        (const char *));
1823   LLDB_REGISTER_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory,
1824                        (lldb::LanguageType));
1825   LLDB_REGISTER_METHOD(lldb::SBTypeCategory, SBDebugger, CreateCategory,
1826                        (const char *));
1827   LLDB_REGISTER_METHOD(bool, SBDebugger, DeleteCategory, (const char *));
1828   LLDB_REGISTER_METHOD(uint32_t, SBDebugger, GetNumCategories, ());
1829   LLDB_REGISTER_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategoryAtIndex,
1830                        (uint32_t));
1831   LLDB_REGISTER_METHOD(lldb::SBTypeCategory, SBDebugger, GetDefaultCategory,
1832                        ());
1833   LLDB_REGISTER_METHOD(lldb::SBTypeFormat, SBDebugger, GetFormatForType,
1834                        (lldb::SBTypeNameSpecifier));
1835   LLDB_REGISTER_METHOD(lldb::SBTypeSummary, SBDebugger, GetSummaryForType,
1836                        (lldb::SBTypeNameSpecifier));
1837   LLDB_REGISTER_METHOD(lldb::SBTypeSynthetic, SBDebugger, GetSyntheticForType,
1838                        (lldb::SBTypeNameSpecifier));
1839   LLDB_REGISTER_METHOD(lldb::SBTypeFilter, SBDebugger, GetFilterForType,
1840                        (lldb::SBTypeNameSpecifier));
1841   LLDB_REGISTER_METHOD(bool, SBDebugger, EnableLog,
1842                        (const char *, const char **));
1843   LLDB_REGISTER_METHOD(lldb::SBCommandInterpreterRunResult, SBDebugger,
1844                        RunCommandInterpreter,
1845                        (const lldb::SBCommandInterpreterRunOptions &));
1846 }
1847 
1848 } // namespace repro
1849 } // namespace lldb_private
1850