blob: c2a1dbc1c881fca903d915a31c2dc31de0f6649f [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
17#include <stddef.h>
Jeff Brown053b8652012-06-06 16:25:03 -070018#include <stdlib.h>
19#include <string.h>
20#include <stdio.h>
21#include <time.h>
22#include <errno.h>
23#include <limits.h>
24#include <dirent.h>
25#include <unistd.h>
26#include <sys/types.h>
27#include <sys/ptrace.h>
28
Christopher Ferris20303f82014-01-10 16:33:16 -080029#include <backtrace/Backtrace.h>
30#include <UniquePtr.h>
Jeff Brown053b8652012-06-06 16:25:03 -070031
Christopher Ferris365e4ae2013-10-02 12:26:48 -070032#include "backtrace.h"
Brigid Smith62ba4892014-06-10 11:53:08 -070033
Jeff Brown053b8652012-06-06 16:25:03 -070034#include "utility.h"
35
Jeff Brown053b8652012-06-06 16:25:03 -070036static void dump_process_header(log_t* log, pid_t pid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080037 char path[PATH_MAX];
38 char procnamebuf[1024];
39 char* procname = NULL;
40 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070041
Christopher Ferris20303f82014-01-10 16:33:16 -080042 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
43 if ((fp = fopen(path, "r"))) {
44 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
45 fclose(fp);
46 }
Jeff Brown053b8652012-06-06 16:25:03 -070047
Christopher Ferris20303f82014-01-10 16:33:16 -080048 time_t t = time(NULL);
49 struct tm tm;
50 localtime_r(&t, &tm);
51 char timestr[64];
52 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Jeff Brown9b12d532014-09-10 15:47:49 -070053 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
Jeff Brown053b8652012-06-06 16:25:03 -070054
Christopher Ferris20303f82014-01-10 16:33:16 -080055 if (procname) {
Brigid Smith62ba4892014-06-10 11:53:08 -070056 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname);
Christopher Ferris20303f82014-01-10 16:33:16 -080057 }
Jeff Brown9b12d532014-09-10 15:47:49 -070058 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070059}
60
61static void dump_process_footer(log_t* log, pid_t pid) {
Brigid Smith62ba4892014-06-10 11:53:08 -070062 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070063}
64
Christopher Ferris20303f82014-01-10 16:33:16 -080065static void dump_thread(
66 log_t* log, pid_t tid, bool attached, bool* detach_failed, int* total_sleep_time_usec) {
67 char path[PATH_MAX];
68 char threadnamebuf[1024];
69 char* threadname = NULL;
70 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070071
Christopher Ferris20303f82014-01-10 16:33:16 -080072 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
73 if ((fp = fopen(path, "r"))) {
74 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
75 fclose(fp);
76 if (threadname) {
77 size_t len = strlen(threadname);
78 if (len && threadname[len - 1] == '\n') {
79 threadname[len - 1] = '\0';
80 }
Jeff Brown053b8652012-06-06 16:25:03 -070081 }
Christopher Ferris20303f82014-01-10 16:33:16 -080082 }
Jeff Brown053b8652012-06-06 16:25:03 -070083
Brigid Smith62ba4892014-06-10 11:53:08 -070084 _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
Jeff Brown053b8652012-06-06 16:25:03 -070085
Christopher Ferris20303f82014-01-10 16:33:16 -080086 if (!attached && ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
Brigid Smith62ba4892014-06-10 11:53:08 -070087 _LOG(log, logtype::BACKTRACE, "Could not attach to thread: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080088 return;
89 }
Jeff Brown053b8652012-06-06 16:25:03 -070090
Christopher Ferris84ddb342014-10-31 21:34:38 -070091 if (!attached && wait_for_sigstop(tid, total_sleep_time_usec, detach_failed) == -1) {
92 return;
93 }
Jeff Brown053b8652012-06-06 16:25:03 -070094
Christopher Ferris20303f82014-01-10 16:33:16 -080095 UniquePtr<Backtrace> backtrace(Backtrace::Create(tid, BACKTRACE_CURRENT_THREAD));
96 if (backtrace->Unwind(0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -070097 dump_backtrace_to_log(backtrace.get(), log, " ");
Christopher Ferris20303f82014-01-10 16:33:16 -080098 }
Jeff Brown053b8652012-06-06 16:25:03 -070099
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700101 _LOG(log, logtype::ERROR, "ptrace detach from %d failed: %s\n", tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800102 *detach_failed = true;
103 }
Jeff Brown053b8652012-06-06 16:25:03 -0700104}
105
Christopher Tateded2e5a2013-03-19 13:12:23 -0700106void dump_backtrace(int fd, int amfd, pid_t pid, pid_t tid, bool* detach_failed,
Christopher Ferris20303f82014-01-10 16:33:16 -0800107 int* total_sleep_time_usec) {
108 log_t log;
109 log.tfd = fd;
110 log.amfd = amfd;
Jeff Brown053b8652012-06-06 16:25:03 -0700111
Christopher Ferris20303f82014-01-10 16:33:16 -0800112 dump_process_header(&log, pid);
113 dump_thread(&log, tid, true, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700114
Christopher Ferris20303f82014-01-10 16:33:16 -0800115 char task_path[64];
116 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
117 DIR* d = opendir(task_path);
118 if (d != NULL) {
119 struct dirent* de = NULL;
120 while ((de = readdir(d)) != NULL) {
121 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
122 continue;
123 }
Jeff Brown053b8652012-06-06 16:25:03 -0700124
Christopher Ferris20303f82014-01-10 16:33:16 -0800125 char* end;
126 pid_t new_tid = strtoul(de->d_name, &end, 10);
127 if (*end || new_tid == tid) {
128 continue;
129 }
Jeff Brown053b8652012-06-06 16:25:03 -0700130
Christopher Ferris20303f82014-01-10 16:33:16 -0800131 dump_thread(&log, new_tid, false, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700132 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800133 closedir(d);
134 }
Jeff Brown053b8652012-06-06 16:25:03 -0700135
Christopher Ferris20303f82014-01-10 16:33:16 -0800136 dump_process_footer(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700137}
138
Brigid Smith62ba4892014-06-10 11:53:08 -0700139void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800140 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700141 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800142 }
Jeff Brown053b8652012-06-06 16:25:03 -0700143}