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