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/DataExtractor.h" 30 #include "lldb/Core/Error.h" 31 #include "lldb/Core/Log.h" 32 #include "lldb/Core/Module.h" 33 #include "lldb/Core/StreamFile.h" 34 #include "lldb/Core/StreamString.h" 35 #include "lldb/Host/Endian.h" 36 #include "lldb/Host/Host.h" 37 #include "lldb/Host/HostInfo.h" 38 #include "lldb/Target/Platform.h" 39 #include "lldb/Target/Process.h" 40 41 #include "lldb/Core/DataBufferHeap.h" 42 #include "lldb/Core/DataExtractor.h" 43 #include "lldb/Utility/CleanUp.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 size_t Host::GetEnvironment(StringList &env) { 56 char *v; 57 char **var = environ; 58 for (; var != NULL && *var != NULL; ++var) { 59 v = strchr(*var, (int)'-'); 60 if (v == NULL) 61 continue; 62 env.AppendString(v); 63 } 64 return env.GetSize(); 65 } 66 67 static bool 68 GetFreeBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr, 69 ProcessInstanceInfo &process_info) { 70 if (process_info.ProcessIDIsValid()) { 71 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, 72 (int)process_info.GetProcessID()}; 73 74 char arg_data[8192]; 75 size_t arg_data_size = sizeof(arg_data); 76 if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) == 0) { 77 DataExtractor data(arg_data, arg_data_size, endian::InlHostByteOrder(), 78 sizeof(void *)); 79 lldb::offset_t offset = 0; 80 const char *cstr; 81 82 cstr = data.GetCStr(&offset); 83 if (cstr) { 84 process_info.GetExecutableFile().SetFile(cstr, false); 85 86 if (!(match_info_ptr == NULL || 87 NameMatches( 88 process_info.GetExecutableFile().GetFilename().GetCString(), 89 match_info_ptr->GetNameMatchType(), 90 match_info_ptr->GetProcessInfo().GetName()))) 91 return false; 92 93 Args &proc_args = process_info.GetArguments(); 94 while (1) { 95 const uint8_t *p = data.PeekData(offset, 1); 96 while ((p != NULL) && (*p == '\0') && offset < arg_data_size) { 97 ++offset; 98 p = data.PeekData(offset, 1); 99 } 100 if (p == NULL || offset >= arg_data_size) 101 return true; 102 103 cstr = data.GetCStr(&offset); 104 if (cstr) 105 proc_args.AppendArgument(llvm::StringRef(cstr)); 106 else 107 return true; 108 } 109 } 110 } 111 } 112 return false; 113 } 114 115 static bool GetFreeBSDProcessCPUType(ProcessInstanceInfo &process_info) { 116 if (process_info.ProcessIDIsValid()) { 117 process_info.GetArchitecture() = 118 HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 119 return true; 120 } 121 process_info.GetArchitecture().Clear(); 122 return false; 123 } 124 125 static bool GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) { 126 struct kinfo_proc proc_kinfo; 127 size_t proc_kinfo_size; 128 129 if (process_info.ProcessIDIsValid()) { 130 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, 131 (int)process_info.GetProcessID()}; 132 proc_kinfo_size = sizeof(struct kinfo_proc); 133 134 if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0) { 135 if (proc_kinfo_size > 0) { 136 process_info.SetParentProcessID(proc_kinfo.ki_ppid); 137 process_info.SetUserID(proc_kinfo.ki_ruid); 138 process_info.SetGroupID(proc_kinfo.ki_rgid); 139 process_info.SetEffectiveUserID(proc_kinfo.ki_uid); 140 if (proc_kinfo.ki_ngroups > 0) 141 process_info.SetEffectiveGroupID(proc_kinfo.ki_groups[0]); 142 else 143 process_info.SetEffectiveGroupID(UINT32_MAX); 144 return true; 145 } 146 } 147 } 148 process_info.SetParentProcessID(LLDB_INVALID_PROCESS_ID); 149 process_info.SetUserID(UINT32_MAX); 150 process_info.SetGroupID(UINT32_MAX); 151 process_info.SetEffectiveUserID(UINT32_MAX); 152 process_info.SetEffectiveGroupID(UINT32_MAX); 153 return false; 154 } 155 156 uint32_t Host::FindProcesses(const ProcessInstanceInfoMatch &match_info, 157 ProcessInstanceInfoList &process_infos) { 158 std::vector<struct kinfo_proc> kinfos; 159 160 int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; 161 162 size_t pid_data_size = 0; 163 if (::sysctl(mib, 3, NULL, &pid_data_size, NULL, 0) != 0) 164 return 0; 165 166 // Add a few extra in case a few more show up 167 const size_t estimated_pid_count = 168 (pid_data_size / sizeof(struct kinfo_proc)) + 10; 169 170 kinfos.resize(estimated_pid_count); 171 pid_data_size = kinfos.size() * sizeof(struct kinfo_proc); 172 173 if (::sysctl(mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0) 174 return 0; 175 176 const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc)); 177 178 bool all_users = match_info.GetMatchAllUsers(); 179 const ::pid_t our_pid = getpid(); 180 const uid_t our_uid = getuid(); 181 for (size_t i = 0; i < actual_pid_count; i++) { 182 const struct kinfo_proc &kinfo = kinfos[i]; 183 const bool kinfo_user_matches = (all_users || (kinfo.ki_ruid == our_uid) || 184 // Special case, if lldb is being run as 185 // root we can attach to anything. 186 (our_uid == 0)); 187 188 if (kinfo_user_matches == false || // Make sure the user is acceptable 189 kinfo.ki_pid == our_pid || // Skip this process 190 kinfo.ki_pid == 0 || // Skip kernel (kernel pid is zero) 191 kinfo.ki_stat == SZOMB || // Zombies are bad, they like brains... 192 kinfo.ki_flag & P_TRACED || // Being debugged? 193 kinfo.ki_flag & P_WEXIT) // Working on exiting 194 continue; 195 196 // Every thread is a process in FreeBSD, but all the threads of a single 197 // process 198 // have the same pid. Do not store the process info in the result list if a 199 // process 200 // with given identifier is already registered there. 201 bool already_registered = false; 202 for (uint32_t pi = 0; 203 !already_registered && (const int)kinfo.ki_numthreads > 1 && 204 pi < (const uint32_t)process_infos.GetSize(); 205 pi++) 206 already_registered = 207 (process_infos.GetProcessIDAtIndex(pi) == (uint32_t)kinfo.ki_pid); 208 209 if (already_registered) 210 continue; 211 212 ProcessInstanceInfo process_info; 213 process_info.SetProcessID(kinfo.ki_pid); 214 process_info.SetParentProcessID(kinfo.ki_ppid); 215 process_info.SetUserID(kinfo.ki_ruid); 216 process_info.SetGroupID(kinfo.ki_rgid); 217 process_info.SetEffectiveUserID(kinfo.ki_svuid); 218 process_info.SetEffectiveGroupID(kinfo.ki_svgid); 219 220 // Make sure our info matches before we go fetch the name and cpu type 221 if (match_info.Matches(process_info) && 222 GetFreeBSDProcessArgs(&match_info, process_info)) { 223 GetFreeBSDProcessCPUType(process_info); 224 if (match_info.Matches(process_info)) 225 process_infos.Append(process_info); 226 } 227 } 228 229 return process_infos.GetSize(); 230 } 231 232 bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) { 233 process_info.SetProcessID(pid); 234 235 if (GetFreeBSDProcessArgs(NULL, process_info)) { 236 // should use libprocstat instead of going right into sysctl? 237 GetFreeBSDProcessCPUType(process_info); 238 GetFreeBSDProcessUserAndGroup(process_info); 239 return true; 240 } 241 242 process_info.Clear(); 243 return false; 244 } 245 246 lldb::DataBufferSP Host::GetAuxvData(lldb_private::Process *process) { 247 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_AUXV, 0}; 248 size_t auxv_size = AT_COUNT * sizeof(Elf_Auxinfo); 249 DataBufferSP buf_sp; 250 251 std::unique_ptr<DataBufferHeap> buf_ap(new DataBufferHeap(auxv_size, 0)); 252 253 mib[3] = process->GetID(); 254 if (::sysctl(mib, 4, buf_ap->GetBytes(), &auxv_size, NULL, 0) == 0) { 255 buf_sp.reset(buf_ap.release()); 256 } else { 257 perror("sysctl failed on auxv"); 258 } 259 260 return buf_sp; 261 } 262 263 Error Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) { 264 return Error("unimplemented"); 265 } 266