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 (error.Success())
1118             {
1119                 // If we are doing synchronous mode, then wait for the
1120                 // process to stop!
1121                 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1122                     process_sp->WaitForProcessToStop (NULL);
1123             }
1124         }
1125         else
1126         {
1127             error.SetErrorString ("unable to create lldb_private::Process");
1128         }
1129     }
1130     else
1131     {
1132         error.SetErrorString ("SBTarget is invalid");
1133     }
1134 
1135     if (log)
1136     {
1137         log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
1138                      target_sp.get(), process_sp.get());
1139     }
1140     return sb_process;
1141 }
1142 
1143 lldb::SBProcess
1144 SBTarget::ConnectRemote
1145 (
1146     SBListener &listener,
1147     const char *url,
1148     const char *plugin_name,
1149     SBError& error
1150 )
1151 {
1152     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1153 
1154     SBProcess sb_process;
1155     ProcessSP process_sp;
1156     TargetSP target_sp(GetSP());
1157 
1158     if (log)
1159     {
1160         log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...", target_sp.get(), url, plugin_name);
1161     }
1162 
1163     if (target_sp)
1164     {
1165         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1166         if (listener.IsValid())
1167             process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
1168         else
1169             process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
1170 
1171 
1172         if (process_sp)
1173         {
1174             sb_process.SetSP (process_sp);
1175             error.SetError (process_sp->ConnectRemote (NULL, url));
1176         }
1177         else
1178         {
1179             error.SetErrorString ("unable to create lldb_private::Process");
1180         }
1181     }
1182     else
1183     {
1184         error.SetErrorString ("SBTarget is invalid");
1185     }
1186 
1187     if (log)
1188     {
1189         log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
1190                      target_sp.get(), process_sp.get());
1191     }
1192     return sb_process;
1193 }
1194 
1195 SBFileSpec
1196 SBTarget::GetExecutable ()
1197 {
1198 
1199     SBFileSpec exe_file_spec;
1200     TargetSP target_sp(GetSP());
1201     if (target_sp)
1202     {
1203         Module *exe_module = target_sp->GetExecutableModulePointer();
1204         if (exe_module)
1205             exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
1206     }
1207 
1208     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1209     if (log)
1210     {
1211         log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
1212                      target_sp.get(), exe_file_spec.get());
1213     }
1214 
1215     return exe_file_spec;
1216 }
1217 
1218 bool
1219 SBTarget::operator == (const SBTarget &rhs) const
1220 {
1221     return m_opaque_sp.get() == rhs.m_opaque_sp.get();
1222 }
1223 
1224 bool
1225 SBTarget::operator != (const SBTarget &rhs) const
1226 {
1227     return m_opaque_sp.get() != rhs.m_opaque_sp.get();
1228 }
1229 
1230 lldb::TargetSP
1231 SBTarget::GetSP () const
1232 {
1233     return m_opaque_sp;
1234 }
1235 
1236 void
1237 SBTarget::SetSP (const lldb::TargetSP& target_sp)
1238 {
1239     m_opaque_sp = target_sp;
1240 }
1241 
1242 lldb::SBAddress
1243 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
1244 {
1245     lldb::SBAddress sb_addr;
1246     Address &addr = sb_addr.ref();
1247     TargetSP target_sp(GetSP());
1248     if (target_sp)
1249     {
1250         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1251         if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
1252             return sb_addr;
1253     }
1254 
1255     // We have a load address that isn't in a section, just return an address
1256     // with the offset filled in (the address) and the section set to NULL
1257     addr.SetRawAddress(vm_addr);
1258     return sb_addr;
1259 }
1260 
1261 SBSymbolContext
1262 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
1263 {
1264     SBSymbolContext sc;
1265     if (addr.IsValid())
1266     {
1267         TargetSP target_sp(GetSP());
1268         if (target_sp)
1269             target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1270     }
1271     return sc;
1272 }
1273 
1274 
1275 SBBreakpoint
1276 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
1277 {
1278     return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
1279 }
1280 
1281 SBBreakpoint
1282 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
1283 {
1284     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1285 
1286     SBBreakpoint sb_bp;
1287     TargetSP target_sp(GetSP());
1288     if (target_sp && line != 0)
1289     {
1290         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1291 
1292         const LazyBool check_inlines = eLazyBoolCalculate;
1293         const LazyBool skip_prologue = eLazyBoolCalculate;
1294         const bool internal = false;
1295         *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal);
1296     }
1297 
1298     if (log)
1299     {
1300         SBStream sstr;
1301         sb_bp.GetDescription (sstr);
1302         char path[PATH_MAX];
1303         sb_file_spec->GetPath (path, sizeof(path));
1304         log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
1305                      target_sp.get(),
1306                      path,
1307                      line,
1308                      sb_bp.get(),
1309                      sstr.GetData());
1310     }
1311 
1312     return sb_bp;
1313 }
1314 
1315 SBBreakpoint
1316 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
1317 {
1318     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1319 
1320     SBBreakpoint sb_bp;
1321     TargetSP target_sp(GetSP());
1322     if (target_sp.get())
1323     {
1324         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1325 
1326         const bool internal = false;
1327         const LazyBool skip_prologue = eLazyBoolCalculate;
1328         if (module_name && module_name[0])
1329         {
1330             FileSpecList module_spec_list;
1331             module_spec_list.Append (FileSpec (module_name, false));
1332             *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
1333         }
1334         else
1335         {
1336             *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
1337         }
1338     }
1339 
1340     if (log)
1341     {
1342         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
1343                      target_sp.get(), symbol_name, module_name, sb_bp.get());
1344     }
1345 
1346     return sb_bp;
1347 }
1348 
1349 lldb::SBBreakpoint
1350 SBTarget::BreakpointCreateByName (const char *symbol_name,
1351                             const SBFileSpecList &module_list,
1352                             const SBFileSpecList &comp_unit_list)
1353 {
1354     uint32_t name_type_mask = eFunctionNameTypeAuto;
1355     return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1356 }
1357 
1358 lldb::SBBreakpoint
1359 SBTarget::BreakpointCreateByName (const char *symbol_name,
1360                             uint32_t name_type_mask,
1361                             const SBFileSpecList &module_list,
1362                             const SBFileSpecList &comp_unit_list)
1363 {
1364     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1365 
1366     SBBreakpoint sb_bp;
1367     TargetSP target_sp(GetSP());
1368     if (target_sp && symbol_name && symbol_name[0])
1369     {
1370         const bool internal = false;
1371         const LazyBool skip_prologue = eLazyBoolCalculate;
1372         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1373         *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1374                                                 comp_unit_list.get(),
1375                                                 symbol_name,
1376                                                 name_type_mask,
1377                                                 skip_prologue,
1378                                                 internal);
1379     }
1380 
1381     if (log)
1382     {
1383         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
1384                      target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
1385     }
1386 
1387     return sb_bp;
1388 }
1389 
1390 lldb::SBBreakpoint
1391 SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1392                                    uint32_t num_names,
1393                                    uint32_t name_type_mask,
1394                                    const SBFileSpecList &module_list,
1395                                    const SBFileSpecList &comp_unit_list)
1396 {
1397     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1398 
1399     SBBreakpoint sb_bp;
1400     TargetSP target_sp(GetSP());
1401     if (target_sp && num_names > 0)
1402     {
1403         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1404         const bool internal = false;
1405         const LazyBool skip_prologue = eLazyBoolCalculate;
1406         *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1407                                                 comp_unit_list.get(),
1408                                                 symbol_names,
1409                                                 num_names,
1410                                                 name_type_mask,
1411                                                 skip_prologue,
1412                                                 internal);
1413     }
1414 
1415     if (log)
1416     {
1417         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1418         for (uint32_t i = 0 ; i < num_names; i++)
1419         {
1420             char sep;
1421             if (i < num_names - 1)
1422                 sep = ',';
1423             else
1424                 sep = '}';
1425             if (symbol_names[i] != NULL)
1426                 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1427             else
1428                 log->Printf ("\"<NULL>\"%c ", sep);
1429 
1430         }
1431         log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1432     }
1433 
1434     return sb_bp;
1435 }
1436 
1437 SBBreakpoint
1438 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
1439 {
1440     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1441 
1442     SBBreakpoint sb_bp;
1443     TargetSP target_sp(GetSP());
1444     if (target_sp && symbol_name_regex && symbol_name_regex[0])
1445     {
1446         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1447         RegularExpression regexp(symbol_name_regex);
1448         const bool internal = false;
1449         const LazyBool skip_prologue = eLazyBoolCalculate;
1450 
1451         if (module_name && module_name[0])
1452         {
1453             FileSpecList module_spec_list;
1454             module_spec_list.Append (FileSpec (module_name, false));
1455 
1456             *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal);
1457         }
1458         else
1459         {
1460             *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal);
1461         }
1462     }
1463 
1464     if (log)
1465     {
1466         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1467                      target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
1468     }
1469 
1470     return sb_bp;
1471 }
1472 
1473 lldb::SBBreakpoint
1474 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1475                          const SBFileSpecList &module_list,
1476                          const SBFileSpecList &comp_unit_list)
1477 {
1478     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1479 
1480     SBBreakpoint sb_bp;
1481     TargetSP target_sp(GetSP());
1482     if (target_sp && symbol_name_regex && symbol_name_regex[0])
1483     {
1484         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1485         RegularExpression regexp(symbol_name_regex);
1486         const bool internal = false;
1487         const LazyBool skip_prologue = eLazyBoolCalculate;
1488 
1489         *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal);
1490     }
1491 
1492     if (log)
1493     {
1494         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1495                      target_sp.get(), symbol_name_regex, sb_bp.get());
1496     }
1497 
1498     return sb_bp;
1499 }
1500 
1501 SBBreakpoint
1502 SBTarget::BreakpointCreateByAddress (addr_t address)
1503 {
1504     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1505 
1506     SBBreakpoint sb_bp;
1507     TargetSP target_sp(GetSP());
1508     if (target_sp)
1509     {
1510         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1511         *sb_bp = target_sp->CreateBreakpoint (address, false);
1512     }
1513 
1514     if (log)
1515     {
1516         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)", target_sp.get(), (uint64_t) address, sb_bp.get());
1517     }
1518 
1519     return sb_bp;
1520 }
1521 
1522 lldb::SBBreakpoint
1523 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
1524 {
1525     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1526 
1527     SBBreakpoint sb_bp;
1528     TargetSP target_sp(GetSP());
1529     if (target_sp && source_regex && source_regex[0])
1530     {
1531         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1532         RegularExpression regexp(source_regex);
1533         FileSpecList source_file_spec_list;
1534         source_file_spec_list.Append (source_file.ref());
1535 
1536         if (module_name && module_name[0])
1537         {
1538             FileSpecList module_spec_list;
1539             module_spec_list.Append (FileSpec (module_name, false));
1540 
1541             *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
1542         }
1543         else
1544         {
1545             *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
1546         }
1547     }
1548 
1549     if (log)
1550     {
1551         char path[PATH_MAX];
1552         source_file->GetPath (path, sizeof(path));
1553         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1554                      target_sp.get(), source_regex, path, module_name, sb_bp.get());
1555     }
1556 
1557     return sb_bp;
1558 }
1559 
1560 lldb::SBBreakpoint
1561 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1562                                const SBFileSpecList &module_list,
1563                                const lldb::SBFileSpecList &source_file_list)
1564 {
1565     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1566 
1567     SBBreakpoint sb_bp;
1568     TargetSP target_sp(GetSP());
1569     if (target_sp && source_regex && source_regex[0])
1570     {
1571         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1572         RegularExpression regexp(source_regex);
1573         *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
1574     }
1575 
1576     if (log)
1577     {
1578         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1579                      target_sp.get(), source_regex, sb_bp.get());
1580     }
1581 
1582     return sb_bp;
1583 }
1584 
1585 lldb::SBBreakpoint
1586 SBTarget::BreakpointCreateForException  (lldb::LanguageType language,
1587                                bool catch_bp,
1588                                bool throw_bp)
1589 {
1590     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1591 
1592     SBBreakpoint sb_bp;
1593     TargetSP target_sp(GetSP());
1594     if (target_sp)
1595     {
1596         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1597         *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp);
1598     }
1599 
1600     if (log)
1601     {
1602         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1603                      target_sp.get(),
1604                      LanguageRuntime::GetNameForLanguageType(language),
1605                      catch_bp ? "on" : "off",
1606                      throw_bp ? "on" : "off",
1607                      sb_bp.get());
1608     }
1609 
1610     return sb_bp;
1611 }
1612 
1613 uint32_t
1614 SBTarget::GetNumBreakpoints () const
1615 {
1616     TargetSP target_sp(GetSP());
1617     if (target_sp)
1618     {
1619         // The breakpoint list is thread safe, no need to lock
1620         return target_sp->GetBreakpointList().GetSize();
1621     }
1622     return 0;
1623 }
1624 
1625 SBBreakpoint
1626 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1627 {
1628     SBBreakpoint sb_breakpoint;
1629     TargetSP target_sp(GetSP());
1630     if (target_sp)
1631     {
1632         // The breakpoint list is thread safe, no need to lock
1633         *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1634     }
1635     return sb_breakpoint;
1636 }
1637 
1638 bool
1639 SBTarget::BreakpointDelete (break_id_t bp_id)
1640 {
1641     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1642 
1643     bool result = false;
1644     TargetSP target_sp(GetSP());
1645     if (target_sp)
1646     {
1647         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1648         result = target_sp->RemoveBreakpointByID (bp_id);
1649     }
1650 
1651     if (log)
1652     {
1653         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
1654     }
1655 
1656     return result;
1657 }
1658 
1659 SBBreakpoint
1660 SBTarget::FindBreakpointByID (break_id_t bp_id)
1661 {
1662     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1663 
1664     SBBreakpoint sb_breakpoint;
1665     TargetSP target_sp(GetSP());
1666     if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
1667     {
1668         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1669         *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
1670     }
1671 
1672     if (log)
1673     {
1674         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1675                      target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
1676     }
1677 
1678     return sb_breakpoint;
1679 }
1680 
1681 bool
1682 SBTarget::EnableAllBreakpoints ()
1683 {
1684     TargetSP target_sp(GetSP());
1685     if (target_sp)
1686     {
1687         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1688         target_sp->EnableAllBreakpoints ();
1689         return true;
1690     }
1691     return false;
1692 }
1693 
1694 bool
1695 SBTarget::DisableAllBreakpoints ()
1696 {
1697     TargetSP target_sp(GetSP());
1698     if (target_sp)
1699     {
1700         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1701         target_sp->DisableAllBreakpoints ();
1702         return true;
1703     }
1704     return false;
1705 }
1706 
1707 bool
1708 SBTarget::DeleteAllBreakpoints ()
1709 {
1710     TargetSP target_sp(GetSP());
1711     if (target_sp)
1712     {
1713         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1714         target_sp->RemoveAllBreakpoints ();
1715         return true;
1716     }
1717     return false;
1718 }
1719 
1720 uint32_t
1721 SBTarget::GetNumWatchpoints () const
1722 {
1723     TargetSP target_sp(GetSP());
1724     if (target_sp)
1725     {
1726         // The watchpoint list is thread safe, no need to lock
1727         return target_sp->GetWatchpointList().GetSize();
1728     }
1729     return 0;
1730 }
1731 
1732 SBWatchpoint
1733 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
1734 {
1735     SBWatchpoint sb_watchpoint;
1736     TargetSP target_sp(GetSP());
1737     if (target_sp)
1738     {
1739         // The watchpoint list is thread safe, no need to lock
1740         sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
1741     }
1742     return sb_watchpoint;
1743 }
1744 
1745 bool
1746 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
1747 {
1748     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1749 
1750     bool result = false;
1751     TargetSP target_sp(GetSP());
1752     if (target_sp)
1753     {
1754         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1755         Mutex::Locker locker;
1756         target_sp->GetWatchpointList().GetListMutex(locker);
1757         result = target_sp->RemoveWatchpointByID (wp_id);
1758     }
1759 
1760     if (log)
1761     {
1762         log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
1763     }
1764 
1765     return result;
1766 }
1767 
1768 SBWatchpoint
1769 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
1770 {
1771     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1772 
1773     SBWatchpoint sb_watchpoint;
1774     lldb::WatchpointSP watchpoint_sp;
1775     TargetSP target_sp(GetSP());
1776     if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
1777     {
1778         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1779         Mutex::Locker locker;
1780         target_sp->GetWatchpointList().GetListMutex(locker);
1781         watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1782         sb_watchpoint.SetSP (watchpoint_sp);
1783     }
1784 
1785     if (log)
1786     {
1787         log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1788                      target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
1789     }
1790 
1791     return sb_watchpoint;
1792 }
1793 
1794 lldb::SBWatchpoint
1795 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
1796 {
1797     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1798 
1799     SBWatchpoint sb_watchpoint;
1800     lldb::WatchpointSP watchpoint_sp;
1801     TargetSP target_sp(GetSP());
1802     if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
1803     {
1804         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1805         uint32_t watch_type = 0;
1806         if (read)
1807             watch_type |= LLDB_WATCH_TYPE_READ;
1808         if (write)
1809             watch_type |= LLDB_WATCH_TYPE_WRITE;
1810         // Target::CreateWatchpoint() is thread safe.
1811         Error cw_error;
1812         // This API doesn't take in a type, so we can't figure out what it is.
1813         ClangASTType *type = NULL;
1814         watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1815         error.SetError(cw_error);
1816         sb_watchpoint.SetSP (watchpoint_sp);
1817     }
1818 
1819     if (log)
1820     {
1821         log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
1822                      target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
1823     }
1824 
1825     return sb_watchpoint;
1826 }
1827 
1828 bool
1829 SBTarget::EnableAllWatchpoints ()
1830 {
1831     TargetSP target_sp(GetSP());
1832     if (target_sp)
1833     {
1834         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1835         Mutex::Locker locker;
1836         target_sp->GetWatchpointList().GetListMutex(locker);
1837         target_sp->EnableAllWatchpoints ();
1838         return true;
1839     }
1840     return false;
1841 }
1842 
1843 bool
1844 SBTarget::DisableAllWatchpoints ()
1845 {
1846     TargetSP target_sp(GetSP());
1847     if (target_sp)
1848     {
1849         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1850         Mutex::Locker locker;
1851         target_sp->GetWatchpointList().GetListMutex(locker);
1852         target_sp->DisableAllWatchpoints ();
1853         return true;
1854     }
1855     return false;
1856 }
1857 
1858 bool
1859 SBTarget::DeleteAllWatchpoints ()
1860 {
1861     TargetSP target_sp(GetSP());
1862     if (target_sp)
1863     {
1864         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1865         Mutex::Locker locker;
1866         target_sp->GetWatchpointList().GetListMutex(locker);
1867         target_sp->RemoveAllWatchpoints ();
1868         return true;
1869     }
1870     return false;
1871 }
1872 
1873 
1874 lldb::SBModule
1875 SBTarget::AddModule (const char *path,
1876                      const char *triple,
1877                      const char *uuid_cstr)
1878 {
1879     return AddModule (path, triple, uuid_cstr, NULL);
1880 }
1881 
1882 lldb::SBModule
1883 SBTarget::AddModule (const char *path,
1884                      const char *triple,
1885                      const char *uuid_cstr,
1886                      const char *symfile)
1887 {
1888     lldb::SBModule sb_module;
1889     TargetSP target_sp(GetSP());
1890     if (target_sp)
1891     {
1892         ModuleSpec module_spec;
1893         if (path)
1894             module_spec.GetFileSpec().SetFile(path, false);
1895 
1896         if (uuid_cstr)
1897             module_spec.GetUUID().SetFromCString(uuid_cstr);
1898 
1899         if (triple)
1900             module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1901 
1902         if (symfile)
1903             module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
1904 
1905         sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1906     }
1907     return sb_module;
1908 }
1909 
1910 bool
1911 SBTarget::AddModule (lldb::SBModule &module)
1912 {
1913     TargetSP target_sp(GetSP());
1914     if (target_sp)
1915     {
1916         target_sp->GetImages().AppendIfNeeded (module.GetSP());
1917         return true;
1918     }
1919     return false;
1920 }
1921 
1922 uint32_t
1923 SBTarget::GetNumModules () const
1924 {
1925     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1926 
1927     uint32_t num = 0;
1928     TargetSP target_sp(GetSP());
1929     if (target_sp)
1930     {
1931         // The module list is thread safe, no need to lock
1932         num = target_sp->GetImages().GetSize();
1933     }
1934 
1935     if (log)
1936         log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
1937 
1938     return num;
1939 }
1940 
1941 void
1942 SBTarget::Clear ()
1943 {
1944     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1945 
1946     if (log)
1947         log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
1948 
1949     m_opaque_sp.reset();
1950 }
1951 
1952 
1953 SBModule
1954 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1955 {
1956     SBModule sb_module;
1957     TargetSP target_sp(GetSP());
1958     if (target_sp && sb_file_spec.IsValid())
1959     {
1960         ModuleSpec module_spec(*sb_file_spec);
1961         // The module list is thread safe, no need to lock
1962         sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
1963     }
1964     return sb_module;
1965 }
1966 
1967 lldb::ByteOrder
1968 SBTarget::GetByteOrder ()
1969 {
1970     TargetSP target_sp(GetSP());
1971     if (target_sp)
1972         return target_sp->GetArchitecture().GetByteOrder();
1973     return eByteOrderInvalid;
1974 }
1975 
1976 const char *
1977 SBTarget::GetTriple ()
1978 {
1979     TargetSP target_sp(GetSP());
1980     if (target_sp)
1981     {
1982         std::string triple (target_sp->GetArchitecture().GetTriple().str());
1983         // Unique the string so we don't run into ownership issues since
1984         // the const strings put the string into the string pool once and
1985         // the strings never comes out
1986         ConstString const_triple (triple.c_str());
1987         return const_triple.GetCString();
1988     }
1989     return NULL;
1990 }
1991 
1992 uint32_t
1993 SBTarget::GetAddressByteSize()
1994 {
1995     TargetSP target_sp(GetSP());
1996     if (target_sp)
1997         return target_sp->GetArchitecture().GetAddressByteSize();
1998     return sizeof(void*);
1999 }
2000 
2001 
2002 SBModule
2003 SBTarget::GetModuleAtIndex (uint32_t idx)
2004 {
2005     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2006 
2007     SBModule sb_module;
2008     ModuleSP module_sp;
2009     TargetSP target_sp(GetSP());
2010     if (target_sp)
2011     {
2012         // The module list is thread safe, no need to lock
2013         module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2014         sb_module.SetSP (module_sp);
2015     }
2016 
2017     if (log)
2018     {
2019         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2020                      target_sp.get(), idx, module_sp.get());
2021     }
2022 
2023     return sb_module;
2024 }
2025 
2026 bool
2027 SBTarget::RemoveModule (lldb::SBModule module)
2028 {
2029     TargetSP target_sp(GetSP());
2030     if (target_sp)
2031         return target_sp->GetImages().Remove(module.GetSP());
2032     return false;
2033 }
2034 
2035 
2036 SBBroadcaster
2037 SBTarget::GetBroadcaster () const
2038 {
2039     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2040 
2041     TargetSP target_sp(GetSP());
2042     SBBroadcaster broadcaster(target_sp.get(), false);
2043 
2044     if (log)
2045         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2046                      target_sp.get(), broadcaster.get());
2047 
2048     return broadcaster;
2049 }
2050 
2051 bool
2052 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
2053 {
2054     Stream &strm = description.ref();
2055 
2056     TargetSP target_sp(GetSP());
2057     if (target_sp)
2058     {
2059         target_sp->Dump (&strm, description_level);
2060     }
2061     else
2062         strm.PutCString ("No value");
2063 
2064     return true;
2065 }
2066 
2067 lldb::SBSymbolContextList
2068 SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
2069 {
2070     lldb::SBSymbolContextList sb_sc_list;
2071     if (name && name[0])
2072     {
2073         TargetSP target_sp(GetSP());
2074         if (target_sp)
2075         {
2076             const bool symbols_ok = true;
2077             const bool inlines_ok = true;
2078             const bool append = true;
2079             target_sp->GetImages().FindFunctions (ConstString(name),
2080                                                   name_type_mask,
2081                                                   symbols_ok,
2082                                                   inlines_ok,
2083                                                   append,
2084                                                   *sb_sc_list);
2085         }
2086     }
2087     return sb_sc_list;
2088 }
2089 
2090 lldb::SBType
2091 SBTarget::FindFirstType (const char* typename_cstr)
2092 {
2093     TargetSP target_sp(GetSP());
2094     if (typename_cstr && typename_cstr[0] && target_sp)
2095     {
2096         ConstString const_typename(typename_cstr);
2097         SymbolContext sc;
2098         const bool exact_match = false;
2099 
2100         const ModuleList &module_list = target_sp->GetImages();
2101         size_t count = module_list.GetSize();
2102         for (size_t idx = 0; idx < count; idx++)
2103         {
2104             ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2105             if (module_sp)
2106             {
2107                 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2108                 if (type_sp)
2109                     return SBType(type_sp);
2110             }
2111         }
2112 
2113         // Didn't find the type in the symbols; try the Objective-C runtime
2114         // if one is installed
2115 
2116         ProcessSP process_sp(target_sp->GetProcessSP());
2117 
2118         if (process_sp)
2119         {
2120             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2121 
2122             if (objc_language_runtime)
2123             {
2124                 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2125 
2126                 if (objc_type_vendor)
2127                 {
2128                     std::vector <ClangASTType> types;
2129 
2130                     if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2131                         return SBType(types[0]);
2132                 }
2133             }
2134         }
2135 
2136         // No matches, search for basic typename matches
2137         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2138         if (clang_ast)
2139             return SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), const_typename));
2140     }
2141     return SBType();
2142 }
2143 
2144 SBType
2145 SBTarget::GetBasicType(lldb::BasicType type)
2146 {
2147     TargetSP target_sp(GetSP());
2148     if (target_sp)
2149     {
2150         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2151         if (clang_ast)
2152             return SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), type));
2153     }
2154     return SBType();
2155 }
2156 
2157 
2158 lldb::SBTypeList
2159 SBTarget::FindTypes (const char* typename_cstr)
2160 {
2161     SBTypeList sb_type_list;
2162     TargetSP target_sp(GetSP());
2163     if (typename_cstr && typename_cstr[0] && target_sp)
2164     {
2165         ModuleList& images = target_sp->GetImages();
2166         ConstString const_typename(typename_cstr);
2167         bool exact_match = false;
2168         SymbolContext sc;
2169         TypeList type_list;
2170 
2171         uint32_t num_matches = images.FindTypes (sc,
2172                                                  const_typename,
2173                                                  exact_match,
2174                                                  UINT32_MAX,
2175                                                  type_list);
2176 
2177         if (num_matches > 0)
2178         {
2179             for (size_t idx = 0; idx < num_matches; idx++)
2180             {
2181                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2182                 if (type_sp)
2183                     sb_type_list.Append(SBType(type_sp));
2184             }
2185         }
2186 
2187         // Try the Objective-C runtime if one is installed
2188 
2189         ProcessSP process_sp(target_sp->GetProcessSP());
2190 
2191         if (process_sp)
2192         {
2193             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2194 
2195             if (objc_language_runtime)
2196             {
2197                 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2198 
2199                 if (objc_type_vendor)
2200                 {
2201                     std::vector <ClangASTType> types;
2202 
2203                     if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2204                     {
2205                         for (ClangASTType &type : types)
2206                         {
2207                             sb_type_list.Append(SBType(type));
2208                         }
2209                     }
2210                 }
2211             }
2212         }
2213 
2214         if (sb_type_list.GetSize() == 0)
2215         {
2216             // No matches, search for basic typename matches
2217             ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2218             if (clang_ast)
2219                 sb_type_list.Append (SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), const_typename)));
2220         }
2221     }
2222     return sb_type_list;
2223 }
2224 
2225 SBValueList
2226 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2227 {
2228     SBValueList sb_value_list;
2229 
2230     TargetSP target_sp(GetSP());
2231     if (name && target_sp)
2232     {
2233         VariableList variable_list;
2234         const bool append = true;
2235         const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2236                                                                                  append,
2237                                                                                  max_matches,
2238                                                                                  variable_list);
2239 
2240         if (match_count > 0)
2241         {
2242             ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2243             if (exe_scope == NULL)
2244                 exe_scope = target_sp.get();
2245             for (uint32_t i=0; i<match_count; ++i)
2246             {
2247                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2248                 if (valobj_sp)
2249                     sb_value_list.Append(SBValue(valobj_sp));
2250             }
2251         }
2252     }
2253 
2254     return sb_value_list;
2255 }
2256 
2257 lldb::SBValue
2258 SBTarget::FindFirstGlobalVariable (const char* name)
2259 {
2260     SBValueList sb_value_list(FindGlobalVariables(name, 1));
2261     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2262         return sb_value_list.GetValueAtIndex(0);
2263     return SBValue();
2264 }
2265 
2266 SBSourceManager
2267 SBTarget::GetSourceManager()
2268 {
2269     SBSourceManager source_manager (*this);
2270     return source_manager;
2271 }
2272 
2273 lldb::SBInstructionList
2274 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2275 {
2276     return ReadInstructions (base_addr, count, NULL);
2277 }
2278 
2279 lldb::SBInstructionList
2280 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2281 {
2282     SBInstructionList sb_instructions;
2283 
2284     TargetSP target_sp(GetSP());
2285     if (target_sp)
2286     {
2287         Address *addr_ptr = base_addr.get();
2288 
2289         if (addr_ptr)
2290         {
2291             DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2292             bool prefer_file_cache = false;
2293             lldb_private::Error error;
2294             lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2295             const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2296                                                             prefer_file_cache,
2297                                                             data.GetBytes(),
2298                                                             data.GetByteSize(),
2299                                                             error,
2300                                                             &load_addr);
2301             const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2302             sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2303                                                                              NULL,
2304                                                                              flavor_string,
2305                                                                              *addr_ptr,
2306                                                                              data.GetBytes(),
2307                                                                              bytes_read,
2308                                                                              count,
2309                                                                              data_from_file));
2310         }
2311     }
2312 
2313     return sb_instructions;
2314 
2315 }
2316 
2317 lldb::SBInstructionList
2318 SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2319 {
2320     return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2321 }
2322 
2323 lldb::SBInstructionList
2324 SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2325 {
2326     SBInstructionList sb_instructions;
2327 
2328     TargetSP target_sp(GetSP());
2329     if (target_sp)
2330     {
2331         Address addr;
2332 
2333         if (base_addr.get())
2334             addr = *base_addr.get();
2335 
2336         const bool data_from_file = true;
2337 
2338         sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2339                                                                          NULL,
2340                                                                          flavor_string,
2341                                                                          addr,
2342                                                                          buf,
2343                                                                          size,
2344                                                                          UINT32_MAX,
2345                                                                          data_from_file));
2346     }
2347 
2348     return sb_instructions;
2349 }
2350 
2351 lldb::SBInstructionList
2352 SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2353 {
2354     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2355 }
2356 
2357 lldb::SBInstructionList
2358 SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2359 {
2360     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
2361 }
2362 
2363 SBError
2364 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2365                                  lldb::addr_t section_base_addr)
2366 {
2367     SBError sb_error;
2368     TargetSP target_sp(GetSP());
2369     if (target_sp)
2370     {
2371         if (!section.IsValid())
2372         {
2373             sb_error.SetErrorStringWithFormat ("invalid section");
2374         }
2375         else
2376         {
2377             SectionSP section_sp (section.GetSP());
2378             if (section_sp)
2379             {
2380                 if (section_sp->IsThreadSpecific())
2381                 {
2382                     sb_error.SetErrorString ("thread specific sections are not yet supported");
2383                 }
2384                 else
2385                 {
2386                     if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2387                     {
2388                         // Flush info in the process (stack frames, etc)
2389                         ProcessSP process_sp (target_sp->GetProcessSP());
2390                         if (process_sp)
2391                             process_sp->Flush();
2392                     }
2393                 }
2394             }
2395         }
2396     }
2397     else
2398     {
2399         sb_error.SetErrorString ("invalid target");
2400     }
2401     return sb_error;
2402 }
2403 
2404 SBError
2405 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2406 {
2407     SBError sb_error;
2408 
2409     TargetSP target_sp(GetSP());
2410     if (target_sp)
2411     {
2412         if (!section.IsValid())
2413         {
2414             sb_error.SetErrorStringWithFormat ("invalid section");
2415         }
2416         else
2417         {
2418             if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
2419             {
2420                 // Flush info in the process (stack frames, etc)
2421                 ProcessSP process_sp (target_sp->GetProcessSP());
2422                 if (process_sp)
2423                     process_sp->Flush();
2424             }
2425         }
2426     }
2427     else
2428     {
2429         sb_error.SetErrorStringWithFormat ("invalid target");
2430     }
2431     return sb_error;
2432 }
2433 
2434 SBError
2435 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2436 {
2437     SBError sb_error;
2438 
2439     TargetSP target_sp(GetSP());
2440     if (target_sp)
2441     {
2442         ModuleSP module_sp (module.GetSP());
2443         if (module_sp)
2444         {
2445             bool changed = false;
2446             if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
2447             {
2448                 // The load was successful, make sure that at least some sections
2449                 // changed before we notify that our module was loaded.
2450                 if (changed)
2451                 {
2452                     ModuleList module_list;
2453                     module_list.Append(module_sp);
2454                     target_sp->ModulesDidLoad (module_list);
2455                     // Flush info in the process (stack frames, etc)
2456                     ProcessSP process_sp (target_sp->GetProcessSP());
2457                     if (process_sp)
2458                         process_sp->Flush();
2459                 }
2460             }
2461         }
2462         else
2463         {
2464             sb_error.SetErrorStringWithFormat ("invalid module");
2465         }
2466 
2467     }
2468     else
2469     {
2470         sb_error.SetErrorStringWithFormat ("invalid target");
2471     }
2472     return sb_error;
2473 }
2474 
2475 SBError
2476 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2477 {
2478     SBError sb_error;
2479 
2480     char path[PATH_MAX];
2481     TargetSP target_sp(GetSP());
2482     if (target_sp)
2483     {
2484         ModuleSP module_sp (module.GetSP());
2485         if (module_sp)
2486         {
2487             ObjectFile *objfile = module_sp->GetObjectFile();
2488             if (objfile)
2489             {
2490                 SectionList *section_list = objfile->GetSectionList();
2491                 if (section_list)
2492                 {
2493                     bool changed = false;
2494                     const size_t num_sections = section_list->GetSize();
2495                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2496                     {
2497                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2498                         if (section_sp)
2499                             changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2500                     }
2501                     if (changed)
2502                     {
2503                         // Flush info in the process (stack frames, etc)
2504                         ProcessSP process_sp (target_sp->GetProcessSP());
2505                         if (process_sp)
2506                             process_sp->Flush();
2507                     }
2508                 }
2509                 else
2510                 {
2511                     module_sp->GetFileSpec().GetPath (path, sizeof(path));
2512                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2513                 }
2514             }
2515             else
2516             {
2517                 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2518                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2519             }
2520         }
2521         else
2522         {
2523             sb_error.SetErrorStringWithFormat ("invalid module");
2524         }
2525     }
2526     else
2527     {
2528         sb_error.SetErrorStringWithFormat ("invalid target");
2529     }
2530     return sb_error;
2531 }
2532 
2533 
2534 lldb::SBSymbolContextList
2535 SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2536 {
2537     SBSymbolContextList sb_sc_list;
2538     if (name && name[0])
2539     {
2540         TargetSP target_sp(GetSP());
2541         if (target_sp)
2542         {
2543             bool append = true;
2544             target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2545                                                                symbol_type,
2546                                                                *sb_sc_list,
2547                                                                append);
2548         }
2549     }
2550     return sb_sc_list;
2551 
2552 }
2553 
2554 
2555 lldb::SBValue
2556 SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2557 {
2558     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2559     Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2560     SBValue expr_result;
2561     ExecutionResults exe_results = eExecutionSetupError;
2562     ValueObjectSP expr_value_sp;
2563     TargetSP target_sp(GetSP());
2564     StackFrame *frame = NULL;
2565     if (target_sp)
2566     {
2567         if (expr == NULL || expr[0] == '\0')
2568         {
2569             if (log)
2570                 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2571             return expr_result;
2572         }
2573 
2574         Mutex::Locker api_locker (target_sp->GetAPIMutex());
2575         ExecutionContext exe_ctx (m_opaque_sp.get());
2576 
2577         if (log)
2578             log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2579 
2580         frame = exe_ctx.GetFramePtr();
2581         Target *target = exe_ctx.GetTargetPtr();
2582 
2583         if (target)
2584         {
2585 #ifdef LLDB_CONFIGURATION_DEBUG
2586             StreamString frame_description;
2587             if (frame)
2588                 frame->DumpUsingSettingsFormat (&frame_description);
2589             Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2590                                                  expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2591 #endif
2592             exe_results = target->EvaluateExpression (expr,
2593                                                       frame,
2594                                                       expr_value_sp,
2595                                                       options.ref());
2596 
2597             expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2598 #ifdef LLDB_CONFIGURATION_DEBUG
2599             Host::SetCrashDescription (NULL);
2600 #endif
2601         }
2602         else
2603         {
2604             if (log)
2605                 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2606         }
2607     }
2608 #ifndef LLDB_DISABLE_PYTHON
2609     if (expr_log)
2610         expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2611                          expr_result.GetValue(),
2612                          expr_result.GetSummary());
2613 
2614     if (log)
2615         log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2616                      frame,
2617                      expr,
2618                      expr_value_sp.get(),
2619                      exe_results);
2620 #endif
2621 
2622     return expr_result;
2623 }
2624 
2625 
2626 lldb::addr_t
2627 SBTarget::GetStackRedZoneSize()
2628 {
2629     TargetSP target_sp(GetSP());
2630     if (target_sp)
2631     {
2632         ABISP abi_sp;
2633         ProcessSP process_sp (target_sp->GetProcessSP());
2634         if (process_sp)
2635             abi_sp = process_sp->GetABI();
2636         else
2637             abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2638         if (abi_sp)
2639             return abi_sp->GetRedZoneSize();
2640     }
2641     return 0;
2642 }
2643 
2644