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/ValueObjectConstResult.h"
45 #include "lldb/Core/ValueObjectList.h"
46 #include "lldb/Core/ValueObjectVariable.h"
47 #include "lldb/Host/FileSpec.h"
48 #include "lldb/Host/Host.h"
49 #include "lldb/Interpreter/Args.h"
50 #include "lldb/Symbol/ObjectFile.h"
51 #include "lldb/Symbol/SymbolVendor.h"
52 #include "lldb/Symbol/VariableList.h"
53 #include "lldb/Target/LanguageRuntime.h"
54 #include "lldb/Target/Process.h"
55 #include "lldb/Target/Target.h"
56 #include "lldb/Target/TargetList.h"
58 #include "lldb/Interpreter/CommandReturnObject.h"
59 #include "../source/Commands/CommandObjectBreakpoint.h"
63 using namespace lldb_private;
65 #define DEFAULT_DISASM_BYTE_SIZE 32
67 SBLaunchInfo::SBLaunchInfo (const char **argv) :
68 m_opaque_sp(new ProcessLaunchInfo())
70 m_opaque_sp->GetFlags().Reset (eLaunchFlagDebug | eLaunchFlagDisableASLR);
72 m_opaque_sp->GetArguments().SetArguments(argv);
75 SBLaunchInfo::~SBLaunchInfo()
79 lldb_private::ProcessLaunchInfo &
87 SBLaunchInfo::GetUserID()
89 return m_opaque_sp->GetUserID();
93 SBLaunchInfo::GetGroupID()
95 return m_opaque_sp->GetGroupID();
99 SBLaunchInfo::UserIDIsValid ()
101 return m_opaque_sp->UserIDIsValid();
105 SBLaunchInfo::GroupIDIsValid ()
107 return m_opaque_sp->GroupIDIsValid();
111 SBLaunchInfo::SetUserID (uint32_t uid)
113 m_opaque_sp->SetUserID (uid);
117 SBLaunchInfo::SetGroupID (uint32_t gid)
119 m_opaque_sp->SetGroupID (gid);
123 SBLaunchInfo::GetNumArguments ()
125 return m_opaque_sp->GetArguments().GetArgumentCount();
129 SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
131 return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
135 SBLaunchInfo::SetArguments (const char **argv, bool append)
140 m_opaque_sp->GetArguments().AppendArguments(argv);
145 m_opaque_sp->GetArguments().SetArguments(argv);
147 m_opaque_sp->GetArguments().Clear();
152 SBLaunchInfo::GetNumEnvironmentEntries ()
154 return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
158 SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
160 return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
164 SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
169 m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
174 m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
176 m_opaque_sp->GetEnvironmentEntries().Clear();
181 SBLaunchInfo::Clear ()
183 m_opaque_sp->Clear();
187 SBLaunchInfo::GetWorkingDirectory () const
189 return m_opaque_sp->GetWorkingDirectory();
193 SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
195 m_opaque_sp->SetWorkingDirectory(working_dir);
199 SBLaunchInfo::GetLaunchFlags ()
201 return m_opaque_sp->GetFlags().Get();
205 SBLaunchInfo::SetLaunchFlags (uint32_t flags)
207 m_opaque_sp->GetFlags().Reset(flags);
211 SBLaunchInfo::GetProcessPluginName ()
213 return m_opaque_sp->GetProcessPluginName();
217 SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
219 return m_opaque_sp->SetProcessPluginName (plugin_name);
223 SBLaunchInfo::GetShell ()
225 return m_opaque_sp->GetShell();
229 SBLaunchInfo::SetShell (const char * path)
231 m_opaque_sp->SetShell (path);
235 SBLaunchInfo::GetResumeCount ()
237 return m_opaque_sp->GetResumeCount();
241 SBLaunchInfo::SetResumeCount (uint32_t c)
243 m_opaque_sp->SetResumeCount (c);
247 SBLaunchInfo::AddCloseFileAction (int fd)
249 return m_opaque_sp->AppendCloseFileAction(fd);
253 SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
255 return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
259 SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
261 return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
265 SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
267 return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
271 SBAttachInfo::SBAttachInfo () :
272 m_opaque_sp (new ProcessAttachInfo())
276 SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
277 m_opaque_sp (new ProcessAttachInfo())
279 m_opaque_sp->SetProcessID (pid);
282 SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
283 m_opaque_sp (new ProcessAttachInfo())
286 m_opaque_sp->GetExecutableFile().SetFile(path, false);
287 m_opaque_sp->SetWaitForLaunch (wait_for);
290 SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
291 m_opaque_sp (new ProcessAttachInfo())
293 *m_opaque_sp = *rhs.m_opaque_sp;
296 SBAttachInfo::~SBAttachInfo()
300 lldb_private::ProcessAttachInfo &
307 SBAttachInfo::operator = (const SBAttachInfo &rhs)
310 *m_opaque_sp = *rhs.m_opaque_sp;
315 SBAttachInfo::GetProcessID ()
317 return m_opaque_sp->GetProcessID();
321 SBAttachInfo::SetProcessID (lldb::pid_t pid)
323 m_opaque_sp->SetProcessID (pid);
328 SBAttachInfo::GetResumeCount ()
330 return m_opaque_sp->GetResumeCount();
334 SBAttachInfo::SetResumeCount (uint32_t c)
336 m_opaque_sp->SetResumeCount (c);
340 SBAttachInfo::GetProcessPluginName ()
342 return m_opaque_sp->GetProcessPluginName();
346 SBAttachInfo::SetProcessPluginName (const char *plugin_name)
348 return m_opaque_sp->SetProcessPluginName (plugin_name);
352 SBAttachInfo::SetExecutable (const char *path)
355 m_opaque_sp->GetExecutableFile().SetFile(path, false);
357 m_opaque_sp->GetExecutableFile().Clear();
361 SBAttachInfo::SetExecutable (SBFileSpec exe_file)
363 if (exe_file.IsValid())
364 m_opaque_sp->GetExecutableFile() = exe_file.ref();
366 m_opaque_sp->GetExecutableFile().Clear();
370 SBAttachInfo::GetWaitForLaunch ()
372 return m_opaque_sp->GetWaitForLaunch();
376 SBAttachInfo::SetWaitForLaunch (bool b)
378 m_opaque_sp->SetWaitForLaunch (b);
382 SBAttachInfo::GetIgnoreExisting ()
384 return m_opaque_sp->GetIgnoreExisting();
388 SBAttachInfo::SetIgnoreExisting (bool b)
390 m_opaque_sp->SetIgnoreExisting (b);
394 SBAttachInfo::GetUserID()
396 return m_opaque_sp->GetUserID();
400 SBAttachInfo::GetGroupID()
402 return m_opaque_sp->GetGroupID();
406 SBAttachInfo::UserIDIsValid ()
408 return m_opaque_sp->UserIDIsValid();
412 SBAttachInfo::GroupIDIsValid ()
414 return m_opaque_sp->GroupIDIsValid();
418 SBAttachInfo::SetUserID (uint32_t uid)
420 m_opaque_sp->SetUserID (uid);
424 SBAttachInfo::SetGroupID (uint32_t gid)
426 m_opaque_sp->SetGroupID (gid);
430 SBAttachInfo::GetEffectiveUserID()
432 return m_opaque_sp->GetEffectiveUserID();
436 SBAttachInfo::GetEffectiveGroupID()
438 return m_opaque_sp->GetEffectiveGroupID();
442 SBAttachInfo::EffectiveUserIDIsValid ()
444 return m_opaque_sp->EffectiveUserIDIsValid();
448 SBAttachInfo::EffectiveGroupIDIsValid ()
450 return m_opaque_sp->EffectiveGroupIDIsValid ();
454 SBAttachInfo::SetEffectiveUserID (uint32_t uid)
456 m_opaque_sp->SetEffectiveUserID(uid);
460 SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
462 m_opaque_sp->SetEffectiveGroupID(gid);
466 SBAttachInfo::GetParentProcessID ()
468 return m_opaque_sp->GetParentProcessID();
472 SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
474 m_opaque_sp->SetParentProcessID (pid);
478 SBAttachInfo::ParentProcessIDIsValid()
480 return m_opaque_sp->ParentProcessIDIsValid();
484 //----------------------------------------------------------------------
485 // SBTarget constructor
486 //----------------------------------------------------------------------
487 SBTarget::SBTarget () :
492 SBTarget::SBTarget (const SBTarget& rhs) :
493 m_opaque_sp (rhs.m_opaque_sp)
497 SBTarget::SBTarget(const TargetSP& target_sp) :
498 m_opaque_sp (target_sp)
503 SBTarget::operator = (const SBTarget& rhs)
506 m_opaque_sp = rhs.m_opaque_sp;
510 //----------------------------------------------------------------------
512 //----------------------------------------------------------------------
513 SBTarget::~SBTarget()
518 SBTarget::GetBroadcasterClassName ()
520 return Target::GetStaticBroadcasterClass().AsCString();
524 SBTarget::IsValid () const
526 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
530 SBTarget::GetProcess ()
532 SBProcess sb_process;
533 ProcessSP process_sp;
534 TargetSP target_sp(GetSP());
537 process_sp = target_sp->GetProcessSP();
538 sb_process.SetSP (process_sp);
541 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
544 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
545 target_sp.get(), process_sp.get());
552 SBTarget::GetDebugger () const
555 TargetSP target_sp(GetSP());
557 debugger.reset (target_sp->GetDebugger().shared_from_this());
562 SBTarget::LoadCore (const char *core_file)
564 SBProcess sb_process;
565 TargetSP target_sp(GetSP());
568 FileSpec filespec(core_file, true);
569 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
574 process_sp->LoadCore();
575 sb_process.SetSP (process_sp);
582 SBTarget::LaunchSimple
586 const char *working_directory
589 char *stdin_path = NULL;
590 char *stdout_path = NULL;
591 char *stderr_path = NULL;
592 uint32_t launch_flags = 0;
593 bool stop_at_entry = false;
595 SBListener listener = GetDebugger().GetListener();
596 return Launch (listener,
612 TargetSP target_sp(GetSP());
615 Mutex::Locker api_locker (target_sp->GetAPIMutex());
616 sb_error.ref() = target_sp->Install(NULL);
624 SBListener &listener,
627 const char *stdin_path,
628 const char *stdout_path,
629 const char *stderr_path,
630 const char *working_directory,
631 uint32_t launch_flags, // See LaunchFlags
636 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
638 SBProcess sb_process;
639 ProcessSP process_sp;
640 TargetSP target_sp(GetSP());
644 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))...",
648 stdin_path ? stdin_path : "NULL",
649 stdout_path ? stdout_path : "NULL",
650 stderr_path ? stderr_path : "NULL",
651 working_directory ? working_directory : "NULL",
659 Mutex::Locker api_locker (target_sp->GetAPIMutex());
661 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
662 launch_flags |= eLaunchFlagDisableASLR;
664 StateType state = eStateInvalid;
665 process_sp = target_sp->GetProcessSP();
668 state = process_sp->GetState();
670 if (process_sp->IsAlive() && state != eStateConnected)
672 if (state == eStateAttaching)
673 error.SetErrorString ("process attach is in progress");
675 error.SetErrorString ("a process is already being debugged");
680 if (state == eStateConnected)
682 // If we are already connected, then we have already specified the
683 // listener, so if a valid listener is supplied, we need to error out
684 // to let the client know.
685 if (listener.IsValid())
687 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
693 if (listener.IsValid())
694 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
696 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
701 sb_process.SetSP (process_sp);
702 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
703 launch_flags |= eLaunchFlagDisableSTDIO;
705 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
707 Module *exe_module = target_sp->GetExecutableModulePointer();
709 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
711 launch_info.GetArguments().AppendArguments (argv);
713 launch_info.GetEnvironmentEntries ().SetArguments (envp);
715 error.SetError (process_sp->Launch (launch_info));
718 // We we are stopping at the entry point, we can return now!
722 // Make sure we are stopped at the entry
723 StateType state = process_sp->WaitForProcessToStop (NULL);
724 if (state == eStateStopped)
726 // resume the process to skip the entry point
727 error.SetError (process_sp->Resume());
730 // If we are doing synchronous mode, then wait for the
731 // process to stop yet again!
732 if (target_sp->GetDebugger().GetAsyncExecution () == false)
733 process_sp->WaitForProcessToStop (NULL);
740 error.SetErrorString ("unable to create lldb_private::Process");
745 error.SetErrorString ("SBTarget is invalid");
748 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
751 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
752 target_sp.get(), process_sp.get());
759 SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
761 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
763 SBProcess sb_process;
764 ProcessSP process_sp;
765 TargetSP target_sp(GetSP());
769 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...", target_sp.get());
774 Mutex::Locker api_locker (target_sp->GetAPIMutex());
775 StateType state = eStateInvalid;
776 process_sp = target_sp->GetProcessSP();
779 state = process_sp->GetState();
781 if (process_sp->IsAlive() && state != eStateConnected)
783 if (state == eStateAttaching)
784 error.SetErrorString ("process attach is in progress");
786 error.SetErrorString ("a process is already being debugged");
791 if (state != eStateConnected)
792 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
796 sb_process.SetSP (process_sp);
797 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
799 Module *exe_module = target_sp->GetExecutableModulePointer();
801 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
803 const ArchSpec &arch_spec = target_sp->GetArchitecture();
804 if (arch_spec.IsValid())
805 launch_info.GetArchitecture () = arch_spec;
807 error.SetError (process_sp->Launch (launch_info));
808 const bool synchronous_execution = target_sp->GetDebugger().GetAsyncExecution () == false;
811 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
813 // If we are doing synchronous mode, then wait for the initial
814 // stop to happen, else, return and let the caller watch for
816 if (synchronous_execution)
817 process_sp->WaitForProcessToStop (NULL);
818 // We we are stopping at the entry point, we can return now!
822 // Make sure we are stopped at the entry
823 StateType state = process_sp->WaitForProcessToStop (NULL);
824 if (state == eStateStopped)
826 // resume the process to skip the entry point
827 error.SetError (process_sp->Resume());
830 // If we are doing synchronous mode, then wait for the
831 // process to stop yet again!
832 if (synchronous_execution)
833 process_sp->WaitForProcessToStop (NULL);
840 error.SetErrorString ("unable to create lldb_private::Process");
845 error.SetErrorString ("SBTarget is invalid");
848 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
851 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
852 target_sp.get(), process_sp.get());
859 SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
861 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
863 SBProcess sb_process;
864 ProcessSP process_sp;
865 TargetSP target_sp(GetSP());
869 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...", target_sp.get());
874 Mutex::Locker api_locker (target_sp->GetAPIMutex());
876 StateType state = eStateInvalid;
877 process_sp = target_sp->GetProcessSP();
880 state = process_sp->GetState();
882 if (process_sp->IsAlive() && state != eStateConnected)
884 if (state == eStateAttaching)
885 error.SetErrorString ("process attach is in progress");
887 error.SetErrorString ("a process is already being debugged");
890 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
891 target_sp.get(), error.GetCString());
897 if (state != eStateConnected)
898 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
902 ProcessAttachInfo &attach_info = sb_attach_info.ref();
903 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
905 PlatformSP platform_sp = target_sp->GetPlatform();
906 // See if we can pre-verify if a process exists or not
907 if (platform_sp && platform_sp->IsConnected())
909 lldb::pid_t attach_pid = attach_info.GetProcessID();
910 ProcessInstanceInfo instance_info;
911 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
913 attach_info.SetUserID(instance_info.GetEffectiveUserID());
917 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
920 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
921 target_sp.get(), error.GetCString());
927 error.SetError (process_sp->Attach (attach_info));
930 sb_process.SetSP (process_sp);
931 // If we are doing synchronous mode, then wait for the
933 if (target_sp->GetDebugger().GetAsyncExecution () == false)
934 process_sp->WaitForProcessToStop (NULL);
939 error.SetErrorString ("unable to create lldb_private::Process");
944 error.SetErrorString ("SBTarget is invalid");
949 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
950 target_sp.get(), process_sp.get());
957 #if defined(__APPLE__)
960 SBTarget::AttachToProcessWithID (SBListener &listener,
962 lldb::SBError& error)
964 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
967 #endif // #if defined(__APPLE__)
970 SBTarget::AttachToProcessWithID
972 SBListener &listener,
973 lldb::pid_t pid,// The process ID to attach to
974 SBError& error // An error explaining what went wrong if attach fails
977 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
979 SBProcess sb_process;
980 ProcessSP process_sp;
981 TargetSP target_sp(GetSP());
985 log->Printf ("SBTarget(%p)::AttachToProcessWithID (listener, pid=%" PRId64 ", error)...", target_sp.get(), pid);
990 Mutex::Locker api_locker (target_sp->GetAPIMutex());
992 StateType state = eStateInvalid;
993 process_sp = target_sp->GetProcessSP();
996 state = process_sp->GetState();
998 if (process_sp->IsAlive() && state != eStateConnected)
1000 if (state == eStateAttaching)
1001 error.SetErrorString ("process attach is in progress");
1003 error.SetErrorString ("a process is already being debugged");
1008 if (state == eStateConnected)
1010 // If we are already connected, then we have already specified the
1011 // listener, so if a valid listener is supplied, we need to error out
1012 // to let the client know.
1013 if (listener.IsValid())
1015 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
1021 if (listener.IsValid())
1022 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
1024 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
1028 sb_process.SetSP (process_sp);
1030 ProcessAttachInfo attach_info;
1031 attach_info.SetProcessID (pid);
1033 PlatformSP platform_sp = target_sp->GetPlatform();
1034 ProcessInstanceInfo instance_info;
1035 if (platform_sp->GetProcessInfo(pid, instance_info))
1037 attach_info.SetUserID(instance_info.GetEffectiveUserID());
1039 error.SetError (process_sp->Attach (attach_info));
1040 if (error.Success())
1042 // If we are doing synchronous mode, then wait for the
1044 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1045 process_sp->WaitForProcessToStop (NULL);
1050 error.SetErrorString ("unable to create lldb_private::Process");
1055 error.SetErrorString ("SBTarget is invalid");
1060 log->Printf ("SBTarget(%p)::AttachToProcessWithID (...) => SBProcess(%p)",
1061 target_sp.get(), process_sp.get());
1067 SBTarget::AttachToProcessWithName
1069 SBListener &listener,
1070 const char *name, // basename of process to attach to
1071 bool wait_for, // if true wait for a new instance of "name" to be launched
1072 SBError& error // An error explaining what went wrong if attach fails
1075 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1077 SBProcess sb_process;
1078 ProcessSP process_sp;
1079 TargetSP target_sp(GetSP());
1083 log->Printf ("SBTarget(%p)::AttachToProcessWithName (listener, name=%s, wait_for=%s, error)...", target_sp.get(), name, wait_for ? "true" : "false");
1086 if (name && target_sp)
1088 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1090 StateType state = eStateInvalid;
1091 process_sp = target_sp->GetProcessSP();
1094 state = process_sp->GetState();
1096 if (process_sp->IsAlive() && state != eStateConnected)
1098 if (state == eStateAttaching)
1099 error.SetErrorString ("process attach is in progress");
1101 error.SetErrorString ("a process is already being debugged");
1106 if (state == eStateConnected)
1108 // If we are already connected, then we have already specified the
1109 // listener, so if a valid listener is supplied, we need to error out
1110 // to let the client know.
1111 if (listener.IsValid())
1113 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
1119 if (listener.IsValid())
1120 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
1122 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
1127 sb_process.SetSP (process_sp);
1128 ProcessAttachInfo attach_info;
1129 attach_info.GetExecutableFile().SetFile(name, false);
1130 attach_info.SetWaitForLaunch(wait_for);
1131 error.SetError (process_sp->Attach (attach_info));
1132 if (error.Success())
1134 // If we are doing synchronous mode, then wait for the
1136 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1137 process_sp->WaitForProcessToStop (NULL);
1142 error.SetErrorString ("unable to create lldb_private::Process");
1147 error.SetErrorString ("SBTarget is invalid");
1152 log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
1153 target_sp.get(), process_sp.get());
1159 SBTarget::ConnectRemote
1161 SBListener &listener,
1163 const char *plugin_name,
1167 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1169 SBProcess sb_process;
1170 ProcessSP process_sp;
1171 TargetSP target_sp(GetSP());
1175 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...", target_sp.get(), url, plugin_name);
1180 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1181 if (listener.IsValid())
1182 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
1184 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
1189 sb_process.SetSP (process_sp);
1190 error.SetError (process_sp->ConnectRemote (NULL, url));
1194 error.SetErrorString ("unable to create lldb_private::Process");
1199 error.SetErrorString ("SBTarget is invalid");
1204 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
1205 target_sp.get(), process_sp.get());
1211 SBTarget::GetExecutable ()
1214 SBFileSpec exe_file_spec;
1215 TargetSP target_sp(GetSP());
1218 Module *exe_module = target_sp->GetExecutableModulePointer();
1220 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
1223 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1226 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
1227 target_sp.get(), exe_file_spec.get());
1230 return exe_file_spec;
1234 SBTarget::operator == (const SBTarget &rhs) const
1236 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
1240 SBTarget::operator != (const SBTarget &rhs) const
1242 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
1246 SBTarget::GetSP () const
1252 SBTarget::SetSP (const lldb::TargetSP& target_sp)
1254 m_opaque_sp = target_sp;
1258 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
1260 lldb::SBAddress sb_addr;
1261 Address &addr = sb_addr.ref();
1262 TargetSP target_sp(GetSP());
1265 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1266 if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
1270 // We have a load address that isn't in a section, just return an address
1271 // with the offset filled in (the address) and the section set to NULL
1272 addr.SetRawAddress(vm_addr);
1277 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
1278 uint32_t resolve_scope)
1283 TargetSP target_sp(GetSP());
1285 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1292 SBTarget::BreakpointCreateByLocation (const char *file,
1295 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
1299 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
1302 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1305 TargetSP target_sp(GetSP());
1306 if (target_sp && line != 0)
1308 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1310 const LazyBool check_inlines = eLazyBoolCalculate;
1311 const LazyBool skip_prologue = eLazyBoolCalculate;
1312 const bool internal = false;
1313 const bool hardware = false;
1314 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
1320 sb_bp.GetDescription (sstr);
1321 char path[PATH_MAX];
1322 sb_file_spec->GetPath (path, sizeof(path));
1323 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
1335 SBTarget::BreakpointCreateByName (const char *symbol_name,
1336 const char *module_name)
1338 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1341 TargetSP target_sp(GetSP());
1342 if (target_sp.get())
1344 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1346 const bool internal = false;
1347 const bool hardware = false;
1348 const LazyBool skip_prologue = eLazyBoolCalculate;
1349 if (module_name && module_name[0])
1351 FileSpecList module_spec_list;
1352 module_spec_list.Append (FileSpec (module_name, false));
1353 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
1357 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
1363 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
1364 target_sp.get(), symbol_name, module_name, sb_bp.get());
1371 SBTarget::BreakpointCreateByName (const char *symbol_name,
1372 const SBFileSpecList &module_list,
1373 const SBFileSpecList &comp_unit_list)
1375 uint32_t name_type_mask = eFunctionNameTypeAuto;
1376 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1380 SBTarget::BreakpointCreateByName (const char *symbol_name,
1381 uint32_t name_type_mask,
1382 const SBFileSpecList &module_list,
1383 const SBFileSpecList &comp_unit_list)
1385 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1388 TargetSP target_sp(GetSP());
1389 if (target_sp && symbol_name && symbol_name[0])
1391 const bool internal = false;
1392 const bool hardware = false;
1393 const LazyBool skip_prologue = eLazyBoolCalculate;
1394 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1395 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1396 comp_unit_list.get(),
1406 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
1407 target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
1414 SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1416 uint32_t name_type_mask,
1417 const SBFileSpecList &module_list,
1418 const SBFileSpecList &comp_unit_list)
1420 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1423 TargetSP target_sp(GetSP());
1424 if (target_sp && num_names > 0)
1426 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1427 const bool internal = false;
1428 const bool hardware = false;
1429 const LazyBool skip_prologue = eLazyBoolCalculate;
1430 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1431 comp_unit_list.get(),
1442 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1443 for (uint32_t i = 0 ; i < num_names; i++)
1446 if (i < num_names - 1)
1450 if (symbol_names[i] != NULL)
1451 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1453 log->Printf ("\"<NULL>\"%c ", sep);
1456 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1463 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1464 const char *module_name)
1466 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1469 TargetSP target_sp(GetSP());
1470 if (target_sp && symbol_name_regex && symbol_name_regex[0])
1472 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1473 RegularExpression regexp(symbol_name_regex);
1474 const bool internal = false;
1475 const bool hardware = false;
1476 const LazyBool skip_prologue = eLazyBoolCalculate;
1478 if (module_name && module_name[0])
1480 FileSpecList module_spec_list;
1481 module_spec_list.Append (FileSpec (module_name, false));
1483 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
1487 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
1493 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1494 target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
1501 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1502 const SBFileSpecList &module_list,
1503 const SBFileSpecList &comp_unit_list)
1505 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1508 TargetSP target_sp(GetSP());
1509 if (target_sp && symbol_name_regex && symbol_name_regex[0])
1511 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1512 RegularExpression regexp(symbol_name_regex);
1513 const bool internal = false;
1514 const bool hardware = false;
1515 const LazyBool skip_prologue = eLazyBoolCalculate;
1517 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
1522 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1523 target_sp.get(), symbol_name_regex, sb_bp.get());
1530 SBTarget::BreakpointCreateByAddress (addr_t address)
1532 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1535 TargetSP target_sp(GetSP());
1538 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1539 const bool hardware = false;
1540 *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
1545 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)", target_sp.get(), (uint64_t) address, sb_bp.get());
1552 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1553 const lldb::SBFileSpec &source_file,
1554 const char *module_name)
1556 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1559 TargetSP target_sp(GetSP());
1560 if (target_sp && source_regex && source_regex[0])
1562 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1563 RegularExpression regexp(source_regex);
1564 FileSpecList source_file_spec_list;
1565 const bool hardware = false;
1566 source_file_spec_list.Append (source_file.ref());
1568 if (module_name && module_name[0])
1570 FileSpecList module_spec_list;
1571 module_spec_list.Append (FileSpec (module_name, false));
1573 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
1577 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
1583 char path[PATH_MAX];
1584 source_file->GetPath (path, sizeof(path));
1585 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1586 target_sp.get(), source_regex, path, module_name, sb_bp.get());
1593 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1594 const SBFileSpecList &module_list,
1595 const lldb::SBFileSpecList &source_file_list)
1597 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1600 TargetSP target_sp(GetSP());
1601 if (target_sp && source_regex && source_regex[0])
1603 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1604 const bool hardware = false;
1605 RegularExpression regexp(source_regex);
1606 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
1611 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1612 target_sp.get(), source_regex, sb_bp.get());
1619 SBTarget::BreakpointCreateForException (lldb::LanguageType language,
1623 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1626 TargetSP target_sp(GetSP());
1629 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1630 const bool hardware = false;
1631 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
1636 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1638 LanguageRuntime::GetNameForLanguageType(language),
1639 catch_bp ? "on" : "off",
1640 throw_bp ? "on" : "off",
1648 SBTarget::GetNumBreakpoints () const
1650 TargetSP target_sp(GetSP());
1653 // The breakpoint list is thread safe, no need to lock
1654 return target_sp->GetBreakpointList().GetSize();
1660 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1662 SBBreakpoint sb_breakpoint;
1663 TargetSP target_sp(GetSP());
1666 // The breakpoint list is thread safe, no need to lock
1667 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1669 return sb_breakpoint;
1673 SBTarget::BreakpointDelete (break_id_t bp_id)
1675 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1677 bool result = false;
1678 TargetSP target_sp(GetSP());
1681 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1682 result = target_sp->RemoveBreakpointByID (bp_id);
1687 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
1694 SBTarget::FindBreakpointByID (break_id_t bp_id)
1696 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1698 SBBreakpoint sb_breakpoint;
1699 TargetSP target_sp(GetSP());
1700 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
1702 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1703 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
1708 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1709 target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
1712 return sb_breakpoint;
1716 SBTarget::EnableAllBreakpoints ()
1718 TargetSP target_sp(GetSP());
1721 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1722 target_sp->EnableAllBreakpoints ();
1729 SBTarget::DisableAllBreakpoints ()
1731 TargetSP target_sp(GetSP());
1734 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1735 target_sp->DisableAllBreakpoints ();
1742 SBTarget::DeleteAllBreakpoints ()
1744 TargetSP target_sp(GetSP());
1747 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1748 target_sp->RemoveAllBreakpoints ();
1755 SBTarget::GetNumWatchpoints () const
1757 TargetSP target_sp(GetSP());
1760 // The watchpoint list is thread safe, no need to lock
1761 return target_sp->GetWatchpointList().GetSize();
1767 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
1769 SBWatchpoint sb_watchpoint;
1770 TargetSP target_sp(GetSP());
1773 // The watchpoint list is thread safe, no need to lock
1774 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
1776 return sb_watchpoint;
1780 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
1782 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1784 bool result = false;
1785 TargetSP target_sp(GetSP());
1788 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1789 Mutex::Locker locker;
1790 target_sp->GetWatchpointList().GetListMutex(locker);
1791 result = target_sp->RemoveWatchpointByID (wp_id);
1796 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
1803 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
1805 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1807 SBWatchpoint sb_watchpoint;
1808 lldb::WatchpointSP watchpoint_sp;
1809 TargetSP target_sp(GetSP());
1810 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
1812 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1813 Mutex::Locker locker;
1814 target_sp->GetWatchpointList().GetListMutex(locker);
1815 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1816 sb_watchpoint.SetSP (watchpoint_sp);
1821 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1822 target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
1825 return sb_watchpoint;
1829 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
1831 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1833 SBWatchpoint sb_watchpoint;
1834 lldb::WatchpointSP watchpoint_sp;
1835 TargetSP target_sp(GetSP());
1836 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
1838 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1839 uint32_t watch_type = 0;
1841 watch_type |= LLDB_WATCH_TYPE_READ;
1843 watch_type |= LLDB_WATCH_TYPE_WRITE;
1844 if (watch_type == 0)
1846 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1847 return sb_watchpoint;
1850 // Target::CreateWatchpoint() is thread safe.
1852 // This API doesn't take in a type, so we can't figure out what it is.
1853 ClangASTType *type = NULL;
1854 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1855 error.SetError(cw_error);
1856 sb_watchpoint.SetSP (watchpoint_sp);
1861 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
1862 target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
1865 return sb_watchpoint;
1869 SBTarget::EnableAllWatchpoints ()
1871 TargetSP target_sp(GetSP());
1874 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1875 Mutex::Locker locker;
1876 target_sp->GetWatchpointList().GetListMutex(locker);
1877 target_sp->EnableAllWatchpoints ();
1884 SBTarget::DisableAllWatchpoints ()
1886 TargetSP target_sp(GetSP());
1889 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1890 Mutex::Locker locker;
1891 target_sp->GetWatchpointList().GetListMutex(locker);
1892 target_sp->DisableAllWatchpoints ();
1899 SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1902 lldb::ValueObjectSP new_value_sp;
1903 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1905 lldb::addr_t address(addr.GetLoadAddress(*this));
1906 lldb::TypeImplSP type_impl_sp (type.GetSP());
1907 ClangASTType pointer_ast_type(type_impl_sp->GetClangASTType(true).GetPointerType ());
1908 if (pointer_ast_type)
1910 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
1912 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1913 ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
1917 exe_ctx.GetByteOrder(),
1918 exe_ctx.GetAddressByteSize()));
1920 if (ptr_result_valobj_sp)
1922 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
1924 new_value_sp = ptr_result_valobj_sp->Dereference(err);
1926 new_value_sp->SetName(ConstString(name));
1930 sb_value.SetSP(new_value_sp);
1931 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1935 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"", m_opaque_sp.get(), new_value_sp->GetName().AsCString());
1937 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL", m_opaque_sp.get());
1943 SBTarget::DeleteAllWatchpoints ()
1945 TargetSP target_sp(GetSP());
1948 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1949 Mutex::Locker locker;
1950 target_sp->GetWatchpointList().GetListMutex(locker);
1951 target_sp->RemoveAllWatchpoints ();
1959 SBTarget::AddModule (const char *path,
1961 const char *uuid_cstr)
1963 return AddModule (path, triple, uuid_cstr, NULL);
1967 SBTarget::AddModule (const char *path,
1969 const char *uuid_cstr,
1970 const char *symfile)
1972 lldb::SBModule sb_module;
1973 TargetSP target_sp(GetSP());
1976 ModuleSpec module_spec;
1978 module_spec.GetFileSpec().SetFile(path, false);
1981 module_spec.GetUUID().SetFromCString(uuid_cstr);
1984 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1986 module_spec.GetArchitecture() = target_sp->GetArchitecture();
1989 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
1991 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1997 SBTarget::AddModule (const SBModuleSpec &module_spec)
1999 lldb::SBModule sb_module;
2000 TargetSP target_sp(GetSP());
2002 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
2007 SBTarget::AddModule (lldb::SBModule &module)
2009 TargetSP target_sp(GetSP());
2012 target_sp->GetImages().AppendIfNeeded (module.GetSP());
2019 SBTarget::GetNumModules () const
2021 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2024 TargetSP target_sp(GetSP());
2027 // The module list is thread safe, no need to lock
2028 num = target_sp->GetImages().GetSize();
2032 log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
2040 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2043 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
2045 m_opaque_sp.reset();
2050 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
2053 TargetSP target_sp(GetSP());
2054 if (target_sp && sb_file_spec.IsValid())
2056 ModuleSpec module_spec(*sb_file_spec);
2057 // The module list is thread safe, no need to lock
2058 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
2064 SBTarget::GetByteOrder ()
2066 TargetSP target_sp(GetSP());
2068 return target_sp->GetArchitecture().GetByteOrder();
2069 return eByteOrderInvalid;
2073 SBTarget::GetTriple ()
2075 TargetSP target_sp(GetSP());
2078 std::string triple (target_sp->GetArchitecture().GetTriple().str());
2079 // Unique the string so we don't run into ownership issues since
2080 // the const strings put the string into the string pool once and
2081 // the strings never comes out
2082 ConstString const_triple (triple.c_str());
2083 return const_triple.GetCString();
2089 SBTarget::GetAddressByteSize()
2091 TargetSP target_sp(GetSP());
2093 return target_sp->GetArchitecture().GetAddressByteSize();
2094 return sizeof(void*);
2099 SBTarget::GetModuleAtIndex (uint32_t idx)
2101 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2105 TargetSP target_sp(GetSP());
2108 // The module list is thread safe, no need to lock
2109 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2110 sb_module.SetSP (module_sp);
2115 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2116 target_sp.get(), idx, module_sp.get());
2123 SBTarget::RemoveModule (lldb::SBModule module)
2125 TargetSP target_sp(GetSP());
2127 return target_sp->GetImages().Remove(module.GetSP());
2133 SBTarget::GetBroadcaster () const
2135 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2137 TargetSP target_sp(GetSP());
2138 SBBroadcaster broadcaster(target_sp.get(), false);
2141 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2142 target_sp.get(), broadcaster.get());
2148 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
2150 Stream &strm = description.ref();
2152 TargetSP target_sp(GetSP());
2155 target_sp->Dump (&strm, description_level);
2158 strm.PutCString ("No value");
2163 lldb::SBSymbolContextList
2164 SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
2166 lldb::SBSymbolContextList sb_sc_list;
2167 if (name && name[0])
2169 TargetSP target_sp(GetSP());
2172 const bool symbols_ok = true;
2173 const bool inlines_ok = true;
2174 const bool append = true;
2175 target_sp->GetImages().FindFunctions (ConstString(name),
2187 SBTarget::FindFirstType (const char* typename_cstr)
2189 TargetSP target_sp(GetSP());
2190 if (typename_cstr && typename_cstr[0] && target_sp)
2192 ConstString const_typename(typename_cstr);
2194 const bool exact_match = false;
2196 const ModuleList &module_list = target_sp->GetImages();
2197 size_t count = module_list.GetSize();
2198 for (size_t idx = 0; idx < count; idx++)
2200 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2203 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2205 return SBType(type_sp);
2209 // Didn't find the type in the symbols; try the Objective-C runtime
2210 // if one is installed
2212 ProcessSP process_sp(target_sp->GetProcessSP());
2216 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2218 if (objc_language_runtime)
2220 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2222 if (objc_type_vendor)
2224 std::vector <ClangASTType> types;
2226 if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2227 return SBType(types[0]);
2232 // No matches, search for basic typename matches
2233 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2235 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
2241 SBTarget::GetBasicType(lldb::BasicType type)
2243 TargetSP target_sp(GetSP());
2246 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2248 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
2255 SBTarget::FindTypes (const char* typename_cstr)
2257 SBTypeList sb_type_list;
2258 TargetSP target_sp(GetSP());
2259 if (typename_cstr && typename_cstr[0] && target_sp)
2261 ModuleList& images = target_sp->GetImages();
2262 ConstString const_typename(typename_cstr);
2263 bool exact_match = false;
2267 uint32_t num_matches = images.FindTypes (sc,
2273 if (num_matches > 0)
2275 for (size_t idx = 0; idx < num_matches; idx++)
2277 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2279 sb_type_list.Append(SBType(type_sp));
2283 // Try the Objective-C runtime if one is installed
2285 ProcessSP process_sp(target_sp->GetProcessSP());
2289 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2291 if (objc_language_runtime)
2293 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2295 if (objc_type_vendor)
2297 std::vector <ClangASTType> types;
2299 if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2301 for (ClangASTType &type : types)
2303 sb_type_list.Append(SBType(type));
2310 if (sb_type_list.GetSize() == 0)
2312 // No matches, search for basic typename matches
2313 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2315 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
2318 return sb_type_list;
2322 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2324 SBValueList sb_value_list;
2326 TargetSP target_sp(GetSP());
2327 if (name && target_sp)
2329 VariableList variable_list;
2330 const bool append = true;
2331 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2336 if (match_count > 0)
2338 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2339 if (exe_scope == NULL)
2340 exe_scope = target_sp.get();
2341 for (uint32_t i=0; i<match_count; ++i)
2343 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2345 sb_value_list.Append(SBValue(valobj_sp));
2350 return sb_value_list;
2354 SBTarget::FindFirstGlobalVariable (const char* name)
2356 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2357 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2358 return sb_value_list.GetValueAtIndex(0);
2363 SBTarget::GetSourceManager()
2365 SBSourceManager source_manager (*this);
2366 return source_manager;
2369 lldb::SBInstructionList
2370 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2372 return ReadInstructions (base_addr, count, NULL);
2375 lldb::SBInstructionList
2376 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2378 SBInstructionList sb_instructions;
2380 TargetSP target_sp(GetSP());
2383 Address *addr_ptr = base_addr.get();
2387 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2388 bool prefer_file_cache = false;
2389 lldb_private::Error error;
2390 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2391 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2397 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2398 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2409 return sb_instructions;
2413 lldb::SBInstructionList
2414 SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2416 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2419 lldb::SBInstructionList
2420 SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2422 SBInstructionList sb_instructions;
2424 TargetSP target_sp(GetSP());
2429 if (base_addr.get())
2430 addr = *base_addr.get();
2432 const bool data_from_file = true;
2434 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2444 return sb_instructions;
2447 lldb::SBInstructionList
2448 SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2450 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2453 lldb::SBInstructionList
2454 SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2456 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
2460 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2461 lldb::addr_t section_base_addr)
2464 TargetSP target_sp(GetSP());
2467 if (!section.IsValid())
2469 sb_error.SetErrorStringWithFormat ("invalid section");
2473 SectionSP section_sp (section.GetSP());
2476 if (section_sp->IsThreadSpecific())
2478 sb_error.SetErrorString ("thread specific sections are not yet supported");
2482 if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2484 // Flush info in the process (stack frames, etc)
2485 ProcessSP process_sp (target_sp->GetProcessSP());
2487 process_sp->Flush();
2495 sb_error.SetErrorString ("invalid target");
2501 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2505 TargetSP target_sp(GetSP());
2508 if (!section.IsValid())
2510 sb_error.SetErrorStringWithFormat ("invalid section");
2514 if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
2516 // Flush info in the process (stack frames, etc)
2517 ProcessSP process_sp (target_sp->GetProcessSP());
2519 process_sp->Flush();
2525 sb_error.SetErrorStringWithFormat ("invalid target");
2531 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2535 TargetSP target_sp(GetSP());
2538 ModuleSP module_sp (module.GetSP());
2541 bool changed = false;
2542 if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
2544 // The load was successful, make sure that at least some sections
2545 // changed before we notify that our module was loaded.
2548 ModuleList module_list;
2549 module_list.Append(module_sp);
2550 target_sp->ModulesDidLoad (module_list);
2551 // Flush info in the process (stack frames, etc)
2552 ProcessSP process_sp (target_sp->GetProcessSP());
2554 process_sp->Flush();
2560 sb_error.SetErrorStringWithFormat ("invalid module");
2566 sb_error.SetErrorStringWithFormat ("invalid target");
2572 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2576 char path[PATH_MAX];
2577 TargetSP target_sp(GetSP());
2580 ModuleSP module_sp (module.GetSP());
2583 ObjectFile *objfile = module_sp->GetObjectFile();
2586 SectionList *section_list = objfile->GetSectionList();
2589 bool changed = false;
2590 const size_t num_sections = section_list->GetSize();
2591 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2593 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2595 changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2599 // Flush info in the process (stack frames, etc)
2600 ProcessSP process_sp (target_sp->GetProcessSP());
2602 process_sp->Flush();
2607 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2608 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2613 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2614 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2619 sb_error.SetErrorStringWithFormat ("invalid module");
2624 sb_error.SetErrorStringWithFormat ("invalid target");
2630 lldb::SBSymbolContextList
2631 SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2633 SBSymbolContextList sb_sc_list;
2634 if (name && name[0])
2636 TargetSP target_sp(GetSP());
2640 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2652 SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2654 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2655 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2656 SBValue expr_result;
2657 ExecutionResults exe_results = eExecutionSetupError;
2658 ValueObjectSP expr_value_sp;
2659 TargetSP target_sp(GetSP());
2660 StackFrame *frame = NULL;
2663 if (expr == NULL || expr[0] == '\0')
2666 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2670 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2671 ExecutionContext exe_ctx (m_opaque_sp.get());
2674 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2676 frame = exe_ctx.GetFramePtr();
2677 Target *target = exe_ctx.GetTargetPtr();
2681 #ifdef LLDB_CONFIGURATION_DEBUG
2682 StreamString frame_description;
2684 frame->DumpUsingSettingsFormat (&frame_description);
2685 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2686 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2688 exe_results = target->EvaluateExpression (expr,
2693 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2694 #ifdef LLDB_CONFIGURATION_DEBUG
2695 Host::SetCrashDescription (NULL);
2701 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2704 #ifndef LLDB_DISABLE_PYTHON
2706 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2707 expr_result.GetValue(),
2708 expr_result.GetSummary());
2711 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2714 expr_value_sp.get(),
2723 SBTarget::GetStackRedZoneSize()
2725 TargetSP target_sp(GetSP());
2729 ProcessSP process_sp (target_sp->GetProcessSP());
2731 abi_sp = process_sp->GetABI();
2733 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2735 return abi_sp->GetRedZoneSize();