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