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