1 //===-- 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 
10 #include "lldb/lldb-python.h"
11 
12 #include "lldb/API/SBDebugger.h"
13 
14 #include "lldb/lldb-private.h"
15 
16 #include "lldb/API/SBListener.h"
17 #include "lldb/API/SBBroadcaster.h"
18 #include "lldb/API/SBCommandInterpreter.h"
19 #include "lldb/API/SBCommandReturnObject.h"
20 #include "lldb/API/SBError.h"
21 #include "lldb/API/SBEvent.h"
22 #include "lldb/API/SBFrame.h"
23 #include "lldb/API/SBProcess.h"
24 #include "lldb/API/SBSourceManager.h"
25 #include "lldb/API/SBStream.h"
26 #include "lldb/API/SBStringList.h"
27 #include "lldb/API/SBTarget.h"
28 #include "lldb/API/SBThread.h"
29 #include "lldb/API/SBTypeCategory.h"
30 #include "lldb/API/SBTypeFormat.h"
31 #include "lldb/API/SBTypeFilter.h"
32 #include "lldb/API/SBTypeNameSpecifier.h"
33 #include "lldb/API/SBTypeSummary.h"
34 #include "lldb/API/SBTypeSynthetic.h"
35 
36 
37 #include "lldb/Core/Debugger.h"
38 #include "lldb/Core/State.h"
39 #include "lldb/Core/StreamFile.h"
40 #include "lldb/DataFormatters/DataVisualization.h"
41 #include "lldb/Host/DynamicLibrary.h"
42 #include "lldb/Interpreter/Args.h"
43 #include "lldb/Interpreter/CommandInterpreter.h"
44 #include "lldb/Interpreter/OptionGroupPlatform.h"
45 #include "lldb/Target/Process.h"
46 #include "lldb/Target/TargetList.h"
47 
48 using namespace lldb;
49 using namespace lldb_private;
50 
51 
52 SBInputReader::SBInputReader()
53 {
54 }
55 SBInputReader::~SBInputReader()
56 {
57 }
58 
59 SBError
60 SBInputReader::Initialize(lldb::SBDebugger& sb_debugger, unsigned long (*)(void*, lldb::SBInputReader*, lldb::InputReaderAction, char const*, unsigned long), void*, lldb::InputReaderGranularity, char const*, char const*, bool)
61 {
62     return SBError();
63 }
64 
65 void
66 SBInputReader::SetIsDone(bool)
67 {
68 }
69 bool
70 SBInputReader::IsActive() const
71 {
72     return false;
73 }
74 
75 static lldb::DynamicLibrarySP
76 LoadPlugin (const lldb::DebuggerSP &debugger_sp, const FileSpec& spec, Error& error)
77 {
78     lldb::DynamicLibrarySP dynlib_sp(new lldb_private::DynamicLibrary(spec));
79     if (dynlib_sp && dynlib_sp->IsValid())
80     {
81         typedef bool (*LLDBCommandPluginInit) (lldb::SBDebugger& debugger);
82 
83         lldb::SBDebugger debugger_sb(debugger_sp);
84         // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger) function.
85         // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays
86         LLDBCommandPluginInit init_func = dynlib_sp->GetSymbol<LLDBCommandPluginInit>("_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
87         if (init_func)
88         {
89             if (init_func(debugger_sb))
90                 return dynlib_sp;
91             else
92                 error.SetErrorString("plug-in refused to load (lldb::PluginInitialize(lldb::SBDebugger) returned false)");
93         }
94         else
95         {
96             error.SetErrorString("plug-in is missing the required initialization: lldb::PluginInitialize(lldb::SBDebugger)");
97         }
98     }
99     else
100     {
101         if (spec.Exists())
102             error.SetErrorString("this file does not represent a loadable dylib");
103         else
104             error.SetErrorString("no such file");
105     }
106     return lldb::DynamicLibrarySP();
107 }
108 
109 void
110 SBDebugger::Initialize ()
111 {
112     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
113 
114     if (log)
115         log->Printf ("SBDebugger::Initialize ()");
116 
117     SBCommandInterpreter::InitializeSWIG ();
118 
119     Debugger::Initialize(LoadPlugin);
120 }
121 
122 void
123 SBDebugger::Terminate ()
124 {
125     Debugger::Terminate();
126 }
127 
128 void
129 SBDebugger::Clear ()
130 {
131     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
132 
133     if (log)
134         log->Printf ("SBDebugger(%p)::Clear ()",
135                      static_cast<void*>(m_opaque_sp.get()));
136 
137     if (m_opaque_sp)
138         m_opaque_sp->ClearIOHandlers ();
139 
140     m_opaque_sp.reset();
141 }
142 
143 SBDebugger
144 SBDebugger::Create()
145 {
146     return SBDebugger::Create(false, NULL, NULL);
147 }
148 
149 SBDebugger
150 SBDebugger::Create(bool source_init_files)
151 {
152     return SBDebugger::Create (source_init_files, NULL, NULL);
153 }
154 
155 SBDebugger
156 SBDebugger::Create(bool source_init_files, lldb::LogOutputCallback callback, void *baton)
157 
158 {
159     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
160 
161     SBDebugger debugger;
162     debugger.reset(Debugger::CreateInstance(callback, baton));
163 
164     if (log)
165     {
166         SBStream sstr;
167         debugger.GetDescription (sstr);
168         log->Printf ("SBDebugger::Create () => SBDebugger(%p): %s",
169                      static_cast<void*>(debugger.m_opaque_sp.get()),
170                      sstr.GetData());
171     }
172 
173     SBCommandInterpreter interp = debugger.GetCommandInterpreter();
174     if (source_init_files)
175     {
176         interp.get()->SkipLLDBInitFiles(false);
177         interp.get()->SkipAppInitFiles (false);
178         SBCommandReturnObject result;
179         interp.SourceInitFileInHomeDirectory(result);
180     }
181     else
182     {
183         interp.get()->SkipLLDBInitFiles(true);
184         interp.get()->SkipAppInitFiles (true);
185     }
186     return debugger;
187 }
188 
189 void
190 SBDebugger::Destroy (SBDebugger &debugger)
191 {
192     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
193 
194     if (log)
195     {
196         SBStream sstr;
197         debugger.GetDescription (sstr);
198         log->Printf ("SBDebugger::Destroy () => SBDebugger(%p): %s",
199                      static_cast<void*>(debugger.m_opaque_sp.get()),
200                      sstr.GetData());
201     }
202 
203     Debugger::Destroy (debugger.m_opaque_sp);
204 
205     if (debugger.m_opaque_sp.get() != NULL)
206         debugger.m_opaque_sp.reset();
207 }
208 
209 void
210 SBDebugger::MemoryPressureDetected ()
211 {
212     // Since this function can be call asynchronously, we allow it to be
213     // non-mandatory. We have seen deadlocks with this function when called
214     // so we need to safeguard against this until we can determine what is
215     // causing the deadlocks.
216     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
217 
218     const bool mandatory = false;
219     if (log)
220     {
221         log->Printf ("SBDebugger::MemoryPressureDetected (), mandatory = %d", mandatory);
222     }
223 
224     ModuleList::RemoveOrphanSharedModules(mandatory);
225 }
226 
227 SBDebugger::SBDebugger () :
228     m_opaque_sp ()
229 {
230 }
231 
232 SBDebugger::SBDebugger(const lldb::DebuggerSP &debugger_sp) :
233     m_opaque_sp(debugger_sp)
234 {
235 }
236 
237 SBDebugger::SBDebugger(const SBDebugger &rhs) :
238     m_opaque_sp (rhs.m_opaque_sp)
239 {
240 }
241 
242 SBDebugger &
243 SBDebugger::operator = (const SBDebugger &rhs)
244 {
245     if (this != &rhs)
246     {
247         m_opaque_sp = rhs.m_opaque_sp;
248     }
249     return *this;
250 }
251 
252 SBDebugger::~SBDebugger ()
253 {
254 }
255 
256 bool
257 SBDebugger::IsValid() const
258 {
259     return m_opaque_sp.get() != NULL;
260 }
261 
262 
263 void
264 SBDebugger::SetAsync (bool b)
265 {
266     if (m_opaque_sp)
267         m_opaque_sp->SetAsyncExecution(b);
268 }
269 
270 bool
271 SBDebugger::GetAsync ()
272 {
273     if (m_opaque_sp)
274         return m_opaque_sp->GetAsyncExecution();
275     else
276         return false;
277 }
278 
279 void
280 SBDebugger::SkipLLDBInitFiles (bool b)
281 {
282     if (m_opaque_sp)
283         m_opaque_sp->GetCommandInterpreter().SkipLLDBInitFiles (b);
284 }
285 
286 void
287 SBDebugger::SkipAppInitFiles (bool b)
288 {
289     if (m_opaque_sp)
290         m_opaque_sp->GetCommandInterpreter().SkipAppInitFiles (b);
291 }
292 
293 // Shouldn't really be settable after initialization as this could cause lots of problems; don't want users
294 // trying to switch modes in the middle of a debugging session.
295 void
296 SBDebugger::SetInputFileHandle (FILE *fh, bool transfer_ownership)
297 {
298     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
299 
300     if (log)
301         log->Printf ("SBDebugger(%p)::SetInputFileHandle (fh=%p, transfer_ownership=%i)",
302                      static_cast<void*>(m_opaque_sp.get()),
303                      static_cast<void*>(fh), transfer_ownership);
304 
305     if (m_opaque_sp)
306         m_opaque_sp->SetInputFileHandle (fh, transfer_ownership);
307 }
308 
309 void
310 SBDebugger::SetOutputFileHandle (FILE *fh, bool transfer_ownership)
311 {
312     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
313 
314 
315     if (log)
316         log->Printf ("SBDebugger(%p)::SetOutputFileHandle (fh=%p, transfer_ownership=%i)",
317                      static_cast<void*>(m_opaque_sp.get()),
318                      static_cast<void*>(fh), transfer_ownership);
319 
320     if (m_opaque_sp)
321         m_opaque_sp->SetOutputFileHandle (fh, transfer_ownership);
322 }
323 
324 void
325 SBDebugger::SetErrorFileHandle (FILE *fh, bool transfer_ownership)
326 {
327     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
328 
329 
330     if (log)
331         log->Printf ("SBDebugger(%p)::SetErrorFileHandle (fh=%p, transfer_ownership=%i)",
332                      static_cast<void*>(m_opaque_sp.get()),
333                      static_cast<void*>(fh), transfer_ownership);
334 
335     if (m_opaque_sp)
336         m_opaque_sp->SetErrorFileHandle (fh, transfer_ownership);
337 }
338 
339 FILE *
340 SBDebugger::GetInputFileHandle ()
341 {
342     if (m_opaque_sp)
343     {
344         StreamFileSP stream_file_sp (m_opaque_sp->GetInputFile());
345         if (stream_file_sp)
346             return stream_file_sp->GetFile().GetStream();
347     }
348     return NULL;
349 }
350 
351 FILE *
352 SBDebugger::GetOutputFileHandle ()
353 {
354     if (m_opaque_sp)
355     {
356         StreamFileSP stream_file_sp (m_opaque_sp->GetOutputFile());
357         if (stream_file_sp)
358             return stream_file_sp->GetFile().GetStream();
359     }
360     return NULL;
361 }
362 
363 FILE *
364 SBDebugger::GetErrorFileHandle ()
365 {
366     if (m_opaque_sp)
367         if (m_opaque_sp)
368         {
369             StreamFileSP stream_file_sp (m_opaque_sp->GetErrorFile());
370             if (stream_file_sp)
371                 return stream_file_sp->GetFile().GetStream();
372         }
373     return NULL;
374 }
375 
376 void
377 SBDebugger::SaveInputTerminalState()
378 {
379     if (m_opaque_sp)
380         m_opaque_sp->SaveInputTerminalState();
381 }
382 
383 void
384 SBDebugger::RestoreInputTerminalState()
385 {
386     if (m_opaque_sp)
387         m_opaque_sp->RestoreInputTerminalState();
388 
389 }
390 SBCommandInterpreter
391 SBDebugger::GetCommandInterpreter ()
392 {
393     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
394 
395     SBCommandInterpreter sb_interpreter;
396     if (m_opaque_sp)
397         sb_interpreter.reset (&m_opaque_sp->GetCommandInterpreter());
398 
399     if (log)
400         log->Printf ("SBDebugger(%p)::GetCommandInterpreter () => SBCommandInterpreter(%p)",
401                      static_cast<void*>(m_opaque_sp.get()),
402                      static_cast<void*>(sb_interpreter.get()));
403 
404     return sb_interpreter;
405 }
406 
407 void
408 SBDebugger::HandleCommand (const char *command)
409 {
410     if (m_opaque_sp)
411     {
412         TargetSP target_sp (m_opaque_sp->GetSelectedTarget());
413         Mutex::Locker api_locker;
414         if (target_sp)
415             api_locker.Lock(target_sp->GetAPIMutex());
416 
417         SBCommandInterpreter sb_interpreter(GetCommandInterpreter ());
418         SBCommandReturnObject result;
419 
420         sb_interpreter.HandleCommand (command, result, false);
421 
422         if (GetErrorFileHandle() != NULL)
423             result.PutError (GetErrorFileHandle());
424         if (GetOutputFileHandle() != NULL)
425             result.PutOutput (GetOutputFileHandle());
426 
427         if (m_opaque_sp->GetAsyncExecution() == false)
428         {
429             SBProcess process(GetCommandInterpreter().GetProcess ());
430             ProcessSP process_sp (process.GetSP());
431             if (process_sp)
432             {
433                 EventSP event_sp;
434                 Listener &lldb_listener = m_opaque_sp->GetListener();
435                 while (lldb_listener.GetNextEventForBroadcaster (process_sp.get(), event_sp))
436                 {
437                     SBEvent event(event_sp);
438                     HandleProcessEvent (process, event, GetOutputFileHandle(), GetErrorFileHandle());
439                 }
440             }
441         }
442     }
443 }
444 
445 SBListener
446 SBDebugger::GetListener ()
447 {
448     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
449 
450     SBListener sb_listener;
451     if (m_opaque_sp)
452         sb_listener.reset(&m_opaque_sp->GetListener(), false);
453 
454     if (log)
455         log->Printf ("SBDebugger(%p)::GetListener () => SBListener(%p)",
456                      static_cast<void*>(m_opaque_sp.get()),
457                      static_cast<void*>(sb_listener.get()));
458 
459     return sb_listener;
460 }
461 
462 void
463 SBDebugger::HandleProcessEvent (const SBProcess &process, const SBEvent &event, FILE *out, FILE *err)
464 {
465     if (!process.IsValid())
466         return;
467 
468     TargetSP target_sp (process.GetTarget().GetSP());
469     if (!target_sp)
470         return;
471 
472     const uint32_t event_type = event.GetType();
473     char stdio_buffer[1024];
474     size_t len;
475 
476     Mutex::Locker api_locker (target_sp->GetAPIMutex());
477 
478     if (event_type & (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged))
479     {
480         // Drain stdout when we stop just in case we have any bytes
481         while ((len = process.GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0)
482             if (out != NULL)
483                 ::fwrite (stdio_buffer, 1, len, out);
484     }
485 
486     if (event_type & (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged))
487     {
488         // Drain stderr when we stop just in case we have any bytes
489         while ((len = process.GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0)
490             if (err != NULL)
491                 ::fwrite (stdio_buffer, 1, len, err);
492     }
493 
494     if (event_type & Process::eBroadcastBitStateChanged)
495     {
496         StateType event_state = SBProcess::GetStateFromEvent (event);
497 
498         if (event_state == eStateInvalid)
499             return;
500 
501         bool is_stopped = StateIsStoppedState (event_state);
502         if (!is_stopped)
503             process.ReportEventState (event, out);
504     }
505 }
506 
507 SBSourceManager
508 SBDebugger::GetSourceManager ()
509 {
510     SBSourceManager sb_source_manager (*this);
511     return sb_source_manager;
512 }
513 
514 
515 bool
516 SBDebugger::GetDefaultArchitecture (char *arch_name, size_t arch_name_len)
517 {
518     if (arch_name && arch_name_len)
519     {
520         ArchSpec default_arch = Target::GetDefaultArchitecture ();
521 
522         if (default_arch.IsValid())
523         {
524             const std::string &triple_str = default_arch.GetTriple().str();
525             if (!triple_str.empty())
526                 ::snprintf (arch_name, arch_name_len, "%s", triple_str.c_str());
527             else
528                 ::snprintf (arch_name, arch_name_len, "%s", default_arch.GetArchitectureName());
529             return true;
530         }
531     }
532     if (arch_name && arch_name_len)
533         arch_name[0] = '\0';
534     return false;
535 }
536 
537 
538 bool
539 SBDebugger::SetDefaultArchitecture (const char *arch_name)
540 {
541     if (arch_name)
542     {
543         ArchSpec arch (arch_name);
544         if (arch.IsValid())
545         {
546             Target::SetDefaultArchitecture (arch);
547             return true;
548         }
549     }
550     return false;
551 }
552 
553 ScriptLanguage
554 SBDebugger::GetScriptingLanguage (const char *script_language_name)
555 {
556 
557     return Args::StringToScriptLanguage (script_language_name,
558                                          eScriptLanguageDefault,
559                                          NULL);
560 }
561 
562 const char *
563 SBDebugger::GetVersionString ()
564 {
565     return lldb_private::GetVersion();
566 }
567 
568 const char *
569 SBDebugger::StateAsCString (StateType state)
570 {
571     return lldb_private::StateAsCString (state);
572 }
573 
574 bool
575 SBDebugger::StateIsRunningState (StateType state)
576 {
577     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
578 
579     const bool result = lldb_private::StateIsRunningState (state);
580     if (log)
581         log->Printf ("SBDebugger::StateIsRunningState (state=%s) => %i",
582                      StateAsCString (state), result);
583 
584     return result;
585 }
586 
587 bool
588 SBDebugger::StateIsStoppedState (StateType state)
589 {
590     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
591 
592     const bool result = lldb_private::StateIsStoppedState (state, false);
593     if (log)
594         log->Printf ("SBDebugger::StateIsStoppedState (state=%s) => %i",
595                      StateAsCString (state), result);
596 
597     return result;
598 }
599 
600 lldb::SBTarget
601 SBDebugger::CreateTarget (const char *filename,
602                           const char *target_triple,
603                           const char *platform_name,
604                           bool add_dependent_modules,
605                           lldb::SBError& sb_error)
606 {
607     SBTarget sb_target;
608     TargetSP target_sp;
609     if (m_opaque_sp)
610     {
611         sb_error.Clear();
612         OptionGroupPlatform platform_options (false);
613         platform_options.SetPlatformName (platform_name);
614 
615         sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
616                                                                     filename,
617                                                                     target_triple,
618                                                                     add_dependent_modules,
619                                                                     &platform_options,
620                                                                     target_sp);
621 
622         if (sb_error.Success())
623             sb_target.SetSP (target_sp);
624     }
625     else
626     {
627         sb_error.SetErrorString("invalid target");
628     }
629 
630     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
631     if (log)
632         log->Printf ("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, platform_name=%s, add_dependent_modules=%u, error=%s) => SBTarget(%p)",
633                      static_cast<void*>(m_opaque_sp.get()), filename,
634                      target_triple, platform_name, add_dependent_modules,
635                      sb_error.GetCString(), static_cast<void*>(target_sp.get()));
636 
637     return sb_target;
638 }
639 
640 SBTarget
641 SBDebugger::CreateTargetWithFileAndTargetTriple (const char *filename,
642                                                  const char *target_triple)
643 {
644     SBTarget sb_target;
645     TargetSP target_sp;
646     if (m_opaque_sp)
647     {
648         const bool add_dependent_modules = true;
649         Error error (m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
650                                                                 filename,
651                                                                 target_triple,
652                                                                 add_dependent_modules,
653                                                                 NULL,
654                                                                 target_sp));
655         sb_target.SetSP (target_sp);
656     }
657 
658     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
659     if (log)
660         log->Printf ("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple (filename=\"%s\", triple=%s) => SBTarget(%p)",
661                      static_cast<void*>(m_opaque_sp.get()), filename,
662                      target_triple, static_cast<void*>(target_sp.get()));
663 
664     return sb_target;
665 }
666 
667 SBTarget
668 SBDebugger::CreateTargetWithFileAndArch (const char *filename, const char *arch_cstr)
669 {
670     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
671 
672     SBTarget sb_target;
673     TargetSP target_sp;
674     if (m_opaque_sp)
675     {
676         Error error;
677         const bool add_dependent_modules = true;
678 
679         error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
680                                                            filename,
681                                                            arch_cstr,
682                                                            add_dependent_modules,
683                                                            NULL,
684                                                            target_sp);
685 
686         if (error.Success())
687         {
688             m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
689             sb_target.SetSP (target_sp);
690         }
691     }
692 
693     if (log)
694         log->Printf ("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", arch=%s) => SBTarget(%p)",
695                      static_cast<void*>(m_opaque_sp.get()), filename, arch_cstr,
696                      static_cast<void*>(target_sp.get()));
697 
698     return sb_target;
699 }
700 
701 SBTarget
702 SBDebugger::CreateTarget (const char *filename)
703 {
704     SBTarget sb_target;
705     TargetSP target_sp;
706     if (m_opaque_sp)
707     {
708         ArchSpec arch = Target::GetDefaultArchitecture ();
709         Error error;
710         const bool add_dependent_modules = true;
711 
712         PlatformSP platform_sp(m_opaque_sp->GetPlatformList().GetSelectedPlatform());
713         error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
714                                                            filename,
715                                                            arch,
716                                                            add_dependent_modules,
717                                                            platform_sp,
718                                                            target_sp);
719 
720         if (error.Success())
721         {
722             m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
723             sb_target.SetSP (target_sp);
724         }
725     }
726     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
727     if (log)
728         log->Printf ("SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
729                      static_cast<void*>(m_opaque_sp.get()), filename,
730                      static_cast<void*>(target_sp.get()));
731     return sb_target;
732 }
733 
734 bool
735 SBDebugger::DeleteTarget (lldb::SBTarget &target)
736 {
737     bool result = false;
738     if (m_opaque_sp)
739     {
740         TargetSP target_sp(target.GetSP());
741         if (target_sp)
742         {
743             // No need to lock, the target list is thread safe
744             result = m_opaque_sp->GetTargetList().DeleteTarget (target_sp);
745             target_sp->Destroy();
746             target.Clear();
747             const bool mandatory = true;
748             ModuleList::RemoveOrphanSharedModules(mandatory);
749         }
750     }
751 
752     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
753     if (log)
754         log->Printf ("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i",
755                      static_cast<void*>(m_opaque_sp.get()),
756                      static_cast<void*>(target.m_opaque_sp.get()), result);
757 
758     return result;
759 }
760 SBTarget
761 SBDebugger::GetTargetAtIndex (uint32_t idx)
762 {
763     SBTarget sb_target;
764     if (m_opaque_sp)
765     {
766         // No need to lock, the target list is thread safe
767         sb_target.SetSP (m_opaque_sp->GetTargetList().GetTargetAtIndex (idx));
768     }
769     return sb_target;
770 }
771 
772 uint32_t
773 SBDebugger::GetIndexOfTarget (lldb::SBTarget target)
774 {
775 
776     lldb::TargetSP target_sp = target.GetSP();
777     if (!target_sp)
778         return UINT32_MAX;
779 
780     if (!m_opaque_sp)
781         return UINT32_MAX;
782 
783     return m_opaque_sp->GetTargetList().GetIndexOfTarget (target.GetSP());
784 }
785 
786 SBTarget
787 SBDebugger::FindTargetWithProcessID (lldb::pid_t pid)
788 {
789     SBTarget sb_target;
790     if (m_opaque_sp)
791     {
792         // No need to lock, the target list is thread safe
793         sb_target.SetSP (m_opaque_sp->GetTargetList().FindTargetWithProcessID (pid));
794     }
795     return sb_target;
796 }
797 
798 SBTarget
799 SBDebugger::FindTargetWithFileAndArch (const char *filename, const char *arch_name)
800 {
801     SBTarget sb_target;
802     if (m_opaque_sp && filename && filename[0])
803     {
804         // No need to lock, the target list is thread safe
805         ArchSpec arch (arch_name, m_opaque_sp->GetPlatformList().GetSelectedPlatform().get());
806         TargetSP target_sp (m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture (FileSpec(filename, false), arch_name ? &arch : NULL));
807         sb_target.SetSP (target_sp);
808     }
809     return sb_target;
810 }
811 
812 SBTarget
813 SBDebugger::FindTargetWithLLDBProcess (const ProcessSP &process_sp)
814 {
815     SBTarget sb_target;
816     if (m_opaque_sp)
817     {
818         // No need to lock, the target list is thread safe
819         sb_target.SetSP (m_opaque_sp->GetTargetList().FindTargetWithProcess (process_sp.get()));
820     }
821     return sb_target;
822 }
823 
824 
825 uint32_t
826 SBDebugger::GetNumTargets ()
827 {
828     if (m_opaque_sp)
829     {
830         // No need to lock, the target list is thread safe
831         return m_opaque_sp->GetTargetList().GetNumTargets ();
832     }
833     return 0;
834 }
835 
836 SBTarget
837 SBDebugger::GetSelectedTarget ()
838 {
839     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
840 
841     SBTarget sb_target;
842     TargetSP target_sp;
843     if (m_opaque_sp)
844     {
845         // No need to lock, the target list is thread safe
846         target_sp = m_opaque_sp->GetTargetList().GetSelectedTarget ();
847         sb_target.SetSP (target_sp);
848     }
849 
850     if (log)
851     {
852         SBStream sstr;
853         sb_target.GetDescription (sstr, eDescriptionLevelBrief);
854         log->Printf ("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s",
855                      static_cast<void*>(m_opaque_sp.get()),
856                      static_cast<void*>(target_sp.get()), sstr.GetData());
857     }
858 
859     return sb_target;
860 }
861 
862 void
863 SBDebugger::SetSelectedTarget (SBTarget &sb_target)
864 {
865     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
866 
867     TargetSP target_sp (sb_target.GetSP());
868     if (m_opaque_sp)
869     {
870         m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
871     }
872     if (log)
873     {
874         SBStream sstr;
875         sb_target.GetDescription (sstr, eDescriptionLevelBrief);
876         log->Printf ("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s",
877                      static_cast<void*>(m_opaque_sp.get()),
878                      static_cast<void*>(target_sp.get()), sstr.GetData());
879     }
880 }
881 
882 SBPlatform
883 SBDebugger::GetSelectedPlatform()
884 {
885     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
886 
887     SBPlatform sb_platform;
888     DebuggerSP debugger_sp(m_opaque_sp);
889     if (debugger_sp)
890     {
891         sb_platform.SetSP(debugger_sp->GetPlatformList().GetSelectedPlatform());
892     }
893     if (log)
894         log->Printf ("SBDebugger(%p)::GetSelectedPlatform () => SBPlatform(%p): %s",
895                      static_cast<void*>(m_opaque_sp.get()),
896                      static_cast<void*>(sb_platform.GetSP().get()),
897                      sb_platform.GetName());
898     return sb_platform;
899 }
900 
901 void
902 SBDebugger::SetSelectedPlatform(SBPlatform &sb_platform)
903 {
904     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
905 
906     DebuggerSP debugger_sp(m_opaque_sp);
907     if (debugger_sp)
908     {
909         debugger_sp->GetPlatformList().SetSelectedPlatform(sb_platform.GetSP());
910     }
911 
912     if (log)
913         log->Printf ("SBDebugger(%p)::SetSelectedPlatform (SBPlatform(%p) %s)",
914                      static_cast<void*>(m_opaque_sp.get()),
915                      static_cast<void*>(sb_platform.GetSP().get()),
916                      sb_platform.GetName());
917 }
918 
919 void
920 SBDebugger::DispatchInput (void* baton, const void *data, size_t data_len)
921 {
922     DispatchInput (data,data_len);
923 }
924 
925 void
926 SBDebugger::DispatchInput (const void *data, size_t data_len)
927 {
928 //    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
929 //
930 //    if (log)
931 //        log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\", size_t=%" PRIu64 ")",
932 //                     m_opaque_sp.get(),
933 //                     (int) data_len,
934 //                     (const char *) data,
935 //                     (uint64_t)data_len);
936 //
937 //    if (m_opaque_sp)
938 //        m_opaque_sp->DispatchInput ((const char *) data, data_len);
939 }
940 
941 void
942 SBDebugger::DispatchInputInterrupt ()
943 {
944     if (m_opaque_sp)
945         m_opaque_sp->DispatchInputInterrupt ();
946 }
947 
948 void
949 SBDebugger::DispatchInputEndOfFile ()
950 {
951     if (m_opaque_sp)
952         m_opaque_sp->DispatchInputEndOfFile ();
953 }
954 
955 void
956 SBDebugger::PushInputReader (SBInputReader &reader)
957 {
958 }
959 
960 void
961 SBDebugger::RunCommandInterpreter (bool auto_handle_events,
962                                    bool spawn_thread)
963 {
964     if (m_opaque_sp)
965         m_opaque_sp->GetCommandInterpreter().RunCommandInterpreter(auto_handle_events, spawn_thread);
966 }
967 
968 void
969 SBDebugger::reset (const DebuggerSP &debugger_sp)
970 {
971     m_opaque_sp = debugger_sp;
972 }
973 
974 Debugger *
975 SBDebugger::get () const
976 {
977     return m_opaque_sp.get();
978 }
979 
980 Debugger &
981 SBDebugger::ref () const
982 {
983     assert (m_opaque_sp.get());
984     return *m_opaque_sp;
985 }
986 
987 const lldb::DebuggerSP &
988 SBDebugger::get_sp () const
989 {
990     return m_opaque_sp;
991 }
992 
993 SBDebugger
994 SBDebugger::FindDebuggerWithID (int id)
995 {
996     // No need to lock, the debugger list is thread safe
997     SBDebugger sb_debugger;
998     DebuggerSP debugger_sp = Debugger::FindDebuggerWithID (id);
999     if (debugger_sp)
1000         sb_debugger.reset (debugger_sp);
1001     return sb_debugger;
1002 }
1003 
1004 const char *
1005 SBDebugger::GetInstanceName()
1006 {
1007     if (m_opaque_sp)
1008         return m_opaque_sp->GetInstanceName().AsCString();
1009     else
1010         return NULL;
1011 }
1012 
1013 SBError
1014 SBDebugger::SetInternalVariable (const char *var_name, const char *value, const char *debugger_instance_name)
1015 {
1016     SBError sb_error;
1017     DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name)));
1018     Error error;
1019     if (debugger_sp)
1020     {
1021         ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext());
1022         error = debugger_sp->SetPropertyValue (&exe_ctx,
1023                                                eVarSetOperationAssign,
1024                                                var_name,
1025                                                value);
1026     }
1027     else
1028     {
1029         error.SetErrorStringWithFormat ("invalid debugger instance name '%s'", debugger_instance_name);
1030     }
1031     if (error.Fail())
1032         sb_error.SetError(error);
1033     return sb_error;
1034 }
1035 
1036 SBStringList
1037 SBDebugger::GetInternalVariableValue (const char *var_name, const char *debugger_instance_name)
1038 {
1039     SBStringList ret_value;
1040     DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name)));
1041     Error error;
1042     if (debugger_sp)
1043     {
1044         ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext());
1045         lldb::OptionValueSP value_sp (debugger_sp->GetPropertyValue (&exe_ctx,
1046                                                                      var_name,
1047                                                                      false,
1048                                                                      error));
1049         if (value_sp)
1050         {
1051             StreamString value_strm;
1052             value_sp->DumpValue (&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
1053             const std::string &value_str = value_strm.GetString();
1054             if (!value_str.empty())
1055             {
1056                 StringList string_list;
1057                 string_list.SplitIntoLines(value_str);
1058                 return SBStringList(&string_list);
1059             }
1060         }
1061     }
1062     return SBStringList();
1063 }
1064 
1065 uint32_t
1066 SBDebugger::GetTerminalWidth () const
1067 {
1068     if (m_opaque_sp)
1069         return m_opaque_sp->GetTerminalWidth ();
1070     return 0;
1071 }
1072 
1073 void
1074 SBDebugger::SetTerminalWidth (uint32_t term_width)
1075 {
1076     if (m_opaque_sp)
1077         m_opaque_sp->SetTerminalWidth (term_width);
1078 }
1079 
1080 const char *
1081 SBDebugger::GetPrompt() const
1082 {
1083     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1084 
1085     if (log)
1086         log->Printf ("SBDebugger(%p)::GetPrompt () => \"%s\"",
1087                      static_cast<void*>(m_opaque_sp.get()),
1088                      (m_opaque_sp ? m_opaque_sp->GetPrompt() : ""));
1089 
1090     if (m_opaque_sp)
1091         return m_opaque_sp->GetPrompt ();
1092     return 0;
1093 }
1094 
1095 void
1096 SBDebugger::SetPrompt (const char *prompt)
1097 {
1098     if (m_opaque_sp)
1099         m_opaque_sp->SetPrompt (prompt);
1100 }
1101 
1102 
1103 ScriptLanguage
1104 SBDebugger::GetScriptLanguage() const
1105 {
1106     if (m_opaque_sp)
1107         return m_opaque_sp->GetScriptLanguage ();
1108     return eScriptLanguageNone;
1109 }
1110 
1111 void
1112 SBDebugger::SetScriptLanguage (ScriptLanguage script_lang)
1113 {
1114     if (m_opaque_sp)
1115     {
1116         m_opaque_sp->SetScriptLanguage (script_lang);
1117     }
1118 }
1119 
1120 bool
1121 SBDebugger::SetUseExternalEditor (bool value)
1122 {
1123     if (m_opaque_sp)
1124         return m_opaque_sp->SetUseExternalEditor (value);
1125     return false;
1126 }
1127 
1128 bool
1129 SBDebugger::GetUseExternalEditor ()
1130 {
1131     if (m_opaque_sp)
1132         return m_opaque_sp->GetUseExternalEditor ();
1133     return false;
1134 }
1135 
1136 bool
1137 SBDebugger::SetUseColor (bool value)
1138 {
1139     if (m_opaque_sp)
1140         return m_opaque_sp->SetUseColor (value);
1141     return false;
1142 }
1143 
1144 bool
1145 SBDebugger::GetUseColor () const
1146 {
1147     if (m_opaque_sp)
1148         return m_opaque_sp->GetUseColor ();
1149     return false;
1150 }
1151 
1152 bool
1153 SBDebugger::GetDescription (SBStream &description)
1154 {
1155     Stream &strm = description.ref();
1156 
1157     if (m_opaque_sp)
1158     {
1159         const char *name = m_opaque_sp->GetInstanceName().AsCString();
1160         user_id_t id = m_opaque_sp->GetID();
1161         strm.Printf ("Debugger (instance: \"%s\", id: %" PRIu64 ")", name, id);
1162     }
1163     else
1164         strm.PutCString ("No value");
1165 
1166     return true;
1167 }
1168 
1169 user_id_t
1170 SBDebugger::GetID()
1171 {
1172     if (m_opaque_sp)
1173         return m_opaque_sp->GetID();
1174     return LLDB_INVALID_UID;
1175 }
1176 
1177 
1178 SBError
1179 SBDebugger::SetCurrentPlatform (const char *platform_name)
1180 {
1181     SBError sb_error;
1182     if (m_opaque_sp)
1183     {
1184         PlatformSP platform_sp (Platform::Create (platform_name, sb_error.ref()));
1185 
1186         if (platform_sp)
1187         {
1188             bool make_selected = true;
1189             m_opaque_sp->GetPlatformList().Append (platform_sp, make_selected);
1190         }
1191     }
1192     return sb_error;
1193 }
1194 
1195 bool
1196 SBDebugger::SetCurrentPlatformSDKRoot (const char *sysroot)
1197 {
1198     if (m_opaque_sp)
1199     {
1200         PlatformSP platform_sp (m_opaque_sp->GetPlatformList().GetSelectedPlatform());
1201 
1202         if (platform_sp)
1203         {
1204             platform_sp->SetSDKRootDirectory (ConstString (sysroot));
1205             return true;
1206         }
1207     }
1208     return false;
1209 }
1210 
1211 bool
1212 SBDebugger::GetCloseInputOnEOF () const
1213 {
1214     if (m_opaque_sp)
1215         return m_opaque_sp->GetCloseInputOnEOF ();
1216     return false;
1217 }
1218 
1219 void
1220 SBDebugger::SetCloseInputOnEOF (bool b)
1221 {
1222     if (m_opaque_sp)
1223         m_opaque_sp->SetCloseInputOnEOF (b);
1224 }
1225 
1226 SBTypeCategory
1227 SBDebugger::GetCategory (const char* category_name)
1228 {
1229     if (!category_name || *category_name == 0)
1230         return SBTypeCategory();
1231 
1232     TypeCategoryImplSP category_sp;
1233 
1234     if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, false))
1235         return SBTypeCategory(category_sp);
1236     else
1237         return SBTypeCategory();
1238 }
1239 
1240 SBTypeCategory
1241 SBDebugger::CreateCategory (const char* category_name)
1242 {
1243     if (!category_name || *category_name == 0)
1244         return SBTypeCategory();
1245 
1246     TypeCategoryImplSP category_sp;
1247 
1248     if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, true))
1249         return SBTypeCategory(category_sp);
1250     else
1251         return SBTypeCategory();
1252 }
1253 
1254 bool
1255 SBDebugger::DeleteCategory (const char* category_name)
1256 {
1257     if (!category_name || *category_name == 0)
1258         return false;
1259 
1260     return DataVisualization::Categories::Delete(ConstString(category_name));
1261 }
1262 
1263 uint32_t
1264 SBDebugger::GetNumCategories()
1265 {
1266     return DataVisualization::Categories::GetCount();
1267 }
1268 
1269 SBTypeCategory
1270 SBDebugger::GetCategoryAtIndex (uint32_t index)
1271 {
1272     return SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index));
1273 }
1274 
1275 SBTypeCategory
1276 SBDebugger::GetDefaultCategory()
1277 {
1278     return GetCategory("default");
1279 }
1280 
1281 SBTypeFormat
1282 SBDebugger::GetFormatForType (SBTypeNameSpecifier type_name)
1283 {
1284     SBTypeCategory default_category_sb = GetDefaultCategory();
1285     if (default_category_sb.GetEnabled())
1286         return default_category_sb.GetFormatForType(type_name);
1287     return SBTypeFormat();
1288 }
1289 
1290 #ifndef LLDB_DISABLE_PYTHON
1291 SBTypeSummary
1292 SBDebugger::GetSummaryForType (SBTypeNameSpecifier type_name)
1293 {
1294     if (type_name.IsValid() == false)
1295         return SBTypeSummary();
1296     return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1297 }
1298 #endif // LLDB_DISABLE_PYTHON
1299 
1300 SBTypeFilter
1301 SBDebugger::GetFilterForType (SBTypeNameSpecifier type_name)
1302 {
1303     if (type_name.IsValid() == false)
1304         return SBTypeFilter();
1305     return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1306 }
1307 
1308 #ifndef LLDB_DISABLE_PYTHON
1309 SBTypeSynthetic
1310 SBDebugger::GetSyntheticForType (SBTypeNameSpecifier type_name)
1311 {
1312     if (type_name.IsValid() == false)
1313         return SBTypeSynthetic();
1314     return SBTypeSynthetic(DataVisualization::GetSyntheticForType(type_name.GetSP()));
1315 }
1316 #endif // LLDB_DISABLE_PYTHON
1317 
1318 bool
1319 SBDebugger::EnableLog (const char *channel, const char **categories)
1320 {
1321     if (m_opaque_sp)
1322     {
1323         uint32_t log_options = LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1324         StreamString errors;
1325         return m_opaque_sp->EnableLog (channel, categories, NULL, log_options, errors);
1326 
1327     }
1328     else
1329         return false;
1330 }
1331 
1332 void
1333 SBDebugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1334 {
1335     if (m_opaque_sp)
1336     {
1337         return m_opaque_sp->SetLoggingCallback (log_callback, baton);
1338     }
1339 }
1340 
1341 
1342