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