Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016, 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 | |
| 17 | #include <fcntl.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <array> |
| 25 | #include <deque> |
Josh Gao | cb68a03 | 2017-06-02 13:02:10 -0700 | [diff] [blame] | 26 | #include <string> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 27 | #include <unordered_map> |
Josh Gao | cb68a03 | 2017-06-02 13:02:10 -0700 | [diff] [blame] | 28 | #include <utility> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 29 | |
| 30 | #include <event2/event.h> |
| 31 | #include <event2/listener.h> |
| 32 | #include <event2/thread.h> |
| 33 | |
Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 34 | #include <android-base/cmsg.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 36 | #include <android-base/properties.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
| 38 | #include <android-base/unique_fd.h> |
| 39 | #include <cutils/sockets.h> |
| 40 | |
Josh Gao | 55f79a5 | 2017-03-06 12:24:07 -0800 | [diff] [blame] | 41 | #include "debuggerd/handler.h" |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 42 | #include "dump_type.h" |
Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 43 | #include "protocol.h" |
| 44 | #include "util.h" |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 45 | |
| 46 | #include "intercept_manager.h" |
| 47 | |
Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 48 | using android::base::GetIntProperty; |
Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 49 | using android::base::SendFileDescriptors; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 50 | using android::base::StringPrintf; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 51 | |
| 52 | using android::base::borrowed_fd; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 53 | using android::base::unique_fd; |
| 54 | |
| 55 | static InterceptManager* intercept_manager; |
| 56 | |
| 57 | enum CrashStatus { |
| 58 | kCrashStatusRunning, |
| 59 | kCrashStatusQueued, |
| 60 | }; |
| 61 | |
Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 62 | // Ownership of Crash is a bit messy. |
| 63 | // It's either owned by an active event that must have a timeout, or owned by |
| 64 | // queued_requests, in the case that multiple crashes come in at the same time. |
| 65 | struct Crash { |
| 66 | ~Crash() { event_free(crash_event); } |
| 67 | |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 68 | std::string crash_tombstone_path; |
| 69 | unique_fd crash_tombstone_fd; |
Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 70 | unique_fd crash_socket_fd; |
Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 71 | pid_t crash_pid; |
| 72 | event* crash_event = nullptr; |
Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 73 | |
| 74 | DebuggerdDumpType crash_type; |
| 75 | }; |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 76 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 77 | class CrashQueue { |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 78 | public: |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 79 | CrashQueue(const std::string& dir_path, const std::string& file_name_prefix, size_t max_artifacts, |
Jerome Gaillard | 5ec54d1 | 2021-01-26 12:36:12 +0000 | [diff] [blame] | 80 | size_t max_concurrent_dumps) |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 81 | : file_name_prefix_(file_name_prefix), |
| 82 | dir_path_(dir_path), |
| 83 | dir_fd_(open(dir_path.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC)), |
| 84 | max_artifacts_(max_artifacts), |
| 85 | next_artifact_(0), |
| 86 | max_concurrent_dumps_(max_concurrent_dumps), |
Jerome Gaillard | 5ec54d1 | 2021-01-26 12:36:12 +0000 | [diff] [blame] | 87 | num_concurrent_dumps_(0) { |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 88 | if (dir_fd_ == -1) { |
| 89 | PLOG(FATAL) << "failed to open directory: " << dir_path; |
| 90 | } |
| 91 | |
| 92 | // NOTE: If max_artifacts_ <= max_concurrent_dumps_, then theoretically the |
| 93 | // same filename could be handed out to multiple processes. |
| 94 | CHECK(max_artifacts_ > max_concurrent_dumps_); |
| 95 | |
| 96 | find_oldest_artifact(); |
| 97 | } |
| 98 | |
Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 99 | static CrashQueue* for_crash(const Crash* crash) { |
| 100 | return (crash->crash_type == kDebuggerdJavaBacktrace) ? for_anrs() : for_tombstones(); |
| 101 | } |
| 102 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 103 | static CrashQueue* for_crash(const std::unique_ptr<Crash>& crash) { |
| 104 | return for_crash(crash.get()); |
| 105 | } |
| 106 | |
Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 107 | static CrashQueue* for_tombstones() { |
| 108 | static CrashQueue queue("/data/tombstones", "tombstone_" /* file_name_prefix */, |
Elliott Hughes | ec220cd | 2019-09-26 14:35:24 -0700 | [diff] [blame] | 109 | GetIntProperty("tombstoned.max_tombstone_count", 32), |
Jerome Gaillard | 5ec54d1 | 2021-01-26 12:36:12 +0000 | [diff] [blame] | 110 | 1 /* max_concurrent_dumps */); |
Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 111 | return &queue; |
| 112 | } |
| 113 | |
| 114 | static CrashQueue* for_anrs() { |
| 115 | static CrashQueue queue("/data/anr", "trace_" /* file_name_prefix */, |
| 116 | GetIntProperty("tombstoned.max_anr_count", 64), |
Jerome Gaillard | 5ec54d1 | 2021-01-26 12:36:12 +0000 | [diff] [blame] | 117 | 4 /* max_concurrent_dumps */); |
Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 118 | return &queue; |
| 119 | } |
| 120 | |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 121 | std::pair<std::string, unique_fd> get_output() { |
| 122 | std::string path; |
| 123 | unique_fd result(openat(dir_fd_, ".", O_WRONLY | O_APPEND | O_TMPFILE | O_CLOEXEC, 0640)); |
| 124 | if (result == -1) { |
Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 125 | // We might not have O_TMPFILE. Try creating with an arbitrary filename instead. |
| 126 | static size_t counter = 0; |
| 127 | std::string tmp_filename = StringPrintf(".temporary%zu", counter++); |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 128 | result.reset(openat(dir_fd_, tmp_filename.c_str(), |
| 129 | O_WRONLY | O_APPEND | O_CREAT | O_TRUNC | O_CLOEXEC, 0640)); |
| 130 | if (result == -1) { |
Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 131 | PLOG(FATAL) << "failed to create temporary tombstone in " << dir_path_; |
| 132 | } |
Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 133 | |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 134 | path = StringPrintf("%s/%s", dir_path_.c_str(), tmp_filename.c_str()); |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 135 | } |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 136 | return std::make_pair(std::move(path), std::move(result)); |
Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 137 | } |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 138 | |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 139 | std::string get_next_artifact_path() { |
| 140 | std::string file_name = |
| 141 | StringPrintf("%s/%s%02d", dir_path_.c_str(), file_name_prefix_.c_str(), next_artifact_); |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 142 | next_artifact_ = (next_artifact_ + 1) % max_artifacts_; |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 143 | return file_name; |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 146 | // Consumes crash if it returns true, otherwise leaves it untouched. |
| 147 | bool maybe_enqueue_crash(std::unique_ptr<Crash>&& crash) { |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 148 | if (num_concurrent_dumps_ == max_concurrent_dumps_) { |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 149 | queued_requests_.emplace_back(std::move(crash)); |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | |
| 153 | return false; |
| 154 | } |
| 155 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 156 | void maybe_dequeue_crashes(void (*handler)(std::unique_ptr<Crash> crash)) { |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 157 | while (!queued_requests_.empty() && num_concurrent_dumps_ < max_concurrent_dumps_) { |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 158 | std::unique_ptr<Crash> next_crash = std::move(queued_requests_.front()); |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 159 | queued_requests_.pop_front(); |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 160 | handler(std::move(next_crash)); |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | void on_crash_started() { ++num_concurrent_dumps_; } |
| 165 | |
| 166 | void on_crash_completed() { --num_concurrent_dumps_; } |
| 167 | |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 168 | private: |
| 169 | void find_oldest_artifact() { |
| 170 | size_t oldest_tombstone = 0; |
| 171 | time_t oldest_time = std::numeric_limits<time_t>::max(); |
| 172 | |
| 173 | for (size_t i = 0; i < max_artifacts_; ++i) { |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 174 | std::string path = StringPrintf("%s/%s%02zu", dir_path_.c_str(), file_name_prefix_.c_str(), i); |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 175 | struct stat st; |
| 176 | if (stat(path.c_str(), &st) != 0) { |
| 177 | if (errno == ENOENT) { |
| 178 | oldest_tombstone = i; |
| 179 | break; |
| 180 | } else { |
| 181 | PLOG(ERROR) << "failed to stat " << path; |
| 182 | continue; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (st.st_mtime < oldest_time) { |
| 187 | oldest_tombstone = i; |
| 188 | oldest_time = st.st_mtime; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | next_artifact_ = oldest_tombstone; |
| 193 | } |
| 194 | |
| 195 | const std::string file_name_prefix_; |
| 196 | |
| 197 | const std::string dir_path_; |
| 198 | const int dir_fd_; |
| 199 | |
| 200 | const size_t max_artifacts_; |
| 201 | int next_artifact_; |
| 202 | |
| 203 | const size_t max_concurrent_dumps_; |
| 204 | size_t num_concurrent_dumps_; |
| 205 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 206 | std::deque<std::unique_ptr<Crash>> queued_requests_; |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 207 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 208 | DISALLOW_COPY_AND_ASSIGN(CrashQueue); |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | // Whether java trace dumps are produced via tombstoned. |
Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 212 | static constexpr bool kJavaTraceDumpsEnabled = true; |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 213 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 214 | // Forward declare the callbacks so they can be placed in a sensible order. |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 215 | static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, sockaddr*, int, void*); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 216 | static void crash_request_cb(evutil_socket_t sockfd, short ev, void* arg); |
| 217 | static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg); |
| 218 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 219 | static void perform_request(std::unique_ptr<Crash> crash) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 220 | unique_fd output_fd; |
Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 221 | bool intercepted = |
| 222 | intercept_manager->GetIntercept(crash->crash_pid, crash->crash_type, &output_fd); |
Jerome Gaillard | 5ec54d1 | 2021-01-26 12:36:12 +0000 | [diff] [blame] | 223 | if (!intercepted) { |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 224 | if (crash->crash_type == kDebuggerdNativeBacktrace) { |
| 225 | // Don't generate tombstones for native backtrace requests. |
| 226 | output_fd.reset(open("/dev/null", O_WRONLY | O_CLOEXEC)); |
Josh Gao | 2b22ae1 | 2018-09-12 14:51:03 -0700 | [diff] [blame] | 227 | } else { |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 228 | std::tie(crash->crash_tombstone_path, output_fd) = CrashQueue::for_crash(crash)->get_output(); |
| 229 | crash->crash_tombstone_fd.reset(dup(output_fd.get())); |
Josh Gao | 2b22ae1 | 2018-09-12 14:51:03 -0700 | [diff] [blame] | 230 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 233 | TombstonedCrashPacket response = { |
| 234 | .packet_type = CrashPacketType::kPerformDump |
| 235 | }; |
Jerome Gaillard | 5ec54d1 | 2021-01-26 12:36:12 +0000 | [diff] [blame] | 236 | ssize_t rc = |
| 237 | SendFileDescriptors(crash->crash_socket_fd, &response, sizeof(response), output_fd.get()); |
Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 238 | output_fd.reset(); |
| 239 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 240 | if (rc == -1) { |
| 241 | PLOG(WARNING) << "failed to send response to CrashRequest"; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 242 | return; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 243 | } else if (rc != sizeof(response)) { |
| 244 | PLOG(WARNING) << "crash socket write returned short"; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 245 | return; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 246 | } |
Josh Gao | 1307824 | 2017-03-30 14:42:46 -0700 | [diff] [blame] | 247 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 248 | // TODO: Make this configurable by the interceptor? |
| 249 | struct timeval timeout = {10, 0}; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 250 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 251 | event_base* base = event_get_base(crash->crash_event); |
| 252 | |
| 253 | event_assign(crash->crash_event, base, crash->crash_socket_fd, EV_TIMEOUT | EV_READ, |
| 254 | crash_completed_cb, crash.get()); |
| 255 | event_add(crash->crash_event, &timeout); |
| 256 | CrashQueue::for_crash(crash)->on_crash_started(); |
| 257 | |
| 258 | // The crash is now owned by the event loop. |
| 259 | crash.release(); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, sockaddr*, int, |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 263 | void*) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 264 | event_base* base = evconnlistener_get_base(listener); |
| 265 | Crash* crash = new Crash(); |
| 266 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 267 | // TODO: Make sure that only java crashes come in on the java socket |
| 268 | // and only native crashes on the native socket. |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 269 | struct timeval timeout = { 1, 0 }; |
| 270 | event* crash_event = event_new(base, sockfd, EV_TIMEOUT | EV_READ, crash_request_cb, crash); |
Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 271 | crash->crash_socket_fd.reset(sockfd); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 272 | crash->crash_event = crash_event; |
| 273 | event_add(crash_event, &timeout); |
| 274 | } |
| 275 | |
| 276 | static void crash_request_cb(evutil_socket_t sockfd, short ev, void* arg) { |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 277 | std::unique_ptr<Crash> crash(static_cast<Crash*>(arg)); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 278 | TombstonedCrashPacket request = {}; |
| 279 | |
| 280 | if ((ev & EV_TIMEOUT) != 0) { |
| 281 | LOG(WARNING) << "crash request timed out"; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 282 | return; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 283 | } else if ((ev & EV_READ) == 0) { |
| 284 | LOG(WARNING) << "tombstoned received unexpected event from crash socket"; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 285 | return; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 288 | ssize_t rc = TEMP_FAILURE_RETRY(read(sockfd, &request, sizeof(request))); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 289 | if (rc == -1) { |
| 290 | PLOG(WARNING) << "failed to read from crash socket"; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 291 | return; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 292 | } else if (rc != sizeof(request)) { |
| 293 | LOG(WARNING) << "crash socket received short read of length " << rc << " (expected " |
| 294 | << sizeof(request) << ")"; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 295 | return; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | if (request.packet_type != CrashPacketType::kDumpRequest) { |
| 299 | LOG(WARNING) << "unexpected crash packet type, expected kDumpRequest, received " |
| 300 | << StringPrintf("%#2hhX", request.packet_type); |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 301 | return; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 304 | crash->crash_type = request.packet.dump_request.dump_type; |
Jerome Gaillard | 5ec54d1 | 2021-01-26 12:36:12 +0000 | [diff] [blame] | 305 | if (crash->crash_type < 0 || crash->crash_type > kDebuggerdAnyIntercept) { |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 306 | LOG(WARNING) << "unexpected crash dump type: " << crash->crash_type; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 307 | return; |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if (crash->crash_type != kDebuggerdJavaBacktrace) { |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 311 | crash->crash_pid = request.packet.dump_request.pid; |
| 312 | } else { |
| 313 | // Requests for java traces are sent from untrusted processes, so we |
| 314 | // must not trust the PID sent down with the request. Instead, we ask the |
| 315 | // kernel. |
| 316 | ucred cr = {}; |
| 317 | socklen_t len = sizeof(cr); |
| 318 | int ret = getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &len); |
| 319 | if (ret != 0) { |
| 320 | PLOG(ERROR) << "Failed to getsockopt(..SO_PEERCRED)"; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 321 | return; |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | crash->crash_pid = cr.pid; |
| 325 | } |
| 326 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 327 | pid_t crash_pid = crash->crash_pid; |
| 328 | LOG(INFO) << "received crash request for pid " << crash_pid; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 329 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 330 | if (CrashQueue::for_crash(crash)->maybe_enqueue_crash(std::move(crash))) { |
| 331 | LOG(INFO) << "enqueueing crash request for pid " << crash_pid; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 332 | } else { |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 333 | perform_request(std::move(crash)); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 334 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 337 | static void crash_completed(borrowed_fd sockfd, std::unique_ptr<Crash> crash) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 338 | TombstonedCrashPacket request = {}; |
| 339 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 340 | ssize_t rc = TEMP_FAILURE_RETRY(read(sockfd.get(), &request, sizeof(request))); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 341 | if (rc == -1) { |
| 342 | PLOG(WARNING) << "failed to read from crash socket"; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 343 | return; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 344 | } else if (rc != sizeof(request)) { |
| 345 | LOG(WARNING) << "crash socket received short read of length " << rc << " (expected " |
| 346 | << sizeof(request) << ")"; |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 347 | return; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | if (request.packet_type != CrashPacketType::kCompletedDump) { |
| 351 | LOG(WARNING) << "unexpected crash packet type, expected kCompletedDump, received " |
| 352 | << uint32_t(request.packet_type); |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 353 | return; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 356 | if (crash->crash_tombstone_fd != -1) { |
| 357 | std::string fd_path = StringPrintf("/proc/self/fd/%d", crash->crash_tombstone_fd.get()); |
| 358 | std::string tombstone_path = CrashQueue::for_crash(crash)->get_next_artifact_path(); |
Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 359 | |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 360 | // linkat doesn't let us replace a file, so we need to unlink first. |
| 361 | int rc = unlink(tombstone_path.c_str()); |
| 362 | if (rc != 0 && errno != ENOENT) { |
| 363 | PLOG(ERROR) << "failed to unlink tombstone at " << tombstone_path; |
| 364 | return; |
| 365 | } |
Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 366 | |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 367 | rc = linkat(AT_FDCWD, fd_path.c_str(), AT_FDCWD, tombstone_path.c_str(), AT_SYMLINK_FOLLOW); |
| 368 | if (rc != 0) { |
| 369 | PLOG(ERROR) << "failed to link tombstone"; |
Narayan Kamath | 79dd143 | 2017-06-21 19:42:00 +0100 | [diff] [blame] | 370 | } else { |
Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 371 | if (crash->crash_type == kDebuggerdJavaBacktrace) { |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 372 | LOG(ERROR) << "Traces for pid " << crash->crash_pid << " written to: " << tombstone_path; |
Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 373 | } else { |
| 374 | // NOTE: Several tools parse this log message to figure out where the |
| 375 | // tombstone associated with a given native crash was written. Any changes |
| 376 | // to this message must be carefully considered. |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 377 | LOG(ERROR) << "Tombstone written to: " << tombstone_path; |
Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 378 | } |
Narayan Kamath | 79dd143 | 2017-06-21 19:42:00 +0100 | [diff] [blame] | 379 | } |
Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 380 | |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 381 | // If we don't have O_TMPFILE, we need to clean up after ourselves. |
| 382 | if (!crash->crash_tombstone_path.empty()) { |
| 383 | rc = unlink(crash->crash_tombstone_path.c_str()); |
| 384 | if (rc != 0) { |
| 385 | PLOG(ERROR) << "failed to unlink temporary tombstone at " << crash->crash_tombstone_path; |
| 386 | } |
Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 387 | } |
Josh Gao | cb68a03 | 2017-06-02 13:02:10 -0700 | [diff] [blame] | 388 | } |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 389 | } |
Josh Gao | cb68a03 | 2017-06-02 13:02:10 -0700 | [diff] [blame] | 390 | |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 391 | static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg) { |
| 392 | std::unique_ptr<Crash> crash(static_cast<Crash*>(arg)); |
Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 393 | CrashQueue* queue = CrashQueue::for_crash(crash); |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 394 | |
Jerome Gaillard | eda96ed | 2021-01-26 12:36:12 +0000 | [diff] [blame^] | 395 | CrashQueue::for_crash(crash)->on_crash_completed(); |
Josh Gao | 9a61f68 | 2020-12-01 21:04:09 -0800 | [diff] [blame] | 396 | |
| 397 | if ((ev & EV_READ) == EV_READ) { |
| 398 | crash_completed(sockfd, std::move(crash)); |
| 399 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 400 | |
| 401 | // If there's something queued up, let them proceed. |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 402 | queue->maybe_dequeue_crashes(perform_request); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | int main(int, char* []) { |
Josh Gao | 8830c95 | 2017-03-06 12:23:55 -0800 | [diff] [blame] | 406 | umask(0137); |
| 407 | |
Josh Gao | 55f79a5 | 2017-03-06 12:24:07 -0800 | [diff] [blame] | 408 | // Don't try to connect to ourselves if we crash. |
| 409 | struct sigaction action = {}; |
| 410 | action.sa_handler = [](int signal) { |
| 411 | LOG(ERROR) << "received fatal signal " << signal; |
| 412 | _exit(1); |
| 413 | }; |
| 414 | debuggerd_register_handlers(&action); |
| 415 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 416 | int intercept_socket = android_get_control_socket(kTombstonedInterceptSocketName); |
| 417 | int crash_socket = android_get_control_socket(kTombstonedCrashSocketName); |
| 418 | |
| 419 | if (intercept_socket == -1 || crash_socket == -1) { |
| 420 | PLOG(FATAL) << "failed to get socket from init"; |
| 421 | } |
| 422 | |
| 423 | evutil_make_socket_nonblocking(intercept_socket); |
| 424 | evutil_make_socket_nonblocking(crash_socket); |
| 425 | |
| 426 | event_base* base = event_base_new(); |
| 427 | if (!base) { |
| 428 | LOG(FATAL) << "failed to create event_base"; |
| 429 | } |
| 430 | |
| 431 | intercept_manager = new InterceptManager(base, intercept_socket); |
| 432 | |
Narayan Kamath | c2e98f6 | 2017-09-13 13:12:34 +0100 | [diff] [blame] | 433 | evconnlistener* tombstone_listener = |
| 434 | evconnlistener_new(base, crash_accept_cb, CrashQueue::for_tombstones(), LEV_OPT_CLOSE_ON_FREE, |
| 435 | -1 /* backlog */, crash_socket); |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 436 | if (!tombstone_listener) { |
| 437 | LOG(FATAL) << "failed to create evconnlistener for tombstones."; |
| 438 | } |
| 439 | |
| 440 | if (kJavaTraceDumpsEnabled) { |
| 441 | const int java_trace_socket = android_get_control_socket(kTombstonedJavaTraceSocketName); |
| 442 | if (java_trace_socket == -1) { |
| 443 | PLOG(FATAL) << "failed to get socket from init"; |
| 444 | } |
| 445 | |
| 446 | evutil_make_socket_nonblocking(java_trace_socket); |
Narayan Kamath | c2e98f6 | 2017-09-13 13:12:34 +0100 | [diff] [blame] | 447 | evconnlistener* java_trace_listener = |
| 448 | evconnlistener_new(base, crash_accept_cb, CrashQueue::for_anrs(), LEV_OPT_CLOSE_ON_FREE, |
| 449 | -1 /* backlog */, java_trace_socket); |
Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 450 | if (!java_trace_listener) { |
| 451 | LOG(FATAL) << "failed to create evconnlistener for java traces."; |
| 452 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | LOG(INFO) << "tombstoned successfully initialized"; |
| 456 | event_base_dispatch(base); |
| 457 | } |