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