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