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