1 //===-- SBTarget.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/API/SBTarget.h"
14 #include "lldb/lldb-public.h"
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/ValueObjectList.h"
45 #include "lldb/Core/ValueObjectVariable.h"
46 #include "lldb/Host/FileSpec.h"
47 #include "lldb/Host/Host.h"
48 #include "lldb/Interpreter/Args.h"
49 #include "lldb/Symbol/ObjectFile.h"
50 #include "lldb/Symbol/SymbolVendor.h"
51 #include "lldb/Symbol/VariableList.h"
52 #include "lldb/Target/LanguageRuntime.h"
53 #include "lldb/Target/Process.h"
54 #include "lldb/Target/Target.h"
55 #include "lldb/Target/TargetList.h"
57 #include "lldb/Interpreter/CommandReturnObject.h"
58 #include "../source/Commands/CommandObjectBreakpoint.h"
62 using namespace lldb_private;
64 #define DEFAULT_DISASM_BYTE_SIZE 32
66 SBLaunchInfo::SBLaunchInfo (const char **argv) :
67 m_opaque_sp(new ProcessLaunchInfo())
69 m_opaque_sp->GetFlags().Reset (eLaunchFlagDebug | eLaunchFlagDisableASLR);
71 m_opaque_sp->GetArguments().SetArguments(argv);
74 SBLaunchInfo::~SBLaunchInfo()
78 lldb_private::ProcessLaunchInfo &
86 SBLaunchInfo::GetUserID()
88 return m_opaque_sp->GetUserID();
92 SBLaunchInfo::GetGroupID()
94 return m_opaque_sp->GetGroupID();
98 SBLaunchInfo::UserIDIsValid ()
100 return m_opaque_sp->UserIDIsValid();
104 SBLaunchInfo::GroupIDIsValid ()
106 return m_opaque_sp->GroupIDIsValid();
110 SBLaunchInfo::SetUserID (uint32_t uid)
112 m_opaque_sp->SetUserID (uid);
116 SBLaunchInfo::SetGroupID (uint32_t gid)
118 m_opaque_sp->SetGroupID (gid);
122 SBLaunchInfo::GetNumArguments ()
124 return m_opaque_sp->GetArguments().GetArgumentCount();
128 SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
130 return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
134 SBLaunchInfo::SetArguments (const char **argv, bool append)
139 m_opaque_sp->GetArguments().AppendArguments(argv);
144 m_opaque_sp->GetArguments().SetArguments(argv);
146 m_opaque_sp->GetArguments().Clear();
151 SBLaunchInfo::GetNumEnvironmentEntries ()
153 return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
157 SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
159 return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
163 SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
168 m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
173 m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
175 m_opaque_sp->GetEnvironmentEntries().Clear();
180 SBLaunchInfo::Clear ()
182 m_opaque_sp->Clear();
186 SBLaunchInfo::GetWorkingDirectory () const
188 return m_opaque_sp->GetWorkingDirectory();
192 SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
194 m_opaque_sp->SetWorkingDirectory(working_dir);
198 SBLaunchInfo::GetLaunchFlags ()
200 return m_opaque_sp->GetFlags().Get();
204 SBLaunchInfo::SetLaunchFlags (uint32_t flags)
206 m_opaque_sp->GetFlags().Reset(flags);
210 SBLaunchInfo::GetProcessPluginName ()
212 return m_opaque_sp->GetProcessPluginName();
216 SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
218 return m_opaque_sp->SetProcessPluginName (plugin_name);
222 SBLaunchInfo::GetShell ()
224 return m_opaque_sp->GetShell();
228 SBLaunchInfo::SetShell (const char * path)
230 m_opaque_sp->SetShell (path);
234 SBLaunchInfo::GetResumeCount ()
236 return m_opaque_sp->GetResumeCount();
240 SBLaunchInfo::SetResumeCount (uint32_t c)
242 m_opaque_sp->SetResumeCount (c);
246 SBLaunchInfo::AddCloseFileAction (int fd)
248 return m_opaque_sp->AppendCloseFileAction(fd);
252 SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
254 return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
258 SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
260 return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
264 SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
266 return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
270 SBAttachInfo::SBAttachInfo () :
271 m_opaque_sp (new ProcessAttachInfo())
275 SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
276 m_opaque_sp (new ProcessAttachInfo())
278 m_opaque_sp->SetProcessID (pid);
281 SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
282 m_opaque_sp (new ProcessAttachInfo())
285 m_opaque_sp->GetExecutableFile().SetFile(path, false);
286 m_opaque_sp->SetWaitForLaunch (wait_for);
289 SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
290 m_opaque_sp (new ProcessAttachInfo())
292 *m_opaque_sp = *rhs.m_opaque_sp;
295 SBAttachInfo::~SBAttachInfo()
299 lldb_private::ProcessAttachInfo &
306 SBAttachInfo::operator = (const SBAttachInfo &rhs)
309 *m_opaque_sp = *rhs.m_opaque_sp;
314 SBAttachInfo::GetProcessID ()
316 return m_opaque_sp->GetProcessID();
320 SBAttachInfo::SetProcessID (lldb::pid_t pid)
322 m_opaque_sp->SetProcessID (pid);
327 SBAttachInfo::GetResumeCount ()
329 return m_opaque_sp->GetResumeCount();
333 SBAttachInfo::SetResumeCount (uint32_t c)
335 m_opaque_sp->SetResumeCount (c);
339 SBAttachInfo::GetProcessPluginName ()
341 return m_opaque_sp->GetProcessPluginName();
345 SBAttachInfo::SetProcessPluginName (const char *plugin_name)
347 return m_opaque_sp->SetProcessPluginName (plugin_name);
351 SBAttachInfo::SetExecutable (const char *path)
354 m_opaque_sp->GetExecutableFile().SetFile(path, false);
356 m_opaque_sp->GetExecutableFile().Clear();
360 SBAttachInfo::SetExecutable (SBFileSpec exe_file)
362 if (exe_file.IsValid())
363 m_opaque_sp->GetExecutableFile() = exe_file.ref();
365 m_opaque_sp->GetExecutableFile().Clear();
369 SBAttachInfo::GetWaitForLaunch ()
371 return m_opaque_sp->GetWaitForLaunch();
375 SBAttachInfo::SetWaitForLaunch (bool b)
377 m_opaque_sp->SetWaitForLaunch (b);
381 SBAttachInfo::GetIgnoreExisting ()
383 return m_opaque_sp->GetIgnoreExisting();
387 SBAttachInfo::SetIgnoreExisting (bool b)
389 m_opaque_sp->SetIgnoreExisting (b);
393 SBAttachInfo::GetUserID()
395 return m_opaque_sp->GetUserID();
399 SBAttachInfo::GetGroupID()
401 return m_opaque_sp->GetGroupID();
405 SBAttachInfo::UserIDIsValid ()
407 return m_opaque_sp->UserIDIsValid();
411 SBAttachInfo::GroupIDIsValid ()
413 return m_opaque_sp->GroupIDIsValid();
417 SBAttachInfo::SetUserID (uint32_t uid)
419 m_opaque_sp->SetUserID (uid);
423 SBAttachInfo::SetGroupID (uint32_t gid)
425 m_opaque_sp->SetGroupID (gid);
429 SBAttachInfo::GetEffectiveUserID()
431 return m_opaque_sp->GetEffectiveUserID();
435 SBAttachInfo::GetEffectiveGroupID()
437 return m_opaque_sp->GetEffectiveGroupID();
441 SBAttachInfo::EffectiveUserIDIsValid ()
443 return m_opaque_sp->EffectiveUserIDIsValid();
447 SBAttachInfo::EffectiveGroupIDIsValid ()
449 return m_opaque_sp->EffectiveGroupIDIsValid ();
453 SBAttachInfo::SetEffectiveUserID (uint32_t uid)
455 m_opaque_sp->SetEffectiveUserID(uid);
459 SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
461 m_opaque_sp->SetEffectiveGroupID(gid);
465 SBAttachInfo::GetParentProcessID ()
467 return m_opaque_sp->GetParentProcessID();
471 SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
473 m_opaque_sp->SetParentProcessID (pid);
477 SBAttachInfo::ParentProcessIDIsValid()
479 return m_opaque_sp->ParentProcessIDIsValid();
483 //----------------------------------------------------------------------
484 // SBTarget constructor
485 //----------------------------------------------------------------------
486 SBTarget::SBTarget () :
491 SBTarget::SBTarget (const SBTarget& rhs) :
492 m_opaque_sp (rhs.m_opaque_sp)
496 SBTarget::SBTarget(const TargetSP& target_sp) :
497 m_opaque_sp (target_sp)
502 SBTarget::operator = (const SBTarget& rhs)
505 m_opaque_sp = rhs.m_opaque_sp;
509 //----------------------------------------------------------------------
511 //----------------------------------------------------------------------
512 SBTarget::~SBTarget()
517 SBTarget::GetBroadcasterClassName ()
519 return Target::GetStaticBroadcasterClass().AsCString();
523 SBTarget::IsValid () const
525 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
529 SBTarget::GetProcess ()
531 SBProcess sb_process;
532 ProcessSP process_sp;
533 TargetSP target_sp(GetSP());
536 process_sp = target_sp->GetProcessSP();
537 sb_process.SetSP (process_sp);
540 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
543 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
544 target_sp.get(), process_sp.get());
551 SBTarget::GetDebugger () const
554 TargetSP target_sp(GetSP());
556 debugger.reset (target_sp->GetDebugger().shared_from_this());
561 SBTarget::LoadCore (const char *core_file)
563 SBProcess sb_process;
564 TargetSP target_sp(GetSP());
567 FileSpec filespec(core_file, true);
568 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
573 process_sp->LoadCore();
574 sb_process.SetSP (process_sp);
581 SBTarget::LaunchSimple
585 const char *working_directory
588 char *stdin_path = NULL;
589 char *stdout_path = NULL;
590 char *stderr_path = NULL;
591 uint32_t launch_flags = 0;
592 bool stop_at_entry = false;
594 SBListener listener = GetDebugger().GetListener();
595 return Launch (listener,
610 SBListener &listener,
613 const char *stdin_path,
614 const char *stdout_path,
615 const char *stderr_path,
616 const char *working_directory,
617 uint32_t launch_flags, // See LaunchFlags
622 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
624 SBProcess sb_process;
625 ProcessSP process_sp;
626 TargetSP target_sp(GetSP());
630 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))...",
634 stdin_path ? stdin_path : "NULL",
635 stdout_path ? stdout_path : "NULL",
636 stderr_path ? stderr_path : "NULL",
637 working_directory ? working_directory : "NULL",
645 Mutex::Locker api_locker (target_sp->GetAPIMutex());
647 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
648 launch_flags |= eLaunchFlagDisableASLR;
650 StateType state = eStateInvalid;
651 process_sp = target_sp->GetProcessSP();
654 state = process_sp->GetState();
656 if (process_sp->IsAlive() && state != eStateConnected)
658 if (state == eStateAttaching)
659 error.SetErrorString ("process attach is in progress");
661 error.SetErrorString ("a process is already being debugged");
666 if (state == eStateConnected)
668 // If we are already connected, then we have already specified the
669 // listener, so if a valid listener is supplied, we need to error out
670 // to let the client know.
671 if (listener.IsValid())
673 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
679 if (listener.IsValid())
680 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
682 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
687 sb_process.SetSP (process_sp);
688 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
689 launch_flags |= eLaunchFlagDisableSTDIO;
691 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
693 Module *exe_module = target_sp->GetExecutableModulePointer();
695 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
697 launch_info.GetArguments().AppendArguments (argv);
699 launch_info.GetEnvironmentEntries ().SetArguments (envp);
701 error.SetError (process_sp->Launch (launch_info));
704 // We we are stopping at the entry point, we can return now!
708 // Make sure we are stopped at the entry
709 StateType state = process_sp->WaitForProcessToStop (NULL);
710 if (state == eStateStopped)
712 // resume the process to skip the entry point
713 error.SetError (process_sp->Resume());
716 // If we are doing synchronous mode, then wait for the
717 // process to stop yet again!
718 if (target_sp->GetDebugger().GetAsyncExecution () == false)
719 process_sp->WaitForProcessToStop (NULL);
726 error.SetErrorString ("unable to create lldb_private::Process");
731 error.SetErrorString ("SBTarget is invalid");
734 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
737 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
738 target_sp.get(), process_sp.get());
745 SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
747 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
749 SBProcess sb_process;
750 ProcessSP process_sp;
751 TargetSP target_sp(GetSP());
755 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...", target_sp.get());
760 Mutex::Locker api_locker (target_sp->GetAPIMutex());
761 StateType state = eStateInvalid;
762 process_sp = target_sp->GetProcessSP();
765 state = process_sp->GetState();
767 if (process_sp->IsAlive() && state != eStateConnected)
769 if (state == eStateAttaching)
770 error.SetErrorString ("process attach is in progress");
772 error.SetErrorString ("a process is already being debugged");
777 if (state != eStateConnected)
778 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
782 sb_process.SetSP (process_sp);
783 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
785 Module *exe_module = target_sp->GetExecutableModulePointer();
787 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
789 const ArchSpec &arch_spec = target_sp->GetArchitecture();
790 if (arch_spec.IsValid())
791 launch_info.GetArchitecture () = arch_spec;
793 error.SetError (process_sp->Launch (launch_info));
794 const bool synchronous_execution = target_sp->GetDebugger().GetAsyncExecution () == false;
797 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
799 // If we are doing synchronous mode, then wait for the initial
800 // stop to happen, else, return and let the caller watch for
802 if (synchronous_execution)
803 process_sp->WaitForProcessToStop (NULL);
804 // We we are stopping at the entry point, we can return now!
808 // Make sure we are stopped at the entry
809 StateType state = process_sp->WaitForProcessToStop (NULL);
810 if (state == eStateStopped)
812 // resume the process to skip the entry point
813 error.SetError (process_sp->Resume());
816 // If we are doing synchronous mode, then wait for the
817 // process to stop yet again!
818 if (synchronous_execution)
819 process_sp->WaitForProcessToStop (NULL);
826 error.SetErrorString ("unable to create lldb_private::Process");
831 error.SetErrorString ("SBTarget is invalid");
834 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
837 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
838 target_sp.get(), process_sp.get());
845 SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
847 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
849 SBProcess sb_process;
850 ProcessSP process_sp;
851 TargetSP target_sp(GetSP());
855 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...", target_sp.get());
860 Mutex::Locker api_locker (target_sp->GetAPIMutex());
862 StateType state = eStateInvalid;
863 process_sp = target_sp->GetProcessSP();
866 state = process_sp->GetState();
868 if (process_sp->IsAlive() && state != eStateConnected)
870 if (state == eStateAttaching)
871 error.SetErrorString ("process attach is in progress");
873 error.SetErrorString ("a process is already being debugged");
876 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
877 target_sp.get(), error.GetCString());
883 if (state != eStateConnected)
884 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
888 ProcessAttachInfo &attach_info = sb_attach_info.ref();
889 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
891 PlatformSP platform_sp = target_sp->GetPlatform();
892 // See if we can pre-verify if a process exists or not
893 if (platform_sp && platform_sp->IsConnected())
895 lldb::pid_t attach_pid = attach_info.GetProcessID();
896 ProcessInstanceInfo instance_info;
897 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
899 attach_info.SetUserID(instance_info.GetEffectiveUserID());
903 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
906 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
907 target_sp.get(), error.GetCString());
913 error.SetError (process_sp->Attach (attach_info));
916 sb_process.SetSP (process_sp);
917 // If we are doing synchronous mode, then wait for the
919 if (target_sp->GetDebugger().GetAsyncExecution () == false)
920 process_sp->WaitForProcessToStop (NULL);
925 error.SetErrorString ("unable to create lldb_private::Process");
930 error.SetErrorString ("SBTarget is invalid");
935 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
936 target_sp.get(), process_sp.get());
943 #if defined(__APPLE__)
946 SBTarget::AttachToProcessWithID (SBListener &listener,
948 lldb::SBError& error)
950 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
953 #endif // #if defined(__APPLE__)
956 SBTarget::AttachToProcessWithID
958 SBListener &listener,
959 lldb::pid_t pid,// The process ID to attach to
960 SBError& error // An error explaining what went wrong if attach fails
963 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
965 SBProcess sb_process;
966 ProcessSP process_sp;
967 TargetSP target_sp(GetSP());
971 log->Printf ("SBTarget(%p)::AttachToProcessWithID (listener, pid=%" PRId64 ", error)...", target_sp.get(), pid);
976 Mutex::Locker api_locker (target_sp->GetAPIMutex());
978 StateType state = eStateInvalid;
979 process_sp = target_sp->GetProcessSP();
982 state = process_sp->GetState();
984 if (process_sp->IsAlive() && state != eStateConnected)
986 if (state == eStateAttaching)
987 error.SetErrorString ("process attach is in progress");
989 error.SetErrorString ("a process is already being debugged");
994 if (state == eStateConnected)
996 // If we are already connected, then we have already specified the
997 // listener, so if a valid listener is supplied, we need to error out
998 // to let the client know.
999 if (listener.IsValid())
1001 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
1007 if (listener.IsValid())
1008 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
1010 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
1014 sb_process.SetSP (process_sp);
1016 ProcessAttachInfo attach_info;
1017 attach_info.SetProcessID (pid);
1019 PlatformSP platform_sp = target_sp->GetPlatform();
1020 ProcessInstanceInfo instance_info;
1021 if (platform_sp->GetProcessInfo(pid, instance_info))
1023 attach_info.SetUserID(instance_info.GetEffectiveUserID());
1025 error.SetError (process_sp->Attach (attach_info));
1026 if (error.Success())
1028 // If we are doing synchronous mode, then wait for the
1030 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1031 process_sp->WaitForProcessToStop (NULL);
1036 error.SetErrorString ("unable to create lldb_private::Process");
1041 error.SetErrorString ("SBTarget is invalid");
1046 log->Printf ("SBTarget(%p)::AttachToProcessWithID (...) => SBProcess(%p)",
1047 target_sp.get(), process_sp.get());
1053 SBTarget::AttachToProcessWithName
1055 SBListener &listener,
1056 const char *name, // basename of process to attach to
1057 bool wait_for, // if true wait for a new instance of "name" to be launched
1058 SBError& error // An error explaining what went wrong if attach fails
1061 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1063 SBProcess sb_process;
1064 ProcessSP process_sp;
1065 TargetSP target_sp(GetSP());
1069 log->Printf ("SBTarget(%p)::AttachToProcessWithName (listener, name=%s, wait_for=%s, error)...", target_sp.get(), name, wait_for ? "true" : "false");
1072 if (name && target_sp)
1074 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1076 StateType state = eStateInvalid;
1077 process_sp = target_sp->GetProcessSP();
1080 state = process_sp->GetState();
1082 if (process_sp->IsAlive() && state != eStateConnected)
1084 if (state == eStateAttaching)
1085 error.SetErrorString ("process attach is in progress");
1087 error.SetErrorString ("a process is already being debugged");
1092 if (state == eStateConnected)
1094 // If we are already connected, then we have already specified the
1095 // listener, so if a valid listener is supplied, we need to error out
1096 // to let the client know.
1097 if (listener.IsValid())
1099 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
1105 if (listener.IsValid())
1106 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
1108 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
1113 sb_process.SetSP (process_sp);
1114 ProcessAttachInfo attach_info;
1115 attach_info.GetExecutableFile().SetFile(name, false);
1116 attach_info.SetWaitForLaunch(wait_for);
1117 error.SetError (process_sp->Attach (attach_info));
1118 if (error.Success())
1120 // If we are doing synchronous mode, then wait for the
1122 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1123 process_sp->WaitForProcessToStop (NULL);
1128 error.SetErrorString ("unable to create lldb_private::Process");
1133 error.SetErrorString ("SBTarget is invalid");
1138 log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
1139 target_sp.get(), process_sp.get());
1145 SBTarget::ConnectRemote
1147 SBListener &listener,
1149 const char *plugin_name,
1153 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1155 SBProcess sb_process;
1156 ProcessSP process_sp;
1157 TargetSP target_sp(GetSP());
1161 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...", target_sp.get(), url, plugin_name);
1166 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1167 if (listener.IsValid())
1168 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
1170 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
1175 sb_process.SetSP (process_sp);
1176 error.SetError (process_sp->ConnectRemote (NULL, url));
1180 error.SetErrorString ("unable to create lldb_private::Process");
1185 error.SetErrorString ("SBTarget is invalid");
1190 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
1191 target_sp.get(), process_sp.get());
1197 SBTarget::GetExecutable ()
1200 SBFileSpec exe_file_spec;
1201 TargetSP target_sp(GetSP());
1204 Module *exe_module = target_sp->GetExecutableModulePointer();
1206 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
1209 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1212 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
1213 target_sp.get(), exe_file_spec.get());
1216 return exe_file_spec;
1220 SBTarget::operator == (const SBTarget &rhs) const
1222 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
1226 SBTarget::operator != (const SBTarget &rhs) const
1228 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
1232 SBTarget::GetSP () const
1238 SBTarget::SetSP (const lldb::TargetSP& target_sp)
1240 m_opaque_sp = target_sp;
1244 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
1246 lldb::SBAddress sb_addr;
1247 Address &addr = sb_addr.ref();
1248 TargetSP target_sp(GetSP());
1251 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1252 if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
1256 // We have a load address that isn't in a section, just return an address
1257 // with the offset filled in (the address) and the section set to NULL
1258 addr.SetRawAddress(vm_addr);
1263 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
1268 TargetSP target_sp(GetSP());
1270 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1277 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
1279 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
1283 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
1285 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1288 TargetSP target_sp(GetSP());
1289 if (target_sp && line != 0)
1291 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1293 const LazyBool check_inlines = eLazyBoolCalculate;
1294 const LazyBool skip_prologue = eLazyBoolCalculate;
1295 const bool internal = false;
1296 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal);
1302 sb_bp.GetDescription (sstr);
1303 char path[PATH_MAX];
1304 sb_file_spec->GetPath (path, sizeof(path));
1305 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
1317 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
1319 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1322 TargetSP target_sp(GetSP());
1323 if (target_sp.get())
1325 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1327 const bool internal = false;
1328 const LazyBool skip_prologue = eLazyBoolCalculate;
1329 if (module_name && module_name[0])
1331 FileSpecList module_spec_list;
1332 module_spec_list.Append (FileSpec (module_name, false));
1333 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
1337 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
1343 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
1344 target_sp.get(), symbol_name, module_name, sb_bp.get());
1351 SBTarget::BreakpointCreateByName (const char *symbol_name,
1352 const SBFileSpecList &module_list,
1353 const SBFileSpecList &comp_unit_list)
1355 uint32_t name_type_mask = eFunctionNameTypeAuto;
1356 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1360 SBTarget::BreakpointCreateByName (const char *symbol_name,
1361 uint32_t name_type_mask,
1362 const SBFileSpecList &module_list,
1363 const SBFileSpecList &comp_unit_list)
1365 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1368 TargetSP target_sp(GetSP());
1369 if (target_sp && symbol_name && symbol_name[0])
1371 const bool internal = false;
1372 const LazyBool skip_prologue = eLazyBoolCalculate;
1373 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1374 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1375 comp_unit_list.get(),
1384 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
1385 target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
1392 SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1394 uint32_t name_type_mask,
1395 const SBFileSpecList &module_list,
1396 const SBFileSpecList &comp_unit_list)
1398 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1401 TargetSP target_sp(GetSP());
1402 if (target_sp && num_names > 0)
1404 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1405 const bool internal = false;
1406 const LazyBool skip_prologue = eLazyBoolCalculate;
1407 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1408 comp_unit_list.get(),
1418 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1419 for (uint32_t i = 0 ; i < num_names; i++)
1422 if (i < num_names - 1)
1426 if (symbol_names[i] != NULL)
1427 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1429 log->Printf ("\"<NULL>\"%c ", sep);
1432 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1439 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
1441 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1444 TargetSP target_sp(GetSP());
1445 if (target_sp && symbol_name_regex && symbol_name_regex[0])
1447 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1448 RegularExpression regexp(symbol_name_regex);
1449 const bool internal = false;
1450 const LazyBool skip_prologue = eLazyBoolCalculate;
1452 if (module_name && module_name[0])
1454 FileSpecList module_spec_list;
1455 module_spec_list.Append (FileSpec (module_name, false));
1457 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal);
1461 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal);
1467 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1468 target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
1475 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1476 const SBFileSpecList &module_list,
1477 const SBFileSpecList &comp_unit_list)
1479 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1482 TargetSP target_sp(GetSP());
1483 if (target_sp && symbol_name_regex && symbol_name_regex[0])
1485 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1486 RegularExpression regexp(symbol_name_regex);
1487 const bool internal = false;
1488 const LazyBool skip_prologue = eLazyBoolCalculate;
1490 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal);
1495 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1496 target_sp.get(), symbol_name_regex, sb_bp.get());
1503 SBTarget::BreakpointCreateByAddress (addr_t address)
1505 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1508 TargetSP target_sp(GetSP());
1511 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1512 *sb_bp = target_sp->CreateBreakpoint (address, false);
1517 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)", target_sp.get(), (uint64_t) address, sb_bp.get());
1524 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
1526 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1529 TargetSP target_sp(GetSP());
1530 if (target_sp && source_regex && source_regex[0])
1532 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1533 RegularExpression regexp(source_regex);
1534 FileSpecList source_file_spec_list;
1535 source_file_spec_list.Append (source_file.ref());
1537 if (module_name && module_name[0])
1539 FileSpecList module_spec_list;
1540 module_spec_list.Append (FileSpec (module_name, false));
1542 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
1546 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
1552 char path[PATH_MAX];
1553 source_file->GetPath (path, sizeof(path));
1554 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1555 target_sp.get(), source_regex, path, module_name, sb_bp.get());
1562 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1563 const SBFileSpecList &module_list,
1564 const lldb::SBFileSpecList &source_file_list)
1566 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1569 TargetSP target_sp(GetSP());
1570 if (target_sp && source_regex && source_regex[0])
1572 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1573 RegularExpression regexp(source_regex);
1574 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
1579 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1580 target_sp.get(), source_regex, sb_bp.get());
1587 SBTarget::BreakpointCreateForException (lldb::LanguageType language,
1591 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1594 TargetSP target_sp(GetSP());
1597 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1598 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp);
1603 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1605 LanguageRuntime::GetNameForLanguageType(language),
1606 catch_bp ? "on" : "off",
1607 throw_bp ? "on" : "off",
1615 SBTarget::GetNumBreakpoints () const
1617 TargetSP target_sp(GetSP());
1620 // The breakpoint list is thread safe, no need to lock
1621 return target_sp->GetBreakpointList().GetSize();
1627 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1629 SBBreakpoint sb_breakpoint;
1630 TargetSP target_sp(GetSP());
1633 // The breakpoint list is thread safe, no need to lock
1634 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1636 return sb_breakpoint;
1640 SBTarget::BreakpointDelete (break_id_t bp_id)
1642 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1644 bool result = false;
1645 TargetSP target_sp(GetSP());
1648 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1649 result = target_sp->RemoveBreakpointByID (bp_id);
1654 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
1661 SBTarget::FindBreakpointByID (break_id_t bp_id)
1663 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1665 SBBreakpoint sb_breakpoint;
1666 TargetSP target_sp(GetSP());
1667 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
1669 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1670 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
1675 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1676 target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
1679 return sb_breakpoint;
1683 SBTarget::EnableAllBreakpoints ()
1685 TargetSP target_sp(GetSP());
1688 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1689 target_sp->EnableAllBreakpoints ();
1696 SBTarget::DisableAllBreakpoints ()
1698 TargetSP target_sp(GetSP());
1701 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1702 target_sp->DisableAllBreakpoints ();
1709 SBTarget::DeleteAllBreakpoints ()
1711 TargetSP target_sp(GetSP());
1714 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1715 target_sp->RemoveAllBreakpoints ();
1722 SBTarget::GetNumWatchpoints () const
1724 TargetSP target_sp(GetSP());
1727 // The watchpoint list is thread safe, no need to lock
1728 return target_sp->GetWatchpointList().GetSize();
1734 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
1736 SBWatchpoint sb_watchpoint;
1737 TargetSP target_sp(GetSP());
1740 // The watchpoint list is thread safe, no need to lock
1741 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
1743 return sb_watchpoint;
1747 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
1749 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1751 bool result = false;
1752 TargetSP target_sp(GetSP());
1755 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1756 Mutex::Locker locker;
1757 target_sp->GetWatchpointList().GetListMutex(locker);
1758 result = target_sp->RemoveWatchpointByID (wp_id);
1763 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
1770 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
1772 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1774 SBWatchpoint sb_watchpoint;
1775 lldb::WatchpointSP watchpoint_sp;
1776 TargetSP target_sp(GetSP());
1777 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
1779 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1780 Mutex::Locker locker;
1781 target_sp->GetWatchpointList().GetListMutex(locker);
1782 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1783 sb_watchpoint.SetSP (watchpoint_sp);
1788 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1789 target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
1792 return sb_watchpoint;
1796 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
1798 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1800 SBWatchpoint sb_watchpoint;
1801 lldb::WatchpointSP watchpoint_sp;
1802 TargetSP target_sp(GetSP());
1803 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
1805 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1806 uint32_t watch_type = 0;
1808 watch_type |= LLDB_WATCH_TYPE_READ;
1810 watch_type |= LLDB_WATCH_TYPE_WRITE;
1811 if (watch_type == 0)
1813 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1814 return sb_watchpoint;
1817 // Target::CreateWatchpoint() is thread safe.
1819 // This API doesn't take in a type, so we can't figure out what it is.
1820 ClangASTType *type = NULL;
1821 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1822 error.SetError(cw_error);
1823 sb_watchpoint.SetSP (watchpoint_sp);
1828 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
1829 target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
1832 return sb_watchpoint;
1836 SBTarget::EnableAllWatchpoints ()
1838 TargetSP target_sp(GetSP());
1841 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1842 Mutex::Locker locker;
1843 target_sp->GetWatchpointList().GetListMutex(locker);
1844 target_sp->EnableAllWatchpoints ();
1851 SBTarget::DisableAllWatchpoints ()
1853 TargetSP target_sp(GetSP());
1856 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1857 Mutex::Locker locker;
1858 target_sp->GetWatchpointList().GetListMutex(locker);
1859 target_sp->DisableAllWatchpoints ();
1866 SBTarget::DeleteAllWatchpoints ()
1868 TargetSP target_sp(GetSP());
1871 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1872 Mutex::Locker locker;
1873 target_sp->GetWatchpointList().GetListMutex(locker);
1874 target_sp->RemoveAllWatchpoints ();
1882 SBTarget::AddModule (const char *path,
1884 const char *uuid_cstr)
1886 return AddModule (path, triple, uuid_cstr, NULL);
1890 SBTarget::AddModule (const char *path,
1892 const char *uuid_cstr,
1893 const char *symfile)
1895 lldb::SBModule sb_module;
1896 TargetSP target_sp(GetSP());
1899 ModuleSpec module_spec;
1901 module_spec.GetFileSpec().SetFile(path, false);
1904 module_spec.GetUUID().SetFromCString(uuid_cstr);
1907 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1910 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
1912 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1918 SBTarget::AddModule (const SBModuleSpec &module_spec)
1920 lldb::SBModule sb_module;
1921 TargetSP target_sp(GetSP());
1923 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1928 SBTarget::AddModule (lldb::SBModule &module)
1930 TargetSP target_sp(GetSP());
1933 target_sp->GetImages().AppendIfNeeded (module.GetSP());
1940 SBTarget::GetNumModules () const
1942 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1945 TargetSP target_sp(GetSP());
1948 // The module list is thread safe, no need to lock
1949 num = target_sp->GetImages().GetSize();
1953 log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
1961 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1964 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
1966 m_opaque_sp.reset();
1971 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1974 TargetSP target_sp(GetSP());
1975 if (target_sp && sb_file_spec.IsValid())
1977 ModuleSpec module_spec(*sb_file_spec);
1978 // The module list is thread safe, no need to lock
1979 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
1985 SBTarget::GetByteOrder ()
1987 TargetSP target_sp(GetSP());
1989 return target_sp->GetArchitecture().GetByteOrder();
1990 return eByteOrderInvalid;
1994 SBTarget::GetTriple ()
1996 TargetSP target_sp(GetSP());
1999 std::string triple (target_sp->GetArchitecture().GetTriple().str());
2000 // Unique the string so we don't run into ownership issues since
2001 // the const strings put the string into the string pool once and
2002 // the strings never comes out
2003 ConstString const_triple (triple.c_str());
2004 return const_triple.GetCString();
2010 SBTarget::GetAddressByteSize()
2012 TargetSP target_sp(GetSP());
2014 return target_sp->GetArchitecture().GetAddressByteSize();
2015 return sizeof(void*);
2020 SBTarget::GetModuleAtIndex (uint32_t idx)
2022 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2026 TargetSP target_sp(GetSP());
2029 // The module list is thread safe, no need to lock
2030 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2031 sb_module.SetSP (module_sp);
2036 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2037 target_sp.get(), idx, module_sp.get());
2044 SBTarget::RemoveModule (lldb::SBModule module)
2046 TargetSP target_sp(GetSP());
2048 return target_sp->GetImages().Remove(module.GetSP());
2054 SBTarget::GetBroadcaster () const
2056 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2058 TargetSP target_sp(GetSP());
2059 SBBroadcaster broadcaster(target_sp.get(), false);
2062 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2063 target_sp.get(), broadcaster.get());
2069 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
2071 Stream &strm = description.ref();
2073 TargetSP target_sp(GetSP());
2076 target_sp->Dump (&strm, description_level);
2079 strm.PutCString ("No value");
2084 lldb::SBSymbolContextList
2085 SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
2087 lldb::SBSymbolContextList sb_sc_list;
2088 if (name && name[0])
2090 TargetSP target_sp(GetSP());
2093 const bool symbols_ok = true;
2094 const bool inlines_ok = true;
2095 const bool append = true;
2096 target_sp->GetImages().FindFunctions (ConstString(name),
2108 SBTarget::FindFirstType (const char* typename_cstr)
2110 TargetSP target_sp(GetSP());
2111 if (typename_cstr && typename_cstr[0] && target_sp)
2113 ConstString const_typename(typename_cstr);
2115 const bool exact_match = false;
2117 const ModuleList &module_list = target_sp->GetImages();
2118 size_t count = module_list.GetSize();
2119 for (size_t idx = 0; idx < count; idx++)
2121 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2124 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2126 return SBType(type_sp);
2130 // Didn't find the type in the symbols; try the Objective-C runtime
2131 // if one is installed
2133 ProcessSP process_sp(target_sp->GetProcessSP());
2137 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2139 if (objc_language_runtime)
2141 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2143 if (objc_type_vendor)
2145 std::vector <ClangASTType> types;
2147 if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2148 return SBType(types[0]);
2153 // No matches, search for basic typename matches
2154 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2156 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
2162 SBTarget::GetBasicType(lldb::BasicType type)
2164 TargetSP target_sp(GetSP());
2167 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2169 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
2176 SBTarget::FindTypes (const char* typename_cstr)
2178 SBTypeList sb_type_list;
2179 TargetSP target_sp(GetSP());
2180 if (typename_cstr && typename_cstr[0] && target_sp)
2182 ModuleList& images = target_sp->GetImages();
2183 ConstString const_typename(typename_cstr);
2184 bool exact_match = false;
2188 uint32_t num_matches = images.FindTypes (sc,
2194 if (num_matches > 0)
2196 for (size_t idx = 0; idx < num_matches; idx++)
2198 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2200 sb_type_list.Append(SBType(type_sp));
2204 // Try the Objective-C runtime if one is installed
2206 ProcessSP process_sp(target_sp->GetProcessSP());
2210 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2212 if (objc_language_runtime)
2214 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2216 if (objc_type_vendor)
2218 std::vector <ClangASTType> types;
2220 if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2222 for (ClangASTType &type : types)
2224 sb_type_list.Append(SBType(type));
2231 if (sb_type_list.GetSize() == 0)
2233 // No matches, search for basic typename matches
2234 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2236 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
2239 return sb_type_list;
2243 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2245 SBValueList sb_value_list;
2247 TargetSP target_sp(GetSP());
2248 if (name && target_sp)
2250 VariableList variable_list;
2251 const bool append = true;
2252 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2257 if (match_count > 0)
2259 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2260 if (exe_scope == NULL)
2261 exe_scope = target_sp.get();
2262 for (uint32_t i=0; i<match_count; ++i)
2264 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2266 sb_value_list.Append(SBValue(valobj_sp));
2271 return sb_value_list;
2275 SBTarget::FindFirstGlobalVariable (const char* name)
2277 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2278 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2279 return sb_value_list.GetValueAtIndex(0);
2284 SBTarget::GetSourceManager()
2286 SBSourceManager source_manager (*this);
2287 return source_manager;
2290 lldb::SBInstructionList
2291 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2293 return ReadInstructions (base_addr, count, NULL);
2296 lldb::SBInstructionList
2297 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2299 SBInstructionList sb_instructions;
2301 TargetSP target_sp(GetSP());
2304 Address *addr_ptr = base_addr.get();
2308 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2309 bool prefer_file_cache = false;
2310 lldb_private::Error error;
2311 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2312 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2318 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2319 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2330 return sb_instructions;
2334 lldb::SBInstructionList
2335 SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2337 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2340 lldb::SBInstructionList
2341 SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2343 SBInstructionList sb_instructions;
2345 TargetSP target_sp(GetSP());
2350 if (base_addr.get())
2351 addr = *base_addr.get();
2353 const bool data_from_file = true;
2355 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2365 return sb_instructions;
2368 lldb::SBInstructionList
2369 SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2371 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2374 lldb::SBInstructionList
2375 SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2377 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
2381 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2382 lldb::addr_t section_base_addr)
2385 TargetSP target_sp(GetSP());
2388 if (!section.IsValid())
2390 sb_error.SetErrorStringWithFormat ("invalid section");
2394 SectionSP section_sp (section.GetSP());
2397 if (section_sp->IsThreadSpecific())
2399 sb_error.SetErrorString ("thread specific sections are not yet supported");
2403 if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2405 // Flush info in the process (stack frames, etc)
2406 ProcessSP process_sp (target_sp->GetProcessSP());
2408 process_sp->Flush();
2416 sb_error.SetErrorString ("invalid target");
2422 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2426 TargetSP target_sp(GetSP());
2429 if (!section.IsValid())
2431 sb_error.SetErrorStringWithFormat ("invalid section");
2435 if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
2437 // Flush info in the process (stack frames, etc)
2438 ProcessSP process_sp (target_sp->GetProcessSP());
2440 process_sp->Flush();
2446 sb_error.SetErrorStringWithFormat ("invalid target");
2452 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2456 TargetSP target_sp(GetSP());
2459 ModuleSP module_sp (module.GetSP());
2462 bool changed = false;
2463 if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
2465 // The load was successful, make sure that at least some sections
2466 // changed before we notify that our module was loaded.
2469 ModuleList module_list;
2470 module_list.Append(module_sp);
2471 target_sp->ModulesDidLoad (module_list);
2472 // Flush info in the process (stack frames, etc)
2473 ProcessSP process_sp (target_sp->GetProcessSP());
2475 process_sp->Flush();
2481 sb_error.SetErrorStringWithFormat ("invalid module");
2487 sb_error.SetErrorStringWithFormat ("invalid target");
2493 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2497 char path[PATH_MAX];
2498 TargetSP target_sp(GetSP());
2501 ModuleSP module_sp (module.GetSP());
2504 ObjectFile *objfile = module_sp->GetObjectFile();
2507 SectionList *section_list = objfile->GetSectionList();
2510 bool changed = false;
2511 const size_t num_sections = section_list->GetSize();
2512 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2514 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2516 changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2520 // Flush info in the process (stack frames, etc)
2521 ProcessSP process_sp (target_sp->GetProcessSP());
2523 process_sp->Flush();
2528 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2529 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2534 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2535 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2540 sb_error.SetErrorStringWithFormat ("invalid module");
2545 sb_error.SetErrorStringWithFormat ("invalid target");
2551 lldb::SBSymbolContextList
2552 SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2554 SBSymbolContextList sb_sc_list;
2555 if (name && name[0])
2557 TargetSP target_sp(GetSP());
2561 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2573 SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2575 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2576 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2577 SBValue expr_result;
2578 ExecutionResults exe_results = eExecutionSetupError;
2579 ValueObjectSP expr_value_sp;
2580 TargetSP target_sp(GetSP());
2581 StackFrame *frame = NULL;
2584 if (expr == NULL || expr[0] == '\0')
2587 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2591 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2592 ExecutionContext exe_ctx (m_opaque_sp.get());
2595 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2597 frame = exe_ctx.GetFramePtr();
2598 Target *target = exe_ctx.GetTargetPtr();
2602 #ifdef LLDB_CONFIGURATION_DEBUG
2603 StreamString frame_description;
2605 frame->DumpUsingSettingsFormat (&frame_description);
2606 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2607 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2609 exe_results = target->EvaluateExpression (expr,
2614 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2615 #ifdef LLDB_CONFIGURATION_DEBUG
2616 Host::SetCrashDescription (NULL);
2622 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2625 #ifndef LLDB_DISABLE_PYTHON
2627 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2628 expr_result.GetValue(),
2629 expr_result.GetSummary());
2632 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2635 expr_value_sp.get(),
2644 SBTarget::GetStackRedZoneSize()
2646 TargetSP target_sp(GetSP());
2650 ProcessSP process_sp (target_sp->GetProcessSP());
2652 abi_sp = process_sp->GetABI();
2654 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2656 return abi_sp->GetRedZoneSize();