xref: /openbsd-src/gnu/llvm/lldb/source/Host/freebsd/Host.cpp (revision adae0cfddad1cb50e512c3edbc993eee4037999b)
1 //===-- source/Host/freebsd/Host.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 <sys/types.h>
10 
11 #include <sys/exec.h>
12 #include <sys/proc.h>
13 #include <sys/ptrace.h>
14 #include <sys/sysctl.h>
15 #include <sys/user.h>
16 
17 #include <machine/elf.h>
18 
19 #include <dlfcn.h>
20 #include <execinfo.h>
21 #include <stdio.h>
22 
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/Log.h"
29 #include "lldb/Utility/NameMatches.h"
30 #include "lldb/Utility/ProcessInfo.h"
31 #include "lldb/Utility/Status.h"
32 #include "lldb/Utility/StreamString.h"
33 
34 #include "llvm/Support/Host.h"
35 
36 extern "C" {
37 extern char **environ;
38 }
39 
40 namespace lldb_private {
41 class ProcessLaunchInfo;
42 }
43 
44 using namespace lldb;
45 using namespace lldb_private;
46 
47 static bool
48 GetFreeBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr,
49                       ProcessInstanceInfo &process_info) {
50   if (!process_info.ProcessIDIsValid())
51     return false;
52 
53   int pid = process_info.GetProcessID();
54 
55   int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, pid};
56 
57   char arg_data[8192];
58   size_t arg_data_size = sizeof(arg_data);
59   if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) != 0)
60     return false;
61 
62   DataExtractor data(arg_data, arg_data_size, endian::InlHostByteOrder(),
63                      sizeof(void *));
64   lldb::offset_t offset = 0;
65   const char *cstr;
66 
67   cstr = data.GetCStr(&offset);
68   if (!cstr)
69     return false;
70 
71   // Get pathname for pid. If that fails fall back to argv[0].
72   char pathname[MAXPATHLEN];
73   size_t pathname_len = sizeof(pathname);
74   mib[2] = KERN_PROC_PATHNAME;
75   if (::sysctl(mib, 4, pathname, &pathname_len, NULL, 0) == 0)
76     process_info.GetExecutableFile().SetFile(pathname, FileSpec::Style::native);
77   else
78     process_info.GetExecutableFile().SetFile(cstr, FileSpec::Style::native);
79 
80   if (!(match_info_ptr == NULL ||
81         NameMatches(process_info.GetExecutableFile().GetFilename().GetCString(),
82                     match_info_ptr->GetNameMatchType(),
83                     match_info_ptr->GetProcessInfo().GetName())))
84     return false;
85 
86   Args &proc_args = process_info.GetArguments();
87   while (1) {
88     const uint8_t *p = data.PeekData(offset, 1);
89     while ((p != NULL) && (*p == '\0') && offset < arg_data_size) {
90       ++offset;
91       p = data.PeekData(offset, 1);
92     }
93     if (p == NULL || offset >= arg_data_size)
94       break;
95 
96     cstr = data.GetCStr(&offset);
97     if (!cstr)
98       break;
99 
100     proc_args.AppendArgument(llvm::StringRef(cstr));
101   }
102 
103   return true;
104 }
105 
106 static bool GetFreeBSDProcessCPUType(ProcessInstanceInfo &process_info) {
107   if (process_info.ProcessIDIsValid()) {
108     process_info.GetArchitecture() =
109         HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
110     return true;
111   }
112   process_info.GetArchitecture().Clear();
113   return false;
114 }
115 
116 static bool GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) {
117   struct kinfo_proc proc_kinfo;
118   size_t proc_kinfo_size;
119   const int pid = process_info.GetProcessID();
120   int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
121 
122   if (!process_info.ProcessIDIsValid())
123     goto error;
124 
125   proc_kinfo_size = sizeof(struct kinfo_proc);
126 
127   if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) != 0)
128     goto error;
129 
130   if (proc_kinfo_size == 0)
131     goto error;
132 
133   process_info.SetParentProcessID(proc_kinfo.ki_ppid);
134   process_info.SetUserID(proc_kinfo.ki_ruid);
135   process_info.SetGroupID(proc_kinfo.ki_rgid);
136   process_info.SetEffectiveUserID(proc_kinfo.ki_uid);
137   if (proc_kinfo.ki_ngroups > 0)
138     process_info.SetEffectiveGroupID(proc_kinfo.ki_groups[0]);
139   else
140     process_info.SetEffectiveGroupID(UINT32_MAX);
141   return true;
142 
143 error:
144   process_info.SetParentProcessID(LLDB_INVALID_PROCESS_ID);
145   process_info.SetUserID(UINT32_MAX);
146   process_info.SetGroupID(UINT32_MAX);
147   process_info.SetEffectiveUserID(UINT32_MAX);
148   process_info.SetEffectiveGroupID(UINT32_MAX);
149   return false;
150 }
151 
152 uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
153                                  ProcessInstanceInfoList &process_infos) {
154   const ::pid_t our_pid = ::getpid();
155   const ::uid_t our_uid = ::getuid();
156   std::vector<struct kinfo_proc> kinfos;
157   // Special case, if lldb is being run as root we can attach to anything.
158   bool all_users = match_info.GetMatchAllUsers() || (our_uid == 0);
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   for (size_t i = 0; i < actual_pid_count; i++) {
179     const struct kinfo_proc &kinfo = kinfos[i];
180 
181     /* Make sure the user is acceptable */
182     if (!all_users && kinfo.ki_ruid != our_uid)
183       continue;
184 
185     if (kinfo.ki_pid == our_pid ||  // Skip this process
186         kinfo.ki_pid == 0 ||        // Skip kernel (kernel pid is 0)
187         kinfo.ki_stat == SZOMB ||   // Zombies are bad
188         kinfo.ki_flag & P_TRACED || // Being debugged?
189         kinfo.ki_flag & P_WEXIT)    // Working on exiting
190       continue;
191 
192     // Every thread is a process in FreeBSD, but all the threads of a single
193     // process have the same pid. Do not store the process info in the result
194     // list if a process with given identifier is already registered there.
195     bool already_registered = false;
196     for (uint32_t pi = 0;
197          !already_registered && (const int)kinfo.ki_numthreads > 1 &&
198          pi < (const uint32_t)process_infos.size();
199          pi++)
200       already_registered =
201           (process_infos[pi].GetProcessID() == (uint32_t)kinfo.ki_pid);
202 
203     if (already_registered)
204       continue;
205 
206     ProcessInstanceInfo process_info;
207     process_info.SetProcessID(kinfo.ki_pid);
208     process_info.SetParentProcessID(kinfo.ki_ppid);
209     process_info.SetUserID(kinfo.ki_ruid);
210     process_info.SetGroupID(kinfo.ki_rgid);
211     process_info.SetEffectiveUserID(kinfo.ki_svuid);
212     process_info.SetEffectiveGroupID(kinfo.ki_svgid);
213 
214     // Make sure our info matches before we go fetch the name and cpu type
215     if (match_info.Matches(process_info) &&
216         GetFreeBSDProcessArgs(&match_info, process_info)) {
217       GetFreeBSDProcessCPUType(process_info);
218       if (match_info.Matches(process_info))
219         process_infos.push_back(process_info);
220     }
221   }
222 
223   return process_infos.size();
224 }
225 
226 bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {
227   process_info.SetProcessID(pid);
228 
229   if (GetFreeBSDProcessArgs(NULL, process_info)) {
230     // should use libprocstat instead of going right into sysctl?
231     GetFreeBSDProcessCPUType(process_info);
232     GetFreeBSDProcessUserAndGroup(process_info);
233     return true;
234   }
235 
236   process_info.Clear();
237   return false;
238 }
239 
240 Environment Host::GetEnvironment() { return Environment(environ); }
241 
242 Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
243   return Status("unimplemented");
244 }
245