blob: df49aef37e38b591844faff3fec44bfd7dec73b1 [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
Mark Salyzynff2dcd92016-09-28 15:54:45 -070019#include <errno.h>
20#include <dirent.h>
21#include <limits.h>
Jeff Brown053b8652012-06-06 16:25:03 -070022#include <stddef.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070023#include <stdio.h>
Jeff Brown053b8652012-06-06 16:25:03 -070024#include <stdlib.h>
25#include <string.h>
Jeff Brown053b8652012-06-06 16:25:03 -070026#include <sys/ptrace.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070027#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070030
Christopher Ferris6e964032015-05-15 17:30:21 -070031#include <memory>
Christopher Ferris9818bd22016-05-03 16:32:13 -070032#include <string>
Christopher Ferris6e964032015-05-15 17:30:21 -070033
Christopher Ferris20303f82014-01-10 16:33:16 -080034#include <backtrace/Backtrace.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080035#include <log/log.h>
Christopher Ferris30c942c2015-05-14 15:39:52 -070036
Christopher Ferris365e4ae2013-10-02 12:26:48 -070037#include "backtrace.h"
Brigid Smith62ba4892014-06-10 11:53:08 -070038
Jeff Brown053b8652012-06-06 16:25:03 -070039#include "utility.h"
40
Jeff Brown053b8652012-06-06 16:25:03 -070041static void dump_process_header(log_t* log, pid_t pid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080042 char path[PATH_MAX];
43 char procnamebuf[1024];
44 char* procname = NULL;
45 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070046
Christopher Ferris20303f82014-01-10 16:33:16 -080047 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
48 if ((fp = fopen(path, "r"))) {
49 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
50 fclose(fp);
51 }
Jeff Brown053b8652012-06-06 16:25:03 -070052
Christopher Ferris20303f82014-01-10 16:33:16 -080053 time_t t = time(NULL);
54 struct tm tm;
55 localtime_r(&t, &tm);
56 char timestr[64];
57 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Jeff Brown9b12d532014-09-10 15:47:49 -070058 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
Jeff Brown053b8652012-06-06 16:25:03 -070059
Christopher Ferris20303f82014-01-10 16:33:16 -080060 if (procname) {
Brigid Smith62ba4892014-06-10 11:53:08 -070061 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname);
Christopher Ferris20303f82014-01-10 16:33:16 -080062 }
Jeff Brown9b12d532014-09-10 15:47:49 -070063 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070064}
65
66static void dump_process_footer(log_t* log, pid_t pid) {
Brigid Smith62ba4892014-06-10 11:53:08 -070067 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070068}
69
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080070static void log_thread_name(log_t* log, pid_t tid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080071 FILE* fp;
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080072 char buf[1024];
73 char path[PATH_MAX];
74 char* threadname = NULL;
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"))) {
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080078 threadname = fgets(buf, sizeof(buf), fp);
Christopher Ferris20303f82014-01-10 16:33:16 -080079 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 }
Brigid Smith62ba4892014-06-10 11:53:08 -070087 _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080088}
89
90static void dump_thread(log_t* log, BacktraceMap* map, pid_t pid, pid_t tid) {
91 log_thread_name(log, tid);
Jeff Brown053b8652012-06-06 16:25:03 -070092
Josh Gao7c89f9e2016-01-13 17:57:14 -080093 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map));
Christopher Ferris20303f82014-01-10 16:33:16 -080094 if (backtrace->Unwind(0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -070095 dump_backtrace_to_log(backtrace.get(), log, " ");
Christopher Ferris30c942c2015-05-14 15:39:52 -070096 } else {
Christopher Ferrisc463ba42016-03-09 14:35:54 -080097 ALOGE("Unwind failed: tid = %d: %s", tid,
98 backtrace->GetErrorString(backtrace->GetError()).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -080099 }
Jeff Brown053b8652012-06-06 16:25:03 -0700100}
101
Christopher Ferris9818bd22016-05-03 16:32:13 -0700102void dump_backtrace(int fd, BacktraceMap* map, pid_t pid, pid_t tid,
103 const std::set<pid_t>& siblings, std::string* amfd_data) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 log_t log;
105 log.tfd = fd;
Christopher Ferris9818bd22016-05-03 16:32:13 -0700106 log.amfd_data = amfd_data;
Jeff Brown053b8652012-06-06 16:25:03 -0700107
Christopher Ferris20303f82014-01-10 16:33:16 -0800108 dump_process_header(&log, pid);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800109 dump_thread(&log, map, pid, tid);
Jeff Brown053b8652012-06-06 16:25:03 -0700110
Josh Gao7c89f9e2016-01-13 17:57:14 -0800111 for (pid_t sibling : siblings) {
112 dump_thread(&log, map, pid, sibling);
Christopher Ferris20303f82014-01-10 16:33:16 -0800113 }
Jeff Brown053b8652012-06-06 16:25:03 -0700114
Christopher Ferris20303f82014-01-10 16:33:16 -0800115 dump_process_footer(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700116}
117
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800118void dump_backtrace_ucontext(int output_fd, ucontext_t* ucontext) {
119 pid_t pid = getpid();
120 pid_t tid = gettid();
121
122 log_t log;
123 log.tfd = output_fd;
124 log.amfd_data = nullptr;
125
126 log_thread_name(&log, tid);
127
128 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid));
129 if (backtrace->Unwind(0, ucontext)) {
130 dump_backtrace_to_log(backtrace.get(), &log, " ");
131 } else {
132 ALOGE("Unwind failed: tid = %d: %s", tid,
133 backtrace->GetErrorString(backtrace->GetError()).c_str());
134 }
135}
136
137void dump_backtrace_header(int output_fd) {
138 log_t log;
139 log.tfd = output_fd;
140 log.amfd_data = nullptr;
141
142 dump_process_header(&log, getpid());
143}
144
145void dump_backtrace_footer(int output_fd) {
146 log_t log;
147 log.tfd = output_fd;
148 log.amfd_data = nullptr;
149
150 dump_process_footer(&log, getpid());
151}
152
Brigid Smith62ba4892014-06-10 11:53:08 -0700153void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800154 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700155 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800156 }
Jeff Brown053b8652012-06-06 16:25:03 -0700157}