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, uint32_t resolve_scope)
1265 {
1266     SBSymbolContext sc;
1267     if (addr.IsValid())
1268     {
1269         TargetSP target_sp(GetSP());
1270         if (target_sp)
1271             target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1272     }
1273     return sc;
1274 }
1275 
1276 
1277 SBBreakpoint
1278 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
1279 {
1280     return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
1281 }
1282 
1283 SBBreakpoint
1284 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
1285 {
1286     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1287 
1288     SBBreakpoint sb_bp;
1289     TargetSP target_sp(GetSP());
1290     if (target_sp && line != 0)
1291     {
1292         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1293 
1294         const LazyBool check_inlines = eLazyBoolCalculate;
1295         const LazyBool skip_prologue = eLazyBoolCalculate;
1296         const bool internal = false;
1297         *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal);
1298     }
1299 
1300     if (log)
1301     {
1302         SBStream sstr;
1303         sb_bp.GetDescription (sstr);
1304         char path[PATH_MAX];
1305         sb_file_spec->GetPath (path, sizeof(path));
1306         log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
1307                      target_sp.get(),
1308                      path,
1309                      line,
1310                      sb_bp.get(),
1311                      sstr.GetData());
1312     }
1313 
1314     return sb_bp;
1315 }
1316 
1317 SBBreakpoint
1318 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
1319 {
1320     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1321 
1322     SBBreakpoint sb_bp;
1323     TargetSP target_sp(GetSP());
1324     if (target_sp.get())
1325     {
1326         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1327 
1328         const bool internal = false;
1329         const LazyBool skip_prologue = eLazyBoolCalculate;
1330         if (module_name && module_name[0])
1331         {
1332             FileSpecList module_spec_list;
1333             module_spec_list.Append (FileSpec (module_name, false));
1334             *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
1335         }
1336         else
1337         {
1338             *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
1339         }
1340     }
1341 
1342     if (log)
1343     {
1344         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
1345                      target_sp.get(), symbol_name, module_name, sb_bp.get());
1346     }
1347 
1348     return sb_bp;
1349 }
1350 
1351 lldb::SBBreakpoint
1352 SBTarget::BreakpointCreateByName (const char *symbol_name,
1353                             const SBFileSpecList &module_list,
1354                             const SBFileSpecList &comp_unit_list)
1355 {
1356     uint32_t name_type_mask = eFunctionNameTypeAuto;
1357     return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1358 }
1359 
1360 lldb::SBBreakpoint
1361 SBTarget::BreakpointCreateByName (const char *symbol_name,
1362                             uint32_t name_type_mask,
1363                             const SBFileSpecList &module_list,
1364                             const SBFileSpecList &comp_unit_list)
1365 {
1366     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1367 
1368     SBBreakpoint sb_bp;
1369     TargetSP target_sp(GetSP());
1370     if (target_sp && symbol_name && symbol_name[0])
1371     {
1372         const bool internal = false;
1373         const LazyBool skip_prologue = eLazyBoolCalculate;
1374         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1375         *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1376                                                 comp_unit_list.get(),
1377                                                 symbol_name,
1378                                                 name_type_mask,
1379                                                 skip_prologue,
1380                                                 internal);
1381     }
1382 
1383     if (log)
1384     {
1385         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
1386                      target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
1387     }
1388 
1389     return sb_bp;
1390 }
1391 
1392 lldb::SBBreakpoint
1393 SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1394                                    uint32_t num_names,
1395                                    uint32_t name_type_mask,
1396                                    const SBFileSpecList &module_list,
1397                                    const SBFileSpecList &comp_unit_list)
1398 {
1399     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1400 
1401     SBBreakpoint sb_bp;
1402     TargetSP target_sp(GetSP());
1403     if (target_sp && num_names > 0)
1404     {
1405         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1406         const bool internal = false;
1407         const LazyBool skip_prologue = eLazyBoolCalculate;
1408         *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1409                                                 comp_unit_list.get(),
1410                                                 symbol_names,
1411                                                 num_names,
1412                                                 name_type_mask,
1413                                                 skip_prologue,
1414                                                 internal);
1415     }
1416 
1417     if (log)
1418     {
1419         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1420         for (uint32_t i = 0 ; i < num_names; i++)
1421         {
1422             char sep;
1423             if (i < num_names - 1)
1424                 sep = ',';
1425             else
1426                 sep = '}';
1427             if (symbol_names[i] != NULL)
1428                 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1429             else
1430                 log->Printf ("\"<NULL>\"%c ", sep);
1431 
1432         }
1433         log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1434     }
1435 
1436     return sb_bp;
1437 }
1438 
1439 SBBreakpoint
1440 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
1441 {
1442     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1443 
1444     SBBreakpoint sb_bp;
1445     TargetSP target_sp(GetSP());
1446     if (target_sp && symbol_name_regex && symbol_name_regex[0])
1447     {
1448         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1449         RegularExpression regexp(symbol_name_regex);
1450         const bool internal = false;
1451         const LazyBool skip_prologue = eLazyBoolCalculate;
1452 
1453         if (module_name && module_name[0])
1454         {
1455             FileSpecList module_spec_list;
1456             module_spec_list.Append (FileSpec (module_name, false));
1457 
1458             *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal);
1459         }
1460         else
1461         {
1462             *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal);
1463         }
1464     }
1465 
1466     if (log)
1467     {
1468         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1469                      target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
1470     }
1471 
1472     return sb_bp;
1473 }
1474 
1475 lldb::SBBreakpoint
1476 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1477                          const SBFileSpecList &module_list,
1478                          const SBFileSpecList &comp_unit_list)
1479 {
1480     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1481 
1482     SBBreakpoint sb_bp;
1483     TargetSP target_sp(GetSP());
1484     if (target_sp && symbol_name_regex && symbol_name_regex[0])
1485     {
1486         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1487         RegularExpression regexp(symbol_name_regex);
1488         const bool internal = false;
1489         const LazyBool skip_prologue = eLazyBoolCalculate;
1490 
1491         *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal);
1492     }
1493 
1494     if (log)
1495     {
1496         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1497                      target_sp.get(), symbol_name_regex, sb_bp.get());
1498     }
1499 
1500     return sb_bp;
1501 }
1502 
1503 SBBreakpoint
1504 SBTarget::BreakpointCreateByAddress (addr_t address)
1505 {
1506     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1507 
1508     SBBreakpoint sb_bp;
1509     TargetSP target_sp(GetSP());
1510     if (target_sp)
1511     {
1512         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1513         *sb_bp = target_sp->CreateBreakpoint (address, false);
1514     }
1515 
1516     if (log)
1517     {
1518         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)", target_sp.get(), (uint64_t) address, sb_bp.get());
1519     }
1520 
1521     return sb_bp;
1522 }
1523 
1524 lldb::SBBreakpoint
1525 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
1526 {
1527     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1528 
1529     SBBreakpoint sb_bp;
1530     TargetSP target_sp(GetSP());
1531     if (target_sp && source_regex && source_regex[0])
1532     {
1533         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1534         RegularExpression regexp(source_regex);
1535         FileSpecList source_file_spec_list;
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);
1544         }
1545         else
1546         {
1547             *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
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                      target_sp.get(), source_regex, path, module_name, sb_bp.get());
1557     }
1558 
1559     return sb_bp;
1560 }
1561 
1562 lldb::SBBreakpoint
1563 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1564                                const SBFileSpecList &module_list,
1565                                const lldb::SBFileSpecList &source_file_list)
1566 {
1567     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1568 
1569     SBBreakpoint sb_bp;
1570     TargetSP target_sp(GetSP());
1571     if (target_sp && source_regex && source_regex[0])
1572     {
1573         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1574         RegularExpression regexp(source_regex);
1575         *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
1576     }
1577 
1578     if (log)
1579     {
1580         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1581                      target_sp.get(), source_regex, sb_bp.get());
1582     }
1583 
1584     return sb_bp;
1585 }
1586 
1587 lldb::SBBreakpoint
1588 SBTarget::BreakpointCreateForException  (lldb::LanguageType language,
1589                                bool catch_bp,
1590                                bool throw_bp)
1591 {
1592     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1593 
1594     SBBreakpoint sb_bp;
1595     TargetSP target_sp(GetSP());
1596     if (target_sp)
1597     {
1598         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1599         *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp);
1600     }
1601 
1602     if (log)
1603     {
1604         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1605                      target_sp.get(),
1606                      LanguageRuntime::GetNameForLanguageType(language),
1607                      catch_bp ? "on" : "off",
1608                      throw_bp ? "on" : "off",
1609                      sb_bp.get());
1610     }
1611 
1612     return sb_bp;
1613 }
1614 
1615 uint32_t
1616 SBTarget::GetNumBreakpoints () const
1617 {
1618     TargetSP target_sp(GetSP());
1619     if (target_sp)
1620     {
1621         // The breakpoint list is thread safe, no need to lock
1622         return target_sp->GetBreakpointList().GetSize();
1623     }
1624     return 0;
1625 }
1626 
1627 SBBreakpoint
1628 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1629 {
1630     SBBreakpoint sb_breakpoint;
1631     TargetSP target_sp(GetSP());
1632     if (target_sp)
1633     {
1634         // The breakpoint list is thread safe, no need to lock
1635         *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1636     }
1637     return sb_breakpoint;
1638 }
1639 
1640 bool
1641 SBTarget::BreakpointDelete (break_id_t bp_id)
1642 {
1643     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1644 
1645     bool result = false;
1646     TargetSP target_sp(GetSP());
1647     if (target_sp)
1648     {
1649         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1650         result = target_sp->RemoveBreakpointByID (bp_id);
1651     }
1652 
1653     if (log)
1654     {
1655         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
1656     }
1657 
1658     return result;
1659 }
1660 
1661 SBBreakpoint
1662 SBTarget::FindBreakpointByID (break_id_t bp_id)
1663 {
1664     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1665 
1666     SBBreakpoint sb_breakpoint;
1667     TargetSP target_sp(GetSP());
1668     if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
1669     {
1670         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1671         *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
1672     }
1673 
1674     if (log)
1675     {
1676         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1677                      target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
1678     }
1679 
1680     return sb_breakpoint;
1681 }
1682 
1683 bool
1684 SBTarget::EnableAllBreakpoints ()
1685 {
1686     TargetSP target_sp(GetSP());
1687     if (target_sp)
1688     {
1689         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1690         target_sp->EnableAllBreakpoints ();
1691         return true;
1692     }
1693     return false;
1694 }
1695 
1696 bool
1697 SBTarget::DisableAllBreakpoints ()
1698 {
1699     TargetSP target_sp(GetSP());
1700     if (target_sp)
1701     {
1702         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1703         target_sp->DisableAllBreakpoints ();
1704         return true;
1705     }
1706     return false;
1707 }
1708 
1709 bool
1710 SBTarget::DeleteAllBreakpoints ()
1711 {
1712     TargetSP target_sp(GetSP());
1713     if (target_sp)
1714     {
1715         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1716         target_sp->RemoveAllBreakpoints ();
1717         return true;
1718     }
1719     return false;
1720 }
1721 
1722 uint32_t
1723 SBTarget::GetNumWatchpoints () const
1724 {
1725     TargetSP target_sp(GetSP());
1726     if (target_sp)
1727     {
1728         // The watchpoint list is thread safe, no need to lock
1729         return target_sp->GetWatchpointList().GetSize();
1730     }
1731     return 0;
1732 }
1733 
1734 SBWatchpoint
1735 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
1736 {
1737     SBWatchpoint sb_watchpoint;
1738     TargetSP target_sp(GetSP());
1739     if (target_sp)
1740     {
1741         // The watchpoint list is thread safe, no need to lock
1742         sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
1743     }
1744     return sb_watchpoint;
1745 }
1746 
1747 bool
1748 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
1749 {
1750     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1751 
1752     bool result = false;
1753     TargetSP target_sp(GetSP());
1754     if (target_sp)
1755     {
1756         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1757         Mutex::Locker locker;
1758         target_sp->GetWatchpointList().GetListMutex(locker);
1759         result = target_sp->RemoveWatchpointByID (wp_id);
1760     }
1761 
1762     if (log)
1763     {
1764         log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
1765     }
1766 
1767     return result;
1768 }
1769 
1770 SBWatchpoint
1771 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
1772 {
1773     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1774 
1775     SBWatchpoint sb_watchpoint;
1776     lldb::WatchpointSP watchpoint_sp;
1777     TargetSP target_sp(GetSP());
1778     if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
1779     {
1780         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1781         Mutex::Locker locker;
1782         target_sp->GetWatchpointList().GetListMutex(locker);
1783         watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1784         sb_watchpoint.SetSP (watchpoint_sp);
1785     }
1786 
1787     if (log)
1788     {
1789         log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1790                      target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
1791     }
1792 
1793     return sb_watchpoint;
1794 }
1795 
1796 lldb::SBWatchpoint
1797 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
1798 {
1799     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1800 
1801     SBWatchpoint sb_watchpoint;
1802     lldb::WatchpointSP watchpoint_sp;
1803     TargetSP target_sp(GetSP());
1804     if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
1805     {
1806         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1807         uint32_t watch_type = 0;
1808         if (read)
1809             watch_type |= LLDB_WATCH_TYPE_READ;
1810         if (write)
1811             watch_type |= LLDB_WATCH_TYPE_WRITE;
1812         if (watch_type == 0)
1813         {
1814             error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1815             return sb_watchpoint;
1816         }
1817 
1818         // Target::CreateWatchpoint() is thread safe.
1819         Error cw_error;
1820         // This API doesn't take in a type, so we can't figure out what it is.
1821         ClangASTType *type = NULL;
1822         watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1823         error.SetError(cw_error);
1824         sb_watchpoint.SetSP (watchpoint_sp);
1825     }
1826 
1827     if (log)
1828     {
1829         log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
1830                      target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
1831     }
1832 
1833     return sb_watchpoint;
1834 }
1835 
1836 bool
1837 SBTarget::EnableAllWatchpoints ()
1838 {
1839     TargetSP target_sp(GetSP());
1840     if (target_sp)
1841     {
1842         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1843         Mutex::Locker locker;
1844         target_sp->GetWatchpointList().GetListMutex(locker);
1845         target_sp->EnableAllWatchpoints ();
1846         return true;
1847     }
1848     return false;
1849 }
1850 
1851 bool
1852 SBTarget::DisableAllWatchpoints ()
1853 {
1854     TargetSP target_sp(GetSP());
1855     if (target_sp)
1856     {
1857         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1858         Mutex::Locker locker;
1859         target_sp->GetWatchpointList().GetListMutex(locker);
1860         target_sp->DisableAllWatchpoints ();
1861         return true;
1862     }
1863     return false;
1864 }
1865 
1866 SBValue
1867 SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1868 {
1869     SBValue sb_value;
1870     lldb::ValueObjectSP new_value_sp;
1871     if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1872     {
1873         lldb::addr_t address(addr.GetLoadAddress(*this));
1874         lldb::TypeImplSP type_impl_sp (type.GetSP());
1875         ClangASTType pointer_ast_type(type_impl_sp->GetClangASTType().GetPointerType ());
1876         if (pointer_ast_type)
1877         {
1878             lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
1879 
1880             ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1881             ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
1882                                                                                pointer_ast_type,
1883                                                                                ConstString(name),
1884                                                                                buffer,
1885                                                                                exe_ctx.GetByteOrder(),
1886                                                                                exe_ctx.GetAddressByteSize()));
1887 
1888             if (ptr_result_valobj_sp)
1889             {
1890                 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
1891                 Error err;
1892                 new_value_sp = ptr_result_valobj_sp->Dereference(err);
1893                 if (new_value_sp)
1894                     new_value_sp->SetName(ConstString(name));
1895             }
1896         }
1897     }
1898     sb_value.SetSP(new_value_sp);
1899     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1900     if (log)
1901     {
1902         if (new_value_sp)
1903             log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"", m_opaque_sp.get(), new_value_sp->GetName().AsCString());
1904         else
1905             log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL", m_opaque_sp.get());
1906     }
1907     return sb_value;
1908 }
1909 
1910 bool
1911 SBTarget::DeleteAllWatchpoints ()
1912 {
1913     TargetSP target_sp(GetSP());
1914     if (target_sp)
1915     {
1916         Mutex::Locker api_locker (target_sp->GetAPIMutex());
1917         Mutex::Locker locker;
1918         target_sp->GetWatchpointList().GetListMutex(locker);
1919         target_sp->RemoveAllWatchpoints ();
1920         return true;
1921     }
1922     return false;
1923 }
1924 
1925 
1926 lldb::SBModule
1927 SBTarget::AddModule (const char *path,
1928                      const char *triple,
1929                      const char *uuid_cstr)
1930 {
1931     return AddModule (path, triple, uuid_cstr, NULL);
1932 }
1933 
1934 lldb::SBModule
1935 SBTarget::AddModule (const char *path,
1936                      const char *triple,
1937                      const char *uuid_cstr,
1938                      const char *symfile)
1939 {
1940     lldb::SBModule sb_module;
1941     TargetSP target_sp(GetSP());
1942     if (target_sp)
1943     {
1944         ModuleSpec module_spec;
1945         if (path)
1946             module_spec.GetFileSpec().SetFile(path, false);
1947 
1948         if (uuid_cstr)
1949             module_spec.GetUUID().SetFromCString(uuid_cstr);
1950 
1951         if (triple)
1952             module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1953         else
1954             module_spec.GetArchitecture() = target_sp->GetArchitecture();
1955 
1956         if (symfile)
1957             module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
1958 
1959         sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1960     }
1961     return sb_module;
1962 }
1963 
1964 lldb::SBModule
1965 SBTarget::AddModule (const SBModuleSpec &module_spec)
1966 {
1967     lldb::SBModule sb_module;
1968     TargetSP target_sp(GetSP());
1969     if (target_sp)
1970         sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1971     return sb_module;
1972 }
1973 
1974 bool
1975 SBTarget::AddModule (lldb::SBModule &module)
1976 {
1977     TargetSP target_sp(GetSP());
1978     if (target_sp)
1979     {
1980         target_sp->GetImages().AppendIfNeeded (module.GetSP());
1981         return true;
1982     }
1983     return false;
1984 }
1985 
1986 uint32_t
1987 SBTarget::GetNumModules () const
1988 {
1989     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1990 
1991     uint32_t num = 0;
1992     TargetSP target_sp(GetSP());
1993     if (target_sp)
1994     {
1995         // The module list is thread safe, no need to lock
1996         num = target_sp->GetImages().GetSize();
1997     }
1998 
1999     if (log)
2000         log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
2001 
2002     return num;
2003 }
2004 
2005 void
2006 SBTarget::Clear ()
2007 {
2008     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2009 
2010     if (log)
2011         log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
2012 
2013     m_opaque_sp.reset();
2014 }
2015 
2016 
2017 SBModule
2018 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
2019 {
2020     SBModule sb_module;
2021     TargetSP target_sp(GetSP());
2022     if (target_sp && sb_file_spec.IsValid())
2023     {
2024         ModuleSpec module_spec(*sb_file_spec);
2025         // The module list is thread safe, no need to lock
2026         sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
2027     }
2028     return sb_module;
2029 }
2030 
2031 lldb::ByteOrder
2032 SBTarget::GetByteOrder ()
2033 {
2034     TargetSP target_sp(GetSP());
2035     if (target_sp)
2036         return target_sp->GetArchitecture().GetByteOrder();
2037     return eByteOrderInvalid;
2038 }
2039 
2040 const char *
2041 SBTarget::GetTriple ()
2042 {
2043     TargetSP target_sp(GetSP());
2044     if (target_sp)
2045     {
2046         std::string triple (target_sp->GetArchitecture().GetTriple().str());
2047         // Unique the string so we don't run into ownership issues since
2048         // the const strings put the string into the string pool once and
2049         // the strings never comes out
2050         ConstString const_triple (triple.c_str());
2051         return const_triple.GetCString();
2052     }
2053     return NULL;
2054 }
2055 
2056 uint32_t
2057 SBTarget::GetAddressByteSize()
2058 {
2059     TargetSP target_sp(GetSP());
2060     if (target_sp)
2061         return target_sp->GetArchitecture().GetAddressByteSize();
2062     return sizeof(void*);
2063 }
2064 
2065 
2066 SBModule
2067 SBTarget::GetModuleAtIndex (uint32_t idx)
2068 {
2069     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2070 
2071     SBModule sb_module;
2072     ModuleSP module_sp;
2073     TargetSP target_sp(GetSP());
2074     if (target_sp)
2075     {
2076         // The module list is thread safe, no need to lock
2077         module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2078         sb_module.SetSP (module_sp);
2079     }
2080 
2081     if (log)
2082     {
2083         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2084                      target_sp.get(), idx, module_sp.get());
2085     }
2086 
2087     return sb_module;
2088 }
2089 
2090 bool
2091 SBTarget::RemoveModule (lldb::SBModule module)
2092 {
2093     TargetSP target_sp(GetSP());
2094     if (target_sp)
2095         return target_sp->GetImages().Remove(module.GetSP());
2096     return false;
2097 }
2098 
2099 
2100 SBBroadcaster
2101 SBTarget::GetBroadcaster () const
2102 {
2103     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2104 
2105     TargetSP target_sp(GetSP());
2106     SBBroadcaster broadcaster(target_sp.get(), false);
2107 
2108     if (log)
2109         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2110                      target_sp.get(), broadcaster.get());
2111 
2112     return broadcaster;
2113 }
2114 
2115 bool
2116 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
2117 {
2118     Stream &strm = description.ref();
2119 
2120     TargetSP target_sp(GetSP());
2121     if (target_sp)
2122     {
2123         target_sp->Dump (&strm, description_level);
2124     }
2125     else
2126         strm.PutCString ("No value");
2127 
2128     return true;
2129 }
2130 
2131 lldb::SBSymbolContextList
2132 SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
2133 {
2134     lldb::SBSymbolContextList sb_sc_list;
2135     if (name && name[0])
2136     {
2137         TargetSP target_sp(GetSP());
2138         if (target_sp)
2139         {
2140             const bool symbols_ok = true;
2141             const bool inlines_ok = true;
2142             const bool append = true;
2143             target_sp->GetImages().FindFunctions (ConstString(name),
2144                                                   name_type_mask,
2145                                                   symbols_ok,
2146                                                   inlines_ok,
2147                                                   append,
2148                                                   *sb_sc_list);
2149         }
2150     }
2151     return sb_sc_list;
2152 }
2153 
2154 lldb::SBType
2155 SBTarget::FindFirstType (const char* typename_cstr)
2156 {
2157     TargetSP target_sp(GetSP());
2158     if (typename_cstr && typename_cstr[0] && target_sp)
2159     {
2160         ConstString const_typename(typename_cstr);
2161         SymbolContext sc;
2162         const bool exact_match = false;
2163 
2164         const ModuleList &module_list = target_sp->GetImages();
2165         size_t count = module_list.GetSize();
2166         for (size_t idx = 0; idx < count; idx++)
2167         {
2168             ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2169             if (module_sp)
2170             {
2171                 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2172                 if (type_sp)
2173                     return SBType(type_sp);
2174             }
2175         }
2176 
2177         // Didn't find the type in the symbols; try the Objective-C runtime
2178         // if one is installed
2179 
2180         ProcessSP process_sp(target_sp->GetProcessSP());
2181 
2182         if (process_sp)
2183         {
2184             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2185 
2186             if (objc_language_runtime)
2187             {
2188                 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2189 
2190                 if (objc_type_vendor)
2191                 {
2192                     std::vector <ClangASTType> types;
2193 
2194                     if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2195                         return SBType(types[0]);
2196                 }
2197             }
2198         }
2199 
2200         // No matches, search for basic typename matches
2201         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2202         if (clang_ast)
2203             return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
2204     }
2205     return SBType();
2206 }
2207 
2208 SBType
2209 SBTarget::GetBasicType(lldb::BasicType type)
2210 {
2211     TargetSP target_sp(GetSP());
2212     if (target_sp)
2213     {
2214         ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2215         if (clang_ast)
2216             return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
2217     }
2218     return SBType();
2219 }
2220 
2221 
2222 lldb::SBTypeList
2223 SBTarget::FindTypes (const char* typename_cstr)
2224 {
2225     SBTypeList sb_type_list;
2226     TargetSP target_sp(GetSP());
2227     if (typename_cstr && typename_cstr[0] && target_sp)
2228     {
2229         ModuleList& images = target_sp->GetImages();
2230         ConstString const_typename(typename_cstr);
2231         bool exact_match = false;
2232         SymbolContext sc;
2233         TypeList type_list;
2234 
2235         uint32_t num_matches = images.FindTypes (sc,
2236                                                  const_typename,
2237                                                  exact_match,
2238                                                  UINT32_MAX,
2239                                                  type_list);
2240 
2241         if (num_matches > 0)
2242         {
2243             for (size_t idx = 0; idx < num_matches; idx++)
2244             {
2245                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2246                 if (type_sp)
2247                     sb_type_list.Append(SBType(type_sp));
2248             }
2249         }
2250 
2251         // Try the Objective-C runtime if one is installed
2252 
2253         ProcessSP process_sp(target_sp->GetProcessSP());
2254 
2255         if (process_sp)
2256         {
2257             ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2258 
2259             if (objc_language_runtime)
2260             {
2261                 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2262 
2263                 if (objc_type_vendor)
2264                 {
2265                     std::vector <ClangASTType> types;
2266 
2267                     if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2268                     {
2269                         for (ClangASTType &type : types)
2270                         {
2271                             sb_type_list.Append(SBType(type));
2272                         }
2273                     }
2274                 }
2275             }
2276         }
2277 
2278         if (sb_type_list.GetSize() == 0)
2279         {
2280             // No matches, search for basic typename matches
2281             ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2282             if (clang_ast)
2283                 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
2284         }
2285     }
2286     return sb_type_list;
2287 }
2288 
2289 SBValueList
2290 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2291 {
2292     SBValueList sb_value_list;
2293 
2294     TargetSP target_sp(GetSP());
2295     if (name && target_sp)
2296     {
2297         VariableList variable_list;
2298         const bool append = true;
2299         const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2300                                                                                  append,
2301                                                                                  max_matches,
2302                                                                                  variable_list);
2303 
2304         if (match_count > 0)
2305         {
2306             ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2307             if (exe_scope == NULL)
2308                 exe_scope = target_sp.get();
2309             for (uint32_t i=0; i<match_count; ++i)
2310             {
2311                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2312                 if (valobj_sp)
2313                     sb_value_list.Append(SBValue(valobj_sp));
2314             }
2315         }
2316     }
2317 
2318     return sb_value_list;
2319 }
2320 
2321 lldb::SBValue
2322 SBTarget::FindFirstGlobalVariable (const char* name)
2323 {
2324     SBValueList sb_value_list(FindGlobalVariables(name, 1));
2325     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2326         return sb_value_list.GetValueAtIndex(0);
2327     return SBValue();
2328 }
2329 
2330 SBSourceManager
2331 SBTarget::GetSourceManager()
2332 {
2333     SBSourceManager source_manager (*this);
2334     return source_manager;
2335 }
2336 
2337 lldb::SBInstructionList
2338 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2339 {
2340     return ReadInstructions (base_addr, count, NULL);
2341 }
2342 
2343 lldb::SBInstructionList
2344 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2345 {
2346     SBInstructionList sb_instructions;
2347 
2348     TargetSP target_sp(GetSP());
2349     if (target_sp)
2350     {
2351         Address *addr_ptr = base_addr.get();
2352 
2353         if (addr_ptr)
2354         {
2355             DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2356             bool prefer_file_cache = false;
2357             lldb_private::Error error;
2358             lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2359             const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2360                                                             prefer_file_cache,
2361                                                             data.GetBytes(),
2362                                                             data.GetByteSize(),
2363                                                             error,
2364                                                             &load_addr);
2365             const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2366             sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2367                                                                              NULL,
2368                                                                              flavor_string,
2369                                                                              *addr_ptr,
2370                                                                              data.GetBytes(),
2371                                                                              bytes_read,
2372                                                                              count,
2373                                                                              data_from_file));
2374         }
2375     }
2376 
2377     return sb_instructions;
2378 
2379 }
2380 
2381 lldb::SBInstructionList
2382 SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2383 {
2384     return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2385 }
2386 
2387 lldb::SBInstructionList
2388 SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2389 {
2390     SBInstructionList sb_instructions;
2391 
2392     TargetSP target_sp(GetSP());
2393     if (target_sp)
2394     {
2395         Address addr;
2396 
2397         if (base_addr.get())
2398             addr = *base_addr.get();
2399 
2400         const bool data_from_file = true;
2401 
2402         sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2403                                                                          NULL,
2404                                                                          flavor_string,
2405                                                                          addr,
2406                                                                          buf,
2407                                                                          size,
2408                                                                          UINT32_MAX,
2409                                                                          data_from_file));
2410     }
2411 
2412     return sb_instructions;
2413 }
2414 
2415 lldb::SBInstructionList
2416 SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2417 {
2418     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2419 }
2420 
2421 lldb::SBInstructionList
2422 SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2423 {
2424     return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
2425 }
2426 
2427 SBError
2428 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2429                                  lldb::addr_t section_base_addr)
2430 {
2431     SBError sb_error;
2432     TargetSP target_sp(GetSP());
2433     if (target_sp)
2434     {
2435         if (!section.IsValid())
2436         {
2437             sb_error.SetErrorStringWithFormat ("invalid section");
2438         }
2439         else
2440         {
2441             SectionSP section_sp (section.GetSP());
2442             if (section_sp)
2443             {
2444                 if (section_sp->IsThreadSpecific())
2445                 {
2446                     sb_error.SetErrorString ("thread specific sections are not yet supported");
2447                 }
2448                 else
2449                 {
2450                     if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2451                     {
2452                         // Flush info in the process (stack frames, etc)
2453                         ProcessSP process_sp (target_sp->GetProcessSP());
2454                         if (process_sp)
2455                             process_sp->Flush();
2456                     }
2457                 }
2458             }
2459         }
2460     }
2461     else
2462     {
2463         sb_error.SetErrorString ("invalid target");
2464     }
2465     return sb_error;
2466 }
2467 
2468 SBError
2469 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2470 {
2471     SBError sb_error;
2472 
2473     TargetSP target_sp(GetSP());
2474     if (target_sp)
2475     {
2476         if (!section.IsValid())
2477         {
2478             sb_error.SetErrorStringWithFormat ("invalid section");
2479         }
2480         else
2481         {
2482             if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
2483             {
2484                 // Flush info in the process (stack frames, etc)
2485                 ProcessSP process_sp (target_sp->GetProcessSP());
2486                 if (process_sp)
2487                     process_sp->Flush();
2488             }
2489         }
2490     }
2491     else
2492     {
2493         sb_error.SetErrorStringWithFormat ("invalid target");
2494     }
2495     return sb_error;
2496 }
2497 
2498 SBError
2499 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2500 {
2501     SBError sb_error;
2502 
2503     TargetSP target_sp(GetSP());
2504     if (target_sp)
2505     {
2506         ModuleSP module_sp (module.GetSP());
2507         if (module_sp)
2508         {
2509             bool changed = false;
2510             if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
2511             {
2512                 // The load was successful, make sure that at least some sections
2513                 // changed before we notify that our module was loaded.
2514                 if (changed)
2515                 {
2516                     ModuleList module_list;
2517                     module_list.Append(module_sp);
2518                     target_sp->ModulesDidLoad (module_list);
2519                     // Flush info in the process (stack frames, etc)
2520                     ProcessSP process_sp (target_sp->GetProcessSP());
2521                     if (process_sp)
2522                         process_sp->Flush();
2523                 }
2524             }
2525         }
2526         else
2527         {
2528             sb_error.SetErrorStringWithFormat ("invalid module");
2529         }
2530 
2531     }
2532     else
2533     {
2534         sb_error.SetErrorStringWithFormat ("invalid target");
2535     }
2536     return sb_error;
2537 }
2538 
2539 SBError
2540 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2541 {
2542     SBError sb_error;
2543 
2544     char path[PATH_MAX];
2545     TargetSP target_sp(GetSP());
2546     if (target_sp)
2547     {
2548         ModuleSP module_sp (module.GetSP());
2549         if (module_sp)
2550         {
2551             ObjectFile *objfile = module_sp->GetObjectFile();
2552             if (objfile)
2553             {
2554                 SectionList *section_list = objfile->GetSectionList();
2555                 if (section_list)
2556                 {
2557                     bool changed = false;
2558                     const size_t num_sections = section_list->GetSize();
2559                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2560                     {
2561                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2562                         if (section_sp)
2563                             changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2564                     }
2565                     if (changed)
2566                     {
2567                         // Flush info in the process (stack frames, etc)
2568                         ProcessSP process_sp (target_sp->GetProcessSP());
2569                         if (process_sp)
2570                             process_sp->Flush();
2571                     }
2572                 }
2573                 else
2574                 {
2575                     module_sp->GetFileSpec().GetPath (path, sizeof(path));
2576                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2577                 }
2578             }
2579             else
2580             {
2581                 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2582                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2583             }
2584         }
2585         else
2586         {
2587             sb_error.SetErrorStringWithFormat ("invalid module");
2588         }
2589     }
2590     else
2591     {
2592         sb_error.SetErrorStringWithFormat ("invalid target");
2593     }
2594     return sb_error;
2595 }
2596 
2597 
2598 lldb::SBSymbolContextList
2599 SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2600 {
2601     SBSymbolContextList sb_sc_list;
2602     if (name && name[0])
2603     {
2604         TargetSP target_sp(GetSP());
2605         if (target_sp)
2606         {
2607             bool append = true;
2608             target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2609                                                                symbol_type,
2610                                                                *sb_sc_list,
2611                                                                append);
2612         }
2613     }
2614     return sb_sc_list;
2615 
2616 }
2617 
2618 
2619 lldb::SBValue
2620 SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2621 {
2622     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2623     Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2624     SBValue expr_result;
2625     ExecutionResults exe_results = eExecutionSetupError;
2626     ValueObjectSP expr_value_sp;
2627     TargetSP target_sp(GetSP());
2628     StackFrame *frame = NULL;
2629     if (target_sp)
2630     {
2631         if (expr == NULL || expr[0] == '\0')
2632         {
2633             if (log)
2634                 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2635             return expr_result;
2636         }
2637 
2638         Mutex::Locker api_locker (target_sp->GetAPIMutex());
2639         ExecutionContext exe_ctx (m_opaque_sp.get());
2640 
2641         if (log)
2642             log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2643 
2644         frame = exe_ctx.GetFramePtr();
2645         Target *target = exe_ctx.GetTargetPtr();
2646 
2647         if (target)
2648         {
2649 #ifdef LLDB_CONFIGURATION_DEBUG
2650             StreamString frame_description;
2651             if (frame)
2652                 frame->DumpUsingSettingsFormat (&frame_description);
2653             Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2654                                                  expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2655 #endif
2656             exe_results = target->EvaluateExpression (expr,
2657                                                       frame,
2658                                                       expr_value_sp,
2659                                                       options.ref());
2660 
2661             expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2662 #ifdef LLDB_CONFIGURATION_DEBUG
2663             Host::SetCrashDescription (NULL);
2664 #endif
2665         }
2666         else
2667         {
2668             if (log)
2669                 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2670         }
2671     }
2672 #ifndef LLDB_DISABLE_PYTHON
2673     if (expr_log)
2674         expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2675                          expr_result.GetValue(),
2676                          expr_result.GetSummary());
2677 
2678     if (log)
2679         log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2680                      frame,
2681                      expr,
2682                      expr_value_sp.get(),
2683                      exe_results);
2684 #endif
2685 
2686     return expr_result;
2687 }
2688 
2689 
2690 lldb::addr_t
2691 SBTarget::GetStackRedZoneSize()
2692 {
2693     TargetSP target_sp(GetSP());
2694     if (target_sp)
2695     {
2696         ABISP abi_sp;
2697         ProcessSP process_sp (target_sp->GetProcessSP());
2698         if (process_sp)
2699             abi_sp = process_sp->GetABI();
2700         else
2701             abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2702         if (abi_sp)
2703             return abi_sp->GetRedZoneSize();
2704     }
2705     return 0;
2706 }
2707 
2708