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