blob: 79ee4e5b2db1f61088aac3c91b9b8e597b5a606a [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 Ferris20303f82014-01-10 16:33:16 -080031#include <backtrace/Backtrace.h>
Christopher Ferris30c942c2015-05-14 15:39:52 -070032
33#include <log/log.h>
Christopher Ferris20303f82014-01-10 16:33:16 -080034#include <UniquePtr.h>
Jeff Brown053b8652012-06-06 16:25:03 -070035
Christopher Ferris365e4ae2013-10-02 12:26:48 -070036#include "backtrace.h"
Brigid Smith62ba4892014-06-10 11:53:08 -070037
Jeff Brown053b8652012-06-06 16:25:03 -070038#include "utility.h"
39
Jeff Brown053b8652012-06-06 16:25:03 -070040static void dump_process_header(log_t* log, pid_t pid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080041 char path[PATH_MAX];
42 char procnamebuf[1024];
43 char* procname = NULL;
44 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070045
Christopher Ferris20303f82014-01-10 16:33:16 -080046 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
47 if ((fp = fopen(path, "r"))) {
48 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
49 fclose(fp);
50 }
Jeff Brown053b8652012-06-06 16:25:03 -070051
Christopher Ferris20303f82014-01-10 16:33:16 -080052 time_t t = time(NULL);
53 struct tm tm;
54 localtime_r(&t, &tm);
55 char timestr[64];
56 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Jeff Brown9b12d532014-09-10 15:47:49 -070057 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
Jeff Brown053b8652012-06-06 16:25:03 -070058
Christopher Ferris20303f82014-01-10 16:33:16 -080059 if (procname) {
Brigid Smith62ba4892014-06-10 11:53:08 -070060 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname);
Christopher Ferris20303f82014-01-10 16:33:16 -080061 }
Jeff Brown9b12d532014-09-10 15:47:49 -070062 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070063}
64
65static void dump_process_footer(log_t* log, pid_t pid) {
Brigid Smith62ba4892014-06-10 11:53:08 -070066 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070067}
68
Christopher Ferris20303f82014-01-10 16:33:16 -080069static void dump_thread(
70 log_t* log, pid_t tid, bool attached, bool* detach_failed, int* total_sleep_time_usec) {
71 char path[PATH_MAX];
72 char threadnamebuf[1024];
73 char* threadname = NULL;
74 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070075
Christopher Ferris20303f82014-01-10 16:33:16 -080076 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
77 if ((fp = fopen(path, "r"))) {
78 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
79 fclose(fp);
80 if (threadname) {
81 size_t len = strlen(threadname);
82 if (len && threadname[len - 1] == '\n') {
83 threadname[len - 1] = '\0';
84 }
Jeff Brown053b8652012-06-06 16:25:03 -070085 }
Christopher Ferris20303f82014-01-10 16:33:16 -080086 }
Jeff Brown053b8652012-06-06 16:25:03 -070087
Brigid Smith62ba4892014-06-10 11:53:08 -070088 _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
Jeff Brown053b8652012-06-06 16:25:03 -070089
Christopher Ferris20303f82014-01-10 16:33:16 -080090 if (!attached && ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
Brigid Smith62ba4892014-06-10 11:53:08 -070091 _LOG(log, logtype::BACKTRACE, "Could not attach to thread: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080092 return;
93 }
Jeff Brown053b8652012-06-06 16:25:03 -070094
Christopher Ferris1072f912014-10-31 21:34:38 -070095 if (!attached && wait_for_sigstop(tid, total_sleep_time_usec, detach_failed) == -1) {
96 return;
97 }
Jeff Brown053b8652012-06-06 16:25:03 -070098
Christopher Ferris20303f82014-01-10 16:33:16 -080099 UniquePtr<Backtrace> backtrace(Backtrace::Create(tid, BACKTRACE_CURRENT_THREAD));
100 if (backtrace->Unwind(0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700101 dump_backtrace_to_log(backtrace.get(), log, " ");
Christopher Ferris30c942c2015-05-14 15:39:52 -0700102 } else {
103 ALOGE("Unwind failed: tid = %d", tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 }
Jeff Brown053b8652012-06-06 16:25:03 -0700105
Christopher Ferris20303f82014-01-10 16:33:16 -0800106 if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700107 _LOG(log, logtype::ERROR, "ptrace detach from %d failed: %s\n", tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800108 *detach_failed = true;
109 }
Jeff Brown053b8652012-06-06 16:25:03 -0700110}
111
Christopher Tateded2e5a2013-03-19 13:12:23 -0700112void dump_backtrace(int fd, int amfd, pid_t pid, pid_t tid, bool* detach_failed,
Christopher Ferris20303f82014-01-10 16:33:16 -0800113 int* total_sleep_time_usec) {
114 log_t log;
115 log.tfd = fd;
116 log.amfd = amfd;
Jeff Brown053b8652012-06-06 16:25:03 -0700117
Christopher Ferris20303f82014-01-10 16:33:16 -0800118 dump_process_header(&log, pid);
119 dump_thread(&log, tid, true, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700120
Christopher Ferris20303f82014-01-10 16:33:16 -0800121 char task_path[64];
122 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
123 DIR* d = opendir(task_path);
124 if (d != NULL) {
125 struct dirent* de = NULL;
126 while ((de = readdir(d)) != NULL) {
127 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
128 continue;
129 }
Jeff Brown053b8652012-06-06 16:25:03 -0700130
Christopher Ferris20303f82014-01-10 16:33:16 -0800131 char* end;
132 pid_t new_tid = strtoul(de->d_name, &end, 10);
133 if (*end || new_tid == tid) {
134 continue;
135 }
Jeff Brown053b8652012-06-06 16:25:03 -0700136
Christopher Ferris20303f82014-01-10 16:33:16 -0800137 dump_thread(&log, new_tid, false, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700138 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800139 closedir(d);
140 }
Jeff Brown053b8652012-06-06 16:25:03 -0700141
Christopher Ferris20303f82014-01-10 16:33:16 -0800142 dump_process_footer(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700143}
144
Brigid Smith62ba4892014-06-10 11:53:08 -0700145void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800146 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700147 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800148 }
Jeff Brown053b8652012-06-06 16:25:03 -0700149}