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