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