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         *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
812     }
813 
814     if (log)
815     {
816         SBStream sstr;
817         sb_bp.GetDescription (sstr);
818         char path[PATH_MAX];
819         sb_file_spec->GetPath (path, sizeof(path));
820         log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
821                      static_cast<void*>(target_sp.get()), path, line,
822                      static_cast<void*>(sb_bp.get()), sstr.GetData());
823     }
824 
825     return sb_bp;
826 }
827 
828 SBBreakpoint
829 SBTarget::BreakpointCreateByName (const char *symbol_name,
830                                   const char *module_name)
831 {
832     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
833 
834     SBBreakpoint sb_bp;
835     TargetSP target_sp(GetSP());
836     if (target_sp.get())
837     {
838         Mutex::Locker api_locker (target_sp->GetAPIMutex());
839 
840         const bool internal = false;
841         const bool hardware = false;
842         const LazyBool skip_prologue = eLazyBoolCalculate;
843         if (module_name && module_name[0])
844         {
845             FileSpecList module_spec_list;
846             module_spec_list.Append (FileSpec (module_name, false));
847             *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
848         }
849         else
850         {
851             *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
852         }
853     }
854 
855     if (log)
856         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
857                      static_cast<void*>(target_sp.get()), symbol_name,
858                      module_name, static_cast<void*>(sb_bp.get()));
859 
860     return sb_bp;
861 }
862 
863 lldb::SBBreakpoint
864 SBTarget::BreakpointCreateByName (const char *symbol_name,
865                                   const SBFileSpecList &module_list,
866                                   const SBFileSpecList &comp_unit_list)
867 {
868     uint32_t name_type_mask = eFunctionNameTypeAuto;
869     return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
870 }
871 
872 lldb::SBBreakpoint
873 SBTarget::BreakpointCreateByName (const char *symbol_name,
874                                   uint32_t name_type_mask,
875                                   const SBFileSpecList &module_list,
876                                   const SBFileSpecList &comp_unit_list)
877 {
878     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
879 
880     SBBreakpoint sb_bp;
881     TargetSP target_sp(GetSP());
882     if (target_sp && symbol_name && symbol_name[0])
883     {
884         const bool internal = false;
885         const bool hardware = false;
886         const LazyBool skip_prologue = eLazyBoolCalculate;
887         Mutex::Locker api_locker (target_sp->GetAPIMutex());
888         *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
889                                               comp_unit_list.get(),
890                                               symbol_name,
891                                               name_type_mask,
892                                               skip_prologue,
893                                               internal,
894                                               hardware);
895     }
896 
897     if (log)
898         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
899                      static_cast<void*>(target_sp.get()), symbol_name,
900                      name_type_mask, static_cast<void*>(sb_bp.get()));
901 
902     return sb_bp;
903 }
904 
905 lldb::SBBreakpoint
906 SBTarget::BreakpointCreateByNames (const char *symbol_names[],
907                                    uint32_t num_names,
908                                    uint32_t name_type_mask,
909                                    const SBFileSpecList &module_list,
910                                    const SBFileSpecList &comp_unit_list)
911 {
912     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
913 
914     SBBreakpoint sb_bp;
915     TargetSP target_sp(GetSP());
916     if (target_sp && num_names > 0)
917     {
918         Mutex::Locker api_locker (target_sp->GetAPIMutex());
919         const bool internal = false;
920         const bool hardware = false;
921         const LazyBool skip_prologue = eLazyBoolCalculate;
922         *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
923                                                 comp_unit_list.get(),
924                                                 symbol_names,
925                                                 num_names,
926                                                 name_type_mask,
927                                                 skip_prologue,
928                                                 internal,
929                                                 hardware);
930     }
931 
932     if (log)
933     {
934         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={",
935                      static_cast<void*>(target_sp.get()));
936         for (uint32_t i = 0 ; i < num_names; i++)
937         {
938             char sep;
939             if (i < num_names - 1)
940                 sep = ',';
941             else
942                 sep = '}';
943             if (symbol_names[i] != NULL)
944                 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
945             else
946                 log->Printf ("\"<NULL>\"%c ", sep);
947         }
948         log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
949                      static_cast<void*>(sb_bp.get()));
950     }
951 
952     return sb_bp;
953 }
954 
955 SBBreakpoint
956 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
957                                    const char *module_name)
958 {
959     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
960 
961     SBBreakpoint sb_bp;
962     TargetSP target_sp(GetSP());
963     if (target_sp && symbol_name_regex && symbol_name_regex[0])
964     {
965         Mutex::Locker api_locker (target_sp->GetAPIMutex());
966         RegularExpression regexp(symbol_name_regex);
967         const bool internal = false;
968         const bool hardware = false;
969         const LazyBool skip_prologue = eLazyBoolCalculate;
970 
971         if (module_name && module_name[0])
972         {
973             FileSpecList module_spec_list;
974             module_spec_list.Append (FileSpec (module_name, false));
975 
976             *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
977         }
978         else
979         {
980             *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
981         }
982     }
983 
984     if (log)
985         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
986                      static_cast<void*>(target_sp.get()), symbol_name_regex,
987                      module_name, static_cast<void*>(sb_bp.get()));
988 
989     return sb_bp;
990 }
991 
992 lldb::SBBreakpoint
993 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
994                                    const SBFileSpecList &module_list,
995                                    const SBFileSpecList &comp_unit_list)
996 {
997     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
998 
999     SBBreakpoint sb_bp;
1000     TargetSP target_sp(GetSP());
1001     if (target_sp && symbol_name_regex && symbol_name_regex[0])
1002     {
1003         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1004         RegularExpression regexp(symbol_name_regex);
1005         const bool internal = false;
1006         const bool hardware = false;
1007         const LazyBool skip_prologue = eLazyBoolCalculate;
1008 
1009         *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
1010     }
1011 
1012     if (log)
1013         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1014                      static_cast<void*>(target_sp.get()), symbol_name_regex,
1015                      static_cast<void*>(sb_bp.get()));
1016 
1017     return sb_bp;
1018 }
1019 
1020 SBBreakpoint
1021 SBTarget::BreakpointCreateByAddress (addr_t address)
1022 {
1023     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1024 
1025     SBBreakpoint sb_bp;
1026     TargetSP target_sp(GetSP());
1027     if (target_sp)
1028     {
1029         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1030         const bool hardware = false;
1031         *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
1032     }
1033 
1034     if (log)
1035         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)",
1036                      static_cast<void*>(target_sp.get()),
1037                      static_cast<uint64_t>(address),
1038                      static_cast<void*>(sb_bp.get()));
1039 
1040     return sb_bp;
1041 }
1042 
1043 lldb::SBBreakpoint
1044 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1045                                          const lldb::SBFileSpec &source_file,
1046                                          const char *module_name)
1047 {
1048     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1049 
1050     SBBreakpoint sb_bp;
1051     TargetSP target_sp(GetSP());
1052     if (target_sp && source_regex && source_regex[0])
1053     {
1054         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1055         RegularExpression regexp(source_regex);
1056         FileSpecList source_file_spec_list;
1057         const bool hardware = false;
1058         source_file_spec_list.Append (source_file.ref());
1059 
1060         if (module_name && module_name[0])
1061         {
1062             FileSpecList module_spec_list;
1063             module_spec_list.Append (FileSpec (module_name, false));
1064 
1065             *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
1066         }
1067         else
1068         {
1069             *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
1070         }
1071     }
1072 
1073     if (log)
1074     {
1075         char path[PATH_MAX];
1076         source_file->GetPath (path, sizeof(path));
1077         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1078                      static_cast<void*>(target_sp.get()), source_regex, path,
1079                      module_name, static_cast<void*>(sb_bp.get()));
1080     }
1081 
1082     return sb_bp;
1083 }
1084 
1085 lldb::SBBreakpoint
1086 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1087                                                  const SBFileSpecList &module_list,
1088                                                  const lldb::SBFileSpecList &source_file_list)
1089 {
1090     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1091 
1092     SBBreakpoint sb_bp;
1093     TargetSP target_sp(GetSP());
1094     if (target_sp && source_regex && source_regex[0])
1095     {
1096         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1097         const bool hardware = false;
1098         RegularExpression regexp(source_regex);
1099         *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
1100     }
1101 
1102     if (log)
1103         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1104                      static_cast<void*>(target_sp.get()), source_regex,
1105                      static_cast<void*>(sb_bp.get()));
1106 
1107     return sb_bp;
1108 }
1109 
1110 lldb::SBBreakpoint
1111 SBTarget::BreakpointCreateForException  (lldb::LanguageType language,
1112                                          bool catch_bp,
1113                                          bool throw_bp)
1114 {
1115     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1116 
1117     SBBreakpoint sb_bp;
1118     TargetSP target_sp(GetSP());
1119     if (target_sp)
1120     {
1121         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1122         const bool hardware = false;
1123         *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
1124     }
1125 
1126     if (log)
1127         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1128                      static_cast<void*>(target_sp.get()),
1129                      LanguageRuntime::GetNameForLanguageType(language),
1130                      catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1131                      static_cast<void*>(sb_bp.get()));
1132 
1133     return sb_bp;
1134 }
1135 
1136 uint32_t
1137 SBTarget::GetNumBreakpoints () const
1138 {
1139     TargetSP target_sp(GetSP());
1140     if (target_sp)
1141     {
1142         // The breakpoint list is thread safe, no need to lock
1143         return target_sp->GetBreakpointList().GetSize();
1144     }
1145     return 0;
1146 }
1147 
1148 SBBreakpoint
1149 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1150 {
1151     SBBreakpoint sb_breakpoint;
1152     TargetSP target_sp(GetSP());
1153     if (target_sp)
1154     {
1155         // The breakpoint list is thread safe, no need to lock
1156         *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1157     }
1158     return sb_breakpoint;
1159 }
1160 
1161 bool
1162 SBTarget::BreakpointDelete (break_id_t bp_id)
1163 {
1164     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1165 
1166     bool result = false;
1167     TargetSP target_sp(GetSP());
1168     if (target_sp)
1169     {
1170         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1171         result = target_sp->RemoveBreakpointByID (bp_id);
1172     }
1173 
1174     if (log)
1175         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1176                      static_cast<void*>(target_sp.get()),
1177                      static_cast<uint32_t>(bp_id), result);
1178 
1179     return result;
1180 }
1181 
1182 SBBreakpoint
1183 SBTarget::FindBreakpointByID (break_id_t bp_id)
1184 {
1185     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1186 
1187     SBBreakpoint sb_breakpoint;
1188     TargetSP target_sp(GetSP());
1189     if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
1190     {
1191         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1192         *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
1193     }
1194 
1195     if (log)
1196         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1197                      static_cast<void*>(target_sp.get()),
1198                      static_cast<uint32_t>(bp_id),
1199                      static_cast<void*>(sb_breakpoint.get()));
1200 
1201     return sb_breakpoint;
1202 }
1203 
1204 bool
1205 SBTarget::EnableAllBreakpoints ()
1206 {
1207     TargetSP target_sp(GetSP());
1208     if (target_sp)
1209     {
1210         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1211         target_sp->EnableAllBreakpoints ();
1212         return true;
1213     }
1214     return false;
1215 }
1216 
1217 bool
1218 SBTarget::DisableAllBreakpoints ()
1219 {
1220     TargetSP target_sp(GetSP());
1221     if (target_sp)
1222     {
1223         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1224         target_sp->DisableAllBreakpoints ();
1225         return true;
1226     }
1227     return false;
1228 }
1229 
1230 bool
1231 SBTarget::DeleteAllBreakpoints ()
1232 {
1233     TargetSP target_sp(GetSP());
1234     if (target_sp)
1235     {
1236         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1237         target_sp->RemoveAllBreakpoints ();
1238         return true;
1239     }
1240     return false;
1241 }
1242 
1243 uint32_t
1244 SBTarget::GetNumWatchpoints () const
1245 {
1246     TargetSP target_sp(GetSP());
1247     if (target_sp)
1248     {
1249         // The watchpoint list is thread safe, no need to lock
1250         return target_sp->GetWatchpointList().GetSize();
1251     }
1252     return 0;
1253 }
1254 
1255 SBWatchpoint
1256 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
1257 {
1258     SBWatchpoint sb_watchpoint;
1259     TargetSP target_sp(GetSP());
1260     if (target_sp)
1261     {
1262         // The watchpoint list is thread safe, no need to lock
1263         sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
1264     }
1265     return sb_watchpoint;
1266 }
1267 
1268 bool
1269 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
1270 {
1271     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1272 
1273     bool result = false;
1274     TargetSP target_sp(GetSP());
1275     if (target_sp)
1276     {
1277         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1278         Mutex::Locker locker;
1279         target_sp->GetWatchpointList().GetListMutex(locker);
1280         result = target_sp->RemoveWatchpointByID (wp_id);
1281     }
1282 
1283     if (log)
1284         log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1285                      static_cast<void*>(target_sp.get()),
1286                      static_cast<uint32_t>(wp_id), result);
1287 
1288     return result;
1289 }
1290 
1291 SBWatchpoint
1292 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
1293 {
1294     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1295 
1296     SBWatchpoint sb_watchpoint;
1297     lldb::WatchpointSP watchpoint_sp;
1298     TargetSP target_sp(GetSP());
1299     if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
1300     {
1301         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1302         Mutex::Locker locker;
1303         target_sp->GetWatchpointList().GetListMutex(locker);
1304         watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1305         sb_watchpoint.SetSP (watchpoint_sp);
1306     }
1307 
1308     if (log)
1309         log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1310                      static_cast<void*>(target_sp.get()),
1311                      static_cast<uint32_t>(wp_id),
1312                      static_cast<void*>(watchpoint_sp.get()));
1313 
1314     return sb_watchpoint;
1315 }
1316 
1317 lldb::SBWatchpoint
1318 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
1319 {
1320     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1321 
1322     SBWatchpoint sb_watchpoint;
1323     lldb::WatchpointSP watchpoint_sp;
1324     TargetSP target_sp(GetSP());
1325     if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
1326     {
1327         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1328         uint32_t watch_type = 0;
1329         if (read)
1330             watch_type |= LLDB_WATCH_TYPE_READ;
1331         if (write)
1332             watch_type |= LLDB_WATCH_TYPE_WRITE;
1333         if (watch_type == 0)
1334         {
1335             error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1336             return sb_watchpoint;
1337         }
1338 
1339         // Target::CreateWatchpoint() is thread safe.
1340         Error cw_error;
1341         // This API doesn't take in a type, so we can't figure out what it is.
1342         ClangASTType *type = NULL;
1343         watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1344         error.SetError(cw_error);
1345         sb_watchpoint.SetSP (watchpoint_sp);
1346     }
1347 
1348     if (log)
1349         log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
1350                      static_cast<void*>(target_sp.get()), addr,
1351                      static_cast<uint32_t>(size),
1352                      static_cast<void*>(watchpoint_sp.get()));
1353 
1354     return sb_watchpoint;
1355 }
1356 
1357 bool
1358 SBTarget::EnableAllWatchpoints ()
1359 {
1360     TargetSP target_sp(GetSP());
1361     if (target_sp)
1362     {
1363         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1364         Mutex::Locker locker;
1365         target_sp->GetWatchpointList().GetListMutex(locker);
1366         target_sp->EnableAllWatchpoints ();
1367         return true;
1368     }
1369     return false;
1370 }
1371 
1372 bool
1373 SBTarget::DisableAllWatchpoints ()
1374 {
1375     TargetSP target_sp(GetSP());
1376     if (target_sp)
1377     {
1378         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1379         Mutex::Locker locker;
1380         target_sp->GetWatchpointList().GetListMutex(locker);
1381         target_sp->DisableAllWatchpoints ();
1382         return true;
1383     }
1384     return false;
1385 }
1386 
1387 SBValue
1388 SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1389 {
1390     SBValue sb_value;
1391     lldb::ValueObjectSP new_value_sp;
1392     if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1393     {
1394         lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1395         ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1396         ClangASTType ast_type(type.GetSP()->GetClangASTType(true));
1397         new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr, exe_ctx, ast_type);
1398     }
1399     sb_value.SetSP(new_value_sp);
1400     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1401     if (log)
1402     {
1403         if (new_value_sp)
1404             log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1405                          static_cast<void*>(m_opaque_sp.get()),
1406                          new_value_sp->GetName().AsCString());
1407         else
1408             log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL",
1409                          static_cast<void*>(m_opaque_sp.get()));
1410     }
1411     return sb_value;
1412 }
1413 
1414 lldb::SBValue
1415 SBTarget::CreateValueFromData (const char *name, lldb::SBData data, lldb::SBType type)
1416 {
1417     SBValue sb_value;
1418     lldb::ValueObjectSP new_value_sp;
1419     if (IsValid() && name && *name && data.IsValid() && type.IsValid())
1420     {
1421         DataExtractorSP extractor(*data);
1422         ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1423         ClangASTType ast_type(type.GetSP()->GetClangASTType(true));
1424         new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor, exe_ctx, ast_type);
1425     }
1426     sb_value.SetSP(new_value_sp);
1427     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1428     if (log)
1429     {
1430         if (new_value_sp)
1431             log->Printf ("SBTarget(%p)::CreateValueFromData => \"%s\"",
1432                          static_cast<void*>(m_opaque_sp.get()),
1433                          new_value_sp->GetName().AsCString());
1434         else
1435             log->Printf ("SBTarget(%p)::CreateValueFromData => NULL",
1436                          static_cast<void*>(m_opaque_sp.get()));
1437     }
1438     return sb_value;
1439 }
1440 
1441 lldb::SBValue
1442 SBTarget::CreateValueFromExpression (const char *name, const char* expr)
1443 {
1444     SBValue sb_value;
1445     lldb::ValueObjectSP new_value_sp;
1446     if (IsValid() && name && *name && expr && *expr)
1447     {
1448         ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1449         new_value_sp = ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1450     }
1451     sb_value.SetSP(new_value_sp);
1452     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1453     if (log)
1454     {
1455         if (new_value_sp)
1456             log->Printf ("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1457                          static_cast<void*>(m_opaque_sp.get()),
1458                          new_value_sp->GetName().AsCString());
1459         else
1460             log->Printf ("SBTarget(%p)::CreateValueFromExpression => NULL",
1461                          static_cast<void*>(m_opaque_sp.get()));
1462     }
1463     return sb_value;
1464 }
1465 
1466 bool
1467 SBTarget::DeleteAllWatchpoints ()
1468 {
1469     TargetSP target_sp(GetSP());
1470     if (target_sp)
1471     {
1472         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1473         Mutex::Locker locker;
1474         target_sp->GetWatchpointList().GetListMutex(locker);
1475         target_sp->RemoveAllWatchpoints ();
1476         return true;
1477     }
1478     return false;
1479 }
1480 
1481 
1482 lldb::SBModule
1483 SBTarget::AddModule (const char *path,
1484                      const char *triple,
1485                      const char *uuid_cstr)
1486 {
1487     return AddModule (path, triple, uuid_cstr, NULL);
1488 }
1489 
1490 lldb::SBModule
1491 SBTarget::AddModule (const char *path,
1492                      const char *triple,
1493                      const char *uuid_cstr,
1494                      const char *symfile)
1495 {
1496     lldb::SBModule sb_module;
1497     TargetSP target_sp(GetSP());
1498     if (target_sp)
1499     {
1500         ModuleSpec module_spec;
1501         if (path)
1502             module_spec.GetFileSpec().SetFile(path, false);
1503 
1504         if (uuid_cstr)
1505             module_spec.GetUUID().SetFromCString(uuid_cstr);
1506 
1507         if (triple)
1508             module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1509         else
1510             module_spec.GetArchitecture() = target_sp->GetArchitecture();
1511 
1512         if (symfile)
1513             module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
1514 
1515         sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1516     }
1517     return sb_module;
1518 }
1519 
1520 lldb::SBModule
1521 SBTarget::AddModule (const SBModuleSpec &module_spec)
1522 {
1523     lldb::SBModule sb_module;
1524     TargetSP target_sp(GetSP());
1525     if (target_sp)
1526         sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1527     return sb_module;
1528 }
1529 
1530 bool
1531 SBTarget::AddModule (lldb::SBModule &module)
1532 {
1533     TargetSP target_sp(GetSP());
1534     if (target_sp)
1535     {
1536         target_sp->GetImages().AppendIfNeeded (module.GetSP());
1537         return true;
1538     }
1539     return false;
1540 }
1541 
1542 uint32_t
1543 SBTarget::GetNumModules () const
1544 {
1545     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1546 
1547     uint32_t num = 0;
1548     TargetSP target_sp(GetSP());
1549     if (target_sp)
1550     {
1551         // The module list is thread safe, no need to lock
1552         num = target_sp->GetImages().GetSize();
1553     }
1554 
1555     if (log)
1556         log->Printf ("SBTarget(%p)::GetNumModules () => %d",
1557                      static_cast<void*>(target_sp.get()), num);
1558 
1559     return num;
1560 }
1561 
1562 void
1563 SBTarget::Clear ()
1564 {
1565     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1566 
1567     if (log)
1568         log->Printf ("SBTarget(%p)::Clear ()",
1569                      static_cast<void*>(m_opaque_sp.get()));
1570 
1571     m_opaque_sp.reset();
1572 }
1573 
1574 
1575 SBModule
1576 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1577 {
1578     SBModule sb_module;
1579     TargetSP target_sp(GetSP());
1580     if (target_sp && sb_file_spec.IsValid())
1581     {
1582         ModuleSpec module_spec(*sb_file_spec);
1583         // The module list is thread safe, no need to lock
1584         sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
1585     }
1586     return sb_module;
1587 }
1588 
1589 lldb::ByteOrder
1590 SBTarget::GetByteOrder ()
1591 {
1592     TargetSP target_sp(GetSP());
1593     if (target_sp)
1594         return target_sp->GetArchitecture().GetByteOrder();
1595     return eByteOrderInvalid;
1596 }
1597 
1598 const char *
1599 SBTarget::GetTriple ()
1600 {
1601     TargetSP target_sp(GetSP());
1602     if (target_sp)
1603     {
1604         std::string triple (target_sp->GetArchitecture().GetTriple().str());
1605         // Unique the string so we don't run into ownership issues since
1606         // the const strings put the string into the string pool once and
1607         // the strings never comes out
1608         ConstString const_triple (triple.c_str());
1609         return const_triple.GetCString();
1610     }
1611     return NULL;
1612 }
1613 
1614 uint32_t
1615 SBTarget::GetDataByteSize ()
1616 {
1617     TargetSP target_sp(GetSP());
1618     if (target_sp)
1619     {
1620         return target_sp->GetArchitecture().GetDataByteSize() ;
1621     }
1622     return 0;
1623 }
1624 
1625 uint32_t
1626 SBTarget::GetCodeByteSize ()
1627 {
1628     TargetSP target_sp(GetSP());
1629     if (target_sp)
1630     {
1631         return target_sp->GetArchitecture().GetCodeByteSize() ;
1632     }
1633     return 0;
1634 }
1635 
1636 uint32_t
1637 SBTarget::GetAddressByteSize()
1638 {
1639     TargetSP target_sp(GetSP());
1640     if (target_sp)
1641         return target_sp->GetArchitecture().GetAddressByteSize();
1642     return sizeof(void*);
1643 }
1644 
1645 
1646 SBModule
1647 SBTarget::GetModuleAtIndex (uint32_t idx)
1648 {
1649     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1650 
1651     SBModule sb_module;
1652     ModuleSP module_sp;
1653     TargetSP target_sp(GetSP());
1654     if (target_sp)
1655     {
1656         // The module list is thread safe, no need to lock
1657         module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1658         sb_module.SetSP (module_sp);
1659     }
1660 
1661     if (log)
1662         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1663                      static_cast<void*>(target_sp.get()), idx,
1664                      static_cast<void*>(module_sp.get()));
1665 
1666     return sb_module;
1667 }
1668 
1669 bool
1670 SBTarget::RemoveModule (lldb::SBModule module)
1671 {
1672     TargetSP target_sp(GetSP());
1673     if (target_sp)
1674         return target_sp->GetImages().Remove(module.GetSP());
1675     return false;
1676 }
1677 
1678 
1679 SBBroadcaster
1680 SBTarget::GetBroadcaster () const
1681 {
1682     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1683 
1684     TargetSP target_sp(GetSP());
1685     SBBroadcaster broadcaster(target_sp.get(), false);
1686 
1687     if (log)
1688         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1689                      static_cast<void*>(target_sp.get()),
1690                      static_cast<void*>(broadcaster.get()));
1691 
1692     return broadcaster;
1693 }
1694 
1695 bool
1696 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
1697 {
1698     Stream &strm = description.ref();
1699 
1700     TargetSP target_sp(GetSP());
1701     if (target_sp)
1702     {
1703         target_sp->Dump (&strm, description_level);
1704     }
1705     else
1706         strm.PutCString ("No value");
1707 
1708     return true;
1709 }
1710 
1711 lldb::SBSymbolContextList
1712 SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
1713 {
1714     lldb::SBSymbolContextList sb_sc_list;
1715     if (name && name[0])
1716     {
1717         TargetSP target_sp(GetSP());
1718         if (target_sp)
1719         {
1720             const bool symbols_ok = true;
1721             const bool inlines_ok = true;
1722             const bool append = true;
1723             target_sp->GetImages().FindFunctions (ConstString(name),
1724                                                   name_type_mask,
1725                                                   symbols_ok,
1726                                                   inlines_ok,
1727                                                   append,
1728                                                   *sb_sc_list);
1729         }
1730     }
1731     return sb_sc_list;
1732 }
1733 
1734 lldb::SBSymbolContextList
1735 SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype)
1736 {
1737     lldb::SBSymbolContextList sb_sc_list;
1738     if (name && name[0])
1739     {
1740         TargetSP target_sp(GetSP());
1741         if (target_sp)
1742         {
1743             std::string regexstr;
1744             switch (matchtype)
1745             {
1746             case eMatchTypeRegex:
1747                 target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list);
1748                 break;
1749             case eMatchTypeStartsWith:
1750                 regexstr = llvm::Regex::escape(name) + ".*";
1751                 target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list);
1752                 break;
1753             default:
1754                 target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list);
1755                 break;
1756             }
1757         }
1758     }
1759     return sb_sc_list;
1760 }
1761 
1762 lldb::SBType
1763 SBTarget::FindFirstType (const char* typename_cstr)
1764 {
1765     TargetSP target_sp(GetSP());
1766     if (typename_cstr && typename_cstr[0] && target_sp)
1767     {
1768         ConstString const_typename(typename_cstr);
1769         SymbolContext sc;
1770         const bool exact_match = false;
1771 
1772         const ModuleList &module_list = target_sp->GetImages();
1773         size_t count = module_list.GetSize();
1774         for (size_t idx = 0; idx < count; idx++)
1775         {
1776             ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
1777             if (module_sp)
1778             {
1779                 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
1780                 if (type_sp)
1781                     return SBType(type_sp);
1782             }
1783         }
1784 
1785         // Didn't find the type in the symbols; try the Objective-C runtime
1786         // if one is installed
1787 
1788         ProcessSP process_sp(target_sp->GetProcessSP());
1789 
1790         if (process_sp)
1791         {
1792             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1793 
1794             if (objc_language_runtime)
1795             {
1796                 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1797 
1798                 if (objc_decl_vendor)
1799                 {
1800                     std::vector <clang::NamedDecl *> decls;
1801 
1802                     if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
1803                     {
1804                         if (ClangASTType type = ClangASTContext::GetTypeForDecl(decls[0]))
1805                         {
1806                             return SBType(type);
1807                         }
1808                     }
1809                 }
1810             }
1811         }
1812 
1813         // No matches, search for basic typename matches
1814         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1815         if (clang_ast)
1816             return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
1817     }
1818     return SBType();
1819 }
1820 
1821 SBType
1822 SBTarget::GetBasicType(lldb::BasicType type)
1823 {
1824     TargetSP target_sp(GetSP());
1825     if (target_sp)
1826     {
1827         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1828         if (clang_ast)
1829             return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
1830     }
1831     return SBType();
1832 }
1833 
1834 
1835 lldb::SBTypeList
1836 SBTarget::FindTypes (const char* typename_cstr)
1837 {
1838     SBTypeList sb_type_list;
1839     TargetSP target_sp(GetSP());
1840     if (typename_cstr && typename_cstr[0] && target_sp)
1841     {
1842         ModuleList& images = target_sp->GetImages();
1843         ConstString const_typename(typename_cstr);
1844         bool exact_match = false;
1845         SymbolContext sc;
1846         TypeList type_list;
1847 
1848         uint32_t num_matches = images.FindTypes (sc,
1849                                                  const_typename,
1850                                                  exact_match,
1851                                                  UINT32_MAX,
1852                                                  type_list);
1853 
1854         if (num_matches > 0)
1855         {
1856             for (size_t idx = 0; idx < num_matches; idx++)
1857             {
1858                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1859                 if (type_sp)
1860                     sb_type_list.Append(SBType(type_sp));
1861             }
1862         }
1863 
1864         // Try the Objective-C runtime if one is installed
1865 
1866         ProcessSP process_sp(target_sp->GetProcessSP());
1867 
1868         if (process_sp)
1869         {
1870             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1871 
1872             if (objc_language_runtime)
1873             {
1874                 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1875 
1876                 if (objc_decl_vendor)
1877                 {
1878                     std::vector <clang::NamedDecl *> decls;
1879 
1880                     if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
1881                     {
1882                         for (clang::NamedDecl *decl : decls)
1883                         {
1884                             if (ClangASTType type = ClangASTContext::GetTypeForDecl(decl))
1885                             {
1886                                 sb_type_list.Append(SBType(type));
1887                             }
1888                         }
1889                     }
1890                 }
1891             }
1892         }
1893 
1894         if (sb_type_list.GetSize() == 0)
1895         {
1896             // No matches, search for basic typename matches
1897             ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1898             if (clang_ast)
1899                 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
1900         }
1901     }
1902     return sb_type_list;
1903 }
1904 
1905 SBValueList
1906 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1907 {
1908     SBValueList sb_value_list;
1909 
1910     TargetSP target_sp(GetSP());
1911     if (name && target_sp)
1912     {
1913         VariableList variable_list;
1914         const bool append = true;
1915         const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
1916                                                                                  append,
1917                                                                                  max_matches,
1918                                                                                  variable_list);
1919 
1920         if (match_count > 0)
1921         {
1922             ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1923             if (exe_scope == NULL)
1924                 exe_scope = target_sp.get();
1925             for (uint32_t i=0; i<match_count; ++i)
1926             {
1927                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
1928                 if (valobj_sp)
1929                     sb_value_list.Append(SBValue(valobj_sp));
1930             }
1931         }
1932     }
1933 
1934     return sb_value_list;
1935 }
1936 
1937 SBValueList
1938 SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype)
1939 {
1940     SBValueList sb_value_list;
1941 
1942     TargetSP target_sp(GetSP());
1943     if (name && target_sp)
1944     {
1945         VariableList variable_list;
1946         const bool append = true;
1947 
1948         std::string regexstr;
1949         uint32_t match_count;
1950         switch (matchtype)
1951         {
1952         case eMatchTypeNormal:
1953             match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name),
1954                 append,
1955                 max_matches,
1956                 variable_list);
1957             break;
1958         case eMatchTypeRegex:
1959             match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name),
1960                 append,
1961                 max_matches,
1962                 variable_list);
1963             break;
1964         case eMatchTypeStartsWith:
1965             regexstr = llvm::Regex::escape(name) + ".*";
1966             match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()),
1967                 append,
1968                 max_matches,
1969                 variable_list);
1970             break;
1971         }
1972 
1973 
1974         if (match_count > 0)
1975         {
1976             ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1977             if (exe_scope == NULL)
1978                 exe_scope = target_sp.get();
1979             for (uint32_t i = 0; i<match_count; ++i)
1980             {
1981                 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(exe_scope, variable_list.GetVariableAtIndex(i)));
1982                 if (valobj_sp)
1983                     sb_value_list.Append(SBValue(valobj_sp));
1984             }
1985         }
1986     }
1987 
1988     return sb_value_list;
1989 }
1990 
1991 
1992 lldb::SBValue
1993 SBTarget::FindFirstGlobalVariable (const char* name)
1994 {
1995     SBValueList sb_value_list(FindGlobalVariables(name, 1));
1996     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
1997         return sb_value_list.GetValueAtIndex(0);
1998     return SBValue();
1999 }
2000 
2001 SBSourceManager
2002 SBTarget::GetSourceManager()
2003 {
2004     SBSourceManager source_manager (*this);
2005     return source_manager;
2006 }
2007 
2008 lldb::SBInstructionList
2009 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2010 {
2011     return ReadInstructions (base_addr, count, NULL);
2012 }
2013 
2014 lldb::SBInstructionList
2015 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2016 {
2017     SBInstructionList sb_instructions;
2018 
2019     TargetSP target_sp(GetSP());
2020     if (target_sp)
2021     {
2022         Address *addr_ptr = base_addr.get();
2023 
2024         if (addr_ptr)
2025         {
2026             DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2027             bool prefer_file_cache = false;
2028             lldb_private::Error error;
2029             lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2030             const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2031                                                             prefer_file_cache,
2032                                                             data.GetBytes(),
2033                                                             data.GetByteSize(),
2034                                                             error,
2035                                                             &load_addr);
2036             const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2037             sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2038                                                                              NULL,
2039                                                                              flavor_string,
2040                                                                              *addr_ptr,
2041                                                                              data.GetBytes(),
2042                                                                              bytes_read,
2043                                                                              count,
2044                                                                              data_from_file));
2045         }
2046     }
2047 
2048     return sb_instructions;
2049 
2050 }
2051 
2052 lldb::SBInstructionList
2053 SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2054 {
2055     return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2056 }
2057 
2058 lldb::SBInstructionList
2059 SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2060 {
2061     SBInstructionList sb_instructions;
2062 
2063     TargetSP target_sp(GetSP());
2064     if (target_sp)
2065     {
2066         Address addr;
2067 
2068         if (base_addr.get())
2069             addr = *base_addr.get();
2070 
2071         const bool data_from_file = true;
2072 
2073         sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2074                                                                          NULL,
2075                                                                          flavor_string,
2076                                                                          addr,
2077                                                                          buf,
2078                                                                          size,
2079                                                                          UINT32_MAX,
2080                                                                          data_from_file));
2081     }
2082 
2083     return sb_instructions;
2084 }
2085 
2086 lldb::SBInstructionList
2087 SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2088 {
2089     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2090 }
2091 
2092 lldb::SBInstructionList
2093 SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2094 {
2095     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
2096 }
2097 
2098 SBError
2099 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2100                                  lldb::addr_t section_base_addr)
2101 {
2102     SBError sb_error;
2103     TargetSP target_sp(GetSP());
2104     if (target_sp)
2105     {
2106         if (!section.IsValid())
2107         {
2108             sb_error.SetErrorStringWithFormat ("invalid section");
2109         }
2110         else
2111         {
2112             SectionSP section_sp (section.GetSP());
2113             if (section_sp)
2114             {
2115                 if (section_sp->IsThreadSpecific())
2116                 {
2117                     sb_error.SetErrorString ("thread specific sections are not yet supported");
2118                 }
2119                 else
2120                 {
2121                     ProcessSP process_sp (target_sp->GetProcessSP());
2122                     if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
2123                     {
2124                         // Flush info in the process (stack frames, etc)
2125                         if (process_sp)
2126                             process_sp->Flush();
2127                     }
2128                 }
2129             }
2130         }
2131     }
2132     else
2133     {
2134         sb_error.SetErrorString ("invalid target");
2135     }
2136     return sb_error;
2137 }
2138 
2139 SBError
2140 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2141 {
2142     SBError sb_error;
2143 
2144     TargetSP target_sp(GetSP());
2145     if (target_sp)
2146     {
2147         if (!section.IsValid())
2148         {
2149             sb_error.SetErrorStringWithFormat ("invalid section");
2150         }
2151         else
2152         {
2153             ProcessSP process_sp (target_sp->GetProcessSP());
2154             if (target_sp->SetSectionUnloaded (section.GetSP()))
2155             {
2156                 // Flush info in the process (stack frames, etc)
2157                 if (process_sp)
2158                     process_sp->Flush();
2159             }
2160         }
2161     }
2162     else
2163     {
2164         sb_error.SetErrorStringWithFormat ("invalid target");
2165     }
2166     return sb_error;
2167 }
2168 
2169 SBError
2170 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2171 {
2172     SBError sb_error;
2173 
2174     TargetSP target_sp(GetSP());
2175     if (target_sp)
2176     {
2177         ModuleSP module_sp (module.GetSP());
2178         if (module_sp)
2179         {
2180             bool changed = false;
2181             if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
2182             {
2183                 // The load was successful, make sure that at least some sections
2184                 // changed before we notify that our module was loaded.
2185                 if (changed)
2186                 {
2187                     ModuleList module_list;
2188                     module_list.Append(module_sp);
2189                     target_sp->ModulesDidLoad (module_list);
2190                     // Flush info in the process (stack frames, etc)
2191                     ProcessSP process_sp (target_sp->GetProcessSP());
2192                     if (process_sp)
2193                         process_sp->Flush();
2194                 }
2195             }
2196         }
2197         else
2198         {
2199             sb_error.SetErrorStringWithFormat ("invalid module");
2200         }
2201 
2202     }
2203     else
2204     {
2205         sb_error.SetErrorStringWithFormat ("invalid target");
2206     }
2207     return sb_error;
2208 }
2209 
2210 SBError
2211 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2212 {
2213     SBError sb_error;
2214 
2215     char path[PATH_MAX];
2216     TargetSP target_sp(GetSP());
2217     if (target_sp)
2218     {
2219         ModuleSP module_sp (module.GetSP());
2220         if (module_sp)
2221         {
2222             ObjectFile *objfile = module_sp->GetObjectFile();
2223             if (objfile)
2224             {
2225                 SectionList *section_list = objfile->GetSectionList();
2226                 if (section_list)
2227                 {
2228                     ProcessSP process_sp (target_sp->GetProcessSP());
2229 
2230                     bool changed = false;
2231                     const size_t num_sections = section_list->GetSize();
2232                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2233                     {
2234                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2235                         if (section_sp)
2236                             changed |= target_sp->SetSectionUnloaded (section_sp);
2237                     }
2238                     if (changed)
2239                     {
2240                         // Flush info in the process (stack frames, etc)
2241                         ProcessSP process_sp (target_sp->GetProcessSP());
2242                         if (process_sp)
2243                             process_sp->Flush();
2244                     }
2245                 }
2246                 else
2247                 {
2248                     module_sp->GetFileSpec().GetPath (path, sizeof(path));
2249                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2250                 }
2251             }
2252             else
2253             {
2254                 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2255                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2256             }
2257         }
2258         else
2259         {
2260             sb_error.SetErrorStringWithFormat ("invalid module");
2261         }
2262     }
2263     else
2264     {
2265         sb_error.SetErrorStringWithFormat ("invalid target");
2266     }
2267     return sb_error;
2268 }
2269 
2270 
2271 lldb::SBSymbolContextList
2272 SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2273 {
2274     SBSymbolContextList sb_sc_list;
2275     if (name && name[0])
2276     {
2277         TargetSP target_sp(GetSP());
2278         if (target_sp)
2279         {
2280             bool append = true;
2281             target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2282                                                                symbol_type,
2283                                                                *sb_sc_list,
2284                                                                append);
2285         }
2286     }
2287     return sb_sc_list;
2288 
2289 }
2290 
2291 
2292 lldb::SBValue
2293 SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2294 {
2295     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2296     Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2297     SBValue expr_result;
2298     ExpressionResults exe_results = eExpressionSetupError;
2299     ValueObjectSP expr_value_sp;
2300     TargetSP target_sp(GetSP());
2301     StackFrame *frame = NULL;
2302     if (target_sp)
2303     {
2304         if (expr == NULL || expr[0] == '\0')
2305         {
2306             if (log)
2307                 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2308             return expr_result;
2309         }
2310 
2311         Mutex::Locker api_locker (target_sp->GetAPIMutex());
2312         ExecutionContext exe_ctx (m_opaque_sp.get());
2313 
2314         if (log)
2315             log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2316 
2317         frame = exe_ctx.GetFramePtr();
2318         Target *target = exe_ctx.GetTargetPtr();
2319 
2320         if (target)
2321         {
2322 #ifdef LLDB_CONFIGURATION_DEBUG
2323             StreamString frame_description;
2324             if (frame)
2325                 frame->DumpUsingSettingsFormat (&frame_description);
2326             Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2327                                                  expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2328 #endif
2329             exe_results = target->EvaluateExpression (expr,
2330                                                       frame,
2331                                                       expr_value_sp,
2332                                                       options.ref());
2333 
2334             expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2335 #ifdef LLDB_CONFIGURATION_DEBUG
2336             Host::SetCrashDescription (NULL);
2337 #endif
2338         }
2339         else
2340         {
2341             if (log)
2342                 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2343         }
2344     }
2345 #ifndef LLDB_DISABLE_PYTHON
2346     if (expr_log)
2347         expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2348                          expr_result.GetValue(), expr_result.GetSummary());
2349 
2350     if (log)
2351         log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2352                      static_cast<void*>(frame), expr,
2353                      static_cast<void*>(expr_value_sp.get()), exe_results);
2354 #endif
2355 
2356     return expr_result;
2357 }
2358 
2359 
2360 lldb::addr_t
2361 SBTarget::GetStackRedZoneSize()
2362 {
2363     TargetSP target_sp(GetSP());
2364     if (target_sp)
2365     {
2366         ABISP abi_sp;
2367         ProcessSP process_sp (target_sp->GetProcessSP());
2368         if (process_sp)
2369             abi_sp = process_sp->GetABI();
2370         else
2371             abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2372         if (abi_sp)
2373             return abi_sp->GetRedZoneSize();
2374     }
2375     return 0;
2376 }
2377 
2378 lldb::SBLaunchInfo
2379 SBTarget::GetLaunchInfo () const
2380 {
2381     lldb::SBLaunchInfo launch_info(NULL);
2382     TargetSP target_sp(GetSP());
2383     if (target_sp)
2384         launch_info.ref() = m_opaque_sp->GetProcessLaunchInfo();
2385     return launch_info;
2386 }
2387 
2388 void
2389 SBTarget::SetLaunchInfo (const lldb::SBLaunchInfo &launch_info)
2390 {
2391     TargetSP target_sp(GetSP());
2392     if (target_sp)
2393         m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
2394 }
2395