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 <stddef.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <time.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <dirent.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/ptrace.h>
30
31 #include <memory>
32 #include <string>
33
34 #include <backtrace/Backtrace.h>
35
36 #include <log/log.h>
37
38 #include "backtrace.h"
39
40 #include "utility.h"
41
dump_process_header(log_t * log,pid_t pid)42 static void dump_process_header(log_t* log, pid_t pid) {
43 char path[PATH_MAX];
44 char procnamebuf[1024];
45 char* procname = NULL;
46 FILE* fp;
47
48 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
49 if ((fp = fopen(path, "r"))) {
50 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
51 fclose(fp);
52 }
53
54 time_t t = time(NULL);
55 struct tm tm;
56 localtime_r(&t, &tm);
57 char timestr[64];
58 strftime(timestr, sizeof(timestr), "%F %T", &tm);
59 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
60
61 if (procname) {
62 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname);
63 }
64 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
65 }
66
dump_process_footer(log_t * log,pid_t pid)67 static void dump_process_footer(log_t* log, pid_t pid) {
68 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
69 }
70
dump_thread(log_t * log,BacktraceMap * map,pid_t pid,pid_t tid)71 static void dump_thread(log_t* log, BacktraceMap* map, pid_t pid, pid_t tid) {
72 char path[PATH_MAX];
73 char threadnamebuf[1024];
74 char* threadname = NULL;
75 FILE* fp;
76
77 snprintf(path, sizeof(path), "/proc/%d/comm", tid);
78 if ((fp = fopen(path, "r"))) {
79 threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
80 fclose(fp);
81 if (threadname) {
82 size_t len = strlen(threadname);
83 if (len && threadname[len - 1] == '\n') {
84 threadname[len - 1] = '\0';
85 }
86 }
87 }
88
89 _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
90
91 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map));
92 if (backtrace->Unwind(0)) {
93 dump_backtrace_to_log(backtrace.get(), log, " ");
94 } else {
95 ALOGE("Unwind failed: tid = %d: %s", tid,
96 backtrace->GetErrorString(backtrace->GetError()).c_str());
97 }
98 }
99
dump_backtrace(int fd,BacktraceMap * map,pid_t pid,pid_t tid,const std::set<pid_t> & siblings,std::string * amfd_data)100 void dump_backtrace(int fd, BacktraceMap* map, pid_t pid, pid_t tid,
101 const std::set<pid_t>& siblings, std::string* amfd_data) {
102 log_t log;
103 log.tfd = fd;
104 log.amfd_data = amfd_data;
105
106 dump_process_header(&log, pid);
107 dump_thread(&log, map, pid, tid);
108
109 for (pid_t sibling : siblings) {
110 dump_thread(&log, map, pid, sibling);
111 }
112
113 dump_process_footer(&log, pid);
114 }
115
dump_backtrace_to_log(Backtrace * backtrace,log_t * log,const char * prefix)116 void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
117 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
118 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
119 }
120 }
121