blob: 753ebcb7c51017dd1b8215f8751b23230a5b8909 [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 <errno.h>
22#include <dirent.h>
23#include <limits.h>
Jeff Brown053b8652012-06-06 16:25:03 -070024#include <stddef.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070025#include <stdio.h>
Jeff Brown053b8652012-06-06 16:25:03 -070026#include <stdlib.h>
27#include <string.h>
Jeff Brown053b8652012-06-06 16:25:03 -070028#include <sys/ptrace.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070029#include <sys/types.h>
30#include <time.h>
31#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 Gao2b2ae0c2017-08-21 14:31:17 -070037#include <android-base/unique_fd.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080038#include <log/log.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080039#include <unwindstack/Unwinder.h>
Christopher Ferris30c942c2015-05-14 15:39:52 -070040
Josh Gao2b2ae0c2017-08-21 14:31:17 -070041#include "libdebuggerd/types.h"
Josh Gaoc3706662017-08-29 13:08:32 -070042#include "libdebuggerd/utility.h"
Jeff Brown053b8652012-06-06 16:25:03 -070043
Josh Gao57f58f82017-03-15 23:23:22 -070044static void dump_process_header(log_t* log, pid_t pid, const char* process_name) {
Christopher Ferris20303f82014-01-10 16:33:16 -080045 time_t t = time(NULL);
46 struct tm tm;
47 localtime_r(&t, &tm);
48 char timestr[64];
49 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Jeff Brown9b12d532014-09-10 15:47:49 -070050 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
Jeff Brown053b8652012-06-06 16:25:03 -070051
Josh Gao57f58f82017-03-15 23:23:22 -070052 if (process_name) {
53 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", process_name);
Christopher Ferris20303f82014-01-10 16:33:16 -080054 }
Jeff Brown9b12d532014-09-10 15:47:49 -070055 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070056}
57
58static void dump_process_footer(log_t* log, pid_t pid) {
Brigid Smith62ba4892014-06-10 11:53:08 -070059 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070060}
61
Christopher Ferris60eb1972019-01-15 15:18:43 -080062void dump_backtrace_thread(int output_fd, unwindstack::Unwinder* unwinder,
63 const ThreadInfo& thread) {
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080064 log_t log;
65 log.tfd = output_fd;
66 log.amfd_data = nullptr;
67
Josh Gao2b2ae0c2017-08-21 14:31:17 -070068 _LOG(&log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", thread.thread_name.c_str(), thread.tid);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080069
Christopher Ferris60eb1972019-01-15 15:18:43 -080070 unwinder->SetRegs(thread.registers.get());
71 unwinder->Unwind();
72 if (unwinder->NumFrames() == 0) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -070073 _LOG(&log, logtype::THREAD, "Unwind failed: tid = %d", thread.tid);
74 return;
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080075 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -070076
Christopher Ferris60eb1972019-01-15 15:18:43 -080077 for (size_t i = 0; i < unwinder->NumFrames(); i++) {
78 _LOG(&log, logtype::BACKTRACE, " %s\n", unwinder->FormatFrame(i).c_str());
Josh Gao2b2ae0c2017-08-21 14:31:17 -070079 }
80}
81
Christopher Ferris60eb1972019-01-15 15:18:43 -080082void dump_backtrace(android::base::unique_fd output_fd, unwindstack::Unwinder* unwinder,
Josh Gao2b2ae0c2017-08-21 14:31:17 -070083 const std::map<pid_t, ThreadInfo>& thread_info, pid_t target_thread) {
84 log_t log;
85 log.tfd = output_fd.get();
86 log.amfd_data = nullptr;
87
88 auto target = thread_info.find(target_thread);
89 if (target == thread_info.end()) {
90 ALOGE("failed to find target thread in thread info");
91 return;
92 }
93
94 dump_process_header(&log, target->second.pid, target->second.process_name.c_str());
95
Christopher Ferris60eb1972019-01-15 15:18:43 -080096 dump_backtrace_thread(output_fd.get(), unwinder, target->second);
Josh Gao2b2ae0c2017-08-21 14:31:17 -070097 for (const auto& [tid, info] : thread_info) {
98 if (tid != target_thread) {
Christopher Ferris60eb1972019-01-15 15:18:43 -080099 dump_backtrace_thread(output_fd.get(), unwinder, info);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700100 }
101 }
102
103 dump_process_footer(&log, target->second.pid);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800104}
105
106void dump_backtrace_header(int output_fd) {
107 log_t log;
108 log.tfd = output_fd;
109 log.amfd_data = nullptr;
110
Josh Gao57f58f82017-03-15 23:23:22 -0700111 char process_name[128];
112 read_with_default("/proc/self/cmdline", process_name, sizeof(process_name), "<unknown>");
113 dump_process_header(&log, getpid(), process_name);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800114}
115
116void dump_backtrace_footer(int output_fd) {
117 log_t log;
118 log.tfd = output_fd;
119 log.amfd_data = nullptr;
120
121 dump_process_footer(&log, getpid());
122}