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