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"
42 #include "Plugins/Process/Utility/FreeBSDSignals.h"
44 #include "llvm/Support/Host.h"
47 extern char **environ;
51 using namespace lldb_private;
54 Host::Backtrace (Stream &strm, uint32_t max_frames)
56 char backtrace_path[] = "/tmp/lldb-backtrace-tmp-XXXXXX";
57 int backtrace_fd = ::mkstemp (backtrace_path);
58 if (backtrace_fd != -1)
60 std::vector<void *> frame_buffer (max_frames, NULL);
61 int count = ::backtrace (&frame_buffer[0], frame_buffer.size());
62 ::backtrace_symbols_fd (&frame_buffer[0], count, backtrace_fd);
64 const off_t buffer_size = ::lseek(backtrace_fd, 0, SEEK_CUR);
66 if (::lseek(backtrace_fd, 0, SEEK_SET) == 0)
68 char *buffer = (char *)::malloc (buffer_size);
71 ssize_t bytes_read = ::read (backtrace_fd, buffer, buffer_size);
73 strm.Write(buffer, bytes_read);
77 ::close (backtrace_fd);
78 ::unlink (backtrace_path);
83 Host::GetEnvironment (StringList &env)
87 for (; var != NULL && *var != NULL; ++var)
89 v = strchr(*var, (int)'-');
98 GetFreeBSDProcessArgs (const ProcessInstanceInfoMatch *match_info_ptr,
99 ProcessInstanceInfo &process_info)
101 if (process_info.ProcessIDIsValid())
103 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ARGS, (int)process_info.GetProcessID() };
106 size_t arg_data_size = sizeof(arg_data);
107 if (::sysctl (mib, 4, arg_data, &arg_data_size , NULL, 0) == 0)
109 DataExtractor data (arg_data, arg_data_size, lldb::endian::InlHostByteOrder(), sizeof(void *));
110 lldb::offset_t offset = 0;
113 cstr = data.GetCStr (&offset);
116 process_info.GetExecutableFile().SetFile(cstr, false);
118 if (!(match_info_ptr == NULL ||
119 NameMatches (process_info.GetExecutableFile().GetFilename().GetCString(),
120 match_info_ptr->GetNameMatchType(),
121 match_info_ptr->GetProcessInfo().GetName())))
124 Args &proc_args = process_info.GetArguments();
127 const uint8_t *p = data.PeekData(offset, 1);
128 while ((p != NULL) && (*p == '\0') && offset < arg_data_size)
131 p = data.PeekData(offset, 1);
133 if (p == NULL || offset >= arg_data_size)
136 cstr = data.GetCStr(&offset);
138 proc_args.AppendArgument(cstr);
149 GetFreeBSDProcessCPUType (ProcessInstanceInfo &process_info)
151 if (process_info.ProcessIDIsValid())
153 process_info.GetArchitecture() = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
156 process_info.GetArchitecture().Clear();
161 GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info)
163 struct kinfo_proc proc_kinfo;
164 size_t proc_kinfo_size;
166 if (process_info.ProcessIDIsValid())
168 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID,
169 (int)process_info.GetProcessID() };
170 proc_kinfo_size = sizeof(struct kinfo_proc);
172 if (::sysctl (mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0)
174 if (proc_kinfo_size > 0)
176 process_info.SetParentProcessID (proc_kinfo.ki_ppid);
177 process_info.SetUserID (proc_kinfo.ki_ruid);
178 process_info.SetGroupID (proc_kinfo.ki_rgid);
179 process_info.SetEffectiveUserID (proc_kinfo.ki_uid);
180 if (proc_kinfo.ki_ngroups > 0)
181 process_info.SetEffectiveGroupID (proc_kinfo.ki_groups[0]);
183 process_info.SetEffectiveGroupID (UINT32_MAX);
188 process_info.SetParentProcessID (LLDB_INVALID_PROCESS_ID);
189 process_info.SetUserID (UINT32_MAX);
190 process_info.SetGroupID (UINT32_MAX);
191 process_info.SetEffectiveUserID (UINT32_MAX);
192 process_info.SetEffectiveGroupID (UINT32_MAX);
197 Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
199 std::vector<struct kinfo_proc> kinfos;
201 int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL };
203 size_t pid_data_size = 0;
204 if (::sysctl (mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
207 // Add a few extra in case a few more show up
208 const size_t estimated_pid_count = (pid_data_size / sizeof(struct kinfo_proc)) + 10;
210 kinfos.resize (estimated_pid_count);
211 pid_data_size = kinfos.size() * sizeof(struct kinfo_proc);
213 if (::sysctl (mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
216 const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc));
218 bool all_users = match_info.GetMatchAllUsers();
219 const ::pid_t our_pid = getpid();
220 const uid_t our_uid = getuid();
221 for (size_t i = 0; i < actual_pid_count; i++)
223 const struct kinfo_proc &kinfo = kinfos[i];
224 const bool kinfo_user_matches = (all_users ||
225 (kinfo.ki_ruid == our_uid) ||
226 // Special case, if lldb is being run as root we can attach to anything.
230 if (kinfo_user_matches == false || // Make sure the user is acceptable
231 kinfo.ki_pid == our_pid || // Skip this process
232 kinfo.ki_pid == 0 || // Skip kernel (kernel pid is zero)
233 kinfo.ki_stat == SZOMB || // Zombies are bad, they like brains...
234 kinfo.ki_flag & P_TRACED || // Being debugged?
235 kinfo.ki_flag & P_WEXIT) // Working on exiting
238 // Every thread is a process in FreeBSD, but all the threads of a single process
239 // have the same pid. Do not store the process info in the result list if a process
240 // with given identifier is already registered there.
241 bool already_registered = false;
242 for (uint32_t pi = 0;
243 !already_registered &&
244 (const int)kinfo.ki_numthreads > 1 &&
245 pi < (const uint32_t)process_infos.GetSize(); pi++)
246 already_registered = (process_infos.GetProcessIDAtIndex(pi) == (uint32_t)kinfo.ki_pid);
248 if (already_registered)
251 ProcessInstanceInfo process_info;
252 process_info.SetProcessID (kinfo.ki_pid);
253 process_info.SetParentProcessID (kinfo.ki_ppid);
254 process_info.SetUserID (kinfo.ki_ruid);
255 process_info.SetGroupID (kinfo.ki_rgid);
256 process_info.SetEffectiveUserID (kinfo.ki_svuid);
257 process_info.SetEffectiveGroupID (kinfo.ki_svgid);
259 // Make sure our info matches before we go fetch the name and cpu type
260 if (match_info.Matches (process_info) &&
261 GetFreeBSDProcessArgs (&match_info, process_info))
263 GetFreeBSDProcessCPUType (process_info);
264 if (match_info.Matches (process_info))
265 process_infos.Append (process_info);
269 return process_infos.GetSize();
273 Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
275 process_info.SetProcessID(pid);
277 if (GetFreeBSDProcessArgs(NULL, process_info))
279 // should use libprocstat instead of going right into sysctl?
280 GetFreeBSDProcessCPUType(process_info);
281 GetFreeBSDProcessUserAndGroup(process_info);
285 process_info.Clear();
290 Host::GetAuxvData(lldb_private::Process *process)
292 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_AUXV, 0 };
293 size_t auxv_size = AT_COUNT * sizeof(Elf_Auxinfo);
296 std::unique_ptr<DataBufferHeap> buf_ap(new DataBufferHeap(auxv_size, 0));
298 mib[3] = process->GetID();
299 if (::sysctl(mib, 4, buf_ap->GetBytes(), &auxv_size, NULL, 0) == 0) {
300 buf_sp.reset(buf_ap.release());
302 perror("sysctl failed on auxv");
309 Host::GetUnixSignals ()
311 static const lldb_private::UnixSignalsSP s_unix_signals_sp (new FreeBSDSignals ());
312 return s_unix_signals_sp;