summaryrefslogtreecommitdiffstats
path: root/debuggerd/utility.cpp
diff options
context:
space:
mode:
authorJessica Wagantall <jwagantall@cyngn.com>2016-09-07 13:30:24 -0700
committerJessica Wagantall <jwagantall@cyngn.com>2016-09-07 13:30:24 -0700
commitb58cc75fe8243ca0319d6d6db27a750579e01433 (patch)
tree80b7b6cac8fee1c2cf54316b8912caf29d5de6d0 /debuggerd/utility.cpp
parentc00328f99aad5f1e8e879557f142981f08146fe3 (diff)
parent4cc6d3d4057ef566a87a2e1db2c4c152b52e0765 (diff)
downloadsystem_core-b58cc75fe8243ca0319d6d6db27a750579e01433.tar.gz
system_core-b58cc75fe8243ca0319d6d6db27a750579e01433.tar.bz2
system_core-b58cc75fe8243ca0319d6d6db27a750579e01433.zip
Merge tag 'android-6.0.1_r66' into HEAD
Android 6.0.1 release 66 Change-Id: I5ccc6e68283e30b8d0419eb7512c7183e58ec5ed
Diffstat (limited to 'debuggerd/utility.cpp')
-rw-r--r--debuggerd/utility.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/debuggerd/utility.cpp b/debuggerd/utility.cpp
index 9f340a8ac..236d667a3 100644
--- a/debuggerd/utility.cpp
+++ b/debuggerd/utility.cpp
@@ -20,6 +20,7 @@
#include <errno.h>
#include <signal.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ptrace.h>
@@ -207,3 +208,31 @@ void dump_memory(log_t* log, Backtrace* backtrace, uintptr_t addr, const char* f
_LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str());
}
}
+
+bool pid_contains_tid(pid_t pid, pid_t tid) {
+ char task_path[PATH_MAX];
+ if (snprintf(task_path, PATH_MAX, "/proc/%d/task/%d", pid, tid) >= PATH_MAX) {
+ ALOGE("debuggerd: task path overflow (pid = %d, tid = %d)\n", pid, tid);
+ exit(1);
+ }
+
+ return access(task_path, F_OK) == 0;
+}
+
+// Attach to a thread, and verify that it's still a member of the given process
+bool ptrace_attach_thread(pid_t pid, pid_t tid) {
+ if (ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) {
+ return false;
+ }
+
+ // Make sure that the task we attached to is actually part of the pid we're dumping.
+ if (!pid_contains_tid(pid, tid)) {
+ if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
+ ALOGE("debuggerd: failed to detach from thread '%d'", tid);
+ exit(1);
+ }
+ return false;
+ }
+
+ return true;
+}