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         if (watch_type == 0)
1811         {
1812             error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1813             return sb_watchpoint;
1814         }
1815 
1816         // Target::CreateWatchpoint() is thread safe.
1817         Error cw_error;
1818         // This API doesn't take in a type, so we can't figure out what it is.
1819         ClangASTType *type = NULL;
1820         watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1821         error.SetError(cw_error);
1822         sb_watchpoint.SetSP (watchpoint_sp);
1823     }
1824 
1825     if (log)
1826     {
1827         log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
1828                      target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
1829     }
1830 
1831     return sb_watchpoint;
1832 }
1833 
1834 bool
1835 SBTarget::EnableAllWatchpoints ()
1836 {
1837     TargetSP target_sp(GetSP());
1838     if (target_sp)
1839     {
1840         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1841         Mutex::Locker locker;
1842         target_sp->GetWatchpointList().GetListMutex(locker);
1843         target_sp->EnableAllWatchpoints ();
1844         return true;
1845     }
1846     return false;
1847 }
1848 
1849 bool
1850 SBTarget::DisableAllWatchpoints ()
1851 {
1852     TargetSP target_sp(GetSP());
1853     if (target_sp)
1854     {
1855         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1856         Mutex::Locker locker;
1857         target_sp->GetWatchpointList().GetListMutex(locker);
1858         target_sp->DisableAllWatchpoints ();
1859         return true;
1860     }
1861     return false;
1862 }
1863 
1864 bool
1865 SBTarget::DeleteAllWatchpoints ()
1866 {
1867     TargetSP target_sp(GetSP());
1868     if (target_sp)
1869     {
1870         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1871         Mutex::Locker locker;
1872         target_sp->GetWatchpointList().GetListMutex(locker);
1873         target_sp->RemoveAllWatchpoints ();
1874         return true;
1875     }
1876     return false;
1877 }
1878 
1879 
1880 lldb::SBModule
1881 SBTarget::AddModule (const char *path,
1882                      const char *triple,
1883                      const char *uuid_cstr)
1884 {
1885     return AddModule (path, triple, uuid_cstr, NULL);
1886 }
1887 
1888 lldb::SBModule
1889 SBTarget::AddModule (const char *path,
1890                      const char *triple,
1891                      const char *uuid_cstr,
1892                      const char *symfile)
1893 {
1894     lldb::SBModule sb_module;
1895     TargetSP target_sp(GetSP());
1896     if (target_sp)
1897     {
1898         ModuleSpec module_spec;
1899         if (path)
1900             module_spec.GetFileSpec().SetFile(path, false);
1901 
1902         if (uuid_cstr)
1903             module_spec.GetUUID().SetFromCString(uuid_cstr);
1904 
1905         if (triple)
1906             module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1907 
1908         if (symfile)
1909             module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
1910 
1911         sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1912     }
1913     return sb_module;
1914 }
1915 
1916 bool
1917 SBTarget::AddModule (lldb::SBModule &module)
1918 {
1919     TargetSP target_sp(GetSP());
1920     if (target_sp)
1921     {
1922         target_sp->GetImages().AppendIfNeeded (module.GetSP());
1923         return true;
1924     }
1925     return false;
1926 }
1927 
1928 uint32_t
1929 SBTarget::GetNumModules () const
1930 {
1931     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1932 
1933     uint32_t num = 0;
1934     TargetSP target_sp(GetSP());
1935     if (target_sp)
1936     {
1937         // The module list is thread safe, no need to lock
1938         num = target_sp->GetImages().GetSize();
1939     }
1940 
1941     if (log)
1942         log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
1943 
1944     return num;
1945 }
1946 
1947 void
1948 SBTarget::Clear ()
1949 {
1950     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1951 
1952     if (log)
1953         log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
1954 
1955     m_opaque_sp.reset();
1956 }
1957 
1958 
1959 SBModule
1960 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1961 {
1962     SBModule sb_module;
1963     TargetSP target_sp(GetSP());
1964     if (target_sp && sb_file_spec.IsValid())
1965     {
1966         ModuleSpec module_spec(*sb_file_spec);
1967         // The module list is thread safe, no need to lock
1968         sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
1969     }
1970     return sb_module;
1971 }
1972 
1973 lldb::ByteOrder
1974 SBTarget::GetByteOrder ()
1975 {
1976     TargetSP target_sp(GetSP());
1977     if (target_sp)
1978         return target_sp->GetArchitecture().GetByteOrder();
1979     return eByteOrderInvalid;
1980 }
1981 
1982 const char *
1983 SBTarget::GetTriple ()
1984 {
1985     TargetSP target_sp(GetSP());
1986     if (target_sp)
1987     {
1988         std::string triple (target_sp->GetArchitecture().GetTriple().str());
1989         // Unique the string so we don't run into ownership issues since
1990         // the const strings put the string into the string pool once and
1991         // the strings never comes out
1992         ConstString const_triple (triple.c_str());
1993         return const_triple.GetCString();
1994     }
1995     return NULL;
1996 }
1997 
1998 uint32_t
1999 SBTarget::GetAddressByteSize()
2000 {
2001     TargetSP target_sp(GetSP());
2002     if (target_sp)
2003         return target_sp->GetArchitecture().GetAddressByteSize();
2004     return sizeof(void*);
2005 }
2006 
2007 
2008 SBModule
2009 SBTarget::GetModuleAtIndex (uint32_t idx)
2010 {
2011     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2012 
2013     SBModule sb_module;
2014     ModuleSP module_sp;
2015     TargetSP target_sp(GetSP());
2016     if (target_sp)
2017     {
2018         // The module list is thread safe, no need to lock
2019         module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2020         sb_module.SetSP (module_sp);
2021     }
2022 
2023     if (log)
2024     {
2025         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2026                      target_sp.get(), idx, module_sp.get());
2027     }
2028 
2029     return sb_module;
2030 }
2031 
2032 bool
2033 SBTarget::RemoveModule (lldb::SBModule module)
2034 {
2035     TargetSP target_sp(GetSP());
2036     if (target_sp)
2037         return target_sp->GetImages().Remove(module.GetSP());
2038     return false;
2039 }
2040 
2041 
2042 SBBroadcaster
2043 SBTarget::GetBroadcaster () const
2044 {
2045     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2046 
2047     TargetSP target_sp(GetSP());
2048     SBBroadcaster broadcaster(target_sp.get(), false);
2049 
2050     if (log)
2051         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2052                      target_sp.get(), broadcaster.get());
2053 
2054     return broadcaster;
2055 }
2056 
2057 bool
2058 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
2059 {
2060     Stream &strm = description.ref();
2061 
2062     TargetSP target_sp(GetSP());
2063     if (target_sp)
2064     {
2065         target_sp->Dump (&strm, description_level);
2066     }
2067     else
2068         strm.PutCString ("No value");
2069 
2070     return true;
2071 }
2072 
2073 lldb::SBSymbolContextList
2074 SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
2075 {
2076     lldb::SBSymbolContextList sb_sc_list;
2077     if (name && name[0])
2078     {
2079         TargetSP target_sp(GetSP());
2080         if (target_sp)
2081         {
2082             const bool symbols_ok = true;
2083             const bool inlines_ok = true;
2084             const bool append = true;
2085             target_sp->GetImages().FindFunctions (ConstString(name),
2086                                                   name_type_mask,
2087                                                   symbols_ok,
2088                                                   inlines_ok,
2089                                                   append,
2090                                                   *sb_sc_list);
2091         }
2092     }
2093     return sb_sc_list;
2094 }
2095 
2096 lldb::SBType
2097 SBTarget::FindFirstType (const char* typename_cstr)
2098 {
2099     TargetSP target_sp(GetSP());
2100     if (typename_cstr && typename_cstr[0] && target_sp)
2101     {
2102         ConstString const_typename(typename_cstr);
2103         SymbolContext sc;
2104         const bool exact_match = false;
2105 
2106         const ModuleList &module_list = target_sp->GetImages();
2107         size_t count = module_list.GetSize();
2108         for (size_t idx = 0; idx < count; idx++)
2109         {
2110             ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2111             if (module_sp)
2112             {
2113                 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2114                 if (type_sp)
2115                     return SBType(type_sp);
2116             }
2117         }
2118 
2119         // Didn't find the type in the symbols; try the Objective-C runtime
2120         // if one is installed
2121 
2122         ProcessSP process_sp(target_sp->GetProcessSP());
2123 
2124         if (process_sp)
2125         {
2126             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2127 
2128             if (objc_language_runtime)
2129             {
2130                 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2131 
2132                 if (objc_type_vendor)
2133                 {
2134                     std::vector <ClangASTType> types;
2135 
2136                     if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2137                         return SBType(types[0]);
2138                 }
2139             }
2140         }
2141 
2142         // No matches, search for basic typename matches
2143         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2144         if (clang_ast)
2145             return SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), const_typename));
2146     }
2147     return SBType();
2148 }
2149 
2150 SBType
2151 SBTarget::GetBasicType(lldb::BasicType type)
2152 {
2153     TargetSP target_sp(GetSP());
2154     if (target_sp)
2155     {
2156         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2157         if (clang_ast)
2158             return SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), type));
2159     }
2160     return SBType();
2161 }
2162 
2163 
2164 lldb::SBTypeList
2165 SBTarget::FindTypes (const char* typename_cstr)
2166 {
2167     SBTypeList sb_type_list;
2168     TargetSP target_sp(GetSP());
2169     if (typename_cstr && typename_cstr[0] && target_sp)
2170     {
2171         ModuleList& images = target_sp->GetImages();
2172         ConstString const_typename(typename_cstr);
2173         bool exact_match = false;
2174         SymbolContext sc;
2175         TypeList type_list;
2176 
2177         uint32_t num_matches = images.FindTypes (sc,
2178                                                  const_typename,
2179                                                  exact_match,
2180                                                  UINT32_MAX,
2181                                                  type_list);
2182 
2183         if (num_matches > 0)
2184         {
2185             for (size_t idx = 0; idx < num_matches; idx++)
2186             {
2187                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2188                 if (type_sp)
2189                     sb_type_list.Append(SBType(type_sp));
2190             }
2191         }
2192 
2193         // Try the Objective-C runtime if one is installed
2194 
2195         ProcessSP process_sp(target_sp->GetProcessSP());
2196 
2197         if (process_sp)
2198         {
2199             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2200 
2201             if (objc_language_runtime)
2202             {
2203                 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2204 
2205                 if (objc_type_vendor)
2206                 {
2207                     std::vector <ClangASTType> types;
2208 
2209                     if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2210                     {
2211                         for (ClangASTType &type : types)
2212                         {
2213                             sb_type_list.Append(SBType(type));
2214                         }
2215                     }
2216                 }
2217             }
2218         }
2219 
2220         if (sb_type_list.GetSize() == 0)
2221         {
2222             // No matches, search for basic typename matches
2223             ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2224             if (clang_ast)
2225                 sb_type_list.Append (SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), const_typename)));
2226         }
2227     }
2228     return sb_type_list;
2229 }
2230 
2231 SBValueList
2232 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2233 {
2234     SBValueList sb_value_list;
2235 
2236     TargetSP target_sp(GetSP());
2237     if (name && target_sp)
2238     {
2239         VariableList variable_list;
2240         const bool append = true;
2241         const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2242                                                                                  append,
2243                                                                                  max_matches,
2244                                                                                  variable_list);
2245 
2246         if (match_count > 0)
2247         {
2248             ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2249             if (exe_scope == NULL)
2250                 exe_scope = target_sp.get();
2251             for (uint32_t i=0; i<match_count; ++i)
2252             {
2253                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2254                 if (valobj_sp)
2255                     sb_value_list.Append(SBValue(valobj_sp));
2256             }
2257         }
2258     }
2259 
2260     return sb_value_list;
2261 }
2262 
2263 lldb::SBValue
2264 SBTarget::FindFirstGlobalVariable (const char* name)
2265 {
2266     SBValueList sb_value_list(FindGlobalVariables(name, 1));
2267     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2268         return sb_value_list.GetValueAtIndex(0);
2269     return SBValue();
2270 }
2271 
2272 SBSourceManager
2273 SBTarget::GetSourceManager()
2274 {
2275     SBSourceManager source_manager (*this);
2276     return source_manager;
2277 }
2278 
2279 lldb::SBInstructionList
2280 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2281 {
2282     return ReadInstructions (base_addr, count, NULL);
2283 }
2284 
2285 lldb::SBInstructionList
2286 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2287 {
2288     SBInstructionList sb_instructions;
2289 
2290     TargetSP target_sp(GetSP());
2291     if (target_sp)
2292     {
2293         Address *addr_ptr = base_addr.get();
2294 
2295         if (addr_ptr)
2296         {
2297             DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2298             bool prefer_file_cache = false;
2299             lldb_private::Error error;
2300             lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2301             const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2302                                                             prefer_file_cache,
2303                                                             data.GetBytes(),
2304                                                             data.GetByteSize(),
2305                                                             error,
2306                                                             &load_addr);
2307             const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2308             sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2309                                                                              NULL,
2310                                                                              flavor_string,
2311                                                                              *addr_ptr,
2312                                                                              data.GetBytes(),
2313                                                                              bytes_read,
2314                                                                              count,
2315                                                                              data_from_file));
2316         }
2317     }
2318 
2319     return sb_instructions;
2320 
2321 }
2322 
2323 lldb::SBInstructionList
2324 SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2325 {
2326     return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2327 }
2328 
2329 lldb::SBInstructionList
2330 SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2331 {
2332     SBInstructionList sb_instructions;
2333 
2334     TargetSP target_sp(GetSP());
2335     if (target_sp)
2336     {
2337         Address addr;
2338 
2339         if (base_addr.get())
2340             addr = *base_addr.get();
2341 
2342         const bool data_from_file = true;
2343 
2344         sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2345                                                                          NULL,
2346                                                                          flavor_string,
2347                                                                          addr,
2348                                                                          buf,
2349                                                                          size,
2350                                                                          UINT32_MAX,
2351                                                                          data_from_file));
2352     }
2353 
2354     return sb_instructions;
2355 }
2356 
2357 lldb::SBInstructionList
2358 SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2359 {
2360     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2361 }
2362 
2363 lldb::SBInstructionList
2364 SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2365 {
2366     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
2367 }
2368 
2369 SBError
2370 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2371                                  lldb::addr_t section_base_addr)
2372 {
2373     SBError sb_error;
2374     TargetSP target_sp(GetSP());
2375     if (target_sp)
2376     {
2377         if (!section.IsValid())
2378         {
2379             sb_error.SetErrorStringWithFormat ("invalid section");
2380         }
2381         else
2382         {
2383             SectionSP section_sp (section.GetSP());
2384             if (section_sp)
2385             {
2386                 if (section_sp->IsThreadSpecific())
2387                 {
2388                     sb_error.SetErrorString ("thread specific sections are not yet supported");
2389                 }
2390                 else
2391                 {
2392                     if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2393                     {
2394                         // Flush info in the process (stack frames, etc)
2395                         ProcessSP process_sp (target_sp->GetProcessSP());
2396                         if (process_sp)
2397                             process_sp->Flush();
2398                     }
2399                 }
2400             }
2401         }
2402     }
2403     else
2404     {
2405         sb_error.SetErrorString ("invalid target");
2406     }
2407     return sb_error;
2408 }
2409 
2410 SBError
2411 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2412 {
2413     SBError sb_error;
2414 
2415     TargetSP target_sp(GetSP());
2416     if (target_sp)
2417     {
2418         if (!section.IsValid())
2419         {
2420             sb_error.SetErrorStringWithFormat ("invalid section");
2421         }
2422         else
2423         {
2424             if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
2425             {
2426                 // Flush info in the process (stack frames, etc)
2427                 ProcessSP process_sp (target_sp->GetProcessSP());
2428                 if (process_sp)
2429                     process_sp->Flush();
2430             }
2431         }
2432     }
2433     else
2434     {
2435         sb_error.SetErrorStringWithFormat ("invalid target");
2436     }
2437     return sb_error;
2438 }
2439 
2440 SBError
2441 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2442 {
2443     SBError sb_error;
2444 
2445     TargetSP target_sp(GetSP());
2446     if (target_sp)
2447     {
2448         ModuleSP module_sp (module.GetSP());
2449         if (module_sp)
2450         {
2451             bool changed = false;
2452             if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
2453             {
2454                 // The load was successful, make sure that at least some sections
2455                 // changed before we notify that our module was loaded.
2456                 if (changed)
2457                 {
2458                     ModuleList module_list;
2459                     module_list.Append(module_sp);
2460                     target_sp->ModulesDidLoad (module_list);
2461                     // Flush info in the process (stack frames, etc)
2462                     ProcessSP process_sp (target_sp->GetProcessSP());
2463                     if (process_sp)
2464                         process_sp->Flush();
2465                 }
2466             }
2467         }
2468         else
2469         {
2470             sb_error.SetErrorStringWithFormat ("invalid module");
2471         }
2472 
2473     }
2474     else
2475     {
2476         sb_error.SetErrorStringWithFormat ("invalid target");
2477     }
2478     return sb_error;
2479 }
2480 
2481 SBError
2482 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2483 {
2484     SBError sb_error;
2485 
2486     char path[PATH_MAX];
2487     TargetSP target_sp(GetSP());
2488     if (target_sp)
2489     {
2490         ModuleSP module_sp (module.GetSP());
2491         if (module_sp)
2492         {
2493             ObjectFile *objfile = module_sp->GetObjectFile();
2494             if (objfile)
2495             {
2496                 SectionList *section_list = objfile->GetSectionList();
2497                 if (section_list)
2498                 {
2499                     bool changed = false;
2500                     const size_t num_sections = section_list->GetSize();
2501                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2502                     {
2503                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2504                         if (section_sp)
2505                             changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2506                     }
2507                     if (changed)
2508                     {
2509                         // Flush info in the process (stack frames, etc)
2510                         ProcessSP process_sp (target_sp->GetProcessSP());
2511                         if (process_sp)
2512                             process_sp->Flush();
2513                     }
2514                 }
2515                 else
2516                 {
2517                     module_sp->GetFileSpec().GetPath (path, sizeof(path));
2518                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2519                 }
2520             }
2521             else
2522             {
2523                 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2524                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2525             }
2526         }
2527         else
2528         {
2529             sb_error.SetErrorStringWithFormat ("invalid module");
2530         }
2531     }
2532     else
2533     {
2534         sb_error.SetErrorStringWithFormat ("invalid target");
2535     }
2536     return sb_error;
2537 }
2538 
2539 
2540 lldb::SBSymbolContextList
2541 SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2542 {
2543     SBSymbolContextList sb_sc_list;
2544     if (name && name[0])
2545     {
2546         TargetSP target_sp(GetSP());
2547         if (target_sp)
2548         {
2549             bool append = true;
2550             target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2551                                                                symbol_type,
2552                                                                *sb_sc_list,
2553                                                                append);
2554         }
2555     }
2556     return sb_sc_list;
2557 
2558 }
2559 
2560 
2561 lldb::SBValue
2562 SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2563 {
2564     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2565     Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2566     SBValue expr_result;
2567     ExecutionResults exe_results = eExecutionSetupError;
2568     ValueObjectSP expr_value_sp;
2569     TargetSP target_sp(GetSP());
2570     StackFrame *frame = NULL;
2571     if (target_sp)
2572     {
2573         if (expr == NULL || expr[0] == '\0')
2574         {
2575             if (log)
2576                 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2577             return expr_result;
2578         }
2579 
2580         Mutex::Locker api_locker (target_sp->GetAPIMutex());
2581         ExecutionContext exe_ctx (m_opaque_sp.get());
2582 
2583         if (log)
2584             log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2585 
2586         frame = exe_ctx.GetFramePtr();
2587         Target *target = exe_ctx.GetTargetPtr();
2588 
2589         if (target)
2590         {
2591 #ifdef LLDB_CONFIGURATION_DEBUG
2592             StreamString frame_description;
2593             if (frame)
2594                 frame->DumpUsingSettingsFormat (&frame_description);
2595             Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2596                                                  expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2597 #endif
2598             exe_results = target->EvaluateExpression (expr,
2599                                                       frame,
2600                                                       expr_value_sp,
2601                                                       options.ref());
2602 
2603             expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2604 #ifdef LLDB_CONFIGURATION_DEBUG
2605             Host::SetCrashDescription (NULL);
2606 #endif
2607         }
2608         else
2609         {
2610             if (log)
2611                 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2612         }
2613     }
2614 #ifndef LLDB_DISABLE_PYTHON
2615     if (expr_log)
2616         expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2617                          expr_result.GetValue(),
2618                          expr_result.GetSummary());
2619 
2620     if (log)
2621         log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2622                      frame,
2623                      expr,
2624                      expr_value_sp.get(),
2625                      exe_results);
2626 #endif
2627 
2628     return expr_result;
2629 }
2630 
2631 
2632 lldb::addr_t
2633 SBTarget::GetStackRedZoneSize()
2634 {
2635     TargetSP target_sp(GetSP());
2636     if (target_sp)
2637     {
2638         ABISP abi_sp;
2639         ProcessSP process_sp (target_sp->GetProcessSP());
2640         if (process_sp)
2641             abi_sp = process_sp->GetABI();
2642         else
2643             abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2644         if (abi_sp)
2645             return abi_sp->GetRedZoneSize();
2646     }
2647     return 0;
2648 }
2649 
2650