]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/freebsd/Host.cpp
Update LLDB snapshot to upstream r225923 (git 2b588ecd)
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / freebsd / Host.cpp
1 //===-- source/Host/freebsd/Host.cpp ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // C Includes
11 #include <stdio.h>
12 #include <dlfcn.h>
13 #include <execinfo.h>
14 #include <sys/types.h>
15 #include <sys/user.h>
16 #include <sys/sysctl.h>
17 #include <sys/proc.h>
18
19 #include <sys/ptrace.h>
20 #include <sys/exec.h>
21 #include <machine/elf.h>
22
23 // C++ Includes
24 // Other libraries and framework includes
25 // Project includes
26 #include "lldb/Core/Error.h"
27 #include "lldb/Host/Endian.h"
28 #include "lldb/Host/Host.h"
29 #include "lldb/Host/HostInfo.h"
30 #include "lldb/Core/Module.h"
31 #include "lldb/Core/DataExtractor.h"
32 #include "lldb/Core/StreamFile.h"
33 #include "lldb/Core/StreamString.h"
34 #include "lldb/Core/Log.h"
35 #include "lldb/Target/Process.h"
36 #include "lldb/Target/Platform.h"
37
38 #include "lldb/Core/DataBufferHeap.h"
39 #include "lldb/Core/DataExtractor.h"
40 #include "lldb/Utility/CleanUp.h"
41
42 #include "Plugins/Process/Utility/FreeBSDSignals.h"
43
44 #include "llvm/Support/Host.h"
45
46 extern "C" {
47     extern char **environ;
48 }
49
50 using namespace lldb;
51 using namespace lldb_private;
52
53 void
54 Host::Backtrace (Stream &strm, uint32_t max_frames)
55 {
56     char backtrace_path[] = "/tmp/lldb-backtrace-tmp-XXXXXX";
57     int backtrace_fd = ::mkstemp (backtrace_path);
58     if (backtrace_fd != -1)
59     {
60         std::vector<void *> frame_buffer (max_frames, NULL);
61         int count = ::backtrace (&frame_buffer[0], frame_buffer.size());
62         ::backtrace_symbols_fd (&frame_buffer[0], count, backtrace_fd);
63
64         const off_t buffer_size = ::lseek(backtrace_fd, 0, SEEK_CUR);
65
66         if (::lseek(backtrace_fd, 0, SEEK_SET) == 0)
67         {
68             char *buffer = (char *)::malloc (buffer_size);
69             if (buffer)
70             {
71                 ssize_t bytes_read = ::read (backtrace_fd, buffer, buffer_size);
72                 if (bytes_read > 0)
73                     strm.Write(buffer, bytes_read);
74                 ::free (buffer);
75             }
76         }
77         ::close (backtrace_fd);
78         ::unlink (backtrace_path);
79     }
80 }
81
82 size_t
83 Host::GetEnvironment (StringList &env)
84 {
85     char *v;
86     char **var = environ;
87     for (; var != NULL && *var != NULL; ++var)
88     {
89         v = strchr(*var, (int)'-');
90         if (v == NULL)
91             continue;
92         env.AppendString(v);
93     }
94     return env.GetSize();
95 }
96
97 static bool
98 GetFreeBSDProcessArgs (const ProcessInstanceInfoMatch *match_info_ptr,
99                       ProcessInstanceInfo &process_info)
100 {
101     if (process_info.ProcessIDIsValid())
102     {
103         int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ARGS, (int)process_info.GetProcessID() };
104
105         char arg_data[8192];
106         size_t arg_data_size = sizeof(arg_data);
107         if (::sysctl (mib, 4, arg_data, &arg_data_size , NULL, 0) == 0)
108         {
109             DataExtractor data (arg_data, arg_data_size, lldb::endian::InlHostByteOrder(), sizeof(void *));
110             lldb::offset_t offset = 0;
111             const char *cstr;
112
113             cstr = data.GetCStr (&offset);
114             if (cstr)
115             {
116                 process_info.GetExecutableFile().SetFile(cstr, false);
117
118                 if (!(match_info_ptr == NULL ||
119                     NameMatches (process_info.GetExecutableFile().GetFilename().GetCString(),
120                                  match_info_ptr->GetNameMatchType(),
121                                  match_info_ptr->GetProcessInfo().GetName())))
122                     return false;
123
124                 Args &proc_args = process_info.GetArguments();
125                 while (1)
126                 {
127                     const uint8_t *p = data.PeekData(offset, 1);
128                     while ((p != NULL) && (*p == '\0') && offset < arg_data_size)
129                     {
130                         ++offset;
131                         p = data.PeekData(offset, 1);
132                     }
133                     if (p == NULL || offset >= arg_data_size)
134                         return true;
135
136                     cstr = data.GetCStr(&offset);
137                     if (cstr)
138                         proc_args.AppendArgument(cstr);
139                     else
140                         return true;
141                 }
142             }
143         }
144     }
145     return false;
146 }
147
148 static bool
149 GetFreeBSDProcessCPUType (ProcessInstanceInfo &process_info)
150 {
151     if (process_info.ProcessIDIsValid())
152     {
153         process_info.GetArchitecture() = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
154         return true;
155     }
156     process_info.GetArchitecture().Clear();
157     return false;
158 }
159
160 static bool
161 GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info)
162 {
163     struct kinfo_proc proc_kinfo;
164     size_t proc_kinfo_size;
165
166     if (process_info.ProcessIDIsValid())
167     {
168         int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID,
169             (int)process_info.GetProcessID() };
170         proc_kinfo_size = sizeof(struct kinfo_proc);
171
172         if (::sysctl (mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0)
173         {
174             if (proc_kinfo_size > 0)
175             {
176                 process_info.SetParentProcessID (proc_kinfo.ki_ppid);
177                 process_info.SetUserID (proc_kinfo.ki_ruid);
178                 process_info.SetGroupID (proc_kinfo.ki_rgid);
179                 process_info.SetEffectiveUserID (proc_kinfo.ki_uid);
180                 if (proc_kinfo.ki_ngroups > 0)
181                     process_info.SetEffectiveGroupID (proc_kinfo.ki_groups[0]);
182                 else
183                     process_info.SetEffectiveGroupID (UINT32_MAX);
184                 return true;
185             }
186         }
187     }
188     process_info.SetParentProcessID (LLDB_INVALID_PROCESS_ID);
189     process_info.SetUserID (UINT32_MAX);
190     process_info.SetGroupID (UINT32_MAX);
191     process_info.SetEffectiveUserID (UINT32_MAX);
192     process_info.SetEffectiveGroupID (UINT32_MAX);
193     return false;
194 }
195
196 uint32_t
197 Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
198 {
199     std::vector<struct kinfo_proc> kinfos;
200
201     int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL };
202
203     size_t pid_data_size = 0;
204     if (::sysctl (mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
205         return 0;
206
207     // Add a few extra in case a few more show up
208     const size_t estimated_pid_count = (pid_data_size / sizeof(struct kinfo_proc)) + 10;
209
210     kinfos.resize (estimated_pid_count);
211     pid_data_size = kinfos.size() * sizeof(struct kinfo_proc);
212
213     if (::sysctl (mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
214         return 0;
215
216     const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc));
217
218     bool all_users = match_info.GetMatchAllUsers();
219     const ::pid_t our_pid = getpid();
220     const uid_t our_uid = getuid();
221     for (size_t i = 0; i < actual_pid_count; i++)
222     {
223         const struct kinfo_proc &kinfo = kinfos[i];
224         const bool kinfo_user_matches = (all_users ||
225                                          (kinfo.ki_ruid == our_uid) ||
226                                          // Special case, if lldb is being run as root we can attach to anything.
227                                          (our_uid == 0)
228                                          );
229
230         if (kinfo_user_matches == false      || // Make sure the user is acceptable
231             kinfo.ki_pid == our_pid          || // Skip this process
232             kinfo.ki_pid == 0                || // Skip kernel (kernel pid is zero)
233             kinfo.ki_stat == SZOMB    || // Zombies are bad, they like brains...
234             kinfo.ki_flag & P_TRACED  || // Being debugged?
235             kinfo.ki_flag & P_WEXIT)     // Working on exiting
236             continue;
237
238         // Every thread is a process in FreeBSD, but all the threads of a single process
239         // have the same pid. Do not store the process info in the result list if a process
240         // with given identifier is already registered there.
241         bool already_registered = false;
242         for (uint32_t pi = 0;
243              !already_registered &&
244              (const int)kinfo.ki_numthreads > 1 &&
245              pi < (const uint32_t)process_infos.GetSize(); pi++)
246             already_registered = (process_infos.GetProcessIDAtIndex(pi) == (uint32_t)kinfo.ki_pid);
247
248         if (already_registered)
249             continue;
250
251         ProcessInstanceInfo process_info;
252         process_info.SetProcessID (kinfo.ki_pid);
253         process_info.SetParentProcessID (kinfo.ki_ppid);
254         process_info.SetUserID (kinfo.ki_ruid);
255         process_info.SetGroupID (kinfo.ki_rgid);
256         process_info.SetEffectiveUserID (kinfo.ki_svuid);
257         process_info.SetEffectiveGroupID (kinfo.ki_svgid);
258
259         // Make sure our info matches before we go fetch the name and cpu type
260         if (match_info.Matches (process_info) &&
261             GetFreeBSDProcessArgs (&match_info, process_info))
262         {
263             GetFreeBSDProcessCPUType (process_info);
264             if (match_info.Matches (process_info))
265                 process_infos.Append (process_info);
266         }
267     }
268
269     return process_infos.GetSize();
270 }
271
272 bool
273 Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
274 {
275     process_info.SetProcessID(pid);
276
277     if (GetFreeBSDProcessArgs(NULL, process_info))
278     {
279         // should use libprocstat instead of going right into sysctl?
280         GetFreeBSDProcessCPUType(process_info);
281         GetFreeBSDProcessUserAndGroup(process_info);
282         return true;
283     }
284
285     process_info.Clear();
286     return false;
287 }
288
289 lldb::DataBufferSP
290 Host::GetAuxvData(lldb_private::Process *process)
291 {
292    int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_AUXV, 0 };
293    size_t auxv_size = AT_COUNT * sizeof(Elf_Auxinfo);
294    DataBufferSP buf_sp;
295
296    std::unique_ptr<DataBufferHeap> buf_ap(new DataBufferHeap(auxv_size, 0));
297
298    mib[3] = process->GetID();
299    if (::sysctl(mib, 4, buf_ap->GetBytes(), &auxv_size, NULL, 0) == 0) {
300            buf_sp.reset(buf_ap.release());
301    } else {
302            perror("sysctl failed on auxv");
303    }
304
305    return buf_sp;
306 }
307
308 const UnixSignalsSP&
309 Host::GetUnixSignals ()
310 {
311     static const lldb_private::UnixSignalsSP s_unix_signals_sp (new FreeBSDSignals ());
312     return s_unix_signals_sp;
313 }
314