blob: d3883485c1c873f7b522c01fc7bca65c0dd71022 [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"
Jeff Brown053b8652012-06-06 16:25:03 -070033#include "utility.h"
34
Jeff Brown053b8652012-06-06 16:25:03 -070035static void dump_process_header(log_t* log, pid_t pid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080036 char path[PATH_MAX];
37 char procnamebuf[1024];
38 char* procname = NULL;
39 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070040
Christopher Ferris20303f82014-01-10 16:33:16 -080041 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
42 if ((fp = fopen(path, "r"))) {
43 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
44 fclose(fp);
45 }
Jeff Brown053b8652012-06-06 16:25:03 -070046
Christopher Ferris20303f82014-01-10 16:33:16 -080047 time_t t = time(NULL);
48 struct tm tm;
49 localtime_r(&t, &tm);
50 char timestr[64];
51 strftime(timestr, sizeof(timestr), "%F %T", &tm);
52 _LOG(log, SCOPE_AT_FAULT, "\n\n----- pid %d at %s -----\n", pid, timestr);
Jeff Brown053b8652012-06-06 16:25:03 -070053
Christopher Ferris20303f82014-01-10 16:33:16 -080054 if (procname) {
55 _LOG(log, SCOPE_AT_FAULT, "Cmd line: %s\n", procname);
56 }
Jeff Brown053b8652012-06-06 16:25:03 -070057}
58
59static void dump_process_footer(log_t* log, pid_t pid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080060 _LOG(log, SCOPE_AT_FAULT, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070061}
62
Christopher Ferris20303f82014-01-10 16:33:16 -080063static void dump_thread(
64 log_t* log, pid_t tid, bool attached, bool* detach_failed, int* total_sleep_time_usec) {
65 char path[PATH_MAX];
66 char threadnamebuf[1024];
67 char* threadname = NULL;
68 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070069
Christopher Ferris20303f82014-01-10 16:33:16 -080070 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
71 if ((fp = fopen(path, "r"))) {
72 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
73 fclose(fp);
74 if (threadname) {
75 size_t len = strlen(threadname);
76 if (len && threadname[len - 1] == '\n') {
77 threadname[len - 1] = '\0';
78 }
Jeff Brown053b8652012-06-06 16:25:03 -070079 }
Christopher Ferris20303f82014-01-10 16:33:16 -080080 }
Jeff Brown053b8652012-06-06 16:25:03 -070081
Christopher Ferris20303f82014-01-10 16:33:16 -080082 _LOG(log, SCOPE_AT_FAULT, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
Jeff Brown053b8652012-06-06 16:25:03 -070083
Christopher Ferris20303f82014-01-10 16:33:16 -080084 if (!attached && ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
85 _LOG(log, SCOPE_AT_FAULT, "Could not attach to thread: %s\n", strerror(errno));
86 return;
87 }
Jeff Brown053b8652012-06-06 16:25:03 -070088
Christopher Ferris20303f82014-01-10 16:33:16 -080089 wait_for_stop(tid, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -070090
Christopher Ferris20303f82014-01-10 16:33:16 -080091 UniquePtr<Backtrace> backtrace(Backtrace::Create(tid, BACKTRACE_CURRENT_THREAD));
92 if (backtrace->Unwind(0)) {
93 dump_backtrace_to_log(backtrace.get(), log, SCOPE_AT_FAULT, " ");
94 }
Jeff Brown053b8652012-06-06 16:25:03 -070095
Christopher Ferris20303f82014-01-10 16:33:16 -080096 if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
97 LOG("ptrace detach from %d failed: %s\n", tid, strerror(errno));
98 *detach_failed = true;
99 }
Jeff Brown053b8652012-06-06 16:25:03 -0700100}
101
Christopher Tateded2e5a2013-03-19 13:12:23 -0700102void dump_backtrace(int fd, int amfd, pid_t pid, pid_t tid, bool* detach_failed,
Christopher Ferris20303f82014-01-10 16:33:16 -0800103 int* total_sleep_time_usec) {
104 log_t log;
105 log.tfd = fd;
106 log.amfd = amfd;
107 log.quiet = true;
Jeff Brown053b8652012-06-06 16:25:03 -0700108
Christopher Ferris20303f82014-01-10 16:33:16 -0800109 dump_process_header(&log, pid);
110 dump_thread(&log, tid, true, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700111
Christopher Ferris20303f82014-01-10 16:33:16 -0800112 char task_path[64];
113 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
114 DIR* d = opendir(task_path);
115 if (d != NULL) {
116 struct dirent* de = NULL;
117 while ((de = readdir(d)) != NULL) {
118 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
119 continue;
120 }
Jeff Brown053b8652012-06-06 16:25:03 -0700121
Christopher Ferris20303f82014-01-10 16:33:16 -0800122 char* end;
123 pid_t new_tid = strtoul(de->d_name, &end, 10);
124 if (*end || new_tid == tid) {
125 continue;
126 }
Jeff Brown053b8652012-06-06 16:25:03 -0700127
Christopher Ferris20303f82014-01-10 16:33:16 -0800128 dump_thread(&log, new_tid, false, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700129 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800130 closedir(d);
131 }
Jeff Brown053b8652012-06-06 16:25:03 -0700132
Christopher Ferris20303f82014-01-10 16:33:16 -0800133 dump_process_footer(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700134}
135
Christopher Ferris20303f82014-01-10 16:33:16 -0800136void dump_backtrace_to_log(Backtrace* backtrace, log_t* log,
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700137 int scope_flags, const char* prefix) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800138 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
139 _LOG(log, scope_flags, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
140 }
Jeff Brown053b8652012-06-06 16:25:03 -0700141}