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