blob: d08242e76167b5262773166aa2f342ba02684557 [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);
Brigid Smith62ba4892014-06-10 11:53:08 -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 Brown053b8652012-06-06 16:25:03 -070058}
59
60static void dump_process_footer(log_t* log, pid_t pid) {
Brigid Smith62ba4892014-06-10 11:53:08 -070061 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070062}
63
Christopher Ferris20303f82014-01-10 16:33:16 -080064static void dump_thread(
65 log_t* log, pid_t tid, bool attached, bool* detach_failed, int* total_sleep_time_usec) {
66 char path[PATH_MAX];
67 char threadnamebuf[1024];
68 char* threadname = NULL;
69 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070070
Christopher Ferris20303f82014-01-10 16:33:16 -080071 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
72 if ((fp = fopen(path, "r"))) {
73 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
74 fclose(fp);
75 if (threadname) {
76 size_t len = strlen(threadname);
77 if (len && threadname[len - 1] == '\n') {
78 threadname[len - 1] = '\0';
79 }
Jeff Brown053b8652012-06-06 16:25:03 -070080 }
Christopher Ferris20303f82014-01-10 16:33:16 -080081 }
Jeff Brown053b8652012-06-06 16:25:03 -070082
Brigid Smith62ba4892014-06-10 11:53:08 -070083 _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
Jeff Brown053b8652012-06-06 16:25:03 -070084
Christopher Ferris20303f82014-01-10 16:33:16 -080085 if (!attached && ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
Brigid Smith62ba4892014-06-10 11:53:08 -070086 _LOG(log, logtype::BACKTRACE, "Could not attach to thread: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080087 return;
88 }
Jeff Brown053b8652012-06-06 16:25:03 -070089
Christopher Ferris20303f82014-01-10 16:33:16 -080090 wait_for_stop(tid, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -070091
Christopher Ferris20303f82014-01-10 16:33:16 -080092 UniquePtr<Backtrace> backtrace(Backtrace::Create(tid, BACKTRACE_CURRENT_THREAD));
93 if (backtrace->Unwind(0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -070094 dump_backtrace_to_log(backtrace.get(), log, " ");
Christopher Ferris20303f82014-01-10 16:33:16 -080095 }
Jeff Brown053b8652012-06-06 16:25:03 -070096
Christopher Ferris20303f82014-01-10 16:33:16 -080097 if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
Brigid Smith62ba4892014-06-10 11:53:08 -070098 LOG_ERROR("ptrace detach from %d failed: %s\n", tid, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080099 *detach_failed = true;
100 }
Jeff Brown053b8652012-06-06 16:25:03 -0700101}
102
Christopher Tateded2e5a2013-03-19 13:12:23 -0700103void dump_backtrace(int fd, int amfd, pid_t pid, pid_t tid, bool* detach_failed,
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 int* total_sleep_time_usec) {
105 log_t log;
106 log.tfd = fd;
107 log.amfd = amfd;
108 log.quiet = true;
Jeff Brown053b8652012-06-06 16:25:03 -0700109
Christopher Ferris20303f82014-01-10 16:33:16 -0800110 dump_process_header(&log, pid);
111 dump_thread(&log, tid, true, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700112
Christopher Ferris20303f82014-01-10 16:33:16 -0800113 char task_path[64];
114 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
115 DIR* d = opendir(task_path);
116 if (d != NULL) {
117 struct dirent* de = NULL;
118 while ((de = readdir(d)) != NULL) {
119 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
120 continue;
121 }
Jeff Brown053b8652012-06-06 16:25:03 -0700122
Christopher Ferris20303f82014-01-10 16:33:16 -0800123 char* end;
124 pid_t new_tid = strtoul(de->d_name, &end, 10);
125 if (*end || new_tid == tid) {
126 continue;
127 }
Jeff Brown053b8652012-06-06 16:25:03 -0700128
Christopher Ferris20303f82014-01-10 16:33:16 -0800129 dump_thread(&log, new_tid, false, detach_failed, total_sleep_time_usec);
Jeff Brown053b8652012-06-06 16:25:03 -0700130 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800131 closedir(d);
132 }
Jeff Brown053b8652012-06-06 16:25:03 -0700133
Christopher Ferris20303f82014-01-10 16:33:16 -0800134 dump_process_footer(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700135}
136
Brigid Smith62ba4892014-06-10 11:53:08 -0700137void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800138 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700139 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800140 }
Jeff Brown053b8652012-06-06 16:25:03 -0700141}