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 (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 (data=\"%.*s\", size_t=%zu)", m_opaque_sp.get(),
803                      (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     SBError sb_error;
921     DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name)));
922     Error error;
923     if (debugger_sp)
924     {
925         ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext());
926         error = debugger_sp->SetPropertyValue (&exe_ctx,
927                                                eVarSetOperationAssign,
928                                                var_name,
929                                                value);
930     }
931     else
932     {
933         error.SetErrorStringWithFormat ("invalid debugger instance name '%s'", debugger_instance_name);
934     }
935     if (error.Fail())
936         sb_error.SetError(error);
937     return sb_error;
938 }
939 
940 SBStringList
941 SBDebugger::GetInternalVariableValue (const char *var_name, const char *debugger_instance_name)
942 {
943     SBStringList ret_value;
944     DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name)));
945     Error error;
946     if (debugger_sp)
947     {
948         ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext());
949         lldb::OptionValueSP value_sp (debugger_sp->GetPropertyValue (&exe_ctx,
950                                                                      var_name,
951                                                                      false,
952                                                                      error));
953         if (value_sp)
954         {
955             StreamString value_strm;
956             value_sp->DumpValue (&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
957             const std::string &value_str = value_strm.GetString();
958             if (!value_str.empty())
959             {
960                 StringList string_list;
961                 string_list.SplitIntoLines(value_str.c_str(), value_str.size());
962                 return SBStringList(&string_list);
963             }
964         }
965     }
966     return SBStringList();
967 }
968 
969 uint32_t
970 SBDebugger::GetTerminalWidth () const
971 {
972     if (m_opaque_sp)
973         return m_opaque_sp->GetTerminalWidth ();
974     return 0;
975 }
976 
977 void
978 SBDebugger::SetTerminalWidth (uint32_t term_width)
979 {
980     if (m_opaque_sp)
981         m_opaque_sp->SetTerminalWidth (term_width);
982 }
983 
984 const char *
985 SBDebugger::GetPrompt() const
986 {
987     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
988 
989     if (log)
990         log->Printf ("SBDebugger(%p)::GetPrompt () => \"%s\"", m_opaque_sp.get(),
991                      (m_opaque_sp ? m_opaque_sp->GetPrompt() : ""));
992 
993     if (m_opaque_sp)
994         return m_opaque_sp->GetPrompt ();
995     return 0;
996 }
997 
998 void
999 SBDebugger::SetPrompt (const char *prompt)
1000 {
1001     if (m_opaque_sp)
1002         m_opaque_sp->SetPrompt (prompt);
1003 }
1004 
1005 
1006 ScriptLanguage
1007 SBDebugger::GetScriptLanguage() const
1008 {
1009     if (m_opaque_sp)
1010         return m_opaque_sp->GetScriptLanguage ();
1011     return eScriptLanguageNone;
1012 }
1013 
1014 void
1015 SBDebugger::SetScriptLanguage (ScriptLanguage script_lang)
1016 {
1017     if (m_opaque_sp)
1018     {
1019         m_opaque_sp->SetScriptLanguage (script_lang);
1020     }
1021 }
1022 
1023 bool
1024 SBDebugger::SetUseExternalEditor (bool value)
1025 {
1026     if (m_opaque_sp)
1027         return m_opaque_sp->SetUseExternalEditor (value);
1028     return false;
1029 }
1030 
1031 bool
1032 SBDebugger::GetUseExternalEditor ()
1033 {
1034     if (m_opaque_sp)
1035         return m_opaque_sp->GetUseExternalEditor ();
1036     return false;
1037 }
1038 
1039 bool
1040 SBDebugger::GetDescription (SBStream &description)
1041 {
1042     Stream &strm = description.ref();
1043 
1044     if (m_opaque_sp)
1045     {
1046         const char *name = m_opaque_sp->GetInstanceName().AsCString();
1047         user_id_t id = m_opaque_sp->GetID();
1048         strm.Printf ("Debugger (instance: \"%s\", id: %llu)", name, id);
1049     }
1050     else
1051         strm.PutCString ("No value");
1052 
1053     return true;
1054 }
1055 
1056 user_id_t
1057 SBDebugger::GetID()
1058 {
1059     if (m_opaque_sp)
1060         return m_opaque_sp->GetID();
1061     return LLDB_INVALID_UID;
1062 }
1063 
1064 
1065 SBError
1066 SBDebugger::SetCurrentPlatform (const char *platform_name)
1067 {
1068     SBError sb_error;
1069     if (m_opaque_sp)
1070     {
1071         PlatformSP platform_sp (Platform::Create (platform_name, sb_error.ref()));
1072 
1073         if (platform_sp)
1074         {
1075             bool make_selected = true;
1076             m_opaque_sp->GetPlatformList().Append (platform_sp, make_selected);
1077         }
1078     }
1079     return sb_error;
1080 }
1081 
1082 bool
1083 SBDebugger::SetCurrentPlatformSDKRoot (const char *sysroot)
1084 {
1085     if (m_opaque_sp)
1086     {
1087         PlatformSP platform_sp (m_opaque_sp->GetPlatformList().GetSelectedPlatform());
1088 
1089         if (platform_sp)
1090         {
1091             platform_sp->SetSDKRootDirectory (ConstString (sysroot));
1092             return true;
1093         }
1094     }
1095     return false;
1096 }
1097 
1098 bool
1099 SBDebugger::GetCloseInputOnEOF () const
1100 {
1101     if (m_opaque_sp)
1102         return m_opaque_sp->GetCloseInputOnEOF ();
1103     return false;
1104 }
1105 
1106 void
1107 SBDebugger::SetCloseInputOnEOF (bool b)
1108 {
1109     if (m_opaque_sp)
1110         m_opaque_sp->SetCloseInputOnEOF (b);
1111 }
1112 
1113 SBTypeCategory
1114 SBDebugger::GetCategory (const char* category_name)
1115 {
1116     if (!category_name || *category_name == 0)
1117         return SBTypeCategory();
1118 
1119     TypeCategoryImplSP category_sp;
1120 
1121     if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, false))
1122         return SBTypeCategory(category_sp);
1123     else
1124         return SBTypeCategory();
1125 }
1126 
1127 SBTypeCategory
1128 SBDebugger::CreateCategory (const char* category_name)
1129 {
1130     if (!category_name || *category_name == 0)
1131         return SBTypeCategory();
1132 
1133     TypeCategoryImplSP category_sp;
1134 
1135     if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, true))
1136         return SBTypeCategory(category_sp);
1137     else
1138         return SBTypeCategory();
1139 }
1140 
1141 bool
1142 SBDebugger::DeleteCategory (const char* category_name)
1143 {
1144     if (!category_name || *category_name == 0)
1145         return false;
1146 
1147     return DataVisualization::Categories::Delete(ConstString(category_name));
1148 }
1149 
1150 uint32_t
1151 SBDebugger::GetNumCategories()
1152 {
1153     return DataVisualization::Categories::GetCount();
1154 }
1155 
1156 SBTypeCategory
1157 SBDebugger::GetCategoryAtIndex (uint32_t index)
1158 {
1159     return SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index));
1160 }
1161 
1162 SBTypeCategory
1163 SBDebugger::GetDefaultCategory()
1164 {
1165     return GetCategory("default");
1166 }
1167 
1168 SBTypeFormat
1169 SBDebugger::GetFormatForType (SBTypeNameSpecifier type_name)
1170 {
1171     SBTypeCategory default_category_sb = GetDefaultCategory();
1172     if (default_category_sb.GetEnabled())
1173         return default_category_sb.GetFormatForType(type_name);
1174     return SBTypeFormat();
1175 }
1176 
1177 #ifndef LLDB_DISABLE_PYTHON
1178 SBTypeSummary
1179 SBDebugger::GetSummaryForType (SBTypeNameSpecifier type_name)
1180 {
1181     if (type_name.IsValid() == false)
1182         return SBTypeSummary();
1183     return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1184 }
1185 #endif // LLDB_DISABLE_PYTHON
1186 
1187 SBTypeFilter
1188 SBDebugger::GetFilterForType (SBTypeNameSpecifier type_name)
1189 {
1190     if (type_name.IsValid() == false)
1191         return SBTypeFilter();
1192     return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1193 }
1194 
1195 #ifndef LLDB_DISABLE_PYTHON
1196 SBTypeSynthetic
1197 SBDebugger::GetSyntheticForType (SBTypeNameSpecifier type_name)
1198 {
1199     if (type_name.IsValid() == false)
1200         return SBTypeSynthetic();
1201     return SBTypeSynthetic(DataVisualization::GetSyntheticForType(type_name.GetSP()));
1202 }
1203 #endif // LLDB_DISABLE_PYTHON
1204 
1205 bool
1206 SBDebugger::EnableLog (const char *channel, const char **categories)
1207 {
1208     if (m_opaque_sp)
1209     {
1210         uint32_t log_options = LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1211         StreamString errors;
1212         return m_opaque_sp->EnableLog (channel, categories, NULL, log_options, errors);
1213 
1214     }
1215     else
1216         return false;
1217 }
1218 
1219 void
1220 SBDebugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1221 {
1222     if (m_opaque_sp)
1223     {
1224         return m_opaque_sp->SetLoggingCallback (log_callback, baton);
1225     }
1226 }
1227 
1228 
1229