blob: 62bf5362de01c2a1f003671b7b155d147c4f3596 [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
22// Project includes
Johnny Chenc18a5382011-05-19 23:07:19 +000023#include "lldb/Core/Error.h"
Todd Fialaf3d61de2014-01-17 20:18:59 +000024#include "lldb/Core/Log.h"
Johnny Chen30213ff2012-01-05 19:17:38 +000025#include "lldb/Target/Process.h"
26
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000027#include "lldb/Host/Host.h"
Tamas Berghammera1094212015-03-13 11:16:12 +000028#include "lldb/Host/HostInfo.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"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000038
Stephen Wilson3e2a18f2011-03-23 01:58:26 +000039using namespace lldb;
40using namespace lldb_private;
41
Daniel Malea25d7eb02013-05-15 17:54:07 +000042typedef enum ProcessStateFlags
43{
44 eProcessStateRunning = (1u << 0), // Running
45 eProcessStateSleeping = (1u << 1), // Sleeping in an interruptible wait
46 eProcessStateWaiting = (1u << 2), // Waiting in an uninterruptible disk sleep
47 eProcessStateZombie = (1u << 3), // Zombie
48 eProcessStateTracedOrStopped = (1u << 4), // Traced or stopped (on a signal)
49 eProcessStatePaging = (1u << 5) // Paging
50} ProcessStateFlags;
51
52typedef struct ProcessStatInfo
53{
54 lldb::pid_t ppid; // Parent Process ID
55 uint32_t fProcessState; // ProcessStateFlags
56} ProcessStatInfo;
57
58// Get the process info with additional information from /proc/$PID/stat (like process state, and tracer pid).
59static bool GetProcessAndStatInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info, ProcessStatInfo &stat_info, lldb::pid_t &tracerpid);
60
Daniel Malea25d7eb02013-05-15 17:54:07 +000061static bool
62ReadProcPseudoFileStat (lldb::pid_t pid, ProcessStatInfo& stat_info)
63{
64 // Read the /proc/$PID/stat file.
Tamas Berghammerdb264a62015-03-31 09:52:22 +000065 lldb::DataBufferSP buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer (pid, "stat");
Daniel Malea25d7eb02013-05-15 17:54:07 +000066
67 // The filename of the executable is stored in parenthesis right after the pid. We look for the closing
68 // parenthesis for the filename and work from there in case the name has something funky like ')' in it.
69 const char *filename_end = strrchr ((const char *)buf_sp->GetBytes(), ')');
70 if (filename_end)
71 {
72 char state = '\0';
73 int ppid = LLDB_INVALID_PROCESS_ID;
74
75 // Read state and ppid.
76 sscanf (filename_end + 1, " %c %d", &state, &ppid);
77
78 stat_info.ppid = ppid;
79
80 switch (state)
81 {
82 case 'R':
83 stat_info.fProcessState |= eProcessStateRunning;
84 break;
85 case 'S':
86 stat_info.fProcessState |= eProcessStateSleeping;
87 break;
88 case 'D':
89 stat_info.fProcessState |= eProcessStateWaiting;
90 break;
91 case 'Z':
92 stat_info.fProcessState |= eProcessStateZombie;
93 break;
94 case 'T':
95 stat_info.fProcessState |= eProcessStateTracedOrStopped;
96 break;
97 case 'W':
98 stat_info.fProcessState |= eProcessStatePaging;
99 break;
100 }
101
102 return true;
103 }
104
105 return false;
106}
107
108static void
109GetLinuxProcessUserAndGroup (lldb::pid_t pid, ProcessInstanceInfo &process_info, lldb::pid_t &tracerpid)
110{
111 tracerpid = 0;
112 uint32_t rUid = UINT32_MAX; // Real User ID
113 uint32_t eUid = UINT32_MAX; // Effective User ID
114 uint32_t rGid = UINT32_MAX; // Real Group ID
115 uint32_t eGid = UINT32_MAX; // Effective Group ID
116
117 // Read the /proc/$PID/status file and parse the Uid:, Gid:, and TracerPid: fields.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000118 lldb::DataBufferSP buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer (pid, "status");
Daniel Malea25d7eb02013-05-15 17:54:07 +0000119
120 static const char uid_token[] = "Uid:";
121 char *buf_uid = strstr ((char *)buf_sp->GetBytes(), uid_token);
122 if (buf_uid)
123 {
124 // Real, effective, saved set, and file system UIDs. Read the first two.
125 buf_uid += sizeof(uid_token);
126 rUid = strtol (buf_uid, &buf_uid, 10);
127 eUid = strtol (buf_uid, &buf_uid, 10);
128 }
129
130 static const char gid_token[] = "Gid:";
131 char *buf_gid = strstr ((char *)buf_sp->GetBytes(), gid_token);
132 if (buf_gid)
133 {
134 // Real, effective, saved set, and file system GIDs. Read the first two.
135 buf_gid += sizeof(gid_token);
136 rGid = strtol (buf_gid, &buf_gid, 10);
137 eGid = strtol (buf_gid, &buf_gid, 10);
138 }
139
140 static const char tracerpid_token[] = "TracerPid:";
141 char *buf_tracerpid = strstr((char *)buf_sp->GetBytes(), tracerpid_token);
142 if (buf_tracerpid)
143 {
144 // Tracer PID. 0 if we're not being debugged.
145 buf_tracerpid += sizeof(tracerpid_token);
146 tracerpid = strtol (buf_tracerpid, &buf_tracerpid, 10);
147 }
148
149 process_info.SetUserID (rUid);
150 process_info.SetEffectiveUserID (eUid);
151 process_info.SetGroupID (rGid);
152 process_info.SetEffectiveGroupID (eGid);
153}
154
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000155lldb::DataBufferSP
156Host::GetAuxvData(lldb_private::Process *process)
157{
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000158 return process_linux::ProcFileReader::ReadIntoDataBuffer (process->GetID(), "auxv");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000159}
160
Todd Fialaaf245d12014-06-30 21:05:18 +0000161lldb::DataBufferSP
162Host::GetAuxvData (lldb::pid_t pid)
163{
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000164 return process_linux::ProcFileReader::ReadIntoDataBuffer (pid, "auxv");
Todd Fialaaf245d12014-06-30 21:05:18 +0000165}
166
Daniel Malea25d7eb02013-05-15 17:54:07 +0000167static bool
168IsDirNumeric(const char *dname)
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000169{
Daniel Malea25d7eb02013-05-15 17:54:07 +0000170 for (; *dname; dname++)
171 {
172 if (!isdigit (*dname))
173 return false;
174 }
175 return true;
176}
177
178uint32_t
179Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
180{
181 static const char procdir[] = "/proc/";
182
183 DIR *dirproc = opendir (procdir);
184 if (dirproc)
185 {
186 struct dirent *direntry = NULL;
187 const uid_t our_uid = getuid();
188 const lldb::pid_t our_pid = getpid();
189 bool all_users = match_info.GetMatchAllUsers();
190
191 while ((direntry = readdir (dirproc)) != NULL)
192 {
193 if (direntry->d_type != DT_DIR || !IsDirNumeric (direntry->d_name))
194 continue;
195
196 lldb::pid_t pid = atoi (direntry->d_name);
197
198 // Skip this process.
199 if (pid == our_pid)
200 continue;
201
202 lldb::pid_t tracerpid;
203 ProcessStatInfo stat_info;
204 ProcessInstanceInfo process_info;
205
206 if (!GetProcessAndStatInfo (pid, process_info, stat_info, tracerpid))
207 continue;
208
209 // Skip if process is being debugged.
210 if (tracerpid != 0)
211 continue;
212
213 // Skip zombies.
214 if (stat_info.fProcessState & eProcessStateZombie)
215 continue;
216
217 // Check for user match if we're not matching all users and not running as root.
218 if (!all_users && (our_uid != 0) && (process_info.GetUserID() != our_uid))
219 continue;
220
221 if (match_info.Matches (process_info))
222 {
223 process_infos.Append (process_info);
224 }
225 }
226
227 closedir (dirproc);
228 }
229
230 return process_infos.GetSize();
231}
232
Matt Kopec085d6ce2013-05-31 22:00:07 +0000233bool
234Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach)
235{
236 bool tids_changed = false;
237 static const char procdir[] = "/proc/";
238 static const char taskdir[] = "/task/";
239 std::string process_task_dir = procdir + std::to_string(pid) + taskdir;
240 DIR *dirproc = opendir (process_task_dir.c_str());
241
242 if (dirproc)
243 {
244 struct dirent *direntry = NULL;
245 while ((direntry = readdir (dirproc)) != NULL)
246 {
247 if (direntry->d_type != DT_DIR || !IsDirNumeric (direntry->d_name))
248 continue;
249
250 lldb::tid_t tid = atoi(direntry->d_name);
251 TidMap::iterator it = tids_to_attach.find(tid);
252 if (it == tids_to_attach.end())
253 {
254 tids_to_attach.insert(TidPair(tid, false));
255 tids_changed = true;
256 }
257 }
258 closedir (dirproc);
259 }
260
261 return tids_changed;
262}
263
Daniel Malea25d7eb02013-05-15 17:54:07 +0000264static bool
Michael Sartainc836ae72013-05-23 20:57:03 +0000265GetELFProcessCPUType (const char *exe_path, ProcessInstanceInfo &process_info)
266{
267 // Clear the architecture.
268 process_info.GetArchitecture().Clear();
269
270 ModuleSpecList specs;
271 FileSpec filespec (exe_path, false);
Jason Molendad6cfc162013-07-15 03:25:21 +0000272 const size_t num_specs = ObjectFile::GetModuleSpecifications (filespec, 0, 0, specs);
Michael Sartainc836ae72013-05-23 20:57:03 +0000273 // GetModuleSpecifications() could fail if the executable has been deleted or is locked.
274 // But it shouldn't return more than 1 architecture.
275 assert(num_specs <= 1 && "Linux plugin supports only a single architecture");
276 if (num_specs == 1)
277 {
278 ModuleSpec module_spec;
279 if (specs.GetModuleSpecAtIndex (0, module_spec) && module_spec.GetArchitecture().IsValid())
280 {
281 process_info.GetArchitecture () = module_spec.GetArchitecture();
282 return true;
283 }
284 }
285 return false;
286}
287
288static bool
Daniel Malea25d7eb02013-05-15 17:54:07 +0000289GetProcessAndStatInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info, ProcessStatInfo &stat_info, lldb::pid_t &tracerpid)
290{
291 tracerpid = 0;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000292 process_info.Clear();
Daniel Malea25d7eb02013-05-15 17:54:07 +0000293 ::memset (&stat_info, 0, sizeof(stat_info));
294 stat_info.ppid = LLDB_INVALID_PROCESS_ID;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000295
Oleksiy Vyalov9d901862016-07-12 18:14:27 +0000296 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
297
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000298 // Use special code here because proc/[pid]/exe is a symbolic link.
299 char link_path[PATH_MAX];
300 char exe_path[PATH_MAX] = "";
Daniel Malea25d7eb02013-05-15 17:54:07 +0000301 if (snprintf (link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", pid) <= 0)
Oleksiy Vyalov9d901862016-07-12 18:14:27 +0000302 {
303 if (log)
304 log->Printf("%s: failed to sprintf pid %" PRIu64, __FUNCTION__, pid);
Daniel Malea25d7eb02013-05-15 17:54:07 +0000305 return false;
Oleksiy Vyalov9d901862016-07-12 18:14:27 +0000306 }
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000307
Daniel Malea25d7eb02013-05-15 17:54:07 +0000308 ssize_t len = readlink (link_path, exe_path, sizeof(exe_path) - 1);
309 if (len <= 0)
Oleksiy Vyalov9d901862016-07-12 18:14:27 +0000310 {
311 if (log)
312 log->Printf("%s: failed to read link %s: %s", __FUNCTION__, link_path, strerror(errno));
Daniel Malea25d7eb02013-05-15 17:54:07 +0000313 return false;
Oleksiy Vyalov9d901862016-07-12 18:14:27 +0000314 }
Daniel Malea25d7eb02013-05-15 17:54:07 +0000315
316 // readlink does not append a null byte.
317 exe_path[len] = 0;
318
319 // If the binary has been deleted, the link name has " (deleted)" appended.
320 // Remove if there.
321 static const ssize_t deleted_len = strlen(" (deleted)");
322 if (len > deleted_len &&
323 !strcmp(exe_path + len - deleted_len, " (deleted)"))
324 {
325 exe_path[len - deleted_len] = 0;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000326 }
Michael Sartainc836ae72013-05-23 20:57:03 +0000327 else
328 {
329 GetELFProcessCPUType (exe_path, process_info);
330 }
Daniel Malea25d7eb02013-05-15 17:54:07 +0000331
332 process_info.SetProcessID(pid);
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000333 process_info.GetExecutableFile().SetFile(exe_path, false);
Tamas Berghammera1094212015-03-13 11:16:12 +0000334 process_info.GetArchitecture().MergeFrom(HostInfo::GetArchitecture());
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000335
336 lldb::DataBufferSP buf_sp;
337
338 // Get the process environment.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000339 buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer(pid, "environ");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000340 Args &info_env = process_info.GetEnvironmentEntries();
341 char *next_var = (char *)buf_sp->GetBytes();
342 char *end_buf = next_var + buf_sp->GetByteSize();
343 while (next_var < end_buf && 0 != *next_var)
344 {
345 info_env.AppendArgument(next_var);
346 next_var += strlen(next_var) + 1;
347 }
348
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000349 // Get the command line used to start the process.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000350 buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer(pid, "cmdline");
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000351
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000352 // Grab Arg0 first, if there is one.
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000353 char *cmd = (char *)buf_sp->GetBytes();
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000354 if (cmd)
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000355 {
Matt Kopecdc7c73c2013-10-09 19:23:34 +0000356 process_info.SetArg0(cmd);
357
358 // Now process any remaining arguments.
359 Args &info_args = process_info.GetArguments();
360 char *next_arg = cmd + strlen(cmd) + 1;
361 end_buf = cmd + buf_sp->GetByteSize();
362 while (next_arg < end_buf && 0 != *next_arg)
363 {
364 info_args.AppendArgument(next_arg);
365 next_arg += strlen(next_arg) + 1;
366 }
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000367 }
368
Daniel Malea25d7eb02013-05-15 17:54:07 +0000369 // Read /proc/$PID/stat to get our parent pid.
370 if (ReadProcPseudoFileStat (pid, stat_info))
371 {
372 process_info.SetParentProcessID (stat_info.ppid);
373 }
374
375 // Get User and Group IDs and get tracer pid.
376 GetLinuxProcessUserAndGroup (pid, process_info, tracerpid);
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000377
378 return true;
Matt Kopec62502c62013-05-13 19:33:58 +0000379}
380
Daniel Malea25d7eb02013-05-15 17:54:07 +0000381bool
382Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
383{
384 lldb::pid_t tracerpid;
385 ProcessStatInfo stat_info;
386
387 return GetProcessAndStatInfo (pid, process_info, stat_info, tracerpid);
388}
389
Matt Kopec62502c62013-05-13 19:33:58 +0000390size_t
391Host::GetEnvironment (StringList &env)
392{
Michael Sartain3cf443d2013-07-17 00:26:30 +0000393 char **host_env = environ;
394 char *env_entry;
395 size_t i;
396 for (i=0; (env_entry = host_env[i]) != NULL; ++i)
397 env.AppendString(env_entry);
398 return i;
Matt Kopec62502c62013-05-13 19:33:58 +0000399}
Todd Fiala4ceced32014-08-29 17:35:57 +0000400
Enrico Granata83a14372015-02-20 21:48:38 +0000401Error
Enrico Granatab38ef8c2015-02-20 22:20:30 +0000402Host::ShellExpandArguments (ProcessLaunchInfo &launch_info)
Enrico Granata83a14372015-02-20 21:48:38 +0000403{
404 return Error("unimplemented");
405}