blob: cf6a27a049b154554f81cd962dbd2fb9371c19a5 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBDebugger.cpp ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eli Friedmanca93cc12010-06-09 07:37:52 +000014#include "lldb/API/SBDebugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Greg Claytone0d378b2011-03-24 21:19:54 +000016#include "lldb/lldb-private.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Eli Friedmanca93cc12010-06-09 07:37:52 +000018#include "lldb/API/SBBroadcaster.h"
19#include "lldb/API/SBCommandInterpreter.h"
20#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton2289fa42011-04-30 01:09:13 +000021#include "lldb/API/SBError.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000022#include "lldb/API/SBEvent.h"
23#include "lldb/API/SBFrame.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "lldb/API/SBListener.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000025#include "lldb/API/SBProcess.h"
26#include "lldb/API/SBSourceManager.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000027#include "lldb/API/SBStream.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000028#include "lldb/API/SBStringList.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000029#include "lldb/API/SBStructuredData.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000030#include "lldb/API/SBTarget.h"
31#include "lldb/API/SBThread.h"
Enrico Granata061858c2012-02-15 02:34:21 +000032#include "lldb/API/SBTypeCategory.h"
Enrico Granata061858c2012-02-15 02:34:21 +000033#include "lldb/API/SBTypeFilter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000034#include "lldb/API/SBTypeFormat.h"
Enrico Granata061858c2012-02-15 02:34:21 +000035#include "lldb/API/SBTypeNameSpecifier.h"
36#include "lldb/API/SBTypeSummary.h"
37#include "lldb/API/SBTypeSynthetic.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000038#include "lldb/API/SystemInitializerFull.h"
Enrico Granata061858c2012-02-15 02:34:21 +000039
Greg Clayton6eee5aa2010-10-11 01:05:37 +000040#include "lldb/Core/Debugger.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000041#include "lldb/Core/PluginManager.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000042#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000043#include "lldb/Core/StreamFile.h"
Vadim Macagonc10e34d2017-08-09 09:20:40 +000044#include "lldb/Core/StructuredDataImpl.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000045#include "lldb/DataFormatters/DataVisualization.h"
Zachary Turnere6e2bb32015-03-31 21:03:22 +000046#include "lldb/Initialization/SystemLifetimeManager.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000047#include "lldb/Interpreter/Args.h"
48#include "lldb/Interpreter/CommandInterpreter.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000049#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Clayton6eee5aa2010-10-11 01:05:37 +000050#include "lldb/Target/Process.h"
51#include "lldb/Target/TargetList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052
Enrico Granatad717cc92015-10-20 04:50:09 +000053#include "llvm/ADT/STLExtras.h"
Zachary Turner7b2e5a32016-09-16 19:09:12 +000054#include "llvm/ADT/StringRef.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000055#include "llvm/Support/DynamicLibrary.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000056#include "llvm/Support/ManagedStatic.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000057
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058using namespace lldb;
59using namespace lldb_private;
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp,
62 const FileSpec &spec,
Zachary Turner97206d52017-05-12 04:51:55 +000063 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 llvm::sys::DynamicLibrary dynlib =
65 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
66 if (dynlib.isValid()) {
67 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger);
68
69 lldb::SBDebugger debugger_sb(debugger_sp);
70 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
71 // function.
72 // TODO: mangle this differently for your system - on OSX, the first
73 // underscore needs to be removed and the second one stays
74 LLDBCommandPluginInit init_func =
75 (LLDBCommandPluginInit)dynlib.getAddressOfSymbol(
76 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
77 if (init_func) {
78 if (init_func(debugger_sb))
79 return dynlib;
80 else
81 error.SetErrorString("plug-in refused to load "
82 "(lldb::PluginInitialize(lldb::SBDebugger) "
83 "returned false)");
84 } else {
85 error.SetErrorString("plug-in is missing the required initialization: "
86 "lldb::PluginInitialize(lldb::SBDebugger)");
Greg Clayton5fb8f792013-12-02 19:35:49 +000087 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 } else {
89 if (spec.Exists())
90 error.SetErrorString("this file does not represent a loadable dylib");
Greg Clayton5fb8f792013-12-02 19:35:49 +000091 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 error.SetErrorString("no such file");
93 }
94 return llvm::sys::DynamicLibrary();
Greg Clayton5fb8f792013-12-02 19:35:49 +000095}
96
Zachary Turnere6e2bb32015-03-31 21:03:22 +000097static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099SBError SBInputReader::Initialize(
100 lldb::SBDebugger &sb_debugger,
101 unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction,
102 char const *, unsigned long),
103 void *, lldb::InputReaderGranularity, char const *, char const *, bool) {
104 return SBError();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107void SBInputReader::SetIsDone(bool) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109bool SBInputReader::IsActive() const { return false; }
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000110
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000111SBDebugger::SBDebugger() = default;
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp)
114 : m_opaque_sp(debugger_sp) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000117
118SBDebugger::~SBDebugger() = default;
119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) {
121 if (this != &rhs) {
122 m_opaque_sp = rhs.m_opaque_sp;
123 }
124 return *this;
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127void SBDebugger::Initialize() {
128 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 if (log)
131 log->Printf("SBDebugger::Initialize ()");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 g_debugger_lifetime->Initialize(llvm::make_unique<SystemInitializerFull>(),
134 LoadPlugin);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137void SBDebugger::Terminate() { g_debugger_lifetime->Terminate(); }
138
139void SBDebugger::Clear() {
140 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
141
142 if (log)
143 log->Printf("SBDebugger(%p)::Clear ()",
144 static_cast<void *>(m_opaque_sp.get()));
145
146 if (m_opaque_sp)
147 m_opaque_sp->ClearIOHandlers();
148
149 m_opaque_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150}
151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152SBDebugger SBDebugger::Create() {
153 return SBDebugger::Create(false, nullptr, nullptr);
Greg Clayton48e42542010-07-30 20:12:55 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156SBDebugger SBDebugger::Create(bool source_init_files) {
157 return SBDebugger::Create(source_init_files, nullptr, nullptr);
Jim Ingham06942692011-08-13 00:22:20 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160SBDebugger SBDebugger::Create(bool source_init_files,
161 lldb::LogOutputCallback callback, void *baton)
Jim Ingham228063c2012-02-21 02:23:08 +0000162
163{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 SBDebugger debugger;
Greg Claytoncb172b12014-05-19 20:42:14 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 // Currently we have issues if this function is called simultaneously on two
169 // different
170 // threads. The issues mainly revolve around the fact that the
171 // lldb_private::FormatManager
172 // uses global collections and having two threads parsing the .lldbinit files
173 // can cause
174 // mayhem. So to get around this for now we need to use a mutex to prevent bad
175 // things
176 // from happening.
177 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() {
221 // Since this function can be call asynchronously, we allow it to be
222 // non-mandatory. We have seen deadlocks with this function when called
223 // so we need to safeguard against this until we can determine what is
224 // causing the deadlocks.
225 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257// Shouldn't really be settable after initialization as this could cause lots of
258// problems; don't want users
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259// trying to switch modes in the middle of a 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;
482 return Args::StringToScriptLanguage(llvm::StringRef(script_language_name),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494bool SBDebugger::StateIsRunningState(StateType state) {
495 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000496
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 const bool result = lldb_private::StateIsRunningState(state);
498 if (log)
499 log->Printf("SBDebugger::StateIsRunningState (state=%s) => %i",
500 StateAsCString(state), result);
Greg Clayton48381312010-10-30 04:51:46 +0000501
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503}
504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505bool SBDebugger::StateIsStoppedState(StateType state) {
506 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000507
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 const bool result = lldb_private::StateIsStoppedState(state, false);
509 if (log)
510 log->Printf("SBDebugger::StateIsStoppedState (state=%s) => %i",
511 StateAsCString(state), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514}
515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516lldb::SBTarget SBDebugger::CreateTarget(const char *filename,
517 const char *target_triple,
518 const char *platform_name,
519 bool add_dependent_modules,
520 lldb::SBError &sb_error) {
521 SBTarget sb_target;
522 TargetSP target_sp;
523 if (m_opaque_sp) {
524 sb_error.Clear();
525 OptionGroupPlatform platform_options(false);
526 platform_options.SetPlatformName(platform_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget(
529 *m_opaque_sp, filename, target_triple, add_dependent_modules,
530 &platform_options, target_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000531
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 if (sb_error.Success())
533 sb_target.SetSP(target_sp);
534 } else {
535 sb_error.SetErrorString("invalid debugger");
536 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000537
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
539 if (log)
540 log->Printf("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, "
541 "platform_name=%s, add_dependent_modules=%u, error=%s) => "
542 "SBTarget(%p)",
543 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
544 platform_name, add_dependent_modules, sb_error.GetCString(),
545 static_cast<void *>(target_sp.get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000546
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547 return sb_target;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000548}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549
550SBTarget
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename,
552 const char *target_triple) {
553 SBTarget sb_target;
554 TargetSP target_sp;
555 if (m_opaque_sp) {
556 const bool add_dependent_modules = true;
Zachary Turner97206d52017-05-12 04:51:55 +0000557 Status error(m_opaque_sp->GetTargetList().CreateTarget(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 *m_opaque_sp, filename, target_triple, add_dependent_modules, nullptr,
559 target_sp));
560 sb_target.SetSP(target_sp);
561 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000562
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
564 if (log)
565 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple "
566 "(filename=\"%s\", triple=%s) => SBTarget(%p)",
567 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
568 static_cast<void *>(target_sp.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000569
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 return sb_target;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571}
572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
574 const char *arch_cstr) {
575 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000576
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 SBTarget sb_target;
578 TargetSP target_sp;
579 if (m_opaque_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000580 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 const bool add_dependent_modules = true;
582
583 error = m_opaque_sp->GetTargetList().CreateTarget(
584 *m_opaque_sp, filename, arch_cstr, add_dependent_modules, nullptr,
585 target_sp);
586
587 if (error.Success()) {
588 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
589 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000590 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 }
592
593 if (log)
594 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
595 "arch=%s) => SBTarget(%p)",
596 static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr,
597 static_cast<void *>(target_sp.get()));
598
599 return sb_target;
600}
601
602SBTarget SBDebugger::CreateTarget(const char *filename) {
603 SBTarget sb_target;
604 TargetSP target_sp;
605 if (m_opaque_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000606 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 const bool add_dependent_modules = true;
608 error = m_opaque_sp->GetTargetList().CreateTarget(
Zachary Turnera47464b2016-11-18 20:44:46 +0000609 *m_opaque_sp, filename, "", add_dependent_modules, nullptr, target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610
611 if (error.Success()) {
612 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
613 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000614 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 }
616 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
617 if (log)
618 log->Printf(
619 "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
620 static_cast<void *>(m_opaque_sp.get()), filename,
621 static_cast<void *>(target_sp.get()));
622 return sb_target;
623}
624
625bool SBDebugger::DeleteTarget(lldb::SBTarget &target) {
626 bool result = false;
627 if (m_opaque_sp) {
628 TargetSP target_sp(target.GetSP());
629 if (target_sp) {
630 // No need to lock, the target list is thread safe
631 result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp);
632 target_sp->Destroy();
633 target.Clear();
634 const bool mandatory = true;
635 ModuleList::RemoveOrphanSharedModules(mandatory);
636 }
637 }
638
639 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
640 if (log)
641 log->Printf("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
642 static_cast<void *>(m_opaque_sp.get()),
643 static_cast<void *>(target.m_opaque_sp.get()), result);
644
645 return result;
646}
647
648SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) {
649 SBTarget sb_target;
650 if (m_opaque_sp) {
651 // No need to lock, the target list is thread safe
652 sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx));
653 }
654 return sb_target;
655}
656
657uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) {
658
659 lldb::TargetSP target_sp = target.GetSP();
660 if (!target_sp)
661 return UINT32_MAX;
662
663 if (!m_opaque_sp)
664 return UINT32_MAX;
665
666 return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP());
667}
668
669SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) {
670 SBTarget sb_target;
671 if (m_opaque_sp) {
672 // No need to lock, the target list is thread safe
673 sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid));
674 }
675 return sb_target;
676}
677
678SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename,
679 const char *arch_name) {
680 SBTarget sb_target;
681 if (m_opaque_sp && filename && filename[0]) {
682 // No need to lock, the target list is thread safe
683 ArchSpec arch(arch_name,
684 m_opaque_sp->GetPlatformList().GetSelectedPlatform().get());
685 TargetSP target_sp(
686 m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(
687 FileSpec(filename, false), arch_name ? &arch : nullptr));
688 sb_target.SetSP(target_sp);
689 }
690 return sb_target;
691}
692
693SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) {
694 SBTarget sb_target;
695 if (m_opaque_sp) {
696 // No need to lock, the target list is thread safe
697 sb_target.SetSP(
698 m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get()));
699 }
700 return sb_target;
701}
702
703uint32_t SBDebugger::GetNumTargets() {
704 if (m_opaque_sp) {
705 // No need to lock, the target list is thread safe
706 return m_opaque_sp->GetTargetList().GetNumTargets();
707 }
708 return 0;
709}
710
711SBTarget SBDebugger::GetSelectedTarget() {
712 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
713
714 SBTarget sb_target;
715 TargetSP target_sp;
716 if (m_opaque_sp) {
717 // No need to lock, the target list is thread safe
718 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget();
719 sb_target.SetSP(target_sp);
720 }
721
722 if (log) {
723 SBStream sstr;
724 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
725 log->Printf("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
726 static_cast<void *>(m_opaque_sp.get()),
727 static_cast<void *>(target_sp.get()), sstr.GetData());
728 }
729
730 return sb_target;
731}
732
733void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
734 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
735
736 TargetSP target_sp(sb_target.GetSP());
737 if (m_opaque_sp) {
738 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
739 }
740 if (log) {
741 SBStream sstr;
742 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
743 log->Printf("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
744 static_cast<void *>(m_opaque_sp.get()),
745 static_cast<void *>(target_sp.get()), sstr.GetData());
746 }
747}
748
749SBPlatform SBDebugger::GetSelectedPlatform() {
750 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
751
752 SBPlatform sb_platform;
753 DebuggerSP debugger_sp(m_opaque_sp);
754 if (debugger_sp) {
755 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
756 }
757 if (log)
758 log->Printf("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
759 static_cast<void *>(m_opaque_sp.get()),
760 static_cast<void *>(sb_platform.GetSP().get()),
761 sb_platform.GetName());
762 return sb_platform;
763}
764
765void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) {
766 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
767
768 DebuggerSP debugger_sp(m_opaque_sp);
769 if (debugger_sp) {
770 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
771 }
772
773 if (log)
774 log->Printf("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
775 static_cast<void *>(m_opaque_sp.get()),
776 static_cast<void *>(sb_platform.GetSP().get()),
777 sb_platform.GetName());
778}
779
Vadim Macagonc10e34d2017-08-09 09:20:40 +0000780uint32_t SBDebugger::GetNumPlatforms() {
781 if (m_opaque_sp) {
782 // No need to lock, the platform list is thread safe
783 return m_opaque_sp->GetPlatformList().GetSize();
784 }
785 return 0;
786}
787
788SBPlatform SBDebugger::GetPlatformAtIndex(uint32_t idx) {
789 SBPlatform sb_platform;
790 if (m_opaque_sp) {
791 // No need to lock, the platform list is thread safe
792 sb_platform.SetSP(m_opaque_sp->GetPlatformList().GetAtIndex(idx));
793 }
794 return sb_platform;
795}
796
797uint32_t SBDebugger::GetNumAvailablePlatforms() {
798 uint32_t idx = 0;
799 while (true) {
800 if (!PluginManager::GetPlatformPluginNameAtIndex(idx)) {
801 break;
802 }
803 ++idx;
804 }
805 // +1 for the host platform, which should always appear first in the list.
806 return idx + 1;
807}
808
809SBStructuredData SBDebugger::GetAvailablePlatformInfoAtIndex(uint32_t idx) {
810 SBStructuredData data;
811 auto platform_dict = llvm::make_unique<StructuredData::Dictionary>();
812 llvm::StringRef name_str("name"), desc_str("description");
813
814 if (idx == 0) {
815 PlatformSP host_platform_sp(Platform::GetHostPlatform());
816 platform_dict->AddStringItem(
817 name_str, host_platform_sp->GetPluginName().GetStringRef());
818 platform_dict->AddStringItem(
819 desc_str, llvm::StringRef(host_platform_sp->GetDescription()));
820 } else if (idx > 0) {
821 const char *plugin_name =
822 PluginManager::GetPlatformPluginNameAtIndex(idx - 1);
823 if (!plugin_name) {
824 return data;
825 }
826 platform_dict->AddStringItem(name_str, llvm::StringRef(plugin_name));
827
828 const char *plugin_desc =
829 PluginManager::GetPlatformPluginDescriptionAtIndex(idx - 1);
830 if (!plugin_desc) {
831 return data;
832 }
833 platform_dict->AddStringItem(desc_str, llvm::StringRef(plugin_desc));
834 }
835
836 data.m_impl_up->SetObjectSP(
837 StructuredData::ObjectSP(platform_dict.release()));
838 return data;
839}
840
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) {
842 DispatchInput(data, data_len);
843}
844
845void SBDebugger::DispatchInput(const void *data, size_t data_len) {
846 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
847 //
848 // if (log)
849 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\",
850 // size_t=%" PRIu64 ")",
851 // m_opaque_sp.get(),
852 // (int) data_len,
853 // (const char *) data,
854 // (uint64_t)data_len);
855 //
856 // if (m_opaque_sp)
857 // m_opaque_sp->DispatchInput ((const char *) data, data_len);
858}
859
860void SBDebugger::DispatchInputInterrupt() {
861 if (m_opaque_sp)
862 m_opaque_sp->DispatchInputInterrupt();
863}
864
865void SBDebugger::DispatchInputEndOfFile() {
866 if (m_opaque_sp)
867 m_opaque_sp->DispatchInputEndOfFile();
868}
869
870void SBDebugger::PushInputReader(SBInputReader &reader) {}
871
872void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
873 bool spawn_thread) {
874 if (m_opaque_sp) {
875 CommandInterpreterRunOptions options;
876
877 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(
878 auto_handle_events, spawn_thread, options);
879 }
880}
881
882void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
883 bool spawn_thread,
884 SBCommandInterpreterRunOptions &options,
885 int &num_errors, bool &quit_requested,
886 bool &stopped_for_crash)
887
888{
889 if (m_opaque_sp) {
890 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
891 interp.RunCommandInterpreter(auto_handle_events, spawn_thread,
892 options.ref());
893 num_errors = interp.GetNumErrors();
894 quit_requested = interp.GetQuitRequested();
895 stopped_for_crash = interp.GetStoppedForCrash();
896 }
897}
898
899SBError SBDebugger::RunREPL(lldb::LanguageType language,
900 const char *repl_options) {
901 SBError error;
902 if (m_opaque_sp)
903 error.ref() = m_opaque_sp->RunREPL(language, repl_options);
904 else
905 error.SetErrorString("invalid debugger");
906 return error;
907}
908
909void SBDebugger::reset(const DebuggerSP &debugger_sp) {
910 m_opaque_sp = debugger_sp;
911}
912
913Debugger *SBDebugger::get() const { return m_opaque_sp.get(); }
914
915Debugger &SBDebugger::ref() const {
916 assert(m_opaque_sp.get());
917 return *m_opaque_sp;
918}
919
920const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; }
921
922SBDebugger SBDebugger::FindDebuggerWithID(int id) {
923 // No need to lock, the debugger list is thread safe
924 SBDebugger sb_debugger;
925 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id);
926 if (debugger_sp)
927 sb_debugger.reset(debugger_sp);
928 return sb_debugger;
929}
930
931const char *SBDebugger::GetInstanceName() {
932 return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr);
933}
934
935SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
936 const char *debugger_instance_name) {
937 SBError sb_error;
938 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
939 ConstString(debugger_instance_name)));
Zachary Turner97206d52017-05-12 04:51:55 +0000940 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 if (debugger_sp) {
942 ExecutionContext exe_ctx(
943 debugger_sp->GetCommandInterpreter().GetExecutionContext());
944 error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
945 var_name, value);
946 } else {
947 error.SetErrorStringWithFormat("invalid debugger instance name '%s'",
948 debugger_instance_name);
949 }
950 if (error.Fail())
951 sb_error.SetError(error);
952 return sb_error;
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000953}
954
Greg Clayton431ce672011-04-18 23:15:17 +0000955SBStringList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956SBDebugger::GetInternalVariableValue(const char *var_name,
957 const char *debugger_instance_name) {
958 SBStringList ret_value;
959 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
960 ConstString(debugger_instance_name)));
Zachary Turner97206d52017-05-12 04:51:55 +0000961 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000962 if (debugger_sp) {
963 ExecutionContext exe_ctx(
964 debugger_sp->GetCommandInterpreter().GetExecutionContext());
965 lldb::OptionValueSP value_sp(
966 debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error));
967 if (value_sp) {
968 StreamString value_strm;
969 value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
970 const std::string &value_str = value_strm.GetString();
971 if (!value_str.empty()) {
972 StringList string_list;
973 string_list.SplitIntoLines(value_str);
974 return SBStringList(&string_list);
975 }
976 }
977 }
978 return SBStringList();
979}
980
981uint32_t SBDebugger::GetTerminalWidth() const {
982 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
983}
984
985void SBDebugger::SetTerminalWidth(uint32_t term_width) {
986 if (m_opaque_sp)
987 m_opaque_sp->SetTerminalWidth(term_width);
988}
989
990const char *SBDebugger::GetPrompt() const {
991 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
992
993 if (log)
994 log->Printf("SBDebugger(%p)::GetPrompt () => \"%s\"",
995 static_cast<void *>(m_opaque_sp.get()),
Zachary Turner514d8cd2016-09-23 18:06:53 +0000996 (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : ""));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997
Zachary Turner514d8cd2016-09-23 18:06:53 +0000998 return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
999 : nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000}
1001
1002void SBDebugger::SetPrompt(const char *prompt) {
1003 if (m_opaque_sp)
Zachary Turner514d8cd2016-09-23 18:06:53 +00001004 m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005}
1006
1007ScriptLanguage SBDebugger::GetScriptLanguage() const {
1008 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
1009}
1010
1011void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) {
1012 if (m_opaque_sp) {
1013 m_opaque_sp->SetScriptLanguage(script_lang);
1014 }
1015}
1016
1017bool SBDebugger::SetUseExternalEditor(bool value) {
1018 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
1019}
1020
1021bool SBDebugger::GetUseExternalEditor() {
1022 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
1023}
1024
1025bool SBDebugger::SetUseColor(bool value) {
1026 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
1027}
1028
1029bool SBDebugger::GetUseColor() const {
1030 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
1031}
1032
1033bool SBDebugger::GetDescription(SBStream &description) {
1034 Stream &strm = description.ref();
1035
1036 if (m_opaque_sp) {
1037 const char *name = m_opaque_sp->GetInstanceName().AsCString();
1038 user_id_t id = m_opaque_sp->GetID();
1039 strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1040 } else
1041 strm.PutCString("No value");
1042
1043 return true;
1044}
1045
1046user_id_t SBDebugger::GetID() {
1047 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
1048}
1049
1050SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
1051 SBError sb_error;
1052 if (m_opaque_sp) {
1053 if (platform_name_cstr && platform_name_cstr[0]) {
1054 ConstString platform_name(platform_name_cstr);
1055 PlatformSP platform_sp(Platform::Find(platform_name));
1056
1057 if (platform_sp) {
1058 // Already have a platform with this name, just select it
1059 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
1060 } else {
1061 // We don't have a platform by this name yet, create one
1062 platform_sp = Platform::Create(platform_name, sb_error.ref());
1063 if (platform_sp) {
1064 // We created the platform, now append and select it
1065 bool make_selected = true;
1066 m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected);
Greg Clayton6920b522012-08-22 18:39:03 +00001067 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001068 }
1069 } else {
1070 sb_error.ref().SetErrorString("invalid platform name");
Greg Clayton6920b522012-08-22 18:39:03 +00001071 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001072 } else {
1073 sb_error.ref().SetErrorString("invalid debugger");
1074 }
1075 return sb_error;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001076}
1077
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) {
1079 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1080 if (m_opaque_sp) {
1081 PlatformSP platform_sp(
1082 m_opaque_sp->GetPlatformList().GetSelectedPlatform());
Greg Claytona7015092010-09-18 01:14:36 +00001083
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 if (platform_sp) {
1085 if (log && sysroot)
1086 log->Printf("SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")", sysroot);
1087 platform_sp->SetSDKRootDirectory(ConstString(sysroot));
1088 return true;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 }
1091 return false;
Greg Claytona7015092010-09-18 01:14:36 +00001092}
1093
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094bool SBDebugger::GetCloseInputOnEOF() const {
1095 return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false);
Jim Inghame40e4212010-08-30 19:44:40 +00001096}
1097
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098void SBDebugger::SetCloseInputOnEOF(bool b) {
1099 if (m_opaque_sp)
1100 m_opaque_sp->SetCloseInputOnEOF(b);
Jim Inghame40e4212010-08-30 19:44:40 +00001101}
1102
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103SBTypeCategory SBDebugger::GetCategory(const char *category_name) {
1104 if (!category_name || *category_name == 0)
1105 return SBTypeCategory();
1106
1107 TypeCategoryImplSP category_sp;
1108
1109 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1110 category_sp, false))
1111 return SBTypeCategory(category_sp);
1112 else
1113 return SBTypeCategory();
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001114}
1115
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) {
1117 TypeCategoryImplSP category_sp;
1118 if (DataVisualization::Categories::GetCategory(lang_type, category_sp))
1119 return SBTypeCategory(category_sp);
1120 else
1121 return SBTypeCategory();
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001122}
1123
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124SBTypeCategory SBDebugger::CreateCategory(const char *category_name) {
1125 if (!category_name || *category_name == 0)
1126 return SBTypeCategory();
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001127
Kate Stoneb9c1b512016-09-06 20:57:50 +00001128 TypeCategoryImplSP category_sp;
1129
1130 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1131 category_sp, true))
1132 return SBTypeCategory(category_sp);
1133 else
1134 return SBTypeCategory();
Caroline Ticedde9cff2010-09-20 05:20:02 +00001135}
Caroline Ticeefed6132010-11-19 20:47:54 +00001136
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137bool SBDebugger::DeleteCategory(const char *category_name) {
1138 if (!category_name || *category_name == 0)
1139 return false;
1140
1141 return DataVisualization::Categories::Delete(ConstString(category_name));
Caroline Ticeefed6132010-11-19 20:47:54 +00001142}
Greg Clayton2289fa42011-04-30 01:09:13 +00001143
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144uint32_t SBDebugger::GetNumCategories() {
1145 return DataVisualization::Categories::GetCount();
Greg Clayton2289fa42011-04-30 01:09:13 +00001146}
1147
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) {
1149 return SBTypeCategory(
1150 DataVisualization::Categories::GetCategoryAtIndex(index));
1151}
1152
1153SBTypeCategory SBDebugger::GetDefaultCategory() {
1154 return GetCategory("default");
1155}
1156
1157SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) {
1158 SBTypeCategory default_category_sb = GetDefaultCategory();
1159 if (default_category_sb.GetEnabled())
1160 return default_category_sb.GetFormatForType(type_name);
1161 return SBTypeFormat();
1162}
1163
1164#ifndef LLDB_DISABLE_PYTHON
1165SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) {
1166 if (!type_name.IsValid())
1167 return SBTypeSummary();
1168 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1169}
1170#endif // LLDB_DISABLE_PYTHON
1171
1172SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) {
1173 if (!type_name.IsValid())
1174 return SBTypeFilter();
1175 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1176}
1177
1178#ifndef LLDB_DISABLE_PYTHON
1179SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) {
1180 if (!type_name.IsValid())
1181 return SBTypeSynthetic();
1182 return SBTypeSynthetic(
1183 DataVisualization::GetSyntheticForType(type_name.GetSP()));
1184}
1185#endif // LLDB_DISABLE_PYTHON
1186
Pavel Labath5e336902017-03-01 10:08:40 +00001187static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) {
1188 if (categories == nullptr)
1189 return {};
1190 size_t len = 0;
1191 while (categories[len] != nullptr)
1192 ++len;
1193 return llvm::makeArrayRef(categories, len);
1194}
1195
Kate Stoneb9c1b512016-09-06 20:57:50 +00001196bool SBDebugger::EnableLog(const char *channel, const char **categories) {
1197 if (m_opaque_sp) {
1198 uint32_t log_options =
1199 LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
Pavel Labath775588c2017-03-15 09:06:58 +00001200 std::string error;
1201 llvm::raw_string_ostream error_stream(error);
Pavel Labath5e336902017-03-01 10:08:40 +00001202 return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
Pavel Labath775588c2017-03-15 09:06:58 +00001203 log_options, error_stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001204 } else
Greg Claytonf3dd93c2011-06-17 03:31:01 +00001205 return false;
1206}
1207
Kate Stoneb9c1b512016-09-06 20:57:50 +00001208void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
1209 void *baton) {
1210 if (m_opaque_sp) {
1211 return m_opaque_sp->SetLoggingCallback(log_callback, baton);
1212 }
Jim Ingham4f02b222012-02-22 22:49:20 +00001213}