LLDB  mainline
freebsd/Host.cpp
Go to the documentation of this file.
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 <cstdio>
20 #include <dlfcn.h>
21 #include <execinfo.h>
22 
23 #include "lldb/Host/Host.h"
24 #include "lldb/Host/HostInfo.h"
27 #include "lldb/Utility/Endian.h"
28 #include "lldb/Utility/Log.h"
31 #include "lldb/Utility/Status.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
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 ||
82  match_info_ptr->GetNameMatchType(),
83  match_info_ptr->GetProcessInfo().GetName())))
84  return false;
85 
86  process_info.SetArg0(cstr);
87  Args &proc_args = process_info.GetArguments();
88  while (1) {
89  const uint8_t *p = data.PeekData(offset, 1);
90  while ((p != NULL) && (*p == '\0') && offset < arg_data_size) {
91  ++offset;
92  p = data.PeekData(offset, 1);
93  }
94  if (p == NULL || offset >= arg_data_size)
95  break;
96 
97  cstr = data.GetCStr(&offset);
98  if (!cstr)
99  break;
100 
101  proc_args.AppendArgument(llvm::StringRef(cstr));
102  }
103 
104  return true;
105 }
106 
107 static bool GetFreeBSDProcessCPUType(ProcessInstanceInfo &process_info) {
108  if (process_info.ProcessIDIsValid()) {
109  process_info.GetArchitecture() =
110  HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
111  return true;
112  }
113  process_info.GetArchitecture().Clear();
114  return false;
115 }
116 
118  struct kinfo_proc proc_kinfo;
119  size_t proc_kinfo_size;
120  const int pid = process_info.GetProcessID();
121  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
122 
123  if (!process_info.ProcessIDIsValid())
124  goto error;
125 
126  proc_kinfo_size = sizeof(struct kinfo_proc);
127 
128  if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) != 0)
129  goto error;
130 
131  if (proc_kinfo_size == 0)
132  goto error;
133 
134  process_info.SetParentProcessID(proc_kinfo.ki_ppid);
135  process_info.SetUserID(proc_kinfo.ki_ruid);
136  process_info.SetGroupID(proc_kinfo.ki_rgid);
137  process_info.SetEffectiveUserID(proc_kinfo.ki_uid);
138  if (proc_kinfo.ki_ngroups > 0)
139  process_info.SetEffectiveGroupID(proc_kinfo.ki_groups[0]);
140  else
141  process_info.SetEffectiveGroupID(UINT32_MAX);
142  return true;
143 
144 error:
146  process_info.SetUserID(UINT32_MAX);
147  process_info.SetGroupID(UINT32_MAX);
148  process_info.SetEffectiveUserID(UINT32_MAX);
149  process_info.SetEffectiveGroupID(UINT32_MAX);
150  return false;
151 }
152 
153 uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
154  ProcessInstanceInfoList &process_infos) {
155  const ::pid_t our_pid = ::getpid();
156  const ::uid_t our_uid = ::getuid();
157  std::vector<struct kinfo_proc> kinfos;
158  // Special case, if lldb is being run as root we can attach to anything.
159  bool all_users = match_info.GetMatchAllUsers() || (our_uid == 0);
160 
161  int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
162 
163  size_t pid_data_size = 0;
164  if (::sysctl(mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
165  return 0;
166 
167  // Add a few extra in case a few more show up
168  const size_t estimated_pid_count =
169  (pid_data_size / sizeof(struct kinfo_proc)) + 10;
170 
171  kinfos.resize(estimated_pid_count);
172  pid_data_size = kinfos.size() * sizeof(struct kinfo_proc);
173 
174  if (::sysctl(mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
175  return 0;
176 
177  const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc));
178 
179  ProcessInstanceInfoMatch match_info_noname{match_info};
180  match_info_noname.SetNameMatchType(NameMatch::Ignore);
181 
182  for (size_t i = 0; i < actual_pid_count; i++) {
183  const struct kinfo_proc &kinfo = kinfos[i];
184 
185  /* Make sure the user is acceptable */
186  if (!all_users && kinfo.ki_ruid != our_uid)
187  continue;
188 
189  if (kinfo.ki_pid == our_pid || // Skip this process
190  kinfo.ki_pid == 0 || // Skip kernel (kernel pid is 0)
191  kinfo.ki_stat == SZOMB || // Zombies are bad
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 have the same pid. Do not store the process info in the result
198  // list if a process with given identifier is already registered there.
199  bool already_registered = false;
200  for (uint32_t pi = 0;
201  !already_registered && (const int)kinfo.ki_numthreads > 1 &&
202  pi < (const uint32_t)process_infos.size();
203  pi++)
204  already_registered =
205  (process_infos[pi].GetProcessID() == (uint32_t)kinfo.ki_pid);
206 
207  if (already_registered)
208  continue;
209 
210  ProcessInstanceInfo process_info;
211  process_info.SetProcessID(kinfo.ki_pid);
212  process_info.SetParentProcessID(kinfo.ki_ppid);
213  process_info.SetUserID(kinfo.ki_ruid);
214  process_info.SetGroupID(kinfo.ki_rgid);
215  process_info.SetEffectiveUserID(kinfo.ki_svuid);
216  process_info.SetEffectiveGroupID(kinfo.ki_svgid);
217 
218  // Make sure our info matches before we go fetch the name and cpu type
219  if (match_info_noname.Matches(process_info) &&
220  GetFreeBSDProcessArgs(&match_info, process_info)) {
221  GetFreeBSDProcessCPUType(process_info);
222  if (match_info.Matches(process_info))
223  process_infos.push_back(process_info);
224  }
225  }
226 
227  return process_infos.size();
228 }
229 
230 bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {
231  process_info.SetProcessID(pid);
232 
233  if (GetFreeBSDProcessArgs(NULL, process_info)) {
234  // should use libprocstat instead of going right into sysctl?
235  GetFreeBSDProcessCPUType(process_info);
236  GetFreeBSDProcessUserAndGroup(process_info);
237  return true;
238  }
239 
240  process_info.Clear();
241  return false;
242 }
243 
244 Environment Host::GetEnvironment() { return Environment(environ); }
245 
246 Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
247  return Status("unimplemented");
248 }
lldb_private::ProcessInfo::GetArguments
Args & GetArguments()
Definition: ProcessInfo.h:75
lldb_private::ProcessInstanceInfo
Definition: ProcessInfo.h:108
lldb_private::ProcessInstanceInfoMatch::SetNameMatchType
void SetNameMatchType(NameMatch name_match_type)
Definition: ProcessInfo.h:185
lldb_private::ProcessInfo::ProcessIDIsValid
bool ProcessIDIsValid() const
Definition: ProcessInfo.h:71
lldb_private::ProcessInstanceInfoMatch::GetMatchAllUsers
bool GetMatchAllUsers() const
Definition: ProcessInfo.h:179
LLDB_INVALID_PROCESS_ID
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:93
lldb_private::ProcessInstanceInfo::Clear
void Clear()
Definition: ProcessInfo.h:116
Host.h
lldb_private::Args::AppendArgument
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:318
lldb_private::DataExtractor::PeekData
const uint8_t * PeekData(lldb::offset_t offset, lldb::offset_t length) const
Peek at a bytes at offset.
Definition: DataExtractor.h:832
lldb_private::ProcessInfo::SetUserID
void SetUserID(uint32_t uid)
Definition: ProcessInfo.h:57
lldb_private::ProcessInstanceInfoMatch
Definition: ProcessInfo.h:163
lldb_private::ProcessInfo::GetProcessID
lldb::pid_t GetProcessID() const
Definition: ProcessInfo.h:67
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::ProcessInfo::SetProcessID
void SetProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:69
lldb_private::ProcessInstanceInfo::SetEffectiveUserID
void SetEffectiveUserID(uint32_t uid)
Definition: ProcessInfo.h:131
lldb_private::Args
Definition: Args.h:33
lldb_private::ProcessInstanceInfoMatch::Matches
bool Matches(const ProcessInstanceInfo &proc_info) const
Definition: ProcessInfo.cpp:292
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
GetFreeBSDProcessCPUType
static bool GetFreeBSDProcessCPUType(ProcessInstanceInfo &process_info)
Definition: freebsd/Host.cpp:107
ProcessInfo.h
lldb_private::ProcessLaunchInfo
Definition: ProcessLaunchInfo.h:31
lldb_private::DataExtractor
Definition: DataExtractor.h:48
Log.h
lldb_private::ProcessInstanceInfo::SetEffectiveGroupID
void SetEffectiveGroupID(uint32_t gid)
Definition: ProcessInfo.h:133
NameMatches.h
lldb_private::ProcessInfo::SetArg0
void SetArg0(llvm::StringRef arg)
Definition: ProcessInfo.cpp:79
lldb_private::ProcessInstanceInfoMatch::GetProcessInfo
ProcessInstanceInfo & GetProcessInfo()
Definition: ProcessInfo.h:175
StreamString.h
GetFreeBSDProcessArgs
static bool GetFreeBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr, ProcessInstanceInfo &process_info)
Definition: freebsd/Host.cpp:48
lldb_private::ProcessInfo::GetName
const char * GetName() const
Definition: ProcessInfo.cpp:42
HostInfo.h
lldb_private::ProcessInstanceInfoMatch::GetNameMatchType
NameMatch GetNameMatchType() const
Definition: ProcessInfo.h:183
lldb_private::Status
Definition: Status.h:44
GetFreeBSDProcessUserAndGroup
static bool GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info)
Definition: freebsd/Host.cpp:117
uint32_t
lldb::pid_t
uint64_t pid_t
Definition: lldb-types.h:85
lldb_private::endian::InlHostByteOrder
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::Environment
Definition: Environment.h:18
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
DataExtractor.h
Status.h
lldb_private::ProcessInfo::GetArchitecture
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:61
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::DataExtractor::GetCStr
const char * GetCStr(lldb::offset_t *offset_ptr) const
Extract a C string from *offset_ptr.
Definition: DataExtractor.cpp:786
lldb_private::ProcessInfo::GetExecutableFile
FileSpec & GetExecutableFile()
Definition: ProcessInfo.h:42
environ
char ** environ
lldb_private::FileSpec::SetFile
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:174
lldb_private::ArchSpec::Clear
void Clear()
Clears the object state.
Definition: ArchSpec.cpp:527
lldb_private::ProcessInstanceInfoList
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList
Definition: Host.h:30
lldb
Definition: SBAddress.h:15
Endian.h
lldb_private::ProcessInstanceInfo::SetParentProcessID
void SetParentProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:137
lldb_private::FileSpec::GetFilename
ConstString & GetFilename()
Filename string get accessor.
Definition: FileSpec.cpp:341
lldb_private::ProcessInfo::SetGroupID
void SetGroupID(uint32_t gid)
Definition: ProcessInfo.h:59
lldb_private::NameMatches
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
Definition: NameMatches.cpp:15
DataBufferHeap.h