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