blob: c9909bb210de21694f2bc89fc3ef95e03a7413b7 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBDebugger.cpp ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eli Friedmanca93cc12010-06-09 07:37:52 +000014#include "lldb/API/SBDebugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Greg Claytone0d378b2011-03-24 21:19:54 +000016#include "lldb/lldb-private.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Eli Friedmanca93cc12010-06-09 07:37:52 +000018#include "lldb/API/SBBroadcaster.h"
19#include "lldb/API/SBCommandInterpreter.h"
20#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton2289fa42011-04-30 01:09:13 +000021#include "lldb/API/SBError.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000022#include "lldb/API/SBEvent.h"
23#include "lldb/API/SBFrame.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "lldb/API/SBListener.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000025#include "lldb/API/SBProcess.h"
26#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000027#include "lldb/API/SBStream.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000028#include "lldb/API/SBStringList.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000029#include "lldb/API/SBStructuredData.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000030#include "lldb/API/SBTarget.h"
31#include "lldb/API/SBThread.h"
Enrico Granata061858c2012-02-15 02:34:21 +000032#include "lldb/API/SBTypeCategory.h"
Enrico Granata061858c2012-02-15 02:34:21 +000033#include "lldb/API/SBTypeFilter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000034#include "lldb/API/SBTypeFormat.h"
Enrico Granata061858c2012-02-15 02:34:21 +000035#include "lldb/API/SBTypeNameSpecifier.h"
36#include "lldb/API/SBTypeSummary.h"
37#include "lldb/API/SBTypeSynthetic.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000038#include "lldb/API/SystemInitializerFull.h"
Enrico Granata061858c2012-02-15 02:34:21 +000039
Greg Clayton6eee5aa2010-10-11 01:05:37 +000040#include "lldb/Core/Debugger.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000041#include "lldb/Core/PluginManager.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000042#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000043#include "lldb/Core/StreamFile.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000044#include "lldb/Core/StructuredDataImpl.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000045#include "lldb/DataFormatters/DataVisualization.h"
Pavel Labathf1389e92018-02-19 15:06:28 +000046#include "lldb/Host/XML.h"
Zachary Turnere6e2bb32015-03-31 21:03:22 +000047#include "lldb/Initialization/SystemLifetimeManager.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000048#include "lldb/Interpreter/Args.h"
49#include "lldb/Interpreter/CommandInterpreter.h"
Pavel Labath47cbf4a2018-04-10 09:03:59 +000050#include "lldb/Interpreter/OptionArgParser.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000051#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000052#include "lldb/Target/Process.h"
53#include "lldb/Target/TargetList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
Enrico Granatad717cc92015-10-20 04:50:09 +000055#include "llvm/ADT/STLExtras.h"
Zachary Turner7b2e5a32016-09-16 19:09:12 +000056#include "llvm/ADT/StringRef.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000057#include "llvm/Support/DynamicLibrary.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000058#include "llvm/Support/ManagedStatic.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000059
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060using namespace lldb;
61using namespace lldb_private;
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp,
64 const FileSpec &spec,
Zachary Turner97206d52017-05-12 04:51:55 +000065 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 llvm::sys::DynamicLibrary dynlib =
67 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
68 if (dynlib.isValid()) {
69 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger);
70
71 lldb::SBDebugger debugger_sb(debugger_sp);
72 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
73 // function.
74 // TODO: mangle this differently for your system - on OSX, the first
75 // underscore needs to be removed and the second one stays
76 LLDBCommandPluginInit init_func =
Stephane Sezer22701b22017-10-24 23:46:00 +000077 (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
79 if (init_func) {
80 if (init_func(debugger_sb))
81 return dynlib;
82 else
83 error.SetErrorString("plug-in refused to load "
84 "(lldb::PluginInitialize(lldb::SBDebugger) "
85 "returned false)");
86 } else {
87 error.SetErrorString("plug-in is missing the required initialization: "
88 "lldb::PluginInitialize(lldb::SBDebugger)");
Greg Clayton5fb8f792013-12-02 19:35:49 +000089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 } else {
91 if (spec.Exists())
92 error.SetErrorString("this file does not represent a loadable dylib");
Greg Clayton5fb8f792013-12-02 19:35:49 +000093 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 error.SetErrorString("no such file");
95 }
96 return llvm::sys::DynamicLibrary();
Greg Clayton5fb8f792013-12-02 19:35:49 +000097}
98
Zachary Turnere6e2bb32015-03-31 21:03:22 +000099static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101SBError SBInputReader::Initialize(
102 lldb::SBDebugger &sb_debugger,
103 unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction,
104 char const *, unsigned long),
105 void *, lldb::InputReaderGranularity, char const *, char const *, bool) {
106 return SBError();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000107}
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109void SBInputReader::SetIsDone(bool) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111bool SBInputReader::IsActive() const { return false; }
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000112
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000113SBDebugger::SBDebugger() = default;
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp)
116 : m_opaque_sp(debugger_sp) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000119
120SBDebugger::~SBDebugger() = default;
121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) {
123 if (this != &rhs) {
124 m_opaque_sp = rhs.m_opaque_sp;
125 }
126 return *this;
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000127}
128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129void SBDebugger::Initialize() {
130 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 if (log)
133 log->Printf("SBDebugger::Initialize ()");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 g_debugger_lifetime->Initialize(llvm::make_unique<SystemInitializerFull>(),
136 LoadPlugin);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137}
138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139void SBDebugger::Terminate() { g_debugger_lifetime->Terminate(); }
140
141void SBDebugger::Clear() {
142 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
143
144 if (log)
145 log->Printf("SBDebugger(%p)::Clear ()",
146 static_cast<void *>(m_opaque_sp.get()));
147
148 if (m_opaque_sp)
149 m_opaque_sp->ClearIOHandlers();
150
151 m_opaque_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152}
153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154SBDebugger SBDebugger::Create() {
155 return SBDebugger::Create(false, nullptr, nullptr);
Greg Clayton48e42542010-07-30 20:12:55 +0000156}
157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158SBDebugger SBDebugger::Create(bool source_init_files) {
159 return SBDebugger::Create(source_init_files, nullptr, nullptr);
Jim Ingham06942692011-08-13 00:22:20 +0000160}
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162SBDebugger SBDebugger::Create(bool source_init_files,
163 lldb::LogOutputCallback callback, void *baton)
Jim Ingham228063c2012-02-21 02:23:08 +0000164
165{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 SBDebugger debugger;
Greg Claytoncb172b12014-05-19 20:42:14 +0000169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 // Currently we have issues if this function is called simultaneously on two
171 // different
172 // threads. The issues mainly revolve around the fact that the
173 // lldb_private::FormatManager
174 // uses global collections and having two threads parsing the .lldbinit files
175 // can cause
176 // mayhem. So to get around this for now we need to use a mutex to prevent bad
177 // things
178 // from happening.
179 static std::recursive_mutex g_mutex;
180 std::lock_guard<std::recursive_mutex> guard(g_mutex);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 debugger.reset(Debugger::CreateInstance(callback, baton));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 if (log) {
185 SBStream sstr;
186 debugger.GetDescription(sstr);
187 log->Printf("SBDebugger::Create () => SBDebugger(%p): %s",
188 static_cast<void *>(debugger.m_opaque_sp.get()),
189 sstr.GetData());
190 }
191
192 SBCommandInterpreter interp = debugger.GetCommandInterpreter();
193 if (source_init_files) {
194 interp.get()->SkipLLDBInitFiles(false);
195 interp.get()->SkipAppInitFiles(false);
196 SBCommandReturnObject result;
197 interp.SourceInitFileInHomeDirectory(result);
198 } else {
199 interp.get()->SkipLLDBInitFiles(true);
200 interp.get()->SkipAppInitFiles(true);
201 }
202 return debugger;
Greg Clayton66111032010-06-23 01:19:29 +0000203}
204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205void SBDebugger::Destroy(SBDebugger &debugger) {
206 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 if (log) {
209 SBStream sstr;
210 debugger.GetDescription(sstr);
211 log->Printf("SBDebugger::Destroy () => SBDebugger(%p): %s",
212 static_cast<void *>(debugger.m_opaque_sp.get()),
213 sstr.GetData());
214 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 Debugger::Destroy(debugger.m_opaque_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 if (debugger.m_opaque_sp.get() != nullptr)
219 debugger.m_opaque_sp.reset();
Caroline Ticee02657b2011-01-22 01:02:07 +0000220}
221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222void SBDebugger::MemoryPressureDetected() {
223 // Since this function can be call asynchronously, we allow it to be
224 // non-mandatory. We have seen deadlocks with this function when called
225 // so we need to safeguard against this until we can determine what is
226 // causing the deadlocks.
227 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
228
229 const bool mandatory = false;
230 if (log) {
231 log->Printf("SBDebugger::MemoryPressureDetected (), mandatory = %d",
232 mandatory);
233 }
234
235 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonf9322412011-12-15 04:38:41 +0000236}
237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238bool SBDebugger::IsValid() const { return m_opaque_sp.get() != nullptr; }
239
240void SBDebugger::SetAsync(bool b) {
241 if (m_opaque_sp)
242 m_opaque_sp->SetAsyncExecution(b);
Greg Clayton66111032010-06-23 01:19:29 +0000243}
244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245bool SBDebugger::GetAsync() {
246 return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247}
248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249void SBDebugger::SkipLLDBInitFiles(bool b) {
250 if (m_opaque_sp)
251 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b);
Jim Inghame64f0dc2011-09-13 23:25:31 +0000252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254void SBDebugger::SkipAppInitFiles(bool b) {
255 if (m_opaque_sp)
256 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b);
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000257}
258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259// Shouldn't really be settable after initialization as this could cause lots of
260// problems; don't want users
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261// trying to switch modes in the middle of a debugging session.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) {
263 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 if (log)
266 log->Printf(
267 "SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)",
268 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
269 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 if (m_opaque_sp)
272 m_opaque_sp->SetInputFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273}
274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) {
276 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 if (log)
279 log->Printf(
280 "SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)",
281 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
282 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000283
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 if (m_opaque_sp)
285 m_opaque_sp->SetOutputFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286}
287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) {
289 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 if (log)
292 log->Printf(
293 "SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)",
294 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
295 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000296
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 if (m_opaque_sp)
298 m_opaque_sp->SetErrorFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299}
300
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301FILE *SBDebugger::GetInputFileHandle() {
302 if (m_opaque_sp) {
303 StreamFileSP stream_file_sp(m_opaque_sp->GetInputFile());
304 if (stream_file_sp)
305 return stream_file_sp->GetFile().GetStream();
306 }
307 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308}
309
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310FILE *SBDebugger::GetOutputFileHandle() {
311 if (m_opaque_sp) {
312 StreamFileSP stream_file_sp(m_opaque_sp->GetOutputFile());
313 if (stream_file_sp)
314 return stream_file_sp->GetFile().GetStream();
315 }
316 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317}
318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319FILE *SBDebugger::GetErrorFileHandle() {
320 if (m_opaque_sp) {
321 StreamFileSP stream_file_sp(m_opaque_sp->GetErrorFile());
322 if (stream_file_sp)
323 return stream_file_sp->GetFile().GetStream();
324 }
325 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326}
327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328void SBDebugger::SaveInputTerminalState() {
329 if (m_opaque_sp)
330 m_opaque_sp->SaveInputTerminalState();
Jim Inghamc5917d92012-11-30 20:23:19 +0000331}
332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333void SBDebugger::RestoreInputTerminalState() {
334 if (m_opaque_sp)
335 m_opaque_sp->RestoreInputTerminalState();
Jim Inghamc5917d92012-11-30 20:23:19 +0000336}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337SBCommandInterpreter SBDebugger::GetCommandInterpreter() {
338 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000339
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340 SBCommandInterpreter sb_interpreter;
341 if (m_opaque_sp)
342 sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 if (log)
345 log->Printf(
346 "SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)",
347 static_cast<void *>(m_opaque_sp.get()),
348 static_cast<void *>(sb_interpreter.get()));
Caroline Tice750cd172010-10-26 23:49:36 +0000349
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 return sb_interpreter;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351}
352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353void SBDebugger::HandleCommand(const char *command) {
354 if (m_opaque_sp) {
355 TargetSP target_sp(m_opaque_sp->GetSelectedTarget());
356 std::unique_lock<std::recursive_mutex> lock;
357 if (target_sp)
358 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +0000359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 SBCommandInterpreter sb_interpreter(GetCommandInterpreter());
361 SBCommandReturnObject result;
Greg Clayton66111032010-06-23 01:19:29 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 sb_interpreter.HandleCommand(command, result, false);
Greg Clayton66111032010-06-23 01:19:29 +0000364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 if (GetErrorFileHandle() != nullptr)
366 result.PutError(GetErrorFileHandle());
367 if (GetOutputFileHandle() != nullptr)
368 result.PutOutput(GetOutputFileHandle());
Greg Clayton66111032010-06-23 01:19:29 +0000369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 if (!m_opaque_sp->GetAsyncExecution()) {
371 SBProcess process(GetCommandInterpreter().GetProcess());
372 ProcessSP process_sp(process.GetSP());
373 if (process_sp) {
374 EventSP event_sp;
375 ListenerSP lldb_listener_sp = m_opaque_sp->GetListener();
Pavel Labathd35031e12016-11-30 10:41:42 +0000376 while (lldb_listener_sp->GetEventForBroadcaster(
377 process_sp.get(), event_sp, std::chrono::seconds(0))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 SBEvent event(event_sp);
379 HandleProcessEvent(process, event, GetOutputFileHandle(),
380 GetErrorFileHandle());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385}
386
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387SBListener SBDebugger::GetListener() {
388 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000389
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 SBListener sb_listener;
391 if (m_opaque_sp)
392 sb_listener.reset(m_opaque_sp->GetListener());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 if (log)
395 log->Printf("SBDebugger(%p)::GetListener () => SBListener(%p)",
396 static_cast<void *>(m_opaque_sp.get()),
397 static_cast<void *>(sb_listener.get()));
Caroline Tice750cd172010-10-26 23:49:36 +0000398
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 return sb_listener;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400}
401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402void SBDebugger::HandleProcessEvent(const SBProcess &process,
403 const SBEvent &event, FILE *out,
404 FILE *err) {
405 if (!process.IsValid())
406 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 TargetSP target_sp(process.GetTarget().GetSP());
409 if (!target_sp)
410 return;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 const uint32_t event_type = event.GetType();
413 char stdio_buffer[1024];
414 size_t len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 if (event_type &
419 (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) {
420 // Drain stdout when we stop just in case we have any bytes
421 while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0)
422 if (out != nullptr)
423 ::fwrite(stdio_buffer, 1, len, out);
424 }
Todd Fiala75930012016-08-19 04:21:48 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 if (event_type &
427 (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) {
428 // Drain stderr when we stop just in case we have any bytes
429 while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0)
430 if (err != nullptr)
431 ::fwrite(stdio_buffer, 1, len, err);
432 }
433
434 if (event_type & Process::eBroadcastBitStateChanged) {
435 StateType event_state = SBProcess::GetStateFromEvent(event);
436
437 if (event_state == eStateInvalid)
438 return;
439
440 bool is_stopped = StateIsStoppedState(event_state);
441 if (!is_stopped)
442 process.ReportEventState(event, out);
443 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444}
445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446SBSourceManager SBDebugger::GetSourceManager() {
447 SBSourceManager sb_source_manager(*this);
448 return sb_source_manager;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449}
450
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) {
452 if (arch_name && arch_name_len) {
453 ArchSpec default_arch = Target::GetDefaultArchitecture();
Caroline Ticedaccaa92010-09-20 20:44:43 +0000454
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 if (default_arch.IsValid()) {
456 const std::string &triple_str = default_arch.GetTriple().str();
457 if (!triple_str.empty())
458 ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str());
459 else
460 ::snprintf(arch_name, arch_name_len, "%s",
461 default_arch.GetArchitectureName());
462 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 }
465 if (arch_name && arch_name_len)
466 arch_name[0] = '\0';
467 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468}
469
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470bool SBDebugger::SetDefaultArchitecture(const char *arch_name) {
471 if (arch_name) {
472 ArchSpec arch(arch_name);
473 if (arch.IsValid()) {
474 Target::SetDefaultArchitecture(arch);
475 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 }
478 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479}
480
481ScriptLanguage
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482SBDebugger::GetScriptingLanguage(const char *script_language_name) {
Zachary Turner7b2e5a32016-09-16 19:09:12 +0000483 if (!script_language_name) return eScriptLanguageDefault;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000484 return OptionArgParser::ToScriptLanguage(
485 llvm::StringRef(script_language_name), eScriptLanguageDefault, nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488const char *SBDebugger::GetVersionString() {
489 return lldb_private::GetVersion();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490}
491
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492const char *SBDebugger::StateAsCString(StateType state) {
493 return lldb_private::StateAsCString(state);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494}
495
Pavel Labathf1389e92018-02-19 15:06:28 +0000496static void AddBoolConfigEntry(StructuredData::Dictionary &dict,
497 llvm::StringRef name, bool value,
498 llvm::StringRef description) {
499 auto entry_up = llvm::make_unique<StructuredData::Dictionary>();
500 entry_up->AddBooleanItem("value", value);
501 entry_up->AddStringItem("description", description);
502 dict.AddItem(name, std::move(entry_up));
503}
504
505SBStructuredData SBDebugger::GetBuildConfiguration() {
506 auto config_up = llvm::make_unique<StructuredData::Dictionary>();
507 AddBoolConfigEntry(
508 *config_up, "xml", XMLDocument::XMLEnabled(),
509 "A boolean value that indicates if XML support is enabled in LLDB");
510
511 SBStructuredData data;
512 data.m_impl_up->SetObjectSP(std::move(config_up));
513 return data;
514}
515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516bool SBDebugger::StateIsRunningState(StateType state) {
517 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 const bool result = lldb_private::StateIsRunningState(state);
520 if (log)
521 log->Printf("SBDebugger::StateIsRunningState (state=%s) => %i",
522 StateAsCString(state), result);
Greg Clayton48381312010-10-30 04:51:46 +0000523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525}
526
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527bool SBDebugger::StateIsStoppedState(StateType state) {
528 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000529
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 const bool result = lldb_private::StateIsStoppedState(state, false);
531 if (log)
532 log->Printf("SBDebugger::StateIsStoppedState (state=%s) => %i",
533 StateAsCString(state), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000534
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536}
537
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538lldb::SBTarget SBDebugger::CreateTarget(const char *filename,
539 const char *target_triple,
540 const char *platform_name,
541 bool add_dependent_modules,
542 lldb::SBError &sb_error) {
543 SBTarget sb_target;
544 TargetSP target_sp;
545 if (m_opaque_sp) {
546 sb_error.Clear();
547 OptionGroupPlatform platform_options(false);
548 platform_options.SetPlatformName(platform_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000549
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget(
551 *m_opaque_sp, filename, target_triple, add_dependent_modules,
552 &platform_options, target_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000553
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 if (sb_error.Success())
555 sb_target.SetSP(target_sp);
556 } else {
557 sb_error.SetErrorString("invalid debugger");
558 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000559
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
561 if (log)
562 log->Printf("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, "
563 "platform_name=%s, add_dependent_modules=%u, error=%s) => "
564 "SBTarget(%p)",
565 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
566 platform_name, add_dependent_modules, sb_error.GetCString(),
567 static_cast<void *>(target_sp.get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000568
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 return sb_target;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000570}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571
572SBTarget
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename,
574 const char *target_triple) {
575 SBTarget sb_target;
576 TargetSP target_sp;
577 if (m_opaque_sp) {
578 const bool add_dependent_modules = true;
Zachary Turner97206d52017-05-12 04:51:55 +0000579 Status error(m_opaque_sp->GetTargetList().CreateTarget(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 *m_opaque_sp, filename, target_triple, add_dependent_modules, nullptr,
581 target_sp));
582 sb_target.SetSP(target_sp);
583 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000584
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
586 if (log)
587 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple "
588 "(filename=\"%s\", triple=%s) => SBTarget(%p)",
589 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
590 static_cast<void *>(target_sp.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000591
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 return sb_target;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593}
594
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
596 const char *arch_cstr) {
597 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000598
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599 SBTarget sb_target;
600 TargetSP target_sp;
601 if (m_opaque_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000602 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603 const bool add_dependent_modules = true;
604
605 error = m_opaque_sp->GetTargetList().CreateTarget(
606 *m_opaque_sp, filename, arch_cstr, add_dependent_modules, nullptr,
607 target_sp);
608
609 if (error.Success()) {
610 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
611 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613 }
614
615 if (log)
616 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
617 "arch=%s) => SBTarget(%p)",
618 static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr,
619 static_cast<void *>(target_sp.get()));
620
621 return sb_target;
622}
623
624SBTarget SBDebugger::CreateTarget(const char *filename) {
625 SBTarget sb_target;
626 TargetSP target_sp;
627 if (m_opaque_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000628 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 const bool add_dependent_modules = true;
630 error = m_opaque_sp->GetTargetList().CreateTarget(
Zachary Turnera47464b2016-11-18 20:44:46 +0000631 *m_opaque_sp, filename, "", add_dependent_modules, nullptr, target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632
633 if (error.Success()) {
634 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
635 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000636 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637 }
638 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
639 if (log)
640 log->Printf(
641 "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
642 static_cast<void *>(m_opaque_sp.get()), filename,
643 static_cast<void *>(target_sp.get()));
644 return sb_target;
645}
646
Jim Inghamb842f2e2017-09-14 20:22:49 +0000647SBTarget SBDebugger::GetDummyTarget() {
648 SBTarget sb_target;
649 if (m_opaque_sp) {
650 sb_target.SetSP(m_opaque_sp->GetDummyTarget()->shared_from_this());
651 }
652 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
653 if (log)
654 log->Printf(
655 "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)",
656 static_cast<void *>(m_opaque_sp.get()),
657 static_cast<void *>(sb_target.GetSP().get()));
658 return sb_target;
659}
660
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661bool SBDebugger::DeleteTarget(lldb::SBTarget &target) {
662 bool result = false;
663 if (m_opaque_sp) {
664 TargetSP target_sp(target.GetSP());
665 if (target_sp) {
666 // No need to lock, the target list is thread safe
667 result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp);
668 target_sp->Destroy();
669 target.Clear();
670 const bool mandatory = true;
671 ModuleList::RemoveOrphanSharedModules(mandatory);
672 }
673 }
674
675 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
676 if (log)
677 log->Printf("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
678 static_cast<void *>(m_opaque_sp.get()),
679 static_cast<void *>(target.m_opaque_sp.get()), result);
680
681 return result;
682}
683
684SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) {
685 SBTarget sb_target;
686 if (m_opaque_sp) {
687 // No need to lock, the target list is thread safe
688 sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx));
689 }
690 return sb_target;
691}
692
693uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) {
694
695 lldb::TargetSP target_sp = target.GetSP();
696 if (!target_sp)
697 return UINT32_MAX;
698
699 if (!m_opaque_sp)
700 return UINT32_MAX;
701
702 return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP());
703}
704
705SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) {
706 SBTarget sb_target;
707 if (m_opaque_sp) {
708 // No need to lock, the target list is thread safe
709 sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid));
710 }
711 return sb_target;
712}
713
714SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename,
715 const char *arch_name) {
716 SBTarget sb_target;
717 if (m_opaque_sp && filename && filename[0]) {
718 // No need to lock, the target list is thread safe
Pavel Labath7263f1b2017-10-31 10:56:03 +0000719 ArchSpec arch = Platform::GetAugmentedArchSpec(
720 m_opaque_sp->GetPlatformList().GetSelectedPlatform().get(), arch_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 TargetSP target_sp(
722 m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(
723 FileSpec(filename, false), arch_name ? &arch : nullptr));
724 sb_target.SetSP(target_sp);
725 }
726 return sb_target;
727}
728
729SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) {
730 SBTarget sb_target;
731 if (m_opaque_sp) {
732 // No need to lock, the target list is thread safe
733 sb_target.SetSP(
734 m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get()));
735 }
736 return sb_target;
737}
738
739uint32_t SBDebugger::GetNumTargets() {
740 if (m_opaque_sp) {
741 // No need to lock, the target list is thread safe
742 return m_opaque_sp->GetTargetList().GetNumTargets();
743 }
744 return 0;
745}
746
747SBTarget SBDebugger::GetSelectedTarget() {
748 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
749
750 SBTarget sb_target;
751 TargetSP target_sp;
752 if (m_opaque_sp) {
753 // No need to lock, the target list is thread safe
754 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget();
755 sb_target.SetSP(target_sp);
756 }
757
758 if (log) {
759 SBStream sstr;
760 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
761 log->Printf("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
762 static_cast<void *>(m_opaque_sp.get()),
763 static_cast<void *>(target_sp.get()), sstr.GetData());
764 }
765
766 return sb_target;
767}
768
769void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
770 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
771
772 TargetSP target_sp(sb_target.GetSP());
773 if (m_opaque_sp) {
774 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
775 }
776 if (log) {
777 SBStream sstr;
778 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
779 log->Printf("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
780 static_cast<void *>(m_opaque_sp.get()),
781 static_cast<void *>(target_sp.get()), sstr.GetData());
782 }
783}
784
785SBPlatform SBDebugger::GetSelectedPlatform() {
786 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
787
788 SBPlatform sb_platform;
789 DebuggerSP debugger_sp(m_opaque_sp);
790 if (debugger_sp) {
791 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
792 }
793 if (log)
794 log->Printf("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
795 static_cast<void *>(m_opaque_sp.get()),
796 static_cast<void *>(sb_platform.GetSP().get()),
797 sb_platform.GetName());
798 return sb_platform;
799}
800
801void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) {
802 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
803
804 DebuggerSP debugger_sp(m_opaque_sp);
805 if (debugger_sp) {
806 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
807 }
808
809 if (log)
810 log->Printf("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
811 static_cast<void *>(m_opaque_sp.get()),
812 static_cast<void *>(sb_platform.GetSP().get()),
813 sb_platform.GetName());
814}
815
Vadim Macagonc10e34d2017-08-09 09:20:40 +0000816uint32_t SBDebugger::GetNumPlatforms() {
817 if (m_opaque_sp) {
818 // No need to lock, the platform list is thread safe
819 return m_opaque_sp->GetPlatformList().GetSize();
820 }
821 return 0;
822}
823
824SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) {
825 SBPlatform sb_platform;
826 if (m_opaque_sp) {
827 // No need to lock, the platform list is thread safe
828 sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx));
829 }
830 return sb_platform;
831}
832
833uint32_t SBDebugger::GetNumAvailablePlatforms() {
834 uint32_t idx = 0;
835 while (true) {
836 if (!PluginManager::GetPlatformPluginNameAtIndex(idx)) {
837 break;
838 }
839 ++idx;
840 }
841 // +1 for the host platform, which should always appear first in the list.
842 return idx + 1;
843}
844
845SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) {
846 SBStructuredData data;
847 auto platform_dict = llvm::make_unique<StructuredData::Dictionary>();
848 llvm::StringRef name_str("name"), desc_str("description");
849
850 if (idx == 0) {
851 PlatformSP host_platform_sp(Platform::GetHostPlatform());
852 platform_dict->AddStringItem(
853 name_str, host_platform_sp->GetPluginName().GetStringRef());
854 platform_dict->AddStringItem(
855 desc_str, llvm::StringRef(host_platform_sp->GetDescription()));
856 } else if (idx > 0) {
857 const char *plugin_name =
858 PluginManager::GetPlatformPluginNameAtIndex(idx - 1);
859 if (!plugin_name) {
860 return data;
861 }
862 platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name));
863
864 const char *plugin_desc =
865 PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1);
866 if (!plugin_desc) {
867 return data;
868 }
869 platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc));
870 }
871
872 data.m_impl_up->SetObjectSP(
873 StructuredData::ObjectSP(platform_dict.release()));
874 return data;
875}
876
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) {
878 DispatchInput(data, data_len);
879}
880
881void SBDebugger::DispatchInput(const void *data, size_t data_len) {
882 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
883 //
884 // if (log)
885 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\",
886 // size_t=%" PRIu64 ")",
887 // m_opaque_sp.get(),
888 // (int) data_len,
889 // (const char *) data,
890 // (uint64_t)data_len);
891 //
892 // if (m_opaque_sp)
893 // m_opaque_sp->DispatchInput ((const char *) data, data_len);
894}
895
896void SBDebugger::DispatchInputInterrupt() {
897 if (m_opaque_sp)
898 m_opaque_sp->DispatchInputInterrupt();
899}
900
901void SBDebugger::DispatchInputEndOfFile() {
902 if (m_opaque_sp)
903 m_opaque_sp->DispatchInputEndOfFile();
904}
905
906void SBDebugger::PushInputReader(SBInputReader &reader) {}
907
908void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
909 bool spawn_thread) {
910 if (m_opaque_sp) {
911 CommandInterpreterRunOptions options;
912
913 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(
914 auto_handle_events, spawn_thread, options);
915 }
916}
917
918void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
919 bool spawn_thread,
920 SBCommandInterpreterRunOptions &options,
921 int &num_errors, bool &quit_requested,
922 bool &stopped_for_crash)
923
924{
925 if (m_opaque_sp) {
926 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
927 interp.RunCommandInterpreter(auto_handle_events, spawn_thread,
928 options.ref());
929 num_errors = interp.GetNumErrors();
930 quit_requested = interp.GetQuitRequested();
931 stopped_for_crash = interp.GetStoppedForCrash();
932 }
933}
934
935SBError SBDebugger::RunREPL(lldb::LanguageType language,
936 const char *repl_options) {
937 SBError error;
938 if (m_opaque_sp)
939 error.ref() = m_opaque_sp->RunREPL(language, repl_options);
940 else
941 error.SetErrorString("invalid debugger");
942 return error;
943}
944
945void SBDebugger::reset(const DebuggerSP &debugger_sp) {
946 m_opaque_sp = debugger_sp;
947}
948
949Debugger *SBDebugger::get() const { return m_opaque_sp.get(); }
950
951Debugger &SBDebugger::ref() const {
952 assert(m_opaque_sp.get());
953 return *m_opaque_sp;
954}
955
956const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; }
957
958SBDebugger SBDebugger::FindDebuggerWithID(int id) {
959 // No need to lock, the debugger list is thread safe
960 SBDebugger sb_debugger;
961 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id);
962 if (debugger_sp)
963 sb_debugger.reset(debugger_sp);
964 return sb_debugger;
965}
966
967const char *SBDebugger::GetInstanceName() {
968 return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr);
969}
970
971SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
972 const char *debugger_instance_name) {
973 SBError sb_error;
974 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
975 ConstString(debugger_instance_name)));
Zachary Turner97206d52017-05-12 04:51:55 +0000976 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 if (debugger_sp) {
978 ExecutionContext exe_ctx(
979 debugger_sp->GetCommandInterpreter().GetExecutionContext());
980 error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
981 var_name, value);
982 } else {
983 error.SetErrorStringWithFormat("invalid debugger instance name '%s'",
984 debugger_instance_name);
985 }
986 if (error.Fail())
987 sb_error.SetError(error);
988 return sb_error;
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000989}
990
Greg Clayton431ce672011-04-18 23:15:17 +0000991SBStringList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992SBDebugger::GetInternalVariableValue(const char *var_name,
993 const char *debugger_instance_name) {
994 SBStringList ret_value;
995 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
996 ConstString(debugger_instance_name)));
Zachary Turner97206d52017-05-12 04:51:55 +0000997 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998 if (debugger_sp) {
999 ExecutionContext exe_ctx(
1000 debugger_sp->GetCommandInterpreter().GetExecutionContext());
1001 lldb::OptionValueSP value_sp(
1002 debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error));
1003 if (value_sp) {
1004 StreamString value_strm;
1005 value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
1006 const std::string &value_str = value_strm.GetString();
1007 if (!value_str.empty()) {
1008 StringList string_list;
1009 string_list.SplitIntoLines(value_str);
1010 return SBStringList(&string_list);
1011 }
1012 }
1013 }
1014 return SBStringList();
1015}
1016
1017uint32_t SBDebugger::GetTerminalWidth() const {
1018 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
1019}
1020
1021void SBDebugger::SetTerminalWidth(uint32_t term_width) {
1022 if (m_opaque_sp)
1023 m_opaque_sp->SetTerminalWidth(term_width);
1024}
1025
1026const char *SBDebugger::GetPrompt() const {
1027 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1028
1029 if (log)
1030 log->Printf("SBDebugger(%p)::GetPrompt () => \"%s\"",
1031 static_cast<void *>(m_opaque_sp.get()),
Zachary Turner514d8cd2016-09-23 18:06:53 +00001032 (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : ""));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033
Zachary Turner514d8cd2016-09-23 18:06:53 +00001034 return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
1035 : nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036}
1037
1038void SBDebugger::SetPrompt(const char *prompt) {
1039 if (m_opaque_sp)
Zachary Turner514d8cd2016-09-23 18:06:53 +00001040 m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041}
1042
1043ScriptLanguage SBDebugger::GetScriptLanguage() const {
1044 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
1045}
1046
1047void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) {
1048 if (m_opaque_sp) {
1049 m_opaque_sp->SetScriptLanguage(script_lang);
1050 }
1051}
1052
1053bool SBDebugger::SetUseExternalEditor(bool value) {
1054 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
1055}
1056
1057bool SBDebugger::GetUseExternalEditor() {
1058 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
1059}
1060
1061bool SBDebugger::SetUseColor(bool value) {
1062 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
1063}
1064
1065bool SBDebugger::GetUseColor() const {
1066 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
1067}
1068
1069bool SBDebugger::GetDescription(SBStream &description) {
1070 Stream &strm = description.ref();
1071
1072 if (m_opaque_sp) {
1073 const char *name = m_opaque_sp->GetInstanceName().AsCString();
1074 user_id_t id = m_opaque_sp->GetID();
1075 strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1076 } else
1077 strm.PutCString("No value");
1078
1079 return true;
1080}
1081
1082user_id_t SBDebugger::GetID() {
1083 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
1084}
1085
1086SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
1087 SBError sb_error;
1088 if (m_opaque_sp) {
1089 if (platform_name_cstr && platform_name_cstr[0]) {
1090 ConstString platform_name(platform_name_cstr);
1091 PlatformSP platform_sp(Platform::Find(platform_name));
1092
1093 if (platform_sp) {
1094 // Already have a platform with this name, just select it
1095 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
1096 } else {
1097 // We don't have a platform by this name yet, create one
1098 platform_sp = Platform::Create(platform_name, sb_error.ref());
1099 if (platform_sp) {
1100 // We created the platform, now append and select it
1101 bool make_selected = true;
1102 m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected);
Greg Clayton6920b522012-08-22 18:39:03 +00001103 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001104 }
1105 } else {
1106 sb_error.ref().SetErrorString("invalid platform name");
Greg Clayton6920b522012-08-22 18:39:03 +00001107 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 } else {
1109 sb_error.ref().SetErrorString("invalid debugger");
1110 }
1111 return sb_error;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001112}
1113
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) {
1115 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1116 if (m_opaque_sp) {
1117 PlatformSP platform_sp(
1118 m_opaque_sp->GetPlatformList().GetSelectedPlatform());
Greg Claytona7015092010-09-18 01:14:36 +00001119
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 if (platform_sp) {
1121 if (log && sysroot)
1122 log->Printf("SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")", sysroot);
1123 platform_sp->SetSDKRootDirectory(ConstString(sysroot));
1124 return true;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 }
1127 return false;
Greg Claytona7015092010-09-18 01:14:36 +00001128}
1129
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130bool SBDebugger::GetCloseInputOnEOF() const {
1131 return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false);
Jim Inghame40e4212010-08-30 19:44:40 +00001132}
1133
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134void SBDebugger::SetCloseInputOnEOF(bool b) {
1135 if (m_opaque_sp)
1136 m_opaque_sp->SetCloseInputOnEOF(b);
Jim Inghame40e4212010-08-30 19:44:40 +00001137}
1138
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139SBTypeCategory SBDebugger::GetCategory(const char *category_name) {
1140 if (!category_name || *category_name == 0)
1141 return SBTypeCategory();
1142
1143 TypeCategoryImplSP category_sp;
1144
1145 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1146 category_sp, false))
1147 return SBTypeCategory(category_sp);
1148 else
1149 return SBTypeCategory();
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001150}
1151
Kate Stoneb9c1b512016-09-06 20:57:50 +00001152SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) {
1153 TypeCategoryImplSP category_sp;
1154 if (DataVisualization::Categories::GetCategory(lang_type, category_sp))
1155 return SBTypeCategory(category_sp);
1156 else
1157 return SBTypeCategory();
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001158}
1159
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160SBTypeCategory SBDebugger::CreateCategory(const char *category_name) {
1161 if (!category_name || *category_name == 0)
1162 return SBTypeCategory();
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001163
Kate Stoneb9c1b512016-09-06 20:57:50 +00001164 TypeCategoryImplSP category_sp;
1165
1166 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1167 category_sp, true))
1168 return SBTypeCategory(category_sp);
1169 else
1170 return SBTypeCategory();
Caroline Ticedde9cff2010-09-20 05:20:02 +00001171}
Caroline Ticeefed6132010-11-19 20:47:54 +00001172
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173bool SBDebugger::DeleteCategory(const char *category_name) {
1174 if (!category_name || *category_name == 0)
1175 return false;
1176
1177 return DataVisualization::Categories::Delete(ConstString(category_name));
Caroline Ticeefed6132010-11-19 20:47:54 +00001178}
Greg Clayton2289fa42011-04-30 01:09:13 +00001179
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180uint32_t SBDebugger::GetNumCategories() {
1181 return DataVisualization::Categories::GetCount();
Greg Clayton2289fa42011-04-30 01:09:13 +00001182}
1183
Kate Stoneb9c1b512016-09-06 20:57:50 +00001184SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) {
1185 return SBTypeCategory(
1186 DataVisualization::Categories::GetCategoryAtIndex(index));
1187}
1188
1189SBTypeCategory SBDebugger::GetDefaultCategory() {
1190 return GetCategory("default");
1191}
1192
1193SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) {
1194 SBTypeCategory default_category_sb = GetDefaultCategory();
1195 if (default_category_sb.GetEnabled())
1196 return default_category_sb.GetFormatForType(type_name);
1197 return SBTypeFormat();
1198}
1199
1200#ifndef LLDB_DISABLE_PYTHON
1201SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) {
1202 if (!type_name.IsValid())
1203 return SBTypeSummary();
1204 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1205}
1206#endif // LLDB_DISABLE_PYTHON
1207
1208SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) {
1209 if (!type_name.IsValid())
1210 return SBTypeFilter();
1211 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1212}
1213
1214#ifndef LLDB_DISABLE_PYTHON
1215SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) {
1216 if (!type_name.IsValid())
1217 return SBTypeSynthetic();
1218 return SBTypeSynthetic(
1219 DataVisualization::GetSyntheticForType(type_name.GetSP()));
1220}
1221#endif // LLDB_DISABLE_PYTHON
1222
Pavel Labath5e336902017-03-01 10:08:40 +00001223static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) {
1224 if (categories == nullptr)
1225 return {};
1226 size_t len = 0;
1227 while (categories[len] != nullptr)
1228 ++len;
1229 return llvm::makeArrayRef(categories, len);
1230}
1231
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232bool SBDebugger::EnableLog(const char *channel, const char **categories) {
1233 if (m_opaque_sp) {
1234 uint32_t log_options =
1235 LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
Pavel Labath775588c2017-03-15 09:06:58 +00001236 std::string error;
1237 llvm::raw_string_ostream error_stream(error);
Pavel Labath5e336902017-03-01 10:08:40 +00001238 return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
Pavel Labath775588c2017-03-15 09:06:58 +00001239 log_options, error_stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001240 } else
Greg Claytonf3dd93c2011-06-17 03:31:01 +00001241 return false;
1242}
1243
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
1245 void *baton) {
1246 if (m_opaque_sp) {
1247 return m_opaque_sp->SetLoggingCallback(log_callback, baton);
1248 }
Jim Ingham4f02b222012-02-22 22:49:20 +00001249}