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