blob: 53d74d552347cad162d94dfdc6f9444e9031191d [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
Daniel Malea25d7eb02013-05-15 17:54:07 +000011#include <dirent.h>
Oleksiy Vyalov9d901862016-07-12 18:14:27 +000012#include <errno.h>
Johnny Chen30213ff2012-01-05 19:17:38 +000013#include <fcntl.h>
Oleksiy Vyalov9d901862016-07-12 18:14:27 +000014#include <stdio.h>
15#include <string.h>
16#include <sys/stat.h>
17#include <sys/types.h>
18#include <sys/utsname.h>
Johnny Chen30213ff2012-01-05 19:17:38 +000019
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000020// C++ Includes
21// Other libraries and framework includes
Pavel Labath1eb0d422016-08-08 12:54:36 +000022#include "llvm/Support/ScopedPrinter.h"
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000023// 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"
Tamas Berghammera1094212015-03-13 11:16:12 +000029#include "lldb/Host/HostInfo.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000030#include "lldb/Core/DataBufferHeap.h"
31#include "lldb/Core/DataExtractor.h"
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000032
Michael Sartainc836ae72013-05-23 20:57:03 +000033#include "lldb/Core/ModuleSpec.h"
34#include "lldb/Symbol/ObjectFile.h"
Todd Fiala3dc2fb22014-06-30 04:14:13 +000035#include "Plugins/Process/Linux/ProcFileReader.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +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.
Tamas Berghammerdb264a62015-03-31 09:52:22 +000063 lldb::DataBufferSP buf_sp = process_linux::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.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000116 lldb::DataBufferSP buf_sp = process_linux::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{
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000156 return process_linux::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{
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000162 return process_linux::ProcFileReader::ReadIntoDataBuffer (pid, "auxv");
Todd Fialaaf245d12014-06-30 21:05:18 +0000163}
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/";
Pavel Labath1eb0d422016-08-08 12:54:36 +0000237 std::string process_task_dir = procdir + llvm::to_string(pid) + taskdir;
Matt Kopec085d6ce2013-05-31 22:00:07 +0000238 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
Oleksiy Vyalov9d901862016-07-12 18:14:27 +0000294 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
295
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000296 // Use special code here because proc/[pid]/exe is a symbolic link.
297 char link_path[PATH_MAX];
298 char exe_path[PATH_MAX] = "";
Daniel Malea25d7eb02013-05-15 17:54:07 +0000299 if (snprintf (link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", pid) <= 0)
Oleksiy Vyalov9d901862016-07-12 18:14:27 +0000300 {
301 if (log)
302 log->Printf("%s: failed to sprintf pid %" PRIu64, __FUNCTION__, pid);
Daniel Malea25d7eb02013-05-15 17:54:07 +0000303 return false;
Oleksiy Vyalov9d901862016-07-12 18:14:27 +0000304 }
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000305
Daniel Malea25d7eb02013-05-15 17:54:07 +0000306 ssize_t len = readlink (link_path, exe_path, sizeof(exe_path) - 1);
307 if (len <= 0)
Oleksiy Vyalov9d901862016-07-12 18:14:27 +0000308 {
309 if (log)
310 log->Printf("%s: failed to read link %s: %s", __FUNCTION__, link_path, strerror(errno));
Daniel Malea25d7eb02013-05-15 17:54:07 +0000311 return false;
Oleksiy Vyalov9d901862016-07-12 18:14:27 +0000312 }
Daniel Malea25d7eb02013-05-15 17:54:07 +0000313
314 // readlink does not append a null byte.
315 exe_path[len] = 0;
316
317 // If the binary has been deleted, the link name has " (deleted)" appended.
318 // Remove if there.
319 static const ssize_t deleted_len = strlen(" (deleted)");
320 if (len > deleted_len &&
321 !strcmp(exe_path + len - deleted_len, " (deleted)"))
322 {
323 exe_path[len - deleted_len] = 0;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000324 }
Michael Sartainc836ae72013-05-23 20:57:03 +0000325 else
326 {
327 GetELFProcessCPUType (exe_path, process_info);
328 }
Daniel Malea25d7eb02013-05-15 17:54:07 +0000329
330 process_info.SetProcessID(pid);
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000331 process_info.GetExecutableFile().SetFile(exe_path, false);
Tamas Berghammera1094212015-03-13 11:16:12 +0000332 process_info.GetArchitecture().MergeFrom(HostInfo::GetArchitecture());
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000333
334 lldb::DataBufferSP buf_sp;
335
336 // Get the process environment.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000337 buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer(pid, "environ");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000338 Args &info_env = process_info.GetEnvironmentEntries();
339 char *next_var = (char *)buf_sp->GetBytes();
340 char *end_buf = next_var + buf_sp->GetByteSize();
341 while (next_var < end_buf && 0 != *next_var)
342 {
343 info_env.AppendArgument(next_var);
344 next_var += strlen(next_var) + 1;
345 }
346
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000347 // Get the command line used to start the process.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000348 buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer(pid, "cmdline");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000349
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000350 // Grab Arg0 first, if there is one.
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000351 char *cmd = (char *)buf_sp->GetBytes();
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000352 if (cmd)
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000353 {
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000354 process_info.SetArg0(cmd);
355
356 // Now process any remaining arguments.
357 Args &info_args = process_info.GetArguments();
358 char *next_arg = cmd + strlen(cmd) + 1;
359 end_buf = cmd + buf_sp->GetByteSize();
360 while (next_arg < end_buf && 0 != *next_arg)
361 {
362 info_args.AppendArgument(next_arg);
363 next_arg += strlen(next_arg) + 1;
364 }
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000365 }
366
Daniel Malea25d7eb02013-05-15 17:54:07 +0000367 // Read /proc/$PID/stat to get our parent pid.
368 if (ReadProcPseudoFileStat (pid, stat_info))
369 {
370 process_info.SetParentProcessID (stat_info.ppid);
371 }
372
373 // Get User and Group IDs and get tracer pid.
374 GetLinuxProcessUserAndGroup (pid, process_info, tracerpid);
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000375
376 return true;
Matt Kopec62502c62013-05-13 19:33:58 +0000377}
378
Daniel Malea25d7eb02013-05-15 17:54:07 +0000379bool
380Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
381{
382 lldb::pid_t tracerpid;
383 ProcessStatInfo stat_info;
384
385 return GetProcessAndStatInfo (pid, process_info, stat_info, tracerpid);
386}
387
Matt Kopec62502c62013-05-13 19:33:58 +0000388size_t
389Host::GetEnvironment (StringList &env)
390{
Michael Sartain3cf443d2013-07-17 00:26:30 +0000391 char **host_env = environ;
392 char *env_entry;
393 size_t i;
394 for (i=0; (env_entry = host_env[i]) != NULL; ++i)
395 env.AppendString(env_entry);
396 return i;
Matt Kopec62502c62013-05-13 19:33:58 +0000397}
Todd Fiala4ceced32014-08-29 17:35:57 +0000398
Enrico Granata83a14372015-02-20 21:48:38 +0000399Error
Enrico Granatab38ef8c2015-02-20 22:20:30 +0000400Host::ShellExpandArguments (ProcessLaunchInfo &launch_info)
Enrico Granata83a14372015-02-20 21:48:38 +0000401{
402 return Error("unimplemented");
403}