blob: b46f8f443cb0a1e3fec8a2809d063a9238d938f0 [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Christopher Ferris30c942c2015-05-14 15:39:52 -070017#define LOG_TAG "DEBUG"
18
Jeff Brown053b8652012-06-06 16:25:03 -070019#include <stddef.h>
Jeff Brown053b8652012-06-06 16:25:03 -070020#include <stdlib.h>
21#include <string.h>
22#include <stdio.h>
23#include <time.h>
24#include <errno.h>
25#include <limits.h>
26#include <dirent.h>
27#include <unistd.h>
28#include <sys/types.h>
29#include <sys/ptrace.h>
30
Christopher Ferris6e964032015-05-15 17:30:21 -070031#include <memory>
32
Christopher Ferris20303f82014-01-10 16:33:16 -080033#include <backtrace/Backtrace.h>
Christopher Ferris30c942c2015-05-14 15:39:52 -070034
35#include <log/log.h>
Jeff Brown053b8652012-06-06 16:25:03 -070036
Christopher Ferris365e4ae2013-10-02 12:26:48 -070037#include "backtrace.h"
Brigid Smith62ba4892014-06-10 11:53:08 -070038
Jeff Brown053b8652012-06-06 16:25:03 -070039#include "utility.h"
40
Jeff Brown053b8652012-06-06 16:25:03 -070041static void dump_process_header(log_t* log, pid_t pid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080042 char path[PATH_MAX];
43 char procnamebuf[1024];
44 char* procname = NULL;
45 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070046
Christopher Ferris20303f82014-01-10 16:33:16 -080047 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
48 if ((fp = fopen(path, "r"))) {
49 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
50 fclose(fp);
51 }
Jeff Brown053b8652012-06-06 16:25:03 -070052
Christopher Ferris20303f82014-01-10 16:33:16 -080053 time_t t = time(NULL);
54 struct tm tm;
55 localtime_r(&t, &tm);
56 char timestr[64];
57 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Jeff Brown9b12d532014-09-10 15:47:49 -070058 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
Jeff Brown053b8652012-06-06 16:25:03 -070059
Christopher Ferris20303f82014-01-10 16:33:16 -080060 if (procname) {
Brigid Smith62ba4892014-06-10 11:53:08 -070061 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname);
Christopher Ferris20303f82014-01-10 16:33:16 -080062 }
Jeff Brown9b12d532014-09-10 15:47:49 -070063 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070064}
65
66static void dump_process_footer(log_t* log, pid_t pid) {
Brigid Smith62ba4892014-06-10 11:53:08 -070067 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070068}
69
Christopher Ferris20303f82014-01-10 16:33:16 -080070static void dump_thread(
71 log_t* log, pid_t tid, bool attached, bool* detach_failed, int* total_sleep_time_usec) {
72 char path[PATH_MAX];
73 char threadnamebuf[1024];
74 char* threadname = NULL;
75 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070076
Christopher Ferris20303f82014-01-10 16:33:16 -080077 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
78 if ((fp = fopen(path, "r"))) {
79 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
80 fclose(fp);
81 if (threadname) {
82 size_t len = strlen(threadname);
83 if (len && threadname[len - 1] == '\n') {
84 threadname[len - 1] = '\0';
85 }
Jeff Brown053b8652012-06-06 16:25:03 -070086 }
Christopher Ferris20303f82014-01-10 16:33:16 -080087 }
Jeff Brown053b8652012-06-06 16:25:03 -070088
Brigid Smith62ba4892014-06-10 11:53:08 -070089 _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
Jeff Brown053b8652012-06-06 16:25:03 -070090
Christopher Ferris20303f82014-01-10 16:33:16 -080091 if (!attached && ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
Brigid Smith62ba4892014-06-10 11:53:08 -070092 _LOG(log, logtype::BACKTRACE, "Could not attach to thread: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080093 return;
94 }
Jeff Brown053b8652012-06-06 16:25:03 -070095
Christopher Ferris1072f912014-10-31 21:34:38 -070096 if (!attached && wait_for_sigstop(tid, total_sleep_time_usec, detach_failed) == -1) {
97 return;
98 }
Jeff Brown053b8652012-06-06 16:25:03 -070099
Christopher Ferris6e964032015-05-15 17:30:21 -0700100 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(tid, BACKTRACE_CURRENT_THREAD));
Christopher Ferris20303f82014-01-10 16:33:16 -0800101 if (backtrace->Unwind(0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700102 dump_backtrace_to_log(backtrace.get(), log, " ");
Christopher Ferris30c942c2015-05-14 15:39:52 -0700103 } else {
104 ALOGE("Unwind failed: tid = %d", tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800105 }
Jeff Brown053b8652012-06-06 16:25:03 -0700106
Christopher Ferris20303f82014-01-10 16:33:16 -0800107 if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -0700108 ALOGE("ptrace detach from %d failed: %s\n", tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800109 *detach_failed = true;
110 }
Jeff Brown053b8652012-06-06 16:25:03 -0700111}
112
Christopher Tateded2e5a2013-03-19 13:12:23 -0700113void dump_backtrace(int fd, int amfd, pid_t pid, pid_t tid, bool* detach_failed,
Christopher Ferris20303f82014-01-10 16:33:16 -0800114 int* total_sleep_time_usec) {
115 log_t log;
116 log.tfd = fd;
117 log.amfd = amfd;
Jeff Brown053b8652012-06-06 16:25:03 -0700118
Christopher Ferris20303f82014-01-10 16:33:16 -0800119 dump_process_header(&log, pid);
120 dump_thread(&log, tid, true, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700121
Christopher Ferris20303f82014-01-10 16:33:16 -0800122 char task_path[64];
123 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
124 DIR* d = opendir(task_path);
125 if (d != NULL) {
126 struct dirent* de = NULL;
127 while ((de = readdir(d)) != NULL) {
128 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
129 continue;
130 }
Jeff Brown053b8652012-06-06 16:25:03 -0700131
Christopher Ferris20303f82014-01-10 16:33:16 -0800132 char* end;
133 pid_t new_tid = strtoul(de->d_name, &end, 10);
134 if (*end || new_tid == tid) {
135 continue;
136 }
Jeff Brown053b8652012-06-06 16:25:03 -0700137
Christopher Ferris20303f82014-01-10 16:33:16 -0800138 dump_thread(&log, new_tid, false, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700139 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800140 closedir(d);
141 }
Jeff Brown053b8652012-06-06 16:25:03 -0700142
Christopher Ferris20303f82014-01-10 16:33:16 -0800143 dump_process_footer(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700144}
145
Brigid Smith62ba4892014-06-10 11:53:08 -0700146void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800147 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700148 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800149 }
Jeff Brown053b8652012-06-06 16:25:03 -0700150}