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