blob: 175624dd3e503861a276bc84b3ac90515e220254 [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"
Johnny Chen30213ff2012-01-05 19:17:38 +000029#include "lldb/Core/DataBufferHeap.h"
30#include "lldb/Core/DataExtractor.h"
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000031
Michael Sartainc836ae72013-05-23 20:57:03 +000032#include "lldb/Core/ModuleSpec.h"
33#include "lldb/Symbol/ObjectFile.h"
Todd Fiala3dc2fb22014-06-30 04:14:13 +000034#include "Plugins/Process/Linux/ProcFileReader.h"
Todd Fiala4ceced32014-08-29 17:35:57 +000035#include "Plugins/Process/Utility/LinuxSignals.h"
Michael Sartainc836ae72013-05-23 20:57:03 +000036
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000037using namespace lldb;
38using namespace lldb_private;
39
Daniel Malea25d7eb02013-05-15 17:54:07 +000040typedef enum ProcessStateFlags
41{
42 eProcessStateRunning = (1u << 0), // Running
43 eProcessStateSleeping = (1u << 1), // Sleeping in an interruptible wait
44 eProcessStateWaiting = (1u << 2), // Waiting in an uninterruptible disk sleep
45 eProcessStateZombie = (1u << 3), // Zombie
46 eProcessStateTracedOrStopped = (1u << 4), // Traced or stopped (on a signal)
47 eProcessStatePaging = (1u << 5) // Paging
48} ProcessStateFlags;
49
50typedef struct ProcessStatInfo
51{
52 lldb::pid_t ppid; // Parent Process ID
53 uint32_t fProcessState; // ProcessStateFlags
54} ProcessStatInfo;
55
56// Get the process info with additional information from /proc/$PID/stat (like process state, and tracer pid).
57static bool GetProcessAndStatInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info, ProcessStatInfo &stat_info, lldb::pid_t &tracerpid);
58
Daniel Malea25d7eb02013-05-15 17:54:07 +000059static bool
60ReadProcPseudoFileStat (lldb::pid_t pid, ProcessStatInfo& stat_info)
61{
62 // Read the /proc/$PID/stat file.
Todd Fiala3dc2fb22014-06-30 04:14:13 +000063 lldb::DataBufferSP buf_sp = ProcFileReader::ReadIntoDataBuffer (pid, "stat");
Daniel Malea25d7eb02013-05-15 17:54:07 +000064
65 // The filename of the executable is stored in parenthesis right after the pid. We look for the closing
66 // parenthesis for the filename and work from there in case the name has something funky like ')' in it.
67 const char *filename_end = strrchr ((const char *)buf_sp->GetBytes(), ')');
68 if (filename_end)
69 {
70 char state = '\0';
71 int ppid = LLDB_INVALID_PROCESS_ID;
72
73 // Read state and ppid.
74 sscanf (filename_end + 1, " %c %d", &state, &ppid);
75
76 stat_info.ppid = ppid;
77
78 switch (state)
79 {
80 case 'R':
81 stat_info.fProcessState |= eProcessStateRunning;
82 break;
83 case 'S':
84 stat_info.fProcessState |= eProcessStateSleeping;
85 break;
86 case 'D':
87 stat_info.fProcessState |= eProcessStateWaiting;
88 break;
89 case 'Z':
90 stat_info.fProcessState |= eProcessStateZombie;
91 break;
92 case 'T':
93 stat_info.fProcessState |= eProcessStateTracedOrStopped;
94 break;
95 case 'W':
96 stat_info.fProcessState |= eProcessStatePaging;
97 break;
98 }
99
100 return true;
101 }
102
103 return false;
104}
105
106static void
107GetLinuxProcessUserAndGroup (lldb::pid_t pid, ProcessInstanceInfo &process_info, lldb::pid_t &tracerpid)
108{
109 tracerpid = 0;
110 uint32_t rUid = UINT32_MAX; // Real User ID
111 uint32_t eUid = UINT32_MAX; // Effective User ID
112 uint32_t rGid = UINT32_MAX; // Real Group ID
113 uint32_t eGid = UINT32_MAX; // Effective Group ID
114
115 // Read the /proc/$PID/status file and parse the Uid:, Gid:, and TracerPid: fields.
Todd Fiala3dc2fb22014-06-30 04:14:13 +0000116 lldb::DataBufferSP buf_sp = ProcFileReader::ReadIntoDataBuffer (pid, "status");
Daniel Malea25d7eb02013-05-15 17:54:07 +0000117
118 static const char uid_token[] = "Uid:";
119 char *buf_uid = strstr ((char *)buf_sp->GetBytes(), uid_token);
120 if (buf_uid)
121 {
122 // Real, effective, saved set, and file system UIDs. Read the first two.
123 buf_uid += sizeof(uid_token);
124 rUid = strtol (buf_uid, &buf_uid, 10);
125 eUid = strtol (buf_uid, &buf_uid, 10);
126 }
127
128 static const char gid_token[] = "Gid:";
129 char *buf_gid = strstr ((char *)buf_sp->GetBytes(), gid_token);
130 if (buf_gid)
131 {
132 // Real, effective, saved set, and file system GIDs. Read the first two.
133 buf_gid += sizeof(gid_token);
134 rGid = strtol (buf_gid, &buf_gid, 10);
135 eGid = strtol (buf_gid, &buf_gid, 10);
136 }
137
138 static const char tracerpid_token[] = "TracerPid:";
139 char *buf_tracerpid = strstr((char *)buf_sp->GetBytes(), tracerpid_token);
140 if (buf_tracerpid)
141 {
142 // Tracer PID. 0 if we're not being debugged.
143 buf_tracerpid += sizeof(tracerpid_token);
144 tracerpid = strtol (buf_tracerpid, &buf_tracerpid, 10);
145 }
146
147 process_info.SetUserID (rUid);
148 process_info.SetEffectiveUserID (eUid);
149 process_info.SetGroupID (rGid);
150 process_info.SetEffectiveGroupID (eGid);
151}
152
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000153lldb::DataBufferSP
154Host::GetAuxvData(lldb_private::Process *process)
155{
Todd Fiala3dc2fb22014-06-30 04:14:13 +0000156 return ProcFileReader::ReadIntoDataBuffer (process->GetID(), "auxv");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000157}
158
Todd Fialaaf245d12014-06-30 21:05:18 +0000159lldb::DataBufferSP
160Host::GetAuxvData (lldb::pid_t pid)
161{
162 return ProcFileReader::ReadIntoDataBuffer (pid, "auxv");
163}
164
Daniel Malea25d7eb02013-05-15 17:54:07 +0000165static bool
166IsDirNumeric(const char *dname)
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000167{
Daniel Malea25d7eb02013-05-15 17:54:07 +0000168 for (; *dname; dname++)
169 {
170 if (!isdigit (*dname))
171 return false;
172 }
173 return true;
174}
175
176uint32_t
177Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
178{
179 static const char procdir[] = "/proc/";
180
181 DIR *dirproc = opendir (procdir);
182 if (dirproc)
183 {
184 struct dirent *direntry = NULL;
185 const uid_t our_uid = getuid();
186 const lldb::pid_t our_pid = getpid();
187 bool all_users = match_info.GetMatchAllUsers();
188
189 while ((direntry = readdir (dirproc)) != NULL)
190 {
191 if (direntry->d_type != DT_DIR || !IsDirNumeric (direntry->d_name))
192 continue;
193
194 lldb::pid_t pid = atoi (direntry->d_name);
195
196 // Skip this process.
197 if (pid == our_pid)
198 continue;
199
200 lldb::pid_t tracerpid;
201 ProcessStatInfo stat_info;
202 ProcessInstanceInfo process_info;
203
204 if (!GetProcessAndStatInfo (pid, process_info, stat_info, tracerpid))
205 continue;
206
207 // Skip if process is being debugged.
208 if (tracerpid != 0)
209 continue;
210
211 // Skip zombies.
212 if (stat_info.fProcessState & eProcessStateZombie)
213 continue;
214
215 // Check for user match if we're not matching all users and not running as root.
216 if (!all_users && (our_uid != 0) && (process_info.GetUserID() != our_uid))
217 continue;
218
219 if (match_info.Matches (process_info))
220 {
221 process_infos.Append (process_info);
222 }
223 }
224
225 closedir (dirproc);
226 }
227
228 return process_infos.GetSize();
229}
230
Matt Kopec085d6ce2013-05-31 22:00:07 +0000231bool
232Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach)
233{
234 bool tids_changed = false;
235 static const char procdir[] = "/proc/";
236 static const char taskdir[] = "/task/";
237 std::string process_task_dir = procdir + std::to_string(pid) + taskdir;
238 DIR *dirproc = opendir (process_task_dir.c_str());
239
240 if (dirproc)
241 {
242 struct dirent *direntry = NULL;
243 while ((direntry = readdir (dirproc)) != NULL)
244 {
245 if (direntry->d_type != DT_DIR || !IsDirNumeric (direntry->d_name))
246 continue;
247
248 lldb::tid_t tid = atoi(direntry->d_name);
249 TidMap::iterator it = tids_to_attach.find(tid);
250 if (it == tids_to_attach.end())
251 {
252 tids_to_attach.insert(TidPair(tid, false));
253 tids_changed = true;
254 }
255 }
256 closedir (dirproc);
257 }
258
259 return tids_changed;
260}
261
Daniel Malea25d7eb02013-05-15 17:54:07 +0000262static bool
Michael Sartainc836ae72013-05-23 20:57:03 +0000263GetELFProcessCPUType (const char *exe_path, ProcessInstanceInfo &process_info)
264{
265 // Clear the architecture.
266 process_info.GetArchitecture().Clear();
267
268 ModuleSpecList specs;
269 FileSpec filespec (exe_path, false);
Jason Molendad6cfc162013-07-15 03:25:21 +0000270 const size_t num_specs = ObjectFile::GetModuleSpecifications (filespec, 0, 0, specs);
Michael Sartainc836ae72013-05-23 20:57:03 +0000271 // GetModuleSpecifications() could fail if the executable has been deleted or is locked.
272 // But it shouldn't return more than 1 architecture.
273 assert(num_specs <= 1 && "Linux plugin supports only a single architecture");
274 if (num_specs == 1)
275 {
276 ModuleSpec module_spec;
277 if (specs.GetModuleSpecAtIndex (0, module_spec) && module_spec.GetArchitecture().IsValid())
278 {
279 process_info.GetArchitecture () = module_spec.GetArchitecture();
280 return true;
281 }
282 }
283 return false;
284}
285
286static bool
Daniel Malea25d7eb02013-05-15 17:54:07 +0000287GetProcessAndStatInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info, ProcessStatInfo &stat_info, lldb::pid_t &tracerpid)
288{
289 tracerpid = 0;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000290 process_info.Clear();
Daniel Malea25d7eb02013-05-15 17:54:07 +0000291 ::memset (&stat_info, 0, sizeof(stat_info));
292 stat_info.ppid = LLDB_INVALID_PROCESS_ID;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000293
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000294 // Use special code here because proc/[pid]/exe is a symbolic link.
295 char link_path[PATH_MAX];
296 char exe_path[PATH_MAX] = "";
Daniel Malea25d7eb02013-05-15 17:54:07 +0000297 if (snprintf (link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", pid) <= 0)
298 return false;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000299
Daniel Malea25d7eb02013-05-15 17:54:07 +0000300 ssize_t len = readlink (link_path, exe_path, sizeof(exe_path) - 1);
301 if (len <= 0)
302 return false;
303
304 // readlink does not append a null byte.
305 exe_path[len] = 0;
306
307 // If the binary has been deleted, the link name has " (deleted)" appended.
308 // Remove if there.
309 static const ssize_t deleted_len = strlen(" (deleted)");
310 if (len > deleted_len &&
311 !strcmp(exe_path + len - deleted_len, " (deleted)"))
312 {
313 exe_path[len - deleted_len] = 0;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000314 }
Michael Sartainc836ae72013-05-23 20:57:03 +0000315 else
316 {
317 GetELFProcessCPUType (exe_path, process_info);
318 }
Daniel Malea25d7eb02013-05-15 17:54:07 +0000319
320 process_info.SetProcessID(pid);
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000321 process_info.GetExecutableFile().SetFile(exe_path, false);
322
323 lldb::DataBufferSP buf_sp;
324
325 // Get the process environment.
Todd Fiala3dc2fb22014-06-30 04:14:13 +0000326 buf_sp = ProcFileReader::ReadIntoDataBuffer(pid, "environ");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000327 Args &info_env = process_info.GetEnvironmentEntries();
328 char *next_var = (char *)buf_sp->GetBytes();
329 char *end_buf = next_var + buf_sp->GetByteSize();
330 while (next_var < end_buf && 0 != *next_var)
331 {
332 info_env.AppendArgument(next_var);
333 next_var += strlen(next_var) + 1;
334 }
335
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000336 // Get the command line used to start the process.
Todd Fiala3dc2fb22014-06-30 04:14:13 +0000337 buf_sp = ProcFileReader::ReadIntoDataBuffer(pid, "cmdline");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000338
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000339 // Grab Arg0 first, if there is one.
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000340 char *cmd = (char *)buf_sp->GetBytes();
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000341 if (cmd)
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000342 {
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000343 process_info.SetArg0(cmd);
344
345 // Now process any remaining arguments.
346 Args &info_args = process_info.GetArguments();
347 char *next_arg = cmd + strlen(cmd) + 1;
348 end_buf = cmd + buf_sp->GetByteSize();
349 while (next_arg < end_buf && 0 != *next_arg)
350 {
351 info_args.AppendArgument(next_arg);
352 next_arg += strlen(next_arg) + 1;
353 }
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000354 }
355
Daniel Malea25d7eb02013-05-15 17:54:07 +0000356 // Read /proc/$PID/stat to get our parent pid.
357 if (ReadProcPseudoFileStat (pid, stat_info))
358 {
359 process_info.SetParentProcessID (stat_info.ppid);
360 }
361
362 // Get User and Group IDs and get tracer pid.
363 GetLinuxProcessUserAndGroup (pid, process_info, tracerpid);
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000364
365 return true;
Matt Kopec62502c62013-05-13 19:33:58 +0000366}
367
Daniel Malea25d7eb02013-05-15 17:54:07 +0000368bool
369Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
370{
371 lldb::pid_t tracerpid;
372 ProcessStatInfo stat_info;
373
374 return GetProcessAndStatInfo (pid, process_info, stat_info, tracerpid);
375}
376
Matt Kopec62502c62013-05-13 19:33:58 +0000377void
Matt Kopec62502c62013-05-13 19:33:58 +0000378Host::Backtrace (Stream &strm, uint32_t max_frames)
379{
Todd Fialacacde7d2014-09-27 16:54:22 +0000380#ifndef __ANDROID__
Michael Sartain3cf443d2013-07-17 00:26:30 +0000381 if (max_frames > 0)
382 {
383 std::vector<void *> frame_buffer (max_frames, NULL);
384 int num_frames = ::backtrace (&frame_buffer[0], frame_buffer.size());
385 char** strs = ::backtrace_symbols (&frame_buffer[0], num_frames);
386 if (strs)
387 {
388 // Start at 1 to skip the "Host::Backtrace" frame
389 for (int i = 1; i < num_frames; ++i)
390 strm.Printf("%s\n", strs[i]);
391 ::free (strs);
392 }
393 }
Todd Fialacacde7d2014-09-27 16:54:22 +0000394#else
395 assert(false && "::backtrace() not supported on Android");
396#endif
Matt Kopec62502c62013-05-13 19:33:58 +0000397}
398
399size_t
400Host::GetEnvironment (StringList &env)
401{
Michael Sartain3cf443d2013-07-17 00:26:30 +0000402 char **host_env = environ;
403 char *env_entry;
404 size_t i;
405 for (i=0; (env_entry = host_env[i]) != NULL; ++i)
406 env.AppendString(env_entry);
407 return i;
Matt Kopec62502c62013-05-13 19:33:58 +0000408}
Todd Fiala4ceced32014-08-29 17:35:57 +0000409
410const lldb_private::UnixSignalsSP&
411Host::GetUnixSignals ()
412{
413 static const lldb_private::UnixSignalsSP s_unix_signals_sp (new process_linux::LinuxSignals ());
414 return s_unix_signals_sp;
415}
416