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