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