2011-03-23 09:58:26 +08:00
|
|
|
//===-- source/Host/linux/Host.cpp ------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// C Includes
|
2013-05-16 01:54:07 +08:00
|
|
|
#include <dirent.h>
|
2016-07-13 02:14:27 +08:00
|
|
|
#include <errno.h>
|
2012-01-06 03:17:38 +08:00
|
|
|
#include <fcntl.h>
|
2016-07-13 02:14:27 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/utsname.h>
|
2012-01-06 03:17:38 +08:00
|
|
|
|
2011-03-23 09:58:26 +08:00
|
|
|
// C++ Includes
|
|
|
|
// Other libraries and framework includes
|
2016-08-08 20:54:36 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2011-03-23 09:58:26 +08:00
|
|
|
// Project includes
|
2014-01-18 04:18:59 +08:00
|
|
|
#include "lldb/Core/Log.h"
|
2012-01-06 03:17:38 +08:00
|
|
|
#include "lldb/Target/Process.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/Error.h"
|
2012-01-06 03:17:38 +08:00
|
|
|
|
|
|
|
#include "lldb/Core/DataBufferHeap.h"
|
|
|
|
#include "lldb/Core/DataExtractor.h"
|
2016-09-07 04:57:50 +08:00
|
|
|
#include "lldb/Host/Host.h"
|
|
|
|
#include "lldb/Host/HostInfo.h"
|
2011-03-23 09:58:26 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
#include "Plugins/Process/Linux/ProcFileReader.h"
|
2013-05-24 04:57:03 +08:00
|
|
|
#include "lldb/Core/ModuleSpec.h"
|
|
|
|
#include "lldb/Symbol/ObjectFile.h"
|
2015-07-14 09:09:28 +08:00
|
|
|
|
2011-03-23 09:58:26 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
typedef enum ProcessStateFlags {
|
|
|
|
eProcessStateRunning = (1u << 0), // Running
|
|
|
|
eProcessStateSleeping = (1u << 1), // Sleeping in an interruptible wait
|
|
|
|
eProcessStateWaiting = (1u << 2), // Waiting in an uninterruptible disk sleep
|
|
|
|
eProcessStateZombie = (1u << 3), // Zombie
|
|
|
|
eProcessStateTracedOrStopped = (1u << 4), // Traced or stopped (on a signal)
|
|
|
|
eProcessStatePaging = (1u << 5) // Paging
|
2013-05-16 01:54:07 +08:00
|
|
|
} ProcessStateFlags;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
typedef struct ProcessStatInfo {
|
|
|
|
lldb::pid_t ppid; // Parent Process ID
|
|
|
|
uint32_t fProcessState; // ProcessStateFlags
|
2013-05-16 01:54:07 +08:00
|
|
|
} ProcessStatInfo;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Get the process info with additional information from /proc/$PID/stat (like
|
|
|
|
// process state, and tracer pid).
|
|
|
|
static bool GetProcessAndStatInfo(lldb::pid_t pid,
|
|
|
|
ProcessInstanceInfo &process_info,
|
|
|
|
ProcessStatInfo &stat_info,
|
|
|
|
lldb::pid_t &tracerpid);
|
|
|
|
|
|
|
|
static bool ReadProcPseudoFileStat(lldb::pid_t pid,
|
|
|
|
ProcessStatInfo &stat_info) {
|
|
|
|
// Read the /proc/$PID/stat file.
|
|
|
|
lldb::DataBufferSP buf_sp =
|
|
|
|
process_linux::ProcFileReader::ReadIntoDataBuffer(pid, "stat");
|
|
|
|
|
|
|
|
// The filename of the executable is stored in parenthesis right after the
|
|
|
|
// pid. We look for the closing
|
|
|
|
// parenthesis for the filename and work from there in case the name has
|
|
|
|
// something funky like ')' in it.
|
|
|
|
const char *filename_end = strrchr((const char *)buf_sp->GetBytes(), ')');
|
|
|
|
if (filename_end) {
|
|
|
|
char state = '\0';
|
|
|
|
int ppid = LLDB_INVALID_PROCESS_ID;
|
|
|
|
|
|
|
|
// Read state and ppid.
|
|
|
|
sscanf(filename_end + 1, " %c %d", &state, &ppid);
|
|
|
|
|
|
|
|
stat_info.ppid = ppid;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case 'R':
|
|
|
|
stat_info.fProcessState |= eProcessStateRunning;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
stat_info.fProcessState |= eProcessStateSleeping;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
stat_info.fProcessState |= eProcessStateWaiting;
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
stat_info.fProcessState |= eProcessStateZombie;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
stat_info.fProcessState |= eProcessStateTracedOrStopped;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
stat_info.fProcessState |= eProcessStatePaging;
|
|
|
|
break;
|
2012-01-06 03:17:38 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return false;
|
2013-05-16 01:54:07 +08:00
|
|
|
}
|
2013-05-08 06:46:38 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
static void GetLinuxProcessUserAndGroup(lldb::pid_t pid,
|
|
|
|
ProcessInstanceInfo &process_info,
|
|
|
|
lldb::pid_t &tracerpid) {
|
|
|
|
tracerpid = 0;
|
|
|
|
uint32_t rUid = UINT32_MAX; // Real User ID
|
|
|
|
uint32_t eUid = UINT32_MAX; // Effective User ID
|
|
|
|
uint32_t rGid = UINT32_MAX; // Real Group ID
|
|
|
|
uint32_t eGid = UINT32_MAX; // Effective Group ID
|
|
|
|
|
|
|
|
// Read the /proc/$PID/status file and parse the Uid:, Gid:, and TracerPid:
|
|
|
|
// fields.
|
|
|
|
lldb::DataBufferSP buf_sp =
|
|
|
|
process_linux::ProcFileReader::ReadIntoDataBuffer(pid, "status");
|
|
|
|
|
|
|
|
static const char uid_token[] = "Uid:";
|
|
|
|
char *buf_uid = strstr((char *)buf_sp->GetBytes(), uid_token);
|
|
|
|
if (buf_uid) {
|
|
|
|
// Real, effective, saved set, and file system UIDs. Read the first two.
|
|
|
|
buf_uid += sizeof(uid_token);
|
|
|
|
rUid = strtol(buf_uid, &buf_uid, 10);
|
|
|
|
eUid = strtol(buf_uid, &buf_uid, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char gid_token[] = "Gid:";
|
|
|
|
char *buf_gid = strstr((char *)buf_sp->GetBytes(), gid_token);
|
|
|
|
if (buf_gid) {
|
|
|
|
// Real, effective, saved set, and file system GIDs. Read the first two.
|
|
|
|
buf_gid += sizeof(gid_token);
|
|
|
|
rGid = strtol(buf_gid, &buf_gid, 10);
|
|
|
|
eGid = strtol(buf_gid, &buf_gid, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char tracerpid_token[] = "TracerPid:";
|
|
|
|
char *buf_tracerpid = strstr((char *)buf_sp->GetBytes(), tracerpid_token);
|
|
|
|
if (buf_tracerpid) {
|
|
|
|
// Tracer PID. 0 if we're not being debugged.
|
|
|
|
buf_tracerpid += sizeof(tracerpid_token);
|
|
|
|
tracerpid = strtol(buf_tracerpid, &buf_tracerpid, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
process_info.SetUserID(rUid);
|
|
|
|
process_info.SetEffectiveUserID(eUid);
|
|
|
|
process_info.SetGroupID(rGid);
|
|
|
|
process_info.SetEffectiveGroupID(eGid);
|
2013-05-08 06:46:38 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::DataBufferSP Host::GetAuxvData(lldb_private::Process *process) {
|
|
|
|
return process_linux::ProcFileReader::ReadIntoDataBuffer(process->GetID(),
|
|
|
|
"auxv");
|
2014-07-01 05:05:18 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::DataBufferSP Host::GetAuxvData(lldb::pid_t pid) {
|
|
|
|
return process_linux::ProcFileReader::ReadIntoDataBuffer(pid, "auxv");
|
2013-05-16 01:54:07 +08:00
|
|
|
}
|
2013-05-08 06:46:38 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
static bool IsDirNumeric(const char *dname) {
|
|
|
|
for (; *dname; dname++) {
|
|
|
|
if (!isdigit(*dname))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
uint32_t Host::FindProcesses(const ProcessInstanceInfoMatch &match_info,
|
|
|
|
ProcessInstanceInfoList &process_infos) {
|
|
|
|
static const char procdir[] = "/proc/";
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
DIR *dirproc = opendir(procdir);
|
|
|
|
if (dirproc) {
|
|
|
|
struct dirent *direntry = NULL;
|
|
|
|
const uid_t our_uid = getuid();
|
|
|
|
const lldb::pid_t our_pid = getpid();
|
|
|
|
bool all_users = match_info.GetMatchAllUsers();
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
while ((direntry = readdir(dirproc)) != NULL) {
|
|
|
|
if (direntry->d_type != DT_DIR || !IsDirNumeric(direntry->d_name))
|
|
|
|
continue;
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::pid_t pid = atoi(direntry->d_name);
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Skip this process.
|
|
|
|
if (pid == our_pid)
|
|
|
|
continue;
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::pid_t tracerpid;
|
|
|
|
ProcessStatInfo stat_info;
|
|
|
|
ProcessInstanceInfo process_info;
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (!GetProcessAndStatInfo(pid, process_info, stat_info, tracerpid))
|
|
|
|
continue;
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Skip if process is being debugged.
|
|
|
|
if (tracerpid != 0)
|
|
|
|
continue;
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Skip zombies.
|
|
|
|
if (stat_info.fProcessState & eProcessStateZombie)
|
|
|
|
continue;
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Check for user match if we're not matching all users and not running as
|
|
|
|
// root.
|
|
|
|
if (!all_users && (our_uid != 0) && (process_info.GetUserID() != our_uid))
|
|
|
|
continue;
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (match_info.Matches(process_info)) {
|
|
|
|
process_infos.Append(process_info);
|
|
|
|
}
|
2013-05-16 01:54:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
closedir(dirproc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return process_infos.GetSize();
|
2013-05-16 01:54:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool Host::FindProcessThreads(const lldb::pid_t pid, TidMap &tids_to_attach) {
|
|
|
|
bool tids_changed = false;
|
|
|
|
static const char procdir[] = "/proc/";
|
|
|
|
static const char taskdir[] = "/task/";
|
|
|
|
std::string process_task_dir = procdir + llvm::to_string(pid) + taskdir;
|
|
|
|
DIR *dirproc = opendir(process_task_dir.c_str());
|
|
|
|
|
|
|
|
if (dirproc) {
|
|
|
|
struct dirent *direntry = NULL;
|
|
|
|
while ((direntry = readdir(dirproc)) != NULL) {
|
|
|
|
if (direntry->d_type != DT_DIR || !IsDirNumeric(direntry->d_name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
lldb::tid_t tid = atoi(direntry->d_name);
|
|
|
|
TidMap::iterator it = tids_to_attach.find(tid);
|
|
|
|
if (it == tids_to_attach.end()) {
|
|
|
|
tids_to_attach.insert(TidPair(tid, false));
|
|
|
|
tids_changed = true;
|
|
|
|
}
|
2013-06-01 06:00:07 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
closedir(dirproc);
|
|
|
|
}
|
2013-06-01 06:00:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return tids_changed;
|
2013-06-01 06:00:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
static bool GetELFProcessCPUType(const char *exe_path,
|
|
|
|
ProcessInstanceInfo &process_info) {
|
|
|
|
// Clear the architecture.
|
|
|
|
process_info.GetArchitecture().Clear();
|
|
|
|
|
|
|
|
ModuleSpecList specs;
|
|
|
|
FileSpec filespec(exe_path, false);
|
|
|
|
const size_t num_specs =
|
|
|
|
ObjectFile::GetModuleSpecifications(filespec, 0, 0, specs);
|
|
|
|
// GetModuleSpecifications() could fail if the executable has been deleted or
|
|
|
|
// is locked.
|
|
|
|
// But it shouldn't return more than 1 architecture.
|
|
|
|
assert(num_specs <= 1 && "Linux plugin supports only a single architecture");
|
|
|
|
if (num_specs == 1) {
|
|
|
|
ModuleSpec module_spec;
|
|
|
|
if (specs.GetModuleSpecAtIndex(0, module_spec) &&
|
|
|
|
module_spec.GetArchitecture().IsValid()) {
|
|
|
|
process_info.GetArchitecture() = module_spec.GetArchitecture();
|
|
|
|
return true;
|
2013-05-24 04:57:03 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
return false;
|
2013-05-24 04:57:03 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
static bool GetProcessAndStatInfo(lldb::pid_t pid,
|
|
|
|
ProcessInstanceInfo &process_info,
|
|
|
|
ProcessStatInfo &stat_info,
|
|
|
|
lldb::pid_t &tracerpid) {
|
|
|
|
tracerpid = 0;
|
|
|
|
process_info.Clear();
|
|
|
|
::memset(&stat_info, 0, sizeof(stat_info));
|
|
|
|
stat_info.ppid = LLDB_INVALID_PROCESS_ID;
|
|
|
|
|
|
|
|
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
|
|
|
|
|
|
|
|
// Use special code here because proc/[pid]/exe is a symbolic link.
|
|
|
|
char link_path[PATH_MAX];
|
|
|
|
char exe_path[PATH_MAX] = "";
|
|
|
|
if (snprintf(link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", pid) <= 0) {
|
|
|
|
if (log)
|
|
|
|
log->Printf("%s: failed to sprintf pid %" PRIu64, __FUNCTION__, pid);
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-08 06:46:38 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
ssize_t len = readlink(link_path, exe_path, sizeof(exe_path) - 1);
|
|
|
|
if (len <= 0) {
|
|
|
|
if (log)
|
|
|
|
log->Printf("%s: failed to read link %s: %s", __FUNCTION__, link_path,
|
|
|
|
strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// readlink does not append a null byte.
|
|
|
|
exe_path[len] = 0;
|
|
|
|
|
|
|
|
// If the binary has been deleted, the link name has " (deleted)" appended.
|
|
|
|
// Remove if there.
|
|
|
|
static const ssize_t deleted_len = strlen(" (deleted)");
|
|
|
|
if (len > deleted_len &&
|
|
|
|
!strcmp(exe_path + len - deleted_len, " (deleted)")) {
|
|
|
|
exe_path[len - deleted_len] = 0;
|
|
|
|
} else {
|
|
|
|
GetELFProcessCPUType(exe_path, process_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
process_info.SetProcessID(pid);
|
|
|
|
process_info.GetExecutableFile().SetFile(exe_path, false);
|
|
|
|
process_info.GetArchitecture().MergeFrom(HostInfo::GetArchitecture());
|
|
|
|
|
|
|
|
lldb::DataBufferSP buf_sp;
|
|
|
|
|
|
|
|
// Get the process environment.
|
|
|
|
buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer(pid, "environ");
|
|
|
|
Args &info_env = process_info.GetEnvironmentEntries();
|
|
|
|
char *next_var = (char *)buf_sp->GetBytes();
|
|
|
|
char *end_buf = next_var + buf_sp->GetByteSize();
|
|
|
|
while (next_var < end_buf && 0 != *next_var) {
|
2016-09-20 01:54:06 +08:00
|
|
|
info_env.AppendArgument(llvm::StringRef(next_var));
|
2016-09-07 04:57:50 +08:00
|
|
|
next_var += strlen(next_var) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the command line used to start the process.
|
|
|
|
buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer(pid, "cmdline");
|
|
|
|
|
|
|
|
// Grab Arg0 first, if there is one.
|
|
|
|
char *cmd = (char *)buf_sp->GetBytes();
|
|
|
|
if (cmd) {
|
|
|
|
process_info.SetArg0(cmd);
|
|
|
|
|
|
|
|
// Now process any remaining arguments.
|
|
|
|
Args &info_args = process_info.GetArguments();
|
|
|
|
char *next_arg = cmd + strlen(cmd) + 1;
|
|
|
|
end_buf = cmd + buf_sp->GetByteSize();
|
|
|
|
while (next_arg < end_buf && 0 != *next_arg) {
|
2016-09-20 01:54:06 +08:00
|
|
|
info_args.AppendArgument(llvm::StringRef(next_arg));
|
2016-09-07 04:57:50 +08:00
|
|
|
next_arg += strlen(next_arg) + 1;
|
2013-05-08 06:46:38 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2013-05-08 06:46:38 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Read /proc/$PID/stat to get our parent pid.
|
|
|
|
if (ReadProcPseudoFileStat(pid, stat_info)) {
|
|
|
|
process_info.SetParentProcessID(stat_info.ppid);
|
|
|
|
}
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Get User and Group IDs and get tracer pid.
|
|
|
|
GetLinuxProcessUserAndGroup(pid, process_info, tracerpid);
|
2013-05-08 06:46:38 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return true;
|
2013-05-14 03:33:58 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {
|
|
|
|
lldb::pid_t tracerpid;
|
|
|
|
ProcessStatInfo stat_info;
|
2013-05-16 01:54:07 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return GetProcessAndStatInfo(pid, process_info, stat_info, tracerpid);
|
2013-05-16 01:54:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
size_t Host::GetEnvironment(StringList &env) {
|
|
|
|
char **host_env = environ;
|
|
|
|
char *env_entry;
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; (env_entry = host_env[i]) != NULL; ++i)
|
|
|
|
env.AppendString(env_entry);
|
|
|
|
return i;
|
2013-05-14 03:33:58 +08:00
|
|
|
}
|
2014-08-30 01:35:57 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Error Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
|
|
|
|
return Error("unimplemented");
|
2015-02-21 05:48:38 +08:00
|
|
|
}
|