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