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