1 //===-- source/Host/freebsd/Host.cpp ------------------------------*- C++ 2 //-*-===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 // C Includes 12 #include <sys/types.h> 13 14 #include <sys/exec.h> 15 #include <sys/proc.h> 16 #include <sys/ptrace.h> 17 #include <sys/sysctl.h> 18 #include <sys/user.h> 19 20 #include <machine/elf.h> 21 22 #include <dlfcn.h> 23 #include <execinfo.h> 24 #include <stdio.h> 25 26 // C++ Includes 27 // Other libraries and framework includes 28 // Project includes 29 #include "lldb/Core/Module.h" 30 #include "lldb/Core/StreamFile.h" 31 #include "lldb/Host/Host.h" 32 #include "lldb/Host/HostInfo.h" 33 #include "lldb/Target/Platform.h" 34 #include "lldb/Target/Process.h" 35 #include "lldb/Utility/DataExtractor.h" 36 #include "lldb/Utility/Endian.h" 37 #include "lldb/Utility/Log.h" 38 #include "lldb/Utility/Status.h" 39 #include "lldb/Utility/StreamString.h" 40 41 #include "lldb/Utility/CleanUp.h" 42 #include "lldb/Utility/DataBufferHeap.h" 43 #include "lldb/Utility/DataExtractor.h" 44 #include "lldb/Utility/NameMatches.h" 45 46 #include "llvm/Support/Host.h" 47 48 extern "C" { 49 extern char **environ; 50 } 51 52 using namespace lldb; 53 using namespace lldb_private; 54 55 static bool 56 GetFreeBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr, 57 ProcessInstanceInfo &process_info) { 58 if (!process_info.ProcessIDIsValid()) 59 return false; 60 61 int pid = process_info.GetProcessID(); 62 63 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, pid}; 64 65 char arg_data[8192]; 66 size_t arg_data_size = sizeof(arg_data); 67 if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) != 0) 68 return false; 69 70 DataExtractor data(arg_data, arg_data_size, endian::InlHostByteOrder(), 71 sizeof(void *)); 72 lldb::offset_t offset = 0; 73 const char *cstr; 74 75 cstr = data.GetCStr(&offset); 76 if (!cstr) 77 return false; 78 79 process_info.GetExecutableFile().SetFile(cstr, false); 80 81 if (!(match_info_ptr == NULL || 82 NameMatches(process_info.GetExecutableFile().GetFilename().GetCString(), 83 match_info_ptr->GetNameMatchType(), 84 match_info_ptr->GetProcessInfo().GetName()))) 85 return false; 86 87 Args &proc_args = process_info.GetArguments(); 88 while (1) { 89 const uint8_t *p = data.PeekData(offset, 1); 90 while ((p != NULL) && (*p == '\0') && offset < arg_data_size) { 91 ++offset; 92 p = data.PeekData(offset, 1); 93 } 94 if (p == NULL || offset >= arg_data_size) 95 break; 96 97 cstr = data.GetCStr(&offset); 98 if (!cstr) 99 break; 100 101 proc_args.AppendArgument(llvm::StringRef(cstr)); 102 } 103 104 return true; 105 } 106 107 static bool GetFreeBSDProcessCPUType(ProcessInstanceInfo &process_info) { 108 if (process_info.ProcessIDIsValid()) { 109 process_info.GetArchitecture() = 110 HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 111 return true; 112 } 113 process_info.GetArchitecture().Clear(); 114 return false; 115 } 116 117 static bool GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) { 118 struct kinfo_proc proc_kinfo; 119 size_t proc_kinfo_size; 120 const int pid = process_info.GetProcessID(); 121 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; 122 123 if (!process_info.ProcessIDIsValid()) 124 goto error; 125 126 proc_kinfo_size = sizeof(struct kinfo_proc); 127 128 if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) != 0) 129 goto error; 130 131 if (proc_kinfo_size == 0) 132 goto error; 133 134 process_info.SetParentProcessID(proc_kinfo.ki_ppid); 135 process_info.SetUserID(proc_kinfo.ki_ruid); 136 process_info.SetGroupID(proc_kinfo.ki_rgid); 137 process_info.SetEffectiveUserID(proc_kinfo.ki_uid); 138 if (proc_kinfo.ki_ngroups > 0) 139 process_info.SetEffectiveGroupID(proc_kinfo.ki_groups[0]); 140 else 141 process_info.SetEffectiveGroupID(UINT32_MAX); 142 return true; 143 144 error: 145 process_info.SetParentProcessID(LLDB_INVALID_PROCESS_ID); 146 process_info.SetUserID(UINT32_MAX); 147 process_info.SetGroupID(UINT32_MAX); 148 process_info.SetEffectiveUserID(UINT32_MAX); 149 process_info.SetEffectiveGroupID(UINT32_MAX); 150 return false; 151 } 152 153 uint32_t Host::FindProcesses(const ProcessInstanceInfoMatch &match_info, 154 ProcessInstanceInfoList &process_infos) { 155 const ::pid_t our_pid = ::getpid(); 156 const ::uid_t our_uid = ::getuid(); 157 std::vector<struct kinfo_proc> kinfos; 158 // Special case, if lldb is being run as root we can attach to anything. 159 bool all_users = match_info.GetMatchAllUsers() || (our_uid == 0); 160 161 int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; 162 163 size_t pid_data_size = 0; 164 if (::sysctl(mib, 3, NULL, &pid_data_size, NULL, 0) != 0) 165 return 0; 166 167 // Add a few extra in case a few more show up 168 const size_t estimated_pid_count = 169 (pid_data_size / sizeof(struct kinfo_proc)) + 10; 170 171 kinfos.resize(estimated_pid_count); 172 pid_data_size = kinfos.size() * sizeof(struct kinfo_proc); 173 174 if (::sysctl(mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0) 175 return 0; 176 177 const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc)); 178 179 for (size_t i = 0; i < actual_pid_count; i++) { 180 const struct kinfo_proc &kinfo = kinfos[i]; 181 182 /* Make sure the user is acceptable */ 183 if (!all_users && kinfo.ki_ruid != our_uid) 184 continue; 185 186 if (kinfo.ki_pid == our_pid || // Skip this process 187 kinfo.ki_pid == 0 || // Skip kernel (kernel pid is 0) 188 kinfo.ki_stat == SZOMB || // Zombies are bad 189 kinfo.ki_flag & P_TRACED || // Being debugged? 190 kinfo.ki_flag & P_WEXIT) // Working on exiting 191 continue; 192 193 // Every thread is a process in FreeBSD, but all the threads of a single 194 // process have the same pid. Do not store the process info in the 195 // result list if a process with given identifier is already registered 196 // there. 197 bool already_registered = false; 198 for (uint32_t pi = 0; 199 !already_registered && (const int)kinfo.ki_numthreads > 1 && 200 pi < (const uint32_t)process_infos.GetSize(); 201 pi++) 202 already_registered = 203 (process_infos.GetProcessIDAtIndex(pi) == (uint32_t)kinfo.ki_pid); 204 205 if (already_registered) 206 continue; 207 208 ProcessInstanceInfo process_info; 209 process_info.SetProcessID(kinfo.ki_pid); 210 process_info.SetParentProcessID(kinfo.ki_ppid); 211 process_info.SetUserID(kinfo.ki_ruid); 212 process_info.SetGroupID(kinfo.ki_rgid); 213 process_info.SetEffectiveUserID(kinfo.ki_svuid); 214 process_info.SetEffectiveGroupID(kinfo.ki_svgid); 215 216 // Make sure our info matches before we go fetch the name and cpu type 217 if (match_info.Matches(process_info) && 218 GetFreeBSDProcessArgs(&match_info, process_info)) { 219 GetFreeBSDProcessCPUType(process_info); 220 if (match_info.Matches(process_info)) 221 process_infos.Append(process_info); 222 } 223 } 224 225 return process_infos.GetSize(); 226 } 227 228 bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) { 229 process_info.SetProcessID(pid); 230 231 if (GetFreeBSDProcessArgs(NULL, process_info)) { 232 // should use libprocstat instead of going right into sysctl? 233 GetFreeBSDProcessCPUType(process_info); 234 GetFreeBSDProcessUserAndGroup(process_info); 235 return true; 236 } 237 238 process_info.Clear(); 239 return false; 240 } 241 242 size_t Host::GetEnvironment(StringList &env) { 243 char **host_env = environ; 244 char *env_entry; 245 size_t i; 246 for (i = 0; (env_entry = host_env[i]) != NULL; ++i) 247 env.AppendString(env_entry); 248 return i; 249 } 250 251 Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) { 252 return Status("unimplemented"); 253 } 254