1 /*
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
17 #define LOG_TAG "DEBUG"
18
19 #include <stddef.h>
20 #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
31 #include <memory>
32
33 #include <backtrace/Backtrace.h>
34
35 #include <log/log.h>
36
37 #include "backtrace.h"
38
39 #include "utility.h"
40
dump_process_header(log_t * log,pid_t pid)41 static void dump_process_header(log_t* log, pid_t pid) {
42 char path[PATH_MAX];
43 char procnamebuf[1024];
44 char* procname = NULL;
45 FILE* fp;
46
47 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 }
52
53 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);
58 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
59
60 if (procname) {
61 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname);
62 }
63 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
64 }
65
dump_process_footer(log_t * log,pid_t pid)66 static void dump_process_footer(log_t* log, pid_t pid) {
67 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
68 }
69
dump_thread(log_t * log,pid_t pid,pid_t tid,bool attached,bool * detach_failed,int * total_sleep_time_usec)70 static void dump_thread(log_t* log, pid_t pid, pid_t tid, bool attached,
71 bool* detach_failed, int* total_sleep_time_usec) {
72 char path[PATH_MAX];
73 char threadnamebuf[1024];
74 char* threadname = NULL;
75 FILE* fp;
76
77 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 }
86 }
87 }
88
89 _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
90
91 if (!attached && !ptrace_attach_thread(pid, tid)) {
92 _LOG(log, logtype::BACKTRACE, "Could not attach to thread: %s\n", strerror(errno));
93 return;
94 }
95
96 if (!attached && wait_for_sigstop(tid, total_sleep_time_usec, detach_failed) == -1) {
97 return;
98 }
99
100 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(tid, BACKTRACE_CURRENT_THREAD));
101 if (backtrace->Unwind(0)) {
102 dump_backtrace_to_log(backtrace.get(), log, " ");
103 } else {
104 ALOGE("Unwind failed: tid = %d", tid);
105 }
106
107 if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
108 _LOG(log, logtype::ERROR, "ptrace detach from %d failed: %s\n", tid, strerror(errno));
109 *detach_failed = true;
110 }
111 }
112
dump_backtrace(int fd,int amfd,pid_t pid,pid_t tid,bool * detach_failed,int * total_sleep_time_usec)113 void dump_backtrace(int fd, int amfd, pid_t pid, pid_t tid, bool* detach_failed,
114 int* total_sleep_time_usec) {
115 log_t log;
116 log.tfd = fd;
117 log.amfd = amfd;
118
119 dump_process_header(&log, pid);
120 dump_thread(&log, pid, tid, true, detach_failed, total_sleep_time_usec);
121
122 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 }
131
132 char* end;
133 pid_t new_tid = strtoul(de->d_name, &end, 10);
134 if (*end || new_tid == tid) {
135 continue;
136 }
137
138 dump_thread(&log, pid, new_tid, false, detach_failed, total_sleep_time_usec);
139 }
140 closedir(d);
141 }
142
143 dump_process_footer(&log, pid);
144 }
145
dump_backtrace_to_log(Backtrace * backtrace,log_t * log,const char * prefix)146 void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
147 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
148 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
149 }
150 }
151