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