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