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