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