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