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             if (process.IsValid())
306             {
307                 EventSP event_sp;
308                 Listener &lldb_listener = m_opaque_sp->GetListener();
309                 while (lldb_listener.GetNextEventForBroadcaster (process.get(), event_sp))
310                 {
311                     SBEvent event(event_sp);
312                     HandleProcessEvent (process, event, GetOutputFileHandle(), GetErrorFileHandle());
313                 }
314             }
315         }
316     }
317 }
318 
319 SBListener
320 SBDebugger::GetListener ()
321 {
322     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
323 
324     SBListener sb_listener;
325     if (m_opaque_sp)
326         sb_listener.reset(&m_opaque_sp->GetListener(), false);
327 
328     if (log)
329         log->Printf ("SBDebugger(%p)::GetListener () => SBListener(%p)", m_opaque_sp.get(),
330                      sb_listener.get());
331 
332     return sb_listener;
333 }
334 
335 void
336 SBDebugger::HandleProcessEvent (const SBProcess &process, const SBEvent &event, FILE *out, FILE *err)
337 {
338     if (!process.IsValid())
339         return;
340 
341     const uint32_t event_type = event.GetType();
342     char stdio_buffer[1024];
343     size_t len;
344 
345     Mutex::Locker api_locker (process.GetTarget()->GetAPIMutex());
346 
347     if (event_type & (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged))
348     {
349         // Drain stdout when we stop just in case we have any bytes
350         while ((len = process.GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0)
351             if (out != NULL)
352                 ::fwrite (stdio_buffer, 1, len, out);
353     }
354 
355     if (event_type & (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged))
356     {
357         // Drain stderr when we stop just in case we have any bytes
358         while ((len = process.GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0)
359             if (err != NULL)
360                 ::fwrite (stdio_buffer, 1, len, err);
361     }
362 
363     if (event_type & Process::eBroadcastBitStateChanged)
364     {
365         StateType event_state = SBProcess::GetStateFromEvent (event);
366 
367         if (event_state == eStateInvalid)
368             return;
369 
370         bool is_stopped = StateIsStoppedState (event_state);
371         if (!is_stopped)
372             process.ReportEventState (event, out);
373     }
374 }
375 
376 SBSourceManager
377 SBDebugger::GetSourceManager ()
378 {
379     SBSourceManager sb_source_manager (*this);
380     return sb_source_manager;
381 }
382 
383 
384 bool
385 SBDebugger::GetDefaultArchitecture (char *arch_name, size_t arch_name_len)
386 {
387     if (arch_name && arch_name_len)
388     {
389         ArchSpec default_arch = Target::GetDefaultArchitecture ();
390 
391         if (default_arch.IsValid())
392         {
393             const std::string &triple_str = default_arch.GetTriple().str();
394             if (!triple_str.empty())
395                 ::snprintf (arch_name, arch_name_len, "%s", triple_str.c_str());
396             else
397                 ::snprintf (arch_name, arch_name_len, "%s", default_arch.GetArchitectureName());
398             return true;
399         }
400     }
401     if (arch_name && arch_name_len)
402         arch_name[0] = '\0';
403     return false;
404 }
405 
406 
407 bool
408 SBDebugger::SetDefaultArchitecture (const char *arch_name)
409 {
410     if (arch_name)
411     {
412         ArchSpec arch (arch_name, NULL);
413         if (arch.IsValid())
414         {
415             Target::SetDefaultArchitecture (arch);
416             return true;
417         }
418     }
419     return false;
420 }
421 
422 ScriptLanguage
423 SBDebugger::GetScriptingLanguage (const char *script_language_name)
424 {
425 
426     return Args::StringToScriptLanguage (script_language_name,
427                                          eScriptLanguageDefault,
428                                          NULL);
429 }
430 
431 const char *
432 SBDebugger::GetVersionString ()
433 {
434     return GetVersion();
435 }
436 
437 const char *
438 SBDebugger::StateAsCString (StateType state)
439 {
440     return lldb_private::StateAsCString (state);
441 }
442 
443 bool
444 SBDebugger::StateIsRunningState (StateType state)
445 {
446     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
447 
448     const bool result = lldb_private::StateIsRunningState (state);
449     if (log)
450         log->Printf ("SBDebugger::StateIsRunningState (state=%s) => %i",
451                      StateAsCString (state), result);
452 
453     return result;
454 }
455 
456 bool
457 SBDebugger::StateIsStoppedState (StateType state)
458 {
459     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
460 
461     const bool result = lldb_private::StateIsStoppedState (state, false);
462     if (log)
463         log->Printf ("SBDebugger::StateIsStoppedState (state=%s) => %i",
464                      StateAsCString (state), result);
465 
466     return result;
467 }
468 
469 lldb::SBTarget
470 SBDebugger::CreateTarget (const char *filename,
471                           const char *target_triple,
472                           const char *platform_name,
473                           bool add_dependent_modules,
474                           lldb::SBError& sb_error)
475 {
476     SBTarget sb_target;
477     if (m_opaque_sp)
478     {
479         sb_error.Clear();
480         FileSpec filename_spec (filename, true);
481         OptionGroupPlatform platform_options (false);
482         platform_options.SetPlatformName (platform_name);
483 
484         TargetSP target_sp;
485         sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
486                                                                     filename_spec,
487                                                                     target_triple,
488                                                                     add_dependent_modules,
489                                                                     &platform_options,
490                                                                     target_sp);
491 
492         if (sb_error.Success())
493             sb_target.reset (target_sp);
494     }
495     else
496     {
497         sb_error.SetErrorString("invalid target");
498     }
499 
500     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
501     if (log)
502     {
503         log->Printf ("SBDebugger(%p)::CreateTarget (filename=\"%s\", triple=%s, platform_name=%s, add_dependent_modules=%u, error=%s) => SBTarget(%p)",
504                      m_opaque_sp.get(),
505                      filename,
506                      target_triple,
507                      platform_name,
508                      add_dependent_modules,
509                      sb_error.GetCString(),
510                      sb_target.get());
511     }
512 
513     return sb_target;
514 }
515 
516 SBTarget
517 SBDebugger::CreateTargetWithFileAndTargetTriple (const char *filename,
518                                                  const char *target_triple)
519 {
520     SBTarget target;
521     if (m_opaque_sp)
522     {
523         FileSpec file_spec (filename, true);
524         TargetSP target_sp;
525         const bool add_dependent_modules = true;
526         Error error (m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
527                                                                 file_spec,
528                                                                 target_triple,
529                                                                 add_dependent_modules,
530                                                                 NULL,
531                                                                 target_sp));
532         target.reset (target_sp);
533     }
534 
535     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
536     if (log)
537     {
538         log->Printf ("SBDebugger(%p)::CreateTargetWithFileAndTargetTriple (filename=\"%s\", triple=%s) => SBTarget(%p)",
539                      m_opaque_sp.get(), filename, target_triple, target.get());
540     }
541 
542     return target;
543 }
544 
545 SBTarget
546 SBDebugger::CreateTargetWithFileAndArch (const char *filename, const char *arch_cstr)
547 {
548     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
549 
550     SBTarget target;
551     if (m_opaque_sp)
552     {
553         FileSpec file (filename, true);
554         TargetSP target_sp;
555         Error error;
556         const bool add_dependent_modules = true;
557 
558         error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
559                                                            file,
560                                                            arch_cstr,
561                                                            add_dependent_modules,
562                                                            NULL,
563                                                            target_sp);
564 
565         if (error.Success())
566         {
567             m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
568             target.reset(target_sp);
569         }
570     }
571 
572     if (log)
573     {
574         log->Printf ("SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", arch=%s) => SBTarget(%p)",
575                      m_opaque_sp.get(), filename, arch_cstr, target.get());
576     }
577 
578     return target;
579 }
580 
581 SBTarget
582 SBDebugger::CreateTarget (const char *filename)
583 {
584     SBTarget target;
585     if (m_opaque_sp)
586     {
587         FileSpec file (filename, true);
588         ArchSpec arch = Target::GetDefaultArchitecture ();
589         TargetSP target_sp;
590         Error error;
591         const bool add_dependent_modules = true;
592 
593         error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
594                                                            file,
595                                                            arch,
596                                                            add_dependent_modules,
597                                                            m_opaque_sp->GetPlatformList().GetSelectedPlatform(),
598                                                            target_sp);
599 
600         if (error.Success())
601         {
602             m_opaque_sp->GetTargetList().SetSelectedTarget (target_sp.get());
603             target.reset (target_sp);
604         }
605     }
606     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
607     if (log)
608     {
609         log->Printf ("SBDebugger(%p)::CreateTarget (filename=\"%s\") => SBTarget(%p)",
610                      m_opaque_sp.get(), filename, target.get());
611     }
612     return target;
613 }
614 
615 bool
616 SBDebugger::DeleteTarget (lldb::SBTarget &target)
617 {
618     bool result = false;
619     if (m_opaque_sp && target.IsValid())
620     {
621         // No need to lock, the target list is thread safe
622         result = m_opaque_sp->GetTargetList().DeleteTarget (target.m_opaque_sp);
623         target->Destroy();
624         target.Clear();
625         ModuleList::RemoveOrphanSharedModules();
626     }
627 
628     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
629     if (log)
630     {
631         log->Printf ("SBDebugger(%p)::DeleteTarget (SBTarget(%p)) => %i", m_opaque_sp.get(), target.m_opaque_sp.get(), result);
632     }
633 
634     return result;
635 }
636 SBTarget
637 SBDebugger::GetTargetAtIndex (uint32_t idx)
638 {
639     SBTarget sb_target;
640     if (m_opaque_sp)
641     {
642         // No need to lock, the target list is thread safe
643         sb_target.reset(m_opaque_sp->GetTargetList().GetTargetAtIndex (idx));
644     }
645     return sb_target;
646 }
647 
648 SBTarget
649 SBDebugger::FindTargetWithProcessID (pid_t pid)
650 {
651     SBTarget sb_target;
652     if (m_opaque_sp)
653     {
654         // No need to lock, the target list is thread safe
655         sb_target.reset(m_opaque_sp->GetTargetList().FindTargetWithProcessID (pid));
656     }
657     return sb_target;
658 }
659 
660 SBTarget
661 SBDebugger::FindTargetWithFileAndArch (const char *filename, const char *arch_name)
662 {
663     SBTarget sb_target;
664     if (m_opaque_sp && filename && filename[0])
665     {
666         // No need to lock, the target list is thread safe
667         ArchSpec arch (arch_name, m_opaque_sp->GetPlatformList().GetSelectedPlatform().get());
668         TargetSP target_sp (m_opaque_sp->GetTargetList().FindTargetWithExecutableAndArchitecture (FileSpec(filename, false), arch_name ? &arch : NULL));
669         sb_target.reset(target_sp);
670     }
671     return sb_target;
672 }
673 
674 SBTarget
675 SBDebugger::FindTargetWithLLDBProcess (const ProcessSP &process_sp)
676 {
677     SBTarget sb_target;
678     if (m_opaque_sp)
679     {
680         // No need to lock, the target list is thread safe
681         sb_target.reset(m_opaque_sp->GetTargetList().FindTargetWithProcess (process_sp.get()));
682     }
683     return sb_target;
684 }
685 
686 
687 uint32_t
688 SBDebugger::GetNumTargets ()
689 {
690     if (m_opaque_sp)
691     {
692         // No need to lock, the target list is thread safe
693         return m_opaque_sp->GetTargetList().GetNumTargets ();
694     }
695     return 0;
696 }
697 
698 SBTarget
699 SBDebugger::GetSelectedTarget ()
700 {
701     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
702 
703     SBTarget sb_target;
704     if (m_opaque_sp)
705     {
706         // No need to lock, the target list is thread safe
707         sb_target.reset(m_opaque_sp->GetTargetList().GetSelectedTarget ());
708     }
709 
710     if (log)
711     {
712         SBStream sstr;
713         sb_target.GetDescription (sstr, eDescriptionLevelBrief);
714         log->Printf ("SBDebugger(%p)::GetSelectedTarget () => SBTarget(%p): %s", m_opaque_sp.get(),
715                      sb_target.get(), sstr.GetData());
716     }
717 
718     return sb_target;
719 }
720 
721 void
722 SBDebugger::SetSelectedTarget (SBTarget &sb_target)
723 {
724     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
725 
726     if (m_opaque_sp)
727     {
728         m_opaque_sp->GetTargetList().SetSelectedTarget (sb_target.get());
729     }
730     if (log)
731     {
732         SBStream sstr;
733         sb_target.GetDescription (sstr, eDescriptionLevelBrief);
734         log->Printf ("SBDebugger(%p)::SetSelectedTarget () => SBTarget(%p): %s", m_opaque_sp.get(),
735                      sb_target.get(), sstr.GetData());
736     }
737 }
738 
739 void
740 SBDebugger::DispatchInput (void *baton, const void *data, size_t data_len)
741 {
742     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
743 
744     if (log)
745         log->Printf ("SBDebugger(%p)::DispatchInput (baton=%p, data=\"%.*s\", size_t=%zu)", m_opaque_sp.get(),
746                      baton, (int) data_len, (const char *) data, data_len);
747 
748     if (m_opaque_sp)
749         m_opaque_sp->DispatchInput ((const char *) data, data_len);
750 }
751 
752 void
753 SBDebugger::DispatchInputInterrupt ()
754 {
755     if (m_opaque_sp)
756         m_opaque_sp->DispatchInputInterrupt ();
757 }
758 
759 void
760 SBDebugger::DispatchInputEndOfFile ()
761 {
762     if (m_opaque_sp)
763         m_opaque_sp->DispatchInputEndOfFile ();
764 }
765 
766 bool
767 SBDebugger::InputReaderIsTopReader (const lldb::SBInputReader &reader)
768 {
769     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
770 
771     if (log)
772         log->Printf ("SBDebugger(%p)::InputReaderIsTopReader (SBInputReader(%p))", m_opaque_sp.get(), &reader);
773 
774     if (m_opaque_sp && reader.IsValid())
775     {
776         InputReaderSP reader_sp (*reader);
777         return m_opaque_sp->InputReaderIsTopReader (reader_sp);
778     }
779 
780     return false;
781 }
782 
783 
784 void
785 SBDebugger::PushInputReader (SBInputReader &reader)
786 {
787     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
788 
789     if (log)
790         log->Printf ("SBDebugger(%p)::PushInputReader (SBInputReader(%p))", m_opaque_sp.get(), &reader);
791 
792     if (m_opaque_sp && reader.IsValid())
793     {
794         TargetSP target_sp (m_opaque_sp->GetSelectedTarget());
795         Mutex::Locker api_locker;
796         if (target_sp)
797             api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
798         InputReaderSP reader_sp(*reader);
799         m_opaque_sp->PushInputReader (reader_sp);
800     }
801 }
802 
803 void
804 SBDebugger::NotifyTopInputReader (InputReaderAction notification)
805 {
806     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
807 
808     if (log)
809         log->Printf ("SBDebugger(%p)::NotifyTopInputReader (%d)", m_opaque_sp.get(), notification);
810 
811     if (m_opaque_sp)
812         m_opaque_sp->NotifyTopInputReader (notification);
813 }
814 
815 void
816 SBDebugger::reset (const DebuggerSP &debugger_sp)
817 {
818     m_opaque_sp = debugger_sp;
819 }
820 
821 Debugger *
822 SBDebugger::get () const
823 {
824     return m_opaque_sp.get();
825 }
826 
827 Debugger &
828 SBDebugger::ref () const
829 {
830     assert (m_opaque_sp.get());
831     return *m_opaque_sp;
832 }
833 
834 const lldb::DebuggerSP &
835 SBDebugger::get_sp () const
836 {
837     return m_opaque_sp;
838 }
839 
840 SBDebugger
841 SBDebugger::FindDebuggerWithID (int id)
842 {
843     // No need to lock, the debugger list is thread safe
844     SBDebugger sb_debugger;
845     DebuggerSP debugger_sp = Debugger::FindDebuggerWithID (id);
846     if (debugger_sp)
847         sb_debugger.reset (debugger_sp);
848     return sb_debugger;
849 }
850 
851 const char *
852 SBDebugger::GetInstanceName()
853 {
854     if (m_opaque_sp)
855         return m_opaque_sp->GetInstanceName().AsCString();
856     else
857         return NULL;
858 }
859 
860 SBError
861 SBDebugger::SetInternalVariable (const char *var_name, const char *value, const char *debugger_instance_name)
862 {
863     UserSettingsControllerSP root_settings_controller = Debugger::GetSettingsController();
864 
865     Error err = root_settings_controller->SetVariable (var_name,
866                                                        value,
867                                                        eVarSetOperationAssign,
868                                                        true,
869                                                        debugger_instance_name);
870     SBError sb_error;
871     sb_error.SetError (err);
872 
873     return sb_error;
874 }
875 
876 SBStringList
877 SBDebugger::GetInternalVariableValue (const char *var_name, const char *debugger_instance_name)
878 {
879     SBStringList ret_value;
880     SettableVariableType var_type;
881     Error err;
882 
883     UserSettingsControllerSP root_settings_controller = Debugger::GetSettingsController();
884 
885     StringList value = root_settings_controller->GetVariable (var_name, var_type, debugger_instance_name, err);
886 
887     if (err.Success())
888     {
889         for (unsigned i = 0; i != value.GetSize(); ++i)
890             ret_value.AppendString (value.GetStringAtIndex(i));
891     }
892     else
893     {
894         ret_value.AppendString (err.AsCString());
895     }
896 
897 
898     return ret_value;
899 }
900 
901 uint32_t
902 SBDebugger::GetTerminalWidth () const
903 {
904     if (m_opaque_sp)
905         return m_opaque_sp->GetTerminalWidth ();
906     return 0;
907 }
908 
909 void
910 SBDebugger::SetTerminalWidth (uint32_t term_width)
911 {
912     if (m_opaque_sp)
913         m_opaque_sp->SetTerminalWidth (term_width);
914 }
915 
916 const char *
917 SBDebugger::GetPrompt() const
918 {
919     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
920 
921     if (log)
922         log->Printf ("SBDebugger(%p)::GetPrompt () => \"%s\"", m_opaque_sp.get(),
923                      (m_opaque_sp ? m_opaque_sp->GetPrompt() : ""));
924 
925     if (m_opaque_sp)
926         return m_opaque_sp->GetPrompt ();
927     return 0;
928 }
929 
930 void
931 SBDebugger::SetPrompt (const char *prompt)
932 {
933     if (m_opaque_sp)
934         m_opaque_sp->SetPrompt (prompt);
935 }
936 
937 
938 ScriptLanguage
939 SBDebugger::GetScriptLanguage() const
940 {
941     if (m_opaque_sp)
942         return m_opaque_sp->GetScriptLanguage ();
943     return eScriptLanguageNone;
944 }
945 
946 void
947 SBDebugger::SetScriptLanguage (ScriptLanguage script_lang)
948 {
949     if (m_opaque_sp)
950     {
951         m_opaque_sp->SetScriptLanguage (script_lang);
952     }
953 }
954 
955 bool
956 SBDebugger::SetUseExternalEditor (bool value)
957 {
958     if (m_opaque_sp)
959         return m_opaque_sp->SetUseExternalEditor (value);
960     return false;
961 }
962 
963 bool
964 SBDebugger::GetUseExternalEditor ()
965 {
966     if (m_opaque_sp)
967         return m_opaque_sp->GetUseExternalEditor ();
968     return false;
969 }
970 
971 bool
972 SBDebugger::GetDescription (SBStream &description)
973 {
974     Stream &strm = description.ref();
975 
976     if (m_opaque_sp)
977     {
978         const char *name = m_opaque_sp->GetInstanceName().AsCString();
979         user_id_t id = m_opaque_sp->GetID();
980         strm.Printf ("Debugger (instance: \"%s\", id: %llu)", name, id);
981     }
982     else
983         strm.PutCString ("No value");
984 
985     return true;
986 }
987 
988 user_id_t
989 SBDebugger::GetID()
990 {
991     if (m_opaque_sp)
992         return m_opaque_sp->GetID();
993     return LLDB_INVALID_UID;
994 }
995 
996 
997 SBError
998 SBDebugger::SetCurrentPlatform (const char *platform_name)
999 {
1000     SBError sb_error;
1001     if (m_opaque_sp)
1002     {
1003         PlatformSP platform_sp (Platform::Create (platform_name, sb_error.ref()));
1004 
1005         if (platform_sp)
1006         {
1007             bool make_selected = true;
1008             m_opaque_sp->GetPlatformList().Append (platform_sp, make_selected);
1009         }
1010     }
1011     return sb_error;
1012 }
1013 
1014 bool
1015 SBDebugger::SetCurrentPlatformSDKRoot (const char *sysroot)
1016 {
1017     if (m_opaque_sp)
1018     {
1019         PlatformSP platform_sp (m_opaque_sp->GetPlatformList().GetSelectedPlatform());
1020 
1021         if (platform_sp)
1022         {
1023             platform_sp->SetSDKRootDirectory (ConstString (sysroot));
1024             return true;
1025         }
1026     }
1027     return false;
1028 }
1029 
1030 bool
1031 SBDebugger::GetCloseInputOnEOF () const
1032 {
1033     if (m_opaque_sp)
1034         return m_opaque_sp->GetCloseInputOnEOF ();
1035     return false;
1036 }
1037 
1038 void
1039 SBDebugger::SetCloseInputOnEOF (bool b)
1040 {
1041     if (m_opaque_sp)
1042         m_opaque_sp->SetCloseInputOnEOF (b);
1043 }
1044