blob: f616e1ba0c46325f5c46054253bf394b7860b2a0 [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
Josh Gaoc3706662017-08-29 13:08:32 -070019#include "libdebuggerd/backtrace.h"
20
Mark Salyzynff2dcd92016-09-28 15:54:45 -070021#include <errno.h>
22#include <dirent.h>
23#include <limits.h>
Jeff Brown053b8652012-06-06 16:25:03 -070024#include <stddef.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070025#include <stdio.h>
Jeff Brown053b8652012-06-06 16:25:03 -070026#include <stdlib.h>
27#include <string.h>
Jeff Brown053b8652012-06-06 16:25:03 -070028#include <sys/ptrace.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070029#include <sys/types.h>
30#include <time.h>
31#include <unistd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070032
Christopher Ferris6e964032015-05-15 17:30:21 -070033#include <memory>
Christopher Ferris9818bd22016-05-03 16:32:13 -070034#include <string>
Christopher Ferris6e964032015-05-15 17:30:21 -070035
Christopher Ferris20303f82014-01-10 16:33:16 -080036#include <backtrace/Backtrace.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080037#include <log/log.h>
Christopher Ferris30c942c2015-05-14 15:39:52 -070038
Josh Gaoc3706662017-08-29 13:08:32 -070039#include "libdebuggerd/utility.h"
Jeff Brown053b8652012-06-06 16:25:03 -070040
Josh Gao57f58f82017-03-15 23:23:22 -070041static void dump_process_header(log_t* log, pid_t pid, const char* process_name) {
Christopher Ferris20303f82014-01-10 16:33:16 -080042 time_t t = time(NULL);
43 struct tm tm;
44 localtime_r(&t, &tm);
45 char timestr[64];
46 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Jeff Brown9b12d532014-09-10 15:47:49 -070047 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
Jeff Brown053b8652012-06-06 16:25:03 -070048
Josh Gao57f58f82017-03-15 23:23:22 -070049 if (process_name) {
50 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", process_name);
Christopher Ferris20303f82014-01-10 16:33:16 -080051 }
Jeff Brown9b12d532014-09-10 15:47:49 -070052 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070053}
54
55static void dump_process_footer(log_t* log, pid_t pid) {
Brigid Smith62ba4892014-06-10 11:53:08 -070056 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070057}
58
Josh Gao57f58f82017-03-15 23:23:22 -070059static void log_thread_name(log_t* log, pid_t tid, const char* thread_name) {
60 _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", thread_name, tid);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080061}
62
Josh Gao57f58f82017-03-15 23:23:22 -070063static void dump_thread(log_t* log, BacktraceMap* map, pid_t pid, pid_t tid,
64 const std::string& thread_name) {
65 log_thread_name(log, tid, thread_name.c_str());
Jeff Brown053b8652012-06-06 16:25:03 -070066
Josh Gao7c89f9e2016-01-13 17:57:14 -080067 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map));
Christopher Ferris20303f82014-01-10 16:33:16 -080068 if (backtrace->Unwind(0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -070069 dump_backtrace_to_log(backtrace.get(), log, " ");
Christopher Ferris30c942c2015-05-14 15:39:52 -070070 } else {
Christopher Ferrisc463ba42016-03-09 14:35:54 -080071 ALOGE("Unwind failed: tid = %d: %s", tid,
72 backtrace->GetErrorString(backtrace->GetError()).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -080073 }
Jeff Brown053b8652012-06-06 16:25:03 -070074}
75
Josh Gao57f58f82017-03-15 23:23:22 -070076void dump_backtrace(int fd, BacktraceMap* map, pid_t pid, pid_t tid, const std::string& process_name,
77 const std::map<pid_t, std::string>& threads, std::string* amfd_data) {
Christopher Ferris20303f82014-01-10 16:33:16 -080078 log_t log;
79 log.tfd = fd;
Christopher Ferris9818bd22016-05-03 16:32:13 -070080 log.amfd_data = amfd_data;
Jeff Brown053b8652012-06-06 16:25:03 -070081
Josh Gao57f58f82017-03-15 23:23:22 -070082 dump_process_header(&log, pid, process_name.c_str());
83 dump_thread(&log, map, pid, tid, threads.find(tid)->second.c_str());
Jeff Brown053b8652012-06-06 16:25:03 -070084
Josh Gao57f58f82017-03-15 23:23:22 -070085 for (const auto& it : threads) {
86 pid_t thread_tid = it.first;
87 const std::string& thread_name = it.second;
88 if (thread_tid != tid) {
89 dump_thread(&log, map, pid, thread_tid, thread_name.c_str());
90 }
Christopher Ferris20303f82014-01-10 16:33:16 -080091 }
Jeff Brown053b8652012-06-06 16:25:03 -070092
Christopher Ferris20303f82014-01-10 16:33:16 -080093 dump_process_footer(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -070094}
95
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080096void dump_backtrace_ucontext(int output_fd, ucontext_t* ucontext) {
97 pid_t pid = getpid();
98 pid_t tid = gettid();
99
100 log_t log;
101 log.tfd = output_fd;
102 log.amfd_data = nullptr;
103
Josh Gao57f58f82017-03-15 23:23:22 -0700104 char thread_name[16];
105 read_with_default("/proc/self/comm", thread_name, sizeof(thread_name), "<unknown>");
106 log_thread_name(&log, tid, thread_name);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800107
108 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid));
109 if (backtrace->Unwind(0, ucontext)) {
110 dump_backtrace_to_log(backtrace.get(), &log, " ");
111 } else {
112 ALOGE("Unwind failed: tid = %d: %s", tid,
113 backtrace->GetErrorString(backtrace->GetError()).c_str());
114 }
115}
116
117void dump_backtrace_header(int output_fd) {
118 log_t log;
119 log.tfd = output_fd;
120 log.amfd_data = nullptr;
121
Josh Gao57f58f82017-03-15 23:23:22 -0700122 char process_name[128];
123 read_with_default("/proc/self/cmdline", process_name, sizeof(process_name), "<unknown>");
124 dump_process_header(&log, getpid(), process_name);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800125}
126
127void dump_backtrace_footer(int output_fd) {
128 log_t log;
129 log.tfd = output_fd;
130 log.amfd_data = nullptr;
131
132 dump_process_footer(&log, getpid());
133}
134
Brigid Smith62ba4892014-06-10 11:53:08 -0700135void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800136 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700137 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800138 }
Jeff Brown053b8652012-06-06 16:25:03 -0700139}