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