[lldb-dev] PATCH for REVIEW: Implement Linux Host::FindProcesses()
Michael Sartain
mikesart at valvesoftware.com
Tue May 14 16:42:04 PDT 2013
This should fix bug #14541: http://llvm.org/bugs/show_bug.cgi?id=14541
I haven't been able to run the python test script mentioned in the bug
report yet, but "platform process list" in lldb looks correct on my Ubuntu
12.04 64-bit machine.
Also should fix a leaked file descriptor, and null pointer dereference when
ReadProcPseudoFile() fails.
Diff is down below and also here:
https://gist.github.com/mikesartain/5580617
Please yell at me with any feedback. Thanks!
-Mike
Index: source/Host/common/Host.cpp
===================================================================
--- source/Host/common/Host.cpp (revision 181847)
+++ source/Host/common/Host.cpp (working copy)
@@ -1206,14 +1206,14 @@
return getegid();
}
-#if !defined (__APPLE__)
+#if !defined (__APPLE__) && !defined(__linux__)
uint32_t
Host::FindProcesses (const ProcessInstanceInfoMatch &match_info,
ProcessInstanceInfoList &process_infos)
{
process_infos.Clear();
return process_infos.GetSize();
}
-#endif
+#endif // #if !defined (__APPLE__) && !defined(__linux__)
#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined(__linux__)
bool
Index: source/Host/linux/Host.cpp
===================================================================
--- source/Host/linux/Host.cpp (revision 181847)
+++ source/Host/linux/Host.cpp (working copy)
@@ -12,9 +12,9 @@
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <dirent.h>
#include <fcntl.h>
-
// C++ Includes
// Other libraries and framework includes
// Project includes
@@ -28,57 +28,174 @@
using namespace lldb;
using namespace lldb_private;
+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
+} ProcessStateFlags;
+
+typedef struct ProcessStatInfo
+{
+ lldb::pid_t ppid; // Parent Process ID
+ uint32_t fProcessState; // ProcessStateFlags
+} ProcessStatInfo;
+
+// 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);
+
namespace
{
lldb::DataBufferSP
-ReadProcPseudoFile(lldb::pid_t pid, const char *name)
+ReadProcPseudoFile (lldb::pid_t pid, const char *name)
{
- static const size_t path_size = 128;
- static char path[path_size];
- lldb::DataBufferSP buf_sp;
-
int fd;
+ char path[PATH_MAX];
+ // Make sure we've got a nil terminated buffer for all the folks
calling
+ // GetBytes() directly off our returned DataBufferSP if we hit an
error.
+ lldb::DataBufferSP buf_sp (new DataBufferHeap(1, 0));
+
// Ideally, we would simply create a FileSpec and call
ReadFileContents.
// However, files in procfs have zero size (since they are, in general,
// dynamically generated by the kernel) which is incompatible with the
- // current ReadFileContents implementation. Therefore we simply
stream the
+ // current ReadFileContents implementation. Therefore we simply stream
the
// data into a DataBuffer ourselves.
- if (snprintf(path, path_size, "/proc/%" PRIu64 "/%s", pid, name) < 0)
- return buf_sp;
+ if (snprintf (path, PATH_MAX, "/proc/%" PRIu64 "/%s", pid, name) > 0)
+ {
+ if ((fd = open (path, O_RDONLY, 0)) >= 0)
+ {
+ size_t bytes_read = 0;
+ std::unique_ptr<DataBufferHeap> buf_ap(new
DataBufferHeap(1024, 0));
- if ((fd = open(path, O_RDONLY, 0)) < 0)
- return buf_sp;
+ for (;;)
+ {
+ size_t avail = buf_ap->GetByteSize() - bytes_read;
+ ssize_t status = read (fd, buf_ap->GetBytes() +
bytes_read, avail);
- size_t bytes_read = 0;
- std::unique_ptr<DataBufferHeap> buf_ap(new DataBufferHeap(1024, 0));
- for (;;)
+ if (status < 0)
+ break;
+
+ if (status == 0)
+ {
+ buf_ap->SetByteSize (bytes_read);
+ buf_sp.reset (buf_ap.release());
+ break;
+ }
+
+ bytes_read += status;
+
+ if (avail - status == 0)
+ buf_ap->SetByteSize (2 * buf_ap->GetByteSize());
+ }
+
+ close (fd);
+ }
+ }
+
+ return buf_sp;
+}
+
+} // anonymous namespace
+
+static bool
+ReadProcPseudoFileStat (lldb::pid_t pid, ProcessStatInfo& stat_info)
+{
+ // Read the /proc/$PID/stat file.
+ lldb::DataBufferSP buf_sp = ReadProcPseudoFile (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)
{
- size_t avail = buf_ap->GetByteSize() - bytes_read;
- ssize_t status = read(fd, buf_ap->GetBytes() + bytes_read, avail);
+ char state = '\0';
+ int ppid = LLDB_INVALID_PROCESS_ID;
- if (status < 0)
- break;
+ // Read state and ppid.
+ sscanf (filename_end + 1, " %c %d", &state, &ppid);
- bytes_read += status;
+ stat_info.ppid = ppid;
- if (status == 0)
+ switch (state)
{
- buf_ap->SetByteSize(bytes_read);
- buf_sp.reset(buf_ap.release());
- break;
+ 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;
}
- if (avail - status == 0)
- buf_ap->SetByteSize(2 * buf_ap->GetByteSize());
+ return true;
}
- return buf_sp;
+ return false;
}
-} // anonymous namespace
+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 = ReadProcPseudoFile (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);
+}
+
bool
Host::GetOSVersion(uint32_t &major,
uint32_t &minor,
@@ -108,12 +225,79 @@
return ReadProcPseudoFile(process->GetID(), "auxv");
}
+static bool
+IsDirNumeric(const char *dname)
+{
+ for (; *dname; dname++)
+ {
+ if (!isdigit (*dname))
+ return false;
+ }
+ return true;
+}
-bool
-Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
+uint32_t
+Host::FindProcesses (const ProcessInstanceInfoMatch &match_info,
ProcessInstanceInfoList &process_infos)
{
+ static const char procdir[] = "/proc/";
+
+ 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();
+
+ while ((direntry = readdir (dirproc)) != NULL)
+ {
+ if (direntry->d_type != DT_DIR || !IsDirNumeric
(direntry->d_name))
+ continue;
+
+ lldb::pid_t pid = atoi (direntry->d_name);
+
+ // Skip this process.
+ if (pid == our_pid)
+ continue;
+
+ lldb::pid_t tracerpid;
+ ProcessStatInfo stat_info;
+ ProcessInstanceInfo process_info;
+
+ if (!GetProcessAndStatInfo (pid, process_info, stat_info,
tracerpid))
+ continue;
+
+ // Skip if process is being debugged.
+ if (tracerpid != 0)
+ continue;
+
+ // Skip zombies.
+ if (stat_info.fProcessState & eProcessStateZombie)
+ continue;
+
+ // 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;
+
+ if (match_info.Matches (process_info))
+ {
+ process_infos.Append (process_info);
+ }
+ }
+
+ closedir (dirproc);
+ }
+
+ return process_infos.GetSize();
+}
+
+static bool
+GetProcessAndStatInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info,
ProcessStatInfo &stat_info, lldb::pid_t &tracerpid)
+{
+ tracerpid = 0;
process_info.Clear();
- process_info.SetProcessID(pid);
+ ::memset (&stat_info, 0, sizeof(stat_info));
+ stat_info.ppid = LLDB_INVALID_PROCESS_ID;
// Architecture is intentionally omitted because that's better resolved
// in other places (see ProcessPOSIX::DoAttachWithID().
@@ -121,19 +305,26 @@
// 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 (snprintf (link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", pid) <= 0)
+ return false;
+
+ ssize_t len = readlink (link_path, exe_path, sizeof(exe_path) - 1);
+ if (len <= 0)
+ 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)"))
{
- ssize_t len = readlink(link_path, exe_path, sizeof(exe_path) - 1);
- if (len > 0)
- exe_path[len] = 0;
+ exe_path[len - deleted_len] = 0;
+ }
- 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;
- }
- }
+ process_info.SetProcessID(pid);
process_info.GetExecutableFile().SetFile(exe_path, false);
lldb::DataBufferSP buf_sp;
@@ -166,11 +357,27 @@
next_arg += strlen(next_arg) + 1;
}
- // FIXME: Parse /proc/<pid>/status to get uid, gid, euid, egid and
parent_pid
+ // Read /proc/$PID/stat to get our parent pid.
+ if (ReadProcPseudoFileStat (pid, stat_info))
+ {
+ process_info.SetParentProcessID (stat_info.ppid);
+ }
+ // Get User and Group IDs and get tracer pid.
+ GetLinuxProcessUserAndGroup (pid, process_info, tracerpid);
+
return true;
}
+bool
+Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
+{
+ lldb::pid_t tracerpid;
+ ProcessStatInfo stat_info;
+
+ return GetProcessAndStatInfo (pid, process_info, stat_info, tracerpid);
+}
+
void
Host::ThreadCreated (const char *thread_name)
{
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20130514/12557b65/attachment.html>
More information about the lldb-dev
mailing list