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   AddBoolConfigEntry(*config_up, "fbsdvmcore", LLDB_ENABLE_FBSDVMCORE,
753                      "A boolean value that indicates if fbsdvmcore support is "
754                      "enabled in LLDB");
755   AddLLVMTargets(*config_up);
756 
757   SBStructuredData data;
758   data.m_impl_up->SetObjectSP(std::move(config_up));
759   return LLDB_RECORD_RESULT(data);
760 }
761 
762 bool SBDebugger::StateIsRunningState(StateType state) {
763   LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, StateIsRunningState,
764                             (lldb::StateType), state);
765 
766   const bool result = lldb_private::StateIsRunningState(state);
767 
768   return result;
769 }
770 
771 bool SBDebugger::StateIsStoppedState(StateType state) {
772   LLDB_RECORD_STATIC_METHOD(bool, SBDebugger, StateIsStoppedState,
773                             (lldb::StateType), state);
774 
775   const bool result = lldb_private::StateIsStoppedState(state, false);
776 
777   return result;
778 }
779 
780 lldb::SBTarget SBDebugger::CreateTarget(const char *filename,
781                                         const char *target_triple,
782                                         const char *platform_name,
783                                         bool add_dependent_modules,
784                                         lldb::SBError &sb_error) {
785   LLDB_RECORD_METHOD(
786       lldb::SBTarget, SBDebugger, CreateTarget,
787       (const char *, const char *, const char *, bool, lldb::SBError &),
788       filename, target_triple, platform_name, add_dependent_modules, sb_error);
789 
790   SBTarget sb_target;
791   TargetSP target_sp;
792   if (m_opaque_sp) {
793     sb_error.Clear();
794     OptionGroupPlatform platform_options(false);
795     platform_options.SetPlatformName(platform_name);
796 
797     sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget(
798         *m_opaque_sp, filename, target_triple,
799         add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo,
800         &platform_options, target_sp);
801 
802     if (sb_error.Success())
803       sb_target.SetSP(target_sp);
804   } else {
805     sb_error.SetErrorString("invalid debugger");
806   }
807 
808   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
809   LLDB_LOGF(log,
810             "SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, "
811             "platform_name=%s, add_dependent_modules=%u, error=%s) => "
812             "SBTarget(%p)",
813             static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
814             platform_name, add_dependent_modules, sb_error.GetCString(),
815             static_cast<void *>(target_sp.get()));
816 
817   return LLDB_RECORD_RESULT(sb_target);
818 }
819 
820 SBTarget
821 SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename,
822                                                 const char *target_triple) {
823   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger,
824                      CreateTargetWithFileAndTargetTriple,
825                      (const char *, const char *), filename, target_triple);
826 
827   SBTarget sb_target;
828   TargetSP target_sp;
829   if (m_opaque_sp) {
830     const bool add_dependent_modules = true;
831     Status error(m_opaque_sp->GetTargetList().CreateTarget(
832         *m_opaque_sp, filename, target_triple,
833         add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
834         target_sp));
835     sb_target.SetSP(target_sp);
836   }
837 
838   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
839   LLDB_LOGF(log,
840             "SBDebugger(%p)::CreateTargetWithFileAndTargetTriple "
841             "(filename=\"%s\", triple=%s) => SBTarget(%p)",
842             static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
843             static_cast<void *>(target_sp.get()));
844 
845   return LLDB_RECORD_RESULT(sb_target);
846 }
847 
848 SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
849                                                  const char *arch_cstr) {
850   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, CreateTargetWithFileAndArch,
851                      (const char *, const char *), filename, arch_cstr);
852 
853   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
854 
855   SBTarget sb_target;
856   TargetSP target_sp;
857   if (m_opaque_sp) {
858     Status error;
859     if (arch_cstr == nullptr) {
860       // The version of CreateTarget that takes an ArchSpec won't accept an
861       // empty ArchSpec, so when the arch hasn't been specified, we need to
862       // call the target triple version.
863       error = m_opaque_sp->GetTargetList().CreateTarget(*m_opaque_sp, filename,
864           arch_cstr, eLoadDependentsYes, nullptr, target_sp);
865     } else {
866       PlatformSP platform_sp = m_opaque_sp->GetPlatformList()
867           .GetSelectedPlatform();
868       ArchSpec arch = Platform::GetAugmentedArchSpec(platform_sp.get(),
869           arch_cstr);
870       if (arch.IsValid())
871         error = m_opaque_sp->GetTargetList().CreateTarget(*m_opaque_sp, filename,
872             arch, eLoadDependentsYes, platform_sp, target_sp);
873       else
874         error.SetErrorStringWithFormat("invalid arch_cstr: %s", arch_cstr);
875     }
876     if (error.Success())
877       sb_target.SetSP(target_sp);
878   }
879 
880   LLDB_LOGF(log,
881             "SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
882             "arch=%s) => SBTarget(%p)",
883             static_cast<void *>(m_opaque_sp.get()),
884             filename ? filename : "<unspecified>",
885             arch_cstr ? arch_cstr : "<unspecified>",
886             static_cast<void *>(target_sp.get()));
887 
888   return LLDB_RECORD_RESULT(sb_target);
889 }
890 
891 SBTarget SBDebugger::CreateTarget(const char *filename) {
892   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, CreateTarget, (const char *),
893                      filename);
894 
895   SBTarget sb_target;
896   TargetSP target_sp;
897   if (m_opaque_sp) {
898     Status error;
899     const bool add_dependent_modules = true;
900     error = m_opaque_sp->GetTargetList().CreateTarget(
901         *m_opaque_sp, filename, "",
902         add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
903         target_sp);
904 
905     if (error.Success())
906       sb_target.SetSP(target_sp);
907   }
908   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
909   LLDB_LOGF(log,
910             "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
911             static_cast<void *>(m_opaque_sp.get()), filename,
912             static_cast<void *>(target_sp.get()));
913   return LLDB_RECORD_RESULT(sb_target);
914 }
915 
916 SBTarget SBDebugger::GetDummyTarget() {
917   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTarget, SBDebugger, GetDummyTarget);
918 
919   SBTarget sb_target;
920   if (m_opaque_sp) {
921     sb_target.SetSP(m_opaque_sp->GetDummyTarget().shared_from_this());
922   }
923   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
924   LLDB_LOGF(log, "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)",
925             static_cast<void *>(m_opaque_sp.get()),
926             static_cast<void *>(sb_target.GetSP().get()));
927   return LLDB_RECORD_RESULT(sb_target);
928 }
929 
930 bool SBDebugger::DeleteTarget(lldb::SBTarget &target) {
931   LLDB_RECORD_METHOD(bool, SBDebugger, DeleteTarget, (lldb::SBTarget &),
932                      target);
933 
934   bool result = false;
935   if (m_opaque_sp) {
936     TargetSP target_sp(target.GetSP());
937     if (target_sp) {
938       // No need to lock, the target list is thread safe
939       result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp);
940       target_sp->Destroy();
941       target.Clear();
942     }
943   }
944 
945   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
946   LLDB_LOGF(log, "SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
947             static_cast<void *>(m_opaque_sp.get()),
948             static_cast<void *>(target.m_opaque_sp.get()), result);
949 
950   return result;
951 }
952 
953 SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) {
954   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, GetTargetAtIndex, (uint32_t),
955                      idx);
956 
957   SBTarget sb_target;
958   if (m_opaque_sp) {
959     // No need to lock, the target list is thread safe
960     sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx));
961   }
962   return LLDB_RECORD_RESULT(sb_target);
963 }
964 
965 uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) {
966   LLDB_RECORD_METHOD(uint32_t, SBDebugger, GetIndexOfTarget, (lldb::SBTarget),
967                      target);
968 
969   lldb::TargetSP target_sp = target.GetSP();
970   if (!target_sp)
971     return UINT32_MAX;
972 
973   if (!m_opaque_sp)
974     return UINT32_MAX;
975 
976   return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP());
977 }
978 
979 SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) {
980   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithProcessID,
981                      (lldb::pid_t), pid);
982 
983   SBTarget sb_target;
984   if (m_opaque_sp) {
985     // No need to lock, the target list is thread safe
986     sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid));
987   }
988   return LLDB_RECORD_RESULT(sb_target);
989 }
990 
991 SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename,
992                                                const char *arch_name) {
993   LLDB_RECORD_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithFileAndArch,
994                      (const char *, const char *), filename, arch_name);
995 
996   SBTarget sb_target;
997   if (m_opaque_sp && filename && filename[0]) {
998     // No need to lock, the target list is thread safe
999     ArchSpec arch = Platform::GetAugmentedArchSpec(
1000         m_opaque_sp->GetPlatformList().GetSelectedPlatform().get(), arch_name);
1001     TargetSP target_sp(
1002         m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(
1003             FileSpec(filename), arch_name ? &arch : nullptr));
1004     sb_target.SetSP(target_sp);
1005   }
1006   return LLDB_RECORD_RESULT(sb_target);
1007 }
1008 
1009 SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) {
1010   SBTarget sb_target;
1011   if (m_opaque_sp) {
1012     // No need to lock, the target list is thread safe
1013     sb_target.SetSP(
1014         m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get()));
1015   }
1016   return sb_target;
1017 }
1018 
1019 uint32_t SBDebugger::GetNumTargets() {
1020   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumTargets);
1021 
1022   if (m_opaque_sp) {
1023     // No need to lock, the target list is thread safe
1024     return m_opaque_sp->GetTargetList().GetNumTargets();
1025   }
1026   return 0;
1027 }
1028 
1029 SBTarget SBDebugger::GetSelectedTarget() {
1030   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTarget, SBDebugger, GetSelectedTarget);
1031 
1032   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1033 
1034   SBTarget sb_target;
1035   TargetSP target_sp;
1036   if (m_opaque_sp) {
1037     // No need to lock, the target list is thread safe
1038     target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget();
1039     sb_target.SetSP(target_sp);
1040   }
1041 
1042   if (log) {
1043     SBStream sstr;
1044     sb_target.GetDescription(sstr, eDescriptionLevelBrief);
1045     LLDB_LOGF(log, "SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
1046               static_cast<void *>(m_opaque_sp.get()),
1047               static_cast<void *>(target_sp.get()), sstr.GetData());
1048   }
1049 
1050   return LLDB_RECORD_RESULT(sb_target);
1051 }
1052 
1053 void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
1054   LLDB_RECORD_METHOD(void, SBDebugger, SetSelectedTarget, (lldb::SBTarget &),
1055                      sb_target);
1056 
1057   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1058 
1059   TargetSP target_sp(sb_target.GetSP());
1060   if (m_opaque_sp) {
1061     m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp);
1062   }
1063   if (log) {
1064     SBStream sstr;
1065     sb_target.GetDescription(sstr, eDescriptionLevelBrief);
1066     LLDB_LOGF(log, "SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
1067               static_cast<void *>(m_opaque_sp.get()),
1068               static_cast<void *>(target_sp.get()), sstr.GetData());
1069   }
1070 }
1071 
1072 SBPlatform SBDebugger::GetSelectedPlatform() {
1073   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBPlatform, SBDebugger, GetSelectedPlatform);
1074 
1075   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1076 
1077   SBPlatform sb_platform;
1078   DebuggerSP debugger_sp(m_opaque_sp);
1079   if (debugger_sp) {
1080     sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
1081   }
1082   LLDB_LOGF(log, "SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
1083             static_cast<void *>(m_opaque_sp.get()),
1084             static_cast<void *>(sb_platform.GetSP().get()),
1085             sb_platform.GetName());
1086   return LLDB_RECORD_RESULT(sb_platform);
1087 }
1088 
1089 void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) {
1090   LLDB_RECORD_METHOD(void, SBDebugger, SetSelectedPlatform,
1091                      (lldb::SBPlatform &), sb_platform);
1092 
1093   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1094 
1095   DebuggerSP debugger_sp(m_opaque_sp);
1096   if (debugger_sp) {
1097     debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
1098   }
1099 
1100   LLDB_LOGF(log, "SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
1101             static_cast<void *>(m_opaque_sp.get()),
1102             static_cast<void *>(sb_platform.GetSP().get()),
1103             sb_platform.GetName());
1104 }
1105 
1106 uint32_t SBDebugger::GetNumPlatforms() {
1107   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumPlatforms);
1108 
1109   if (m_opaque_sp) {
1110     // No need to lock, the platform list is thread safe
1111     return m_opaque_sp->GetPlatformList().GetSize();
1112   }
1113   return 0;
1114 }
1115 
1116 SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) {
1117   LLDB_RECORD_METHOD(lldb::SBPlatform, SBDebugger, GetPlatformAtIndex,
1118                      (uint32_t), idx);
1119 
1120   SBPlatform sb_platform;
1121   if (m_opaque_sp) {
1122     // No need to lock, the platform list is thread safe
1123     sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx));
1124   }
1125   return LLDB_RECORD_RESULT(sb_platform);
1126 }
1127 
1128 uint32_t SBDebugger::GetNumAvailablePlatforms() {
1129   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumAvailablePlatforms);
1130 
1131   uint32_t idx = 0;
1132   while (true) {
1133     if (PluginManager::GetPlatformPluginNameAtIndex(idx).empty()) {
1134       break;
1135     }
1136     ++idx;
1137   }
1138   // +1 for the host platform, which should always appear first in the list.
1139   return idx + 1;
1140 }
1141 
1142 SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) {
1143   LLDB_RECORD_METHOD(lldb::SBStructuredData, SBDebugger,
1144                      GetAvailablePlatformInfoAtIndex, (uint32_t), idx);
1145 
1146   SBStructuredData data;
1147   auto platform_dict = std::make_unique<StructuredData::Dictionary>();
1148   llvm::StringRef name_str("name"), desc_str("description");
1149 
1150   if (idx == 0) {
1151     PlatformSP host_platform_sp(Platform::GetHostPlatform());
1152     platform_dict->AddStringItem(name_str, host_platform_sp->GetPluginName());
1153     platform_dict->AddStringItem(
1154         desc_str, llvm::StringRef(host_platform_sp->GetDescription()));
1155   } else if (idx > 0) {
1156     llvm::StringRef plugin_name =
1157         PluginManager::GetPlatformPluginNameAtIndex(idx - 1);
1158     if (plugin_name.empty()) {
1159       return LLDB_RECORD_RESULT(data);
1160     }
1161     platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name));
1162 
1163     llvm::StringRef plugin_desc =
1164         PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1);
1165     platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc));
1166   }
1167 
1168   data.m_impl_up->SetObjectSP(
1169       StructuredData::ObjectSP(platform_dict.release()));
1170   return LLDB_RECORD_RESULT(data);
1171 }
1172 
1173 void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) {
1174   LLDB_RECORD_DUMMY(void, SBDebugger, DispatchInput,
1175                     (void *, const void *, size_t), baton, data, data_len);
1176 
1177   DispatchInput(data, data_len);
1178 }
1179 
1180 void SBDebugger::DispatchInput(const void *data, size_t data_len) {
1181   LLDB_RECORD_DUMMY(void, SBDebugger, DispatchInput, (const void *, size_t),
1182                     data, data_len);
1183 
1184   //    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1185   //
1186   //    if (log)
1187   //        LLDB_LOGF(log, "SBDebugger(%p)::DispatchInput (data=\"%.*s\",
1188   //        size_t=%" PRIu64 ")",
1189   //                     m_opaque_sp.get(),
1190   //                     (int) data_len,
1191   //                     (const char *) data,
1192   //                     (uint64_t)data_len);
1193   //
1194   //    if (m_opaque_sp)
1195   //        m_opaque_sp->DispatchInput ((const char *) data, data_len);
1196 }
1197 
1198 void SBDebugger::DispatchInputInterrupt() {
1199   LLDB_RECORD_DUMMY_NO_ARGS(void, SBDebugger, DispatchInputInterrupt);
1200 
1201   if (m_opaque_sp)
1202     m_opaque_sp->DispatchInputInterrupt();
1203 }
1204 
1205 void SBDebugger::DispatchInputEndOfFile() {
1206   LLDB_RECORD_METHOD_NO_ARGS(void, SBDebugger, DispatchInputEndOfFile);
1207 
1208   if (m_opaque_sp)
1209     m_opaque_sp->DispatchInputEndOfFile();
1210 }
1211 
1212 void SBDebugger::PushInputReader(SBInputReader &reader) {
1213   LLDB_RECORD_METHOD(void, SBDebugger, PushInputReader, (lldb::SBInputReader &),
1214                      reader);
1215 }
1216 
1217 void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
1218                                        bool spawn_thread) {
1219   LLDB_RECORD_METHOD(void, SBDebugger, RunCommandInterpreter, (bool, bool),
1220                      auto_handle_events, spawn_thread);
1221 
1222   if (m_opaque_sp) {
1223     CommandInterpreterRunOptions options;
1224     options.SetAutoHandleEvents(auto_handle_events);
1225     options.SetSpawnThread(spawn_thread);
1226     m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(options);
1227   }
1228 }
1229 
1230 void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
1231                                        bool spawn_thread,
1232                                        SBCommandInterpreterRunOptions &options,
1233                                        int &num_errors, bool &quit_requested,
1234                                        bool &stopped_for_crash)
1235 
1236 {
1237   LLDB_RECORD_METHOD(void, SBDebugger, RunCommandInterpreter,
1238                      (bool, bool, lldb::SBCommandInterpreterRunOptions &, int &,
1239                       bool &, bool &),
1240                      auto_handle_events, spawn_thread, options, num_errors,
1241                      quit_requested, stopped_for_crash);
1242 
1243   if (m_opaque_sp) {
1244     options.SetAutoHandleEvents(auto_handle_events);
1245     options.SetSpawnThread(spawn_thread);
1246     CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
1247     CommandInterpreterRunResult result =
1248         interp.RunCommandInterpreter(options.ref());
1249     num_errors = result.GetNumErrors();
1250     quit_requested =
1251         result.IsResult(lldb::eCommandInterpreterResultQuitRequested);
1252     stopped_for_crash =
1253         result.IsResult(lldb::eCommandInterpreterResultInferiorCrash);
1254   }
1255 }
1256 
1257 SBCommandInterpreterRunResult SBDebugger::RunCommandInterpreter(
1258     const SBCommandInterpreterRunOptions &options) {
1259   LLDB_RECORD_METHOD(lldb::SBCommandInterpreterRunResult, SBDebugger,
1260                      RunCommandInterpreter,
1261                      (const lldb::SBCommandInterpreterRunOptions &), options);
1262 
1263   if (!m_opaque_sp)
1264     return LLDB_RECORD_RESULT(SBCommandInterpreterRunResult());
1265 
1266   CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
1267   CommandInterpreterRunResult result =
1268       interp.RunCommandInterpreter(options.ref());
1269 
1270   return LLDB_RECORD_RESULT(SBCommandInterpreterRunResult(result));
1271 }
1272 
1273 SBError SBDebugger::RunREPL(lldb::LanguageType language,
1274                             const char *repl_options) {
1275   LLDB_RECORD_METHOD(lldb::SBError, SBDebugger, RunREPL,
1276                      (lldb::LanguageType, const char *), language,
1277                      repl_options);
1278 
1279   SBError error;
1280   if (m_opaque_sp)
1281     error.ref() = m_opaque_sp->RunREPL(language, repl_options);
1282   else
1283     error.SetErrorString("invalid debugger");
1284   return LLDB_RECORD_RESULT(error);
1285 }
1286 
1287 void SBDebugger::reset(const DebuggerSP &debugger_sp) {
1288   m_opaque_sp = debugger_sp;
1289 }
1290 
1291 Debugger *SBDebugger::get() const { return m_opaque_sp.get(); }
1292 
1293 Debugger &SBDebugger::ref() const {
1294   assert(m_opaque_sp.get());
1295   return *m_opaque_sp;
1296 }
1297 
1298 const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; }
1299 
1300 SBDebugger SBDebugger::FindDebuggerWithID(int id) {
1301   LLDB_RECORD_STATIC_METHOD(lldb::SBDebugger, SBDebugger, FindDebuggerWithID,
1302                             (int), id);
1303 
1304   // No need to lock, the debugger list is thread safe
1305   SBDebugger sb_debugger;
1306   DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id);
1307   if (debugger_sp)
1308     sb_debugger.reset(debugger_sp);
1309   return LLDB_RECORD_RESULT(sb_debugger);
1310 }
1311 
1312 const char *SBDebugger::GetInstanceName() {
1313   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBDebugger, GetInstanceName);
1314 
1315   return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr);
1316 }
1317 
1318 SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
1319                                         const char *debugger_instance_name) {
1320   LLDB_RECORD_STATIC_METHOD(lldb::SBError, SBDebugger, SetInternalVariable,
1321                             (const char *, const char *, const char *),
1322                             var_name, value, debugger_instance_name);
1323 
1324   SBError sb_error;
1325   DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
1326       ConstString(debugger_instance_name)));
1327   Status error;
1328   if (debugger_sp) {
1329     ExecutionContext exe_ctx(
1330         debugger_sp->GetCommandInterpreter().GetExecutionContext());
1331     error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
1332                                           var_name, value);
1333   } else {
1334     error.SetErrorStringWithFormat("invalid debugger instance name '%s'",
1335                                    debugger_instance_name);
1336   }
1337   if (error.Fail())
1338     sb_error.SetError(error);
1339   return LLDB_RECORD_RESULT(sb_error);
1340 }
1341 
1342 SBStringList
1343 SBDebugger::GetInternalVariableValue(const char *var_name,
1344                                      const char *debugger_instance_name) {
1345   LLDB_RECORD_STATIC_METHOD(
1346       lldb::SBStringList, SBDebugger, GetInternalVariableValue,
1347       (const char *, const char *), var_name, debugger_instance_name);
1348 
1349   DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
1350       ConstString(debugger_instance_name)));
1351   Status error;
1352   if (debugger_sp) {
1353     ExecutionContext exe_ctx(
1354         debugger_sp->GetCommandInterpreter().GetExecutionContext());
1355     lldb::OptionValueSP value_sp(
1356         debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error));
1357     if (value_sp) {
1358       StreamString value_strm;
1359       value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
1360       const std::string &value_str = std::string(value_strm.GetString());
1361       if (!value_str.empty()) {
1362         StringList string_list;
1363         string_list.SplitIntoLines(value_str);
1364         return LLDB_RECORD_RESULT(SBStringList(&string_list));
1365       }
1366     }
1367   }
1368   return LLDB_RECORD_RESULT(SBStringList());
1369 }
1370 
1371 uint32_t SBDebugger::GetTerminalWidth() const {
1372   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBDebugger, GetTerminalWidth);
1373 
1374   return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
1375 }
1376 
1377 void SBDebugger::SetTerminalWidth(uint32_t term_width) {
1378   LLDB_RECORD_DUMMY(void, SBDebugger, SetTerminalWidth, (uint32_t), term_width);
1379 
1380   if (m_opaque_sp)
1381     m_opaque_sp->SetTerminalWidth(term_width);
1382 }
1383 
1384 const char *SBDebugger::GetPrompt() const {
1385   LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBDebugger, GetPrompt);
1386 
1387   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1388 
1389   LLDB_LOGF(log, "SBDebugger(%p)::GetPrompt () => \"%s\"",
1390             static_cast<void *>(m_opaque_sp.get()),
1391             (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : ""));
1392 
1393   return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
1394                       : nullptr);
1395 }
1396 
1397 void SBDebugger::SetPrompt(const char *prompt) {
1398   LLDB_RECORD_METHOD(void, SBDebugger, SetPrompt, (const char *), prompt);
1399 
1400   if (m_opaque_sp)
1401     m_opaque_sp->SetPrompt(llvm::StringRef(prompt));
1402 }
1403 
1404 const char *SBDebugger::GetReproducerPath() const {
1405   LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBDebugger, GetReproducerPath);
1406 
1407   return (m_opaque_sp
1408               ? ConstString(m_opaque_sp->GetReproducerPath()).GetCString()
1409               : nullptr);
1410 }
1411 
1412 ScriptLanguage SBDebugger::GetScriptLanguage() const {
1413   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::ScriptLanguage, SBDebugger,
1414                                    GetScriptLanguage);
1415 
1416   return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
1417 }
1418 
1419 void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) {
1420   LLDB_RECORD_METHOD(void, SBDebugger, SetScriptLanguage,
1421                      (lldb::ScriptLanguage), script_lang);
1422 
1423   if (m_opaque_sp) {
1424     m_opaque_sp->SetScriptLanguage(script_lang);
1425   }
1426 }
1427 
1428 LanguageType SBDebugger::GetREPLLanguage() const {
1429   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::LanguageType, SBDebugger,
1430                                    GetREPLLanguage);
1431 
1432   return (m_opaque_sp ? m_opaque_sp->GetREPLLanguage() : eLanguageTypeUnknown);
1433 }
1434 
1435 void SBDebugger::SetREPLLanguage(LanguageType repl_lang) {
1436   LLDB_RECORD_METHOD(void, SBDebugger, SetREPLLanguage, (lldb::LanguageType),
1437                      repl_lang);
1438 
1439   if (m_opaque_sp) {
1440     m_opaque_sp->SetREPLLanguage(repl_lang);
1441   }
1442 }
1443 
1444 bool SBDebugger::SetUseExternalEditor(bool value) {
1445   LLDB_RECORD_METHOD(bool, SBDebugger, SetUseExternalEditor, (bool), value);
1446 
1447   return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
1448 }
1449 
1450 bool SBDebugger::GetUseExternalEditor() {
1451   LLDB_RECORD_METHOD_NO_ARGS(bool, SBDebugger, GetUseExternalEditor);
1452 
1453   return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
1454 }
1455 
1456 bool SBDebugger::SetUseColor(bool value) {
1457   LLDB_RECORD_METHOD(bool, SBDebugger, SetUseColor, (bool), value);
1458 
1459   return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
1460 }
1461 
1462 bool SBDebugger::GetUseColor() const {
1463   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, GetUseColor);
1464 
1465   return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
1466 }
1467 
1468 bool SBDebugger::SetUseSourceCache(bool value) {
1469   LLDB_RECORD_METHOD(bool, SBDebugger, SetUseSourceCache, (bool), value);
1470 
1471   return (m_opaque_sp ? m_opaque_sp->SetUseSourceCache(value) : false);
1472 }
1473 
1474 bool SBDebugger::GetUseSourceCache() const {
1475   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, GetUseSourceCache);
1476 
1477   return (m_opaque_sp ? m_opaque_sp->GetUseSourceCache() : false);
1478 }
1479 
1480 bool SBDebugger::GetDescription(SBStream &description) {
1481   LLDB_RECORD_METHOD(bool, SBDebugger, GetDescription, (lldb::SBStream &),
1482                      description);
1483 
1484   Stream &strm = description.ref();
1485 
1486   if (m_opaque_sp) {
1487     const char *name = m_opaque_sp->GetInstanceName().AsCString();
1488     user_id_t id = m_opaque_sp->GetID();
1489     strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1490   } else
1491     strm.PutCString("No value");
1492 
1493   return true;
1494 }
1495 
1496 user_id_t SBDebugger::GetID() {
1497   LLDB_RECORD_METHOD_NO_ARGS(lldb::user_id_t, SBDebugger, GetID);
1498 
1499   return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
1500 }
1501 
1502 SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
1503   LLDB_RECORD_METHOD(lldb::SBError, SBDebugger, SetCurrentPlatform,
1504                      (const char *), platform_name_cstr);
1505 
1506   SBError sb_error;
1507   if (m_opaque_sp) {
1508     if (platform_name_cstr && platform_name_cstr[0]) {
1509       ConstString platform_name(platform_name_cstr);
1510       PlatformSP platform_sp(Platform::Find(platform_name));
1511 
1512       if (platform_sp) {
1513         // Already have a platform with this name, just select it
1514         m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
1515       } else {
1516         // We don't have a platform by this name yet, create one
1517         platform_sp = Platform::Create(platform_name, sb_error.ref());
1518         if (platform_sp) {
1519           // We created the platform, now append and select it
1520           bool make_selected = true;
1521           m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected);
1522         }
1523       }
1524     } else {
1525       sb_error.ref().SetErrorString("invalid platform name");
1526     }
1527   } else {
1528     sb_error.ref().SetErrorString("invalid debugger");
1529   }
1530   return LLDB_RECORD_RESULT(sb_error);
1531 }
1532 
1533 bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) {
1534   LLDB_RECORD_METHOD(bool, SBDebugger, SetCurrentPlatformSDKRoot,
1535                      (const char *), sysroot);
1536 
1537   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1538   if (m_opaque_sp) {
1539     PlatformSP platform_sp(
1540         m_opaque_sp->GetPlatformList().GetSelectedPlatform());
1541 
1542     if (platform_sp) {
1543       if (log && sysroot)
1544         LLDB_LOGF(log, "SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")",
1545                   sysroot);
1546       platform_sp->SetSDKRootDirectory(ConstString(sysroot));
1547       return true;
1548     }
1549   }
1550   return false;
1551 }
1552 
1553 bool SBDebugger::GetCloseInputOnEOF() const {
1554   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDebugger, GetCloseInputOnEOF);
1555 
1556   return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false);
1557 }
1558 
1559 void SBDebugger::SetCloseInputOnEOF(bool b) {
1560   LLDB_RECORD_METHOD(void, SBDebugger, SetCloseInputOnEOF, (bool), b);
1561 
1562   if (m_opaque_sp)
1563     m_opaque_sp->SetCloseInputOnEOF(b);
1564 }
1565 
1566 SBTypeCategory SBDebugger::GetCategory(const char *category_name) {
1567   LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory,
1568                      (const char *), category_name);
1569 
1570   if (!category_name || *category_name == 0)
1571     return LLDB_RECORD_RESULT(SBTypeCategory());
1572 
1573   TypeCategoryImplSP category_sp;
1574 
1575   if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1576                                                  category_sp, false)) {
1577     return LLDB_RECORD_RESULT(SBTypeCategory(category_sp));
1578   } else {
1579     return LLDB_RECORD_RESULT(SBTypeCategory());
1580   }
1581 }
1582 
1583 SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) {
1584   LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory,
1585                      (lldb::LanguageType), lang_type);
1586 
1587   TypeCategoryImplSP category_sp;
1588   if (DataVisualization::Categories::GetCategory(lang_type, category_sp)) {
1589     return LLDB_RECORD_RESULT(SBTypeCategory(category_sp));
1590   } else {
1591     return LLDB_RECORD_RESULT(SBTypeCategory());
1592   }
1593 }
1594 
1595 SBTypeCategory SBDebugger::CreateCategory(const char *category_name) {
1596   LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, CreateCategory,
1597                      (const char *), category_name);
1598 
1599   if (!category_name || *category_name == 0)
1600     return LLDB_RECORD_RESULT(SBTypeCategory());
1601 
1602   TypeCategoryImplSP category_sp;
1603 
1604   if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1605                                                  category_sp, true)) {
1606     return LLDB_RECORD_RESULT(SBTypeCategory(category_sp));
1607   } else {
1608     return LLDB_RECORD_RESULT(SBTypeCategory());
1609   }
1610 }
1611 
1612 bool SBDebugger::DeleteCategory(const char *category_name) {
1613   LLDB_RECORD_METHOD(bool, SBDebugger, DeleteCategory, (const char *),
1614                      category_name);
1615 
1616   if (!category_name || *category_name == 0)
1617     return false;
1618 
1619   return DataVisualization::Categories::Delete(ConstString(category_name));
1620 }
1621 
1622 uint32_t SBDebugger::GetNumCategories() {
1623   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBDebugger, GetNumCategories);
1624 
1625   return DataVisualization::Categories::GetCount();
1626 }
1627 
1628 SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) {
1629   LLDB_RECORD_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategoryAtIndex,
1630                      (uint32_t), index);
1631 
1632   return LLDB_RECORD_RESULT(
1633       SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index)));
1634 }
1635 
1636 SBTypeCategory SBDebugger::GetDefaultCategory() {
1637   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTypeCategory, SBDebugger,
1638                              GetDefaultCategory);
1639 
1640   return LLDB_RECORD_RESULT(GetCategory("default"));
1641 }
1642 
1643 SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) {
1644   LLDB_RECORD_METHOD(lldb::SBTypeFormat, SBDebugger, GetFormatForType,
1645                      (lldb::SBTypeNameSpecifier), type_name);
1646 
1647   SBTypeCategory default_category_sb = GetDefaultCategory();
1648   if (default_category_sb.GetEnabled())
1649     return LLDB_RECORD_RESULT(default_category_sb.GetFormatForType(type_name));
1650   return LLDB_RECORD_RESULT(SBTypeFormat());
1651 }
1652 
1653 SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) {
1654   LLDB_RECORD_METHOD(lldb::SBTypeSummary, SBDebugger, GetSummaryForType,
1655                      (lldb::SBTypeNameSpecifier), type_name);
1656 
1657   if (!type_name.IsValid())
1658     return LLDB_RECORD_RESULT(SBTypeSummary());
1659   return LLDB_RECORD_RESULT(
1660       SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP())));
1661 }
1662 
1663 SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) {
1664   LLDB_RECORD_METHOD(lldb::SBTypeFilter, SBDebugger, GetFilterForType,
1665                      (lldb::SBTypeNameSpecifier), type_name);
1666 
1667   if (!type_name.IsValid())
1668     return LLDB_RECORD_RESULT(SBTypeFilter());
1669   return LLDB_RECORD_RESULT(
1670       SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP())));
1671 }
1672 
1673 SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) {
1674   LLDB_RECORD_METHOD(lldb::SBTypeSynthetic, SBDebugger, GetSyntheticForType,
1675                      (lldb::SBTypeNameSpecifier), type_name);
1676 
1677   if (!type_name.IsValid())
1678     return LLDB_RECORD_RESULT(SBTypeSynthetic());
1679   return LLDB_RECORD_RESULT(SBTypeSynthetic(
1680       DataVisualization::GetSyntheticForType(type_name.GetSP())));
1681 }
1682 
1683 static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) {
1684   if (categories == nullptr)
1685     return {};
1686   size_t len = 0;
1687   while (categories[len] != nullptr)
1688     ++len;
1689   return llvm::makeArrayRef(categories, len);
1690 }
1691 
1692 bool SBDebugger::EnableLog(const char *channel, const char **categories) {
1693   LLDB_RECORD_METHOD(bool, SBDebugger, EnableLog, (const char *, const char **),
1694                      channel, categories);
1695 
1696   if (m_opaque_sp) {
1697     uint32_t log_options =
1698         LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1699     std::string error;
1700     llvm::raw_string_ostream error_stream(error);
1701     return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
1702                                   log_options, error_stream);
1703   } else
1704     return false;
1705 }
1706 
1707 void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
1708                                     void *baton) {
1709   LLDB_RECORD_DUMMY(void, SBDebugger, SetLoggingCallback,
1710                     (lldb::LogOutputCallback, void *), log_callback, baton);
1711 
1712   if (m_opaque_sp) {
1713     return m_opaque_sp->SetLoggingCallback(log_callback, baton);
1714   }
1715 }
1716 
1717 namespace lldb_private {
1718 namespace repro {
1719 
1720 template <> void RegisterMethods<SBInputReader>(Registry &R) {
1721   LLDB_REGISTER_METHOD(void, SBInputReader, SetIsDone, (bool));
1722   LLDB_REGISTER_METHOD_CONST(bool, SBInputReader, IsActive, ());
1723 }
1724 
1725 static void SetFileHandleRedirect(SBDebugger *, FILE *, bool) {
1726   // Do nothing.
1727 }
1728 
1729 static SBError SetFileRedirect(SBDebugger *, SBFile file) { return SBError(); }
1730 
1731 static SBError SetFileRedirect(SBDebugger *, FileSP file) { return SBError(); }
1732 
1733 template <> void RegisterMethods<SBDebugger>(Registry &R) {
1734   // Custom implementation.
1735   R.Register(&invoke<void (SBDebugger::*)(FILE *, bool)>::method<
1736                  &SBDebugger::SetErrorFileHandle>::record,
1737              &SetFileHandleRedirect);
1738   R.Register(&invoke<void (SBDebugger::*)(FILE *, bool)>::method<
1739                  &SBDebugger::SetOutputFileHandle>::record,
1740              &SetFileHandleRedirect);
1741 
1742   R.Register(&invoke<SBError (SBDebugger::*)(
1743                  SBFile)>::method<&SBDebugger::SetInputFile>::record,
1744              &SetFileRedirect);
1745   R.Register(&invoke<SBError (SBDebugger::*)(
1746                  SBFile)>::method<&SBDebugger::SetOutputFile>::record,
1747              &SetFileRedirect);
1748   R.Register(&invoke<SBError (SBDebugger::*)(
1749                  SBFile)>::method<&SBDebugger::SetErrorFile>::record,
1750              &SetFileRedirect);
1751 
1752   R.Register(&invoke<SBError (SBDebugger::*)(
1753                  FileSP)>::method<&SBDebugger::SetInputFile>::record,
1754              &SetFileRedirect);
1755   R.Register(&invoke<SBError (SBDebugger::*)(
1756                  FileSP)>::method<&SBDebugger::SetOutputFile>::record,
1757              &SetFileRedirect);
1758   R.Register(&invoke<SBError (SBDebugger::*)(
1759                  FileSP)>::method<&SBDebugger::SetErrorFile>::record,
1760              &SetFileRedirect);
1761 
1762   LLDB_REGISTER_CHAR_PTR_METHOD_STATIC(bool, SBDebugger,
1763                                        GetDefaultArchitecture);
1764 
1765   LLDB_REGISTER_CONSTRUCTOR(SBDebugger, ());
1766   LLDB_REGISTER_CONSTRUCTOR(SBDebugger, (const lldb::DebuggerSP &));
1767   LLDB_REGISTER_CONSTRUCTOR(SBDebugger, (const lldb::SBDebugger &));
1768   LLDB_REGISTER_METHOD(lldb::SBDebugger &,
1769                        SBDebugger, operator=,(const lldb::SBDebugger &));
1770   LLDB_REGISTER_STATIC_METHOD(void, SBDebugger, Initialize, ());
1771   LLDB_REGISTER_STATIC_METHOD(lldb::SBError, SBDebugger,
1772                               InitializeWithErrorHandling, ());
1773   LLDB_REGISTER_STATIC_METHOD(void, SBDebugger, Terminate, ());
1774   LLDB_REGISTER_METHOD(void, SBDebugger, Clear, ());
1775   LLDB_REGISTER_STATIC_METHOD(lldb::SBDebugger, SBDebugger, Create, ());
1776   LLDB_REGISTER_STATIC_METHOD(lldb::SBDebugger, SBDebugger, Create, (bool));
1777   LLDB_REGISTER_STATIC_METHOD(
1778       const char *, SBDebugger, GetProgressFromEvent,
1779       (const lldb::SBEvent &, uint64_t &, uint64_t &, uint64_t &, bool &));
1780   LLDB_REGISTER_STATIC_METHOD(const char *, SBDebugger, GetBroadcasterClass,
1781                               ());
1782   LLDB_REGISTER_METHOD(SBBroadcaster, SBDebugger, GetBroadcaster, ());
1783   LLDB_REGISTER_STATIC_METHOD(void, SBDebugger, Destroy, (lldb::SBDebugger &));
1784   LLDB_REGISTER_STATIC_METHOD(void, SBDebugger, MemoryPressureDetected, ());
1785   LLDB_REGISTER_METHOD_CONST(bool, SBDebugger, IsValid, ());
1786   LLDB_REGISTER_METHOD_CONST(bool, SBDebugger, operator bool,());
1787   LLDB_REGISTER_METHOD(void, SBDebugger, SetAsync, (bool));
1788   LLDB_REGISTER_METHOD(bool, SBDebugger, GetAsync, ());
1789   LLDB_REGISTER_METHOD(void, SBDebugger, SkipLLDBInitFiles, (bool));
1790   LLDB_REGISTER_METHOD(void, SBDebugger, SkipAppInitFiles, (bool));
1791   LLDB_REGISTER_METHOD(SBError, SBDebugger, SetInputString, (const char *));
1792   LLDB_REGISTER_METHOD(void, SBDebugger, SetInputFileHandle, (FILE *, bool));
1793   LLDB_REGISTER_METHOD(FILE *, SBDebugger, GetInputFileHandle, ());
1794   LLDB_REGISTER_METHOD(FILE *, SBDebugger, GetOutputFileHandle, ());
1795   LLDB_REGISTER_METHOD(FILE *, SBDebugger, GetErrorFileHandle, ());
1796   LLDB_REGISTER_METHOD(SBFile, SBDebugger, GetInputFile, ());
1797   LLDB_REGISTER_METHOD(SBFile, SBDebugger, GetOutputFile, ());
1798   LLDB_REGISTER_METHOD(SBFile, SBDebugger, GetErrorFile, ());
1799   LLDB_REGISTER_METHOD(void, SBDebugger, SaveInputTerminalState, ());
1800   LLDB_REGISTER_METHOD(void, SBDebugger, RestoreInputTerminalState, ());
1801   LLDB_REGISTER_METHOD(lldb::SBCommandInterpreter, SBDebugger,
1802                        GetCommandInterpreter, ());
1803   LLDB_REGISTER_METHOD(void, SBDebugger, HandleCommand, (const char *));
1804   LLDB_REGISTER_METHOD(lldb::SBListener, SBDebugger, GetListener, ());
1805   LLDB_REGISTER_METHOD(
1806       void, SBDebugger, HandleProcessEvent,
1807       (const lldb::SBProcess &, const lldb::SBEvent &, FILE *, FILE *));
1808   LLDB_REGISTER_METHOD(
1809       void, SBDebugger, HandleProcessEvent,
1810       (const lldb::SBProcess &, const lldb::SBEvent &, SBFile, SBFile));
1811   LLDB_REGISTER_METHOD(
1812       void, SBDebugger, HandleProcessEvent,
1813       (const lldb::SBProcess &, const lldb::SBEvent &, FileSP, FileSP));
1814   LLDB_REGISTER_METHOD(lldb::SBSourceManager, SBDebugger, GetSourceManager, ());
1815   LLDB_REGISTER_STATIC_METHOD(bool, SBDebugger, SetDefaultArchitecture,
1816                               (const char *));
1817   LLDB_REGISTER_METHOD(lldb::ScriptLanguage, SBDebugger, GetScriptingLanguage,
1818                        (const char *));
1819   LLDB_REGISTER_METHOD(SBStructuredData, SBDebugger, GetScriptInterpreterInfo,
1820                        (lldb::ScriptLanguage));
1821   LLDB_REGISTER_STATIC_METHOD(const char *, SBDebugger, GetVersionString, ());
1822   LLDB_REGISTER_STATIC_METHOD(const char *, SBDebugger, StateAsCString,
1823                               (lldb::StateType));
1824   LLDB_REGISTER_STATIC_METHOD(lldb::SBStructuredData, SBDebugger,
1825                               GetBuildConfiguration, ());
1826   LLDB_REGISTER_STATIC_METHOD(bool, SBDebugger, StateIsRunningState,
1827                               (lldb::StateType));
1828   LLDB_REGISTER_STATIC_METHOD(bool, SBDebugger, StateIsStoppedState,
1829                               (lldb::StateType));
1830   LLDB_REGISTER_METHOD(
1831       lldb::SBTarget, SBDebugger, CreateTarget,
1832       (const char *, const char *, const char *, bool, lldb::SBError &));
1833   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger,
1834                        CreateTargetWithFileAndTargetTriple,
1835                        (const char *, const char *));
1836   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, CreateTargetWithFileAndArch,
1837                        (const char *, const char *));
1838   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, CreateTarget,
1839                        (const char *));
1840   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, GetDummyTarget, ());
1841   LLDB_REGISTER_METHOD(bool, SBDebugger, DeleteTarget, (lldb::SBTarget &));
1842   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, GetTargetAtIndex,
1843                        (uint32_t));
1844   LLDB_REGISTER_METHOD(uint32_t, SBDebugger, GetIndexOfTarget,
1845                        (lldb::SBTarget));
1846   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithProcessID,
1847                        (lldb::pid_t));
1848   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, FindTargetWithFileAndArch,
1849                        (const char *, const char *));
1850   LLDB_REGISTER_METHOD(uint32_t, SBDebugger, GetNumTargets, ());
1851   LLDB_REGISTER_METHOD(lldb::SBTarget, SBDebugger, GetSelectedTarget, ());
1852   LLDB_REGISTER_METHOD(void, SBDebugger, SetSelectedTarget, (lldb::SBTarget &));
1853   LLDB_REGISTER_METHOD(lldb::SBPlatform, SBDebugger, GetSelectedPlatform, ());
1854   LLDB_REGISTER_METHOD(void, SBDebugger, SetSelectedPlatform,
1855                        (lldb::SBPlatform &));
1856   LLDB_REGISTER_METHOD(uint32_t, SBDebugger, GetNumPlatforms, ());
1857   LLDB_REGISTER_METHOD(lldb::SBPlatform, SBDebugger, GetPlatformAtIndex,
1858                        (uint32_t));
1859   LLDB_REGISTER_METHOD(uint32_t, SBDebugger, GetNumAvailablePlatforms, ());
1860   LLDB_REGISTER_METHOD(lldb::SBStructuredData, SBDebugger,
1861                        GetAvailablePlatformInfoAtIndex, (uint32_t));
1862   LLDB_REGISTER_METHOD(void, SBDebugger, DispatchInputInterrupt, ());
1863   LLDB_REGISTER_METHOD(void, SBDebugger, DispatchInputEndOfFile, ());
1864   LLDB_REGISTER_METHOD(void, SBDebugger, PushInputReader,
1865                        (lldb::SBInputReader &));
1866   LLDB_REGISTER_METHOD(void, SBDebugger, RunCommandInterpreter, (bool, bool));
1867   LLDB_REGISTER_METHOD(void, SBDebugger, RunCommandInterpreter,
1868                        (bool, bool, lldb::SBCommandInterpreterRunOptions &,
1869                         int &, bool &, bool &));
1870   LLDB_REGISTER_METHOD(lldb::SBError, SBDebugger, RunREPL,
1871                        (lldb::LanguageType, const char *));
1872   LLDB_REGISTER_STATIC_METHOD(lldb::SBDebugger, SBDebugger, FindDebuggerWithID,
1873                               (int));
1874   LLDB_REGISTER_METHOD(const char *, SBDebugger, GetInstanceName, ());
1875   LLDB_REGISTER_STATIC_METHOD(lldb::SBError, SBDebugger, SetInternalVariable,
1876                               (const char *, const char *, const char *));
1877   LLDB_REGISTER_STATIC_METHOD(lldb::SBStringList, SBDebugger,
1878                               GetInternalVariableValue,
1879                               (const char *, const char *));
1880   LLDB_REGISTER_METHOD_CONST(uint32_t, SBDebugger, GetTerminalWidth, ());
1881   LLDB_REGISTER_METHOD(void, SBDebugger, SetTerminalWidth, (uint32_t));
1882   LLDB_REGISTER_METHOD_CONST(const char *, SBDebugger, GetPrompt, ());
1883   LLDB_REGISTER_METHOD(void, SBDebugger, SetPrompt, (const char *));
1884   LLDB_REGISTER_METHOD_CONST(const char *, SBDebugger, GetReproducerPath, ());
1885   LLDB_REGISTER_METHOD_CONST(lldb::ScriptLanguage, SBDebugger,
1886                              GetScriptLanguage, ());
1887   LLDB_REGISTER_METHOD(void, SBDebugger, SetScriptLanguage,
1888                        (lldb::ScriptLanguage));
1889   LLDB_REGISTER_METHOD_CONST(lldb::LanguageType, SBDebugger, GetREPLLanguage,
1890                              ());
1891   LLDB_REGISTER_METHOD(void, SBDebugger, SetREPLLanguage, (lldb::LanguageType));
1892   LLDB_REGISTER_METHOD(bool, SBDebugger, SetUseExternalEditor, (bool));
1893   LLDB_REGISTER_METHOD(bool, SBDebugger, GetUseExternalEditor, ());
1894   LLDB_REGISTER_METHOD(bool, SBDebugger, SetUseColor, (bool));
1895   LLDB_REGISTER_METHOD_CONST(bool, SBDebugger, GetUseColor, ());
1896   LLDB_REGISTER_METHOD(bool, SBDebugger, GetDescription, (lldb::SBStream &));
1897   LLDB_REGISTER_METHOD(lldb::user_id_t, SBDebugger, GetID, ());
1898   LLDB_REGISTER_METHOD(lldb::SBError, SBDebugger, SetCurrentPlatform,
1899                        (const char *));
1900   LLDB_REGISTER_METHOD(bool, SBDebugger, SetCurrentPlatformSDKRoot,
1901                        (const char *));
1902   LLDB_REGISTER_METHOD_CONST(bool, SBDebugger, GetCloseInputOnEOF, ());
1903   LLDB_REGISTER_METHOD(void, SBDebugger, SetCloseInputOnEOF, (bool));
1904   LLDB_REGISTER_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory,
1905                        (const char *));
1906   LLDB_REGISTER_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategory,
1907                        (lldb::LanguageType));
1908   LLDB_REGISTER_METHOD(lldb::SBTypeCategory, SBDebugger, CreateCategory,
1909                        (const char *));
1910   LLDB_REGISTER_METHOD(bool, SBDebugger, DeleteCategory, (const char *));
1911   LLDB_REGISTER_METHOD(uint32_t, SBDebugger, GetNumCategories, ());
1912   LLDB_REGISTER_METHOD(lldb::SBTypeCategory, SBDebugger, GetCategoryAtIndex,
1913                        (uint32_t));
1914   LLDB_REGISTER_METHOD(lldb::SBTypeCategory, SBDebugger, GetDefaultCategory,
1915                        ());
1916   LLDB_REGISTER_METHOD(lldb::SBTypeFormat, SBDebugger, GetFormatForType,
1917                        (lldb::SBTypeNameSpecifier));
1918   LLDB_REGISTER_METHOD(lldb::SBTypeSummary, SBDebugger, GetSummaryForType,
1919                        (lldb::SBTypeNameSpecifier));
1920   LLDB_REGISTER_METHOD(lldb::SBTypeSynthetic, SBDebugger, GetSyntheticForType,
1921                        (lldb::SBTypeNameSpecifier));
1922   LLDB_REGISTER_METHOD(lldb::SBTypeFilter, SBDebugger, GetFilterForType,
1923                        (lldb::SBTypeNameSpecifier));
1924   LLDB_REGISTER_METHOD(bool, SBDebugger, EnableLog,
1925                        (const char *, const char **));
1926   LLDB_REGISTER_METHOD(lldb::SBCommandInterpreterRunResult, SBDebugger,
1927                        RunCommandInterpreter,
1928                        (const lldb::SBCommandInterpreterRunOptions &));
1929 }
1930 
1931 } // namespace repro
1932 } // namespace lldb_private
1933