Loading debuggerd/tombstoned/tombstoned.cpp +60 −28 Original line number Diff line number Diff line Loading @@ -61,10 +61,11 @@ enum CrashStatus { struct Crash { ~Crash() { event_free(crash_event); } unique_fd crash_fd; std::string crash_tombstone_path; unique_fd crash_tombstone_fd; unique_fd crash_socket_fd; pid_t crash_pid; event* crash_event = nullptr; std::string crash_path; DebuggerdDumpType crash_type; }; Loading Loading @@ -109,24 +110,29 @@ class CrashQueue { return &queue; } std::pair<unique_fd, std::string> get_output() { unique_fd result; std::string file_name = StringPrintf("%s%02d", file_name_prefix_.c_str(), next_artifact_); // Unlink and create the file, instead of using O_TRUNC, to avoid two processes // interleaving their output in case we ever get into that situation. if (unlinkat(dir_fd_, file_name.c_str(), 0) != 0 && errno != ENOENT) { PLOG(FATAL) << "failed to unlink tombstone at " << dir_path_ << "/" << file_name; std::pair<std::string, unique_fd> get_output() { std::string path; unique_fd result(openat(dir_fd_, ".", O_WRONLY | O_APPEND | O_TMPFILE | O_CLOEXEC, 0640)); if (result == -1) { // We might not have O_TMPFILE. Try creating with an arbitrary filename instead. static size_t counter = 0; std::string tmp_filename = StringPrintf(".temporary%zu", counter++); result.reset(openat(dir_fd_, tmp_filename.c_str(), O_WRONLY | O_APPEND | O_CREAT | O_TRUNC | O_CLOEXEC, 0640)); if (result == -1) { PLOG(FATAL) << "failed to create temporary tombstone in " << dir_path_; } result.reset(openat(dir_fd_, file_name.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_APPEND | O_CLOEXEC, 0640)); if (result == -1) { PLOG(FATAL) << "failed to create tombstone at " << dir_path_ << "/" << file_name; path = StringPrintf("%s/%s", dir_path_.c_str(), tmp_filename.c_str()); } return std::make_pair(std::move(path), std::move(result)); } std::string get_next_artifact_path() { std::string file_name = StringPrintf("%s/%s%02d", dir_path_.c_str(), file_name_prefix_.c_str(), next_artifact_); next_artifact_ = (next_artifact_ + 1) % max_artifacts_; return {std::move(result), dir_path_ + "/" + file_name}; return file_name; } bool maybe_enqueue_crash(Crash* crash) { Loading Loading @@ -203,14 +209,17 @@ static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg); static void perform_request(Crash* crash) { unique_fd output_fd; if (!intercept_manager->GetIntercept(crash->crash_pid, crash->crash_type, &output_fd)) { std::tie(output_fd, crash->crash_path) = CrashQueue::for_crash(crash)->get_output(); bool intercepted = intercept_manager->GetIntercept(crash->crash_pid, crash->crash_type, &output_fd); if (!intercepted) { std::tie(crash->crash_tombstone_path, output_fd) = CrashQueue::for_crash(crash)->get_output(); crash->crash_tombstone_fd.reset(dup(output_fd.get())); } TombstonedCrashPacket response = { .packet_type = CrashPacketType::kPerformDump }; ssize_t rc = send_fd(crash->crash_fd, &response, sizeof(response), std::move(output_fd)); ssize_t rc = send_fd(crash->crash_socket_fd, &response, sizeof(response), std::move(output_fd)); if (rc == -1) { PLOG(WARNING) << "failed to send response to CrashRequest"; goto fail; Loading @@ -222,7 +231,7 @@ static void perform_request(Crash* crash) { struct timeval timeout = { 10, 0 }; event_base* base = event_get_base(crash->crash_event); event_assign(crash->crash_event, base, crash->crash_fd, EV_TIMEOUT | EV_READ, event_assign(crash->crash_event, base, crash->crash_socket_fd, EV_TIMEOUT | EV_READ, crash_completed_cb, crash); event_add(crash->crash_event, &timeout); } Loading @@ -243,7 +252,7 @@ static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, so // and only native crashes on the native socket. struct timeval timeout = { 1, 0 }; event* crash_event = event_new(base, sockfd, EV_TIMEOUT | EV_READ, crash_request_cb, crash); crash->crash_fd.reset(sockfd); crash->crash_socket_fd.reset(sockfd); crash->crash_event = crash_event; event_add(crash_event, &timeout); } Loading Loading @@ -342,14 +351,37 @@ static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg) { goto fail; } if (!crash->crash_path.empty()) { if (crash->crash_tombstone_fd != -1) { std::string fd_path = StringPrintf("/proc/self/fd/%d", crash->crash_tombstone_fd.get()); std::string tombstone_path = CrashQueue::for_crash(crash)->get_next_artifact_path(); // linkat doesn't let us replace a file, so we need to unlink first. int rc = unlink(tombstone_path.c_str()); if (rc != 0 && errno != ENOENT) { PLOG(ERROR) << "failed to unlink tombstone at " << tombstone_path; goto fail; } rc = linkat(AT_FDCWD, fd_path.c_str(), AT_FDCWD, tombstone_path.c_str(), AT_SYMLINK_FOLLOW); if (rc != 0) { PLOG(ERROR) << "failed to link tombstone"; } else { if (crash->crash_type == kDebuggerdJavaBacktrace) { LOG(ERROR) << "Traces for pid " << crash->crash_pid << " written to: " << crash->crash_path; LOG(ERROR) << "Traces for pid " << crash->crash_pid << " written to: " << tombstone_path; } else { // NOTE: Several tools parse this log message to figure out where the // tombstone associated with a given native crash was written. Any changes // to this message must be carefully considered. LOG(ERROR) << "Tombstone written to: " << crash->crash_path; LOG(ERROR) << "Tombstone written to: " << tombstone_path; } } // If we don't have O_TMPFILE, we need to clean up after ourselves. if (!crash->crash_tombstone_path.empty()) { rc = unlink(crash->crash_tombstone_path.c_str()); if (rc != 0) { PLOG(ERROR) << "failed to unlink temporary tombstone at " << crash->crash_tombstone_path; } } } Loading lmkd/Android.bp +2 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ cc_binary { ], static_libs: [ "libstatslogc", "libstatssocket", ], local_include_dirs: ["include"], cflags: ["-Werror", "-DLMKD_TRACE_KILLS"], Loading @@ -31,6 +32,7 @@ cc_library_static { shared_libs: [ "liblog", ], static_libs: ["libstatssocket",], } cc_library_static { Loading lmkd/lmkd.c +1 −1 Original line number Diff line number Diff line Loading @@ -37,7 +37,7 @@ #include <log/log.h> #ifdef LMKD_LOG_STATS #include <statslog.h> #include "statslog.h" #endif /* Loading lmkd/statslog.c +23 −6 Original line number Diff line number Diff line Loading @@ -16,8 +16,16 @@ #include <assert.h> #include <errno.h> #include <log/log_event_list.h> #include <log/log_id.h> #include <stats_event_list.h> #include <time.h> static int64_t getElapsedRealTimeNs() { struct timespec t; t.tv_sec = t.tv_nsec = 0; clock_gettime(CLOCK_BOOTTIME, &t); return (int64_t)t.tv_sec * 1000000000LL + t.tv_nsec; } /** * Logs the change in LMKD state which is used as start/stop boundaries for logging Loading @@ -32,6 +40,12 @@ stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t sta return ret; } reset_log_context(ctx); if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) { return ret; } if ((ret = android_log_write_int32(ctx, code)) < 0) { return ret; } Loading @@ -39,7 +53,8 @@ stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t sta if ((ret = android_log_write_int32(ctx, state)) < 0) { return ret; } return ret; return write_to_logger(ctx, LOG_ID_STATS); } /** Loading @@ -56,6 +71,11 @@ stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid if (!ctx) { return ret; } reset_log_context(ctx); if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) { return ret; } if ((ret = android_log_write_int32(ctx, code)) < 0) { return ret; Loading Loading @@ -93,8 +113,5 @@ stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid return ret; } if ((ret = android_log_write_list(ctx, LOG_ID_STATS)) < 0) { return ret; } return ret; return write_to_logger(ctx, LOG_ID_STATS); } lmkd/statslog.h +1 −1 Original line number Diff line number Diff line Loading @@ -18,11 +18,11 @@ #define _STATSLOG_H_ #include <assert.h> #include <stats_event_list.h> #include <stdbool.h> #include <sys/cdefs.h> #include <cutils/properties.h> #include <log/log_event_list.h> __BEGIN_DECLS Loading Loading
debuggerd/tombstoned/tombstoned.cpp +60 −28 Original line number Diff line number Diff line Loading @@ -61,10 +61,11 @@ enum CrashStatus { struct Crash { ~Crash() { event_free(crash_event); } unique_fd crash_fd; std::string crash_tombstone_path; unique_fd crash_tombstone_fd; unique_fd crash_socket_fd; pid_t crash_pid; event* crash_event = nullptr; std::string crash_path; DebuggerdDumpType crash_type; }; Loading Loading @@ -109,24 +110,29 @@ class CrashQueue { return &queue; } std::pair<unique_fd, std::string> get_output() { unique_fd result; std::string file_name = StringPrintf("%s%02d", file_name_prefix_.c_str(), next_artifact_); // Unlink and create the file, instead of using O_TRUNC, to avoid two processes // interleaving their output in case we ever get into that situation. if (unlinkat(dir_fd_, file_name.c_str(), 0) != 0 && errno != ENOENT) { PLOG(FATAL) << "failed to unlink tombstone at " << dir_path_ << "/" << file_name; std::pair<std::string, unique_fd> get_output() { std::string path; unique_fd result(openat(dir_fd_, ".", O_WRONLY | O_APPEND | O_TMPFILE | O_CLOEXEC, 0640)); if (result == -1) { // We might not have O_TMPFILE. Try creating with an arbitrary filename instead. static size_t counter = 0; std::string tmp_filename = StringPrintf(".temporary%zu", counter++); result.reset(openat(dir_fd_, tmp_filename.c_str(), O_WRONLY | O_APPEND | O_CREAT | O_TRUNC | O_CLOEXEC, 0640)); if (result == -1) { PLOG(FATAL) << "failed to create temporary tombstone in " << dir_path_; } result.reset(openat(dir_fd_, file_name.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_APPEND | O_CLOEXEC, 0640)); if (result == -1) { PLOG(FATAL) << "failed to create tombstone at " << dir_path_ << "/" << file_name; path = StringPrintf("%s/%s", dir_path_.c_str(), tmp_filename.c_str()); } return std::make_pair(std::move(path), std::move(result)); } std::string get_next_artifact_path() { std::string file_name = StringPrintf("%s/%s%02d", dir_path_.c_str(), file_name_prefix_.c_str(), next_artifact_); next_artifact_ = (next_artifact_ + 1) % max_artifacts_; return {std::move(result), dir_path_ + "/" + file_name}; return file_name; } bool maybe_enqueue_crash(Crash* crash) { Loading Loading @@ -203,14 +209,17 @@ static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg); static void perform_request(Crash* crash) { unique_fd output_fd; if (!intercept_manager->GetIntercept(crash->crash_pid, crash->crash_type, &output_fd)) { std::tie(output_fd, crash->crash_path) = CrashQueue::for_crash(crash)->get_output(); bool intercepted = intercept_manager->GetIntercept(crash->crash_pid, crash->crash_type, &output_fd); if (!intercepted) { std::tie(crash->crash_tombstone_path, output_fd) = CrashQueue::for_crash(crash)->get_output(); crash->crash_tombstone_fd.reset(dup(output_fd.get())); } TombstonedCrashPacket response = { .packet_type = CrashPacketType::kPerformDump }; ssize_t rc = send_fd(crash->crash_fd, &response, sizeof(response), std::move(output_fd)); ssize_t rc = send_fd(crash->crash_socket_fd, &response, sizeof(response), std::move(output_fd)); if (rc == -1) { PLOG(WARNING) << "failed to send response to CrashRequest"; goto fail; Loading @@ -222,7 +231,7 @@ static void perform_request(Crash* crash) { struct timeval timeout = { 10, 0 }; event_base* base = event_get_base(crash->crash_event); event_assign(crash->crash_event, base, crash->crash_fd, EV_TIMEOUT | EV_READ, event_assign(crash->crash_event, base, crash->crash_socket_fd, EV_TIMEOUT | EV_READ, crash_completed_cb, crash); event_add(crash->crash_event, &timeout); } Loading @@ -243,7 +252,7 @@ static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, so // and only native crashes on the native socket. struct timeval timeout = { 1, 0 }; event* crash_event = event_new(base, sockfd, EV_TIMEOUT | EV_READ, crash_request_cb, crash); crash->crash_fd.reset(sockfd); crash->crash_socket_fd.reset(sockfd); crash->crash_event = crash_event; event_add(crash_event, &timeout); } Loading Loading @@ -342,14 +351,37 @@ static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg) { goto fail; } if (!crash->crash_path.empty()) { if (crash->crash_tombstone_fd != -1) { std::string fd_path = StringPrintf("/proc/self/fd/%d", crash->crash_tombstone_fd.get()); std::string tombstone_path = CrashQueue::for_crash(crash)->get_next_artifact_path(); // linkat doesn't let us replace a file, so we need to unlink first. int rc = unlink(tombstone_path.c_str()); if (rc != 0 && errno != ENOENT) { PLOG(ERROR) << "failed to unlink tombstone at " << tombstone_path; goto fail; } rc = linkat(AT_FDCWD, fd_path.c_str(), AT_FDCWD, tombstone_path.c_str(), AT_SYMLINK_FOLLOW); if (rc != 0) { PLOG(ERROR) << "failed to link tombstone"; } else { if (crash->crash_type == kDebuggerdJavaBacktrace) { LOG(ERROR) << "Traces for pid " << crash->crash_pid << " written to: " << crash->crash_path; LOG(ERROR) << "Traces for pid " << crash->crash_pid << " written to: " << tombstone_path; } else { // NOTE: Several tools parse this log message to figure out where the // tombstone associated with a given native crash was written. Any changes // to this message must be carefully considered. LOG(ERROR) << "Tombstone written to: " << crash->crash_path; LOG(ERROR) << "Tombstone written to: " << tombstone_path; } } // If we don't have O_TMPFILE, we need to clean up after ourselves. if (!crash->crash_tombstone_path.empty()) { rc = unlink(crash->crash_tombstone_path.c_str()); if (rc != 0) { PLOG(ERROR) << "failed to unlink temporary tombstone at " << crash->crash_tombstone_path; } } } Loading
lmkd/Android.bp +2 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ cc_binary { ], static_libs: [ "libstatslogc", "libstatssocket", ], local_include_dirs: ["include"], cflags: ["-Werror", "-DLMKD_TRACE_KILLS"], Loading @@ -31,6 +32,7 @@ cc_library_static { shared_libs: [ "liblog", ], static_libs: ["libstatssocket",], } cc_library_static { Loading
lmkd/lmkd.c +1 −1 Original line number Diff line number Diff line Loading @@ -37,7 +37,7 @@ #include <log/log.h> #ifdef LMKD_LOG_STATS #include <statslog.h> #include "statslog.h" #endif /* Loading
lmkd/statslog.c +23 −6 Original line number Diff line number Diff line Loading @@ -16,8 +16,16 @@ #include <assert.h> #include <errno.h> #include <log/log_event_list.h> #include <log/log_id.h> #include <stats_event_list.h> #include <time.h> static int64_t getElapsedRealTimeNs() { struct timespec t; t.tv_sec = t.tv_nsec = 0; clock_gettime(CLOCK_BOOTTIME, &t); return (int64_t)t.tv_sec * 1000000000LL + t.tv_nsec; } /** * Logs the change in LMKD state which is used as start/stop boundaries for logging Loading @@ -32,6 +40,12 @@ stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t sta return ret; } reset_log_context(ctx); if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) { return ret; } if ((ret = android_log_write_int32(ctx, code)) < 0) { return ret; } Loading @@ -39,7 +53,8 @@ stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t sta if ((ret = android_log_write_int32(ctx, state)) < 0) { return ret; } return ret; return write_to_logger(ctx, LOG_ID_STATS); } /** Loading @@ -56,6 +71,11 @@ stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid if (!ctx) { return ret; } reset_log_context(ctx); if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) { return ret; } if ((ret = android_log_write_int32(ctx, code)) < 0) { return ret; Loading Loading @@ -93,8 +113,5 @@ stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid return ret; } if ((ret = android_log_write_list(ctx, LOG_ID_STATS)) < 0) { return ret; } return ret; return write_to_logger(ctx, LOG_ID_STATS); }
lmkd/statslog.h +1 −1 Original line number Diff line number Diff line Loading @@ -18,11 +18,11 @@ #define _STATSLOG_H_ #include <assert.h> #include <stats_event_list.h> #include <stdbool.h> #include <sys/cdefs.h> #include <cutils/properties.h> #include <log/log_event_list.h> __BEGIN_DECLS Loading