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