summaryrefslogtreecommitdiffstats
path: root/libcutils/debugger.c
diff options
context:
space:
mode:
authorJeff Brown <jeffbrown@google.com>2012-06-06 16:25:03 -0700
committerJeff Brown <jeffbrown@google.com>2012-06-08 13:29:23 -0700
commit053b865412d1982ad1dc0e840898d82527deeb99 (patch)
tree2312442a46a05d24230e832700ad1300af9a2d9f /libcutils/debugger.c
parent5f2d00b0677cbe9ad42ea0394def0a51aef7bdda (diff)
downloadsystem_core-053b865412d1982ad1dc0e840898d82527deeb99.zip
system_core-053b865412d1982ad1dc0e840898d82527deeb99.tar.gz
system_core-053b865412d1982ad1dc0e840898d82527deeb99.tar.bz2
Enhance native stack dumps.
Provides a new mechanism for dumpstate (while running as root) to request that debuggerd dump the stacks of native processes that we care about in bug reports. In this mode, the backtrace is formatted to look similar to a Dalvik backtrace. Moved the tombstone generating code into a separate file to make it easier to maintain. Fixed a bug where sometimes the stack traces would be incomplete because we were not waiting for each thread to stop after issuing PTRACE_ATTACH, only the main thread. So sometimes we were missing traces for some threads. Refactored the logging code to prevent accidentally writing data to logcat when explicitly dumping a tombstone or backtrace from the console. Only root or system server can request to dump backtraces but only root can dump tombstones. Bug: 6615693 Change-Id: Ib3edcc16f9f3a687e414e3f2d250d9500566123b
Diffstat (limited to 'libcutils/debugger.c')
-rw-r--r--libcutils/debugger.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/libcutils/debugger.c b/libcutils/debugger.c
new file mode 100644
index 0000000..9425006
--- /dev/null
+++ b/libcutils/debugger.c
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <cutils/debugger.h>
+#include <cutils/sockets.h>
+
+int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) {
+ int s = socket_local_client(DEBUGGER_SOCKET_NAME,
+ ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
+ if (s < 0) {
+ return -1;
+ }
+
+ debugger_msg_t msg;
+ msg.tid = tid;
+ msg.action = DEBUGGER_ACTION_DUMP_TOMBSTONE;
+
+ int result = 0;
+ if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
+ result = -1;
+ } else {
+ char ack;
+ if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
+ result = -1;
+ } else {
+ if (pathbuf && pathlen) {
+ ssize_t n = TEMP_FAILURE_RETRY(read(s, pathbuf, pathlen - 1));
+ if (n <= 0) {
+ result = -1;
+ } else {
+ pathbuf[n] = '\0';
+ }
+ }
+ }
+ }
+ TEMP_FAILURE_RETRY(close(s));
+ return result;
+}
+
+int dump_backtrace_to_file(pid_t tid, int fd) {
+ int s = socket_local_client(DEBUGGER_SOCKET_NAME,
+ ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
+ if (s < 0) {
+ return -1;
+ }
+
+ debugger_msg_t msg;
+ msg.tid = tid;
+ msg.action = DEBUGGER_ACTION_DUMP_BACKTRACE;
+
+ int result = 0;
+ if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
+ result = -1;
+ } else {
+ char ack;
+ if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
+ result = -1;
+ } else {
+ char buffer[4096];
+ ssize_t n;
+ while ((n = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)))) > 0) {
+ if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) {
+ result = -1;
+ break;
+ }
+ }
+ }
+ }
+ TEMP_FAILURE_RETRY(close(s));
+ return result;
+}