LLDB mainline
openbsd/Host.cpp
Go to the documentation of this file.
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"
24#include "lldb/Utility/Endian.h"
25#include "lldb/Utility/Log.h"
28#include "lldb/Utility/Status.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 {
42}
43
45 Environment env;
46 char *v;
47 char **var = environ;
48 for (; var != NULL && *var != NULL; ++var) {
49 v = strchr(*var, (int)'-');
50 if (v == NULL)
51 continue;
52 env.insert(v);
53 }
54 return env;
55}
56
57static bool
59 ProcessInstanceInfo &process_info) {
60 if (process_info.ProcessIDIsValid()) {
61 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS,
62 (int)process_info.GetProcessID()};
63
64 char arg_data[8192];
65 size_t arg_data_size = sizeof(arg_data);
66 if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) == 0) {
67 DataExtractor data(arg_data, arg_data_size, endian::InlHostByteOrder(),
68 sizeof(void *));
69 lldb::offset_t offset = 0;
70 const char *cstr;
71
72 cstr = data.GetCStr(&offset);
73 if (cstr) {
74 process_info.GetExecutableFile().SetFile(cstr, FileSpec::Style::native);
75
76 if (!(match_info_ptr == NULL ||
78 process_info.GetExecutableFile().GetFilename().GetCString(),
79 match_info_ptr->GetNameMatchType(),
80 match_info_ptr->GetProcessInfo().GetName())))
81 return false;
82
83 Args &proc_args = process_info.GetArguments();
84 while (1) {
85 const uint8_t *p = data.PeekData(offset, 1);
86 while ((p != NULL) && (*p == '\0') && offset < arg_data_size) {
87 ++offset;
88 p = data.PeekData(offset, 1);
89 }
90 if (p == NULL || offset >= arg_data_size)
91 return true;
92
93 cstr = data.GetCStr(&offset);
94 if (cstr)
95 proc_args.AppendArgument(llvm::StringRef(cstr));
96 else
97 return true;
98 }
99 }
100 }
101 }
102 return false;
103}
104
106 if (process_info.ProcessIDIsValid()) {
107 process_info.GetArchitecture() =
108 HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
109 return true;
110 }
111 process_info.GetArchitecture().Clear();
112 return false;
113}
114
116 struct kinfo_proc proc_kinfo;
117 size_t proc_kinfo_size;
118
119 if (process_info.ProcessIDIsValid()) {
120 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID,
121 (int)process_info.GetProcessID()};
122 proc_kinfo_size = sizeof(struct kinfo_proc);
123
124 if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0) {
125 if (proc_kinfo_size > 0) {
126 process_info.SetParentProcessID(proc_kinfo.p_ppid);
127 process_info.SetUserID(proc_kinfo.p_ruid);
128 process_info.SetGroupID(proc_kinfo.p_rgid);
129 process_info.SetEffectiveUserID(proc_kinfo.p_uid);
130 process_info.SetEffectiveGroupID(proc_kinfo.p_gid);
131 return true;
132 }
133 }
134 }
136 process_info.SetUserID(UINT32_MAX);
137 process_info.SetGroupID(UINT32_MAX);
138 process_info.SetEffectiveUserID(UINT32_MAX);
139 process_info.SetEffectiveGroupID(UINT32_MAX);
140 return false;
141}
142
143uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
144 ProcessInstanceInfoList &process_infos) {
145 std::vector<struct kinfo_proc> kinfos;
146
147 int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
148
149 size_t pid_data_size = 0;
150 if (::sysctl(mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
151 return 0;
152
153 // Add a few extra in case a few more show up
154 const size_t estimated_pid_count =
155 (pid_data_size / sizeof(struct kinfo_proc)) + 10;
156
157 kinfos.resize(estimated_pid_count);
158 pid_data_size = kinfos.size() * sizeof(struct kinfo_proc);
159
160 if (::sysctl(mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
161 return 0;
162
163 const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc));
164
165 bool all_users = match_info.GetMatchAllUsers();
166 const ::pid_t our_pid = getpid();
167 const uid_t our_uid = getuid();
168 for (size_t i = 0; i < actual_pid_count; i++) {
169 const struct kinfo_proc &kinfo = kinfos[i];
170 const 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
175 if (kinfo_user_matches == false || // Make sure the user is acceptable
176 kinfo.p_pid == our_pid || // Skip this process
177 kinfo.p_pid == 0 || // Skip kernel (kernel pid is zero)
178 kinfo.p_stat == SZOMB || // Zombies are bad, they like brains...
179 kinfo.p_psflags & PS_TRACED || // Being debugged?
180 kinfo.p_flag & P_WEXIT) // Working on exiting
181 continue;
182
183 ProcessInstanceInfo process_info;
184 process_info.SetProcessID(kinfo.p_pid);
185 process_info.SetParentProcessID(kinfo.p_ppid);
186 process_info.SetUserID(kinfo.p_ruid);
187 process_info.SetGroupID(kinfo.p_rgid);
188 process_info.SetEffectiveUserID(kinfo.p_svuid);
189 process_info.SetEffectiveGroupID(kinfo.p_svgid);
190
191 // Make sure our info matches before we go fetch the name and cpu type
192 if (match_info.Matches(process_info) &&
193 GetOpenBSDProcessArgs(&match_info, process_info)) {
194 GetOpenBSDProcessCPUType(process_info);
195 if (match_info.Matches(process_info))
196 process_infos.push_back(process_info);
197 }
198 }
199
200 return process_infos.size();
201}
202
204 process_info.SetProcessID(pid);
205
206 if (GetOpenBSDProcessArgs(NULL, process_info)) {
207 // should use libprocstat instead of going right into sysctl?
208 GetOpenBSDProcessCPUType(process_info);
209 GetOpenBSDProcessUserAndGroup(process_info);
210 return true;
211 }
212
213 process_info.Clear();
214 return false;
215}
216
218 return Status("unimplemented");
219}
void Clear()
Clears the object state.
Definition: ArchSpec.cpp:542
A command line argument class.
Definition: Args.h:33
void AppendArgument(llvm::StringRef arg_str, char quote_char='\0')
Appends a new argument to the end of the list argument list.
Definition: Args.cpp:322
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
An data extractor class.
Definition: DataExtractor.h:48
const char * GetCStr(lldb::offset_t *offset_ptr) const
Extract a C string from *offset_ptr.
const uint8_t * PeekData(lldb::offset_t offset, lldb::offset_t length) const
Peek at a bytes at offset.
std::pair< iterator, bool > insert(llvm::StringRef KeyEqValue)
Definition: Environment.h:71
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:174
const ConstString & GetFilename() const
Filename string const get accessor.
Definition: FileSpec.h:240
static Status ShellExpandArguments(ProcessLaunchInfo &launch_info)
Perform expansion of the command-line for this launch info This can potentially involve wildcard expa...
static Environment GetEnvironment()
static uint32_t FindProcessesImpl(const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &proc_infos)
static bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info)
void SetGroupID(uint32_t gid)
Definition: ProcessInfo.h:60
bool ProcessIDIsValid() const
Definition: ProcessInfo.h:72
const char * GetName() const
Definition: ProcessInfo.cpp:45
lldb::pid_t GetProcessID() const
Definition: ProcessInfo.h:68
void SetProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:70
FileSpec & GetExecutableFile()
Definition: ProcessInfo.h:43
void SetUserID(uint32_t uid)
Definition: ProcessInfo.h:58
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:62
bool Matches(const ProcessInstanceInfo &proc_info) const
ProcessInstanceInfo & GetProcessInfo()
Definition: ProcessInfo.h:308
void SetEffectiveGroupID(uint32_t gid)
Definition: ProcessInfo.h:170
void SetParentProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:174
void SetEffectiveUserID(uint32_t uid)
Definition: ProcessInfo.h:168
An error handling class.
Definition: Status.h:44
char ** environ
#define UINT32_MAX
Definition: lldb-defines.h:19
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:89
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
A class that represents a running process on the host machine.
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
Definition: NameMatches.cpp:15
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList
Definition: Host.h:32
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:85
uint64_t pid_t
Definition: lldb-types.h:83
static bool GetOpenBSDProcessUserAndGroup(ProcessInstanceInfo &process_info)
static bool GetOpenBSDProcessCPUType(ProcessInstanceInfo &process_info)
char ** environ
static bool GetOpenBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr, ProcessInstanceInfo &process_info)