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 "libdebuggerd/backtrace.h"
20 
21 #include <dirent.h>
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <limits.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/ptrace.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 
33 #include <map>
34 #include <memory>
35 #include <string>
36 
37 #include <android-base/strings.h>
38 #include <android-base/unique_fd.h>
39 #include <log/log.h>
40 #include <unwindstack/AndroidUnwinder.h>
41 #include <unwindstack/Unwinder.h>
42 
43 #include "libdebuggerd/types.h"
44 #include "libdebuggerd/utility.h"
45 #include "util.h"
46 
dump_process_header(log_t * log,pid_t pid,const std::vector<std::string> & command_line)47 static void dump_process_header(log_t* log, pid_t pid,
48                                 const std::vector<std::string>& command_line) {
49   _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, get_timestamp().c_str());
50 
51   if (!command_line.empty()) {
52     _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", android::base::Join(command_line, " ").c_str());
53   }
54   _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
55 }
56 
dump_process_footer(log_t * log,pid_t pid)57 static void dump_process_footer(log_t* log, pid_t pid) {
58   _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
59 }
60 
dump_backtrace_thread(int output_fd,unwindstack::AndroidUnwinder * unwinder,const ThreadInfo & thread)61 void dump_backtrace_thread(int output_fd, unwindstack::AndroidUnwinder* unwinder,
62                            const ThreadInfo& thread) {
63   log_t log;
64   log.tfd = output_fd;
65   log.amfd_data = nullptr;
66 
67   _LOG(&log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", thread.thread_name.c_str(), thread.tid);
68 
69   unwindstack::AndroidUnwinderData data;
70   if (!unwinder->Unwind(thread.registers.get(), data)) {
71     _LOG(&log, logtype::THREAD, "Unwind failed: tid = %d: Error %s\n", thread.tid,
72          data.GetErrorString().c_str());
73     return;
74   }
75 
76   log_backtrace(&log, unwinder, data, "  ");
77 }
78 
dump_backtrace(android::base::unique_fd output_fd,unwindstack::AndroidUnwinder * unwinder,const std::map<pid_t,ThreadInfo> & thread_info,pid_t target_thread)79 void dump_backtrace(android::base::unique_fd output_fd, unwindstack::AndroidUnwinder* unwinder,
80                     const std::map<pid_t, ThreadInfo>& thread_info, pid_t target_thread) {
81   log_t log;
82   log.tfd = output_fd.get();
83   log.amfd_data = nullptr;
84 
85   auto target = thread_info.find(target_thread);
86   if (target == thread_info.end()) {
87     ALOGE("failed to find target thread in thread info");
88     return;
89   }
90 
91   dump_process_header(&log, target->second.pid, target->second.command_line);
92 
93   dump_backtrace_thread(output_fd.get(), unwinder, target->second);
94   for (const auto& [tid, info] : thread_info) {
95     if (tid != target_thread) {
96       dump_backtrace_thread(output_fd.get(), unwinder, info);
97     }
98   }
99 
100   dump_process_footer(&log, target->second.pid);
101 }
102 
dump_backtrace_header(int output_fd)103 void dump_backtrace_header(int output_fd) {
104   log_t log;
105   log.tfd = output_fd;
106   log.amfd_data = nullptr;
107 
108   pid_t pid = getpid();
109   dump_process_header(&log, pid, get_command_line(pid));
110 }
111 
dump_backtrace_footer(int output_fd)112 void dump_backtrace_footer(int output_fd) {
113   log_t log;
114   log.tfd = output_fd;
115   log.amfd_data = nullptr;
116 
117   dump_process_footer(&log, getpid());
118 }
119