blob: e8f55424ae7882be301e7461d1aee31d27f4644f [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"
29#include "lldb/API/SBTarget.h"
30#include "lldb/API/SBThread.h"
Enrico Granata061858c2012-02-15 02:34:21 +000031#include "lldb/API/SBTypeCategory.h"
Enrico Granata061858c2012-02-15 02:34:21 +000032#include "lldb/API/SBTypeFilter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000033#include "lldb/API/SBTypeFormat.h"
Enrico Granata061858c2012-02-15 02:34:21 +000034#include "lldb/API/SBTypeNameSpecifier.h"
35#include "lldb/API/SBTypeSummary.h"
36#include "lldb/API/SBTypeSynthetic.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000037#include "lldb/API/SystemInitializerFull.h"
Enrico Granata061858c2012-02-15 02:34:21 +000038
Greg Clayton6eee5aa2010-10-11 01:05:37 +000039#include "lldb/Core/Debugger.h"
40#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000041#include "lldb/Core/StreamFile.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000042#include "lldb/DataFormatters/DataVisualization.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/Args.h"
45#include "lldb/Interpreter/CommandInterpreter.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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
Enrico Granatad717cc92015-10-20 04:50:09 +000050#include "llvm/ADT/STLExtras.h"
Zachary Turner7b2e5a32016-09-16 19:09:12 +000051#include "llvm/ADT/StringRef.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000052#include "llvm/Support/DynamicLibrary.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000053#include "llvm/Support/ManagedStatic.h"
Zachary Turner58a559c2014-08-27 20:15:09 +000054
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055using namespace lldb;
56using namespace lldb_private;
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp,
59 const FileSpec &spec,
60 Error &error) {
61 llvm::sys::DynamicLibrary dynlib =
62 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
63 if (dynlib.isValid()) {
64 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger & debugger);
65
66 lldb::SBDebugger debugger_sb(debugger_sp);
67 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
68 // function.
69 // TODO: mangle this differently for your system - on OSX, the first
70 // underscore needs to be removed and the second one stays
71 LLDBCommandPluginInit init_func =
72 (LLDBCommandPluginInit)dynlib.getAddressOfSymbol(
73 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
74 if (init_func) {
75 if (init_func(debugger_sb))
76 return dynlib;
77 else
78 error.SetErrorString("plug-in refused to load "
79 "(lldb::PluginInitialize(lldb::SBDebugger) "
80 "returned false)");
81 } else {
82 error.SetErrorString("plug-in is missing the required initialization: "
83 "lldb::PluginInitialize(lldb::SBDebugger)");
Greg Clayton5fb8f792013-12-02 19:35:49 +000084 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 } else {
86 if (spec.Exists())
87 error.SetErrorString("this file does not represent a loadable dylib");
Greg Clayton5fb8f792013-12-02 19:35:49 +000088 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 error.SetErrorString("no such file");
90 }
91 return llvm::sys::DynamicLibrary();
Greg Clayton5fb8f792013-12-02 19:35:49 +000092}
93
Zachary Turnere6e2bb32015-03-31 21:03:22 +000094static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096SBError SBInputReader::Initialize(
97 lldb::SBDebugger &sb_debugger,
98 unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction,
99 char const *, unsigned long),
100 void *, lldb::InputReaderGranularity, char const *, char const *, bool) {
101 return SBError();
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000102}
103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104void SBInputReader::SetIsDone(bool) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106bool SBInputReader::IsActive() const { return false; }
Zachary Turnere6e2bb32015-03-31 21:03:22 +0000107
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000108SBDebugger::SBDebugger() = default;
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp)
111 : m_opaque_sp(debugger_sp) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113SBDebugger::SBDebugger(const SBDebugger &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000114
115SBDebugger::~SBDebugger() = default;
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117SBDebugger &SBDebugger::operator=(const SBDebugger &rhs) {
118 if (this != &rhs) {
119 m_opaque_sp = rhs.m_opaque_sp;
120 }
121 return *this;
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124void SBDebugger::Initialize() {
125 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 if (log)
128 log->Printf("SBDebugger::Initialize ()");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 g_debugger_lifetime->Initialize(llvm::make_unique<SystemInitializerFull>(),
131 LoadPlugin);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134void SBDebugger::Terminate() { g_debugger_lifetime->Terminate(); }
135
136void SBDebugger::Clear() {
137 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
138
139 if (log)
140 log->Printf("SBDebugger(%p)::Clear ()",
141 static_cast<void *>(m_opaque_sp.get()));
142
143 if (m_opaque_sp)
144 m_opaque_sp->ClearIOHandlers();
145
146 m_opaque_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149SBDebugger SBDebugger::Create() {
150 return SBDebugger::Create(false, nullptr, nullptr);
Greg Clayton48e42542010-07-30 20:12:55 +0000151}
152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153SBDebugger SBDebugger::Create(bool source_init_files) {
154 return SBDebugger::Create(source_init_files, nullptr, nullptr);
Jim Ingham06942692011-08-13 00:22:20 +0000155}
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157SBDebugger SBDebugger::Create(bool source_init_files,
158 lldb::LogOutputCallback callback, void *baton)
Jim Ingham228063c2012-02-21 02:23:08 +0000159
160{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 SBDebugger debugger;
Greg Claytoncb172b12014-05-19 20:42:14 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 // Currently we have issues if this function is called simultaneously on two
166 // different
167 // threads. The issues mainly revolve around the fact that the
168 // lldb_private::FormatManager
169 // uses global collections and having two threads parsing the .lldbinit files
170 // can cause
171 // mayhem. So to get around this for now we need to use a mutex to prevent bad
172 // things
173 // from happening.
174 static std::recursive_mutex g_mutex;
175 std::lock_guard<std::recursive_mutex> guard(g_mutex);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 debugger.reset(Debugger::CreateInstance(callback, baton));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 if (log) {
180 SBStream sstr;
181 debugger.GetDescription(sstr);
182 log->Printf("SBDebugger::Create () => SBDebugger(%p): %s",
183 static_cast<void *>(debugger.m_opaque_sp.get()),
184 sstr.GetData());
185 }
186
187 SBCommandInterpreter interp = debugger.GetCommandInterpreter();
188 if (source_init_files) {
189 interp.get()->SkipLLDBInitFiles(false);
190 interp.get()->SkipAppInitFiles(false);
191 SBCommandReturnObject result;
192 interp.SourceInitFileInHomeDirectory(result);
193 } else {
194 interp.get()->SkipLLDBInitFiles(true);
195 interp.get()->SkipAppInitFiles(true);
196 }
197 return debugger;
Greg Clayton66111032010-06-23 01:19:29 +0000198}
199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200void SBDebugger::Destroy(SBDebugger &debugger) {
201 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 if (log) {
204 SBStream sstr;
205 debugger.GetDescription(sstr);
206 log->Printf("SBDebugger::Destroy () => SBDebugger(%p): %s",
207 static_cast<void *>(debugger.m_opaque_sp.get()),
208 sstr.GetData());
209 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 Debugger::Destroy(debugger.m_opaque_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 if (debugger.m_opaque_sp.get() != nullptr)
214 debugger.m_opaque_sp.reset();
Caroline Ticee02657b2011-01-22 01:02:07 +0000215}
216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217void SBDebugger::MemoryPressureDetected() {
218 // Since this function can be call asynchronously, we allow it to be
219 // non-mandatory. We have seen deadlocks with this function when called
220 // so we need to safeguard against this until we can determine what is
221 // causing the deadlocks.
222 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
223
224 const bool mandatory = false;
225 if (log) {
226 log->Printf("SBDebugger::MemoryPressureDetected (), mandatory = %d",
227 mandatory);
228 }
229
230 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonf9322412011-12-15 04:38:41 +0000231}
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233bool SBDebugger::IsValid() const { return m_opaque_sp.get() != nullptr; }
234
235void SBDebugger::SetAsync(bool b) {
236 if (m_opaque_sp)
237 m_opaque_sp->SetAsyncExecution(b);
Greg Clayton66111032010-06-23 01:19:29 +0000238}
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240bool SBDebugger::GetAsync() {
241 return (m_opaque_sp ? m_opaque_sp->GetAsyncExecution() : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244void SBDebugger::SkipLLDBInitFiles(bool b) {
245 if (m_opaque_sp)
246 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles(b);
Jim Inghame64f0dc2011-09-13 23:25:31 +0000247}
248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249void SBDebugger::SkipAppInitFiles(bool b) {
250 if (m_opaque_sp)
251 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles(b);
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254// Shouldn't really be settable after initialization as this could cause lots of
255// problems; don't want users
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256// trying to switch modes in the middle of a debugging session.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) {
258 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 if (log)
261 log->Printf(
262 "SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)",
263 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
264 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 if (m_opaque_sp)
267 m_opaque_sp->SetInputFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268}
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270void SBDebugger::SetOutputFileHandle(FILE *fh, bool transfer_ownership) {
271 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 if (log)
274 log->Printf(
275 "SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)",
276 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
277 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 if (m_opaque_sp)
280 m_opaque_sp->SetOutputFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281}
282
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283void SBDebugger::SetErrorFileHandle(FILE *fh, bool transfer_ownership) {
284 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 if (log)
287 log->Printf(
288 "SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)",
289 static_cast<void *>(m_opaque_sp.get()), static_cast<void *>(fh),
290 transfer_ownership);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 if (m_opaque_sp)
293 m_opaque_sp->SetErrorFileHandle(fh, transfer_ownership);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294}
295
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296FILE *SBDebugger::GetInputFileHandle() {
297 if (m_opaque_sp) {
298 StreamFileSP stream_file_sp(m_opaque_sp->GetInputFile());
299 if (stream_file_sp)
300 return stream_file_sp->GetFile().GetStream();
301 }
302 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303}
304
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305FILE *SBDebugger::GetOutputFileHandle() {
306 if (m_opaque_sp) {
307 StreamFileSP stream_file_sp(m_opaque_sp->GetOutputFile());
308 if (stream_file_sp)
309 return stream_file_sp->GetFile().GetStream();
310 }
311 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312}
313
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314FILE *SBDebugger::GetErrorFileHandle() {
315 if (m_opaque_sp) {
316 StreamFileSP stream_file_sp(m_opaque_sp->GetErrorFile());
317 if (stream_file_sp)
318 return stream_file_sp->GetFile().GetStream();
319 }
320 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321}
322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323void SBDebugger::SaveInputTerminalState() {
324 if (m_opaque_sp)
325 m_opaque_sp->SaveInputTerminalState();
Jim Inghamc5917d92012-11-30 20:23:19 +0000326}
327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328void SBDebugger::RestoreInputTerminalState() {
329 if (m_opaque_sp)
330 m_opaque_sp->RestoreInputTerminalState();
Jim Inghamc5917d92012-11-30 20:23:19 +0000331}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332SBCommandInterpreter SBDebugger::GetCommandInterpreter() {
333 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 SBCommandInterpreter sb_interpreter;
336 if (m_opaque_sp)
337 sb_interpreter.reset(&m_opaque_sp->GetCommandInterpreter());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 if (log)
340 log->Printf(
341 "SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)",
342 static_cast<void *>(m_opaque_sp.get()),
343 static_cast<void *>(sb_interpreter.get()));
Caroline Tice750cd172010-10-26 23:49:36 +0000344
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 return sb_interpreter;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346}
347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348void SBDebugger::HandleCommand(const char *command) {
349 if (m_opaque_sp) {
350 TargetSP target_sp(m_opaque_sp->GetSelectedTarget());
351 std::unique_lock<std::recursive_mutex> lock;
352 if (target_sp)
353 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +0000354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 SBCommandInterpreter sb_interpreter(GetCommandInterpreter());
356 SBCommandReturnObject result;
Greg Clayton66111032010-06-23 01:19:29 +0000357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 sb_interpreter.HandleCommand(command, result, false);
Greg Clayton66111032010-06-23 01:19:29 +0000359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 if (GetErrorFileHandle() != nullptr)
361 result.PutError(GetErrorFileHandle());
362 if (GetOutputFileHandle() != nullptr)
363 result.PutOutput(GetOutputFileHandle());
Greg Clayton66111032010-06-23 01:19:29 +0000364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 if (!m_opaque_sp->GetAsyncExecution()) {
366 SBProcess process(GetCommandInterpreter().GetProcess());
367 ProcessSP process_sp(process.GetSP());
368 if (process_sp) {
369 EventSP event_sp;
370 ListenerSP lldb_listener_sp = m_opaque_sp->GetListener();
371 while (lldb_listener_sp->GetNextEventForBroadcaster(process_sp.get(),
372 event_sp)) {
373 SBEvent event(event_sp);
374 HandleProcessEvent(process, event, GetOutputFileHandle(),
375 GetErrorFileHandle());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380}
381
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382SBListener SBDebugger::GetListener() {
383 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000384
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 SBListener sb_listener;
386 if (m_opaque_sp)
387 sb_listener.reset(m_opaque_sp->GetListener());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 if (log)
390 log->Printf("SBDebugger(%p)::GetListener () => SBListener(%p)",
391 static_cast<void *>(m_opaque_sp.get()),
392 static_cast<void *>(sb_listener.get()));
Caroline Tice750cd172010-10-26 23:49:36 +0000393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 return sb_listener;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395}
396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397void SBDebugger::HandleProcessEvent(const SBProcess &process,
398 const SBEvent &event, FILE *out,
399 FILE *err) {
400 if (!process.IsValid())
401 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 TargetSP target_sp(process.GetTarget().GetSP());
404 if (!target_sp)
405 return;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 const uint32_t event_type = event.GetType();
408 char stdio_buffer[1024];
409 size_t len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 if (event_type &
414 (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) {
415 // Drain stdout when we stop just in case we have any bytes
416 while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0)
417 if (out != nullptr)
418 ::fwrite(stdio_buffer, 1, len, out);
419 }
Todd Fiala75930012016-08-19 04:21:48 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 if (event_type &
422 (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) {
423 // Drain stderr when we stop just in case we have any bytes
424 while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0)
425 if (err != nullptr)
426 ::fwrite(stdio_buffer, 1, len, err);
427 }
428
429 if (event_type & Process::eBroadcastBitStateChanged) {
430 StateType event_state = SBProcess::GetStateFromEvent(event);
431
432 if (event_state == eStateInvalid)
433 return;
434
435 bool is_stopped = StateIsStoppedState(event_state);
436 if (!is_stopped)
437 process.ReportEventState(event, out);
438 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439}
440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441SBSourceManager SBDebugger::GetSourceManager() {
442 SBSourceManager sb_source_manager(*this);
443 return sb_source_manager;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444}
445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446bool SBDebugger::GetDefaultArchitecture(char *arch_name, size_t arch_name_len) {
447 if (arch_name && arch_name_len) {
448 ArchSpec default_arch = Target::GetDefaultArchitecture();
Caroline Ticedaccaa92010-09-20 20:44:43 +0000449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 if (default_arch.IsValid()) {
451 const std::string &triple_str = default_arch.GetTriple().str();
452 if (!triple_str.empty())
453 ::snprintf(arch_name, arch_name_len, "%s", triple_str.c_str());
454 else
455 ::snprintf(arch_name, arch_name_len, "%s",
456 default_arch.GetArchitectureName());
457 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 }
460 if (arch_name && arch_name_len)
461 arch_name[0] = '\0';
462 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463}
464
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465bool SBDebugger::SetDefaultArchitecture(const char *arch_name) {
466 if (arch_name) {
467 ArchSpec arch(arch_name);
468 if (arch.IsValid()) {
469 Target::SetDefaultArchitecture(arch);
470 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472 }
473 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474}
475
476ScriptLanguage
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477SBDebugger::GetScriptingLanguage(const char *script_language_name) {
Zachary Turner7b2e5a32016-09-16 19:09:12 +0000478 if (!script_language_name) return eScriptLanguageDefault;
479 return Args::StringToScriptLanguage(llvm::StringRef(script_language_name),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480 eScriptLanguageDefault, nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483const char *SBDebugger::GetVersionString() {
484 return lldb_private::GetVersion();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485}
486
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487const char *SBDebugger::StateAsCString(StateType state) {
488 return lldb_private::StateAsCString(state);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489}
490
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491bool SBDebugger::StateIsRunningState(StateType state) {
492 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000493
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 const bool result = lldb_private::StateIsRunningState(state);
495 if (log)
496 log->Printf("SBDebugger::StateIsRunningState (state=%s) => %i",
497 StateAsCString(state), result);
Greg Clayton48381312010-10-30 04:51:46 +0000498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500}
501
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502bool SBDebugger::StateIsStoppedState(StateType state) {
503 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 const bool result = lldb_private::StateIsStoppedState(state, false);
506 if (log)
507 log->Printf("SBDebugger::StateIsStoppedState (state=%s) => %i",
508 StateAsCString(state), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000509
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511}
512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513lldb::SBTarget SBDebugger::CreateTarget(const char *filename,
514 const char *target_triple,
515 const char *platform_name,
516 bool add_dependent_modules,
517 lldb::SBError &sb_error) {
518 SBTarget sb_target;
519 TargetSP target_sp;
520 if (m_opaque_sp) {
521 sb_error.Clear();
522 OptionGroupPlatform platform_options(false);
523 platform_options.SetPlatformName(platform_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000524
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget(
526 *m_opaque_sp, filename, target_triple, add_dependent_modules,
527 &platform_options, target_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000528
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 if (sb_error.Success())
530 sb_target.SetSP(target_sp);
531 } else {
532 sb_error.SetErrorString("invalid debugger");
533 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000534
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
536 if (log)
537 log->Printf("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, "
538 "platform_name=%s, add_dependent_modules=%u, error=%s) => "
539 "SBTarget(%p)",
540 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
541 platform_name, add_dependent_modules, sb_error.GetCString(),
542 static_cast<void *>(target_sp.get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000543
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544 return sb_target;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000545}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546
547SBTarget
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548SBDebugger::CreateTargetWithFileAndTargetTriple(const char *filename,
549 const char *target_triple) {
550 SBTarget sb_target;
551 TargetSP target_sp;
552 if (m_opaque_sp) {
553 const bool add_dependent_modules = true;
554 Error error(m_opaque_sp->GetTargetList().CreateTarget(
555 *m_opaque_sp, filename, target_triple, add_dependent_modules, nullptr,
556 target_sp));
557 sb_target.SetSP(target_sp);
558 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000559
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
561 if (log)
562 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple "
563 "(filename=\"%s\", triple=%s) => SBTarget(%p)",
564 static_cast<void *>(m_opaque_sp.get()), filename, target_triple,
565 static_cast<void *>(target_sp.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567 return sb_target;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568}
569
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
571 const char *arch_cstr) {
572 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000573
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 SBTarget sb_target;
575 TargetSP target_sp;
576 if (m_opaque_sp) {
Greg Clayton6920b522012-08-22 18:39:03 +0000577 Error error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 const bool add_dependent_modules = true;
579
580 error = m_opaque_sp->GetTargetList().CreateTarget(
581 *m_opaque_sp, filename, arch_cstr, add_dependent_modules, nullptr,
582 target_sp);
583
584 if (error.Success()) {
585 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
586 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000587 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 }
589
590 if (log)
591 log->Printf("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
592 "arch=%s) => SBTarget(%p)",
593 static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr,
594 static_cast<void *>(target_sp.get()));
595
596 return sb_target;
597}
598
599SBTarget SBDebugger::CreateTarget(const char *filename) {
600 SBTarget sb_target;
601 TargetSP target_sp;
602 if (m_opaque_sp) {
603 Error error;
604 const bool add_dependent_modules = true;
605 error = m_opaque_sp->GetTargetList().CreateTarget(
606 *m_opaque_sp, filename, nullptr, add_dependent_modules, nullptr,
607 target_sp);
608
609 if (error.Success()) {
610 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
611 sb_target.SetSP(target_sp);
Greg Clayton6920b522012-08-22 18:39:03 +0000612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613 }
614 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
615 if (log)
616 log->Printf(
617 "SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
618 static_cast<void *>(m_opaque_sp.get()), filename,
619 static_cast<void *>(target_sp.get()));
620 return sb_target;
621}
622
623bool SBDebugger::DeleteTarget(lldb::SBTarget &target) {
624 bool result = false;
625 if (m_opaque_sp) {
626 TargetSP target_sp(target.GetSP());
627 if (target_sp) {
628 // No need to lock, the target list is thread safe
629 result = m_opaque_sp->GetTargetList().DeleteTarget(target_sp);
630 target_sp->Destroy();
631 target.Clear();
632 const bool mandatory = true;
633 ModuleList::RemoveOrphanSharedModules(mandatory);
634 }
635 }
636
637 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
638 if (log)
639 log->Printf("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
640 static_cast<void *>(m_opaque_sp.get()),
641 static_cast<void *>(target.m_opaque_sp.get()), result);
642
643 return result;
644}
645
646SBTarget SBDebugger::GetTargetAtIndex(uint32_t idx) {
647 SBTarget sb_target;
648 if (m_opaque_sp) {
649 // No need to lock, the target list is thread safe
650 sb_target.SetSP(m_opaque_sp->GetTargetList().GetTargetAtIndex(idx));
651 }
652 return sb_target;
653}
654
655uint32_t SBDebugger::GetIndexOfTarget(lldb::SBTarget target) {
656
657 lldb::TargetSP target_sp = target.GetSP();
658 if (!target_sp)
659 return UINT32_MAX;
660
661 if (!m_opaque_sp)
662 return UINT32_MAX;
663
664 return m_opaque_sp->GetTargetList().GetIndexOfTarget(target.GetSP());
665}
666
667SBTarget SBDebugger::FindTargetWithProcessID(lldb::pid_t pid) {
668 SBTarget sb_target;
669 if (m_opaque_sp) {
670 // No need to lock, the target list is thread safe
671 sb_target.SetSP(m_opaque_sp->GetTargetList().FindTargetWithProcessID(pid));
672 }
673 return sb_target;
674}
675
676SBTarget SBDebugger::FindTargetWithFileAndArch(const char *filename,
677 const char *arch_name) {
678 SBTarget sb_target;
679 if (m_opaque_sp && filename && filename[0]) {
680 // No need to lock, the target list is thread safe
681 ArchSpec arch(arch_name,
682 m_opaque_sp->GetPlatformList().GetSelectedPlatform().get());
683 TargetSP target_sp(
684 m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture(
685 FileSpec(filename, false), arch_name ? &arch : nullptr));
686 sb_target.SetSP(target_sp);
687 }
688 return sb_target;
689}
690
691SBTarget SBDebugger::FindTargetWithLLDBProcess(const ProcessSP &process_sp) {
692 SBTarget sb_target;
693 if (m_opaque_sp) {
694 // No need to lock, the target list is thread safe
695 sb_target.SetSP(
696 m_opaque_sp->GetTargetList().FindTargetWithProcess(process_sp.get()));
697 }
698 return sb_target;
699}
700
701uint32_t SBDebugger::GetNumTargets() {
702 if (m_opaque_sp) {
703 // No need to lock, the target list is thread safe
704 return m_opaque_sp->GetTargetList().GetNumTargets();
705 }
706 return 0;
707}
708
709SBTarget SBDebugger::GetSelectedTarget() {
710 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
711
712 SBTarget sb_target;
713 TargetSP target_sp;
714 if (m_opaque_sp) {
715 // No need to lock, the target list is thread safe
716 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget();
717 sb_target.SetSP(target_sp);
718 }
719
720 if (log) {
721 SBStream sstr;
722 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
723 log->Printf("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
724 static_cast<void *>(m_opaque_sp.get()),
725 static_cast<void *>(target_sp.get()), sstr.GetData());
726 }
727
728 return sb_target;
729}
730
731void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
732 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
733
734 TargetSP target_sp(sb_target.GetSP());
735 if (m_opaque_sp) {
736 m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
737 }
738 if (log) {
739 SBStream sstr;
740 sb_target.GetDescription(sstr, eDescriptionLevelBrief);
741 log->Printf("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
742 static_cast<void *>(m_opaque_sp.get()),
743 static_cast<void *>(target_sp.get()), sstr.GetData());
744 }
745}
746
747SBPlatform SBDebugger::GetSelectedPlatform() {
748 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
749
750 SBPlatform sb_platform;
751 DebuggerSP debugger_sp(m_opaque_sp);
752 if (debugger_sp) {
753 sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
754 }
755 if (log)
756 log->Printf("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
757 static_cast<void *>(m_opaque_sp.get()),
758 static_cast<void *>(sb_platform.GetSP().get()),
759 sb_platform.GetName());
760 return sb_platform;
761}
762
763void SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform) {
764 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
765
766 DebuggerSP debugger_sp(m_opaque_sp);
767 if (debugger_sp) {
768 debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
769 }
770
771 if (log)
772 log->Printf("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
773 static_cast<void *>(m_opaque_sp.get()),
774 static_cast<void *>(sb_platform.GetSP().get()),
775 sb_platform.GetName());
776}
777
778void SBDebugger::DispatchInput(void *baton, const void *data, size_t data_len) {
779 DispatchInput(data, data_len);
780}
781
782void SBDebugger::DispatchInput(const void *data, size_t data_len) {
783 // Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
784 //
785 // if (log)
786 // log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\",
787 // size_t=%" PRIu64 ")",
788 // m_opaque_sp.get(),
789 // (int) data_len,
790 // (const char *) data,
791 // (uint64_t)data_len);
792 //
793 // if (m_opaque_sp)
794 // m_opaque_sp->DispatchInput ((const char *) data, data_len);
795}
796
797void SBDebugger::DispatchInputInterrupt() {
798 if (m_opaque_sp)
799 m_opaque_sp->DispatchInputInterrupt();
800}
801
802void SBDebugger::DispatchInputEndOfFile() {
803 if (m_opaque_sp)
804 m_opaque_sp->DispatchInputEndOfFile();
805}
806
807void SBDebugger::PushInputReader(SBInputReader &reader) {}
808
809void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
810 bool spawn_thread) {
811 if (m_opaque_sp) {
812 CommandInterpreterRunOptions options;
813
814 m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(
815 auto_handle_events, spawn_thread, options);
816 }
817}
818
819void SBDebugger::RunCommandInterpreter(bool auto_handle_events,
820 bool spawn_thread,
821 SBCommandInterpreterRunOptions &options,
822 int &num_errors, bool &quit_requested,
823 bool &stopped_for_crash)
824
825{
826 if (m_opaque_sp) {
827 CommandInterpreter &interp = m_opaque_sp->GetCommandInterpreter();
828 interp.RunCommandInterpreter(auto_handle_events, spawn_thread,
829 options.ref());
830 num_errors = interp.GetNumErrors();
831 quit_requested = interp.GetQuitRequested();
832 stopped_for_crash = interp.GetStoppedForCrash();
833 }
834}
835
836SBError SBDebugger::RunREPL(lldb::LanguageType language,
837 const char *repl_options) {
838 SBError error;
839 if (m_opaque_sp)
840 error.ref() = m_opaque_sp->RunREPL(language, repl_options);
841 else
842 error.SetErrorString("invalid debugger");
843 return error;
844}
845
846void SBDebugger::reset(const DebuggerSP &debugger_sp) {
847 m_opaque_sp = debugger_sp;
848}
849
850Debugger *SBDebugger::get() const { return m_opaque_sp.get(); }
851
852Debugger &SBDebugger::ref() const {
853 assert(m_opaque_sp.get());
854 return *m_opaque_sp;
855}
856
857const lldb::DebuggerSP &SBDebugger::get_sp() const { return m_opaque_sp; }
858
859SBDebugger SBDebugger::FindDebuggerWithID(int id) {
860 // No need to lock, the debugger list is thread safe
861 SBDebugger sb_debugger;
862 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(id);
863 if (debugger_sp)
864 sb_debugger.reset(debugger_sp);
865 return sb_debugger;
866}
867
868const char *SBDebugger::GetInstanceName() {
869 return (m_opaque_sp ? m_opaque_sp->GetInstanceName().AsCString() : nullptr);
870}
871
872SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
873 const char *debugger_instance_name) {
874 SBError sb_error;
875 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
876 ConstString(debugger_instance_name)));
877 Error error;
878 if (debugger_sp) {
879 ExecutionContext exe_ctx(
880 debugger_sp->GetCommandInterpreter().GetExecutionContext());
881 error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
882 var_name, value);
883 } else {
884 error.SetErrorStringWithFormat("invalid debugger instance name '%s'",
885 debugger_instance_name);
886 }
887 if (error.Fail())
888 sb_error.SetError(error);
889 return sb_error;
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000890}
891
Greg Clayton431ce672011-04-18 23:15:17 +0000892SBStringList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893SBDebugger::GetInternalVariableValue(const char *var_name,
894 const char *debugger_instance_name) {
895 SBStringList ret_value;
896 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName(
897 ConstString(debugger_instance_name)));
898 Error error;
899 if (debugger_sp) {
900 ExecutionContext exe_ctx(
901 debugger_sp->GetCommandInterpreter().GetExecutionContext());
902 lldb::OptionValueSP value_sp(
903 debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error));
904 if (value_sp) {
905 StreamString value_strm;
906 value_sp->DumpValue(&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
907 const std::string &value_str = value_strm.GetString();
908 if (!value_str.empty()) {
909 StringList string_list;
910 string_list.SplitIntoLines(value_str);
911 return SBStringList(&string_list);
912 }
913 }
914 }
915 return SBStringList();
916}
917
918uint32_t SBDebugger::GetTerminalWidth() const {
919 return (m_opaque_sp ? m_opaque_sp->GetTerminalWidth() : 0);
920}
921
922void SBDebugger::SetTerminalWidth(uint32_t term_width) {
923 if (m_opaque_sp)
924 m_opaque_sp->SetTerminalWidth(term_width);
925}
926
927const char *SBDebugger::GetPrompt() const {
928 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
929
930 if (log)
931 log->Printf("SBDebugger(%p)::GetPrompt () => \"%s\"",
932 static_cast<void *>(m_opaque_sp.get()),
Zachary Turner514d8cd2016-09-23 18:06:53 +0000933 (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : ""));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934
Zachary Turner514d8cd2016-09-23 18:06:53 +0000935 return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
936 : nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000937}
938
939void SBDebugger::SetPrompt(const char *prompt) {
940 if (m_opaque_sp)
Zachary Turner514d8cd2016-09-23 18:06:53 +0000941 m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000942}
943
944ScriptLanguage SBDebugger::GetScriptLanguage() const {
945 return (m_opaque_sp ? m_opaque_sp->GetScriptLanguage() : eScriptLanguageNone);
946}
947
948void SBDebugger::SetScriptLanguage(ScriptLanguage script_lang) {
949 if (m_opaque_sp) {
950 m_opaque_sp->SetScriptLanguage(script_lang);
951 }
952}
953
954bool SBDebugger::SetUseExternalEditor(bool value) {
955 return (m_opaque_sp ? m_opaque_sp->SetUseExternalEditor(value) : false);
956}
957
958bool SBDebugger::GetUseExternalEditor() {
959 return (m_opaque_sp ? m_opaque_sp->GetUseExternalEditor() : false);
960}
961
962bool SBDebugger::SetUseColor(bool value) {
963 return (m_opaque_sp ? m_opaque_sp->SetUseColor(value) : false);
964}
965
966bool SBDebugger::GetUseColor() const {
967 return (m_opaque_sp ? m_opaque_sp->GetUseColor() : false);
968}
969
970bool SBDebugger::GetDescription(SBStream &description) {
971 Stream &strm = description.ref();
972
973 if (m_opaque_sp) {
974 const char *name = m_opaque_sp->GetInstanceName().AsCString();
975 user_id_t id = m_opaque_sp->GetID();
976 strm.Printf("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
977 } else
978 strm.PutCString("No value");
979
980 return true;
981}
982
983user_id_t SBDebugger::GetID() {
984 return (m_opaque_sp ? m_opaque_sp->GetID() : LLDB_INVALID_UID);
985}
986
987SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
988 SBError sb_error;
989 if (m_opaque_sp) {
990 if (platform_name_cstr && platform_name_cstr[0]) {
991 ConstString platform_name(platform_name_cstr);
992 PlatformSP platform_sp(Platform::Find(platform_name));
993
994 if (platform_sp) {
995 // Already have a platform with this name, just select it
996 m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
997 } else {
998 // We don't have a platform by this name yet, create one
999 platform_sp = Platform::Create(platform_name, sb_error.ref());
1000 if (platform_sp) {
1001 // We created the platform, now append and select it
1002 bool make_selected = true;
1003 m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected);
Greg Clayton6920b522012-08-22 18:39:03 +00001004 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 }
1006 } else {
1007 sb_error.ref().SetErrorString("invalid platform name");
Greg Clayton6920b522012-08-22 18:39:03 +00001008 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 } else {
1010 sb_error.ref().SetErrorString("invalid debugger");
1011 }
1012 return sb_error;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001013}
1014
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015bool SBDebugger::SetCurrentPlatformSDKRoot(const char *sysroot) {
1016 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1017 if (m_opaque_sp) {
1018 PlatformSP platform_sp(
1019 m_opaque_sp->GetPlatformList().GetSelectedPlatform());
Greg Claytona7015092010-09-18 01:14:36 +00001020
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 if (platform_sp) {
1022 if (log && sysroot)
1023 log->Printf("SBDebugger::SetCurrentPlatformSDKRoot (\"%s\")", sysroot);
1024 platform_sp->SetSDKRootDirectory(ConstString(sysroot));
1025 return true;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001026 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001027 }
1028 return false;
Greg Claytona7015092010-09-18 01:14:36 +00001029}
1030
Kate Stoneb9c1b512016-09-06 20:57:50 +00001031bool SBDebugger::GetCloseInputOnEOF() const {
1032 return (m_opaque_sp ? m_opaque_sp->GetCloseInputOnEOF() : false);
Jim Inghame40e4212010-08-30 19:44:40 +00001033}
1034
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035void SBDebugger::SetCloseInputOnEOF(bool b) {
1036 if (m_opaque_sp)
1037 m_opaque_sp->SetCloseInputOnEOF(b);
Jim Inghame40e4212010-08-30 19:44:40 +00001038}
1039
Kate Stoneb9c1b512016-09-06 20:57:50 +00001040SBTypeCategory SBDebugger::GetCategory(const char *category_name) {
1041 if (!category_name || *category_name == 0)
1042 return SBTypeCategory();
1043
1044 TypeCategoryImplSP category_sp;
1045
1046 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1047 category_sp, false))
1048 return SBTypeCategory(category_sp);
1049 else
1050 return SBTypeCategory();
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001051}
1052
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053SBTypeCategory SBDebugger::GetCategory(lldb::LanguageType lang_type) {
1054 TypeCategoryImplSP category_sp;
1055 if (DataVisualization::Categories::GetCategory(lang_type, category_sp))
1056 return SBTypeCategory(category_sp);
1057 else
1058 return SBTypeCategory();
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001059}
1060
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061SBTypeCategory SBDebugger::CreateCategory(const char *category_name) {
1062 if (!category_name || *category_name == 0)
1063 return SBTypeCategory();
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001064
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 TypeCategoryImplSP category_sp;
1066
1067 if (DataVisualization::Categories::GetCategory(ConstString(category_name),
1068 category_sp, true))
1069 return SBTypeCategory(category_sp);
1070 else
1071 return SBTypeCategory();
Caroline Ticedde9cff2010-09-20 05:20:02 +00001072}
Caroline Ticeefed6132010-11-19 20:47:54 +00001073
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074bool SBDebugger::DeleteCategory(const char *category_name) {
1075 if (!category_name || *category_name == 0)
1076 return false;
1077
1078 return DataVisualization::Categories::Delete(ConstString(category_name));
Caroline Ticeefed6132010-11-19 20:47:54 +00001079}
Greg Clayton2289fa42011-04-30 01:09:13 +00001080
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081uint32_t SBDebugger::GetNumCategories() {
1082 return DataVisualization::Categories::GetCount();
Greg Clayton2289fa42011-04-30 01:09:13 +00001083}
1084
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085SBTypeCategory SBDebugger::GetCategoryAtIndex(uint32_t index) {
1086 return SBTypeCategory(
1087 DataVisualization::Categories::GetCategoryAtIndex(index));
1088}
1089
1090SBTypeCategory SBDebugger::GetDefaultCategory() {
1091 return GetCategory("default");
1092}
1093
1094SBTypeFormat SBDebugger::GetFormatForType(SBTypeNameSpecifier type_name) {
1095 SBTypeCategory default_category_sb = GetDefaultCategory();
1096 if (default_category_sb.GetEnabled())
1097 return default_category_sb.GetFormatForType(type_name);
1098 return SBTypeFormat();
1099}
1100
1101#ifndef LLDB_DISABLE_PYTHON
1102SBTypeSummary SBDebugger::GetSummaryForType(SBTypeNameSpecifier type_name) {
1103 if (!type_name.IsValid())
1104 return SBTypeSummary();
1105 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1106}
1107#endif // LLDB_DISABLE_PYTHON
1108
1109SBTypeFilter SBDebugger::GetFilterForType(SBTypeNameSpecifier type_name) {
1110 if (!type_name.IsValid())
1111 return SBTypeFilter();
1112 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1113}
1114
1115#ifndef LLDB_DISABLE_PYTHON
1116SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) {
1117 if (!type_name.IsValid())
1118 return SBTypeSynthetic();
1119 return SBTypeSynthetic(
1120 DataVisualization::GetSyntheticForType(type_name.GetSP()));
1121}
1122#endif // LLDB_DISABLE_PYTHON
1123
1124bool SBDebugger::EnableLog(const char *channel, const char **categories) {
1125 if (m_opaque_sp) {
1126 uint32_t log_options =
1127 LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1128 StreamString errors;
1129 return m_opaque_sp->EnableLog(channel, categories, nullptr, log_options,
1130 errors);
1131 } else
Greg Claytonf3dd93c2011-06-17 03:31:01 +00001132 return false;
1133}
1134
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135void SBDebugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
1136 void *baton) {
1137 if (m_opaque_sp) {
1138 return m_opaque_sp->SetLoggingCallback(log_callback, baton);
1139 }
Jim Ingham4f02b222012-02-22 22:49:20 +00001140}