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