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     DispatchInput (data,data_len);
800 }
801 
802 void
803 SBDebugger::DispatchInput (const void *data, size_t data_len)
804 {
805     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
806 
807     if (log)
808         log->Printf ("SBDebugger(%p)::DispatchInput (data=\"%.*s\", size_t=%llu)",
809                      m_opaque_sp.get(),
810                      (int) data_len,
811                      (const char *) data,
812                      (uint64_t)data_len);
813 
814     if (m_opaque_sp)
815         m_opaque_sp->DispatchInput ((const char *) data, data_len);
816 }
817 
818 void
819 SBDebugger::DispatchInputInterrupt ()
820 {
821     if (m_opaque_sp)
822         m_opaque_sp->DispatchInputInterrupt ();
823 }
824 
825 void
826 SBDebugger::DispatchInputEndOfFile ()
827 {
828     if (m_opaque_sp)
829         m_opaque_sp->DispatchInputEndOfFile ();
830 }
831 
832 bool
833 SBDebugger::InputReaderIsTopReader (const lldb::SBInputReader &reader)
834 {
835     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
836 
837     if (log)
838         log->Printf ("SBDebugger(%p)::InputReaderIsTopReader (SBInputReader(%p))", m_opaque_sp.get(), &reader);
839 
840     if (m_opaque_sp && reader.IsValid())
841     {
842         InputReaderSP reader_sp (*reader);
843         return m_opaque_sp->InputReaderIsTopReader (reader_sp);
844     }
845 
846     return false;
847 }
848 
849 
850 void
851 SBDebugger::PushInputReader (SBInputReader &reader)
852 {
853     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
854 
855     if (log)
856         log->Printf ("SBDebugger(%p)::PushInputReader (SBInputReader(%p))", m_opaque_sp.get(), &reader);
857 
858     if (m_opaque_sp && reader.IsValid())
859     {
860         TargetSP target_sp (m_opaque_sp->GetSelectedTarget());
861         Mutex::Locker api_locker;
862         if (target_sp)
863             api_locker.Lock(target_sp->GetAPIMutex());
864         InputReaderSP reader_sp(*reader);
865         m_opaque_sp->PushInputReader (reader_sp);
866     }
867 }
868 
869 void
870 SBDebugger::NotifyTopInputReader (InputReaderAction notification)
871 {
872     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
873 
874     if (log)
875         log->Printf ("SBDebugger(%p)::NotifyTopInputReader (%d)", m_opaque_sp.get(), notification);
876 
877     if (m_opaque_sp)
878         m_opaque_sp->NotifyTopInputReader (notification);
879 }
880 
881 void
882 SBDebugger::reset (const DebuggerSP &debugger_sp)
883 {
884     m_opaque_sp = debugger_sp;
885 }
886 
887 Debugger *
888 SBDebugger::get () const
889 {
890     return m_opaque_sp.get();
891 }
892 
893 Debugger &
894 SBDebugger::ref () const
895 {
896     assert (m_opaque_sp.get());
897     return *m_opaque_sp;
898 }
899 
900 const lldb::DebuggerSP &
901 SBDebugger::get_sp () const
902 {
903     return m_opaque_sp;
904 }
905 
906 SBDebugger
907 SBDebugger::FindDebuggerWithID (int id)
908 {
909     // No need to lock, the debugger list is thread safe
910     SBDebugger sb_debugger;
911     DebuggerSP debugger_sp = Debugger::FindDebuggerWithID (id);
912     if (debugger_sp)
913         sb_debugger.reset (debugger_sp);
914     return sb_debugger;
915 }
916 
917 const char *
918 SBDebugger::GetInstanceName()
919 {
920     if (m_opaque_sp)
921         return m_opaque_sp->GetInstanceName().AsCString();
922     else
923         return NULL;
924 }
925 
926 SBError
927 SBDebugger::SetInternalVariable (const char *var_name, const char *value, const char *debugger_instance_name)
928 {
929     SBError sb_error;
930     DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name)));
931     Error error;
932     if (debugger_sp)
933     {
934         ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext());
935         error = debugger_sp->SetPropertyValue (&exe_ctx,
936                                                eVarSetOperationAssign,
937                                                var_name,
938                                                value);
939     }
940     else
941     {
942         error.SetErrorStringWithFormat ("invalid debugger instance name '%s'", debugger_instance_name);
943     }
944     if (error.Fail())
945         sb_error.SetError(error);
946     return sb_error;
947 }
948 
949 SBStringList
950 SBDebugger::GetInternalVariableValue (const char *var_name, const char *debugger_instance_name)
951 {
952     SBStringList ret_value;
953     DebuggerSP debugger_sp(Debugger::FindDebuggerWithInstanceName (ConstString(debugger_instance_name)));
954     Error error;
955     if (debugger_sp)
956     {
957         ExecutionContext exe_ctx (debugger_sp->GetCommandInterpreter().GetExecutionContext());
958         lldb::OptionValueSP value_sp (debugger_sp->GetPropertyValue (&exe_ctx,
959                                                                      var_name,
960                                                                      false,
961                                                                      error));
962         if (value_sp)
963         {
964             StreamString value_strm;
965             value_sp->DumpValue (&exe_ctx, value_strm, OptionValue::eDumpOptionValue);
966             const std::string &value_str = value_strm.GetString();
967             if (!value_str.empty())
968             {
969                 StringList string_list;
970                 string_list.SplitIntoLines(value_str.c_str(), value_str.size());
971                 return SBStringList(&string_list);
972             }
973         }
974     }
975     return SBStringList();
976 }
977 
978 uint32_t
979 SBDebugger::GetTerminalWidth () const
980 {
981     if (m_opaque_sp)
982         return m_opaque_sp->GetTerminalWidth ();
983     return 0;
984 }
985 
986 void
987 SBDebugger::SetTerminalWidth (uint32_t term_width)
988 {
989     if (m_opaque_sp)
990         m_opaque_sp->SetTerminalWidth (term_width);
991 }
992 
993 const char *
994 SBDebugger::GetPrompt() const
995 {
996     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
997 
998     if (log)
999         log->Printf ("SBDebugger(%p)::GetPrompt () => \"%s\"", m_opaque_sp.get(),
1000                      (m_opaque_sp ? m_opaque_sp->GetPrompt() : ""));
1001 
1002     if (m_opaque_sp)
1003         return m_opaque_sp->GetPrompt ();
1004     return 0;
1005 }
1006 
1007 void
1008 SBDebugger::SetPrompt (const char *prompt)
1009 {
1010     if (m_opaque_sp)
1011         m_opaque_sp->SetPrompt (prompt);
1012 }
1013 
1014 
1015 ScriptLanguage
1016 SBDebugger::GetScriptLanguage() const
1017 {
1018     if (m_opaque_sp)
1019         return m_opaque_sp->GetScriptLanguage ();
1020     return eScriptLanguageNone;
1021 }
1022 
1023 void
1024 SBDebugger::SetScriptLanguage (ScriptLanguage script_lang)
1025 {
1026     if (m_opaque_sp)
1027     {
1028         m_opaque_sp->SetScriptLanguage (script_lang);
1029     }
1030 }
1031 
1032 bool
1033 SBDebugger::SetUseExternalEditor (bool value)
1034 {
1035     if (m_opaque_sp)
1036         return m_opaque_sp->SetUseExternalEditor (value);
1037     return false;
1038 }
1039 
1040 bool
1041 SBDebugger::GetUseExternalEditor ()
1042 {
1043     if (m_opaque_sp)
1044         return m_opaque_sp->GetUseExternalEditor ();
1045     return false;
1046 }
1047 
1048 bool
1049 SBDebugger::GetDescription (SBStream &description)
1050 {
1051     Stream &strm = description.ref();
1052 
1053     if (m_opaque_sp)
1054     {
1055         const char *name = m_opaque_sp->GetInstanceName().AsCString();
1056         user_id_t id = m_opaque_sp->GetID();
1057         strm.Printf ("Debugger (instance: \"%s\", id: %llu)", name, id);
1058     }
1059     else
1060         strm.PutCString ("No value");
1061 
1062     return true;
1063 }
1064 
1065 user_id_t
1066 SBDebugger::GetID()
1067 {
1068     if (m_opaque_sp)
1069         return m_opaque_sp->GetID();
1070     return LLDB_INVALID_UID;
1071 }
1072 
1073 
1074 SBError
1075 SBDebugger::SetCurrentPlatform (const char *platform_name)
1076 {
1077     SBError sb_error;
1078     if (m_opaque_sp)
1079     {
1080         PlatformSP platform_sp (Platform::Create (platform_name, sb_error.ref()));
1081 
1082         if (platform_sp)
1083         {
1084             bool make_selected = true;
1085             m_opaque_sp->GetPlatformList().Append (platform_sp, make_selected);
1086         }
1087     }
1088     return sb_error;
1089 }
1090 
1091 bool
1092 SBDebugger::SetCurrentPlatformSDKRoot (const char *sysroot)
1093 {
1094     if (m_opaque_sp)
1095     {
1096         PlatformSP platform_sp (m_opaque_sp->GetPlatformList().GetSelectedPlatform());
1097 
1098         if (platform_sp)
1099         {
1100             platform_sp->SetSDKRootDirectory (ConstString (sysroot));
1101             return true;
1102         }
1103     }
1104     return false;
1105 }
1106 
1107 bool
1108 SBDebugger::GetCloseInputOnEOF () const
1109 {
1110     if (m_opaque_sp)
1111         return m_opaque_sp->GetCloseInputOnEOF ();
1112     return false;
1113 }
1114 
1115 void
1116 SBDebugger::SetCloseInputOnEOF (bool b)
1117 {
1118     if (m_opaque_sp)
1119         m_opaque_sp->SetCloseInputOnEOF (b);
1120 }
1121 
1122 SBTypeCategory
1123 SBDebugger::GetCategory (const char* category_name)
1124 {
1125     if (!category_name || *category_name == 0)
1126         return SBTypeCategory();
1127 
1128     TypeCategoryImplSP category_sp;
1129 
1130     if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, false))
1131         return SBTypeCategory(category_sp);
1132     else
1133         return SBTypeCategory();
1134 }
1135 
1136 SBTypeCategory
1137 SBDebugger::CreateCategory (const char* category_name)
1138 {
1139     if (!category_name || *category_name == 0)
1140         return SBTypeCategory();
1141 
1142     TypeCategoryImplSP category_sp;
1143 
1144     if (DataVisualization::Categories::GetCategory(ConstString(category_name), category_sp, true))
1145         return SBTypeCategory(category_sp);
1146     else
1147         return SBTypeCategory();
1148 }
1149 
1150 bool
1151 SBDebugger::DeleteCategory (const char* category_name)
1152 {
1153     if (!category_name || *category_name == 0)
1154         return false;
1155 
1156     return DataVisualization::Categories::Delete(ConstString(category_name));
1157 }
1158 
1159 uint32_t
1160 SBDebugger::GetNumCategories()
1161 {
1162     return DataVisualization::Categories::GetCount();
1163 }
1164 
1165 SBTypeCategory
1166 SBDebugger::GetCategoryAtIndex (uint32_t index)
1167 {
1168     return SBTypeCategory(DataVisualization::Categories::GetCategoryAtIndex(index));
1169 }
1170 
1171 SBTypeCategory
1172 SBDebugger::GetDefaultCategory()
1173 {
1174     return GetCategory("default");
1175 }
1176 
1177 SBTypeFormat
1178 SBDebugger::GetFormatForType (SBTypeNameSpecifier type_name)
1179 {
1180     SBTypeCategory default_category_sb = GetDefaultCategory();
1181     if (default_category_sb.GetEnabled())
1182         return default_category_sb.GetFormatForType(type_name);
1183     return SBTypeFormat();
1184 }
1185 
1186 #ifndef LLDB_DISABLE_PYTHON
1187 SBTypeSummary
1188 SBDebugger::GetSummaryForType (SBTypeNameSpecifier type_name)
1189 {
1190     if (type_name.IsValid() == false)
1191         return SBTypeSummary();
1192     return SBTypeSummary(DataVisualization::GetSummaryForType(type_name.GetSP()));
1193 }
1194 #endif // LLDB_DISABLE_PYTHON
1195 
1196 SBTypeFilter
1197 SBDebugger::GetFilterForType (SBTypeNameSpecifier type_name)
1198 {
1199     if (type_name.IsValid() == false)
1200         return SBTypeFilter();
1201     return SBTypeFilter(DataVisualization::GetFilterForType(type_name.GetSP()));
1202 }
1203 
1204 #ifndef LLDB_DISABLE_PYTHON
1205 SBTypeSynthetic
1206 SBDebugger::GetSyntheticForType (SBTypeNameSpecifier type_name)
1207 {
1208     if (type_name.IsValid() == false)
1209         return SBTypeSynthetic();
1210     return SBTypeSynthetic(DataVisualization::GetSyntheticForType(type_name.GetSP()));
1211 }
1212 #endif // LLDB_DISABLE_PYTHON
1213 
1214 bool
1215 SBDebugger::EnableLog (const char *channel, const char **categories)
1216 {
1217     if (m_opaque_sp)
1218     {
1219         uint32_t log_options = LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
1220         StreamString errors;
1221         return m_opaque_sp->EnableLog (channel, categories, NULL, log_options, errors);
1222 
1223     }
1224     else
1225         return false;
1226 }
1227 
1228 void
1229 SBDebugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
1230 {
1231     if (m_opaque_sp)
1232     {
1233         return m_opaque_sp->SetLoggingCallback (log_callback, baton);
1234     }
1235 }
1236 
1237 
1238