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