blob: 0c957a677df20b0a13393df36258f4e43f150153 [file] [log] [blame]
Chris Lattner24943d22010-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
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000010#include "lldb/API/SBDebugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000011
Greg Claytonb3448432011-03-24 21:19:54 +000012#include "lldb/lldb-private.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000014#include "lldb/API/SBListener.h"
15#include "lldb/API/SBBroadcaster.h"
16#include "lldb/API/SBCommandInterpreter.h"
17#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton180546b2011-04-30 01:09:13 +000018#include "lldb/API/SBError.h"
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000019#include "lldb/API/SBEvent.h"
20#include "lldb/API/SBFrame.h"
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000021#include "lldb/API/SBInputReader.h"
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000022#include "lldb/API/SBProcess.h"
23#include "lldb/API/SBSourceManager.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000024#include "lldb/API/SBStream.h"
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000025#include "lldb/API/SBStringList.h"
26#include "lldb/API/SBTarget.h"
27#include "lldb/API/SBThread.h"
Enrico Granata16376ed2012-02-15 02:34:21 +000028#include "lldb/API/SBTypeCategory.h"
29#include "lldb/API/SBTypeFormat.h"
30#include "lldb/API/SBTypeFilter.h"
31#include "lldb/API/SBTypeNameSpecifier.h"
32#include "lldb/API/SBTypeSummary.h"
33#include "lldb/API/SBTypeSynthetic.h"
34
35
36#include "lldb/Core/DataVisualization.h"
Greg Clayton887aa282010-10-11 01:05:37 +000037#include "lldb/Core/Debugger.h"
38#include "lldb/Core/State.h"
39#include "lldb/Interpreter/Args.h"
40#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton3e8c25f2011-09-24 00:52:29 +000041#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Clayton887aa282010-10-11 01:05:37 +000042#include "lldb/Target/Process.h"
43#include "lldb/Target/TargetList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000044
45using namespace lldb;
46using namespace lldb_private;
47
48void
49SBDebugger::Initialize ()
50{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +000051 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000052
53 if (log)
54 log->Printf ("SBDebugger::Initialize ()");
55
Greg Claytone86cbb92011-03-22 01:14:58 +000056 SBCommandInterpreter::InitializeSWIG ();
57
Chris Lattner24943d22010-06-08 16:52:24 +000058 Debugger::Initialize();
59}
60
61void
62SBDebugger::Terminate ()
63{
64 Debugger::Terminate();
65}
66
Greg Clayton43490d12010-07-30 20:12:55 +000067void
68SBDebugger::Clear ()
69{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +000070 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000071
72 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000073 log->Printf ("SBDebugger(%p)::Clear ()", m_opaque_sp.get());
Caroline Ticec0446862010-12-20 18:35:50 +000074
75 if (m_opaque_sp)
76 m_opaque_sp->CleanUpInputReaders ();
Caroline Tice7826c882010-10-26 03:11:13 +000077
Greg Clayton43490d12010-07-30 20:12:55 +000078 m_opaque_sp.reset();
79}
80
Greg Clayton63094e02010-06-23 01:19:29 +000081SBDebugger
82SBDebugger::Create()
83{
Jim Ingham6c530f22012-02-21 02:23:08 +000084 return SBDebugger::Create(false, NULL, NULL);
Jim Ingham558dd5b2011-08-13 00:22:20 +000085}
86
87SBDebugger
88SBDebugger::Create(bool source_init_files)
89{
Jim Ingham6c530f22012-02-21 02:23:08 +000090 return SBDebugger::Create (source_init_files, NULL, NULL);
91}
92
93SBDebugger
94SBDebugger::Create(bool source_init_files, lldb::LogOutputCallback callback, void *baton)
95
96{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +000097 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000098
Greg Clayton63094e02010-06-23 01:19:29 +000099 SBDebugger debugger;
Jim Ingham6c530f22012-02-21 02:23:08 +0000100 debugger.reset(Debugger::CreateInstance(callback, baton));
Caroline Tice7826c882010-10-26 03:11:13 +0000101
102 if (log)
103 {
104 SBStream sstr;
105 debugger.GetDescription (sstr);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000106 log->Printf ("SBDebugger::Create () => SBDebugger(%p): %s", debugger.m_opaque_sp.get(), sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000107 }
108
Jim Ingham558dd5b2011-08-13 00:22:20 +0000109 SBCommandInterpreter interp = debugger.GetCommandInterpreter();
110 if (source_init_files)
111 {
112 interp.get()->SkipLLDBInitFiles(false);
113 interp.get()->SkipAppInitFiles (false);
114 SBCommandReturnObject result;
115 interp.SourceInitFileInHomeDirectory(result);
116 }
117 else
118 {
119 interp.get()->SkipLLDBInitFiles(true);
120 interp.get()->SkipAppInitFiles (true);
121 }
Greg Clayton63094e02010-06-23 01:19:29 +0000122 return debugger;
123}
124
Caroline Ticec4ed12f2011-01-22 01:02:07 +0000125void
126SBDebugger::Destroy (SBDebugger &debugger)
127{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000128 LogSP log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticec4ed12f2011-01-22 01:02:07 +0000129
130 if (log)
131 {
132 SBStream sstr;
133 debugger.GetDescription (sstr);
134 log->Printf ("SBDebugger::Destroy () => SBDebugger(%p): %s", debugger.m_opaque_sp.get(), sstr.GetData());
135 }
136
137 Debugger::Destroy (debugger.m_opaque_sp);
138
139 if (debugger.m_opaque_sp.get() != NULL)
140 debugger.m_opaque_sp.reset();
141}
142
Greg Claytonc5486872011-12-15 04:38:41 +0000143void
144SBDebugger::MemoryPressureDetected ()
145{
Greg Clayton860b9ea2012-04-09 20:22:01 +0000146 // Since this function can be call asynchronously, we allow it to be
147 // non-mandatory. We have seen deadlocks with this function when called
148 // so we need to safeguard against this until we can determine what is
149 // causing the deadlocks.
Jim Inghamee306fa2012-06-07 19:08:07 +0000150 LogSP log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
151
Greg Clayton860b9ea2012-04-09 20:22:01 +0000152 const bool mandatory = false;
Jim Inghamee306fa2012-06-07 19:08:07 +0000153 if (log)
154 {
155 log->Printf ("SBDebugger::MemoryPressureDetected (), mandatory = %d", mandatory);
156 }
157
Greg Clayton860b9ea2012-04-09 20:22:01 +0000158 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonc5486872011-12-15 04:38:41 +0000159}
160
Greg Clayton63094e02010-06-23 01:19:29 +0000161SBDebugger::SBDebugger () :
162 m_opaque_sp ()
163{
164}
165
Enrico Granata3370f0c2011-08-19 23:56:34 +0000166SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp) :
167 m_opaque_sp(debugger_sp)
168{
169}
170
Greg Clayton538eb822010-11-05 23:17:00 +0000171SBDebugger::SBDebugger(const SBDebugger &rhs) :
172 m_opaque_sp (rhs.m_opaque_sp)
173{
174}
175
176SBDebugger &
177SBDebugger::operator = (const SBDebugger &rhs)
178{
179 if (this != &rhs)
180 {
181 m_opaque_sp = rhs.m_opaque_sp;
182 }
183 return *this;
184}
185
Greg Clayton63094e02010-06-23 01:19:29 +0000186SBDebugger::~SBDebugger ()
187{
188}
189
190bool
191SBDebugger::IsValid() const
192{
193 return m_opaque_sp.get() != NULL;
194}
195
196
Chris Lattner24943d22010-06-08 16:52:24 +0000197void
198SBDebugger::SetAsync (bool b)
199{
Greg Clayton63094e02010-06-23 01:19:29 +0000200 if (m_opaque_sp)
201 m_opaque_sp->SetAsyncExecution(b);
Chris Lattner24943d22010-06-08 16:52:24 +0000202}
203
Jim Ingham83dd2032011-09-13 23:25:31 +0000204bool
205SBDebugger::GetAsync ()
206{
207 if (m_opaque_sp)
208 return m_opaque_sp->GetAsyncExecution();
209 else
210 return false;
211}
212
Greg Clayton887aa282010-10-11 01:05:37 +0000213void
214SBDebugger::SkipLLDBInitFiles (bool b)
215{
216 if (m_opaque_sp)
217 m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles (b);
218}
219
Jim Ingham574c3d62011-08-12 23:34:31 +0000220void
221SBDebugger::SkipAppInitFiles (bool b)
222{
223 if (m_opaque_sp)
224 m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles (b);
225}
226
Chris Lattner24943d22010-06-08 16:52:24 +0000227// Shouldn't really be settable after initialization as this could cause lots of problems; don't want users
228// trying to switch modes in the middle of a debugging session.
229void
230SBDebugger::SetInputFileHandle (FILE *fh, bool transfer_ownership)
231{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000232 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000233
234 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +0000235 log->Printf ("SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)", m_opaque_sp.get(),
236 fh, transfer_ownership);
Caroline Tice7826c882010-10-26 03:11:13 +0000237
Greg Clayton63094e02010-06-23 01:19:29 +0000238 if (m_opaque_sp)
239 m_opaque_sp->SetInputFileHandle (fh, transfer_ownership);
Chris Lattner24943d22010-06-08 16:52:24 +0000240}
241
242void
243SBDebugger::SetOutputFileHandle (FILE *fh, bool transfer_ownership)
244{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000245 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000246
247
248 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +0000249 log->Printf ("SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)", m_opaque_sp.get(),
250 fh, transfer_ownership);
Caroline Tice7826c882010-10-26 03:11:13 +0000251
Greg Clayton63094e02010-06-23 01:19:29 +0000252 if (m_opaque_sp)
253 m_opaque_sp->SetOutputFileHandle (fh, transfer_ownership);
Chris Lattner24943d22010-06-08 16:52:24 +0000254}
255
256void
257SBDebugger::SetErrorFileHandle (FILE *fh, bool transfer_ownership)
258{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000259 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000260
261
262 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +0000263 log->Printf ("SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)", m_opaque_sp.get(),
264 fh, transfer_ownership);
Caroline Tice7826c882010-10-26 03:11:13 +0000265
Greg Clayton63094e02010-06-23 01:19:29 +0000266 if (m_opaque_sp)
267 m_opaque_sp->SetErrorFileHandle (fh, transfer_ownership);
Chris Lattner24943d22010-06-08 16:52:24 +0000268}
269
270FILE *
271SBDebugger::GetInputFileHandle ()
272{
Greg Clayton63094e02010-06-23 01:19:29 +0000273 if (m_opaque_sp)
Greg Clayton58928562011-02-09 01:08:52 +0000274 return m_opaque_sp->GetInputFile().GetStream();
Greg Clayton63094e02010-06-23 01:19:29 +0000275 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000276}
277
278FILE *
279SBDebugger::GetOutputFileHandle ()
280{
Greg Clayton63094e02010-06-23 01:19:29 +0000281 if (m_opaque_sp)
Greg Clayton58928562011-02-09 01:08:52 +0000282 return m_opaque_sp->GetOutputFile().GetStream();
Greg Clayton63094e02010-06-23 01:19:29 +0000283 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000284}
285
286FILE *
287SBDebugger::GetErrorFileHandle ()
288{
Greg Clayton63094e02010-06-23 01:19:29 +0000289 if (m_opaque_sp)
Greg Clayton58928562011-02-09 01:08:52 +0000290 return m_opaque_sp->GetErrorFile().GetStream();
Greg Clayton63094e02010-06-23 01:19:29 +0000291 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000292}
293
Jim Inghamb1087a22012-11-30 20:23:19 +0000294void
295SBDebugger::SaveInputTerminalState()
296{
297 if (m_opaque_sp)
298 m_opaque_sp->SaveInputTerminalState();
299}
300
301void
302SBDebugger::RestoreInputTerminalState()
303{
304 if (m_opaque_sp)
305 m_opaque_sp->RestoreInputTerminalState();
306
307}
Chris Lattner24943d22010-06-08 16:52:24 +0000308SBCommandInterpreter
309SBDebugger::GetCommandInterpreter ()
310{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000311 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000312
Greg Clayton63094e02010-06-23 01:19:29 +0000313 SBCommandInterpreter sb_interpreter;
314 if (m_opaque_sp)
315 sb_interpreter.reset (&m_opaque_sp->GetCommandInterpreter());
Caroline Tice7826c882010-10-26 03:11:13 +0000316
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000317 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000318 log->Printf ("SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000319 m_opaque_sp.get(), sb_interpreter.get());
320
Chris Lattner24943d22010-06-08 16:52:24 +0000321 return sb_interpreter;
322}
323
324void
325SBDebugger::HandleCommand (const char *command)
326{
Greg Clayton63094e02010-06-23 01:19:29 +0000327 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000328 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000329 TargetSP target_sp (m_opaque_sp->GetSelectedTarget());
330 Mutex::Locker api_locker;
331 if (target_sp)
Jim Ingham1b584eb2012-05-04 23:02:50 +0000332 api_locker.Lock(target_sp->GetAPIMutex());
Greg Claytonbdcda462010-12-20 20:49:23 +0000333
Greg Clayton63094e02010-06-23 01:19:29 +0000334 SBCommandInterpreter sb_interpreter(GetCommandInterpreter ());
335 SBCommandReturnObject result;
336
337 sb_interpreter.HandleCommand (command, result, false);
338
339 if (GetErrorFileHandle() != NULL)
340 result.PutError (GetErrorFileHandle());
341 if (GetOutputFileHandle() != NULL)
342 result.PutOutput (GetOutputFileHandle());
343
344 if (m_opaque_sp->GetAsyncExecution() == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000345 {
Greg Clayton63094e02010-06-23 01:19:29 +0000346 SBProcess process(GetCommandInterpreter().GetProcess ());
Greg Clayton334d33a2012-01-30 07:41:31 +0000347 ProcessSP process_sp (process.GetSP());
348 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000349 {
Greg Clayton63094e02010-06-23 01:19:29 +0000350 EventSP event_sp;
351 Listener &lldb_listener = m_opaque_sp->GetListener();
Greg Clayton334d33a2012-01-30 07:41:31 +0000352 while (lldb_listener.GetNextEventForBroadcaster (process_sp.get(), event_sp))
Greg Clayton63094e02010-06-23 01:19:29 +0000353 {
354 SBEvent event(event_sp);
355 HandleProcessEvent (process, event, GetOutputFileHandle(), GetErrorFileHandle());
356 }
Chris Lattner24943d22010-06-08 16:52:24 +0000357 }
358 }
359 }
360}
361
362SBListener
363SBDebugger::GetListener ()
364{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000365 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000366
Greg Clayton63094e02010-06-23 01:19:29 +0000367 SBListener sb_listener;
368 if (m_opaque_sp)
369 sb_listener.reset(&m_opaque_sp->GetListener(), false);
Caroline Tice7826c882010-10-26 03:11:13 +0000370
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000371 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000372 log->Printf ("SBDebugger(%p)::GetListener () => SBListener(%p)", m_opaque_sp.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000373 sb_listener.get());
374
Chris Lattner24943d22010-06-08 16:52:24 +0000375 return sb_listener;
376}
377
378void
379SBDebugger::HandleProcessEvent (const SBProcess &process, const SBEvent &event, FILE *out, FILE *err)
380{
Greg Claytonbdcda462010-12-20 20:49:23 +0000381 if (!process.IsValid())
382 return;
Chris Lattner24943d22010-06-08 16:52:24 +0000383
Greg Clayton334d33a2012-01-30 07:41:31 +0000384 TargetSP target_sp (process.GetTarget().GetSP());
385 if (!target_sp)
386 return;
387
Greg Claytonbdcda462010-12-20 20:49:23 +0000388 const uint32_t event_type = event.GetType();
389 char stdio_buffer[1024];
390 size_t len;
Chris Lattner24943d22010-06-08 16:52:24 +0000391
Greg Clayton334d33a2012-01-30 07:41:31 +0000392 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonbdcda462010-12-20 20:49:23 +0000393
394 if (event_type & (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged))
Chris Lattner24943d22010-06-08 16:52:24 +0000395 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000396 // Drain stdout when we stop just in case we have any bytes
397 while ((len = process.GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0)
398 if (out != NULL)
399 ::fwrite (stdio_buffer, 1, len, out);
400 }
401
402 if (event_type & (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged))
403 {
404 // Drain stderr when we stop just in case we have any bytes
405 while ((len = process.GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0)
406 if (err != NULL)
407 ::fwrite (stdio_buffer, 1, len, err);
408 }
409
410 if (event_type & Process::eBroadcastBitStateChanged)
411 {
412 StateType event_state = SBProcess::GetStateFromEvent (event);
413
414 if (event_state == eStateInvalid)
415 return;
416
417 bool is_stopped = StateIsStoppedState (event_state);
418 if (!is_stopped)
419 process.ReportEventState (event, out);
Chris Lattner24943d22010-06-08 16:52:24 +0000420 }
421}
422
Jim Inghamcc637462011-09-13 00:29:56 +0000423SBSourceManager
Chris Lattner24943d22010-06-08 16:52:24 +0000424SBDebugger::GetSourceManager ()
425{
Jim Inghamcc637462011-09-13 00:29:56 +0000426 SBSourceManager sb_source_manager (*this);
427 return sb_source_manager;
Chris Lattner24943d22010-06-08 16:52:24 +0000428}
429
430
431bool
432SBDebugger::GetDefaultArchitecture (char *arch_name, size_t arch_name_len)
433{
434 if (arch_name && arch_name_len)
435 {
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000436 ArchSpec default_arch = Target::GetDefaultArchitecture ();
Caroline Tice5bc8c972010-09-20 20:44:43 +0000437
Chris Lattner24943d22010-06-08 16:52:24 +0000438 if (default_arch.IsValid())
439 {
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000440 const std::string &triple_str = default_arch.GetTriple().str();
441 if (!triple_str.empty())
442 ::snprintf (arch_name, arch_name_len, "%s", triple_str.c_str());
443 else
444 ::snprintf (arch_name, arch_name_len, "%s", default_arch.GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000445 return true;
446 }
447 }
448 if (arch_name && arch_name_len)
449 arch_name[0] = '\0';
450 return false;
451}
452
453
454bool
455SBDebugger::SetDefaultArchitecture (const char *arch_name)
456{
457 if (arch_name)
458 {
Greg Claytonb170aee2012-05-08 01:45:38 +0000459 ArchSpec arch (arch_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000460 if (arch.IsValid())
461 {
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000462 Target::SetDefaultArchitecture (arch);
Chris Lattner24943d22010-06-08 16:52:24 +0000463 return true;
464 }
465 }
466 return false;
467}
468
469ScriptLanguage
470SBDebugger::GetScriptingLanguage (const char *script_language_name)
471{
Greg Claytonbdcda462010-12-20 20:49:23 +0000472
Chris Lattner24943d22010-06-08 16:52:24 +0000473 return Args::StringToScriptLanguage (script_language_name,
474 eScriptLanguageDefault,
475 NULL);
476}
Chris Lattner24943d22010-06-08 16:52:24 +0000477
478const char *
479SBDebugger::GetVersionString ()
480{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000481 return GetVersion();
Chris Lattner24943d22010-06-08 16:52:24 +0000482}
483
484const char *
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000485SBDebugger::StateAsCString (StateType state)
Chris Lattner24943d22010-06-08 16:52:24 +0000486{
Caroline Ticea2cffd32011-04-25 22:05:51 +0000487 return lldb_private::StateAsCString (state);
Chris Lattner24943d22010-06-08 16:52:24 +0000488}
489
490bool
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000491SBDebugger::StateIsRunningState (StateType state)
Chris Lattner24943d22010-06-08 16:52:24 +0000492{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000493 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000494
Caroline Ticea2cffd32011-04-25 22:05:51 +0000495 const bool result = lldb_private::StateIsRunningState (state);
Greg Claytona66ba462010-10-30 04:51:46 +0000496 if (log)
497 log->Printf ("SBDebugger::StateIsRunningState (state=%s) => %i",
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000498 StateAsCString (state), result);
Greg Claytona66ba462010-10-30 04:51:46 +0000499
500 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000501}
502
503bool
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000504SBDebugger::StateIsStoppedState (StateType state)
Chris Lattner24943d22010-06-08 16:52:24 +0000505{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000506 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000507
Greg Clayton20206082011-11-17 01:23:07 +0000508 const bool result = lldb_private::StateIsStoppedState (state, false);
Caroline Tice7826c882010-10-26 03:11:13 +0000509 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000510 log->Printf ("SBDebugger::StateIsStoppedState (state=%s) => %i",
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000511 StateAsCString (state), result);
Caroline Tice7826c882010-10-26 03:11:13 +0000512
Greg Claytona66ba462010-10-30 04:51:46 +0000513 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000514}
515
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000516lldb::SBTarget
517SBDebugger::CreateTarget (const char *filename,
518 const char *target_triple,
519 const char *platform_name,
520 bool add_dependent_modules,
521 lldb::SBError& sb_error)
522{
523 SBTarget sb_target;
Greg Clayton334d33a2012-01-30 07:41:31 +0000524 TargetSP target_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000525 if (m_opaque_sp)
526 {
527 sb_error.Clear();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000528 OptionGroupPlatform platform_options (false);
529 platform_options.SetPlatformName (platform_name);
530
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000531 sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
Greg Claytoned0a0fb2012-10-18 16:33:33 +0000532 filename,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000533 target_triple,
534 add_dependent_modules,
535 &platform_options,
536 target_sp);
537
538 if (sb_error.Success())
Greg Clayton334d33a2012-01-30 07:41:31 +0000539 sb_target.SetSP (target_sp);
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000540 }
541 else
542 {
543 sb_error.SetErrorString("invalid target");
544 }
545
546 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
547 if (log)
548 {
549 log->Printf ("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, platform_name=%s, add_dependent_modules=%u, error=%s) => SBTarget(%p)",
550 m_opaque_sp.get(),
551 filename,
552 target_triple,
553 platform_name,
554 add_dependent_modules,
555 sb_error.GetCString(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000556 target_sp.get());
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000557 }
558
559 return sb_target;
560}
Chris Lattner24943d22010-06-08 16:52:24 +0000561
562SBTarget
563SBDebugger::CreateTargetWithFileAndTargetTriple (const char *filename,
564 const char *target_triple)
565{
Greg Clayton334d33a2012-01-30 07:41:31 +0000566 SBTarget sb_target;
567 TargetSP target_sp;
Greg Clayton63094e02010-06-23 01:19:29 +0000568 if (m_opaque_sp)
569 {
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000570 const bool add_dependent_modules = true;
571 Error error (m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
Greg Claytoned0a0fb2012-10-18 16:33:33 +0000572 filename,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000573 target_triple,
574 add_dependent_modules,
575 NULL,
576 target_sp));
Greg Clayton334d33a2012-01-30 07:41:31 +0000577 sb_target.SetSP (target_sp);
Greg Clayton63094e02010-06-23 01:19:29 +0000578 }
Greg Claytona66ba462010-10-30 04:51:46 +0000579
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000580 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000581 if (log)
582 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000583 log->Printf ("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple (filename=\"%s\", triple=%s) => SBTarget(%p)",
Greg Clayton334d33a2012-01-30 07:41:31 +0000584 m_opaque_sp.get(), filename, target_triple, target_sp.get());
Greg Claytona66ba462010-10-30 04:51:46 +0000585 }
586
Greg Clayton334d33a2012-01-30 07:41:31 +0000587 return sb_target;
Chris Lattner24943d22010-06-08 16:52:24 +0000588}
589
590SBTarget
Greg Clayton940b1032011-02-23 00:35:02 +0000591SBDebugger::CreateTargetWithFileAndArch (const char *filename, const char *arch_cstr)
Chris Lattner24943d22010-06-08 16:52:24 +0000592{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000593 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000594
Greg Clayton334d33a2012-01-30 07:41:31 +0000595 SBTarget sb_target;
596 TargetSP target_sp;
Greg Clayton63094e02010-06-23 01:19:29 +0000597 if (m_opaque_sp)
598 {
Greg Clayton63094e02010-06-23 01:19:29 +0000599 Error error;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000600 const bool add_dependent_modules = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000601
Greg Claytoned0a0fb2012-10-18 16:33:33 +0000602 error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
603 filename,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000604 arch_cstr,
605 add_dependent_modules,
606 NULL,
607 target_sp);
Greg Clayton63094e02010-06-23 01:19:29 +0000608
609 if (error.Success())
610 {
Jim Inghamc8332952010-08-26 21:32:51 +0000611 m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
Greg Clayton334d33a2012-01-30 07:41:31 +0000612 sb_target.SetSP (target_sp);
Greg Clayton63094e02010-06-23 01:19:29 +0000613 }
Chris Lattner24943d22010-06-08 16:52:24 +0000614 }
Caroline Tice7826c882010-10-26 03:11:13 +0000615
616 if (log)
617 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000618 log->Printf ("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", arch=%s) => SBTarget(%p)",
Greg Clayton334d33a2012-01-30 07:41:31 +0000619 m_opaque_sp.get(), filename, arch_cstr, target_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000620 }
621
Greg Clayton334d33a2012-01-30 07:41:31 +0000622 return sb_target;
Greg Clayton63094e02010-06-23 01:19:29 +0000623}
624
625SBTarget
626SBDebugger::CreateTarget (const char *filename)
627{
Greg Clayton334d33a2012-01-30 07:41:31 +0000628 SBTarget sb_target;
629 TargetSP target_sp;
Greg Clayton63094e02010-06-23 01:19:29 +0000630 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000631 {
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000632 ArchSpec arch = Target::GetDefaultArchitecture ();
Greg Clayton63094e02010-06-23 01:19:29 +0000633 Error error;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000634 const bool add_dependent_modules = true;
Greg Clayton63094e02010-06-23 01:19:29 +0000635
Greg Claytonb1db6582012-03-20 18:34:04 +0000636 PlatformSP platform_sp(m_opaque_sp->GetPlatformList().GetSelectedPlatform());
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000637 error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
Greg Claytoned0a0fb2012-10-18 16:33:33 +0000638 filename,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000639 arch,
640 add_dependent_modules,
Greg Claytonb1db6582012-03-20 18:34:04 +0000641 platform_sp,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000642 target_sp);
Greg Clayton63094e02010-06-23 01:19:29 +0000643
644 if (error.Success())
645 {
Jim Inghamc8332952010-08-26 21:32:51 +0000646 m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
Greg Clayton334d33a2012-01-30 07:41:31 +0000647 sb_target.SetSP (target_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000648 }
649 }
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000650 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000651 if (log)
652 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000653 log->Printf ("SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
Greg Clayton334d33a2012-01-30 07:41:31 +0000654 m_opaque_sp.get(), filename, target_sp.get());
Greg Claytona66ba462010-10-30 04:51:46 +0000655 }
Greg Clayton334d33a2012-01-30 07:41:31 +0000656 return sb_target;
Chris Lattner24943d22010-06-08 16:52:24 +0000657}
658
Johnny Chen409646d2011-06-15 21:24:24 +0000659bool
660SBDebugger::DeleteTarget (lldb::SBTarget &target)
661{
662 bool result = false;
Greg Clayton334d33a2012-01-30 07:41:31 +0000663 if (m_opaque_sp)
Johnny Chen409646d2011-06-15 21:24:24 +0000664 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000665 TargetSP target_sp(target.GetSP());
666 if (target_sp)
667 {
668 // No need to lock, the target list is thread safe
669 result = m_opaque_sp->GetTargetList().DeleteTarget (target_sp);
670 target_sp->Destroy();
671 target.Clear();
Greg Clayton860b9ea2012-04-09 20:22:01 +0000672 const bool mandatory = true;
673 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton334d33a2012-01-30 07:41:31 +0000674 }
Johnny Chen409646d2011-06-15 21:24:24 +0000675 }
676
677 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
678 if (log)
679 {
680 log->Printf ("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i", m_opaque_sp.get(), target.m_opaque_sp.get(), result);
681 }
682
683 return result;
684}
Chris Lattner24943d22010-06-08 16:52:24 +0000685SBTarget
686SBDebugger::GetTargetAtIndex (uint32_t idx)
687{
Greg Clayton63094e02010-06-23 01:19:29 +0000688 SBTarget sb_target;
689 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000690 {
691 // No need to lock, the target list is thread safe
Greg Clayton334d33a2012-01-30 07:41:31 +0000692 sb_target.SetSP (m_opaque_sp->GetTargetList().GetTargetAtIndex (idx));
Greg Claytonbdcda462010-12-20 20:49:23 +0000693 }
Chris Lattner24943d22010-06-08 16:52:24 +0000694 return sb_target;
695}
696
Jim Inghamf92ddcc2012-05-08 23:06:07 +0000697uint32_t
698SBDebugger::GetIndexOfTarget (lldb::SBTarget target)
699{
700
701 lldb::TargetSP target_sp = target.GetSP();
702 if (!target_sp)
703 return UINT32_MAX;
704
705 if (!m_opaque_sp)
706 return UINT32_MAX;
707
708 return m_opaque_sp->GetTargetList().GetIndexOfTarget (target.GetSP());
709}
710
Chris Lattner24943d22010-06-08 16:52:24 +0000711SBTarget
712SBDebugger::FindTargetWithProcessID (pid_t pid)
713{
Greg Clayton63094e02010-06-23 01:19:29 +0000714 SBTarget sb_target;
715 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000716 {
717 // No need to lock, the target list is thread safe
Greg Clayton334d33a2012-01-30 07:41:31 +0000718 sb_target.SetSP (m_opaque_sp->GetTargetList().FindTargetWithProcessID (pid));
Greg Claytonbdcda462010-12-20 20:49:23 +0000719 }
Chris Lattner24943d22010-06-08 16:52:24 +0000720 return sb_target;
721}
722
723SBTarget
724SBDebugger::FindTargetWithFileAndArch (const char *filename, const char *arch_name)
725{
Greg Clayton63094e02010-06-23 01:19:29 +0000726 SBTarget sb_target;
727 if (m_opaque_sp && filename && filename[0])
728 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000729 // No need to lock, the target list is thread safe
Greg Claytonf15996e2011-04-07 22:46:35 +0000730 ArchSpec arch (arch_name, m_opaque_sp->GetPlatformList().GetSelectedPlatform().get());
Greg Clayton537a7a82010-10-20 20:54:39 +0000731 TargetSP target_sp (m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture (FileSpec(filename, false), arch_name ? &arch : NULL));
Greg Clayton334d33a2012-01-30 07:41:31 +0000732 sb_target.SetSP (target_sp);
Greg Clayton63094e02010-06-23 01:19:29 +0000733 }
734 return sb_target;
Chris Lattner24943d22010-06-08 16:52:24 +0000735}
736
737SBTarget
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000738SBDebugger::FindTargetWithLLDBProcess (const ProcessSP &process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000739{
Greg Clayton63094e02010-06-23 01:19:29 +0000740 SBTarget sb_target;
741 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000742 {
743 // No need to lock, the target list is thread safe
Greg Clayton334d33a2012-01-30 07:41:31 +0000744 sb_target.SetSP (m_opaque_sp->GetTargetList().FindTargetWithProcess (process_sp.get()));
Greg Claytonbdcda462010-12-20 20:49:23 +0000745 }
Chris Lattner24943d22010-06-08 16:52:24 +0000746 return sb_target;
747}
748
749
750uint32_t
751SBDebugger::GetNumTargets ()
752{
Greg Clayton63094e02010-06-23 01:19:29 +0000753 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000754 {
755 // No need to lock, the target list is thread safe
Greg Clayton63094e02010-06-23 01:19:29 +0000756 return m_opaque_sp->GetTargetList().GetNumTargets ();
Greg Claytonbdcda462010-12-20 20:49:23 +0000757 }
Greg Clayton63094e02010-06-23 01:19:29 +0000758 return 0;
759}
Chris Lattner24943d22010-06-08 16:52:24 +0000760
761SBTarget
Jim Inghamc8332952010-08-26 21:32:51 +0000762SBDebugger::GetSelectedTarget ()
Chris Lattner24943d22010-06-08 16:52:24 +0000763{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000764 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000765
Greg Clayton63094e02010-06-23 01:19:29 +0000766 SBTarget sb_target;
Greg Clayton334d33a2012-01-30 07:41:31 +0000767 TargetSP target_sp;
Greg Clayton63094e02010-06-23 01:19:29 +0000768 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000769 {
770 // No need to lock, the target list is thread safe
Greg Clayton334d33a2012-01-30 07:41:31 +0000771 target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget ();
772 sb_target.SetSP (target_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000773 }
Caroline Tice7826c882010-10-26 03:11:13 +0000774
775 if (log)
776 {
777 SBStream sstr;
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000778 sb_target.GetDescription (sstr, eDescriptionLevelBrief);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000779 log->Printf ("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s", m_opaque_sp.get(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000780 target_sp.get(), sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000781 }
782
Chris Lattner24943d22010-06-08 16:52:24 +0000783 return sb_target;
784}
785
786void
Jim Ingham83dd2032011-09-13 23:25:31 +0000787SBDebugger::SetSelectedTarget (SBTarget &sb_target)
788{
789 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
790
Greg Clayton334d33a2012-01-30 07:41:31 +0000791 TargetSP target_sp (sb_target.GetSP());
Jim Ingham83dd2032011-09-13 23:25:31 +0000792 if (m_opaque_sp)
793 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000794 m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
Jim Ingham83dd2032011-09-13 23:25:31 +0000795 }
796 if (log)
797 {
798 SBStream sstr;
799 sb_target.GetDescription (sstr, eDescriptionLevelBrief);
800 log->Printf ("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s", m_opaque_sp.get(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000801 target_sp.get(), sstr.GetData());
Jim Ingham83dd2032011-09-13 23:25:31 +0000802 }
803}
804
805void
Enrico Granata7de01172012-09-06 22:02:28 +0000806SBDebugger::DispatchInput (void* baton, const void *data, size_t data_len)
Enrico Granatadf83db82012-09-06 21:58:25 +0000807{
808 DispatchInput (data,data_len);
809}
810
811void
Filipe Cabecinhasf590be82012-08-20 16:21:04 +0000812SBDebugger::DispatchInput (const void *data, size_t data_len)
Chris Lattner24943d22010-06-08 16:52:24 +0000813{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000814 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000815
816 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000817 log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\", size_t=%" PRIu64 ")",
Greg Clayton851e30e2012-09-18 18:04:04 +0000818 m_opaque_sp.get(),
819 (int) data_len,
820 (const char *) data,
821 (uint64_t)data_len);
Caroline Tice7826c882010-10-26 03:11:13 +0000822
Greg Clayton63094e02010-06-23 01:19:29 +0000823 if (m_opaque_sp)
824 m_opaque_sp->DispatchInput ((const char *) data, data_len);
Chris Lattner24943d22010-06-08 16:52:24 +0000825}
826
827void
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000828SBDebugger::DispatchInputInterrupt ()
829{
830 if (m_opaque_sp)
831 m_opaque_sp->DispatchInputInterrupt ();
832}
833
834void
835SBDebugger::DispatchInputEndOfFile ()
836{
837 if (m_opaque_sp)
838 m_opaque_sp->DispatchInputEndOfFile ();
839}
Caroline Ticeb38df1e2011-05-09 23:06:58 +0000840
841bool
842SBDebugger::InputReaderIsTopReader (const lldb::SBInputReader &reader)
843{
844 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
845
846 if (log)
847 log->Printf ("SBDebugger(%p)::InputReaderIsTopReader (SBInputReader(%p))", m_opaque_sp.get(), &reader);
848
849 if (m_opaque_sp && reader.IsValid())
850 {
851 InputReaderSP reader_sp (*reader);
852 return m_opaque_sp->InputReaderIsTopReader (reader_sp);
853 }
854
855 return false;
856}
857
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000858
859void
Chris Lattner24943d22010-06-08 16:52:24 +0000860SBDebugger::PushInputReader (SBInputReader &reader)
861{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000862 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000863
864 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000865 log->Printf ("SBDebugger(%p)::PushInputReader (SBInputReader(%p))", m_opaque_sp.get(), &reader);
Caroline Tice7826c882010-10-26 03:11:13 +0000866
Greg Clayton63094e02010-06-23 01:19:29 +0000867 if (m_opaque_sp && reader.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000868 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000869 TargetSP target_sp (m_opaque_sp->GetSelectedTarget());
870 Mutex::Locker api_locker;
871 if (target_sp)
Jim Ingham1b584eb2012-05-04 23:02:50 +0000872 api_locker.Lock(target_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000873 InputReaderSP reader_sp(*reader);
Greg Clayton63094e02010-06-23 01:19:29 +0000874 m_opaque_sp->PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000875 }
876}
Greg Clayton63094e02010-06-23 01:19:29 +0000877
878void
Caroline Tice4a348082011-05-02 20:41:46 +0000879SBDebugger::NotifyTopInputReader (InputReaderAction notification)
880{
881 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
882
883 if (log)
884 log->Printf ("SBDebugger(%p)::NotifyTopInputReader (%d)", m_opaque_sp.get(), notification);
885
886 if (m_opaque_sp)
887 m_opaque_sp->NotifyTopInputReader (notification);
888}
889
890void
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000891SBDebugger::reset (const DebuggerSP &debugger_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000892{
893 m_opaque_sp = debugger_sp;
894}
895
896Debugger *
897SBDebugger::get () const
898{
899 return m_opaque_sp.get();
900}
901
902Debugger &
903SBDebugger::ref () const
904{
905 assert (m_opaque_sp.get());
906 return *m_opaque_sp;
907}
908
Greg Clayton15afa9f2011-10-01 02:59:24 +0000909const lldb::DebuggerSP &
910SBDebugger::get_sp () const
911{
912 return m_opaque_sp;
913}
Greg Clayton63094e02010-06-23 01:19:29 +0000914
Caroline Tice558be582010-06-30 16:22:25 +0000915SBDebugger
916SBDebugger::FindDebuggerWithID (int id)
917{
Greg Claytonbdcda462010-12-20 20:49:23 +0000918 // No need to lock, the debugger list is thread safe
Caroline Tice558be582010-06-30 16:22:25 +0000919 SBDebugger sb_debugger;
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000920 DebuggerSP debugger_sp = Debugger::FindDebuggerWithID (id);
Caroline Tice558be582010-06-30 16:22:25 +0000921 if (debugger_sp)
922 sb_debugger.reset (debugger_sp);
923 return sb_debugger;
924}
Jim Ingham74989e82010-08-30 19:44:40 +0000925
Caroline Ticeb8314fe2010-09-09 17:45:09 +0000926const char *
927SBDebugger::GetInstanceName()
928{
Greg Claytonc6e82e42012-08-22 18:39:03 +0000929 if (m_opaque_sp)
930 return m_opaque_sp->GetInstanceName().AsCString();
931 else
Caroline Ticeb8314fe2010-09-09 17:45:09 +0000932 return NULL;
933}
934
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000935SBError
Caroline Ticeb8314fe2010-09-09 17:45:09 +0000936SBDebugger::SetInternalVariable (const char *var_name, const char *value, const char *debugger_instance_name)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000937{
Greg Claytonc6e82e42012-08-22 18:39:03 +0000938 SBError sb_error;
939 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name)));
940 Error error;
941 if (debugger_sp)
942 {
943 ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext());
944 error = debugger_sp->SetPropertyValue (&exe_ctx,
945 eVarSetOperationAssign,
946 var_name,
947 value);
948 }
949 else
950 {
951 error.SetErrorStringWithFormat ("invalid debugger instance name '%s'", debugger_instance_name);
952 }
953 if (error.Fail())
954 sb_error.SetError(error);
955 return sb_error;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000956}
957
Greg Clayton9ce9c3c2011-04-18 23:15:17 +0000958SBStringList
Caroline Ticeb8314fe2010-09-09 17:45:09 +0000959SBDebugger::GetInternalVariableValue (const char *var_name, const char *debugger_instance_name)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000960{
961 SBStringList ret_value;
Greg Claytonc6e82e42012-08-22 18:39:03 +0000962 DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name)));
963 Error error;
964 if (debugger_sp)
965 {
966 ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext());
967 lldb::OptionValueSP value_sp (debugger_sp->GetPropertyValue (&exe_ctx,
968 var_name,
969 false,
970 error));
971 if (value_sp)
972 {
973 StreamString value_strm;
974 value_sp->DumpValue (&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
975 const std::string &value_str = value_strm.GetString();
976 if (!value_str.empty())
977 {
978 StringList string_list;
979 string_list.SplitIntoLines(value_str.c_str(), value_str.size());
980 return SBStringList(&string_list);
981 }
982 }
983 }
984 return SBStringList();
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000985}
986
Greg Clayton238c0a12010-09-18 01:14:36 +0000987uint32_t
988SBDebugger::GetTerminalWidth () const
989{
990 if (m_opaque_sp)
991 return m_opaque_sp->GetTerminalWidth ();
992 return 0;
993}
994
995void
996SBDebugger::SetTerminalWidth (uint32_t term_width)
997{
998 if (m_opaque_sp)
999 m_opaque_sp->SetTerminalWidth (term_width);
1000}
1001
1002const char *
1003SBDebugger::GetPrompt() const
1004{
Greg Clayton9ce9c3c2011-04-18 23:15:17 +00001005 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001006
1007 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +00001008 log->Printf ("SBDebugger(%p)::GetPrompt () => \"%s\"", m_opaque_sp.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +00001009 (m_opaque_sp ? m_opaque_sp->GetPrompt() : ""));
Caroline Tice7826c882010-10-26 03:11:13 +00001010
Greg Clayton238c0a12010-09-18 01:14:36 +00001011 if (m_opaque_sp)
1012 return m_opaque_sp->GetPrompt ();
1013 return 0;
1014}
1015
1016void
1017SBDebugger::SetPrompt (const char *prompt)
1018{
1019 if (m_opaque_sp)
1020 m_opaque_sp->SetPrompt (prompt);
1021}
1022
1023
Greg Clayton9ce9c3c2011-04-18 23:15:17 +00001024ScriptLanguage
Greg Clayton238c0a12010-09-18 01:14:36 +00001025SBDebugger::GetScriptLanguage() const
1026{
1027 if (m_opaque_sp)
1028 return m_opaque_sp->GetScriptLanguage ();
1029 return eScriptLanguageNone;
1030}
1031
1032void
Greg Clayton9ce9c3c2011-04-18 23:15:17 +00001033SBDebugger::SetScriptLanguage (ScriptLanguage script_lang)
Greg Clayton238c0a12010-09-18 01:14:36 +00001034{
1035 if (m_opaque_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001036 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001037 m_opaque_sp->SetScriptLanguage (script_lang);
Greg Claytonbdcda462010-12-20 20:49:23 +00001038 }
Greg Clayton238c0a12010-09-18 01:14:36 +00001039}
1040
Jim Ingham74989e82010-08-30 19:44:40 +00001041bool
1042SBDebugger::SetUseExternalEditor (bool value)
1043{
1044 if (m_opaque_sp)
1045 return m_opaque_sp->SetUseExternalEditor (value);
Greg Claytonbdcda462010-12-20 20:49:23 +00001046 return false;
Jim Ingham74989e82010-08-30 19:44:40 +00001047}
1048
1049bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001050SBDebugger::GetUseExternalEditor ()
Jim Ingham74989e82010-08-30 19:44:40 +00001051{
1052 if (m_opaque_sp)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001053 return m_opaque_sp->GetUseExternalEditor ();
Greg Claytonbdcda462010-12-20 20:49:23 +00001054 return false;
Jim Ingham74989e82010-08-30 19:44:40 +00001055}
1056
Caroline Tice98f930f2010-09-20 05:20:02 +00001057bool
1058SBDebugger::GetDescription (SBStream &description)
1059{
Greg Clayton96154be2011-11-13 06:57:31 +00001060 Stream &strm = description.ref();
1061
Caroline Tice98f930f2010-09-20 05:20:02 +00001062 if (m_opaque_sp)
1063 {
1064 const char *name = m_opaque_sp->GetInstanceName().AsCString();
Greg Clayton9ce9c3c2011-04-18 23:15:17 +00001065 user_id_t id = m_opaque_sp->GetID();
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001066 strm.Printf ("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
Caroline Tice98f930f2010-09-20 05:20:02 +00001067 }
1068 else
Greg Clayton96154be2011-11-13 06:57:31 +00001069 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +00001070
1071 return true;
1072}
Caroline Ticec4f55fe2010-11-19 20:47:54 +00001073
Greg Clayton9ce9c3c2011-04-18 23:15:17 +00001074user_id_t
Caroline Ticec4f55fe2010-11-19 20:47:54 +00001075SBDebugger::GetID()
1076{
1077 if (m_opaque_sp)
1078 return m_opaque_sp->GetID();
1079 return LLDB_INVALID_UID;
1080}
Greg Clayton180546b2011-04-30 01:09:13 +00001081
1082
1083SBError
1084SBDebugger::SetCurrentPlatform (const char *platform_name)
1085{
1086 SBError sb_error;
1087 if (m_opaque_sp)
1088 {
1089 PlatformSP platform_sp (Platform::Create (platform_name, sb_error.ref()));
1090
1091 if (platform_sp)
1092 {
1093 bool make_selected = true;
1094 m_opaque_sp->GetPlatformList().Append (platform_sp, make_selected);
1095 }
1096 }
1097 return sb_error;
1098}
1099
Greg Clayton421ca502011-05-29 04:06:55 +00001100bool
Greg Clayton604f0d32011-06-17 03:31:01 +00001101SBDebugger::SetCurrentPlatformSDKRoot (const char *sysroot)
1102{
1103 if (m_opaque_sp)
1104 {
1105 PlatformSP platform_sp (m_opaque_sp->GetPlatformList().GetSelectedPlatform());
1106
1107 if (platform_sp)
1108 {
1109 platform_sp->SetSDKRootDirectory (ConstString (sysroot));
1110 return true;
1111 }
1112 }
1113 return false;
1114}
1115
1116bool
Greg Clayton421ca502011-05-29 04:06:55 +00001117SBDebugger::GetCloseInputOnEOF () const
1118{
1119 if (m_opaque_sp)
1120 return m_opaque_sp->GetCloseInputOnEOF ();
1121 return false;
1122}
1123
1124void
1125SBDebugger::SetCloseInputOnEOF (bool b)
1126{
1127 if (m_opaque_sp)
1128 m_opaque_sp->SetCloseInputOnEOF (b);
1129}
Enrico Granata16376ed2012-02-15 02:34:21 +00001130
1131SBTypeCategory
1132SBDebugger::GetCategory (const char* category_name)
1133{
1134 if (!category_name || *category_name == 0)
1135 return SBTypeCategory();
1136
1137 TypeCategoryImplSP category_sp;
1138
1139 if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, false))
1140 return SBTypeCategory(category_sp);
1141 else
1142 return SBTypeCategory();
1143}
1144
1145SBTypeCategory
1146SBDebugger::CreateCategory (const char* category_name)
1147{
1148 if (!category_name || *category_name == 0)
1149 return SBTypeCategory();
1150
1151 TypeCategoryImplSP category_sp;
1152
1153 if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, true))
1154 return SBTypeCategory(category_sp);
1155 else
1156 return SBTypeCategory();
1157}
1158
1159bool
1160SBDebugger::DeleteCategory (const char* category_name)
1161{
1162 if (!category_name || *category_name == 0)
1163 return false;
1164
1165 return DataVisualization::Categories::Delete(ConstString(category_name));
1166}
1167
1168uint32_t
1169SBDebugger::GetNumCategories()
1170{
1171 return DataVisualization::Categories::GetCount();
1172}
1173
1174SBTypeCategory
1175SBDebugger::GetCategoryAtIndex (uint32_t index)
1176{
1177 return SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index));
1178}
1179
1180SBTypeCategory
1181SBDebugger::GetDefaultCategory()
1182{
1183 return GetCategory("default");
1184}
1185
1186SBTypeFormat
1187SBDebugger::GetFormatForType (SBTypeNameSpecifier type_name)
1188{
1189 SBTypeCategory default_category_sb = GetDefaultCategory();
1190 if (default_category_sb.GetEnabled())
1191 return default_category_sb.GetFormatForType(type_name);
1192 return SBTypeFormat();
1193}
1194
Jason Molendac48ca822012-02-21 05:33:55 +00001195#ifndef LLDB_DISABLE_PYTHON
Enrico Granata16376ed2012-02-15 02:34:21 +00001196SBTypeSummary
1197SBDebugger::GetSummaryForType (SBTypeNameSpecifier type_name)
1198{
Enrico Granata07baf832012-05-08 21:49:57 +00001199 if (type_name.IsValid() == false)
1200 return SBTypeSummary();
1201 return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
Enrico Granata16376ed2012-02-15 02:34:21 +00001202}
Jason Molendac48ca822012-02-21 05:33:55 +00001203#endif // LLDB_DISABLE_PYTHON
Enrico Granata16376ed2012-02-15 02:34:21 +00001204
1205SBTypeFilter
1206SBDebugger::GetFilterForType (SBTypeNameSpecifier type_name)
1207{
Enrico Granata07baf832012-05-08 21:49:57 +00001208 if (type_name.IsValid() == false)
1209 return SBTypeFilter();
1210 return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
Enrico Granata16376ed2012-02-15 02:34:21 +00001211}
1212
Jason Molendac48ca822012-02-21 05:33:55 +00001213#ifndef LLDB_DISABLE_PYTHON
Enrico Granata16376ed2012-02-15 02:34:21 +00001214SBTypeSynthetic
1215SBDebugger::GetSyntheticForType (SBTypeNameSpecifier type_name)
1216{
Enrico Granata07baf832012-05-08 21:49:57 +00001217 if (type_name.IsValid() == false)
1218 return SBTypeSynthetic();
1219 return SBTypeSynthetic(DataVisualization::GetSyntheticForType(type_name.GetSP()));
Enrico Granata16376ed2012-02-15 02:34:21 +00001220}
Jason Molendac48ca822012-02-21 05:33:55 +00001221#endif // LLDB_DISABLE_PYTHON
Enrico Granata16376ed2012-02-15 02:34:21 +00001222
Jim Ingham6c530f22012-02-21 02:23:08 +00001223bool
1224SBDebugger::EnableLog (const char *channel, const char **categories)
1225{
1226 if (m_opaque_sp)
1227 {
1228 uint32_t log_options = LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1229 StreamString errors;
1230 return m_opaque_sp->EnableLog (channel, categories, NULL, log_options, errors);
1231
1232 }
1233 else
1234 return false;
1235}
Jim Inghamd8465212012-02-22 22:49:20 +00001236
1237void
1238SBDebugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1239{
1240 if (m_opaque_sp)
1241 {
1242 return m_opaque_sp->SetLoggingCallback (log_callback, baton);
1243 }
1244}
1245
1246