llvm-project
219 строк · 6.9 Кб
1//===-- source/Host/openbsd/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/signal.h>
12#include <sys/exec.h>
13#include <sys/proc.h>
14#include <sys/ptrace.h>
15#include <sys/sysctl.h>
16#include <sys/user.h>
17
18#include <cstdio>
19
20#include "lldb/Host/Host.h"
21#include "lldb/Host/HostInfo.h"
22#include "lldb/Utility/DataBufferHeap.h"
23#include "lldb/Utility/DataExtractor.h"
24#include "lldb/Utility/Endian.h"
25#include "lldb/Utility/Log.h"
26#include "lldb/Utility/NameMatches.h"
27#include "lldb/Utility/ProcessInfo.h"
28#include "lldb/Utility/Status.h"
29#include "lldb/Utility/StreamString.h"
30
31#include "llvm/TargetParser/Host.h"
32
33extern "C" {
34extern char **environ;
35}
36
37using namespace lldb;
38using namespace lldb_private;
39
40namespace lldb_private {
41class ProcessLaunchInfo;
42}
43
44Environment Host::GetEnvironment() {
45Environment env;
46char *v;
47char **var = environ;
48for (; var != NULL && *var != NULL; ++var) {
49v = strchr(*var, (int)'-');
50if (v == NULL)
51continue;
52env.insert(v);
53}
54return env;
55}
56
57static bool
58GetOpenBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr,
59ProcessInstanceInfo &process_info) {
60if (process_info.ProcessIDIsValid()) {
61int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS,
62(int)process_info.GetProcessID()};
63
64char arg_data[8192];
65size_t arg_data_size = sizeof(arg_data);
66if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) == 0) {
67DataExtractor data(arg_data, arg_data_size, endian::InlHostByteOrder(),
68sizeof(void *));
69lldb::offset_t offset = 0;
70const char *cstr;
71
72cstr = data.GetCStr(&offset);
73if (cstr) {
74process_info.GetExecutableFile().SetFile(cstr, FileSpec::Style::native);
75
76if (!(match_info_ptr == NULL ||
77NameMatches(
78process_info.GetExecutableFile().GetFilename().GetCString(),
79match_info_ptr->GetNameMatchType(),
80match_info_ptr->GetProcessInfo().GetName())))
81return false;
82
83Args &proc_args = process_info.GetArguments();
84while (1) {
85const uint8_t *p = data.PeekData(offset, 1);
86while ((p != NULL) && (*p == '\0') && offset < arg_data_size) {
87++offset;
88p = data.PeekData(offset, 1);
89}
90if (p == NULL || offset >= arg_data_size)
91return true;
92
93cstr = data.GetCStr(&offset);
94if (cstr)
95proc_args.AppendArgument(llvm::StringRef(cstr));
96else
97return true;
98}
99}
100}
101}
102return false;
103}
104
105static bool GetOpenBSDProcessCPUType(ProcessInstanceInfo &process_info) {
106if (process_info.ProcessIDIsValid()) {
107process_info.GetArchitecture() =
108HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
109return true;
110}
111process_info.GetArchitecture().Clear();
112return false;
113}
114
115static bool GetOpenBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) {
116struct kinfo_proc proc_kinfo;
117size_t proc_kinfo_size;
118
119if (process_info.ProcessIDIsValid()) {
120int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID,
121(int)process_info.GetProcessID()};
122proc_kinfo_size = sizeof(struct kinfo_proc);
123
124if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0) {
125if (proc_kinfo_size > 0) {
126process_info.SetParentProcessID(proc_kinfo.p_ppid);
127process_info.SetUserID(proc_kinfo.p_ruid);
128process_info.SetGroupID(proc_kinfo.p_rgid);
129process_info.SetEffectiveUserID(proc_kinfo.p_uid);
130process_info.SetEffectiveGroupID(proc_kinfo.p_gid);
131return true;
132}
133}
134}
135process_info.SetParentProcessID(LLDB_INVALID_PROCESS_ID);
136process_info.SetUserID(UINT32_MAX);
137process_info.SetGroupID(UINT32_MAX);
138process_info.SetEffectiveUserID(UINT32_MAX);
139process_info.SetEffectiveGroupID(UINT32_MAX);
140return false;
141}
142
143uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
144ProcessInstanceInfoList &process_infos) {
145std::vector<struct kinfo_proc> kinfos;
146
147int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
148
149size_t pid_data_size = 0;
150if (::sysctl(mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
151return 0;
152
153// Add a few extra in case a few more show up
154const size_t estimated_pid_count =
155(pid_data_size / sizeof(struct kinfo_proc)) + 10;
156
157kinfos.resize(estimated_pid_count);
158pid_data_size = kinfos.size() * sizeof(struct kinfo_proc);
159
160if (::sysctl(mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
161return 0;
162
163const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc));
164
165bool all_users = match_info.GetMatchAllUsers();
166const ::pid_t our_pid = getpid();
167const uid_t our_uid = getuid();
168for (size_t i = 0; i < actual_pid_count; i++) {
169const struct kinfo_proc &kinfo = kinfos[i];
170const bool kinfo_user_matches = (all_users || (kinfo.p_ruid == our_uid) ||
171// Special case, if lldb is being run as
172// root we can attach to anything.
173(our_uid == 0));
174
175if (kinfo_user_matches == false || // Make sure the user is acceptable
176kinfo.p_pid == our_pid || // Skip this process
177kinfo.p_pid == 0 || // Skip kernel (kernel pid is zero)
178kinfo.p_stat == SZOMB || // Zombies are bad, they like brains...
179kinfo.p_psflags & PS_TRACED || // Being debugged?
180kinfo.p_flag & P_WEXIT) // Working on exiting
181continue;
182
183ProcessInstanceInfo process_info;
184process_info.SetProcessID(kinfo.p_pid);
185process_info.SetParentProcessID(kinfo.p_ppid);
186process_info.SetUserID(kinfo.p_ruid);
187process_info.SetGroupID(kinfo.p_rgid);
188process_info.SetEffectiveUserID(kinfo.p_svuid);
189process_info.SetEffectiveGroupID(kinfo.p_svgid);
190
191// Make sure our info matches before we go fetch the name and cpu type
192if (match_info.Matches(process_info) &&
193GetOpenBSDProcessArgs(&match_info, process_info)) {
194GetOpenBSDProcessCPUType(process_info);
195if (match_info.Matches(process_info))
196process_infos.push_back(process_info);
197}
198}
199
200return process_infos.size();
201}
202
203bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {
204process_info.SetProcessID(pid);
205
206if (GetOpenBSDProcessArgs(NULL, process_info)) {
207// should use libprocstat instead of going right into sysctl?
208GetOpenBSDProcessCPUType(process_info);
209GetOpenBSDProcessUserAndGroup(process_info);
210return true;
211}
212
213process_info.Clear();
214return false;
215}
216
217Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
218return Status("unimplemented");
219}
220