blob: aa7a3c29725f1cb507150f57d0d1ac12f5d91413 [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>
18#include <stdbool.h>
19#include <stdlib.h>
20#include <string.h>
21#include <stdio.h>
22#include <time.h>
23#include <errno.h>
24#include <limits.h>
25#include <dirent.h>
26#include <unistd.h>
27#include <sys/types.h>
28#include <sys/ptrace.h>
29
Christopher Ferris365e4ae2013-10-02 12:26:48 -070030#include <backtrace/backtrace.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) {
36 char path[PATH_MAX];
37 char procnamebuf[1024];
38 char* procname = NULL;
39 FILE* fp;
40
41 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 }
46
47 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);
Christopher Tate7716aef2013-04-02 14:00:27 -070052 _LOG(log, SCOPE_AT_FAULT, "\n\n----- pid %d at %s -----\n", pid, timestr);
Jeff Brown053b8652012-06-06 16:25:03 -070053
54 if (procname) {
Christopher Tate7716aef2013-04-02 14:00:27 -070055 _LOG(log, SCOPE_AT_FAULT, "Cmd line: %s\n", procname);
Jeff Brown053b8652012-06-06 16:25:03 -070056 }
57}
58
59static void dump_process_footer(log_t* log, pid_t pid) {
Christopher Tate7716aef2013-04-02 14:00:27 -070060 _LOG(log, SCOPE_AT_FAULT, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070061}
62
Christopher Ferris365e4ae2013-10-02 12:26:48 -070063static void dump_thread(log_t* log, pid_t tid, bool attached,
Jeff Brown053b8652012-06-06 16:25:03 -070064 bool* detach_failed, int* total_sleep_time_usec) {
65 char path[PATH_MAX];
66 char threadnamebuf[1024];
67 char* threadname = NULL;
68 FILE* fp;
69
70 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 }
79 }
80 }
81
Christopher Tate7716aef2013-04-02 14:00:27 -070082 _LOG(log, SCOPE_AT_FAULT, "\n\"%s\" sysTid=%d\n",
83 threadname ? threadname : "<unknown>", tid);
Jeff Brown053b8652012-06-06 16:25:03 -070084
85 if (!attached && ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
Christopher Tate7716aef2013-04-02 14:00:27 -070086 _LOG(log, SCOPE_AT_FAULT, "Could not attach to thread: %s\n", strerror(errno));
Jeff Brown053b8652012-06-06 16:25:03 -070087 return;
88 }
89
90 wait_for_stop(tid, total_sleep_time_usec);
91
Christopher Ferris17e91d42013-10-21 13:30:52 -070092 backtrace_context_t context;
93 if (!backtrace_create_context(&context, tid, -1, 0)) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -070094 _LOG(log, SCOPE_AT_FAULT, "Could not create backtrace context.\n");
Jeff Brown053b8652012-06-06 16:25:03 -070095 } else {
Christopher Ferris17e91d42013-10-21 13:30:52 -070096 dump_backtrace_to_log(&context, log, SCOPE_AT_FAULT, " ");
97 backtrace_destroy_context(&context);
Jeff Brown053b8652012-06-06 16:25:03 -070098 }
99
100 if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
101 LOG("ptrace detach from %d failed: %s\n", tid, strerror(errno));
102 *detach_failed = true;
103 }
104}
105
Christopher Tateded2e5a2013-03-19 13:12:23 -0700106void dump_backtrace(int fd, int amfd, pid_t pid, pid_t tid, bool* detach_failed,
Jeff Brown053b8652012-06-06 16:25:03 -0700107 int* total_sleep_time_usec) {
108 log_t log;
109 log.tfd = fd;
Christopher Tateded2e5a2013-03-19 13:12:23 -0700110 log.amfd = amfd;
Jeff Brown053b8652012-06-06 16:25:03 -0700111 log.quiet = true;
112
Jeff Brown053b8652012-06-06 16:25:03 -0700113 dump_process_header(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700114 dump_thread(&log, tid, true, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700115
116 char task_path[64];
117 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
118 DIR* d = opendir(task_path);
Elliott Hughesc463d2c2012-10-26 16:47:09 -0700119 if (d != NULL) {
120 struct dirent* de = NULL;
121 while ((de = readdir(d)) != NULL) {
Jeff Brown053b8652012-06-06 16:25:03 -0700122 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
123 continue;
124 }
125
126 char* end;
127 pid_t new_tid = strtoul(de->d_name, &end, 10);
128 if (*end || new_tid == tid) {
129 continue;
130 }
131
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700132 dump_thread(&log, new_tid, false, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700133 }
134 closedir(d);
135 }
136
137 dump_process_footer(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700138}
139
Christopher Ferris17e91d42013-10-21 13:30:52 -0700140void dump_backtrace_to_log(const backtrace_context_t* context, log_t* log,
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700141 int scope_flags, const char* prefix) {
142 char buf[512];
Christopher Ferris17e91d42013-10-21 13:30:52 -0700143 for (size_t i = 0; i < context->backtrace->num_frames; i++) {
144 backtrace_format_frame_data(context, i, buf, sizeof(buf));
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700145 _LOG(log, scope_flags, "%s%s\n", prefix, buf);
146 }
Jeff Brown053b8652012-06-06 16:25:03 -0700147}