1 //===-- source/Host/freebsd/Host.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 //===----------------------------------------------------------------------===//
14 #include <sys/types.h>
16 #include <sys/sysctl.h>
19 #include <sys/ptrace.h>
21 #include <machine/elf.h>
24 // Other libraries and framework includes
26 #include "lldb/Core/Error.h"
27 #include "lldb/Host/Endian.h"
28 #include "lldb/Host/Host.h"
29 #include "lldb/Host/HostInfo.h"
30 #include "lldb/Core/Module.h"
31 #include "lldb/Core/DataExtractor.h"
32 #include "lldb/Core/StreamFile.h"
33 #include "lldb/Core/StreamString.h"
34 #include "lldb/Core/Log.h"
35 #include "lldb/Target/Process.h"
36 #include "lldb/Target/Platform.h"
38 #include "lldb/Core/DataBufferHeap.h"
39 #include "lldb/Core/DataExtractor.h"
40 #include "lldb/Utility/CleanUp.h"
41 #include "lldb/Utility/NameMatches.h"
43 #include "llvm/Support/Host.h"
46 extern char **environ;
50 using namespace lldb_private;
53 Host::GetEnvironment (StringList &env)
57 for (; var != NULL && *var != NULL; ++var)
59 v = strchr(*var, (int)'-');
68 GetFreeBSDProcessArgs (const ProcessInstanceInfoMatch *match_info_ptr,
69 ProcessInstanceInfo &process_info)
71 if (process_info.ProcessIDIsValid())
73 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ARGS, (int)process_info.GetProcessID() };
76 size_t arg_data_size = sizeof(arg_data);
77 if (::sysctl (mib, 4, arg_data, &arg_data_size , NULL, 0) == 0)
79 DataExtractor data (arg_data, arg_data_size, endian::InlHostByteOrder(), sizeof(void *));
80 lldb::offset_t offset = 0;
83 cstr = data.GetCStr (&offset);
86 process_info.GetExecutableFile().SetFile(cstr, false);
88 if (!(match_info_ptr == NULL ||
89 NameMatches (process_info.GetExecutableFile().GetFilename().GetCString(),
90 match_info_ptr->GetNameMatchType(),
91 match_info_ptr->GetProcessInfo().GetName())))
94 Args &proc_args = process_info.GetArguments();
97 const uint8_t *p = data.PeekData(offset, 1);
98 while ((p != NULL) && (*p == '\0') && offset < arg_data_size)
101 p = data.PeekData(offset, 1);
103 if (p == NULL || offset >= arg_data_size)
106 cstr = data.GetCStr(&offset);
108 proc_args.AppendArgument(cstr);
119 GetFreeBSDProcessCPUType (ProcessInstanceInfo &process_info)
121 if (process_info.ProcessIDIsValid())
123 process_info.GetArchitecture() = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
126 process_info.GetArchitecture().Clear();
131 GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info)
133 struct kinfo_proc proc_kinfo;
134 size_t proc_kinfo_size;
136 if (process_info.ProcessIDIsValid())
138 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID,
139 (int)process_info.GetProcessID() };
140 proc_kinfo_size = sizeof(struct kinfo_proc);
142 if (::sysctl (mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0)
144 if (proc_kinfo_size > 0)
146 process_info.SetParentProcessID (proc_kinfo.ki_ppid);
147 process_info.SetUserID (proc_kinfo.ki_ruid);
148 process_info.SetGroupID (proc_kinfo.ki_rgid);
149 process_info.SetEffectiveUserID (proc_kinfo.ki_uid);
150 if (proc_kinfo.ki_ngroups > 0)
151 process_info.SetEffectiveGroupID (proc_kinfo.ki_groups[0]);
153 process_info.SetEffectiveGroupID (UINT32_MAX);
158 process_info.SetParentProcessID (LLDB_INVALID_PROCESS_ID);
159 process_info.SetUserID (UINT32_MAX);
160 process_info.SetGroupID (UINT32_MAX);
161 process_info.SetEffectiveUserID (UINT32_MAX);
162 process_info.SetEffectiveGroupID (UINT32_MAX);
167 Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
169 std::vector<struct kinfo_proc> kinfos;
171 int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL };
173 size_t pid_data_size = 0;
174 if (::sysctl (mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
177 // Add a few extra in case a few more show up
178 const size_t estimated_pid_count = (pid_data_size / sizeof(struct kinfo_proc)) + 10;
180 kinfos.resize (estimated_pid_count);
181 pid_data_size = kinfos.size() * sizeof(struct kinfo_proc);
183 if (::sysctl (mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
186 const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc));
188 bool all_users = match_info.GetMatchAllUsers();
189 const ::pid_t our_pid = getpid();
190 const uid_t our_uid = getuid();
191 for (size_t i = 0; i < actual_pid_count; i++)
193 const struct kinfo_proc &kinfo = kinfos[i];
194 const bool kinfo_user_matches = (all_users ||
195 (kinfo.ki_ruid == our_uid) ||
196 // Special case, if lldb is being run as root we can attach to anything.
200 if (kinfo_user_matches == false || // Make sure the user is acceptable
201 kinfo.ki_pid == our_pid || // Skip this process
202 kinfo.ki_pid == 0 || // Skip kernel (kernel pid is zero)
203 kinfo.ki_stat == SZOMB || // Zombies are bad, they like brains...
204 kinfo.ki_flag & P_TRACED || // Being debugged?
205 kinfo.ki_flag & P_WEXIT) // Working on exiting
208 // Every thread is a process in FreeBSD, but all the threads of a single process
209 // have the same pid. Do not store the process info in the result list if a process
210 // with given identifier is already registered there.
211 bool already_registered = false;
212 for (uint32_t pi = 0;
213 !already_registered &&
214 (const int)kinfo.ki_numthreads > 1 &&
215 pi < (const uint32_t)process_infos.GetSize(); pi++)
216 already_registered = (process_infos.GetProcessIDAtIndex(pi) == (uint32_t)kinfo.ki_pid);
218 if (already_registered)
221 ProcessInstanceInfo process_info;
222 process_info.SetProcessID (kinfo.ki_pid);
223 process_info.SetParentProcessID (kinfo.ki_ppid);
224 process_info.SetUserID (kinfo.ki_ruid);
225 process_info.SetGroupID (kinfo.ki_rgid);
226 process_info.SetEffectiveUserID (kinfo.ki_svuid);
227 process_info.SetEffectiveGroupID (kinfo.ki_svgid);
229 // Make sure our info matches before we go fetch the name and cpu type
230 if (match_info.Matches (process_info) &&
231 GetFreeBSDProcessArgs (&match_info, process_info))
233 GetFreeBSDProcessCPUType (process_info);
234 if (match_info.Matches (process_info))
235 process_infos.Append (process_info);
239 return process_infos.GetSize();
243 Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
245 process_info.SetProcessID(pid);
247 if (GetFreeBSDProcessArgs(NULL, process_info))
249 // should use libprocstat instead of going right into sysctl?
250 GetFreeBSDProcessCPUType(process_info);
251 GetFreeBSDProcessUserAndGroup(process_info);
255 process_info.Clear();
260 Host::GetAuxvData(lldb_private::Process *process)
262 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_AUXV, 0 };
263 size_t auxv_size = AT_COUNT * sizeof(Elf_Auxinfo);
266 std::unique_ptr<DataBufferHeap> buf_ap(new DataBufferHeap(auxv_size, 0));
268 mib[3] = process->GetID();
269 if (::sysctl(mib, 4, buf_ap->GetBytes(), &auxv_size, NULL, 0) == 0) {
270 buf_sp.reset(buf_ap.release());
272 perror("sysctl failed on auxv");
279 Host::ShellExpandArguments (ProcessLaunchInfo &launch_info)
281 return Error("unimplemented");