blob: f21a203786a9420db2c2818fe480fbc876b5da23 [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
Mark Salyzynfca0bd12013-12-12 12:21:20 -08002 * Copyright (C) 2012-2014 The Android Open Source Project
Jeff Brown053b8652012-06-06 16:25:03 -07003 *
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
Brigid Smith62ba4892014-06-10 11:53:08 -070017#define LOG_TAG "DEBUG"
18
Josh Gaoc3706662017-08-29 13:08:32 -070019#include "libdebuggerd/tombstone.h"
20
Kévin PETIT4bb47722013-12-18 16:44:24 +000021#include <errno.h>
Kévin PETIT4bb47722013-12-18 16:44:24 +000022#include <signal.h>
23#include <stddef.h>
24#include <stdio.h>
25#include <stdlib.h>
Christopher Ferrisbdea3bb2021-11-17 01:09:22 +000026#include <sys/types.h>
27#include <unistd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070028
Christopher Ferris6e964032015-05-15 17:30:21 -070029#include <memory>
30#include <string>
31
Josh Gao57f58f82017-03-15 23:23:22 -070032#include <android-base/file.h>
Josh Gao57f58f82017-03-15 23:23:22 -070033#include <android-base/unique_fd.h>
34#include <android/log.h>
Josh Gao618cea32021-01-26 17:45:43 -080035#include <async_safe/log.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070036#include <log/log.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070037#include <private/android_filesystem_config.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070038#include <unwindstack/Memory.h>
39#include <unwindstack/Regs.h>
Christopher Ferris60eb1972019-01-15 15:18:43 -080040#include <unwindstack/Unwinder.h>
Jeff Brown053b8652012-06-06 16:25:03 -070041
Josh Gaoc3706662017-08-29 13:08:32 -070042#include "libdebuggerd/backtrace.h"
Josh Gaoc3706662017-08-29 13:08:32 -070043#include "libdebuggerd/open_files_list.h"
Josh Gao2b2ae0c2017-08-21 14:31:17 -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 Gao76e1e302021-01-26 15:53:11 -080047#include "tombstone.pb.h"
48
Josh Gao2b2ae0c2017-08-21 14:31:17 -070049using android::base::unique_fd;
50
Elliott Hughese1415a52018-02-15 09:18:21 -080051using namespace std::literals::string_literals;
52
Josh Gao76e1e302021-01-26 15:53:11 -080053void engrave_tombstone_ucontext(int tombstone_fd, int proto_fd, uint64_t abort_msg_address,
54 siginfo_t* siginfo, ucontext_t* ucontext) {
Misha Wagner39c5b8c2019-04-18 16:07:33 +010055 pid_t uid = getuid();
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080056 pid_t pid = getpid();
57 pid_t tid = gettid();
58
Josh Gaoe73c9322017-02-08 16:06:26 -080059 log_t log;
60 log.current_tid = tid;
61 log.crashed_tid = tid;
62 log.tfd = tombstone_fd;
63 log.amfd_data = nullptr;
64
Elliott Hughesa660cb32020-07-23 15:26:10 -070065 std::string thread_name = get_thread_name(tid);
Josh Gao31348a72021-03-29 21:53:42 -070066 std::vector<std::string> command_line = get_command_line(pid);
Josh Gao57f58f82017-03-15 23:23:22 -070067
Christopher Ferris60eb1972019-01-15 15:18:43 -080068 std::unique_ptr<unwindstack::Regs> regs(
69 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Josh Gaoe73c9322017-02-08 16:06:26 -080070
Josh Gao76e1e302021-01-26 15:53:11 -080071 std::string selinux_label;
72 android::base::ReadFileToString("/proc/self/attr/current", &selinux_label);
73
Josh Gao2b2ae0c2017-08-21 14:31:17 -070074 std::map<pid_t, ThreadInfo> threads;
Elliott Hughesa660cb32020-07-23 15:26:10 -070075 threads[tid] = ThreadInfo{
Josh Gao2b2ae0c2017-08-21 14:31:17 -070076 .registers = std::move(regs),
Misha Wagner39c5b8c2019-04-18 16:07:33 +010077 .uid = uid,
Josh Gao2b2ae0c2017-08-21 14:31:17 -070078 .tid = tid,
Josh Gao76e1e302021-01-26 15:53:11 -080079 .thread_name = std::move(thread_name),
Josh Gao2b2ae0c2017-08-21 14:31:17 -070080 .pid = pid,
Josh Gao31348a72021-03-29 21:53:42 -070081 .command_line = std::move(command_line),
Josh Gao76e1e302021-01-26 15:53:11 -080082 .selinux_label = std::move(selinux_label),
Josh Gao2b2ae0c2017-08-21 14:31:17 -070083 .siginfo = siginfo,
84 };
Josh Gao77b00ed2017-05-05 18:11:23 -070085
Christopher Ferrisb05c4722020-09-23 15:51:46 -070086 unwindstack::UnwinderFromPid unwinder(kMaxFrames, pid, unwindstack::Regs::CurrentArch());
yidong zhangcbf7c462021-06-24 17:51:56 +080087 auto process_memory =
88 unwindstack::Memory::CreateProcessMemoryCached(getpid());
89 unwinder.SetProcessMemory(process_memory);
Christopher Ferrisb05c4722020-09-23 15:51:46 -070090 if (!unwinder.Init()) {
Josh Gao618cea32021-01-26 17:45:43 -080091 async_safe_fatal("failed to init unwinder object");
Josh Gaoe73c9322017-02-08 16:06:26 -080092 }
Josh Gaofdc95c92017-09-13 15:33:39 -070093
Peter Collingbourne843f7e62020-02-28 19:07:33 -080094 ProcessInfo process_info;
95 process_info.abort_msg_address = abort_msg_address;
Josh Gao76e1e302021-01-26 15:53:11 -080096 engrave_tombstone(unique_fd(dup(tombstone_fd)), unique_fd(dup(proto_fd)), &unwinder, threads, tid,
97 process_info, nullptr, nullptr);
Josh Gao2b2ae0c2017-08-21 14:31:17 -070098}
99
Josh Gao76e1e302021-01-26 15:53:11 -0800100void engrave_tombstone(unique_fd output_fd, unique_fd proto_fd, unwindstack::Unwinder* unwinder,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700101 const std::map<pid_t, ThreadInfo>& threads, pid_t target_thread,
Peter Collingbourne843f7e62020-02-28 19:07:33 -0800102 const ProcessInfo& process_info, OpenFilesList* open_files,
103 std::string* amfd_data) {
Elliott Hughesa660cb32020-07-23 15:26:10 -0700104 // Don't copy log messages to tombstone unless this is a development device.
Josh Gao76e1e302021-01-26 15:53:11 -0800105 Tombstone tombstone;
106 engrave_tombstone_proto(&tombstone, unwinder, threads, target_thread, process_info, open_files);
107
Josh Gao618cea32021-01-26 17:45:43 -0800108 if (proto_fd != -1) {
109 if (!tombstone.SerializeToFileDescriptor(proto_fd.get())) {
110 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to write proto tombstone: %s",
111 strerror(errno));
112 }
Josh Gao76e1e302021-01-26 15:53:11 -0800113 }
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700114
115 log_t log;
116 log.current_tid = target_thread;
117 log.crashed_tid = target_thread;
118 log.tfd = output_fd.get();
119 log.amfd_data = amfd_data;
120
Christopher Ferrisbdea3bb2021-11-17 01:09:22 +0000121 tombstone_proto_to_text(tombstone, [&log](const std::string& line, bool should_log) {
122 _LOG(&log, should_log ? logtype::HEADER : logtype::LOGS, "%s\n", line.c_str());
123 });
Josh Gaoe73c9322017-02-08 16:06:26 -0800124}