blob: cd2d481426e34c483d91556ba78a5667fe5108c5 [file] [log] [blame]
Stephen Wilson3e2a18f2011-03-23 01:58:26 +00001//===-- source/Host/linux/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 <sys/utsname.h>
Johnny Chen30213ff2012-01-05 19:17:38 +000013#include <sys/types.h>
14#include <sys/stat.h>
Daniel Malea25d7eb02013-05-15 17:54:07 +000015#include <dirent.h>
Johnny Chen30213ff2012-01-05 19:17:38 +000016#include <fcntl.h>
Todd Fialacacde7d2014-09-27 16:54:22 +000017#ifndef __ANDROID__
Michael Sartain3cf443d2013-07-17 00:26:30 +000018#include <execinfo.h>
Todd Fialacacde7d2014-09-27 16:54:22 +000019#endif
Johnny Chen30213ff2012-01-05 19:17:38 +000020
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000021// C++ Includes
22// Other libraries and framework includes
23// Project includes
Johnny Chenc18a5382011-05-19 23:07:19 +000024#include "lldb/Core/Error.h"
Todd Fialaf3d61de2014-01-17 20:18:59 +000025#include "lldb/Core/Log.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000026#include "lldb/Target/Process.h"
27
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000028#include "lldb/Host/Host.h"
Shawn Best8da0bf32014-11-08 01:41:49 +000029#ifdef __ANDROID_NDK__
30#include "lldb/Host/android/Android.h"
31#endif
Johnny Chen30213ff2012-01-05 19:17:38 +000032#include "lldb/Core/DataBufferHeap.h"
33#include "lldb/Core/DataExtractor.h"
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000034
Michael Sartainc836ae72013-05-23 20:57:03 +000035#include "lldb/Core/ModuleSpec.h"
36#include "lldb/Symbol/ObjectFile.h"
Todd Fiala3dc2fb22014-06-30 04:14:13 +000037#include "Plugins/Process/Linux/ProcFileReader.h"
Todd Fiala4ceced32014-08-29 17:35:57 +000038#include "Plugins/Process/Utility/LinuxSignals.h"
Michael Sartainc836ae72013-05-23 20:57:03 +000039
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000040using namespace lldb;
41using namespace lldb_private;
42
Daniel Malea25d7eb02013-05-15 17:54:07 +000043typedef enum ProcessStateFlags
44{
45 eProcessStateRunning = (1u << 0), // Running
46 eProcessStateSleeping = (1u << 1), // Sleeping in an interruptible wait
47 eProcessStateWaiting = (1u << 2), // Waiting in an uninterruptible disk sleep
48 eProcessStateZombie = (1u << 3), // Zombie
49 eProcessStateTracedOrStopped = (1u << 4), // Traced or stopped (on a signal)
50 eProcessStatePaging = (1u << 5) // Paging
51} ProcessStateFlags;
52
53typedef struct ProcessStatInfo
54{
55 lldb::pid_t ppid; // Parent Process ID
56 uint32_t fProcessState; // ProcessStateFlags
57} ProcessStatInfo;
58
59// Get the process info with additional information from /proc/$PID/stat (like process state, and tracer pid).
60static bool GetProcessAndStatInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info, ProcessStatInfo &stat_info, lldb::pid_t &tracerpid);
61
Daniel Malea25d7eb02013-05-15 17:54:07 +000062static bool
63ReadProcPseudoFileStat (lldb::pid_t pid, ProcessStatInfo& stat_info)
64{
65 // Read the /proc/$PID/stat file.
Todd Fiala3dc2fb22014-06-30 04:14:13 +000066 lldb::DataBufferSP buf_sp = ProcFileReader::ReadIntoDataBuffer (pid, "stat");
Daniel Malea25d7eb02013-05-15 17:54:07 +000067
68 // The filename of the executable is stored in parenthesis right after the pid. We look for the closing
69 // parenthesis for the filename and work from there in case the name has something funky like ')' in it.
70 const char *filename_end = strrchr ((const char *)buf_sp->GetBytes(), ')');
71 if (filename_end)
72 {
73 char state = '\0';
74 int ppid = LLDB_INVALID_PROCESS_ID;
75
76 // Read state and ppid.
77 sscanf (filename_end + 1, " %c %d", &state, &ppid);
78
79 stat_info.ppid = ppid;
80
81 switch (state)
82 {
83 case 'R':
84 stat_info.fProcessState |= eProcessStateRunning;
85 break;
86 case 'S':
87 stat_info.fProcessState |= eProcessStateSleeping;
88 break;
89 case 'D':
90 stat_info.fProcessState |= eProcessStateWaiting;
91 break;
92 case 'Z':
93 stat_info.fProcessState |= eProcessStateZombie;
94 break;
95 case 'T':
96 stat_info.fProcessState |= eProcessStateTracedOrStopped;
97 break;
98 case 'W':
99 stat_info.fProcessState |= eProcessStatePaging;
100 break;
101 }
102
103 return true;
104 }
105
106 return false;
107}
108
109static void
110GetLinuxProcessUserAndGroup (lldb::pid_t pid, ProcessInstanceInfo &process_info, lldb::pid_t &tracerpid)
111{
112 tracerpid = 0;
113 uint32_t rUid = UINT32_MAX; // Real User ID
114 uint32_t eUid = UINT32_MAX; // Effective User ID
115 uint32_t rGid = UINT32_MAX; // Real Group ID
116 uint32_t eGid = UINT32_MAX; // Effective Group ID
117
118 // Read the /proc/$PID/status file and parse the Uid:, Gid:, and TracerPid: fields.
Todd Fiala3dc2fb22014-06-30 04:14:13 +0000119 lldb::DataBufferSP buf_sp = ProcFileReader::ReadIntoDataBuffer (pid, "status");
Daniel Malea25d7eb02013-05-15 17:54:07 +0000120
121 static const char uid_token[] = "Uid:";
122 char *buf_uid = strstr ((char *)buf_sp->GetBytes(), uid_token);
123 if (buf_uid)
124 {
125 // Real, effective, saved set, and file system UIDs. Read the first two.
126 buf_uid += sizeof(uid_token);
127 rUid = strtol (buf_uid, &buf_uid, 10);
128 eUid = strtol (buf_uid, &buf_uid, 10);
129 }
130
131 static const char gid_token[] = "Gid:";
132 char *buf_gid = strstr ((char *)buf_sp->GetBytes(), gid_token);
133 if (buf_gid)
134 {
135 // Real, effective, saved set, and file system GIDs. Read the first two.
136 buf_gid += sizeof(gid_token);
137 rGid = strtol (buf_gid, &buf_gid, 10);
138 eGid = strtol (buf_gid, &buf_gid, 10);
139 }
140
141 static const char tracerpid_token[] = "TracerPid:";
142 char *buf_tracerpid = strstr((char *)buf_sp->GetBytes(), tracerpid_token);
143 if (buf_tracerpid)
144 {
145 // Tracer PID. 0 if we're not being debugged.
146 buf_tracerpid += sizeof(tracerpid_token);
147 tracerpid = strtol (buf_tracerpid, &buf_tracerpid, 10);
148 }
149
150 process_info.SetUserID (rUid);
151 process_info.SetEffectiveUserID (eUid);
152 process_info.SetGroupID (rGid);
153 process_info.SetEffectiveGroupID (eGid);
154}
155
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000156lldb::DataBufferSP
157Host::GetAuxvData(lldb_private::Process *process)
158{
Todd Fiala3dc2fb22014-06-30 04:14:13 +0000159 return ProcFileReader::ReadIntoDataBuffer (process->GetID(), "auxv");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000160}
161
Todd Fialaaf245d12014-06-30 21:05:18 +0000162lldb::DataBufferSP
163Host::GetAuxvData (lldb::pid_t pid)
164{
165 return ProcFileReader::ReadIntoDataBuffer (pid, "auxv");
166}
167
Daniel Malea25d7eb02013-05-15 17:54:07 +0000168static bool
169IsDirNumeric(const char *dname)
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000170{
Daniel Malea25d7eb02013-05-15 17:54:07 +0000171 for (; *dname; dname++)
172 {
173 if (!isdigit (*dname))
174 return false;
175 }
176 return true;
177}
178
179uint32_t
180Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
181{
182 static const char procdir[] = "/proc/";
183
184 DIR *dirproc = opendir (procdir);
185 if (dirproc)
186 {
187 struct dirent *direntry = NULL;
188 const uid_t our_uid = getuid();
189 const lldb::pid_t our_pid = getpid();
190 bool all_users = match_info.GetMatchAllUsers();
191
192 while ((direntry = readdir (dirproc)) != NULL)
193 {
194 if (direntry->d_type != DT_DIR || !IsDirNumeric (direntry->d_name))
195 continue;
196
197 lldb::pid_t pid = atoi (direntry->d_name);
198
199 // Skip this process.
200 if (pid == our_pid)
201 continue;
202
203 lldb::pid_t tracerpid;
204 ProcessStatInfo stat_info;
205 ProcessInstanceInfo process_info;
206
207 if (!GetProcessAndStatInfo (pid, process_info, stat_info, tracerpid))
208 continue;
209
210 // Skip if process is being debugged.
211 if (tracerpid != 0)
212 continue;
213
214 // Skip zombies.
215 if (stat_info.fProcessState & eProcessStateZombie)
216 continue;
217
218 // Check for user match if we're not matching all users and not running as root.
219 if (!all_users && (our_uid != 0) && (process_info.GetUserID() != our_uid))
220 continue;
221
222 if (match_info.Matches (process_info))
223 {
224 process_infos.Append (process_info);
225 }
226 }
227
228 closedir (dirproc);
229 }
230
231 return process_infos.GetSize();
232}
233
Matt Kopec085d6ce2013-05-31 22:00:07 +0000234bool
235Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach)
236{
237 bool tids_changed = false;
238 static const char procdir[] = "/proc/";
239 static const char taskdir[] = "/task/";
240 std::string process_task_dir = procdir + std::to_string(pid) + taskdir;
241 DIR *dirproc = opendir (process_task_dir.c_str());
242
243 if (dirproc)
244 {
245 struct dirent *direntry = NULL;
246 while ((direntry = readdir (dirproc)) != NULL)
247 {
248 if (direntry->d_type != DT_DIR || !IsDirNumeric (direntry->d_name))
249 continue;
250
251 lldb::tid_t tid = atoi(direntry->d_name);
252 TidMap::iterator it = tids_to_attach.find(tid);
253 if (it == tids_to_attach.end())
254 {
255 tids_to_attach.insert(TidPair(tid, false));
256 tids_changed = true;
257 }
258 }
259 closedir (dirproc);
260 }
261
262 return tids_changed;
263}
264
Daniel Malea25d7eb02013-05-15 17:54:07 +0000265static bool
Michael Sartainc836ae72013-05-23 20:57:03 +0000266GetELFProcessCPUType (const char *exe_path, ProcessInstanceInfo &process_info)
267{
268 // Clear the architecture.
269 process_info.GetArchitecture().Clear();
270
271 ModuleSpecList specs;
272 FileSpec filespec (exe_path, false);
Jason Molendad6cfc162013-07-15 03:25:21 +0000273 const size_t num_specs = ObjectFile::GetModuleSpecifications (filespec, 0, 0, specs);
Michael Sartainc836ae72013-05-23 20:57:03 +0000274 // GetModuleSpecifications() could fail if the executable has been deleted or is locked.
275 // But it shouldn't return more than 1 architecture.
276 assert(num_specs <= 1 && "Linux plugin supports only a single architecture");
277 if (num_specs == 1)
278 {
279 ModuleSpec module_spec;
280 if (specs.GetModuleSpecAtIndex (0, module_spec) && module_spec.GetArchitecture().IsValid())
281 {
282 process_info.GetArchitecture () = module_spec.GetArchitecture();
283 return true;
284 }
285 }
286 return false;
287}
288
289static bool
Daniel Malea25d7eb02013-05-15 17:54:07 +0000290GetProcessAndStatInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info, ProcessStatInfo &stat_info, lldb::pid_t &tracerpid)
291{
292 tracerpid = 0;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000293 process_info.Clear();
Daniel Malea25d7eb02013-05-15 17:54:07 +0000294 ::memset (&stat_info, 0, sizeof(stat_info));
295 stat_info.ppid = LLDB_INVALID_PROCESS_ID;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000296
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000297 // Use special code here because proc/[pid]/exe is a symbolic link.
298 char link_path[PATH_MAX];
299 char exe_path[PATH_MAX] = "";
Daniel Malea25d7eb02013-05-15 17:54:07 +0000300 if (snprintf (link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", pid) <= 0)
301 return false;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000302
Daniel Malea25d7eb02013-05-15 17:54:07 +0000303 ssize_t len = readlink (link_path, exe_path, sizeof(exe_path) - 1);
304 if (len <= 0)
305 return false;
306
307 // readlink does not append a null byte.
308 exe_path[len] = 0;
309
310 // If the binary has been deleted, the link name has " (deleted)" appended.
311 // Remove if there.
312 static const ssize_t deleted_len = strlen(" (deleted)");
313 if (len > deleted_len &&
314 !strcmp(exe_path + len - deleted_len, " (deleted)"))
315 {
316 exe_path[len - deleted_len] = 0;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000317 }
Michael Sartainc836ae72013-05-23 20:57:03 +0000318 else
319 {
320 GetELFProcessCPUType (exe_path, process_info);
321 }
Daniel Malea25d7eb02013-05-15 17:54:07 +0000322
323 process_info.SetProcessID(pid);
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000324 process_info.GetExecutableFile().SetFile(exe_path, false);
325
326 lldb::DataBufferSP buf_sp;
327
328 // Get the process environment.
Todd Fiala3dc2fb22014-06-30 04:14:13 +0000329 buf_sp = ProcFileReader::ReadIntoDataBuffer(pid, "environ");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000330 Args &info_env = process_info.GetEnvironmentEntries();
331 char *next_var = (char *)buf_sp->GetBytes();
332 char *end_buf = next_var + buf_sp->GetByteSize();
333 while (next_var < end_buf && 0 != *next_var)
334 {
335 info_env.AppendArgument(next_var);
336 next_var += strlen(next_var) + 1;
337 }
338
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000339 // Get the command line used to start the process.
Todd Fiala3dc2fb22014-06-30 04:14:13 +0000340 buf_sp = ProcFileReader::ReadIntoDataBuffer(pid, "cmdline");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000341
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000342 // Grab Arg0 first, if there is one.
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000343 char *cmd = (char *)buf_sp->GetBytes();
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000344 if (cmd)
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000345 {
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000346 process_info.SetArg0(cmd);
347
348 // Now process any remaining arguments.
349 Args &info_args = process_info.GetArguments();
350 char *next_arg = cmd + strlen(cmd) + 1;
351 end_buf = cmd + buf_sp->GetByteSize();
352 while (next_arg < end_buf && 0 != *next_arg)
353 {
354 info_args.AppendArgument(next_arg);
355 next_arg += strlen(next_arg) + 1;
356 }
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000357 }
358
Daniel Malea25d7eb02013-05-15 17:54:07 +0000359 // Read /proc/$PID/stat to get our parent pid.
360 if (ReadProcPseudoFileStat (pid, stat_info))
361 {
362 process_info.SetParentProcessID (stat_info.ppid);
363 }
364
365 // Get User and Group IDs and get tracer pid.
366 GetLinuxProcessUserAndGroup (pid, process_info, tracerpid);
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000367
368 return true;
Matt Kopec62502c62013-05-13 19:33:58 +0000369}
370
Daniel Malea25d7eb02013-05-15 17:54:07 +0000371bool
372Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
373{
374 lldb::pid_t tracerpid;
375 ProcessStatInfo stat_info;
376
377 return GetProcessAndStatInfo (pid, process_info, stat_info, tracerpid);
378}
379
Matt Kopec62502c62013-05-13 19:33:58 +0000380void
Matt Kopec62502c62013-05-13 19:33:58 +0000381Host::Backtrace (Stream &strm, uint32_t max_frames)
382{
Todd Fialacacde7d2014-09-27 16:54:22 +0000383#ifndef __ANDROID__
Michael Sartain3cf443d2013-07-17 00:26:30 +0000384 if (max_frames > 0)
385 {
386 std::vector<void *> frame_buffer (max_frames, NULL);
387 int num_frames = ::backtrace (&frame_buffer[0], frame_buffer.size());
388 char** strs = ::backtrace_symbols (&frame_buffer[0], num_frames);
389 if (strs)
390 {
391 // Start at 1 to skip the "Host::Backtrace" frame
392 for (int i = 1; i < num_frames; ++i)
393 strm.Printf("%s\n", strs[i]);
394 ::free (strs);
395 }
396 }
Todd Fialacacde7d2014-09-27 16:54:22 +0000397#else
398 assert(false && "::backtrace() not supported on Android");
399#endif
Matt Kopec62502c62013-05-13 19:33:58 +0000400}
401
402size_t
403Host::GetEnvironment (StringList &env)
404{
Michael Sartain3cf443d2013-07-17 00:26:30 +0000405 char **host_env = environ;
406 char *env_entry;
407 size_t i;
408 for (i=0; (env_entry = host_env[i]) != NULL; ++i)
409 env.AppendString(env_entry);
410 return i;
Matt Kopec62502c62013-05-13 19:33:58 +0000411}
Todd Fiala4ceced32014-08-29 17:35:57 +0000412
413const lldb_private::UnixSignalsSP&
414Host::GetUnixSignals ()
415{
416 static const lldb_private::UnixSignalsSP s_unix_signals_sp (new process_linux::LinuxSignals ());
417 return s_unix_signals_sp;
418}
419