]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/freebsd/Host.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / freebsd / Host.cpp
1 //===-- source/Host/freebsd/Host.cpp ------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 // C Includes
12 #include <sys/types.h>
13
14 #include <sys/exec.h>
15 #include <sys/proc.h>
16 #include <sys/ptrace.h>
17 #include <sys/sysctl.h>
18 #include <sys/user.h>
19
20 #include <machine/elf.h>
21
22 #include <dlfcn.h>
23 #include <execinfo.h>
24 #include <stdio.h>
25
26 // C++ Includes
27 // Other libraries and framework includes
28 // Project includes
29 #include "lldb/Host/Host.h"
30 #include "lldb/Host/HostInfo.h"
31 #include "lldb/Target/Process.h"
32 #include "lldb/Utility/DataBufferHeap.h"
33 #include "lldb/Utility/DataExtractor.h"
34 #include "lldb/Utility/Endian.h"
35 #include "lldb/Utility/Log.h"
36 #include "lldb/Utility/NameMatches.h"
37 #include "lldb/Utility/Status.h"
38 #include "lldb/Utility/StreamString.h"
39
40 #include "llvm/Support/Host.h"
41
42 extern "C" {
43 extern char **environ;
44 }
45
46 using namespace lldb;
47 using namespace lldb_private;
48
49 static bool
50 GetFreeBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr,
51                       ProcessInstanceInfo &process_info) {
52   if (!process_info.ProcessIDIsValid())
53     return false;
54
55   int pid = process_info.GetProcessID();
56
57   int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, pid};
58
59   char arg_data[8192];
60   size_t arg_data_size = sizeof(arg_data);
61   if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) != 0)
62     return false;
63
64   DataExtractor data(arg_data, arg_data_size, endian::InlHostByteOrder(),
65                      sizeof(void *));
66   lldb::offset_t offset = 0;
67   const char *cstr;
68
69   cstr = data.GetCStr(&offset);
70   if (!cstr)
71     return false;
72
73   // Get pathname for pid. If that fails fall back to argv[0].
74   char pathname[MAXPATHLEN];
75   size_t pathname_len = sizeof(pathname);
76   mib[2] = KERN_PROC_PATHNAME;
77   if (::sysctl(mib, 4, pathname, &pathname_len, NULL, 0) == 0)
78     process_info.GetExecutableFile().SetFile(pathname, false,
79                                              FileSpec::Style::native);
80   else
81     process_info.GetExecutableFile().SetFile(cstr, false,
82                                              FileSpec::Style::native);
83
84   if (!(match_info_ptr == NULL ||
85         NameMatches(process_info.GetExecutableFile().GetFilename().GetCString(),
86                     match_info_ptr->GetNameMatchType(),
87                     match_info_ptr->GetProcessInfo().GetName())))
88     return false;
89
90   Args &proc_args = process_info.GetArguments();
91   while (1) {
92     const uint8_t *p = data.PeekData(offset, 1);
93     while ((p != NULL) && (*p == '\0') && offset < arg_data_size) {
94       ++offset;
95       p = data.PeekData(offset, 1);
96     }
97     if (p == NULL || offset >= arg_data_size)
98       break;
99
100     cstr = data.GetCStr(&offset);
101     if (!cstr)
102       break;
103
104     proc_args.AppendArgument(llvm::StringRef(cstr));
105   }
106
107   return true;
108 }
109
110 static bool GetFreeBSDProcessCPUType(ProcessInstanceInfo &process_info) {
111   if (process_info.ProcessIDIsValid()) {
112     process_info.GetArchitecture() =
113         HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
114     return true;
115   }
116   process_info.GetArchitecture().Clear();
117   return false;
118 }
119
120 static bool GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) {
121   struct kinfo_proc proc_kinfo;
122   size_t proc_kinfo_size;
123   const int pid = process_info.GetProcessID();
124   int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
125
126   if (!process_info.ProcessIDIsValid())
127     goto error;
128
129   proc_kinfo_size = sizeof(struct kinfo_proc);
130
131   if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) != 0)
132     goto error;
133
134   if (proc_kinfo_size == 0)
135     goto error;
136
137   process_info.SetParentProcessID(proc_kinfo.ki_ppid);
138   process_info.SetUserID(proc_kinfo.ki_ruid);
139   process_info.SetGroupID(proc_kinfo.ki_rgid);
140   process_info.SetEffectiveUserID(proc_kinfo.ki_uid);
141   if (proc_kinfo.ki_ngroups > 0)
142     process_info.SetEffectiveGroupID(proc_kinfo.ki_groups[0]);
143   else
144     process_info.SetEffectiveGroupID(UINT32_MAX);
145   return true;
146
147 error:
148   process_info.SetParentProcessID(LLDB_INVALID_PROCESS_ID);
149   process_info.SetUserID(UINT32_MAX);
150   process_info.SetGroupID(UINT32_MAX);
151   process_info.SetEffectiveUserID(UINT32_MAX);
152   process_info.SetEffectiveGroupID(UINT32_MAX);
153   return false;
154 }
155
156 uint32_t Host::FindProcesses(const ProcessInstanceInfoMatch &match_info,
157                              ProcessInstanceInfoList &process_infos) {
158   const ::pid_t our_pid = ::getpid();
159   const ::uid_t our_uid = ::getuid();
160   std::vector<struct kinfo_proc> kinfos;
161   // Special case, if lldb is being run as root we can attach to anything.
162   bool all_users = match_info.GetMatchAllUsers() || (our_uid == 0);
163
164   int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
165
166   size_t pid_data_size = 0;
167   if (::sysctl(mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
168     return 0;
169
170   // Add a few extra in case a few more show up
171   const size_t estimated_pid_count =
172       (pid_data_size / sizeof(struct kinfo_proc)) + 10;
173
174   kinfos.resize(estimated_pid_count);
175   pid_data_size = kinfos.size() * sizeof(struct kinfo_proc);
176
177   if (::sysctl(mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
178     return 0;
179
180   const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc));
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.GetSize();
203          pi++)
204       already_registered =
205           (process_infos.GetProcessIDAtIndex(pi) == (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.Matches(process_info) &&
220         GetFreeBSDProcessArgs(&match_info, process_info)) {
221       GetFreeBSDProcessCPUType(process_info);
222       if (match_info.Matches(process_info))
223         process_infos.Append(process_info);
224     }
225   }
226
227   return process_infos.GetSize();
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 }