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