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