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