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