blob: 3ff9710130af07396d78570caf0a334effbcc95f [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
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 Ferris30c942c2015-05-14 15:39:52 -070017#define LOG_TAG "DEBUG"
18
Josh Gaoc3706662017-08-29 13:08:32 -070019#include "libdebuggerd/backtrace.h"
20
Mark Salyzynff2dcd92016-09-28 15:54:45 -070021#include <dirent.h>
Christopher Ferrisb05c4722020-09-23 15:51:46 -070022#include <errno.h>
23#include <inttypes.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070024#include <limits.h>
Jeff Brown053b8652012-06-06 16:25:03 -070025#include <stddef.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070026#include <stdio.h>
Jeff Brown053b8652012-06-06 16:25:03 -070027#include <stdlib.h>
28#include <string.h>
Jeff Brown053b8652012-06-06 16:25:03 -070029#include <sys/ptrace.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070030#include <sys/types.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070031#include <unistd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070032
Josh Gao2b2ae0c2017-08-21 14:31:17 -070033#include <map>
Christopher Ferris6e964032015-05-15 17:30:21 -070034#include <memory>
Christopher Ferris9818bd22016-05-03 16:32:13 -070035#include <string>
Christopher Ferris6e964032015-05-15 17:30:21 -070036
Josh Gao31348a72021-03-29 21:53:42 -070037#include <android-base/strings.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070038#include <android-base/unique_fd.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080039#include <log/log.h>
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070040#include <unwindstack/AndroidUnwinder.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080041#include <unwindstack/Unwinder.h>
Christopher Ferris30c942c2015-05-14 15:39:52 -070042
Josh Gao2b2ae0c2017-08-21 14:31:17 -070043#include "libdebuggerd/types.h"
Josh Gaoc3706662017-08-29 13:08:32 -070044#include "libdebuggerd/utility.h"
Elliott Hughesa660cb32020-07-23 15:26:10 -070045#include "util.h"
Jeff Brown053b8652012-06-06 16:25:03 -070046
Josh Gao31348a72021-03-29 21:53:42 -070047static void dump_process_header(log_t* log, pid_t pid,
48 const std::vector<std::string>& command_line) {
Elliott Hughesa660cb32020-07-23 15:26:10 -070049 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, get_timestamp().c_str());
Jeff Brown053b8652012-06-06 16:25:03 -070050
Josh Gao31348a72021-03-29 21:53:42 -070051 if (!command_line.empty()) {
52 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", android::base::Join(command_line, " ").c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -080053 }
Jeff Brown9b12d532014-09-10 15:47:49 -070054 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070055}
56
57static void dump_process_footer(log_t* log, pid_t pid) {
Brigid Smith62ba4892014-06-10 11:53:08 -070058 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070059}
60
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070061void dump_backtrace_thread(int output_fd, unwindstack::AndroidUnwinder* unwinder,
Christopher Ferris60eb1972019-01-15 15:18:43 -080062 const ThreadInfo& thread) {
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080063 log_t log;
64 log.tfd = output_fd;
65 log.amfd_data = nullptr;
66
Josh Gao2b2ae0c2017-08-21 14:31:17 -070067 _LOG(&log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", thread.thread_name.c_str(), thread.tid);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080068
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070069 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 Gao2b2ae0c2017-08-21 14:31:17 -070073 return;
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080074 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -070075
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070076 log_backtrace(&log, unwinder, data, " ");
Josh Gao2b2ae0c2017-08-21 14:31:17 -070077}
78
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070079void dump_backtrace(android::base::unique_fd output_fd, unwindstack::AndroidUnwinder* unwinder,
Josh Gao2b2ae0c2017-08-21 14:31:17 -070080 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 Gao31348a72021-03-29 21:53:42 -070091 dump_process_header(&log, target->second.pid, target->second.command_line);
Josh Gao2b2ae0c2017-08-21 14:31:17 -070092
Christopher Ferris60eb1972019-01-15 15:18:43 -080093 dump_backtrace_thread(output_fd.get(), unwinder, target->second);
Josh Gao2b2ae0c2017-08-21 14:31:17 -070094 for (const auto& [tid, info] : thread_info) {
95 if (tid != target_thread) {
Christopher Ferris60eb1972019-01-15 15:18:43 -080096 dump_backtrace_thread(output_fd.get(), unwinder, info);
Josh Gao2b2ae0c2017-08-21 14:31:17 -070097 }
98 }
99
100 dump_process_footer(&log, target->second.pid);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800101}
102
103void dump_backtrace_header(int output_fd) {
104 log_t log;
105 log.tfd = output_fd;
106 log.amfd_data = nullptr;
107
Elliott Hughesa660cb32020-07-23 15:26:10 -0700108 pid_t pid = getpid();
Josh Gao31348a72021-03-29 21:53:42 -0700109 dump_process_header(&log, pid, get_command_line(pid));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800110}
111
112void 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}