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, eLanguageTypeUnknown, skip_prologue, internal, hardware);
851         }
852         else
853         {
854             *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown, 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                                               eLanguageTypeUnknown,
896                                               skip_prologue,
897                                               internal,
898                                               hardware);
899     }
900 
901     if (log)
902         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
903                      static_cast<void*>(target_sp.get()), symbol_name,
904                      name_type_mask, static_cast<void*>(sb_bp.get()));
905 
906     return sb_bp;
907 }
908 
909 lldb::SBBreakpoint
910 SBTarget::BreakpointCreateByNames (const char *symbol_names[],
911                                    uint32_t num_names,
912                                    uint32_t name_type_mask,
913                                    const SBFileSpecList &module_list,
914                                    const SBFileSpecList &comp_unit_list)
915 {
916     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
917 
918     SBBreakpoint sb_bp;
919     TargetSP target_sp(GetSP());
920     if (target_sp && num_names > 0)
921     {
922         Mutex::Locker api_locker (target_sp->GetAPIMutex());
923         const bool internal = false;
924         const bool hardware = false;
925         const LazyBool skip_prologue = eLazyBoolCalculate;
926         *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
927                                                 comp_unit_list.get(),
928                                                 symbol_names,
929                                                 num_names,
930                                                 name_type_mask,
931                                                 eLanguageTypeUnknown,
932                                                 skip_prologue,
933                                                 internal,
934                                                 hardware);
935     }
936 
937     if (log)
938     {
939         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={",
940                      static_cast<void*>(target_sp.get()));
941         for (uint32_t i = 0 ; i < num_names; i++)
942         {
943             char sep;
944             if (i < num_names - 1)
945                 sep = ',';
946             else
947                 sep = '}';
948             if (symbol_names[i] != NULL)
949                 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
950             else
951                 log->Printf ("\"<NULL>\"%c ", sep);
952         }
953         log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
954                      static_cast<void*>(sb_bp.get()));
955     }
956 
957     return sb_bp;
958 }
959 
960 SBBreakpoint
961 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
962                                    const char *module_name)
963 {
964     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
965 
966     SBBreakpoint sb_bp;
967     TargetSP target_sp(GetSP());
968     if (target_sp && symbol_name_regex && symbol_name_regex[0])
969     {
970         Mutex::Locker api_locker (target_sp->GetAPIMutex());
971         RegularExpression regexp(symbol_name_regex);
972         const bool internal = false;
973         const bool hardware = false;
974         const LazyBool skip_prologue = eLazyBoolCalculate;
975 
976         if (module_name && module_name[0])
977         {
978             FileSpecList module_spec_list;
979             module_spec_list.Append (FileSpec (module_name, false));
980 
981             *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
982         }
983         else
984         {
985             *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
986         }
987     }
988 
989     if (log)
990         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
991                      static_cast<void*>(target_sp.get()), symbol_name_regex,
992                      module_name, static_cast<void*>(sb_bp.get()));
993 
994     return sb_bp;
995 }
996 
997 lldb::SBBreakpoint
998 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
999                                    const SBFileSpecList &module_list,
1000                                    const SBFileSpecList &comp_unit_list)
1001 {
1002     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1003 
1004     SBBreakpoint sb_bp;
1005     TargetSP target_sp(GetSP());
1006     if (target_sp && symbol_name_regex && symbol_name_regex[0])
1007     {
1008         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1009         RegularExpression regexp(symbol_name_regex);
1010         const bool internal = false;
1011         const bool hardware = false;
1012         const LazyBool skip_prologue = eLazyBoolCalculate;
1013 
1014         *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
1015     }
1016 
1017     if (log)
1018         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1019                      static_cast<void*>(target_sp.get()), symbol_name_regex,
1020                      static_cast<void*>(sb_bp.get()));
1021 
1022     return sb_bp;
1023 }
1024 
1025 SBBreakpoint
1026 SBTarget::BreakpointCreateByAddress (addr_t address)
1027 {
1028     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1029 
1030     SBBreakpoint sb_bp;
1031     TargetSP target_sp(GetSP());
1032     if (target_sp)
1033     {
1034         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1035         const bool hardware = false;
1036         *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
1037     }
1038 
1039     if (log)
1040         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)",
1041                      static_cast<void*>(target_sp.get()),
1042                      static_cast<uint64_t>(address),
1043                      static_cast<void*>(sb_bp.get()));
1044 
1045     return sb_bp;
1046 }
1047 
1048 lldb::SBBreakpoint
1049 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1050                                          const lldb::SBFileSpec &source_file,
1051                                          const char *module_name)
1052 {
1053     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1054 
1055     SBBreakpoint sb_bp;
1056     TargetSP target_sp(GetSP());
1057     if (target_sp && source_regex && source_regex[0])
1058     {
1059         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1060         RegularExpression regexp(source_regex);
1061         FileSpecList source_file_spec_list;
1062         const bool hardware = false;
1063         const LazyBool move_to_nearest_code = eLazyBoolCalculate;
1064         source_file_spec_list.Append (source_file.ref());
1065 
1066         if (module_name && module_name[0])
1067         {
1068             FileSpecList module_spec_list;
1069             module_spec_list.Append (FileSpec (module_name, false));
1070 
1071             *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware, move_to_nearest_code);
1072         }
1073         else
1074         {
1075             *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware, move_to_nearest_code);
1076         }
1077     }
1078 
1079     if (log)
1080     {
1081         char path[PATH_MAX];
1082         source_file->GetPath (path, sizeof(path));
1083         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1084                      static_cast<void*>(target_sp.get()), source_regex, path,
1085                      module_name, static_cast<void*>(sb_bp.get()));
1086     }
1087 
1088     return sb_bp;
1089 }
1090 
1091 lldb::SBBreakpoint
1092 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1093                                                  const SBFileSpecList &module_list,
1094                                                  const lldb::SBFileSpecList &source_file_list)
1095 {
1096     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1097 
1098     SBBreakpoint sb_bp;
1099     TargetSP target_sp(GetSP());
1100     if (target_sp && source_regex && source_regex[0])
1101     {
1102         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1103         const bool hardware = false;
1104         const LazyBool move_to_nearest_code = eLazyBoolCalculate;
1105         RegularExpression regexp(source_regex);
1106         *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware, move_to_nearest_code);
1107     }
1108 
1109     if (log)
1110         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1111                      static_cast<void*>(target_sp.get()), source_regex,
1112                      static_cast<void*>(sb_bp.get()));
1113 
1114     return sb_bp;
1115 }
1116 
1117 lldb::SBBreakpoint
1118 SBTarget::BreakpointCreateForException  (lldb::LanguageType language,
1119                                          bool catch_bp,
1120                                          bool throw_bp)
1121 {
1122     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1123 
1124     SBBreakpoint sb_bp;
1125     TargetSP target_sp(GetSP());
1126     if (target_sp)
1127     {
1128         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1129         const bool hardware = false;
1130         *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
1131     }
1132 
1133     if (log)
1134         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1135                      static_cast<void*>(target_sp.get()),
1136                      LanguageRuntime::GetNameForLanguageType(language),
1137                      catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1138                      static_cast<void*>(sb_bp.get()));
1139 
1140     return sb_bp;
1141 }
1142 
1143 uint32_t
1144 SBTarget::GetNumBreakpoints () const
1145 {
1146     TargetSP target_sp(GetSP());
1147     if (target_sp)
1148     {
1149         // The breakpoint list is thread safe, no need to lock
1150         return target_sp->GetBreakpointList().GetSize();
1151     }
1152     return 0;
1153 }
1154 
1155 SBBreakpoint
1156 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1157 {
1158     SBBreakpoint sb_breakpoint;
1159     TargetSP target_sp(GetSP());
1160     if (target_sp)
1161     {
1162         // The breakpoint list is thread safe, no need to lock
1163         *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1164     }
1165     return sb_breakpoint;
1166 }
1167 
1168 bool
1169 SBTarget::BreakpointDelete (break_id_t bp_id)
1170 {
1171     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1172 
1173     bool result = false;
1174     TargetSP target_sp(GetSP());
1175     if (target_sp)
1176     {
1177         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1178         result = target_sp->RemoveBreakpointByID (bp_id);
1179     }
1180 
1181     if (log)
1182         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1183                      static_cast<void*>(target_sp.get()),
1184                      static_cast<uint32_t>(bp_id), result);
1185 
1186     return result;
1187 }
1188 
1189 SBBreakpoint
1190 SBTarget::FindBreakpointByID (break_id_t bp_id)
1191 {
1192     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1193 
1194     SBBreakpoint sb_breakpoint;
1195     TargetSP target_sp(GetSP());
1196     if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
1197     {
1198         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1199         *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
1200     }
1201 
1202     if (log)
1203         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1204                      static_cast<void*>(target_sp.get()),
1205                      static_cast<uint32_t>(bp_id),
1206                      static_cast<void*>(sb_breakpoint.get()));
1207 
1208     return sb_breakpoint;
1209 }
1210 
1211 bool
1212 SBTarget::EnableAllBreakpoints ()
1213 {
1214     TargetSP target_sp(GetSP());
1215     if (target_sp)
1216     {
1217         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1218         target_sp->EnableAllBreakpoints ();
1219         return true;
1220     }
1221     return false;
1222 }
1223 
1224 bool
1225 SBTarget::DisableAllBreakpoints ()
1226 {
1227     TargetSP target_sp(GetSP());
1228     if (target_sp)
1229     {
1230         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1231         target_sp->DisableAllBreakpoints ();
1232         return true;
1233     }
1234     return false;
1235 }
1236 
1237 bool
1238 SBTarget::DeleteAllBreakpoints ()
1239 {
1240     TargetSP target_sp(GetSP());
1241     if (target_sp)
1242     {
1243         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1244         target_sp->RemoveAllBreakpoints ();
1245         return true;
1246     }
1247     return false;
1248 }
1249 
1250 uint32_t
1251 SBTarget::GetNumWatchpoints () const
1252 {
1253     TargetSP target_sp(GetSP());
1254     if (target_sp)
1255     {
1256         // The watchpoint list is thread safe, no need to lock
1257         return target_sp->GetWatchpointList().GetSize();
1258     }
1259     return 0;
1260 }
1261 
1262 SBWatchpoint
1263 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
1264 {
1265     SBWatchpoint sb_watchpoint;
1266     TargetSP target_sp(GetSP());
1267     if (target_sp)
1268     {
1269         // The watchpoint list is thread safe, no need to lock
1270         sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
1271     }
1272     return sb_watchpoint;
1273 }
1274 
1275 bool
1276 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
1277 {
1278     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1279 
1280     bool result = false;
1281     TargetSP target_sp(GetSP());
1282     if (target_sp)
1283     {
1284         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1285         Mutex::Locker locker;
1286         target_sp->GetWatchpointList().GetListMutex(locker);
1287         result = target_sp->RemoveWatchpointByID (wp_id);
1288     }
1289 
1290     if (log)
1291         log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1292                      static_cast<void*>(target_sp.get()),
1293                      static_cast<uint32_t>(wp_id), result);
1294 
1295     return result;
1296 }
1297 
1298 SBWatchpoint
1299 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
1300 {
1301     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1302 
1303     SBWatchpoint sb_watchpoint;
1304     lldb::WatchpointSP watchpoint_sp;
1305     TargetSP target_sp(GetSP());
1306     if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
1307     {
1308         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1309         Mutex::Locker locker;
1310         target_sp->GetWatchpointList().GetListMutex(locker);
1311         watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1312         sb_watchpoint.SetSP (watchpoint_sp);
1313     }
1314 
1315     if (log)
1316         log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1317                      static_cast<void*>(target_sp.get()),
1318                      static_cast<uint32_t>(wp_id),
1319                      static_cast<void*>(watchpoint_sp.get()));
1320 
1321     return sb_watchpoint;
1322 }
1323 
1324 lldb::SBWatchpoint
1325 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
1326 {
1327     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1328 
1329     SBWatchpoint sb_watchpoint;
1330     lldb::WatchpointSP watchpoint_sp;
1331     TargetSP target_sp(GetSP());
1332     if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
1333     {
1334         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1335         uint32_t watch_type = 0;
1336         if (read)
1337             watch_type |= LLDB_WATCH_TYPE_READ;
1338         if (write)
1339             watch_type |= LLDB_WATCH_TYPE_WRITE;
1340         if (watch_type == 0)
1341         {
1342             error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1343             return sb_watchpoint;
1344         }
1345 
1346         // Target::CreateWatchpoint() is thread safe.
1347         Error cw_error;
1348         // This API doesn't take in a type, so we can't figure out what it is.
1349         ClangASTType *type = NULL;
1350         watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1351         error.SetError(cw_error);
1352         sb_watchpoint.SetSP (watchpoint_sp);
1353     }
1354 
1355     if (log)
1356         log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
1357                      static_cast<void*>(target_sp.get()), addr,
1358                      static_cast<uint32_t>(size),
1359                      static_cast<void*>(watchpoint_sp.get()));
1360 
1361     return sb_watchpoint;
1362 }
1363 
1364 bool
1365 SBTarget::EnableAllWatchpoints ()
1366 {
1367     TargetSP target_sp(GetSP());
1368     if (target_sp)
1369     {
1370         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1371         Mutex::Locker locker;
1372         target_sp->GetWatchpointList().GetListMutex(locker);
1373         target_sp->EnableAllWatchpoints ();
1374         return true;
1375     }
1376     return false;
1377 }
1378 
1379 bool
1380 SBTarget::DisableAllWatchpoints ()
1381 {
1382     TargetSP target_sp(GetSP());
1383     if (target_sp)
1384     {
1385         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1386         Mutex::Locker locker;
1387         target_sp->GetWatchpointList().GetListMutex(locker);
1388         target_sp->DisableAllWatchpoints ();
1389         return true;
1390     }
1391     return false;
1392 }
1393 
1394 SBValue
1395 SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1396 {
1397     SBValue sb_value;
1398     lldb::ValueObjectSP new_value_sp;
1399     if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1400     {
1401         lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1402         ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1403         ClangASTType ast_type(type.GetSP()->GetClangASTType(true));
1404         new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr, exe_ctx, ast_type);
1405     }
1406     sb_value.SetSP(new_value_sp);
1407     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1408     if (log)
1409     {
1410         if (new_value_sp)
1411             log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1412                          static_cast<void*>(m_opaque_sp.get()),
1413                          new_value_sp->GetName().AsCString());
1414         else
1415             log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL",
1416                          static_cast<void*>(m_opaque_sp.get()));
1417     }
1418     return sb_value;
1419 }
1420 
1421 lldb::SBValue
1422 SBTarget::CreateValueFromData (const char *name, lldb::SBData data, lldb::SBType type)
1423 {
1424     SBValue sb_value;
1425     lldb::ValueObjectSP new_value_sp;
1426     if (IsValid() && name && *name && data.IsValid() && type.IsValid())
1427     {
1428         DataExtractorSP extractor(*data);
1429         ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1430         ClangASTType ast_type(type.GetSP()->GetClangASTType(true));
1431         new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor, exe_ctx, ast_type);
1432     }
1433     sb_value.SetSP(new_value_sp);
1434     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1435     if (log)
1436     {
1437         if (new_value_sp)
1438             log->Printf ("SBTarget(%p)::CreateValueFromData => \"%s\"",
1439                          static_cast<void*>(m_opaque_sp.get()),
1440                          new_value_sp->GetName().AsCString());
1441         else
1442             log->Printf ("SBTarget(%p)::CreateValueFromData => NULL",
1443                          static_cast<void*>(m_opaque_sp.get()));
1444     }
1445     return sb_value;
1446 }
1447 
1448 lldb::SBValue
1449 SBTarget::CreateValueFromExpression (const char *name, const char* expr)
1450 {
1451     SBValue sb_value;
1452     lldb::ValueObjectSP new_value_sp;
1453     if (IsValid() && name && *name && expr && *expr)
1454     {
1455         ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1456         new_value_sp = ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1457     }
1458     sb_value.SetSP(new_value_sp);
1459     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1460     if (log)
1461     {
1462         if (new_value_sp)
1463             log->Printf ("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1464                          static_cast<void*>(m_opaque_sp.get()),
1465                          new_value_sp->GetName().AsCString());
1466         else
1467             log->Printf ("SBTarget(%p)::CreateValueFromExpression => NULL",
1468                          static_cast<void*>(m_opaque_sp.get()));
1469     }
1470     return sb_value;
1471 }
1472 
1473 bool
1474 SBTarget::DeleteAllWatchpoints ()
1475 {
1476     TargetSP target_sp(GetSP());
1477     if (target_sp)
1478     {
1479         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1480         Mutex::Locker locker;
1481         target_sp->GetWatchpointList().GetListMutex(locker);
1482         target_sp->RemoveAllWatchpoints ();
1483         return true;
1484     }
1485     return false;
1486 }
1487 
1488 
1489 lldb::SBModule
1490 SBTarget::AddModule (const char *path,
1491                      const char *triple,
1492                      const char *uuid_cstr)
1493 {
1494     return AddModule (path, triple, uuid_cstr, NULL);
1495 }
1496 
1497 lldb::SBModule
1498 SBTarget::AddModule (const char *path,
1499                      const char *triple,
1500                      const char *uuid_cstr,
1501                      const char *symfile)
1502 {
1503     lldb::SBModule sb_module;
1504     TargetSP target_sp(GetSP());
1505     if (target_sp)
1506     {
1507         ModuleSpec module_spec;
1508         if (path)
1509             module_spec.GetFileSpec().SetFile(path, false);
1510 
1511         if (uuid_cstr)
1512             module_spec.GetUUID().SetFromCString(uuid_cstr);
1513 
1514         if (triple)
1515             module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1516         else
1517             module_spec.GetArchitecture() = target_sp->GetArchitecture();
1518 
1519         if (symfile)
1520             module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
1521 
1522         sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1523     }
1524     return sb_module;
1525 }
1526 
1527 lldb::SBModule
1528 SBTarget::AddModule (const SBModuleSpec &module_spec)
1529 {
1530     lldb::SBModule sb_module;
1531     TargetSP target_sp(GetSP());
1532     if (target_sp)
1533         sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1534     return sb_module;
1535 }
1536 
1537 bool
1538 SBTarget::AddModule (lldb::SBModule &module)
1539 {
1540     TargetSP target_sp(GetSP());
1541     if (target_sp)
1542     {
1543         target_sp->GetImages().AppendIfNeeded (module.GetSP());
1544         return true;
1545     }
1546     return false;
1547 }
1548 
1549 uint32_t
1550 SBTarget::GetNumModules () const
1551 {
1552     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1553 
1554     uint32_t num = 0;
1555     TargetSP target_sp(GetSP());
1556     if (target_sp)
1557     {
1558         // The module list is thread safe, no need to lock
1559         num = target_sp->GetImages().GetSize();
1560     }
1561 
1562     if (log)
1563         log->Printf ("SBTarget(%p)::GetNumModules () => %d",
1564                      static_cast<void*>(target_sp.get()), num);
1565 
1566     return num;
1567 }
1568 
1569 void
1570 SBTarget::Clear ()
1571 {
1572     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1573 
1574     if (log)
1575         log->Printf ("SBTarget(%p)::Clear ()",
1576                      static_cast<void*>(m_opaque_sp.get()));
1577 
1578     m_opaque_sp.reset();
1579 }
1580 
1581 
1582 SBModule
1583 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1584 {
1585     SBModule sb_module;
1586     TargetSP target_sp(GetSP());
1587     if (target_sp && sb_file_spec.IsValid())
1588     {
1589         ModuleSpec module_spec(*sb_file_spec);
1590         // The module list is thread safe, no need to lock
1591         sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
1592     }
1593     return sb_module;
1594 }
1595 
1596 lldb::ByteOrder
1597 SBTarget::GetByteOrder ()
1598 {
1599     TargetSP target_sp(GetSP());
1600     if (target_sp)
1601         return target_sp->GetArchitecture().GetByteOrder();
1602     return eByteOrderInvalid;
1603 }
1604 
1605 const char *
1606 SBTarget::GetTriple ()
1607 {
1608     TargetSP target_sp(GetSP());
1609     if (target_sp)
1610     {
1611         std::string triple (target_sp->GetArchitecture().GetTriple().str());
1612         // Unique the string so we don't run into ownership issues since
1613         // the const strings put the string into the string pool once and
1614         // the strings never comes out
1615         ConstString const_triple (triple.c_str());
1616         return const_triple.GetCString();
1617     }
1618     return NULL;
1619 }
1620 
1621 uint32_t
1622 SBTarget::GetDataByteSize ()
1623 {
1624     TargetSP target_sp(GetSP());
1625     if (target_sp)
1626     {
1627         return target_sp->GetArchitecture().GetDataByteSize() ;
1628     }
1629     return 0;
1630 }
1631 
1632 uint32_t
1633 SBTarget::GetCodeByteSize ()
1634 {
1635     TargetSP target_sp(GetSP());
1636     if (target_sp)
1637     {
1638         return target_sp->GetArchitecture().GetCodeByteSize() ;
1639     }
1640     return 0;
1641 }
1642 
1643 uint32_t
1644 SBTarget::GetAddressByteSize()
1645 {
1646     TargetSP target_sp(GetSP());
1647     if (target_sp)
1648         return target_sp->GetArchitecture().GetAddressByteSize();
1649     return sizeof(void*);
1650 }
1651 
1652 
1653 SBModule
1654 SBTarget::GetModuleAtIndex (uint32_t idx)
1655 {
1656     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1657 
1658     SBModule sb_module;
1659     ModuleSP module_sp;
1660     TargetSP target_sp(GetSP());
1661     if (target_sp)
1662     {
1663         // The module list is thread safe, no need to lock
1664         module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1665         sb_module.SetSP (module_sp);
1666     }
1667 
1668     if (log)
1669         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1670                      static_cast<void*>(target_sp.get()), idx,
1671                      static_cast<void*>(module_sp.get()));
1672 
1673     return sb_module;
1674 }
1675 
1676 bool
1677 SBTarget::RemoveModule (lldb::SBModule module)
1678 {
1679     TargetSP target_sp(GetSP());
1680     if (target_sp)
1681         return target_sp->GetImages().Remove(module.GetSP());
1682     return false;
1683 }
1684 
1685 
1686 SBBroadcaster
1687 SBTarget::GetBroadcaster () const
1688 {
1689     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1690 
1691     TargetSP target_sp(GetSP());
1692     SBBroadcaster broadcaster(target_sp.get(), false);
1693 
1694     if (log)
1695         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1696                      static_cast<void*>(target_sp.get()),
1697                      static_cast<void*>(broadcaster.get()));
1698 
1699     return broadcaster;
1700 }
1701 
1702 bool
1703 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
1704 {
1705     Stream &strm = description.ref();
1706 
1707     TargetSP target_sp(GetSP());
1708     if (target_sp)
1709     {
1710         target_sp->Dump (&strm, description_level);
1711     }
1712     else
1713         strm.PutCString ("No value");
1714 
1715     return true;
1716 }
1717 
1718 lldb::SBSymbolContextList
1719 SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
1720 {
1721     lldb::SBSymbolContextList sb_sc_list;
1722     if (name && name[0])
1723     {
1724         TargetSP target_sp(GetSP());
1725         if (target_sp)
1726         {
1727             const bool symbols_ok = true;
1728             const bool inlines_ok = true;
1729             const bool append = true;
1730             target_sp->GetImages().FindFunctions (ConstString(name),
1731                                                   name_type_mask,
1732                                                   symbols_ok,
1733                                                   inlines_ok,
1734                                                   append,
1735                                                   *sb_sc_list);
1736         }
1737     }
1738     return sb_sc_list;
1739 }
1740 
1741 lldb::SBSymbolContextList
1742 SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype)
1743 {
1744     lldb::SBSymbolContextList sb_sc_list;
1745     if (name && name[0])
1746     {
1747         TargetSP target_sp(GetSP());
1748         if (target_sp)
1749         {
1750             std::string regexstr;
1751             switch (matchtype)
1752             {
1753             case eMatchTypeRegex:
1754                 target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list);
1755                 break;
1756             case eMatchTypeStartsWith:
1757                 regexstr = llvm::Regex::escape(name) + ".*";
1758                 target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list);
1759                 break;
1760             default:
1761                 target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list);
1762                 break;
1763             }
1764         }
1765     }
1766     return sb_sc_list;
1767 }
1768 
1769 lldb::SBType
1770 SBTarget::FindFirstType (const char* typename_cstr)
1771 {
1772     TargetSP target_sp(GetSP());
1773     if (typename_cstr && typename_cstr[0] && target_sp)
1774     {
1775         ConstString const_typename(typename_cstr);
1776         SymbolContext sc;
1777         const bool exact_match = false;
1778 
1779         const ModuleList &module_list = target_sp->GetImages();
1780         size_t count = module_list.GetSize();
1781         for (size_t idx = 0; idx < count; idx++)
1782         {
1783             ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
1784             if (module_sp)
1785             {
1786                 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
1787                 if (type_sp)
1788                     return SBType(type_sp);
1789             }
1790         }
1791 
1792         // Didn't find the type in the symbols; try the Objective-C runtime
1793         // if one is installed
1794 
1795         ProcessSP process_sp(target_sp->GetProcessSP());
1796 
1797         if (process_sp)
1798         {
1799             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1800 
1801             if (objc_language_runtime)
1802             {
1803                 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1804 
1805                 if (objc_decl_vendor)
1806                 {
1807                     std::vector <clang::NamedDecl *> decls;
1808 
1809                     if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
1810                     {
1811                         if (ClangASTType type = ClangASTContext::GetTypeForDecl(decls[0]))
1812                         {
1813                             return SBType(type);
1814                         }
1815                     }
1816                 }
1817             }
1818         }
1819 
1820         // No matches, search for basic typename matches
1821         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1822         if (clang_ast)
1823             return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
1824     }
1825     return SBType();
1826 }
1827 
1828 SBType
1829 SBTarget::GetBasicType(lldb::BasicType type)
1830 {
1831     TargetSP target_sp(GetSP());
1832     if (target_sp)
1833     {
1834         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1835         if (clang_ast)
1836             return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
1837     }
1838     return SBType();
1839 }
1840 
1841 
1842 lldb::SBTypeList
1843 SBTarget::FindTypes (const char* typename_cstr)
1844 {
1845     SBTypeList sb_type_list;
1846     TargetSP target_sp(GetSP());
1847     if (typename_cstr && typename_cstr[0] && target_sp)
1848     {
1849         ModuleList& images = target_sp->GetImages();
1850         ConstString const_typename(typename_cstr);
1851         bool exact_match = false;
1852         SymbolContext sc;
1853         TypeList type_list;
1854 
1855         uint32_t num_matches = images.FindTypes (sc,
1856                                                  const_typename,
1857                                                  exact_match,
1858                                                  UINT32_MAX,
1859                                                  type_list);
1860 
1861         if (num_matches > 0)
1862         {
1863             for (size_t idx = 0; idx < num_matches; idx++)
1864             {
1865                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1866                 if (type_sp)
1867                     sb_type_list.Append(SBType(type_sp));
1868             }
1869         }
1870 
1871         // Try the Objective-C runtime if one is installed
1872 
1873         ProcessSP process_sp(target_sp->GetProcessSP());
1874 
1875         if (process_sp)
1876         {
1877             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1878 
1879             if (objc_language_runtime)
1880             {
1881                 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1882 
1883                 if (objc_decl_vendor)
1884                 {
1885                     std::vector <clang::NamedDecl *> decls;
1886 
1887                     if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
1888                     {
1889                         for (clang::NamedDecl *decl : decls)
1890                         {
1891                             if (ClangASTType type = ClangASTContext::GetTypeForDecl(decl))
1892                             {
1893                                 sb_type_list.Append(SBType(type));
1894                             }
1895                         }
1896                     }
1897                 }
1898             }
1899         }
1900 
1901         if (sb_type_list.GetSize() == 0)
1902         {
1903             // No matches, search for basic typename matches
1904             ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1905             if (clang_ast)
1906                 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
1907         }
1908     }
1909     return sb_type_list;
1910 }
1911 
1912 SBValueList
1913 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1914 {
1915     SBValueList sb_value_list;
1916 
1917     TargetSP target_sp(GetSP());
1918     if (name && target_sp)
1919     {
1920         VariableList variable_list;
1921         const bool append = true;
1922         const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
1923                                                                                  append,
1924                                                                                  max_matches,
1925                                                                                  variable_list);
1926 
1927         if (match_count > 0)
1928         {
1929             ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1930             if (exe_scope == NULL)
1931                 exe_scope = target_sp.get();
1932             for (uint32_t i=0; i<match_count; ++i)
1933             {
1934                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
1935                 if (valobj_sp)
1936                     sb_value_list.Append(SBValue(valobj_sp));
1937             }
1938         }
1939     }
1940 
1941     return sb_value_list;
1942 }
1943 
1944 SBValueList
1945 SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype)
1946 {
1947     SBValueList sb_value_list;
1948 
1949     TargetSP target_sp(GetSP());
1950     if (name && target_sp)
1951     {
1952         VariableList variable_list;
1953         const bool append = true;
1954 
1955         std::string regexstr;
1956         uint32_t match_count;
1957         switch (matchtype)
1958         {
1959         case eMatchTypeNormal:
1960             match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name),
1961                 append,
1962                 max_matches,
1963                 variable_list);
1964             break;
1965         case eMatchTypeRegex:
1966             match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name),
1967                 append,
1968                 max_matches,
1969                 variable_list);
1970             break;
1971         case eMatchTypeStartsWith:
1972             regexstr = llvm::Regex::escape(name) + ".*";
1973             match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()),
1974                 append,
1975                 max_matches,
1976                 variable_list);
1977             break;
1978         }
1979 
1980 
1981         if (match_count > 0)
1982         {
1983             ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1984             if (exe_scope == NULL)
1985                 exe_scope = target_sp.get();
1986             for (uint32_t i = 0; i<match_count; ++i)
1987             {
1988                 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(exe_scope, variable_list.GetVariableAtIndex(i)));
1989                 if (valobj_sp)
1990                     sb_value_list.Append(SBValue(valobj_sp));
1991             }
1992         }
1993     }
1994 
1995     return sb_value_list;
1996 }
1997 
1998 
1999 lldb::SBValue
2000 SBTarget::FindFirstGlobalVariable (const char* name)
2001 {
2002     SBValueList sb_value_list(FindGlobalVariables(name, 1));
2003     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2004         return sb_value_list.GetValueAtIndex(0);
2005     return SBValue();
2006 }
2007 
2008 SBSourceManager
2009 SBTarget::GetSourceManager()
2010 {
2011     SBSourceManager source_manager (*this);
2012     return source_manager;
2013 }
2014 
2015 lldb::SBInstructionList
2016 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2017 {
2018     return ReadInstructions (base_addr, count, NULL);
2019 }
2020 
2021 lldb::SBInstructionList
2022 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2023 {
2024     SBInstructionList sb_instructions;
2025 
2026     TargetSP target_sp(GetSP());
2027     if (target_sp)
2028     {
2029         Address *addr_ptr = base_addr.get();
2030 
2031         if (addr_ptr)
2032         {
2033             DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2034             bool prefer_file_cache = false;
2035             lldb_private::Error error;
2036             lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2037             const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2038                                                             prefer_file_cache,
2039                                                             data.GetBytes(),
2040                                                             data.GetByteSize(),
2041                                                             error,
2042                                                             &load_addr);
2043             const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2044             sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2045                                                                              NULL,
2046                                                                              flavor_string,
2047                                                                              *addr_ptr,
2048                                                                              data.GetBytes(),
2049                                                                              bytes_read,
2050                                                                              count,
2051                                                                              data_from_file));
2052         }
2053     }
2054 
2055     return sb_instructions;
2056 
2057 }
2058 
2059 lldb::SBInstructionList
2060 SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2061 {
2062     return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2063 }
2064 
2065 lldb::SBInstructionList
2066 SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2067 {
2068     SBInstructionList sb_instructions;
2069 
2070     TargetSP target_sp(GetSP());
2071     if (target_sp)
2072     {
2073         Address addr;
2074 
2075         if (base_addr.get())
2076             addr = *base_addr.get();
2077 
2078         const bool data_from_file = true;
2079 
2080         sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2081                                                                          NULL,
2082                                                                          flavor_string,
2083                                                                          addr,
2084                                                                          buf,
2085                                                                          size,
2086                                                                          UINT32_MAX,
2087                                                                          data_from_file));
2088     }
2089 
2090     return sb_instructions;
2091 }
2092 
2093 lldb::SBInstructionList
2094 SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2095 {
2096     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2097 }
2098 
2099 lldb::SBInstructionList
2100 SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2101 {
2102     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
2103 }
2104 
2105 SBError
2106 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2107                                  lldb::addr_t section_base_addr)
2108 {
2109     SBError sb_error;
2110     TargetSP target_sp(GetSP());
2111     if (target_sp)
2112     {
2113         if (!section.IsValid())
2114         {
2115             sb_error.SetErrorStringWithFormat ("invalid section");
2116         }
2117         else
2118         {
2119             SectionSP section_sp (section.GetSP());
2120             if (section_sp)
2121             {
2122                 if (section_sp->IsThreadSpecific())
2123                 {
2124                     sb_error.SetErrorString ("thread specific sections are not yet supported");
2125                 }
2126                 else
2127                 {
2128                     ProcessSP process_sp (target_sp->GetProcessSP());
2129                     if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
2130                     {
2131                         // Flush info in the process (stack frames, etc)
2132                         if (process_sp)
2133                             process_sp->Flush();
2134                     }
2135                 }
2136             }
2137         }
2138     }
2139     else
2140     {
2141         sb_error.SetErrorString ("invalid target");
2142     }
2143     return sb_error;
2144 }
2145 
2146 SBError
2147 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2148 {
2149     SBError sb_error;
2150 
2151     TargetSP target_sp(GetSP());
2152     if (target_sp)
2153     {
2154         if (!section.IsValid())
2155         {
2156             sb_error.SetErrorStringWithFormat ("invalid section");
2157         }
2158         else
2159         {
2160             ProcessSP process_sp (target_sp->GetProcessSP());
2161             if (target_sp->SetSectionUnloaded (section.GetSP()))
2162             {
2163                 // Flush info in the process (stack frames, etc)
2164                 if (process_sp)
2165                     process_sp->Flush();
2166             }
2167         }
2168     }
2169     else
2170     {
2171         sb_error.SetErrorStringWithFormat ("invalid target");
2172     }
2173     return sb_error;
2174 }
2175 
2176 SBError
2177 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2178 {
2179     SBError sb_error;
2180 
2181     TargetSP target_sp(GetSP());
2182     if (target_sp)
2183     {
2184         ModuleSP module_sp (module.GetSP());
2185         if (module_sp)
2186         {
2187             bool changed = false;
2188             if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
2189             {
2190                 // The load was successful, make sure that at least some sections
2191                 // changed before we notify that our module was loaded.
2192                 if (changed)
2193                 {
2194                     ModuleList module_list;
2195                     module_list.Append(module_sp);
2196                     target_sp->ModulesDidLoad (module_list);
2197                     // Flush info in the process (stack frames, etc)
2198                     ProcessSP process_sp (target_sp->GetProcessSP());
2199                     if (process_sp)
2200                         process_sp->Flush();
2201                 }
2202             }
2203         }
2204         else
2205         {
2206             sb_error.SetErrorStringWithFormat ("invalid module");
2207         }
2208 
2209     }
2210     else
2211     {
2212         sb_error.SetErrorStringWithFormat ("invalid target");
2213     }
2214     return sb_error;
2215 }
2216 
2217 SBError
2218 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2219 {
2220     SBError sb_error;
2221 
2222     char path[PATH_MAX];
2223     TargetSP target_sp(GetSP());
2224     if (target_sp)
2225     {
2226         ModuleSP module_sp (module.GetSP());
2227         if (module_sp)
2228         {
2229             ObjectFile *objfile = module_sp->GetObjectFile();
2230             if (objfile)
2231             {
2232                 SectionList *section_list = objfile->GetSectionList();
2233                 if (section_list)
2234                 {
2235                     ProcessSP process_sp (target_sp->GetProcessSP());
2236 
2237                     bool changed = false;
2238                     const size_t num_sections = section_list->GetSize();
2239                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2240                     {
2241                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2242                         if (section_sp)
2243                             changed |= target_sp->SetSectionUnloaded (section_sp);
2244                     }
2245                     if (changed)
2246                     {
2247                         // Flush info in the process (stack frames, etc)
2248                         ProcessSP process_sp (target_sp->GetProcessSP());
2249                         if (process_sp)
2250                             process_sp->Flush();
2251                     }
2252                 }
2253                 else
2254                 {
2255                     module_sp->GetFileSpec().GetPath (path, sizeof(path));
2256                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2257                 }
2258             }
2259             else
2260             {
2261                 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2262                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2263             }
2264         }
2265         else
2266         {
2267             sb_error.SetErrorStringWithFormat ("invalid module");
2268         }
2269     }
2270     else
2271     {
2272         sb_error.SetErrorStringWithFormat ("invalid target");
2273     }
2274     return sb_error;
2275 }
2276 
2277 
2278 lldb::SBSymbolContextList
2279 SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2280 {
2281     SBSymbolContextList sb_sc_list;
2282     if (name && name[0])
2283     {
2284         TargetSP target_sp(GetSP());
2285         if (target_sp)
2286         {
2287             bool append = true;
2288             target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2289                                                                symbol_type,
2290                                                                *sb_sc_list,
2291                                                                append);
2292         }
2293     }
2294     return sb_sc_list;
2295 
2296 }
2297 
2298 lldb::SBValue
2299 SBTarget::EvaluateExpression (const char *expr)
2300 {
2301     TargetSP target_sp(GetSP());
2302     if (!target_sp)
2303         return SBValue();
2304 
2305     SBExpressionOptions options;
2306     lldb::DynamicValueType fetch_dynamic_value = target_sp->GetPreferDynamicValue();
2307     options.SetFetchDynamicValue (fetch_dynamic_value);
2308     options.SetUnwindOnError (true);
2309     return EvaluateExpression(expr, options);
2310 }
2311 
2312 lldb::SBValue
2313 SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2314 {
2315     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2316     Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2317     SBValue expr_result;
2318     ExpressionResults exe_results = eExpressionSetupError;
2319     ValueObjectSP expr_value_sp;
2320     TargetSP target_sp(GetSP());
2321     StackFrame *frame = NULL;
2322     if (target_sp)
2323     {
2324         if (expr == NULL || expr[0] == '\0')
2325         {
2326             if (log)
2327                 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2328             return expr_result;
2329         }
2330 
2331         Mutex::Locker api_locker (target_sp->GetAPIMutex());
2332         ExecutionContext exe_ctx (m_opaque_sp.get());
2333 
2334         if (log)
2335             log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2336 
2337         frame = exe_ctx.GetFramePtr();
2338         Target *target = exe_ctx.GetTargetPtr();
2339 
2340         if (target)
2341         {
2342 #ifdef LLDB_CONFIGURATION_DEBUG
2343             StreamString frame_description;
2344             if (frame)
2345                 frame->DumpUsingSettingsFormat (&frame_description);
2346             Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2347                                                  expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2348 #endif
2349             exe_results = target->EvaluateExpression (expr,
2350                                                       frame,
2351                                                       expr_value_sp,
2352                                                       options.ref());
2353 
2354             expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2355 #ifdef LLDB_CONFIGURATION_DEBUG
2356             Host::SetCrashDescription (NULL);
2357 #endif
2358         }
2359         else
2360         {
2361             if (log)
2362                 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2363         }
2364     }
2365 #ifndef LLDB_DISABLE_PYTHON
2366     if (expr_log)
2367         expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2368                          expr_result.GetValue(), expr_result.GetSummary());
2369 
2370     if (log)
2371         log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2372                      static_cast<void*>(frame), expr,
2373                      static_cast<void*>(expr_value_sp.get()), exe_results);
2374 #endif
2375 
2376     return expr_result;
2377 }
2378 
2379 
2380 lldb::addr_t
2381 SBTarget::GetStackRedZoneSize()
2382 {
2383     TargetSP target_sp(GetSP());
2384     if (target_sp)
2385     {
2386         ABISP abi_sp;
2387         ProcessSP process_sp (target_sp->GetProcessSP());
2388         if (process_sp)
2389             abi_sp = process_sp->GetABI();
2390         else
2391             abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2392         if (abi_sp)
2393             return abi_sp->GetRedZoneSize();
2394     }
2395     return 0;
2396 }
2397 
2398 lldb::SBLaunchInfo
2399 SBTarget::GetLaunchInfo () const
2400 {
2401     lldb::SBLaunchInfo launch_info(NULL);
2402     TargetSP target_sp(GetSP());
2403     if (target_sp)
2404         launch_info.ref() = m_opaque_sp->GetProcessLaunchInfo();
2405     return launch_info;
2406 }
2407 
2408 void
2409 SBTarget::SetLaunchInfo (const lldb::SBLaunchInfo &launch_info)
2410 {
2411     TargetSP target_sp(GetSP());
2412     if (target_sp)
2413         m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
2414 }
2415