1 /*
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 #define LOG_TAG "DEBUG"
18
19 #include <errno.h>
20 #include <dirent.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/ptrace.h>
27 #include <sys/types.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #include <memory>
32 #include <string>
33
34 #include <backtrace/Backtrace.h>
35 #include <log/log.h>
36
37 #include "backtrace.h"
38
39 #include "utility.h"
40
dump_process_header(log_t * log,pid_t pid,const char * process_name)41 static void dump_process_header(log_t* log, pid_t pid, const char* process_name) {
42 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);
47 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
48
49 if (process_name) {
50 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", process_name);
51 }
52 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
53 }
54
dump_process_footer(log_t * log,pid_t pid)55 static void dump_process_footer(log_t* log, pid_t pid) {
56 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
57 }
58
log_thread_name(log_t * log,pid_t tid,const char * thread_name)59 static 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);
61 }
62
dump_thread(log_t * log,BacktraceMap * map,pid_t pid,pid_t tid,const std::string & thread_name)63 static 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());
66
67 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map));
68 if (backtrace->Unwind(0)) {
69 dump_backtrace_to_log(backtrace.get(), log, " ");
70 } else {
71 ALOGE("Unwind failed: tid = %d: %s", tid,
72 backtrace->GetErrorString(backtrace->GetError()).c_str());
73 }
74 }
75
dump_backtrace(int fd,BacktraceMap * map,pid_t pid,pid_t tid,const std::string & process_name,const std::map<pid_t,std::string> & threads,std::string * amfd_data)76 void 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) {
78 log_t log;
79 log.tfd = fd;
80 log.amfd_data = amfd_data;
81
82 dump_process_header(&log, pid, process_name.c_str());
83 dump_thread(&log, map, pid, tid, threads.find(tid)->second.c_str());
84
85 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 }
91 }
92
93 dump_process_footer(&log, pid);
94 }
95
dump_backtrace_ucontext(int output_fd,ucontext_t * ucontext)96 void 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
104 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);
107
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
dump_backtrace_header(int output_fd)117 void dump_backtrace_header(int output_fd) {
118 log_t log;
119 log.tfd = output_fd;
120 log.amfd_data = nullptr;
121
122 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);
125 }
126
dump_backtrace_footer(int output_fd)127 void 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
dump_backtrace_to_log(Backtrace * backtrace,log_t * log,const char * prefix)135 void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
136 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
137 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
138 }
139 }
140