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