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