| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 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 |  | 
| Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "DEBUG" | 
|  | 18 |  | 
| Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 19 | #include "libdebuggerd/backtrace.h" | 
|  | 20 |  | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 21 | #include <errno.h> | 
|  | 22 | #include <dirent.h> | 
|  | 23 | #include <limits.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 24 | #include <stddef.h> | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 25 | #include <stdio.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 26 | #include <stdlib.h> | 
|  | 27 | #include <string.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 28 | #include <sys/ptrace.h> | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 29 | #include <sys/types.h> | 
|  | 30 | #include <time.h> | 
|  | 31 | #include <unistd.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 32 |  | 
| Christopher Ferris | 6e96403 | 2015-05-15 17:30:21 -0700 | [diff] [blame] | 33 | #include <memory> | 
| Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 34 | #include <string> | 
| Christopher Ferris | 6e96403 | 2015-05-15 17:30:21 -0700 | [diff] [blame] | 35 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 36 | #include <backtrace/Backtrace.h> | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 37 | #include <log/log.h> | 
| Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 38 |  | 
| Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 39 | #include "libdebuggerd/utility.h" | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 40 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 41 | static void dump_process_header(log_t* log, pid_t pid, const char* process_name) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 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); | 
| Jeff Brown | 9b12d53 | 2014-09-10 15:47:49 -0700 | [diff] [blame] | 47 | _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 48 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 49 | if (process_name) { | 
|  | 50 | _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", process_name); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 51 | } | 
| Jeff Brown | 9b12d53 | 2014-09-10 15:47:49 -0700 | [diff] [blame] | 52 | _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
|  | 55 | static void dump_process_footer(log_t* log, pid_t pid) { | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 56 | _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 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); | 
| Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 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()); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 66 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 67 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 68 | if (backtrace->Unwind(0)) { | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 69 | dump_backtrace_to_log(backtrace.get(), log, "  "); | 
| Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 70 | } else { | 
| Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 71 | ALOGE("Unwind failed: tid = %d: %s", tid, | 
|  | 72 | backtrace->GetErrorString(backtrace->GetError()).c_str()); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 73 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 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) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 78 | log_t log; | 
|  | 79 | log.tfd = fd; | 
| Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 80 | log.amfd_data = amfd_data; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 81 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 82 | dump_process_header(&log, pid, process_name.c_str()); | 
|  | 83 | dump_thread(&log, map, pid, tid, threads.find(tid)->second.c_str()); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 84 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 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 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 91 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 92 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 93 | dump_process_footer(&log, pid); | 
| Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 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 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 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); | 
| Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 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 |  | 
|  | 117 | void dump_backtrace_header(int output_fd) { | 
|  | 118 | log_t log; | 
|  | 119 | log.tfd = output_fd; | 
|  | 120 | log.amfd_data = nullptr; | 
|  | 121 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 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); | 
| Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 125 | } | 
|  | 126 |  | 
|  | 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 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 135 | void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 136 | for (size_t i = 0; i < backtrace->NumFrames(); i++) { | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 137 | _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str()); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 138 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 139 | } |