1 | //===-- source/Host/netbsd/HostNetBSD.cpp ---------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include <cstdio> |
10 | #include <dlfcn.h> |
11 | #include <execinfo.h> |
12 | #include <sys/proc.h> |
13 | #include <sys/sysctl.h> |
14 | #include <sys/types.h> |
15 | |
16 | #include <climits> |
17 | |
18 | #include <kvm.h> |
19 | #include <sys/exec.h> |
20 | #include <sys/ptrace.h> |
21 | |
22 | #include "lldb/Host/FileSystem.h" |
23 | #include "lldb/Host/Host.h" |
24 | #include "lldb/Host/HostInfo.h" |
25 | #include "lldb/Utility/DataBufferHeap.h" |
26 | #include "lldb/Utility/DataExtractor.h" |
27 | #include "lldb/Utility/Endian.h" |
28 | #include "lldb/Utility/LLDBLog.h" |
29 | #include "lldb/Utility/Log.h" |
30 | #include "lldb/Utility/NameMatches.h" |
31 | #include "lldb/Utility/ProcessInfo.h" |
32 | #include "lldb/Utility/Status.h" |
33 | #include "lldb/Utility/StreamString.h" |
34 | |
35 | #include "llvm/Object/ELF.h" |
36 | #include "llvm/TargetParser/Host.h" |
37 | |
38 | extern "C" { |
39 | extern char **environ; |
40 | } |
41 | |
42 | using namespace lldb; |
43 | using namespace lldb_private; |
44 | |
45 | namespace lldb_private { |
46 | class ProcessLaunchInfo; |
47 | } |
48 | |
49 | Environment Host::GetEnvironment() { return Environment(environ); } |
50 | |
51 | static bool GetNetBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr, |
52 | ProcessInstanceInfo &process_info) { |
53 | if (!process_info.ProcessIDIsValid()) |
54 | return false; |
55 | |
56 | int pid = process_info.GetProcessID(); |
57 | |
58 | int mib[4] = {CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_ARGV}; |
59 | |
60 | char arg_data[8192]; |
61 | size_t arg_data_size = sizeof(arg_data); |
62 | if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) != 0) |
63 | return false; |
64 | |
65 | DataExtractor data(arg_data, arg_data_size, endian::InlHostByteOrder(), |
66 | sizeof(void *)); |
67 | lldb::offset_t offset = 0; |
68 | const char *cstr; |
69 | |
70 | cstr = data.GetCStr(offset_ptr: &offset); |
71 | if (!cstr) |
72 | return false; |
73 | |
74 | process_info.GetExecutableFile().SetFile(path: cstr, |
75 | style: FileSpec::Style::native); |
76 | |
77 | if (!(match_info_ptr == NULL || |
78 | NameMatches(name: process_info.GetExecutableFile().GetFilename().GetCString(), |
79 | match_type: match_info_ptr->GetNameMatchType(), |
80 | match: match_info_ptr->GetProcessInfo().GetName()))) |
81 | return false; |
82 | |
83 | process_info.SetArg0(cstr); |
84 | Args &proc_args = process_info.GetArguments(); |
85 | while (1) { |
86 | const uint8_t *p = data.PeekData(offset, length: 1); |
87 | while ((p != NULL) && (*p == '\0') && offset < arg_data_size) { |
88 | ++offset; |
89 | p = data.PeekData(offset, length: 1); |
90 | } |
91 | if (p == NULL || offset >= arg_data_size) |
92 | break; |
93 | |
94 | cstr = data.GetCStr(offset_ptr: &offset); |
95 | if (!cstr) |
96 | break; |
97 | |
98 | proc_args.AppendArgument(arg_str: llvm::StringRef(cstr)); |
99 | } |
100 | |
101 | return true; |
102 | } |
103 | |
104 | static bool GetNetBSDProcessCPUType(ProcessInstanceInfo &process_info) { |
105 | Log *log = GetLog(mask: LLDBLog::Host); |
106 | |
107 | if (process_info.ProcessIDIsValid()) { |
108 | auto buffer_sp = FileSystem::Instance().CreateDataBuffer( |
109 | file_spec: process_info.GetExecutableFile(), size: 0x20, offset: 0); |
110 | if (buffer_sp) { |
111 | uint8_t exe_class = |
112 | llvm::object::getElfArchType( |
113 | Object: {reinterpret_cast<const char *>(buffer_sp->GetBytes()), |
114 | size_t(buffer_sp->GetByteSize())}) |
115 | .first; |
116 | |
117 | switch (exe_class) { |
118 | case llvm::ELF::ELFCLASS32: |
119 | process_info.GetArchitecture() = |
120 | HostInfo::GetArchitecture(arch_kind: HostInfo::eArchKind32); |
121 | return true; |
122 | case llvm::ELF::ELFCLASS64: |
123 | process_info.GetArchitecture() = |
124 | HostInfo::GetArchitecture(arch_kind: HostInfo::eArchKind64); |
125 | return true; |
126 | default: |
127 | LLDB_LOG(log, "Unknown elf class ({0}) in file {1}" , exe_class, |
128 | process_info.GetExecutableFile()); |
129 | } |
130 | } |
131 | } |
132 | process_info.GetArchitecture().Clear(); |
133 | return false; |
134 | } |
135 | |
136 | static bool GetNetBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) { |
137 | ::kvm_t *kdp; |
138 | char errbuf[_POSIX2_LINE_MAX]; /* XXX: error string unused */ |
139 | |
140 | struct ::kinfo_proc2 *proc_kinfo; |
141 | const int pid = process_info.GetProcessID(); |
142 | int nproc; |
143 | |
144 | if (!process_info.ProcessIDIsValid()) |
145 | goto error; |
146 | |
147 | if ((kdp = ::kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf)) == NULL) |
148 | goto error; |
149 | |
150 | if ((proc_kinfo = ::kvm_getproc2(kdp, KERN_PROC_PID, pid, |
151 | sizeof(struct ::kinfo_proc2), &nproc)) == |
152 | NULL) { |
153 | ::kvm_close(kdp); |
154 | goto error; |
155 | } |
156 | |
157 | if (nproc < 1) { |
158 | ::kvm_close(kdp); /* XXX: we don't check for error here */ |
159 | goto error; |
160 | } |
161 | |
162 | process_info.SetParentProcessID(proc_kinfo->p_ppid); |
163 | process_info.SetUserID(proc_kinfo->p_ruid); |
164 | process_info.SetGroupID(proc_kinfo->p_rgid); |
165 | process_info.SetEffectiveUserID(proc_kinfo->p_uid); |
166 | process_info.SetEffectiveGroupID(proc_kinfo->p_gid); |
167 | |
168 | ::kvm_close(kdp); /* XXX: we don't check for error here */ |
169 | |
170 | return true; |
171 | |
172 | error: |
173 | process_info.SetParentProcessID(LLDB_INVALID_PROCESS_ID); |
174 | process_info.SetUserID(UINT32_MAX); |
175 | process_info.SetGroupID(UINT32_MAX); |
176 | process_info.SetEffectiveUserID(UINT32_MAX); |
177 | process_info.SetEffectiveGroupID(UINT32_MAX); |
178 | return false; |
179 | } |
180 | |
181 | uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info, |
182 | ProcessInstanceInfoList &process_infos) { |
183 | const ::pid_t our_pid = ::getpid(); |
184 | const ::uid_t our_uid = ::getuid(); |
185 | |
186 | const bool all_users = |
187 | match_info.GetMatchAllUsers() || |
188 | // Special case, if lldb is being run as root we can attach to anything |
189 | (our_uid == 0); |
190 | |
191 | kvm_t *kdp; |
192 | char errbuf[_POSIX2_LINE_MAX]; /* XXX: error string unused */ |
193 | if ((kdp = ::kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf)) == NULL) |
194 | return 0; |
195 | |
196 | struct ::kinfo_proc2 *proc_kinfo; |
197 | int nproc; |
198 | if ((proc_kinfo = ::kvm_getproc2(kdp, KERN_PROC_ALL, 0, |
199 | sizeof(struct ::kinfo_proc2), &nproc)) == |
200 | NULL) { |
201 | ::kvm_close(kdp); |
202 | return 0; |
203 | } |
204 | |
205 | ProcessInstanceInfoMatch match_info_noname{match_info}; |
206 | match_info_noname.SetNameMatchType(NameMatch::Ignore); |
207 | |
208 | for (int i = 0; i < nproc; i++) { |
209 | if (proc_kinfo[i].p_pid < 1) |
210 | continue; /* not valid */ |
211 | /* Make sure the user is acceptable */ |
212 | if (!all_users && proc_kinfo[i].p_ruid != our_uid) |
213 | continue; |
214 | |
215 | if (proc_kinfo[i].p_pid == our_pid || // Skip this process |
216 | proc_kinfo[i].p_pid == 0 || // Skip kernel (kernel pid is 0) |
217 | proc_kinfo[i].p_stat == LSZOMB || // Zombies are bad |
218 | proc_kinfo[i].p_flag & P_TRACED || // Being debugged? |
219 | proc_kinfo[i].p_flag & P_WEXIT) // Working on exiting |
220 | continue; |
221 | |
222 | // Every thread is a process in NetBSD, but all the threads of a single |
223 | // process have the same pid. Do not store the process info in the result |
224 | // list if a process with given identifier is already registered there. |
225 | if (proc_kinfo[i].p_nlwps > 1) { |
226 | bool already_registered = false; |
227 | for (size_t pi = 0; pi < process_infos.size(); pi++) { |
228 | if ((::pid_t)process_infos[pi].GetProcessID() == proc_kinfo[i].p_pid) { |
229 | already_registered = true; |
230 | break; |
231 | } |
232 | } |
233 | |
234 | if (already_registered) |
235 | continue; |
236 | } |
237 | ProcessInstanceInfo process_info; |
238 | process_info.SetProcessID(proc_kinfo[i].p_pid); |
239 | process_info.SetParentProcessID(proc_kinfo[i].p_ppid); |
240 | process_info.SetUserID(proc_kinfo[i].p_ruid); |
241 | process_info.SetGroupID(proc_kinfo[i].p_rgid); |
242 | process_info.SetEffectiveUserID(proc_kinfo[i].p_uid); |
243 | process_info.SetEffectiveGroupID(proc_kinfo[i].p_gid); |
244 | // Make sure our info matches before we go fetch the name and cpu type |
245 | if (match_info_noname.Matches(proc_info: process_info) && |
246 | GetNetBSDProcessArgs(match_info_ptr: &match_info, process_info)) { |
247 | GetNetBSDProcessCPUType(process_info); |
248 | if (match_info.Matches(proc_info: process_info)) |
249 | process_infos.push_back(x: process_info); |
250 | } |
251 | } |
252 | |
253 | kvm_close(kdp); /* XXX: we don't check for error here */ |
254 | |
255 | return process_infos.size(); |
256 | } |
257 | |
258 | bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) { |
259 | process_info.SetProcessID(pid); |
260 | |
261 | if (GetNetBSDProcessArgs(NULL, process_info)) { |
262 | GetNetBSDProcessCPUType(process_info); |
263 | GetNetBSDProcessUserAndGroup(process_info); |
264 | return true; |
265 | } |
266 | |
267 | process_info.Clear(); |
268 | return false; |
269 | } |
270 | |
271 | Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) { |
272 | return Status("unimplemented" ); |
273 | } |
274 | |