blob: 92ca723a92d00bb52f660b1feb51fd635f55c360 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBDebugger.cpp ------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Alex Langfordd17cd902018-05-25 20:28:16 +00009
10#include "SystemInitializerFull.h"
11
Eli Friedmanca93cc12010-06-09 07:37:52 +000012#include "lldb/API/SBDebugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
Greg Claytone0d378b2011-03-24 21:19:54 +000014#include "lldb/lldb-private.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Eli Friedmanca93cc12010-06-09 07:37:52 +000016#include "lldb/API/SBBroadcaster.h"
17#include "lldb/API/SBCommandInterpreter.h"
18#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton2289fa42011-04-30 01:09:13 +000019#include "lldb/API/SBError.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000020#include "lldb/API/SBEvent.h"
21#include "lldb/API/SBFrame.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000022#include "lldb/API/SBListener.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000023#include "lldb/API/SBProcess.h"
24#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000025#include "lldb/API/SBStream.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000026#include "lldb/API/SBStringList.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000027#include "lldb/API/SBStructuredData.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000028#include "lldb/API/SBTarget.h"
29#include "lldb/API/SBThread.h"
Enrico Granata061858c2012-02-15 02:34:21 +000030#include "lldb/API/SBTypeCategory.h"
Enrico Granata061858c2012-02-15 02:34:21 +000031#include "lldb/API/SBTypeFilter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000032#include "lldb/API/SBTypeFormat.h"
Enrico Granata061858c2012-02-15 02:34:21 +000033#include "lldb/API/SBTypeNameSpecifier.h"
34#include "lldb/API/SBTypeSummary.h"
35#include "lldb/API/SBTypeSynthetic.h"
36
Greg Clayton6eee5aa2010-10-11 01:05:37 +000037#include "lldb/Core/Debugger.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000038#include "lldb/Core/PluginManager.h"
Greg Clayton44d93782014-01-27 23:43:24 +000039#include "lldb/Core/StreamFile.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000040#include "lldb/Core/StructuredDataImpl.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000041#include "lldb/DataFormatters/DataVisualization.h"
Pavel Labathf1389e92018-02-19 15:06:28 +000042#include "lldb/Host/XML.h"
Zachary Turnere6e2bb32015-03-31 21:03:22 +000043#include "lldb/Initialization/SystemLifetimeManager.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000044#include "lldb/Interpreter/CommandInterpreter.h"
Pavel Labath47cbf4a2018-04-10 09:03:59 +000045#include "lldb/Interpreter/OptionArgParser.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000046#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000047#include "lldb/Target/Process.h"
48#include "lldb/Target/TargetList.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000049#include "lldb/Utility/Args.h"
Pavel Labathd821c992018-08-07 11:07:21 +000050#include "lldb/Utility/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051
Enrico Granatad717cc92015-10-20 04:50:09 +000052#include "llvm/ADT/STLExtras.h"
Zachary Turner7b2e5a32016-09-16 19:09:12 +000053#include "llvm/ADT/StringRef.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000054#include "llvm/Support/DynamicLibrary.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000055#include "llvm/Support/ManagedStatic.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000056
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057using namespace lldb;
58using namespace lldb_private;
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp,
61 const FileSpec &spec,
Zachary Turner97206d52017-05-12 04:51:55 +000062 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 llvm::sys::DynamicLibrary dynlib =
64 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
65 if (dynlib.isValid()) {
66 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger);
67
68 lldb::SBDebugger debugger_sb(debugger_sp);
69 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
70 // function.
71 // TODO: mangle this differently for your system - on OSX, the first
72 // underscore needs to be removed and the second one stays
73 LLDBCommandPluginInit init_func =
Stephane Sezer22701b22017-10-24 23:46:00 +000074 (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
76 if (init_func) {
77 if (init_func(debugger_sb))
78 return dynlib;
79 else
80 error.SetErrorString("plug-in refused to load "
81 "(lldb::PluginInitialize(lldb::SBDebugger) "
82 "returned false)");
83 } else {
84 error.SetErrorString("plug-in is missing the required initialization: "
85 "lldb::PluginInitialize(lldb::SBDebugger)");
Greg Clayton5fb8f792013-12-02 19:35:49 +000086 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 } else {
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +000088 if (FileSystem::Instance().Exists(spec))
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 error.SetErrorString("this file does not represent a loadable dylib");
Greg Clayton5fb8f792013-12-02 19:35:49 +000090 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 error.SetErrorString("no such file");
92 }
93 return llvm::sys::DynamicLibrary();
Greg Clayton5fb8f792013-12-02 19:35:49 +000094}
95
Zachary Turnere6e2bb32015-03-31 21:03:22 +000096static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098SBError SBInputReader::Initialize(
99 lldb::SBDebugger &sb_debugger,
100 unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction,
101 char const *, unsigned long),
102 void *, lldb::InputReaderGranularity, char const *, char const *, bool) {
103 return SBError();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000104}
105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106void SBInputReader::SetIsDone(bool) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108bool SBInputReader::IsActive() const { return false; }
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000109
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000110SBDebugger::SBDebugger() = default;
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp)
113 : m_opaque_sp(debugger_sp) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000116
117SBDebugger::~SBDebugger() = default;
118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) {
120 if (this != &rhs) {
121 m_opaque_sp = rhs.m_opaque_sp;
122 }
123 return *this;
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000124}
125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126void SBDebugger::Initialize() {
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000127 SBInitializerOptions options;
128 SBDebugger::Initialize(options);
129}
130
131lldb::SBError SBDebugger::Initialize(SBInitializerOptions &options) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 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
Jonas Devlieghere15eacd72018-12-03 17:28:29 +0000137 SBError error;
138 if (auto e = g_debugger_lifetime->Initialize(
139 llvm::make_unique<SystemInitializerFull>(), *options.m_opaque_up,
140 LoadPlugin)) {
141 error.SetError(Status(std::move(e)));
142 }
143 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146void SBDebugger::Terminate() { g_debugger_lifetime->Terminate(); }
147
148void SBDebugger::Clear() {
149 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
150
151 if (log)
152 log->Printf("SBDebugger(%p)::Clear ()",
153 static_cast<void *>(m_opaque_sp.get()));
154
155 if (m_opaque_sp)
156 m_opaque_sp->ClearIOHandlers();
157
158 m_opaque_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159}
160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161SBDebugger SBDebugger::Create() {
162 return SBDebugger::Create(false, nullptr, nullptr);
Greg Clayton48e42542010-07-30 20:12:55 +0000163}
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165SBDebugger SBDebugger::Create(bool source_init_files) {
166 return SBDebugger::Create(source_init_files, nullptr, nullptr);
Jim Ingham06942692011-08-13 00:22:20 +0000167}
168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169SBDebugger SBDebugger::Create(bool source_init_files,
170 lldb::LogOutputCallback callback, void *baton)
Jim Ingham228063c2012-02-21 02:23:08 +0000171
172{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 SBDebugger debugger;
Greg Claytoncb172b12014-05-19 20:42:14 +0000176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 // Currently we have issues if this function is called simultaneously on two
Adrian Prantl05097242018-04-30 16:49:04 +0000178 // different threads. The issues mainly revolve around the fact that the
179 // lldb_private::FormatManager uses global collections and having two threads
180 // parsing the .lldbinit files can cause mayhem. So to get around this for
181 // now we need to use a mutex to prevent bad things from happening.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 static std::recursive_mutex g_mutex;
183 std::lock_guard<std::recursive_mutex> guard(g_mutex);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 debugger.reset(Debugger::CreateInstance(callback, baton));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 if (log) {
188 SBStream sstr;
189 debugger.GetDescription(sstr);
190 log->Printf("SBDebugger::Create () => SBDebugger(%p): %s",
191 static_cast<void *>(debugger.m_opaque_sp.get()),
192 sstr.GetData());
193 }
194
195 SBCommandInterpreter interp = debugger.GetCommandInterpreter();
196 if (source_init_files) {
197 interp.get()->SkipLLDBInitFiles(false);
198 interp.get()->SkipAppInitFiles(false);
199 SBCommandReturnObject result;
200 interp.SourceInitFileInHomeDirectory(result);
201 } else {
202 interp.get()->SkipLLDBInitFiles(true);
203 interp.get()->SkipAppInitFiles(true);
204 }
205 return debugger;
Greg Clayton66111032010-06-23 01:19:29 +0000206}
207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208void SBDebugger::Destroy(SBDebugger &debugger) {
209 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 if (log) {
212 SBStream sstr;
213 debugger.GetDescription(sstr);
214 log->Printf("SBDebugger::Destroy () => SBDebugger(%p): %s",
215 static_cast<void *>(debugger.m_opaque_sp.get()),
216 sstr.GetData());
217 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000218
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 Debugger::Destroy(debugger.m_opaque_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 if (debugger.m_opaque_sp.get() != nullptr)
222 debugger.m_opaque_sp.reset();
Caroline Ticee02657b2011-01-22 01:02:07 +0000223}
224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225void SBDebugger::MemoryPressureDetected() {
Adrian Prantl05097242018-04-30 16:49:04 +0000226 // Since this function can be call asynchronously, we allow it to be non-
227 // mandatory. We have seen deadlocks with this function when called so we
228 // need to safeguard against this until we can determine what is causing the
229 // deadlocks.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
231
232 const bool mandatory = false;
233 if (log) {
234 log->Printf("SBDebugger::MemoryPressureDetected (), mandatory = %d",
235 mandatory);
236 }
237
238 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonf9322412011-12-15 04:38:41 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241bool SBDebugger::IsValid() const { return m_opaque_sp.get() != nullptr; }
242
243void SBDebugger::SetAsync(bool b) {
244 if (m_opaque_sp)
245 m_opaque_sp->SetAsyncExecution(b);
Greg Clayton66111032010-06-23 01:19:29 +0000246}
247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248bool SBDebugger::GetAsync() {
249 return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250}
251
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252void SBDebugger::SkipLLDBInitFiles(bool b) {
253 if (m_opaque_sp)
254 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b);
Jim Inghame64f0dc2011-09-13 23:25:31 +0000255}
256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257void SBDebugger::SkipAppInitFiles(bool b) {
258 if (m_opaque_sp)
259 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b);
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000260}
261
Adrian Prantl05097242018-04-30 16:49:04 +0000262// Shouldn't really be settable after initialization as this could cause lots
263// of problems; don't want users trying to switch modes in the middle of a
264// debugging session.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) {
266 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 if (log)
269 log->Printf(
270 "SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)",
271 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
272 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 if (m_opaque_sp)
275 m_opaque_sp->SetInputFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276}
277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) {
279 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000280
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 if (log)
282 log->Printf(
283 "SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)",
284 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
285 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 if (m_opaque_sp)
288 m_opaque_sp->SetOutputFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289}
290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) {
292 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000293
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 if (log)
295 log->Printf(
296 "SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)",
297 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
298 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000299
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 if (m_opaque_sp)
301 m_opaque_sp->SetErrorFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302}
303
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304FILE *SBDebugger::GetInputFileHandle() {
305 if (m_opaque_sp) {
306 StreamFileSP stream_file_sp(m_opaque_sp->GetInputFile());
307 if (stream_file_sp)
308 return stream_file_sp->GetFile().GetStream();
309 }
310 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311}
312
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313FILE *SBDebugger::GetOutputFileHandle() {
314 if (m_opaque_sp) {
315 StreamFileSP stream_file_sp(m_opaque_sp->GetOutputFile());
316 if (stream_file_sp)
317 return stream_file_sp->GetFile().GetStream();
318 }
319 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320}
321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322FILE *SBDebugger::GetErrorFileHandle() {
323 if (m_opaque_sp) {
324 StreamFileSP stream_file_sp(m_opaque_sp->GetErrorFile());
325 if (stream_file_sp)
326 return stream_file_sp->GetFile().GetStream();
327 }
328 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329}
330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331void SBDebugger::SaveInputTerminalState() {
332 if (m_opaque_sp)
333 m_opaque_sp->SaveInputTerminalState();
Jim Inghamc5917d92012-11-30 20:23:19 +0000334}
335
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336void SBDebugger::RestoreInputTerminalState() {
337 if (m_opaque_sp)
338 m_opaque_sp->RestoreInputTerminalState();
Jim Inghamc5917d92012-11-30 20:23:19 +0000339}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340SBCommandInterpreter SBDebugger::GetCommandInterpreter() {
341 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 SBCommandInterpreter sb_interpreter;
344 if (m_opaque_sp)
345 sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 if (log)
348 log->Printf(
349 "SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)",
350 static_cast<void *>(m_opaque_sp.get()),
351 static_cast<void *>(sb_interpreter.get()));
Caroline Tice750cd172010-10-26 23:49:36 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 return sb_interpreter;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354}
355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356void SBDebugger::HandleCommand(const char *command) {
357 if (m_opaque_sp) {
358 TargetSP target_sp(m_opaque_sp->GetSelectedTarget());
359 std::unique_lock<std::recursive_mutex> lock;
360 if (target_sp)
361 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 SBCommandInterpreter sb_interpreter(GetCommandInterpreter());
364 SBCommandReturnObject result;
Greg Clayton66111032010-06-23 01:19:29 +0000365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 sb_interpreter.HandleCommand(command, result, false);
Greg Clayton66111032010-06-23 01:19:29 +0000367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 if (GetErrorFileHandle() != nullptr)
369 result.PutError(GetErrorFileHandle());
370 if (GetOutputFileHandle() != nullptr)
371 result.PutOutput(GetOutputFileHandle());
Greg Clayton66111032010-06-23 01:19:29 +0000372
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373 if (!m_opaque_sp->GetAsyncExecution()) {
374 SBProcess process(GetCommandInterpreter().GetProcess());
375 ProcessSP process_sp(process.GetSP());
376 if (process_sp) {
377 EventSP event_sp;
378 ListenerSP lldb_listener_sp = m_opaque_sp->GetListener();
Pavel Labathd35031e12016-11-30 10:41:42 +0000379 while (lldb_listener_sp->GetEventForBroadcaster(
380 process_sp.get(), event_sp, std::chrono::seconds(0))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 SBEvent event(event_sp);
382 HandleProcessEvent(process, event, GetOutputFileHandle(),
383 GetErrorFileHandle());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388}
389
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390SBListener SBDebugger::GetListener() {
391 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 SBListener sb_listener;
394 if (m_opaque_sp)
395 sb_listener.reset(m_opaque_sp->GetListener());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 if (log)
398 log->Printf("SBDebugger(%p)::GetListener () => SBListener(%p)",
399 static_cast<void *>(m_opaque_sp.get()),
400 static_cast<void *>(sb_listener.get()));
Caroline Tice750cd172010-10-26 23:49:36 +0000401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 return sb_listener;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405void SBDebugger::HandleProcessEvent(const SBProcess &process,
406 const SBEvent &event, FILE *out,
407 FILE *err) {
408 if (!process.IsValid())
409 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 TargetSP target_sp(process.GetTarget().GetSP());
412 if (!target_sp)
413 return;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000414
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 const uint32_t event_type = event.GetType();
416 char stdio_buffer[1024];
417 size_t len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 if (event_type &
422 (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) {
423 // Drain stdout when we stop just in case we have any bytes
424 while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0)
425 if (out != nullptr)
426 ::fwrite(stdio_buffer, 1, len, out);
427 }
Todd Fiala75930012016-08-19 04:21:48 +0000428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 if (event_type &
430 (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) {
431 // Drain stderr when we stop just in case we have any bytes
432 while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0)
433 if (err != nullptr)
434 ::fwrite(stdio_buffer, 1, len, err);
435 }
436
437 if (event_type & Process::eBroadcastBitStateChanged) {
438 StateType event_state = SBProcess::GetStateFromEvent(event);
439
440 if (event_state == eStateInvalid)
441 return;
442
443 bool is_stopped = StateIsStoppedState(event_state);
444 if (!is_stopped)
445 process.ReportEventState(event, out);
446 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447}
448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449SBSourceManager SBDebugger::GetSourceManager() {
450 SBSourceManager sb_source_manager(*this);
451 return sb_source_manager;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452}
453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) {
455 if (arch_name && arch_name_len) {
456 ArchSpec default_arch = Target::GetDefaultArchitecture();
Caroline Ticedaccaa92010-09-20 20:44:43 +0000457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 if (default_arch.IsValid()) {
459 const std::string &triple_str = default_arch.GetTriple().str();
460 if (!triple_str.empty())
461 ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str());
462 else
463 ::snprintf(arch_name, arch_name_len, "%s",
464 default_arch.GetArchitectureName());
465 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 }
468 if (arch_name && arch_name_len)
469 arch_name[0] = '\0';
470 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471}
472
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473bool SBDebugger::SetDefaultArchitecture(const char *arch_name) {
474 if (arch_name) {
475 ArchSpec arch(arch_name);
476 if (arch.IsValid()) {
477 Target::SetDefaultArchitecture(arch);
478 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480 }
481 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482}
483
484ScriptLanguage
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485SBDebugger::GetScriptingLanguage(const char *script_language_name) {
Zachary Turner7b2e5a32016-09-16 19:09:12 +0000486 if (!script_language_name) return eScriptLanguageDefault;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000487 return OptionArgParser::ToScriptLanguage(
488 llvm::StringRef(script_language_name), eScriptLanguageDefault, nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491const char *SBDebugger::GetVersionString() {
492 return lldb_private::GetVersion();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493}
494
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495const char *SBDebugger::StateAsCString(StateType state) {
496 return lldb_private::StateAsCString(state);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497}
498
Pavel Labathf1389e92018-02-19 15:06:28 +0000499static void AddBoolConfigEntry(StructuredData::Dictionary &dict,
500 llvm::StringRef name, bool value,
501 llvm::StringRef description) {
502 auto entry_up = llvm::make_unique<StructuredData::Dictionary>();
503 entry_up->AddBooleanItem("value", value);
504 entry_up->AddStringItem("description", description);
505 dict.AddItem(name, std::move(entry_up));
506}
507
Pavel Labathabc0c6a2018-06-28 14:23:04 +0000508static void AddLLVMTargets(StructuredData::Dictionary &dict) {
509 auto array_up = llvm::make_unique<StructuredData::Array>();
510#define LLVM_TARGET(target) \
511 array_up->AddItem(llvm::make_unique<StructuredData::String>(#target));
512#include "llvm/Config/Targets.def"
513 auto entry_up = llvm::make_unique<StructuredData::Dictionary>();
514 entry_up->AddItem("value", std::move(array_up));
515 entry_up->AddStringItem("description", "A list of configured LLVM targets.");
516 dict.AddItem("targets", std::move(entry_up));
517}
518
Pavel Labathf1389e92018-02-19 15:06:28 +0000519SBStructuredData SBDebugger::GetBuildConfiguration() {
520 auto config_up = llvm::make_unique<StructuredData::Dictionary>();
521 AddBoolConfigEntry(
522 *config_up, "xml", XMLDocument::XMLEnabled(),
523 "A boolean value that indicates if XML support is enabled in LLDB");
Pavel Labathabc0c6a2018-06-28 14:23:04 +0000524 AddLLVMTargets(*config_up);
Pavel Labathf1389e92018-02-19 15:06:28 +0000525
526 SBStructuredData data;
527 data.m_impl_up->SetObjectSP(std::move(config_up));
528 return data;
529}
530
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531bool SBDebugger::StateIsRunningState(StateType state) {
532 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000533
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534 const bool result = lldb_private::StateIsRunningState(state);
535 if (log)
536 log->Printf("SBDebugger::StateIsRunningState (state=%s) => %i",
537 StateAsCString(state), result);
Greg Clayton48381312010-10-30 04:51:46 +0000538
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540}
541
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542bool SBDebugger::StateIsStoppedState(StateType state) {
543 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 const bool result = lldb_private::StateIsStoppedState(state, false);
546 if (log)
547 log->Printf("SBDebugger::StateIsStoppedState (state=%s) => %i",
548 StateAsCString(state), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000549
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551}
552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553lldb::SBTarget SBDebugger::CreateTarget(const char *filename,
554 const char *target_triple,
555 const char *platform_name,
556 bool add_dependent_modules,
557 lldb::SBError &sb_error) {
558 SBTarget sb_target;
559 TargetSP target_sp;
560 if (m_opaque_sp) {
561 sb_error.Clear();
562 OptionGroupPlatform platform_options(false);
563 platform_options.SetPlatformName(platform_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000564
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000566 *m_opaque_sp, filename, target_triple,
567 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568 &platform_options, target_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000569
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 if (sb_error.Success())
571 sb_target.SetSP(target_sp);
572 } else {
573 sb_error.SetErrorString("invalid debugger");
574 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000575
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
577 if (log)
578 log->Printf("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, "
579 "platform_name=%s, add_dependent_modules=%u, error=%s) => "
580 "SBTarget(%p)",
581 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
582 platform_name, add_dependent_modules, sb_error.GetCString(),
583 static_cast<void *>(target_sp.get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000584
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 return sb_target;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000586}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587
588SBTarget
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename,
590 const char *target_triple) {
591 SBTarget sb_target;
592 TargetSP target_sp;
593 if (m_opaque_sp) {
594 const bool add_dependent_modules = true;
Zachary Turner97206d52017-05-12 04:51:55 +0000595 Status error(m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000596 *m_opaque_sp, filename, target_triple,
597 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 target_sp));
599 sb_target.SetSP(target_sp);
600 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
603 if (log)
604 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple "
605 "(filename=\"%s\", triple=%s) => SBTarget(%p)",
606 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
607 static_cast<void *>(target_sp.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000608
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609 return sb_target;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610}
611
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
613 const char *arch_cstr) {
614 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000615
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 SBTarget sb_target;
617 TargetSP target_sp;
618 if (m_opaque_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000619 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 const bool add_dependent_modules = true;
621
622 error = m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000623 *m_opaque_sp, filename, arch_cstr,
624 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 target_sp);
626
627 if (error.Success()) {
628 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
629 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000630 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 }
632
633 if (log)
634 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
635 "arch=%s) => SBTarget(%p)",
636 static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr,
637 static_cast<void *>(target_sp.get()));
638
639 return sb_target;
640}
641
642SBTarget SBDebugger::CreateTarget(const char *filename) {
643 SBTarget sb_target;
644 TargetSP target_sp;
645 if (m_opaque_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000646 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 const bool add_dependent_modules = true;
648 error = m_opaque_sp->GetTargetList().CreateTarget(
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000649 *m_opaque_sp, filename, "",
650 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
651 target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652
653 if (error.Success()) {
654 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
655 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000656 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657 }
658 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
659 if (log)
660 log->Printf(
661 "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
662 static_cast<void *>(m_opaque_sp.get()), filename,
663 static_cast<void *>(target_sp.get()));
664 return sb_target;
665}
666
Jim Inghamb842f2e2017-09-14 20:22:49 +0000667SBTarget SBDebugger::GetDummyTarget() {
668 SBTarget sb_target;
669 if (m_opaque_sp) {
670 sb_target.SetSP(m_opaque_sp->GetDummyTarget()->shared_from_this());
671 }
672 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
673 if (log)
674 log->Printf(
675 "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)",
676 static_cast<void *>(m_opaque_sp.get()),
677 static_cast<void *>(sb_target.GetSP().get()));
678 return sb_target;
679}
680
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681bool SBDebugger::DeleteTarget(lldb::SBTarget &target) {
682 bool result = false;
683 if (m_opaque_sp) {
684 TargetSP target_sp(target.GetSP());
685 if (target_sp) {
686 // No need to lock, the target list is thread safe
687 result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp);
688 target_sp->Destroy();
689 target.Clear();
690 const bool mandatory = true;
691 ModuleList::RemoveOrphanSharedModules(mandatory);
692 }
693 }
694
695 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
696 if (log)
697 log->Printf("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
698 static_cast<void *>(m_opaque_sp.get()),
699 static_cast<void *>(target.m_opaque_sp.get()), result);
700
701 return result;
702}
703
704SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) {
705 SBTarget sb_target;
706 if (m_opaque_sp) {
707 // No need to lock, the target list is thread safe
708 sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx));
709 }
710 return sb_target;
711}
712
713uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) {
714
715 lldb::TargetSP target_sp = target.GetSP();
716 if (!target_sp)
717 return UINT32_MAX;
718
719 if (!m_opaque_sp)
720 return UINT32_MAX;
721
722 return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP());
723}
724
725SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) {
726 SBTarget sb_target;
727 if (m_opaque_sp) {
728 // No need to lock, the target list is thread safe
729 sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid));
730 }
731 return sb_target;
732}
733
734SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename,
735 const char *arch_name) {
736 SBTarget sb_target;
737 if (m_opaque_sp && filename && filename[0]) {
738 // No need to lock, the target list is thread safe
Pavel Labath7263f1b2017-10-31 10:56:03 +0000739 ArchSpec arch = Platform::GetAugmentedArchSpec(
740 m_opaque_sp->GetPlatformList().GetSelectedPlatform().get(), arch_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 TargetSP target_sp(
742 m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000743 FileSpec(filename), arch_name ? &arch : nullptr));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 sb_target.SetSP(target_sp);
745 }
746 return sb_target;
747}
748
749SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) {
750 SBTarget sb_target;
751 if (m_opaque_sp) {
752 // No need to lock, the target list is thread safe
753 sb_target.SetSP(
754 m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get()));
755 }
756 return sb_target;
757}
758
759uint32_t SBDebugger::GetNumTargets() {
760 if (m_opaque_sp) {
761 // No need to lock, the target list is thread safe
762 return m_opaque_sp->GetTargetList().GetNumTargets();
763 }
764 return 0;
765}
766
767SBTarget SBDebugger::GetSelectedTarget() {
768 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
769
770 SBTarget sb_target;
771 TargetSP target_sp;
772 if (m_opaque_sp) {
773 // No need to lock, the target list is thread safe
774 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget();
775 sb_target.SetSP(target_sp);
776 }
777
778 if (log) {
779 SBStream sstr;
780 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
781 log->Printf("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
782 static_cast<void *>(m_opaque_sp.get()),
783 static_cast<void *>(target_sp.get()), sstr.GetData());
784 }
785
786 return sb_target;
787}
788
789void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
790 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
791
792 TargetSP target_sp(sb_target.GetSP());
793 if (m_opaque_sp) {
794 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
795 }
796 if (log) {
797 SBStream sstr;
798 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
799 log->Printf("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
800 static_cast<void *>(m_opaque_sp.get()),
801 static_cast<void *>(target_sp.get()), sstr.GetData());
802 }
803}
804
805SBPlatform SBDebugger::GetSelectedPlatform() {
806 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
807
808 SBPlatform sb_platform;
809 DebuggerSP debugger_sp(m_opaque_sp);
810 if (debugger_sp) {
811 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
812 }
813 if (log)
814 log->Printf("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
815 static_cast<void *>(m_opaque_sp.get()),
816 static_cast<void *>(sb_platform.GetSP().get()),
817 sb_platform.GetName());
818 return sb_platform;
819}
820
821void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) {
822 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
823
824 DebuggerSP debugger_sp(m_opaque_sp);
825 if (debugger_sp) {
826 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
827 }
828
829 if (log)
830 log->Printf("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
831 static_cast<void *>(m_opaque_sp.get()),
832 static_cast<void *>(sb_platform.GetSP().get()),
833 sb_platform.GetName());
834}
835
Vadim Macagonc10e34d2017-08-09 09:20:40 +0000836uint32_t SBDebugger::GetNumPlatforms() {
837 if (m_opaque_sp) {
838 // No need to lock, the platform list is thread safe
839 return m_opaque_sp->GetPlatformList().GetSize();
840 }
841 return 0;
842}
843
844SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) {
845 SBPlatform sb_platform;
846 if (m_opaque_sp) {
847 // No need to lock, the platform list is thread safe
848 sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx));
849 }
850 return sb_platform;
851}
852
853uint32_t SBDebugger::GetNumAvailablePlatforms() {
854 uint32_t idx = 0;
855 while (true) {
856 if (!PluginManager::GetPlatformPluginNameAtIndex(idx)) {
857 break;
858 }
859 ++idx;
860 }
861 // +1 for the host platform, which should always appear first in the list.
862 return idx + 1;
863}
864
865SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) {
866 SBStructuredData data;
867 auto platform_dict = llvm::make_unique<StructuredData::Dictionary>();
868 llvm::StringRef name_str("name"), desc_str("description");
869
870 if (idx == 0) {
871 PlatformSP host_platform_sp(Platform::GetHostPlatform());
872 platform_dict->AddStringItem(
873 name_str, host_platform_sp->GetPluginName().GetStringRef());
874 platform_dict->AddStringItem(
875 desc_str, llvm::StringRef(host_platform_sp->GetDescription()));
876 } else if (idx > 0) {
877 const char *plugin_name =
878 PluginManager::GetPlatformPluginNameAtIndex(idx - 1);
879 if (!plugin_name) {
880 return data;
881 }
882 platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name));
883
884 const char *plugin_desc =
885 PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1);
886 if (!plugin_desc) {
887 return data;
888 }
889 platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc));
890 }
891
892 data.m_impl_up->SetObjectSP(
893 StructuredData::ObjectSP(platform_dict.release()));
894 return data;
895}
896
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) {
898 DispatchInput(data, data_len);
899}
900
901void SBDebugger::DispatchInput(const void *data, size_t data_len) {
902 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
903 //
904 // if (log)
905 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\",
906 // size_t=%" PRIu64 ")",
907 // m_opaque_sp.get(),
908 // (int) data_len,
909 // (const char *) data,
910 // (uint64_t)data_len);
911 //
912 // if (m_opaque_sp)
913 // m_opaque_sp->DispatchInput ((const char *) data, data_len);
914}
915
916void SBDebugger::DispatchInputInterrupt() {
917 if (m_opaque_sp)
918 m_opaque_sp->DispatchInputInterrupt();
919}
920
921void SBDebugger::DispatchInputEndOfFile() {
922 if (m_opaque_sp)
923 m_opaque_sp->DispatchInputEndOfFile();
924}
925
926void SBDebugger::PushInputReader(SBInputReader &reader) {}
927
928void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
929 bool spawn_thread) {
930 if (m_opaque_sp) {
931 CommandInterpreterRunOptions options;
932
933 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(
934 auto_handle_events, spawn_thread, options);
935 }
936}
937
938void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
939 bool spawn_thread,
940 SBCommandInterpreterRunOptions &options,
941 int &num_errors, bool &quit_requested,
942 bool &stopped_for_crash)
943
944{
945 if (m_opaque_sp) {
946 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
947 interp.RunCommandInterpreter(auto_handle_events, spawn_thread,
948 options.ref());
949 num_errors = interp.GetNumErrors();
950 quit_requested = interp.GetQuitRequested();
951 stopped_for_crash = interp.GetStoppedForCrash();
952 }
953}
954
955SBError SBDebugger::RunREPL(lldb::LanguageType language,
956 const char *repl_options) {
957 SBError error;
958 if (m_opaque_sp)
959 error.ref() = m_opaque_sp->RunREPL(language, repl_options);
960 else
961 error.SetErrorString("invalid debugger");
962 return error;
963}
964
965void SBDebugger::reset(const DebuggerSP &debugger_sp) {
966 m_opaque_sp = debugger_sp;
967}
968
969Debugger *SBDebugger::get() const { return m_opaque_sp.get(); }
970
971Debugger &SBDebugger::ref() const {
972 assert(m_opaque_sp.get());
973 return *m_opaque_sp;
974}
975
976const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; }
977
978SBDebugger SBDebugger::FindDebuggerWithID(int id) {
979 // No need to lock, the debugger list is thread safe
980 SBDebugger sb_debugger;
981 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id);
982 if (debugger_sp)
983 sb_debugger.reset(debugger_sp);
984 return sb_debugger;
985}
986
987const char *SBDebugger::GetInstanceName() {
988 return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr);
989}
990
991SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
992 const char *debugger_instance_name) {
993 SBError sb_error;
994 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
995 ConstString(debugger_instance_name)));
Zachary Turner97206d52017-05-12 04:51:55 +0000996 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997 if (debugger_sp) {
998 ExecutionContext exe_ctx(
999 debugger_sp->GetCommandInterpreter().GetExecutionContext());
1000 error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
1001 var_name, value);
1002 } else {
1003 error.SetErrorStringWithFormat("invalid debugger instance name '%s'",
1004 debugger_instance_name);
1005 }
1006 if (error.Fail())
1007 sb_error.SetError(error);
1008 return sb_error;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001009}
1010
Greg Clayton431ce672011-04-18 23:15:17 +00001011SBStringList
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012SBDebugger::GetInternalVariableValue(const char *var_name,
1013 const char *debugger_instance_name) {
1014 SBStringList ret_value;
1015 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
1016 ConstString(debugger_instance_name)));
Zachary Turner97206d52017-05-12 04:51:55 +00001017 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018 if (debugger_sp) {
1019 ExecutionContext exe_ctx(
1020 debugger_sp->GetCommandInterpreter().GetExecutionContext());
1021 lldb::OptionValueSP value_sp(
1022 debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error));
1023 if (value_sp) {
1024 StreamString value_strm;
1025 value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
1026 const std::string &value_str = value_strm.GetString();
1027 if (!value_str.empty()) {
1028 StringList string_list;
1029 string_list.SplitIntoLines(value_str);
1030 return SBStringList(&string_list);
1031 }
1032 }
1033 }
1034 return SBStringList();
1035}
1036
1037uint32_t SBDebugger::GetTerminalWidth() const {
1038 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
1039}
1040
1041void SBDebugger::SetTerminalWidth(uint32_t term_width) {
1042 if (m_opaque_sp)
1043 m_opaque_sp->SetTerminalWidth(term_width);
1044}
1045
1046const char *SBDebugger::GetPrompt() const {
1047 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1048
1049 if (log)
1050 log->Printf("SBDebugger(%p)::GetPrompt () => \"%s\"",
1051 static_cast<void *>(m_opaque_sp.get()),
Zachary Turner514d8cd2016-09-23 18:06:53 +00001052 (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : ""));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053
Zachary Turner514d8cd2016-09-23 18:06:53 +00001054 return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
1055 : nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056}
1057
1058void SBDebugger::SetPrompt(const char *prompt) {
1059 if (m_opaque_sp)
Zachary Turner514d8cd2016-09-23 18:06:53 +00001060 m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061}
1062
Jonas Devlieghere9e046f02018-11-13 19:18:16 +00001063const char *SBDebugger::GetReproducerPath() const {
1064 return (m_opaque_sp
1065 ? ConstString(m_opaque_sp->GetReproducerPath()).GetCString()
1066 : nullptr);
1067}
1068
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069ScriptLanguage SBDebugger::GetScriptLanguage() const {
1070 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
1071}
1072
1073void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) {
1074 if (m_opaque_sp) {
1075 m_opaque_sp->SetScriptLanguage(script_lang);
1076 }
1077}
1078
1079bool SBDebugger::SetUseExternalEditor(bool value) {
1080 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
1081}
1082
1083bool SBDebugger::GetUseExternalEditor() {
1084 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
1085}
1086
1087bool SBDebugger::SetUseColor(bool value) {
1088 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
1089}
1090
1091bool SBDebugger::GetUseColor() const {
1092 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
1093}
1094
1095bool SBDebugger::GetDescription(SBStream &description) {
1096 Stream &strm = description.ref();
1097
1098 if (m_opaque_sp) {
1099 const char *name = m_opaque_sp->GetInstanceName().AsCString();
1100 user_id_t id = m_opaque_sp->GetID();
1101 strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1102 } else
1103 strm.PutCString("No value");
1104
1105 return true;
1106}
1107
1108user_id_t SBDebugger::GetID() {
1109 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
1110}
1111
1112SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
1113 SBError sb_error;
1114 if (m_opaque_sp) {
1115 if (platform_name_cstr && platform_name_cstr[0]) {
1116 ConstString platform_name(platform_name_cstr);
1117 PlatformSP platform_sp(Platform::Find(platform_name));
1118
1119 if (platform_sp) {
1120 // Already have a platform with this name, just select it
1121 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
1122 } else {
1123 // We don't have a platform by this name yet, create one
1124 platform_sp = Platform::Create(platform_name, sb_error.ref());
1125 if (platform_sp) {
1126 // We created the platform, now append and select it
1127 bool make_selected = true;
1128 m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected);
Greg Clayton6920b522012-08-22 18:39:03 +00001129 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 }
1131 } else {
1132 sb_error.ref().SetErrorString("invalid platform name");
Greg Clayton6920b522012-08-22 18:39:03 +00001133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 } else {
1135 sb_error.ref().SetErrorString("invalid debugger");
1136 }
1137 return sb_error;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001138}
1139
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) {
1141 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1142 if (m_opaque_sp) {
1143 PlatformSP platform_sp(
1144 m_opaque_sp->GetPlatformList().GetSelectedPlatform());
Greg Claytona7015092010-09-18 01:14:36 +00001145
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146 if (platform_sp) {
1147 if (log && sysroot)
1148 log->Printf("SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")", sysroot);
1149 platform_sp->SetSDKRootDirectory(ConstString(sysroot));
1150 return true;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001151 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001152 }
1153 return false;
Greg Claytona7015092010-09-18 01:14:36 +00001154}
1155
Kate Stoneb9c1b512016-09-06 20:57:50 +00001156bool SBDebugger::GetCloseInputOnEOF() const {
1157 return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false);
Jim Inghame40e4212010-08-30 19:44:40 +00001158}
1159
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160void SBDebugger::SetCloseInputOnEOF(bool b) {
1161 if (m_opaque_sp)
1162 m_opaque_sp->SetCloseInputOnEOF(b);
Jim Inghame40e4212010-08-30 19:44:40 +00001163}
1164
Kate Stoneb9c1b512016-09-06 20:57:50 +00001165SBTypeCategory SBDebugger::GetCategory(const char *category_name) {
1166 if (!category_name || *category_name == 0)
1167 return SBTypeCategory();
1168
1169 TypeCategoryImplSP category_sp;
1170
1171 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1172 category_sp, false))
1173 return SBTypeCategory(category_sp);
1174 else
1175 return SBTypeCategory();
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001176}
1177
Kate Stoneb9c1b512016-09-06 20:57:50 +00001178SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) {
1179 TypeCategoryImplSP category_sp;
1180 if (DataVisualization::Categories::GetCategory(lang_type, category_sp))
1181 return SBTypeCategory(category_sp);
1182 else
1183 return SBTypeCategory();
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001184}
1185
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186SBTypeCategory SBDebugger::CreateCategory(const char *category_name) {
1187 if (!category_name || *category_name == 0)
1188 return SBTypeCategory();
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001189
Kate Stoneb9c1b512016-09-06 20:57:50 +00001190 TypeCategoryImplSP category_sp;
1191
1192 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1193 category_sp, true))
1194 return SBTypeCategory(category_sp);
1195 else
1196 return SBTypeCategory();
Caroline Ticedde9cff2010-09-20 05:20:02 +00001197}
Caroline Ticeefed6132010-11-19 20:47:54 +00001198
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199bool SBDebugger::DeleteCategory(const char *category_name) {
1200 if (!category_name || *category_name == 0)
1201 return false;
1202
1203 return DataVisualization::Categories::Delete(ConstString(category_name));
Caroline Ticeefed6132010-11-19 20:47:54 +00001204}
Greg Clayton2289fa42011-04-30 01:09:13 +00001205
Kate Stoneb9c1b512016-09-06 20:57:50 +00001206uint32_t SBDebugger::GetNumCategories() {
1207 return DataVisualization::Categories::GetCount();
Greg Clayton2289fa42011-04-30 01:09:13 +00001208}
1209
Kate Stoneb9c1b512016-09-06 20:57:50 +00001210SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) {
1211 return SBTypeCategory(
1212 DataVisualization::Categories::GetCategoryAtIndex(index));
1213}
1214
1215SBTypeCategory SBDebugger::GetDefaultCategory() {
1216 return GetCategory("default");
1217}
1218
1219SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) {
1220 SBTypeCategory default_category_sb = GetDefaultCategory();
1221 if (default_category_sb.GetEnabled())
1222 return default_category_sb.GetFormatForType(type_name);
1223 return SBTypeFormat();
1224}
1225
1226#ifndef LLDB_DISABLE_PYTHON
1227SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) {
1228 if (!type_name.IsValid())
1229 return SBTypeSummary();
1230 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1231}
1232#endif // LLDB_DISABLE_PYTHON
1233
1234SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) {
1235 if (!type_name.IsValid())
1236 return SBTypeFilter();
1237 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1238}
1239
1240#ifndef LLDB_DISABLE_PYTHON
1241SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) {
1242 if (!type_name.IsValid())
1243 return SBTypeSynthetic();
1244 return SBTypeSynthetic(
1245 DataVisualization::GetSyntheticForType(type_name.GetSP()));
1246}
1247#endif // LLDB_DISABLE_PYTHON
1248
Pavel Labath5e336902017-03-01 10:08:40 +00001249static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) {
1250 if (categories == nullptr)
1251 return {};
1252 size_t len = 0;
1253 while (categories[len] != nullptr)
1254 ++len;
1255 return llvm::makeArrayRef(categories, len);
1256}
1257
Kate Stoneb9c1b512016-09-06 20:57:50 +00001258bool SBDebugger::EnableLog(const char *channel, const char **categories) {
1259 if (m_opaque_sp) {
1260 uint32_t log_options =
1261 LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
Pavel Labath775588c2017-03-15 09:06:58 +00001262 std::string error;
1263 llvm::raw_string_ostream error_stream(error);
Pavel Labath5e336902017-03-01 10:08:40 +00001264 return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
Pavel Labath775588c2017-03-15 09:06:58 +00001265 log_options, error_stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001266 } else
Greg Claytonf3dd93c2011-06-17 03:31:01 +00001267 return false;
1268}
1269
Kate Stoneb9c1b512016-09-06 20:57:50 +00001270void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
1271 void *baton) {
1272 if (m_opaque_sp) {
1273 return m_opaque_sp->SetLoggingCallback(log_callback, baton);
1274 }
Jim Ingham4f02b222012-02-22 22:49:20 +00001275}