Merge "Revert "Revert "simpleperf: use libprocinfo."""
diff --git a/simpleperf/Android.mk b/simpleperf/Android.mk
index 3c40292..aab702b 100644
--- a/simpleperf/Android.mk
+++ b/simpleperf/Android.mk
@@ -43,6 +43,7 @@
libbase \
libcutils \
liblog \
+ libprocinfo \
libutils \
liblzma \
libLLVMObject \
@@ -71,6 +72,7 @@
libprotobuf-cpp-lite \
simpleperf_static_libraries_host_linux := \
+ libprocinfo \
libbacktrace_offline \
libbacktrace \
libunwind \
diff --git a/simpleperf/environment.cpp b/simpleperf/environment.cpp
index 373f3e0..e3fd4ee 100644
--- a/simpleperf/environment.cpp
+++ b/simpleperf/environment.cpp
@@ -31,6 +31,7 @@
#include <android-base/parseint.h>
#include <android-base/strings.h>
#include <android-base/stringprintf.h>
+#include <procinfo/process.h>
#if defined(__ANDROID__)
#include <sys/system_properties.h>
@@ -218,46 +219,22 @@
}
static bool ReadThreadNameAndPid(pid_t tid, std::string* comm, pid_t* pid) {
- std::string status_file = android::base::StringPrintf("/proc/%d/status", tid);
- FILE* fp = fopen(status_file.c_str(), "re");
- if (fp == nullptr) {
+ android::procinfo::ProcessInfo procinfo;
+ if (!android::procinfo::GetProcessInfo(tid, &procinfo)) {
return false;
}
- bool read_comm = false;
- bool read_tgid = false;
- LineReader reader(fp);
- char* line;
- while ((line = reader.ReadLine()) != nullptr) {
- char s[reader.MaxLineSize()];
- pid_t tgid;
- if (sscanf(line, "Name:%s", s) == 1) {
- read_comm = true;
- if (comm != nullptr) {
- *comm = s;
- }
- } else if (sscanf(line, "Tgid:%d", &tgid) == 1) {
- read_tgid = true;
- if (pid != nullptr) {
- *pid = tgid;
- }
- }
- if (read_comm && read_tgid) {
- return true;
- }
+ if (comm != nullptr) {
+ *comm = procinfo.name;
}
- return false;
+ if (pid != nullptr) {
+ *pid = procinfo.pid;
+ }
+ return true;
}
std::vector<pid_t> GetThreadsInProcess(pid_t pid) {
std::vector<pid_t> result;
- std::string task_dirname = android::base::StringPrintf("/proc/%d/task", pid);
- for (const auto& name : GetSubDirs(task_dirname)) {
- int tid;
- if (!android::base::ParseInt(name.c_str(), &tid, 0)) {
- continue;
- }
- result.push_back(tid);
- }
+ android::procinfo::GetProcessTids(pid, &result);
return result;
}