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