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