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 <dirent.h> |
Christopher Ferris | b05c472 | 2020-09-23 15:51:46 -0700 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <inttypes.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 24 | #include <limits.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 25 | #include <stddef.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 29 | #include <sys/ptrace.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 30 | #include <sys/types.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 32 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 33 | #include <map> |
Christopher Ferris | 6e96403 | 2015-05-15 17:30:21 -0700 | [diff] [blame] | 34 | #include <memory> |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 35 | #include <string> |
Christopher Ferris | 6e96403 | 2015-05-15 17:30:21 -0700 | [diff] [blame] | 36 | |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 37 | #include <android-base/strings.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 38 | #include <android-base/unique_fd.h> |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 39 | #include <log/log.h> |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 40 | #include <unwindstack/AndroidUnwinder.h> |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 41 | #include <unwindstack/Unwinder.h> |
Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 42 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 43 | #include "libdebuggerd/types.h" |
Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 44 | #include "libdebuggerd/utility.h" |
Elliott Hughes | a660cb3 | 2020-07-23 15:26:10 -0700 | [diff] [blame] | 45 | #include "util.h" |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 46 | |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 47 | static void dump_process_header(log_t* log, pid_t pid, |
| 48 | const std::vector<std::string>& command_line) { |
Elliott Hughes | a660cb3 | 2020-07-23 15:26:10 -0700 | [diff] [blame] | 49 | _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, get_timestamp().c_str()); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 50 | |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 51 | if (!command_line.empty()) { |
| 52 | _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", android::base::Join(command_line, " ").c_str()); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 53 | } |
Jeff Brown | 9b12d53 | 2014-09-10 15:47:49 -0700 | [diff] [blame] | 54 | _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static void dump_process_footer(log_t* log, pid_t pid) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 58 | _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 61 | void dump_backtrace_thread(int output_fd, unwindstack::AndroidUnwinder* unwinder, |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 62 | const ThreadInfo& thread) { |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 63 | log_t log; |
| 64 | log.tfd = output_fd; |
| 65 | log.amfd_data = nullptr; |
| 66 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 67 | _LOG(&log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", thread.thread_name.c_str(), thread.tid); |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 68 | |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 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()); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 73 | return; |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 74 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 75 | |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 76 | log_backtrace(&log, unwinder, data, " "); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 79 | void dump_backtrace(android::base::unique_fd output_fd, unwindstack::AndroidUnwinder* unwinder, |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 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 | |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 91 | dump_process_header(&log, target->second.pid, target->second.command_line); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 92 | |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 93 | dump_backtrace_thread(output_fd.get(), unwinder, target->second); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 94 | for (const auto& [tid, info] : thread_info) { |
| 95 | if (tid != target_thread) { |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 96 | dump_backtrace_thread(output_fd.get(), unwinder, info); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | dump_process_footer(&log, target->second.pid); |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void dump_backtrace_header(int output_fd) { |
| 104 | log_t log; |
| 105 | log.tfd = output_fd; |
| 106 | log.amfd_data = nullptr; |
| 107 | |
Elliott Hughes | a660cb3 | 2020-07-23 15:26:10 -0700 | [diff] [blame] | 108 | pid_t pid = getpid(); |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 109 | dump_process_header(&log, pid, get_command_line(pid)); |
Josh Gao | e1aa0ca | 2017-03-01 17:23:22 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 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 | } |