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