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