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