blob: 99afa3652ed09f5d08b9a1810d3a282f8503ab0a [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
Alex Langfordd17cd902018-05-25 20:28:16 +000014
15#include "SystemInitializerFull.h"
16
Eli Friedmanca93cc12010-06-09 07:37:52 +000017#include "lldb/API/SBDebugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
Greg Claytone0d378b2011-03-24 21:19:54 +000019#include "lldb/lldb-private.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
Eli Friedmanca93cc12010-06-09 07:37:52 +000021#include "lldb/API/SBBroadcaster.h"
22#include "lldb/API/SBCommandInterpreter.h"
23#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton2289fa42011-04-30 01:09:13 +000024#include "lldb/API/SBError.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000025#include "lldb/API/SBEvent.h"
26#include "lldb/API/SBFrame.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000027#include "lldb/API/SBListener.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000028#include "lldb/API/SBProcess.h"
29#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000030#include "lldb/API/SBStream.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000031#include "lldb/API/SBStringList.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000032#include "lldb/API/SBStructuredData.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000033#include "lldb/API/SBTarget.h"
34#include "lldb/API/SBThread.h"
Enrico Granata061858c2012-02-15 02:34:21 +000035#include "lldb/API/SBTypeCategory.h"
Enrico Granata061858c2012-02-15 02:34:21 +000036#include "lldb/API/SBTypeFilter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000037#include "lldb/API/SBTypeFormat.h"
Enrico Granata061858c2012-02-15 02:34:21 +000038#include "lldb/API/SBTypeNameSpecifier.h"
39#include "lldb/API/SBTypeSummary.h"
40#include "lldb/API/SBTypeSynthetic.h"
41
Greg Clayton6eee5aa2010-10-11 01:05:37 +000042#include "lldb/Core/Debugger.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000043#include "lldb/Core/PluginManager.h"
Greg Clayton44d93782014-01-27 23:43:24 +000044#include "lldb/Core/StreamFile.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000045#include "lldb/Core/StructuredDataImpl.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000046#include "lldb/DataFormatters/DataVisualization.h"
Pavel Labathf1389e92018-02-19 15:06:28 +000047#include "lldb/Host/XML.h"
Zachary Turnere6e2bb32015-03-31 21:03:22 +000048#include "lldb/Initialization/SystemLifetimeManager.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000049#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"
Pavel Labath145d95c2018-04-17 18:53:35 +000054#include "lldb/Utility/Args.h"
Pavel Labathd821c992018-08-07 11:07:21 +000055#include "lldb/Utility/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
Enrico Granatad717cc92015-10-20 04:50:09 +000057#include "llvm/ADT/STLExtras.h"
Zachary Turner7b2e5a32016-09-16 19:09:12 +000058#include "llvm/ADT/StringRef.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000059#include "llvm/Support/DynamicLibrary.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000060#include "llvm/Support/ManagedStatic.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000061
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062using namespace lldb;
63using namespace lldb_private;
64
Kate Stoneb9c1b512016-09-06 20:57:50 +000065static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp,
66 const FileSpec &spec,
Zachary Turner97206d52017-05-12 04:51:55 +000067 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 llvm::sys::DynamicLibrary dynlib =
69 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
70 if (dynlib.isValid()) {
71 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger);
72
73 lldb::SBDebugger debugger_sb(debugger_sp);
74 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
75 // function.
76 // TODO: mangle this differently for your system - on OSX, the first
77 // underscore needs to be removed and the second one stays
78 LLDBCommandPluginInit init_func =
Stephane Sezer22701b22017-10-24 23:46:00 +000079 (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
81 if (init_func) {
82 if (init_func(debugger_sb))
83 return dynlib;
84 else
85 error.SetErrorString("plug-in refused to load "
86 "(lldb::PluginInitialize(lldb::SBDebugger) "
87 "returned false)");
88 } else {
89 error.SetErrorString("plug-in is missing the required initialization: "
90 "lldb::PluginInitialize(lldb::SBDebugger)");
Greg Clayton5fb8f792013-12-02 19:35:49 +000091 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 } else {
93 if (spec.Exists())
94 error.SetErrorString("this file does not represent a loadable dylib");
Greg Clayton5fb8f792013-12-02 19:35:49 +000095 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 error.SetErrorString("no such file");
97 }
98 return llvm::sys::DynamicLibrary();
Greg Clayton5fb8f792013-12-02 19:35:49 +000099}
100
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000101static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103SBError SBInputReader::Initialize(
104 lldb::SBDebugger &sb_debugger,
105 unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction,
106 char const *, unsigned long),
107 void *, lldb::InputReaderGranularity, char const *, char const *, bool) {
108 return SBError();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000109}
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111void SBInputReader::SetIsDone(bool) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113bool SBInputReader::IsActive() const { return false; }
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000114
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000115SBDebugger::SBDebugger() = default;
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp)
118 : m_opaque_sp(debugger_sp) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000121
122SBDebugger::~SBDebugger() = default;
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) {
125 if (this != &rhs) {
126 m_opaque_sp = rhs.m_opaque_sp;
127 }
128 return *this;
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000129}
130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131void SBDebugger::Initialize() {
132 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 if (log)
135 log->Printf("SBDebugger::Initialize ()");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 g_debugger_lifetime->Initialize(llvm::make_unique<SystemInitializerFull>(),
138 LoadPlugin);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141void SBDebugger::Terminate() { g_debugger_lifetime->Terminate(); }
142
143void SBDebugger::Clear() {
144 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
145
146 if (log)
147 log->Printf("SBDebugger(%p)::Clear ()",
148 static_cast<void *>(m_opaque_sp.get()));
149
150 if (m_opaque_sp)
151 m_opaque_sp->ClearIOHandlers();
152
153 m_opaque_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156SBDebugger SBDebugger::Create() {
157 return SBDebugger::Create(false, nullptr, nullptr);
Greg Clayton48e42542010-07-30 20:12:55 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160SBDebugger SBDebugger::Create(bool source_init_files) {
161 return SBDebugger::Create(source_init_files, nullptr, nullptr);
Jim Ingham06942692011-08-13 00:22:20 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164SBDebugger SBDebugger::Create(bool source_init_files,
165 lldb::LogOutputCallback callback, void *baton)
Jim Ingham228063c2012-02-21 02:23:08 +0000166
167{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 SBDebugger debugger;
Greg Claytoncb172b12014-05-19 20:42:14 +0000171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 // Currently we have issues if this function is called simultaneously on two
Adrian Prantl05097242018-04-30 16:49:04 +0000173 // different threads. The issues mainly revolve around the fact that the
174 // lldb_private::FormatManager uses global collections and having two threads
175 // parsing the .lldbinit files can cause mayhem. So to get around this for
176 // now we need to use a mutex to prevent bad things from happening.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 static std::recursive_mutex g_mutex;
178 std::lock_guard<std::recursive_mutex> guard(g_mutex);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 debugger.reset(Debugger::CreateInstance(callback, baton));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 if (log) {
183 SBStream sstr;
184 debugger.GetDescription(sstr);
185 log->Printf("SBDebugger::Create () => SBDebugger(%p): %s",
186 static_cast<void *>(debugger.m_opaque_sp.get()),
187 sstr.GetData());
188 }
189
190 SBCommandInterpreter interp = debugger.GetCommandInterpreter();
191 if (source_init_files) {
192 interp.get()->SkipLLDBInitFiles(false);
193 interp.get()->SkipAppInitFiles(false);
194 SBCommandReturnObject result;
195 interp.SourceInitFileInHomeDirectory(result);
196 } else {
197 interp.get()->SkipLLDBInitFiles(true);
198 interp.get()->SkipAppInitFiles(true);
199 }
200 return debugger;
Greg Clayton66111032010-06-23 01:19:29 +0000201}
202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203void SBDebugger::Destroy(SBDebugger &debugger) {
204 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 if (log) {
207 SBStream sstr;
208 debugger.GetDescription(sstr);
209 log->Printf("SBDebugger::Destroy () => SBDebugger(%p): %s",
210 static_cast<void *>(debugger.m_opaque_sp.get()),
211 sstr.GetData());
212 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 Debugger::Destroy(debugger.m_opaque_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 if (debugger.m_opaque_sp.get() != nullptr)
217 debugger.m_opaque_sp.reset();
Caroline Ticee02657b2011-01-22 01:02:07 +0000218}
219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220void SBDebugger::MemoryPressureDetected() {
Adrian Prantl05097242018-04-30 16:49:04 +0000221 // Since this function can be call asynchronously, we allow it to be non-
222 // mandatory. We have seen deadlocks with this function when called so we
223 // need to safeguard against this until we can determine what is causing the
224 // deadlocks.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
226
227 const bool mandatory = false;
228 if (log) {
229 log->Printf("SBDebugger::MemoryPressureDetected (), mandatory = %d",
230 mandatory);
231 }
232
233 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonf9322412011-12-15 04:38:41 +0000234}
235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236bool SBDebugger::IsValid() const { return m_opaque_sp.get() != nullptr; }
237
238void SBDebugger::SetAsync(bool b) {
239 if (m_opaque_sp)
240 m_opaque_sp->SetAsyncExecution(b);
Greg Clayton66111032010-06-23 01:19:29 +0000241}
242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243bool SBDebugger::GetAsync() {
244 return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245}
246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247void SBDebugger::SkipLLDBInitFiles(bool b) {
248 if (m_opaque_sp)
249 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b);
Jim Inghame64f0dc2011-09-13 23:25:31 +0000250}
251
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252void SBDebugger::SkipAppInitFiles(bool b) {
253 if (m_opaque_sp)
254 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b);
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000255}
256
Adrian Prantl05097242018-04-30 16:49:04 +0000257// Shouldn't really be settable after initialization as this could cause lots
258// of problems; don't want users trying to switch modes in the middle of a
259// debugging session.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) {
261 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 if (log)
264 log->Printf(
265 "SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)",
266 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
267 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 if (m_opaque_sp)
270 m_opaque_sp->SetInputFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271}
272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) {
274 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 if (log)
277 log->Printf(
278 "SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)",
279 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
280 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000281
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 if (m_opaque_sp)
283 m_opaque_sp->SetOutputFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284}
285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) {
287 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000288
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 if (log)
290 log->Printf(
291 "SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)",
292 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
293 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 if (m_opaque_sp)
296 m_opaque_sp->SetErrorFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297}
298
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299FILE *SBDebugger::GetInputFileHandle() {
300 if (m_opaque_sp) {
301 StreamFileSP stream_file_sp(m_opaque_sp->GetInputFile());
302 if (stream_file_sp)
303 return stream_file_sp->GetFile().GetStream();
304 }
305 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306}
307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308FILE *SBDebugger::GetOutputFileHandle() {
309 if (m_opaque_sp) {
310 StreamFileSP stream_file_sp(m_opaque_sp->GetOutputFile());
311 if (stream_file_sp)
312 return stream_file_sp->GetFile().GetStream();
313 }
314 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315}
316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317FILE *SBDebugger::GetErrorFileHandle() {
318 if (m_opaque_sp) {
319 StreamFileSP stream_file_sp(m_opaque_sp->GetErrorFile());
320 if (stream_file_sp)
321 return stream_file_sp->GetFile().GetStream();
322 }
323 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324}
325
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326void SBDebugger::SaveInputTerminalState() {
327 if (m_opaque_sp)
328 m_opaque_sp->SaveInputTerminalState();
Jim Inghamc5917d92012-11-30 20:23:19 +0000329}
330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331void SBDebugger::RestoreInputTerminalState() {
332 if (m_opaque_sp)
333 m_opaque_sp->RestoreInputTerminalState();
Jim Inghamc5917d92012-11-30 20:23:19 +0000334}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335SBCommandInterpreter SBDebugger::GetCommandInterpreter() {
336 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000337
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 SBCommandInterpreter sb_interpreter;
339 if (m_opaque_sp)
340 sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 if (log)
343 log->Printf(
344 "SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)",
345 static_cast<void *>(m_opaque_sp.get()),
346 static_cast<void *>(sb_interpreter.get()));
Caroline Tice750cd172010-10-26 23:49:36 +0000347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 return sb_interpreter;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349}
350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351void SBDebugger::HandleCommand(const char *command) {
352 if (m_opaque_sp) {
353 TargetSP target_sp(m_opaque_sp->GetSelectedTarget());
354 std::unique_lock<std::recursive_mutex> lock;
355 if (target_sp)
356 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +0000357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 SBCommandInterpreter sb_interpreter(GetCommandInterpreter());
359 SBCommandReturnObject result;
Greg Clayton66111032010-06-23 01:19:29 +0000360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 sb_interpreter.HandleCommand(command, result, false);
Greg Clayton66111032010-06-23 01:19:29 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 if (GetErrorFileHandle() != nullptr)
364 result.PutError(GetErrorFileHandle());
365 if (GetOutputFileHandle() != nullptr)
366 result.PutOutput(GetOutputFileHandle());
Greg Clayton66111032010-06-23 01:19:29 +0000367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 if (!m_opaque_sp->GetAsyncExecution()) {
369 SBProcess process(GetCommandInterpreter().GetProcess());
370 ProcessSP process_sp(process.GetSP());
371 if (process_sp) {
372 EventSP event_sp;
373 ListenerSP lldb_listener_sp = m_opaque_sp->GetListener();
Pavel Labathd35031e12016-11-30 10:41:42 +0000374 while (lldb_listener_sp->GetEventForBroadcaster(
375 process_sp.get(), event_sp, std::chrono::seconds(0))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 SBEvent event(event_sp);
377 HandleProcessEvent(process, event, GetOutputFileHandle(),
378 GetErrorFileHandle());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383}
384
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385SBListener SBDebugger::GetListener() {
386 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 SBListener sb_listener;
389 if (m_opaque_sp)
390 sb_listener.reset(m_opaque_sp->GetListener());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000391
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 if (log)
393 log->Printf("SBDebugger(%p)::GetListener () => SBListener(%p)",
394 static_cast<void *>(m_opaque_sp.get()),
395 static_cast<void *>(sb_listener.get()));
Caroline Tice750cd172010-10-26 23:49:36 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 return sb_listener;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398}
399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400void SBDebugger::HandleProcessEvent(const SBProcess &process,
401 const SBEvent &event, FILE *out,
402 FILE *err) {
403 if (!process.IsValid())
404 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 TargetSP target_sp(process.GetTarget().GetSP());
407 if (!target_sp)
408 return;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000409
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 const uint32_t event_type = event.GetType();
411 char stdio_buffer[1024];
412 size_t len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416 if (event_type &
417 (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) {
418 // Drain stdout when we stop just in case we have any bytes
419 while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0)
420 if (out != nullptr)
421 ::fwrite(stdio_buffer, 1, len, out);
422 }
Todd Fiala75930012016-08-19 04:21:48 +0000423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 if (event_type &
425 (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) {
426 // Drain stderr when we stop just in case we have any bytes
427 while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0)
428 if (err != nullptr)
429 ::fwrite(stdio_buffer, 1, len, err);
430 }
431
432 if (event_type & Process::eBroadcastBitStateChanged) {
433 StateType event_state = SBProcess::GetStateFromEvent(event);
434
435 if (event_state == eStateInvalid)
436 return;
437
438 bool is_stopped = StateIsStoppedState(event_state);
439 if (!is_stopped)
440 process.ReportEventState(event, out);
441 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442}
443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444SBSourceManager SBDebugger::GetSourceManager() {
445 SBSourceManager sb_source_manager(*this);
446 return sb_source_manager;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447}
448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) {
450 if (arch_name && arch_name_len) {
451 ArchSpec default_arch = Target::GetDefaultArchitecture();
Caroline Ticedaccaa92010-09-20 20:44:43 +0000452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 if (default_arch.IsValid()) {
454 const std::string &triple_str = default_arch.GetTriple().str();
455 if (!triple_str.empty())
456 ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str());
457 else
458 ::snprintf(arch_name, arch_name_len, "%s",
459 default_arch.GetArchitectureName());
460 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 }
463 if (arch_name && arch_name_len)
464 arch_name[0] = '\0';
465 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466}
467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468bool SBDebugger::SetDefaultArchitecture(const char *arch_name) {
469 if (arch_name) {
470 ArchSpec arch(arch_name);
471 if (arch.IsValid()) {
472 Target::SetDefaultArchitecture(arch);
473 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 }
476 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477}
478
479ScriptLanguage
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480SBDebugger::GetScriptingLanguage(const char *script_language_name) {
Zachary Turner7b2e5a32016-09-16 19:09:12 +0000481 if (!script_language_name) return eScriptLanguageDefault;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000482 return OptionArgParser::ToScriptLanguage(
483 llvm::StringRef(script_language_name), eScriptLanguageDefault, nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486const char *SBDebugger::GetVersionString() {
487 return lldb_private::GetVersion();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488}
489
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490const char *SBDebugger::StateAsCString(StateType state) {
491 return lldb_private::StateAsCString(state);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492}
493
Pavel Labathf1389e92018-02-19 15:06:28 +0000494static void AddBoolConfigEntry(StructuredData::Dictionary &dict,
495 llvm::StringRef name, bool value,
496 llvm::StringRef description) {
497 auto entry_up = llvm::make_unique<StructuredData::Dictionary>();
498 entry_up->AddBooleanItem("value", value);
499 entry_up->AddStringItem("description", description);
500 dict.AddItem(name, std::move(entry_up));
501}
502
Pavel Labathabc0c6a2018-06-28 14:23:04 +0000503static void AddLLVMTargets(StructuredData::Dictionary &dict) {
504 auto array_up = llvm::make_unique<StructuredData::Array>();
505#define LLVM_TARGET(target) \
506 array_up->AddItem(llvm::make_unique<StructuredData::String>(#target));
507#include "llvm/Config/Targets.def"
508 auto entry_up = llvm::make_unique<StructuredData::Dictionary>();
509 entry_up->AddItem("value", std::move(array_up));
510 entry_up->AddStringItem("description", "A list of configured LLVM targets.");
511 dict.AddItem("targets", std::move(entry_up));
512}
513
Pavel Labathf1389e92018-02-19 15:06:28 +0000514SBStructuredData SBDebugger::GetBuildConfiguration() {
515 auto config_up = llvm::make_unique<StructuredData::Dictionary>();
516 AddBoolConfigEntry(
517 *config_up, "xml", XMLDocument::XMLEnabled(),
518 "A boolean value that indicates if XML support is enabled in LLDB");
Pavel Labathabc0c6a2018-06-28 14:23:04 +0000519 AddLLVMTargets(*config_up);
Pavel Labathf1389e92018-02-19 15:06:28 +0000520
521 SBStructuredData data;
522 data.m_impl_up->SetObjectSP(std::move(config_up));
523 return data;
524}
525
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526bool SBDebugger::StateIsRunningState(StateType state) {
527 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000528
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 const bool result = lldb_private::StateIsRunningState(state);
530 if (log)
531 log->Printf("SBDebugger::StateIsRunningState (state=%s) => %i",
532 StateAsCString(state), result);
Greg Clayton48381312010-10-30 04:51:46 +0000533
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535}
536
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537bool SBDebugger::StateIsStoppedState(StateType state) {
538 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000539
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540 const bool result = lldb_private::StateIsStoppedState(state, false);
541 if (log)
542 log->Printf("SBDebugger::StateIsStoppedState (state=%s) => %i",
543 StateAsCString(state), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546}
547
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548lldb::SBTarget SBDebugger::CreateTarget(const char *filename,
549 const char *target_triple,
550 const char *platform_name,
551 bool add_dependent_modules,
552 lldb::SBError &sb_error) {
553 SBTarget sb_target;
554 TargetSP target_sp;
555 if (m_opaque_sp) {
556 sb_error.Clear();
557 OptionGroupPlatform platform_options(false);
558 platform_options.SetPlatformName(platform_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000559
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000561 *m_opaque_sp, filename, target_triple,
562 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 &platform_options, target_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000564
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 if (sb_error.Success())
566 sb_target.SetSP(target_sp);
567 } else {
568 sb_error.SetErrorString("invalid debugger");
569 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000570
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
572 if (log)
573 log->Printf("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, "
574 "platform_name=%s, add_dependent_modules=%u, error=%s) => "
575 "SBTarget(%p)",
576 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
577 platform_name, add_dependent_modules, sb_error.GetCString(),
578 static_cast<void *>(target_sp.get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000579
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 return sb_target;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000581}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582
583SBTarget
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename,
585 const char *target_triple) {
586 SBTarget sb_target;
587 TargetSP target_sp;
588 if (m_opaque_sp) {
589 const bool add_dependent_modules = true;
Zachary Turner97206d52017-05-12 04:51:55 +0000590 Status error(m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000591 *m_opaque_sp, filename, target_triple,
592 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 target_sp));
594 sb_target.SetSP(target_sp);
595 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000596
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
598 if (log)
599 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple "
600 "(filename=\"%s\", triple=%s) => SBTarget(%p)",
601 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
602 static_cast<void *>(target_sp.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000603
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604 return sb_target;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605}
606
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
608 const char *arch_cstr) {
609 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000610
Kate Stoneb9c1b512016-09-06 20:57:50 +0000611 SBTarget sb_target;
612 TargetSP target_sp;
613 if (m_opaque_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000614 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 const bool add_dependent_modules = true;
616
617 error = m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000618 *m_opaque_sp, filename, arch_cstr,
619 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 target_sp);
621
622 if (error.Success()) {
623 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
624 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000625 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626 }
627
628 if (log)
629 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
630 "arch=%s) => SBTarget(%p)",
631 static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr,
632 static_cast<void *>(target_sp.get()));
633
634 return sb_target;
635}
636
637SBTarget SBDebugger::CreateTarget(const char *filename) {
638 SBTarget sb_target;
639 TargetSP target_sp;
640 if (m_opaque_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000641 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642 const bool add_dependent_modules = true;
643 error = m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000644 *m_opaque_sp, filename, "",
645 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
646 target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647
648 if (error.Success()) {
649 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
650 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000651 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652 }
653 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
654 if (log)
655 log->Printf(
656 "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
657 static_cast<void *>(m_opaque_sp.get()), filename,
658 static_cast<void *>(target_sp.get()));
659 return sb_target;
660}
661
Jim Inghamb842f2e2017-09-14 20:22:49 +0000662SBTarget SBDebugger::GetDummyTarget() {
663 SBTarget sb_target;
664 if (m_opaque_sp) {
665 sb_target.SetSP(m_opaque_sp->GetDummyTarget()->shared_from_this());
666 }
667 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
668 if (log)
669 log->Printf(
670 "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)",
671 static_cast<void *>(m_opaque_sp.get()),
672 static_cast<void *>(sb_target.GetSP().get()));
673 return sb_target;
674}
675
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676bool SBDebugger::DeleteTarget(lldb::SBTarget &target) {
677 bool result = false;
678 if (m_opaque_sp) {
679 TargetSP target_sp(target.GetSP());
680 if (target_sp) {
681 // No need to lock, the target list is thread safe
682 result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp);
683 target_sp->Destroy();
684 target.Clear();
685 const bool mandatory = true;
686 ModuleList::RemoveOrphanSharedModules(mandatory);
687 }
688 }
689
690 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
691 if (log)
692 log->Printf("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
693 static_cast<void *>(m_opaque_sp.get()),
694 static_cast<void *>(target.m_opaque_sp.get()), result);
695
696 return result;
697}
698
699SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) {
700 SBTarget sb_target;
701 if (m_opaque_sp) {
702 // No need to lock, the target list is thread safe
703 sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx));
704 }
705 return sb_target;
706}
707
708uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) {
709
710 lldb::TargetSP target_sp = target.GetSP();
711 if (!target_sp)
712 return UINT32_MAX;
713
714 if (!m_opaque_sp)
715 return UINT32_MAX;
716
717 return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP());
718}
719
720SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) {
721 SBTarget sb_target;
722 if (m_opaque_sp) {
723 // No need to lock, the target list is thread safe
724 sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid));
725 }
726 return sb_target;
727}
728
729SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename,
730 const char *arch_name) {
731 SBTarget sb_target;
732 if (m_opaque_sp && filename && filename[0]) {
733 // No need to lock, the target list is thread safe
Pavel Labath7263f1b2017-10-31 10:56:03 +0000734 ArchSpec arch = Platform::GetAugmentedArchSpec(
735 m_opaque_sp->GetPlatformList().GetSelectedPlatform().get(), arch_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 TargetSP target_sp(
737 m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(
738 FileSpec(filename, false), arch_name ? &arch : nullptr));
739 sb_target.SetSP(target_sp);
740 }
741 return sb_target;
742}
743
744SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) {
745 SBTarget sb_target;
746 if (m_opaque_sp) {
747 // No need to lock, the target list is thread safe
748 sb_target.SetSP(
749 m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get()));
750 }
751 return sb_target;
752}
753
754uint32_t SBDebugger::GetNumTargets() {
755 if (m_opaque_sp) {
756 // No need to lock, the target list is thread safe
757 return m_opaque_sp->GetTargetList().GetNumTargets();
758 }
759 return 0;
760}
761
762SBTarget SBDebugger::GetSelectedTarget() {
763 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
764
765 SBTarget sb_target;
766 TargetSP target_sp;
767 if (m_opaque_sp) {
768 // No need to lock, the target list is thread safe
769 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget();
770 sb_target.SetSP(target_sp);
771 }
772
773 if (log) {
774 SBStream sstr;
775 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
776 log->Printf("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
777 static_cast<void *>(m_opaque_sp.get()),
778 static_cast<void *>(target_sp.get()), sstr.GetData());
779 }
780
781 return sb_target;
782}
783
784void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
785 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
786
787 TargetSP target_sp(sb_target.GetSP());
788 if (m_opaque_sp) {
789 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
790 }
791 if (log) {
792 SBStream sstr;
793 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
794 log->Printf("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
795 static_cast<void *>(m_opaque_sp.get()),
796 static_cast<void *>(target_sp.get()), sstr.GetData());
797 }
798}
799
800SBPlatform SBDebugger::GetSelectedPlatform() {
801 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
802
803 SBPlatform sb_platform;
804 DebuggerSP debugger_sp(m_opaque_sp);
805 if (debugger_sp) {
806 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
807 }
808 if (log)
809 log->Printf("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
810 static_cast<void *>(m_opaque_sp.get()),
811 static_cast<void *>(sb_platform.GetSP().get()),
812 sb_platform.GetName());
813 return sb_platform;
814}
815
816void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) {
817 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
818
819 DebuggerSP debugger_sp(m_opaque_sp);
820 if (debugger_sp) {
821 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
822 }
823
824 if (log)
825 log->Printf("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
826 static_cast<void *>(m_opaque_sp.get()),
827 static_cast<void *>(sb_platform.GetSP().get()),
828 sb_platform.GetName());
829}
830
Vadim Macagonc10e34d2017-08-09 09:20:40 +0000831uint32_t SBDebugger::GetNumPlatforms() {
832 if (m_opaque_sp) {
833 // No need to lock, the platform list is thread safe
834 return m_opaque_sp->GetPlatformList().GetSize();
835 }
836 return 0;
837}
838
839SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) {
840 SBPlatform sb_platform;
841 if (m_opaque_sp) {
842 // No need to lock, the platform list is thread safe
843 sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx));
844 }
845 return sb_platform;
846}
847
848uint32_t SBDebugger::GetNumAvailablePlatforms() {
849 uint32_t idx = 0;
850 while (true) {
851 if (!PluginManager::GetPlatformPluginNameAtIndex(idx)) {
852 break;
853 }
854 ++idx;
855 }
856 // +1 for the host platform, which should always appear first in the list.
857 return idx + 1;
858}
859
860SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) {
861 SBStructuredData data;
862 auto platform_dict = llvm::make_unique<StructuredData::Dictionary>();
863 llvm::StringRef name_str("name"), desc_str("description");
864
865 if (idx == 0) {
866 PlatformSP host_platform_sp(Platform::GetHostPlatform());
867 platform_dict->AddStringItem(
868 name_str, host_platform_sp->GetPluginName().GetStringRef());
869 platform_dict->AddStringItem(
870 desc_str, llvm::StringRef(host_platform_sp->GetDescription()));
871 } else if (idx > 0) {
872 const char *plugin_name =
873 PluginManager::GetPlatformPluginNameAtIndex(idx - 1);
874 if (!plugin_name) {
875 return data;
876 }
877 platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name));
878
879 const char *plugin_desc =
880 PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1);
881 if (!plugin_desc) {
882 return data;
883 }
884 platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc));
885 }
886
887 data.m_impl_up->SetObjectSP(
888 StructuredData::ObjectSP(platform_dict.release()));
889 return data;
890}
891
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) {
893 DispatchInput(data, data_len);
894}
895
896void SBDebugger::DispatchInput(const void *data, size_t data_len) {
897 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
898 //
899 // if (log)
900 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\",
901 // size_t=%" PRIu64 ")",
902 // m_opaque_sp.get(),
903 // (int) data_len,
904 // (const char *) data,
905 // (uint64_t)data_len);
906 //
907 // if (m_opaque_sp)
908 // m_opaque_sp->DispatchInput ((const char *) data, data_len);
909}
910
911void SBDebugger::DispatchInputInterrupt() {
912 if (m_opaque_sp)
913 m_opaque_sp->DispatchInputInterrupt();
914}
915
916void SBDebugger::DispatchInputEndOfFile() {
917 if (m_opaque_sp)
918 m_opaque_sp->DispatchInputEndOfFile();
919}
920
921void SBDebugger::PushInputReader(SBInputReader &reader) {}
922
923void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
924 bool spawn_thread) {
925 if (m_opaque_sp) {
926 CommandInterpreterRunOptions options;
927
928 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(
929 auto_handle_events, spawn_thread, options);
930 }
931}
932
933void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
934 bool spawn_thread,
935 SBCommandInterpreterRunOptions &options,
936 int &num_errors, bool &quit_requested,
937 bool &stopped_for_crash)
938
939{
940 if (m_opaque_sp) {
941 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
942 interp.RunCommandInterpreter(auto_handle_events, spawn_thread,
943 options.ref());
944 num_errors = interp.GetNumErrors();
945 quit_requested = interp.GetQuitRequested();
946 stopped_for_crash = interp.GetStoppedForCrash();
947 }
948}
949
950SBError SBDebugger::RunREPL(lldb::LanguageType language,
951 const char *repl_options) {
952 SBError error;
953 if (m_opaque_sp)
954 error.ref() = m_opaque_sp->RunREPL(language, repl_options);
955 else
956 error.SetErrorString("invalid debugger");
957 return error;
958}
959
960void SBDebugger::reset(const DebuggerSP &debugger_sp) {
961 m_opaque_sp = debugger_sp;
962}
963
964Debugger *SBDebugger::get() const { return m_opaque_sp.get(); }
965
966Debugger &SBDebugger::ref() const {
967 assert(m_opaque_sp.get());
968 return *m_opaque_sp;
969}
970
971const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; }
972
973SBDebugger SBDebugger::FindDebuggerWithID(int id) {
974 // No need to lock, the debugger list is thread safe
975 SBDebugger sb_debugger;
976 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id);
977 if (debugger_sp)
978 sb_debugger.reset(debugger_sp);
979 return sb_debugger;
980}
981
982const char *SBDebugger::GetInstanceName() {
983 return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr);
984}
985
986SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
987 const char *debugger_instance_name) {
988 SBError sb_error;
989 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
990 ConstString(debugger_instance_name)));
Zachary Turner97206d52017-05-12 04:51:55 +0000991 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992 if (debugger_sp) {
993 ExecutionContext exe_ctx(
994 debugger_sp->GetCommandInterpreter().GetExecutionContext());
995 error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
996 var_name, value);
997 } else {
998 error.SetErrorStringWithFormat("invalid debugger instance name '%s'",
999 debugger_instance_name);
1000 }
1001 if (error.Fail())
1002 sb_error.SetError(error);
1003 return sb_error;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001004}
1005
Greg Clayton431ce672011-04-18 23:15:17 +00001006SBStringList
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007SBDebugger::GetInternalVariableValue(const char *var_name,
1008 const char *debugger_instance_name) {
1009 SBStringList ret_value;
1010 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
1011 ConstString(debugger_instance_name)));
Zachary Turner97206d52017-05-12 04:51:55 +00001012 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 if (debugger_sp) {
1014 ExecutionContext exe_ctx(
1015 debugger_sp->GetCommandInterpreter().GetExecutionContext());
1016 lldb::OptionValueSP value_sp(
1017 debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error));
1018 if (value_sp) {
1019 StreamString value_strm;
1020 value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
1021 const std::string &value_str = value_strm.GetString();
1022 if (!value_str.empty()) {
1023 StringList string_list;
1024 string_list.SplitIntoLines(value_str);
1025 return SBStringList(&string_list);
1026 }
1027 }
1028 }
1029 return SBStringList();
1030}
1031
1032uint32_t SBDebugger::GetTerminalWidth() const {
1033 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
1034}
1035
1036void SBDebugger::SetTerminalWidth(uint32_t term_width) {
1037 if (m_opaque_sp)
1038 m_opaque_sp->SetTerminalWidth(term_width);
1039}
1040
1041const char *SBDebugger::GetPrompt() const {
1042 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1043
1044 if (log)
1045 log->Printf("SBDebugger(%p)::GetPrompt () => \"%s\"",
1046 static_cast<void *>(m_opaque_sp.get()),
Zachary Turner514d8cd2016-09-23 18:06:53 +00001047 (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : ""));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048
Zachary Turner514d8cd2016-09-23 18:06:53 +00001049 return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
1050 : nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051}
1052
1053void SBDebugger::SetPrompt(const char *prompt) {
1054 if (m_opaque_sp)
Zachary Turner514d8cd2016-09-23 18:06:53 +00001055 m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056}
1057
1058ScriptLanguage SBDebugger::GetScriptLanguage() const {
1059 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
1060}
1061
1062void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) {
1063 if (m_opaque_sp) {
1064 m_opaque_sp->SetScriptLanguage(script_lang);
1065 }
1066}
1067
1068bool SBDebugger::SetUseExternalEditor(bool value) {
1069 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
1070}
1071
1072bool SBDebugger::GetUseExternalEditor() {
1073 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
1074}
1075
1076bool SBDebugger::SetUseColor(bool value) {
1077 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
1078}
1079
1080bool SBDebugger::GetUseColor() const {
1081 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
1082}
1083
1084bool SBDebugger::GetDescription(SBStream &description) {
1085 Stream &strm = description.ref();
1086
1087 if (m_opaque_sp) {
1088 const char *name = m_opaque_sp->GetInstanceName().AsCString();
1089 user_id_t id = m_opaque_sp->GetID();
1090 strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1091 } else
1092 strm.PutCString("No value");
1093
1094 return true;
1095}
1096
1097user_id_t SBDebugger::GetID() {
1098 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
1099}
1100
1101SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
1102 SBError sb_error;
1103 if (m_opaque_sp) {
1104 if (platform_name_cstr && platform_name_cstr[0]) {
1105 ConstString platform_name(platform_name_cstr);
1106 PlatformSP platform_sp(Platform::Find(platform_name));
1107
1108 if (platform_sp) {
1109 // Already have a platform with this name, just select it
1110 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
1111 } else {
1112 // We don't have a platform by this name yet, create one
1113 platform_sp = Platform::Create(platform_name, sb_error.ref());
1114 if (platform_sp) {
1115 // We created the platform, now append and select it
1116 bool make_selected = true;
1117 m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected);
Greg Clayton6920b522012-08-22 18:39:03 +00001118 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119 }
1120 } else {
1121 sb_error.ref().SetErrorString("invalid platform name");
Greg Clayton6920b522012-08-22 18:39:03 +00001122 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123 } else {
1124 sb_error.ref().SetErrorString("invalid debugger");
1125 }
1126 return sb_error;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001127}
1128
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) {
1130 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1131 if (m_opaque_sp) {
1132 PlatformSP platform_sp(
1133 m_opaque_sp->GetPlatformList().GetSelectedPlatform());
Greg Claytona7015092010-09-18 01:14:36 +00001134
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135 if (platform_sp) {
1136 if (log && sysroot)
1137 log->Printf("SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")", sysroot);
1138 platform_sp->SetSDKRootDirectory(ConstString(sysroot));
1139 return true;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001140 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141 }
1142 return false;
Greg Claytona7015092010-09-18 01:14:36 +00001143}
1144
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145bool SBDebugger::GetCloseInputOnEOF() const {
1146 return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false);
Jim Inghame40e4212010-08-30 19:44:40 +00001147}
1148
Kate Stoneb9c1b512016-09-06 20:57:50 +00001149void SBDebugger::SetCloseInputOnEOF(bool b) {
1150 if (m_opaque_sp)
1151 m_opaque_sp->SetCloseInputOnEOF(b);
Jim Inghame40e4212010-08-30 19:44:40 +00001152}
1153
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154SBTypeCategory SBDebugger::GetCategory(const char *category_name) {
1155 if (!category_name || *category_name == 0)
1156 return SBTypeCategory();
1157
1158 TypeCategoryImplSP category_sp;
1159
1160 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1161 category_sp, false))
1162 return SBTypeCategory(category_sp);
1163 else
1164 return SBTypeCategory();
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001165}
1166
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) {
1168 TypeCategoryImplSP category_sp;
1169 if (DataVisualization::Categories::GetCategory(lang_type, category_sp))
1170 return SBTypeCategory(category_sp);
1171 else
1172 return SBTypeCategory();
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001173}
1174
Kate Stoneb9c1b512016-09-06 20:57:50 +00001175SBTypeCategory SBDebugger::CreateCategory(const char *category_name) {
1176 if (!category_name || *category_name == 0)
1177 return SBTypeCategory();
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001178
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179 TypeCategoryImplSP category_sp;
1180
1181 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1182 category_sp, true))
1183 return SBTypeCategory(category_sp);
1184 else
1185 return SBTypeCategory();
Caroline Ticedde9cff2010-09-20 05:20:02 +00001186}
Caroline Ticeefed6132010-11-19 20:47:54 +00001187
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188bool SBDebugger::DeleteCategory(const char *category_name) {
1189 if (!category_name || *category_name == 0)
1190 return false;
1191
1192 return DataVisualization::Categories::Delete(ConstString(category_name));
Caroline Ticeefed6132010-11-19 20:47:54 +00001193}
Greg Clayton2289fa42011-04-30 01:09:13 +00001194
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195uint32_t SBDebugger::GetNumCategories() {
1196 return DataVisualization::Categories::GetCount();
Greg Clayton2289fa42011-04-30 01:09:13 +00001197}
1198
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) {
1200 return SBTypeCategory(
1201 DataVisualization::Categories::GetCategoryAtIndex(index));
1202}
1203
1204SBTypeCategory SBDebugger::GetDefaultCategory() {
1205 return GetCategory("default");
1206}
1207
1208SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) {
1209 SBTypeCategory default_category_sb = GetDefaultCategory();
1210 if (default_category_sb.GetEnabled())
1211 return default_category_sb.GetFormatForType(type_name);
1212 return SBTypeFormat();
1213}
1214
1215#ifndef LLDB_DISABLE_PYTHON
1216SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) {
1217 if (!type_name.IsValid())
1218 return SBTypeSummary();
1219 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1220}
1221#endif // LLDB_DISABLE_PYTHON
1222
1223SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) {
1224 if (!type_name.IsValid())
1225 return SBTypeFilter();
1226 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1227}
1228
1229#ifndef LLDB_DISABLE_PYTHON
1230SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) {
1231 if (!type_name.IsValid())
1232 return SBTypeSynthetic();
1233 return SBTypeSynthetic(
1234 DataVisualization::GetSyntheticForType(type_name.GetSP()));
1235}
1236#endif // LLDB_DISABLE_PYTHON
1237
Pavel Labath5e336902017-03-01 10:08:40 +00001238static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) {
1239 if (categories == nullptr)
1240 return {};
1241 size_t len = 0;
1242 while (categories[len] != nullptr)
1243 ++len;
1244 return llvm::makeArrayRef(categories, len);
1245}
1246
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247bool SBDebugger::EnableLog(const char *channel, const char **categories) {
1248 if (m_opaque_sp) {
1249 uint32_t log_options =
1250 LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
Pavel Labath775588c2017-03-15 09:06:58 +00001251 std::string error;
1252 llvm::raw_string_ostream error_stream(error);
Pavel Labath5e336902017-03-01 10:08:40 +00001253 return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
Pavel Labath775588c2017-03-15 09:06:58 +00001254 log_options, error_stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255 } else
Greg Claytonf3dd93c2011-06-17 03:31:01 +00001256 return false;
1257}
1258
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
1260 void *baton) {
1261 if (m_opaque_sp) {
1262 return m_opaque_sp->SetLoggingCallback(log_callback, baton);
1263 }
Jim Ingham4f02b222012-02-22 22:49:20 +00001264}