1 //===-- SBTarget.cpp --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/lldb-python.h"
11 
12 #include "lldb/API/SBTarget.h"
13 
14 #include "lldb/lldb-public.h"
15 
16 #include "lldb/API/SBDebugger.h"
17 #include "lldb/API/SBBreakpoint.h"
18 #include "lldb/API/SBExpressionOptions.h"
19 #include "lldb/API/SBFileSpec.h"
20 #include "lldb/API/SBListener.h"
21 #include "lldb/API/SBModule.h"
22 #include "lldb/API/SBModuleSpec.h"
23 #include "lldb/API/SBSourceManager.h"
24 #include "lldb/API/SBProcess.h"
25 #include "lldb/API/SBStream.h"
26 #include "lldb/API/SBSymbolContextList.h"
27 #include "lldb/Breakpoint/BreakpointID.h"
28 #include "lldb/Breakpoint/BreakpointIDList.h"
29 #include "lldb/Breakpoint/BreakpointList.h"
30 #include "lldb/Breakpoint/BreakpointLocation.h"
31 #include "lldb/Core/Address.h"
32 #include "lldb/Core/AddressResolver.h"
33 #include "lldb/Core/AddressResolverName.h"
34 #include "lldb/Core/ArchSpec.h"
35 #include "lldb/Core/Debugger.h"
36 #include "lldb/Core/Disassembler.h"
37 #include "lldb/Core/Log.h"
38 #include "lldb/Core/Module.h"
39 #include "lldb/Core/ModuleSpec.h"
40 #include "lldb/Core/RegularExpression.h"
41 #include "lldb/Core/SearchFilter.h"
42 #include "lldb/Core/Section.h"
43 #include "lldb/Core/STLUtils.h"
44 #include "lldb/Core/ValueObjectConstResult.h"
45 #include "lldb/Core/ValueObjectList.h"
46 #include "lldb/Core/ValueObjectVariable.h"
47 #include "lldb/Host/FileSpec.h"
48 #include "lldb/Host/Host.h"
49 #include "lldb/Interpreter/Args.h"
50 #include "lldb/Symbol/ObjectFile.h"
51 #include "lldb/Symbol/SymbolVendor.h"
52 #include "lldb/Symbol/VariableList.h"
53 #include "lldb/Target/LanguageRuntime.h"
54 #include "lldb/Target/Process.h"
55 
56 #include "lldb/Target/Target.h"
57 #include "lldb/Target/TargetList.h"
58 
59 #include "lldb/Interpreter/CommandReturnObject.h"
60 #include "../source/Commands/CommandObjectBreakpoint.h"
61 #include "llvm/Support/Regex.h"
62 
63 
64 using namespace lldb;
65 using namespace lldb_private;
66 
67 #define DEFAULT_DISASM_BYTE_SIZE 32
68 
69 
70 SBAttachInfo::SBAttachInfo () :
71     m_opaque_sp (new ProcessAttachInfo())
72 {
73 }
74 
75 SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
76     m_opaque_sp (new ProcessAttachInfo())
77 {
78     m_opaque_sp->SetProcessID (pid);
79 }
80 
81 SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
82     m_opaque_sp (new ProcessAttachInfo())
83 {
84     if (path && path[0])
85         m_opaque_sp->GetExecutableFile().SetFile(path, false);
86     m_opaque_sp->SetWaitForLaunch (wait_for);
87 }
88 
89 SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
90     m_opaque_sp (new ProcessAttachInfo())
91 {
92     *m_opaque_sp = *rhs.m_opaque_sp;
93 }
94 
95 SBAttachInfo::~SBAttachInfo()
96 {
97 }
98 
99 lldb_private::ProcessAttachInfo &
100 SBAttachInfo::ref ()
101 {
102     return *m_opaque_sp;
103 }
104 
105 SBAttachInfo &
106 SBAttachInfo::operator = (const SBAttachInfo &rhs)
107 {
108     if (this != &rhs)
109         *m_opaque_sp = *rhs.m_opaque_sp;
110     return *this;
111 }
112 
113 lldb::pid_t
114 SBAttachInfo::GetProcessID ()
115 {
116     return m_opaque_sp->GetProcessID();
117 }
118 
119 void
120 SBAttachInfo::SetProcessID (lldb::pid_t pid)
121 {
122     m_opaque_sp->SetProcessID (pid);
123 }
124 
125 
126 uint32_t
127 SBAttachInfo::GetResumeCount ()
128 {
129     return m_opaque_sp->GetResumeCount();
130 }
131 
132 void
133 SBAttachInfo::SetResumeCount (uint32_t c)
134 {
135     m_opaque_sp->SetResumeCount (c);
136 }
137 
138 const char *
139 SBAttachInfo::GetProcessPluginName ()
140 {
141     return m_opaque_sp->GetProcessPluginName();
142 }
143 
144 void
145 SBAttachInfo::SetProcessPluginName (const char *plugin_name)
146 {
147     return m_opaque_sp->SetProcessPluginName (plugin_name);
148 }
149 
150 void
151 SBAttachInfo::SetExecutable (const char *path)
152 {
153     if (path && path[0])
154         m_opaque_sp->GetExecutableFile().SetFile(path, false);
155     else
156         m_opaque_sp->GetExecutableFile().Clear();
157 }
158 
159 void
160 SBAttachInfo::SetExecutable (SBFileSpec exe_file)
161 {
162     if (exe_file.IsValid())
163         m_opaque_sp->GetExecutableFile() = exe_file.ref();
164     else
165         m_opaque_sp->GetExecutableFile().Clear();
166 }
167 
168 bool
169 SBAttachInfo::GetWaitForLaunch ()
170 {
171     return m_opaque_sp->GetWaitForLaunch();
172 }
173 
174 void
175 SBAttachInfo::SetWaitForLaunch (bool b)
176 {
177     m_opaque_sp->SetWaitForLaunch (b);
178 }
179 
180 bool
181 SBAttachInfo::GetIgnoreExisting ()
182 {
183     return m_opaque_sp->GetIgnoreExisting();
184 }
185 
186 void
187 SBAttachInfo::SetIgnoreExisting (bool b)
188 {
189     m_opaque_sp->SetIgnoreExisting (b);
190 }
191 
192 uint32_t
193 SBAttachInfo::GetUserID()
194 {
195     return m_opaque_sp->GetUserID();
196 }
197 
198 uint32_t
199 SBAttachInfo::GetGroupID()
200 {
201     return m_opaque_sp->GetGroupID();
202 }
203 
204 bool
205 SBAttachInfo::UserIDIsValid ()
206 {
207     return m_opaque_sp->UserIDIsValid();
208 }
209 
210 bool
211 SBAttachInfo::GroupIDIsValid ()
212 {
213     return m_opaque_sp->GroupIDIsValid();
214 }
215 
216 void
217 SBAttachInfo::SetUserID (uint32_t uid)
218 {
219     m_opaque_sp->SetUserID (uid);
220 }
221 
222 void
223 SBAttachInfo::SetGroupID (uint32_t gid)
224 {
225     m_opaque_sp->SetGroupID (gid);
226 }
227 
228 uint32_t
229 SBAttachInfo::GetEffectiveUserID()
230 {
231     return m_opaque_sp->GetEffectiveUserID();
232 }
233 
234 uint32_t
235 SBAttachInfo::GetEffectiveGroupID()
236 {
237     return m_opaque_sp->GetEffectiveGroupID();
238 }
239 
240 bool
241 SBAttachInfo::EffectiveUserIDIsValid ()
242 {
243     return m_opaque_sp->EffectiveUserIDIsValid();
244 }
245 
246 bool
247 SBAttachInfo::EffectiveGroupIDIsValid ()
248 {
249     return m_opaque_sp->EffectiveGroupIDIsValid ();
250 }
251 
252 void
253 SBAttachInfo::SetEffectiveUserID (uint32_t uid)
254 {
255     m_opaque_sp->SetEffectiveUserID(uid);
256 }
257 
258 void
259 SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
260 {
261     m_opaque_sp->SetEffectiveGroupID(gid);
262 }
263 
264 lldb::pid_t
265 SBAttachInfo::GetParentProcessID ()
266 {
267     return m_opaque_sp->GetParentProcessID();
268 }
269 
270 void
271 SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
272 {
273     m_opaque_sp->SetParentProcessID (pid);
274 }
275 
276 bool
277 SBAttachInfo::ParentProcessIDIsValid()
278 {
279     return m_opaque_sp->ParentProcessIDIsValid();
280 }
281 
282 SBListener
283 SBAttachInfo::GetListener ()
284 {
285     return SBListener(m_opaque_sp->GetListener());
286 }
287 
288 void
289 SBAttachInfo::SetListener (SBListener &listener)
290 {
291     m_opaque_sp->SetListener(listener.GetSP());
292 }
293 
294 namespace {
295 
296 Error
297 AttachToProcess (ProcessAttachInfo &attach_info, Target &target)
298 {
299     Mutex::Locker api_locker (target.GetAPIMutex ());
300 
301     auto process_sp = target.GetProcessSP ();
302     if (process_sp)
303     {
304         const auto state = process_sp->GetState ();
305         if (process_sp->IsAlive () && state == eStateConnected)
306         {
307             // If we are already connected, then we have already specified the
308             // listener, so if a valid listener is supplied, we need to error out
309             // to let the client know.
310             if (attach_info.GetListener ())
311                 return Error ("process is connected and already has a listener, pass empty listener");
312         }
313     }
314 
315     return target.Attach (attach_info, nullptr);
316 }
317 
318 }  // namespace
319 
320 //----------------------------------------------------------------------
321 // SBTarget constructor
322 //----------------------------------------------------------------------
323 SBTarget::SBTarget () :
324     m_opaque_sp ()
325 {
326 }
327 
328 SBTarget::SBTarget (const SBTarget& rhs) :
329     m_opaque_sp (rhs.m_opaque_sp)
330 {
331 }
332 
333 SBTarget::SBTarget(const TargetSP& target_sp) :
334     m_opaque_sp (target_sp)
335 {
336 }
337 
338 const SBTarget&
339 SBTarget::operator = (const SBTarget& rhs)
340 {
341     if (this != &rhs)
342         m_opaque_sp = rhs.m_opaque_sp;
343     return *this;
344 }
345 
346 //----------------------------------------------------------------------
347 // Destructor
348 //----------------------------------------------------------------------
349 SBTarget::~SBTarget()
350 {
351 }
352 
353 const char *
354 SBTarget::GetBroadcasterClassName ()
355 {
356     return Target::GetStaticBroadcasterClass().AsCString();
357 }
358 
359 bool
360 SBTarget::IsValid () const
361 {
362     return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
363 }
364 
365 SBProcess
366 SBTarget::GetProcess ()
367 {
368     SBProcess sb_process;
369     ProcessSP process_sp;
370     TargetSP target_sp(GetSP());
371     if (target_sp)
372     {
373         process_sp = target_sp->GetProcessSP();
374         sb_process.SetSP (process_sp);
375     }
376 
377     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
378     if (log)
379         log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
380                      static_cast<void*>(target_sp.get()),
381                      static_cast<void*>(process_sp.get()));
382 
383     return sb_process;
384 }
385 
386 SBPlatform
387 SBTarget::GetPlatform ()
388 {
389     TargetSP target_sp(GetSP());
390     if (!target_sp)
391         return SBPlatform();
392 
393     SBPlatform platform;
394     platform.m_opaque_sp = target_sp->GetPlatform();
395 
396     return platform;
397 }
398 
399 SBDebugger
400 SBTarget::GetDebugger () const
401 {
402     SBDebugger debugger;
403     TargetSP target_sp(GetSP());
404     if (target_sp)
405         debugger.reset (target_sp->GetDebugger().shared_from_this());
406     return debugger;
407 }
408 
409 SBProcess
410 SBTarget::LoadCore (const char *core_file)
411 {
412     SBProcess sb_process;
413     TargetSP target_sp(GetSP());
414     if (target_sp)
415     {
416         FileSpec filespec(core_file, true);
417         ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
418                                                        NULL,
419                                                        &filespec));
420         if (process_sp)
421         {
422             process_sp->LoadCore();
423             sb_process.SetSP (process_sp);
424         }
425     }
426     return sb_process;
427 }
428 
429 SBProcess
430 SBTarget::LaunchSimple
431 (
432     char const **argv,
433     char const **envp,
434     const char *working_directory
435 )
436 {
437     char *stdin_path = NULL;
438     char *stdout_path = NULL;
439     char *stderr_path = NULL;
440     uint32_t launch_flags = 0;
441     bool stop_at_entry = false;
442     SBError error;
443     SBListener listener = GetDebugger().GetListener();
444     return Launch (listener,
445                    argv,
446                    envp,
447                    stdin_path,
448                    stdout_path,
449                    stderr_path,
450                    working_directory,
451                    launch_flags,
452                    stop_at_entry,
453                    error);
454 }
455 
456 SBError
457 SBTarget::Install()
458 {
459     SBError sb_error;
460     TargetSP target_sp(GetSP());
461     if (target_sp)
462     {
463         Mutex::Locker api_locker (target_sp->GetAPIMutex());
464         sb_error.ref() = target_sp->Install(NULL);
465     }
466     return sb_error;
467 }
468 
469 SBProcess
470 SBTarget::Launch
471 (
472     SBListener &listener,
473     char const **argv,
474     char const **envp,
475     const char *stdin_path,
476     const char *stdout_path,
477     const char *stderr_path,
478     const char *working_directory,
479     uint32_t launch_flags,   // See LaunchFlags
480     bool stop_at_entry,
481     lldb::SBError& error
482 )
483 {
484     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
485 
486     SBProcess sb_process;
487     ProcessSP process_sp;
488     TargetSP target_sp(GetSP());
489 
490     if (log)
491         log->Printf ("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, stop_at_entry=%i, &error (%p))...",
492                      static_cast<void*>(target_sp.get()),
493                      static_cast<void*>(argv), static_cast<void*>(envp),
494                      stdin_path ? stdin_path : "NULL",
495                      stdout_path ? stdout_path : "NULL",
496                      stderr_path ? stderr_path : "NULL",
497                      working_directory ? working_directory : "NULL",
498                      launch_flags, stop_at_entry,
499                      static_cast<void*>(error.get()));
500 
501     if (target_sp)
502     {
503         Mutex::Locker api_locker (target_sp->GetAPIMutex());
504 
505         if (stop_at_entry)
506             launch_flags |= eLaunchFlagStopAtEntry;
507 
508         if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
509             launch_flags |= eLaunchFlagDisableASLR;
510 
511         StateType state = eStateInvalid;
512         process_sp = target_sp->GetProcessSP();
513         if (process_sp)
514         {
515             state = process_sp->GetState();
516 
517             if (process_sp->IsAlive() && state != eStateConnected)
518             {
519                 if (state == eStateAttaching)
520                     error.SetErrorString ("process attach is in progress");
521                 else
522                     error.SetErrorString ("a process is already being debugged");
523                 return sb_process;
524             }
525         }
526 
527         if (state == eStateConnected)
528         {
529             // If we are already connected, then we have already specified the
530             // listener, so if a valid listener is supplied, we need to error out
531             // to let the client know.
532             if (listener.IsValid())
533             {
534                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
535                 return sb_process;
536             }
537         }
538 
539         if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
540             launch_flags |= eLaunchFlagDisableSTDIO;
541 
542         ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
543 
544         Module *exe_module = target_sp->GetExecutableModulePointer();
545         if (exe_module)
546             launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
547         if (argv)
548             launch_info.GetArguments().AppendArguments (argv);
549         if (envp)
550             launch_info.GetEnvironmentEntries ().SetArguments (envp);
551 
552         if (listener.IsValid())
553             launch_info.SetListener(listener.GetSP());
554 
555         error.SetError (target_sp->Launch(launch_info, NULL));
556 
557         sb_process.SetSP(target_sp->GetProcessSP());
558     }
559     else
560     {
561         error.SetErrorString ("SBTarget is invalid");
562     }
563 
564     log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
565     if (log)
566         log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
567                      static_cast<void*>(target_sp.get()),
568                      static_cast<void*>(sb_process.GetSP().get()));
569 
570     return sb_process;
571 }
572 
573 SBProcess
574 SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
575 {
576     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
577 
578     SBProcess sb_process;
579     TargetSP target_sp(GetSP());
580 
581     if (log)
582         log->Printf ("SBTarget(%p)::Launch (launch_info, error)...",
583                      static_cast<void*>(target_sp.get()));
584 
585     if (target_sp)
586     {
587         Mutex::Locker api_locker (target_sp->GetAPIMutex());
588         StateType state = eStateInvalid;
589         {
590             ProcessSP process_sp = target_sp->GetProcessSP();
591             if (process_sp)
592             {
593                 state = process_sp->GetState();
594 
595                 if (process_sp->IsAlive() && state != eStateConnected)
596                 {
597                     if (state == eStateAttaching)
598                         error.SetErrorString ("process attach is in progress");
599                     else
600                         error.SetErrorString ("a process is already being debugged");
601                     return sb_process;
602                 }
603             }
604         }
605 
606         lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
607 
608         if (!launch_info.GetExecutableFile())
609         {
610             Module *exe_module = target_sp->GetExecutableModulePointer();
611             if (exe_module)
612                 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
613         }
614 
615         const ArchSpec &arch_spec = target_sp->GetArchitecture();
616         if (arch_spec.IsValid())
617             launch_info.GetArchitecture () = arch_spec;
618 
619         error.SetError (target_sp->Launch (launch_info, NULL));
620         sb_process.SetSP(target_sp->GetProcessSP());
621     }
622     else
623     {
624         error.SetErrorString ("SBTarget is invalid");
625     }
626 
627     log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
628     if (log)
629         log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
630                      static_cast<void*>(target_sp.get()),
631                      static_cast<void*>(sb_process.GetSP().get()));
632 
633     return sb_process;
634 }
635 
636 lldb::SBProcess
637 SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
638 {
639     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
640 
641     SBProcess sb_process;
642     TargetSP target_sp(GetSP());
643 
644     if (log)
645         log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...",
646                      static_cast<void*>(target_sp.get()));
647 
648     if (target_sp)
649     {
650         ProcessAttachInfo &attach_info = sb_attach_info.ref();
651         if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
652         {
653             PlatformSP platform_sp = target_sp->GetPlatform();
654             // See if we can pre-verify if a process exists or not
655             if (platform_sp && platform_sp->IsConnected())
656             {
657                 lldb::pid_t attach_pid = attach_info.GetProcessID();
658                 ProcessInstanceInfo instance_info;
659                 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
660                 {
661                     attach_info.SetUserID(instance_info.GetEffectiveUserID());
662                 }
663                 else
664                 {
665                     error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
666                     if (log)
667                     {
668                         log->Printf ("SBTarget(%p)::Attach (...) => error %s",
669                                      static_cast<void*>(target_sp.get()), error.GetCString());
670                     }
671                     return sb_process;
672                 }
673             }
674         }
675         error.SetError(AttachToProcess(attach_info, *target_sp));
676         if (error.Success())
677             sb_process.SetSP(target_sp->GetProcessSP());
678     }
679     else
680     {
681         error.SetErrorString ("SBTarget is invalid");
682     }
683 
684     if (log)
685         log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
686                      static_cast<void*>(target_sp.get()),
687                      static_cast<void*>(sb_process.GetSP().get()));
688 
689     return sb_process;
690 }
691 
692 
693 #if defined(__APPLE__)
694 
695 lldb::SBProcess
696 SBTarget::AttachToProcessWithID (SBListener &listener,
697                                 ::pid_t pid,
698                                  lldb::SBError& error)
699 {
700     return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
701 }
702 
703 #endif // #if defined(__APPLE__)
704 
705 lldb::SBProcess
706 SBTarget::AttachToProcessWithID
707 (
708     SBListener &listener,
709     lldb::pid_t pid,// The process ID to attach to
710     SBError& error  // An error explaining what went wrong if attach fails
711 )
712 {
713     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
714 
715     SBProcess sb_process;
716     TargetSP target_sp(GetSP());
717 
718     if (log)
719         log->Printf ("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
720                      static_cast<void*>(target_sp.get()),
721                      __FUNCTION__,
722                      pid);
723 
724     if (target_sp)
725     {
726         ProcessAttachInfo attach_info;
727         attach_info.SetProcessID (pid);
728         if (listener.IsValid())
729             attach_info.SetListener(listener.GetSP());
730 
731         ProcessInstanceInfo instance_info;
732         if (target_sp->GetPlatform ()->GetProcessInfo (pid, instance_info))
733             attach_info.SetUserID (instance_info.GetEffectiveUserID ());
734 
735         error.SetError (AttachToProcess (attach_info, *target_sp));
736         if (error.Success ())
737             sb_process.SetSP (target_sp->GetProcessSP ());
738     }
739     else
740         error.SetErrorString ("SBTarget is invalid");
741 
742     if (log)
743         log->Printf ("SBTarget(%p)::%s (...) => SBProcess(%p)",
744                      static_cast<void*>(target_sp.get ()),
745                      __FUNCTION__,
746                      static_cast<void*>(sb_process.GetSP().get ()));
747     return sb_process;
748 }
749 
750 lldb::SBProcess
751 SBTarget::AttachToProcessWithName
752 (
753     SBListener &listener,
754     const char *name,   // basename of process to attach to
755     bool wait_for,      // if true wait for a new instance of "name" to be launched
756     SBError& error      // An error explaining what went wrong if attach fails
757 )
758 {
759     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
760 
761     SBProcess sb_process;
762     TargetSP target_sp(GetSP());
763 
764     if (log)
765         log->Printf ("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
766                      static_cast<void*>(target_sp.get()),
767                      __FUNCTION__,
768                      name,
769                      wait_for ? "true" : "false");
770 
771     if (name && target_sp)
772     {
773         ProcessAttachInfo attach_info;
774         attach_info.GetExecutableFile().SetFile(name, false);
775         attach_info.SetWaitForLaunch(wait_for);
776         if (listener.IsValid())
777             attach_info.SetListener(listener.GetSP());
778 
779         error.SetError (AttachToProcess (attach_info, *target_sp));
780         if (error.Success ())
781             sb_process.SetSP (target_sp->GetProcessSP ());
782     }
783     else
784         error.SetErrorString ("SBTarget is invalid");
785 
786     if (log)
787         log->Printf ("SBTarget(%p)::%s (...) => SBProcess(%p)",
788                      static_cast<void*>(target_sp.get()),
789                      __FUNCTION__,
790                      static_cast<void*>(sb_process.GetSP().get()));
791     return sb_process;
792 }
793 
794 lldb::SBProcess
795 SBTarget::ConnectRemote
796 (
797     SBListener &listener,
798     const char *url,
799     const char *plugin_name,
800     SBError& error
801 )
802 {
803     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
804 
805     SBProcess sb_process;
806     ProcessSP process_sp;
807     TargetSP target_sp(GetSP());
808 
809     if (log)
810         log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...",
811                      static_cast<void*>(target_sp.get()), url, plugin_name);
812 
813     if (target_sp)
814     {
815         Mutex::Locker api_locker (target_sp->GetAPIMutex());
816         if (listener.IsValid())
817             process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
818         else
819             process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
820 
821         if (process_sp)
822         {
823             sb_process.SetSP (process_sp);
824             error.SetError (process_sp->ConnectRemote (NULL, url));
825         }
826         else
827         {
828             error.SetErrorString ("unable to create lldb_private::Process");
829         }
830     }
831     else
832     {
833         error.SetErrorString ("SBTarget is invalid");
834     }
835 
836     if (log)
837         log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
838                      static_cast<void*>(target_sp.get()),
839                      static_cast<void*>(process_sp.get()));
840     return sb_process;
841 }
842 
843 SBFileSpec
844 SBTarget::GetExecutable ()
845 {
846 
847     SBFileSpec exe_file_spec;
848     TargetSP target_sp(GetSP());
849     if (target_sp)
850     {
851         Module *exe_module = target_sp->GetExecutableModulePointer();
852         if (exe_module)
853             exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
854     }
855 
856     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
857     if (log)
858     {
859         log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
860                      static_cast<void*>(target_sp.get()),
861                      static_cast<const void*>(exe_file_spec.get()));
862     }
863 
864     return exe_file_spec;
865 }
866 
867 bool
868 SBTarget::operator == (const SBTarget &rhs) const
869 {
870     return m_opaque_sp.get() == rhs.m_opaque_sp.get();
871 }
872 
873 bool
874 SBTarget::operator != (const SBTarget &rhs) const
875 {
876     return m_opaque_sp.get() != rhs.m_opaque_sp.get();
877 }
878 
879 lldb::TargetSP
880 SBTarget::GetSP () const
881 {
882     return m_opaque_sp;
883 }
884 
885 void
886 SBTarget::SetSP (const lldb::TargetSP& target_sp)
887 {
888     m_opaque_sp = target_sp;
889 }
890 
891 lldb::SBAddress
892 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
893 {
894     lldb::SBAddress sb_addr;
895     Address &addr = sb_addr.ref();
896     TargetSP target_sp(GetSP());
897     if (target_sp)
898     {
899         Mutex::Locker api_locker (target_sp->GetAPIMutex());
900         if (target_sp->ResolveLoadAddress (vm_addr, addr))
901             return sb_addr;
902     }
903 
904     // We have a load address that isn't in a section, just return an address
905     // with the offset filled in (the address) and the section set to NULL
906     addr.SetRawAddress(vm_addr);
907     return sb_addr;
908 }
909 
910 lldb::SBAddress
911 SBTarget::ResolveFileAddress (lldb::addr_t file_addr)
912 {
913     lldb::SBAddress sb_addr;
914     Address &addr = sb_addr.ref();
915     TargetSP target_sp(GetSP());
916     if (target_sp)
917     {
918         Mutex::Locker api_locker (target_sp->GetAPIMutex());
919         if (target_sp->ResolveFileAddress (file_addr, addr))
920             return sb_addr;
921     }
922 
923     addr.SetRawAddress(file_addr);
924     return sb_addr;
925 }
926 
927 lldb::SBAddress
928 SBTarget::ResolvePastLoadAddress (uint32_t stop_id, lldb::addr_t vm_addr)
929 {
930     lldb::SBAddress sb_addr;
931     Address &addr = sb_addr.ref();
932     TargetSP target_sp(GetSP());
933     if (target_sp)
934     {
935         Mutex::Locker api_locker (target_sp->GetAPIMutex());
936         if (target_sp->ResolveLoadAddress (vm_addr, addr))
937             return sb_addr;
938     }
939 
940     // We have a load address that isn't in a section, just return an address
941     // with the offset filled in (the address) and the section set to NULL
942     addr.SetRawAddress(vm_addr);
943     return sb_addr;
944 }
945 
946 SBSymbolContext
947 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
948                                           uint32_t resolve_scope)
949 {
950     SBSymbolContext sc;
951     if (addr.IsValid())
952     {
953         TargetSP target_sp(GetSP());
954         if (target_sp)
955             target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
956     }
957     return sc;
958 }
959 
960 size_t
961 SBTarget::ReadMemory (const SBAddress addr,
962                       void *buf,
963                       size_t size,
964                       lldb::SBError &error)
965 {
966     SBError sb_error;
967     size_t bytes_read = 0;
968     TargetSP target_sp(GetSP());
969     if (target_sp)
970     {
971         Mutex::Locker api_locker (target_sp->GetAPIMutex());
972         bytes_read = target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
973     }
974     else
975     {
976         sb_error.SetErrorString("invalid target");
977     }
978 
979     return bytes_read;
980 }
981 
982 SBBreakpoint
983 SBTarget::BreakpointCreateByLocation (const char *file,
984                                       uint32_t line)
985 {
986     return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
987 }
988 
989 SBBreakpoint
990 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
991                                       uint32_t line)
992 {
993     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
994 
995     SBBreakpoint sb_bp;
996     TargetSP target_sp(GetSP());
997     if (target_sp && line != 0)
998     {
999         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1000 
1001         const LazyBool check_inlines = eLazyBoolCalculate;
1002         const LazyBool skip_prologue = eLazyBoolCalculate;
1003         const bool internal = false;
1004         const bool hardware = false;
1005         *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
1006     }
1007 
1008     if (log)
1009     {
1010         SBStream sstr;
1011         sb_bp.GetDescription (sstr);
1012         char path[PATH_MAX];
1013         sb_file_spec->GetPath (path, sizeof(path));
1014         log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
1015                      static_cast<void*>(target_sp.get()), path, line,
1016                      static_cast<void*>(sb_bp.get()), sstr.GetData());
1017     }
1018 
1019     return sb_bp;
1020 }
1021 
1022 SBBreakpoint
1023 SBTarget::BreakpointCreateByName (const char *symbol_name,
1024                                   const char *module_name)
1025 {
1026     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1027 
1028     SBBreakpoint sb_bp;
1029     TargetSP target_sp(GetSP());
1030     if (target_sp.get())
1031     {
1032         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1033 
1034         const bool internal = false;
1035         const bool hardware = false;
1036         const LazyBool skip_prologue = eLazyBoolCalculate;
1037         if (module_name && module_name[0])
1038         {
1039             FileSpecList module_spec_list;
1040             module_spec_list.Append (FileSpec (module_name, false));
1041             *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
1042         }
1043         else
1044         {
1045             *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
1046         }
1047     }
1048 
1049     if (log)
1050         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
1051                      static_cast<void*>(target_sp.get()), symbol_name,
1052                      module_name, static_cast<void*>(sb_bp.get()));
1053 
1054     return sb_bp;
1055 }
1056 
1057 lldb::SBBreakpoint
1058 SBTarget::BreakpointCreateByName (const char *symbol_name,
1059                                   const SBFileSpecList &module_list,
1060                                   const SBFileSpecList &comp_unit_list)
1061 {
1062     uint32_t name_type_mask = eFunctionNameTypeAuto;
1063     return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1064 }
1065 
1066 lldb::SBBreakpoint
1067 SBTarget::BreakpointCreateByName (const char *symbol_name,
1068                                   uint32_t name_type_mask,
1069                                   const SBFileSpecList &module_list,
1070                                   const SBFileSpecList &comp_unit_list)
1071 {
1072     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1073 
1074     SBBreakpoint sb_bp;
1075     TargetSP target_sp(GetSP());
1076     if (target_sp && symbol_name && symbol_name[0])
1077     {
1078         const bool internal = false;
1079         const bool hardware = false;
1080         const LazyBool skip_prologue = eLazyBoolCalculate;
1081         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1082         *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1083                                               comp_unit_list.get(),
1084                                               symbol_name,
1085                                               name_type_mask,
1086                                               skip_prologue,
1087                                               internal,
1088                                               hardware);
1089     }
1090 
1091     if (log)
1092         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
1093                      static_cast<void*>(target_sp.get()), symbol_name,
1094                      name_type_mask, static_cast<void*>(sb_bp.get()));
1095 
1096     return sb_bp;
1097 }
1098 
1099 lldb::SBBreakpoint
1100 SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1101                                    uint32_t num_names,
1102                                    uint32_t name_type_mask,
1103                                    const SBFileSpecList &module_list,
1104                                    const SBFileSpecList &comp_unit_list)
1105 {
1106     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1107 
1108     SBBreakpoint sb_bp;
1109     TargetSP target_sp(GetSP());
1110     if (target_sp && num_names > 0)
1111     {
1112         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1113         const bool internal = false;
1114         const bool hardware = false;
1115         const LazyBool skip_prologue = eLazyBoolCalculate;
1116         *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1117                                                 comp_unit_list.get(),
1118                                                 symbol_names,
1119                                                 num_names,
1120                                                 name_type_mask,
1121                                                 skip_prologue,
1122                                                 internal,
1123                                                 hardware);
1124     }
1125 
1126     if (log)
1127     {
1128         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={",
1129                      static_cast<void*>(target_sp.get()));
1130         for (uint32_t i = 0 ; i < num_names; i++)
1131         {
1132             char sep;
1133             if (i < num_names - 1)
1134                 sep = ',';
1135             else
1136                 sep = '}';
1137             if (symbol_names[i] != NULL)
1138                 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1139             else
1140                 log->Printf ("\"<NULL>\"%c ", sep);
1141         }
1142         log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
1143                      static_cast<void*>(sb_bp.get()));
1144     }
1145 
1146     return sb_bp;
1147 }
1148 
1149 SBBreakpoint
1150 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1151                                    const char *module_name)
1152 {
1153     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1154 
1155     SBBreakpoint sb_bp;
1156     TargetSP target_sp(GetSP());
1157     if (target_sp && symbol_name_regex && symbol_name_regex[0])
1158     {
1159         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1160         RegularExpression regexp(symbol_name_regex);
1161         const bool internal = false;
1162         const bool hardware = false;
1163         const LazyBool skip_prologue = eLazyBoolCalculate;
1164 
1165         if (module_name && module_name[0])
1166         {
1167             FileSpecList module_spec_list;
1168             module_spec_list.Append (FileSpec (module_name, false));
1169 
1170             *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
1171         }
1172         else
1173         {
1174             *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
1175         }
1176     }
1177 
1178     if (log)
1179         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1180                      static_cast<void*>(target_sp.get()), symbol_name_regex,
1181                      module_name, static_cast<void*>(sb_bp.get()));
1182 
1183     return sb_bp;
1184 }
1185 
1186 lldb::SBBreakpoint
1187 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1188                                    const SBFileSpecList &module_list,
1189                                    const SBFileSpecList &comp_unit_list)
1190 {
1191     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1192 
1193     SBBreakpoint sb_bp;
1194     TargetSP target_sp(GetSP());
1195     if (target_sp && symbol_name_regex && symbol_name_regex[0])
1196     {
1197         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1198         RegularExpression regexp(symbol_name_regex);
1199         const bool internal = false;
1200         const bool hardware = false;
1201         const LazyBool skip_prologue = eLazyBoolCalculate;
1202 
1203         *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
1204     }
1205 
1206     if (log)
1207         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1208                      static_cast<void*>(target_sp.get()), symbol_name_regex,
1209                      static_cast<void*>(sb_bp.get()));
1210 
1211     return sb_bp;
1212 }
1213 
1214 SBBreakpoint
1215 SBTarget::BreakpointCreateByAddress (addr_t address)
1216 {
1217     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1218 
1219     SBBreakpoint sb_bp;
1220     TargetSP target_sp(GetSP());
1221     if (target_sp)
1222     {
1223         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1224         const bool hardware = false;
1225         *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
1226     }
1227 
1228     if (log)
1229         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)",
1230                      static_cast<void*>(target_sp.get()),
1231                      static_cast<uint64_t>(address),
1232                      static_cast<void*>(sb_bp.get()));
1233 
1234     return sb_bp;
1235 }
1236 
1237 lldb::SBBreakpoint
1238 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1239                                          const lldb::SBFileSpec &source_file,
1240                                          const char *module_name)
1241 {
1242     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1243 
1244     SBBreakpoint sb_bp;
1245     TargetSP target_sp(GetSP());
1246     if (target_sp && source_regex && source_regex[0])
1247     {
1248         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1249         RegularExpression regexp(source_regex);
1250         FileSpecList source_file_spec_list;
1251         const bool hardware = false;
1252         source_file_spec_list.Append (source_file.ref());
1253 
1254         if (module_name && module_name[0])
1255         {
1256             FileSpecList module_spec_list;
1257             module_spec_list.Append (FileSpec (module_name, false));
1258 
1259             *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
1260         }
1261         else
1262         {
1263             *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
1264         }
1265     }
1266 
1267     if (log)
1268     {
1269         char path[PATH_MAX];
1270         source_file->GetPath (path, sizeof(path));
1271         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1272                      static_cast<void*>(target_sp.get()), source_regex, path,
1273                      module_name, static_cast<void*>(sb_bp.get()));
1274     }
1275 
1276     return sb_bp;
1277 }
1278 
1279 lldb::SBBreakpoint
1280 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1281                                          const SBFileSpecList &module_list,
1282                                          const lldb::SBFileSpecList &source_file_list)
1283 {
1284     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1285 
1286     SBBreakpoint sb_bp;
1287     TargetSP target_sp(GetSP());
1288     if (target_sp && source_regex && source_regex[0])
1289     {
1290         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1291         const bool hardware = false;
1292         RegularExpression regexp(source_regex);
1293         *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
1294     }
1295 
1296     if (log)
1297         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1298                      static_cast<void*>(target_sp.get()), source_regex,
1299                      static_cast<void*>(sb_bp.get()));
1300 
1301     return sb_bp;
1302 }
1303 
1304 lldb::SBBreakpoint
1305 SBTarget::BreakpointCreateForException  (lldb::LanguageType language,
1306                                          bool catch_bp,
1307                                          bool throw_bp)
1308 {
1309     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1310 
1311     SBBreakpoint sb_bp;
1312     TargetSP target_sp(GetSP());
1313     if (target_sp)
1314     {
1315         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1316         const bool hardware = false;
1317         *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
1318     }
1319 
1320     if (log)
1321         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1322                      static_cast<void*>(target_sp.get()),
1323                      LanguageRuntime::GetNameForLanguageType(language),
1324                      catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1325                      static_cast<void*>(sb_bp.get()));
1326 
1327     return sb_bp;
1328 }
1329 
1330 uint32_t
1331 SBTarget::GetNumBreakpoints () const
1332 {
1333     TargetSP target_sp(GetSP());
1334     if (target_sp)
1335     {
1336         // The breakpoint list is thread safe, no need to lock
1337         return target_sp->GetBreakpointList().GetSize();
1338     }
1339     return 0;
1340 }
1341 
1342 SBBreakpoint
1343 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1344 {
1345     SBBreakpoint sb_breakpoint;
1346     TargetSP target_sp(GetSP());
1347     if (target_sp)
1348     {
1349         // The breakpoint list is thread safe, no need to lock
1350         *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1351     }
1352     return sb_breakpoint;
1353 }
1354 
1355 bool
1356 SBTarget::BreakpointDelete (break_id_t bp_id)
1357 {
1358     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1359 
1360     bool result = false;
1361     TargetSP target_sp(GetSP());
1362     if (target_sp)
1363     {
1364         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1365         result = target_sp->RemoveBreakpointByID (bp_id);
1366     }
1367 
1368     if (log)
1369         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1370                      static_cast<void*>(target_sp.get()),
1371                      static_cast<uint32_t>(bp_id), result);
1372 
1373     return result;
1374 }
1375 
1376 SBBreakpoint
1377 SBTarget::FindBreakpointByID (break_id_t bp_id)
1378 {
1379     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1380 
1381     SBBreakpoint sb_breakpoint;
1382     TargetSP target_sp(GetSP());
1383     if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
1384     {
1385         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1386         *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
1387     }
1388 
1389     if (log)
1390         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1391                      static_cast<void*>(target_sp.get()),
1392                      static_cast<uint32_t>(bp_id),
1393                      static_cast<void*>(sb_breakpoint.get()));
1394 
1395     return sb_breakpoint;
1396 }
1397 
1398 bool
1399 SBTarget::EnableAllBreakpoints ()
1400 {
1401     TargetSP target_sp(GetSP());
1402     if (target_sp)
1403     {
1404         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1405         target_sp->EnableAllBreakpoints ();
1406         return true;
1407     }
1408     return false;
1409 }
1410 
1411 bool
1412 SBTarget::DisableAllBreakpoints ()
1413 {
1414     TargetSP target_sp(GetSP());
1415     if (target_sp)
1416     {
1417         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1418         target_sp->DisableAllBreakpoints ();
1419         return true;
1420     }
1421     return false;
1422 }
1423 
1424 bool
1425 SBTarget::DeleteAllBreakpoints ()
1426 {
1427     TargetSP target_sp(GetSP());
1428     if (target_sp)
1429     {
1430         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1431         target_sp->RemoveAllBreakpoints ();
1432         return true;
1433     }
1434     return false;
1435 }
1436 
1437 uint32_t
1438 SBTarget::GetNumWatchpoints () const
1439 {
1440     TargetSP target_sp(GetSP());
1441     if (target_sp)
1442     {
1443         // The watchpoint list is thread safe, no need to lock
1444         return target_sp->GetWatchpointList().GetSize();
1445     }
1446     return 0;
1447 }
1448 
1449 SBWatchpoint
1450 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
1451 {
1452     SBWatchpoint sb_watchpoint;
1453     TargetSP target_sp(GetSP());
1454     if (target_sp)
1455     {
1456         // The watchpoint list is thread safe, no need to lock
1457         sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
1458     }
1459     return sb_watchpoint;
1460 }
1461 
1462 bool
1463 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
1464 {
1465     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1466 
1467     bool result = false;
1468     TargetSP target_sp(GetSP());
1469     if (target_sp)
1470     {
1471         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1472         Mutex::Locker locker;
1473         target_sp->GetWatchpointList().GetListMutex(locker);
1474         result = target_sp->RemoveWatchpointByID (wp_id);
1475     }
1476 
1477     if (log)
1478         log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1479                      static_cast<void*>(target_sp.get()),
1480                      static_cast<uint32_t>(wp_id), result);
1481 
1482     return result;
1483 }
1484 
1485 SBWatchpoint
1486 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
1487 {
1488     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1489 
1490     SBWatchpoint sb_watchpoint;
1491     lldb::WatchpointSP watchpoint_sp;
1492     TargetSP target_sp(GetSP());
1493     if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
1494     {
1495         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1496         Mutex::Locker locker;
1497         target_sp->GetWatchpointList().GetListMutex(locker);
1498         watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1499         sb_watchpoint.SetSP (watchpoint_sp);
1500     }
1501 
1502     if (log)
1503         log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1504                      static_cast<void*>(target_sp.get()),
1505                      static_cast<uint32_t>(wp_id),
1506                      static_cast<void*>(watchpoint_sp.get()));
1507 
1508     return sb_watchpoint;
1509 }
1510 
1511 lldb::SBWatchpoint
1512 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
1513 {
1514     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1515 
1516     SBWatchpoint sb_watchpoint;
1517     lldb::WatchpointSP watchpoint_sp;
1518     TargetSP target_sp(GetSP());
1519     if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
1520     {
1521         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1522         uint32_t watch_type = 0;
1523         if (read)
1524             watch_type |= LLDB_WATCH_TYPE_READ;
1525         if (write)
1526             watch_type |= LLDB_WATCH_TYPE_WRITE;
1527         if (watch_type == 0)
1528         {
1529             error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1530             return sb_watchpoint;
1531         }
1532 
1533         // Target::CreateWatchpoint() is thread safe.
1534         Error cw_error;
1535         // This API doesn't take in a type, so we can't figure out what it is.
1536         ClangASTType *type = NULL;
1537         watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1538         error.SetError(cw_error);
1539         sb_watchpoint.SetSP (watchpoint_sp);
1540     }
1541 
1542     if (log)
1543         log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
1544                      static_cast<void*>(target_sp.get()), addr,
1545                      static_cast<uint32_t>(size),
1546                      static_cast<void*>(watchpoint_sp.get()));
1547 
1548     return sb_watchpoint;
1549 }
1550 
1551 bool
1552 SBTarget::EnableAllWatchpoints ()
1553 {
1554     TargetSP target_sp(GetSP());
1555     if (target_sp)
1556     {
1557         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1558         Mutex::Locker locker;
1559         target_sp->GetWatchpointList().GetListMutex(locker);
1560         target_sp->EnableAllWatchpoints ();
1561         return true;
1562     }
1563     return false;
1564 }
1565 
1566 bool
1567 SBTarget::DisableAllWatchpoints ()
1568 {
1569     TargetSP target_sp(GetSP());
1570     if (target_sp)
1571     {
1572         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1573         Mutex::Locker locker;
1574         target_sp->GetWatchpointList().GetListMutex(locker);
1575         target_sp->DisableAllWatchpoints ();
1576         return true;
1577     }
1578     return false;
1579 }
1580 
1581 SBValue
1582 SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1583 {
1584     SBValue sb_value;
1585     lldb::ValueObjectSP new_value_sp;
1586     if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1587     {
1588         lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1589         ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1590         ClangASTType ast_type(type.GetSP()->GetClangASTType(true));
1591         new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr, exe_ctx, ast_type);
1592     }
1593     sb_value.SetSP(new_value_sp);
1594     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1595     if (log)
1596     {
1597         if (new_value_sp)
1598             log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1599                          static_cast<void*>(m_opaque_sp.get()),
1600                          new_value_sp->GetName().AsCString());
1601         else
1602             log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL",
1603                          static_cast<void*>(m_opaque_sp.get()));
1604     }
1605     return sb_value;
1606 }
1607 
1608 lldb::SBValue
1609 SBTarget::CreateValueFromData (const char *name, lldb::SBData data, lldb::SBType type)
1610 {
1611     SBValue sb_value;
1612     lldb::ValueObjectSP new_value_sp;
1613     if (IsValid() && name && *name && data.IsValid() && type.IsValid())
1614     {
1615         DataExtractorSP extractor(*data);
1616         ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1617         ClangASTType ast_type(type.GetSP()->GetClangASTType(true));
1618         new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor, exe_ctx, ast_type);
1619     }
1620     sb_value.SetSP(new_value_sp);
1621     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1622     if (log)
1623     {
1624         if (new_value_sp)
1625             log->Printf ("SBTarget(%p)::CreateValueFromData => \"%s\"",
1626                          static_cast<void*>(m_opaque_sp.get()),
1627                          new_value_sp->GetName().AsCString());
1628         else
1629             log->Printf ("SBTarget(%p)::CreateValueFromData => NULL",
1630                          static_cast<void*>(m_opaque_sp.get()));
1631     }
1632     return sb_value;
1633 }
1634 
1635 lldb::SBValue
1636 SBTarget::CreateValueFromExpression (const char *name, const char* expr)
1637 {
1638     SBValue sb_value;
1639     lldb::ValueObjectSP new_value_sp;
1640     if (IsValid() && name && *name && expr && *expr)
1641     {
1642         ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1643         new_value_sp = ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1644     }
1645     sb_value.SetSP(new_value_sp);
1646     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1647     if (log)
1648     {
1649         if (new_value_sp)
1650             log->Printf ("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1651                          static_cast<void*>(m_opaque_sp.get()),
1652                          new_value_sp->GetName().AsCString());
1653         else
1654             log->Printf ("SBTarget(%p)::CreateValueFromExpression => NULL",
1655                          static_cast<void*>(m_opaque_sp.get()));
1656     }
1657     return sb_value;
1658 }
1659 
1660 bool
1661 SBTarget::DeleteAllWatchpoints ()
1662 {
1663     TargetSP target_sp(GetSP());
1664     if (target_sp)
1665     {
1666         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1667         Mutex::Locker locker;
1668         target_sp->GetWatchpointList().GetListMutex(locker);
1669         target_sp->RemoveAllWatchpoints ();
1670         return true;
1671     }
1672     return false;
1673 }
1674 
1675 
1676 lldb::SBModule
1677 SBTarget::AddModule (const char *path,
1678                      const char *triple,
1679                      const char *uuid_cstr)
1680 {
1681     return AddModule (path, triple, uuid_cstr, NULL);
1682 }
1683 
1684 lldb::SBModule
1685 SBTarget::AddModule (const char *path,
1686                      const char *triple,
1687                      const char *uuid_cstr,
1688                      const char *symfile)
1689 {
1690     lldb::SBModule sb_module;
1691     TargetSP target_sp(GetSP());
1692     if (target_sp)
1693     {
1694         ModuleSpec module_spec;
1695         if (path)
1696             module_spec.GetFileSpec().SetFile(path, false);
1697 
1698         if (uuid_cstr)
1699             module_spec.GetUUID().SetFromCString(uuid_cstr);
1700 
1701         if (triple)
1702             module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1703         else
1704             module_spec.GetArchitecture() = target_sp->GetArchitecture();
1705 
1706         if (symfile)
1707             module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
1708 
1709         sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1710     }
1711     return sb_module;
1712 }
1713 
1714 lldb::SBModule
1715 SBTarget::AddModule (const SBModuleSpec &module_spec)
1716 {
1717     lldb::SBModule sb_module;
1718     TargetSP target_sp(GetSP());
1719     if (target_sp)
1720         sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1721     return sb_module;
1722 }
1723 
1724 bool
1725 SBTarget::AddModule (lldb::SBModule &module)
1726 {
1727     TargetSP target_sp(GetSP());
1728     if (target_sp)
1729     {
1730         target_sp->GetImages().AppendIfNeeded (module.GetSP());
1731         return true;
1732     }
1733     return false;
1734 }
1735 
1736 uint32_t
1737 SBTarget::GetNumModules () const
1738 {
1739     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1740 
1741     uint32_t num = 0;
1742     TargetSP target_sp(GetSP());
1743     if (target_sp)
1744     {
1745         // The module list is thread safe, no need to lock
1746         num = target_sp->GetImages().GetSize();
1747     }
1748 
1749     if (log)
1750         log->Printf ("SBTarget(%p)::GetNumModules () => %d",
1751                      static_cast<void*>(target_sp.get()), num);
1752 
1753     return num;
1754 }
1755 
1756 void
1757 SBTarget::Clear ()
1758 {
1759     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1760 
1761     if (log)
1762         log->Printf ("SBTarget(%p)::Clear ()",
1763                      static_cast<void*>(m_opaque_sp.get()));
1764 
1765     m_opaque_sp.reset();
1766 }
1767 
1768 
1769 SBModule
1770 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1771 {
1772     SBModule sb_module;
1773     TargetSP target_sp(GetSP());
1774     if (target_sp && sb_file_spec.IsValid())
1775     {
1776         ModuleSpec module_spec(*sb_file_spec);
1777         // The module list is thread safe, no need to lock
1778         sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
1779     }
1780     return sb_module;
1781 }
1782 
1783 lldb::ByteOrder
1784 SBTarget::GetByteOrder ()
1785 {
1786     TargetSP target_sp(GetSP());
1787     if (target_sp)
1788         return target_sp->GetArchitecture().GetByteOrder();
1789     return eByteOrderInvalid;
1790 }
1791 
1792 const char *
1793 SBTarget::GetTriple ()
1794 {
1795     TargetSP target_sp(GetSP());
1796     if (target_sp)
1797     {
1798         std::string triple (target_sp->GetArchitecture().GetTriple().str());
1799         // Unique the string so we don't run into ownership issues since
1800         // the const strings put the string into the string pool once and
1801         // the strings never comes out
1802         ConstString const_triple (triple.c_str());
1803         return const_triple.GetCString();
1804     }
1805     return NULL;
1806 }
1807 
1808 uint32_t
1809 SBTarget::GetDataByteSize ()
1810 {
1811     TargetSP target_sp(GetSP());
1812     if (target_sp)
1813     {
1814         return target_sp->GetArchitecture().GetDataByteSize() ;
1815     }
1816     return 0;
1817 }
1818 
1819 uint32_t
1820 SBTarget::GetCodeByteSize ()
1821 {
1822     TargetSP target_sp(GetSP());
1823     if (target_sp)
1824     {
1825         return target_sp->GetArchitecture().GetCodeByteSize() ;
1826     }
1827     return 0;
1828 }
1829 
1830 uint32_t
1831 SBTarget::GetAddressByteSize()
1832 {
1833     TargetSP target_sp(GetSP());
1834     if (target_sp)
1835         return target_sp->GetArchitecture().GetAddressByteSize();
1836     return sizeof(void*);
1837 }
1838 
1839 
1840 SBModule
1841 SBTarget::GetModuleAtIndex (uint32_t idx)
1842 {
1843     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1844 
1845     SBModule sb_module;
1846     ModuleSP module_sp;
1847     TargetSP target_sp(GetSP());
1848     if (target_sp)
1849     {
1850         // The module list is thread safe, no need to lock
1851         module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1852         sb_module.SetSP (module_sp);
1853     }
1854 
1855     if (log)
1856         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1857                      static_cast<void*>(target_sp.get()), idx,
1858                      static_cast<void*>(module_sp.get()));
1859 
1860     return sb_module;
1861 }
1862 
1863 bool
1864 SBTarget::RemoveModule (lldb::SBModule module)
1865 {
1866     TargetSP target_sp(GetSP());
1867     if (target_sp)
1868         return target_sp->GetImages().Remove(module.GetSP());
1869     return false;
1870 }
1871 
1872 
1873 SBBroadcaster
1874 SBTarget::GetBroadcaster () const
1875 {
1876     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1877 
1878     TargetSP target_sp(GetSP());
1879     SBBroadcaster broadcaster(target_sp.get(), false);
1880 
1881     if (log)
1882         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1883                      static_cast<void*>(target_sp.get()),
1884                      static_cast<void*>(broadcaster.get()));
1885 
1886     return broadcaster;
1887 }
1888 
1889 bool
1890 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
1891 {
1892     Stream &strm = description.ref();
1893 
1894     TargetSP target_sp(GetSP());
1895     if (target_sp)
1896     {
1897         target_sp->Dump (&strm, description_level);
1898     }
1899     else
1900         strm.PutCString ("No value");
1901 
1902     return true;
1903 }
1904 
1905 lldb::SBSymbolContextList
1906 SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
1907 {
1908     lldb::SBSymbolContextList sb_sc_list;
1909     if (name && name[0])
1910     {
1911         TargetSP target_sp(GetSP());
1912         if (target_sp)
1913         {
1914             const bool symbols_ok = true;
1915             const bool inlines_ok = true;
1916             const bool append = true;
1917             target_sp->GetImages().FindFunctions (ConstString(name),
1918                                                   name_type_mask,
1919                                                   symbols_ok,
1920                                                   inlines_ok,
1921                                                   append,
1922                                                   *sb_sc_list);
1923         }
1924     }
1925     return sb_sc_list;
1926 }
1927 
1928 lldb::SBSymbolContextList
1929 SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype)
1930 {
1931     lldb::SBSymbolContextList sb_sc_list;
1932     if (name && name[0])
1933     {
1934         TargetSP target_sp(GetSP());
1935         if (target_sp)
1936         {
1937             std::string regexstr;
1938             switch (matchtype)
1939             {
1940             case eMatchTypeRegex:
1941                 target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list);
1942                 break;
1943             case eMatchTypeStartsWith:
1944                 regexstr = llvm::Regex::escape(name) + ".*";
1945                 target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list);
1946                 break;
1947             default:
1948                 target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list);
1949                 break;
1950             }
1951         }
1952     }
1953     return sb_sc_list;
1954 }
1955 
1956 lldb::SBType
1957 SBTarget::FindFirstType (const char* typename_cstr)
1958 {
1959     TargetSP target_sp(GetSP());
1960     if (typename_cstr && typename_cstr[0] && target_sp)
1961     {
1962         ConstString const_typename(typename_cstr);
1963         SymbolContext sc;
1964         const bool exact_match = false;
1965 
1966         const ModuleList &module_list = target_sp->GetImages();
1967         size_t count = module_list.GetSize();
1968         for (size_t idx = 0; idx < count; idx++)
1969         {
1970             ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
1971             if (module_sp)
1972             {
1973                 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
1974                 if (type_sp)
1975                     return SBType(type_sp);
1976             }
1977         }
1978 
1979         // Didn't find the type in the symbols; try the Objective-C runtime
1980         // if one is installed
1981 
1982         ProcessSP process_sp(target_sp->GetProcessSP());
1983 
1984         if (process_sp)
1985         {
1986             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1987 
1988             if (objc_language_runtime)
1989             {
1990                 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1991 
1992                 if (objc_decl_vendor)
1993                 {
1994                     std::vector <clang::NamedDecl *> decls;
1995 
1996                     if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
1997                     {
1998                         if (ClangASTType type = ClangASTContext::GetTypeForDecl(decls[0]))
1999                         {
2000                             return SBType(type);
2001                         }
2002                     }
2003                 }
2004             }
2005         }
2006 
2007         // No matches, search for basic typename matches
2008         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2009         if (clang_ast)
2010             return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
2011     }
2012     return SBType();
2013 }
2014 
2015 SBType
2016 SBTarget::GetBasicType(lldb::BasicType type)
2017 {
2018     TargetSP target_sp(GetSP());
2019     if (target_sp)
2020     {
2021         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2022         if (clang_ast)
2023             return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
2024     }
2025     return SBType();
2026 }
2027 
2028 
2029 lldb::SBTypeList
2030 SBTarget::FindTypes (const char* typename_cstr)
2031 {
2032     SBTypeList sb_type_list;
2033     TargetSP target_sp(GetSP());
2034     if (typename_cstr && typename_cstr[0] && target_sp)
2035     {
2036         ModuleList& images = target_sp->GetImages();
2037         ConstString const_typename(typename_cstr);
2038         bool exact_match = false;
2039         SymbolContext sc;
2040         TypeList type_list;
2041 
2042         uint32_t num_matches = images.FindTypes (sc,
2043                                                  const_typename,
2044                                                  exact_match,
2045                                                  UINT32_MAX,
2046                                                  type_list);
2047 
2048         if (num_matches > 0)
2049         {
2050             for (size_t idx = 0; idx < num_matches; idx++)
2051             {
2052                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2053                 if (type_sp)
2054                     sb_type_list.Append(SBType(type_sp));
2055             }
2056         }
2057 
2058         // Try the Objective-C runtime if one is installed
2059 
2060         ProcessSP process_sp(target_sp->GetProcessSP());
2061 
2062         if (process_sp)
2063         {
2064             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2065 
2066             if (objc_language_runtime)
2067             {
2068                 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
2069 
2070                 if (objc_decl_vendor)
2071                 {
2072                     std::vector <clang::NamedDecl *> decls;
2073 
2074                     if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
2075                     {
2076                         for (clang::NamedDecl *decl : decls)
2077                         {
2078                             if (ClangASTType type = ClangASTContext::GetTypeForDecl(decl))
2079                             {
2080                                 sb_type_list.Append(SBType(type));
2081                             }
2082                         }
2083                     }
2084                 }
2085             }
2086         }
2087 
2088         if (sb_type_list.GetSize() == 0)
2089         {
2090             // No matches, search for basic typename matches
2091             ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2092             if (clang_ast)
2093                 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
2094         }
2095     }
2096     return sb_type_list;
2097 }
2098 
2099 SBValueList
2100 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2101 {
2102     SBValueList sb_value_list;
2103 
2104     TargetSP target_sp(GetSP());
2105     if (name && target_sp)
2106     {
2107         VariableList variable_list;
2108         const bool append = true;
2109         const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2110                                                                                  append,
2111                                                                                  max_matches,
2112                                                                                  variable_list);
2113 
2114         if (match_count > 0)
2115         {
2116             ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2117             if (exe_scope == NULL)
2118                 exe_scope = target_sp.get();
2119             for (uint32_t i=0; i<match_count; ++i)
2120             {
2121                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2122                 if (valobj_sp)
2123                     sb_value_list.Append(SBValue(valobj_sp));
2124             }
2125         }
2126     }
2127 
2128     return sb_value_list;
2129 }
2130 
2131 SBValueList
2132 SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype)
2133 {
2134     SBValueList sb_value_list;
2135 
2136     TargetSP target_sp(GetSP());
2137     if (name && target_sp)
2138     {
2139         VariableList variable_list;
2140         const bool append = true;
2141 
2142         std::string regexstr;
2143         uint32_t match_count;
2144         switch (matchtype)
2145         {
2146         case eMatchTypeNormal:
2147             match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name),
2148                 append,
2149                 max_matches,
2150                 variable_list);
2151             break;
2152         case eMatchTypeRegex:
2153             match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name),
2154                 append,
2155                 max_matches,
2156                 variable_list);
2157             break;
2158         case eMatchTypeStartsWith:
2159             regexstr = llvm::Regex::escape(name) + ".*";
2160             match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()),
2161                 append,
2162                 max_matches,
2163                 variable_list);
2164             break;
2165         }
2166 
2167 
2168         if (match_count > 0)
2169         {
2170             ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2171             if (exe_scope == NULL)
2172                 exe_scope = target_sp.get();
2173             for (uint32_t i = 0; i<match_count; ++i)
2174             {
2175                 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(exe_scope, variable_list.GetVariableAtIndex(i)));
2176                 if (valobj_sp)
2177                     sb_value_list.Append(SBValue(valobj_sp));
2178             }
2179         }
2180     }
2181 
2182     return sb_value_list;
2183 }
2184 
2185 
2186 lldb::SBValue
2187 SBTarget::FindFirstGlobalVariable (const char* name)
2188 {
2189     SBValueList sb_value_list(FindGlobalVariables(name, 1));
2190     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2191         return sb_value_list.GetValueAtIndex(0);
2192     return SBValue();
2193 }
2194 
2195 SBSourceManager
2196 SBTarget::GetSourceManager()
2197 {
2198     SBSourceManager source_manager (*this);
2199     return source_manager;
2200 }
2201 
2202 lldb::SBInstructionList
2203 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2204 {
2205     return ReadInstructions (base_addr, count, NULL);
2206 }
2207 
2208 lldb::SBInstructionList
2209 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2210 {
2211     SBInstructionList sb_instructions;
2212 
2213     TargetSP target_sp(GetSP());
2214     if (target_sp)
2215     {
2216         Address *addr_ptr = base_addr.get();
2217 
2218         if (addr_ptr)
2219         {
2220             DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2221             bool prefer_file_cache = false;
2222             lldb_private::Error error;
2223             lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2224             const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2225                                                             prefer_file_cache,
2226                                                             data.GetBytes(),
2227                                                             data.GetByteSize(),
2228                                                             error,
2229                                                             &load_addr);
2230             const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2231             sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2232                                                                              NULL,
2233                                                                              flavor_string,
2234                                                                              *addr_ptr,
2235                                                                              data.GetBytes(),
2236                                                                              bytes_read,
2237                                                                              count,
2238                                                                              data_from_file));
2239         }
2240     }
2241 
2242     return sb_instructions;
2243 
2244 }
2245 
2246 lldb::SBInstructionList
2247 SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2248 {
2249     return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2250 }
2251 
2252 lldb::SBInstructionList
2253 SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2254 {
2255     SBInstructionList sb_instructions;
2256 
2257     TargetSP target_sp(GetSP());
2258     if (target_sp)
2259     {
2260         Address addr;
2261 
2262         if (base_addr.get())
2263             addr = *base_addr.get();
2264 
2265         const bool data_from_file = true;
2266 
2267         sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2268                                                                          NULL,
2269                                                                          flavor_string,
2270                                                                          addr,
2271                                                                          buf,
2272                                                                          size,
2273                                                                          UINT32_MAX,
2274                                                                          data_from_file));
2275     }
2276 
2277     return sb_instructions;
2278 }
2279 
2280 lldb::SBInstructionList
2281 SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2282 {
2283     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2284 }
2285 
2286 lldb::SBInstructionList
2287 SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2288 {
2289     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
2290 }
2291 
2292 SBError
2293 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2294                                  lldb::addr_t section_base_addr)
2295 {
2296     SBError sb_error;
2297     TargetSP target_sp(GetSP());
2298     if (target_sp)
2299     {
2300         if (!section.IsValid())
2301         {
2302             sb_error.SetErrorStringWithFormat ("invalid section");
2303         }
2304         else
2305         {
2306             SectionSP section_sp (section.GetSP());
2307             if (section_sp)
2308             {
2309                 if (section_sp->IsThreadSpecific())
2310                 {
2311                     sb_error.SetErrorString ("thread specific sections are not yet supported");
2312                 }
2313                 else
2314                 {
2315                     ProcessSP process_sp (target_sp->GetProcessSP());
2316                     if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
2317                     {
2318                         // Flush info in the process (stack frames, etc)
2319                         if (process_sp)
2320                             process_sp->Flush();
2321                     }
2322                 }
2323             }
2324         }
2325     }
2326     else
2327     {
2328         sb_error.SetErrorString ("invalid target");
2329     }
2330     return sb_error;
2331 }
2332 
2333 SBError
2334 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2335 {
2336     SBError sb_error;
2337 
2338     TargetSP target_sp(GetSP());
2339     if (target_sp)
2340     {
2341         if (!section.IsValid())
2342         {
2343             sb_error.SetErrorStringWithFormat ("invalid section");
2344         }
2345         else
2346         {
2347             ProcessSP process_sp (target_sp->GetProcessSP());
2348             if (target_sp->SetSectionUnloaded (section.GetSP()))
2349             {
2350                 // Flush info in the process (stack frames, etc)
2351                 if (process_sp)
2352                     process_sp->Flush();
2353             }
2354         }
2355     }
2356     else
2357     {
2358         sb_error.SetErrorStringWithFormat ("invalid target");
2359     }
2360     return sb_error;
2361 }
2362 
2363 SBError
2364 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2365 {
2366     SBError sb_error;
2367 
2368     TargetSP target_sp(GetSP());
2369     if (target_sp)
2370     {
2371         ModuleSP module_sp (module.GetSP());
2372         if (module_sp)
2373         {
2374             bool changed = false;
2375             if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
2376             {
2377                 // The load was successful, make sure that at least some sections
2378                 // changed before we notify that our module was loaded.
2379                 if (changed)
2380                 {
2381                     ModuleList module_list;
2382                     module_list.Append(module_sp);
2383                     target_sp->ModulesDidLoad (module_list);
2384                     // Flush info in the process (stack frames, etc)
2385                     ProcessSP process_sp (target_sp->GetProcessSP());
2386                     if (process_sp)
2387                         process_sp->Flush();
2388                 }
2389             }
2390         }
2391         else
2392         {
2393             sb_error.SetErrorStringWithFormat ("invalid module");
2394         }
2395 
2396     }
2397     else
2398     {
2399         sb_error.SetErrorStringWithFormat ("invalid target");
2400     }
2401     return sb_error;
2402 }
2403 
2404 SBError
2405 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2406 {
2407     SBError sb_error;
2408 
2409     char path[PATH_MAX];
2410     TargetSP target_sp(GetSP());
2411     if (target_sp)
2412     {
2413         ModuleSP module_sp (module.GetSP());
2414         if (module_sp)
2415         {
2416             ObjectFile *objfile = module_sp->GetObjectFile();
2417             if (objfile)
2418             {
2419                 SectionList *section_list = objfile->GetSectionList();
2420                 if (section_list)
2421                 {
2422                     ProcessSP process_sp (target_sp->GetProcessSP());
2423 
2424                     bool changed = false;
2425                     const size_t num_sections = section_list->GetSize();
2426                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2427                     {
2428                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2429                         if (section_sp)
2430                             changed |= target_sp->SetSectionUnloaded (section_sp);
2431                     }
2432                     if (changed)
2433                     {
2434                         // Flush info in the process (stack frames, etc)
2435                         ProcessSP process_sp (target_sp->GetProcessSP());
2436                         if (process_sp)
2437                             process_sp->Flush();
2438                     }
2439                 }
2440                 else
2441                 {
2442                     module_sp->GetFileSpec().GetPath (path, sizeof(path));
2443                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2444                 }
2445             }
2446             else
2447             {
2448                 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2449                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2450             }
2451         }
2452         else
2453         {
2454             sb_error.SetErrorStringWithFormat ("invalid module");
2455         }
2456     }
2457     else
2458     {
2459         sb_error.SetErrorStringWithFormat ("invalid target");
2460     }
2461     return sb_error;
2462 }
2463 
2464 
2465 lldb::SBSymbolContextList
2466 SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2467 {
2468     SBSymbolContextList sb_sc_list;
2469     if (name && name[0])
2470     {
2471         TargetSP target_sp(GetSP());
2472         if (target_sp)
2473         {
2474             bool append = true;
2475             target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2476                                                                symbol_type,
2477                                                                *sb_sc_list,
2478                                                                append);
2479         }
2480     }
2481     return sb_sc_list;
2482 
2483 }
2484 
2485 
2486 lldb::SBValue
2487 SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2488 {
2489     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2490     Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2491     SBValue expr_result;
2492     ExpressionResults exe_results = eExpressionSetupError;
2493     ValueObjectSP expr_value_sp;
2494     TargetSP target_sp(GetSP());
2495     StackFrame *frame = NULL;
2496     if (target_sp)
2497     {
2498         if (expr == NULL || expr[0] == '\0')
2499         {
2500             if (log)
2501                 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2502             return expr_result;
2503         }
2504 
2505         Mutex::Locker api_locker (target_sp->GetAPIMutex());
2506         ExecutionContext exe_ctx (m_opaque_sp.get());
2507 
2508         if (log)
2509             log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2510 
2511         frame = exe_ctx.GetFramePtr();
2512         Target *target = exe_ctx.GetTargetPtr();
2513 
2514         if (target)
2515         {
2516 #ifdef LLDB_CONFIGURATION_DEBUG
2517             StreamString frame_description;
2518             if (frame)
2519                 frame->DumpUsingSettingsFormat (&frame_description);
2520             Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2521                                                  expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2522 #endif
2523             exe_results = target->EvaluateExpression (expr,
2524                                                       frame,
2525                                                       expr_value_sp,
2526                                                       options.ref());
2527 
2528             expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2529 #ifdef LLDB_CONFIGURATION_DEBUG
2530             Host::SetCrashDescription (NULL);
2531 #endif
2532         }
2533         else
2534         {
2535             if (log)
2536                 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2537         }
2538     }
2539 #ifndef LLDB_DISABLE_PYTHON
2540     if (expr_log)
2541         expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2542                          expr_result.GetValue(), expr_result.GetSummary());
2543 
2544     if (log)
2545         log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2546                      static_cast<void*>(frame), expr,
2547                      static_cast<void*>(expr_value_sp.get()), exe_results);
2548 #endif
2549 
2550     return expr_result;
2551 }
2552 
2553 
2554 lldb::addr_t
2555 SBTarget::GetStackRedZoneSize()
2556 {
2557     TargetSP target_sp(GetSP());
2558     if (target_sp)
2559     {
2560         ABISP abi_sp;
2561         ProcessSP process_sp (target_sp->GetProcessSP());
2562         if (process_sp)
2563             abi_sp = process_sp->GetABI();
2564         else
2565             abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2566         if (abi_sp)
2567             return abi_sp->GetRedZoneSize();
2568     }
2569     return 0;
2570 }
2571