| // Copyright 2020 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. | |
| #include "src/developer/forensics/exceptions/handler/crash_reporter.h" | |
| #include <fidl/fuchsia.feedback/cpp/fidl.h> | |
| #include <lib/async/cpp/task.h> | |
| #include <lib/fpromise/bridge.h> | |
| #include <lib/fpromise/promise.h> | |
| #include <lib/fpromise/result.h> | |
| #include <lib/syslog/cpp/macros.h> | |
| #include <lib/zx/exception.h> | |
| #include <zircon/syscalls/object.h> | |
| #include <optional> | |
| #include <string> | |
| #include <utility> | |
| #include "src/developer/forensics/exceptions/constants.h" | |
| #include "src/developer/forensics/exceptions/handler/component_lookup.h" | |
| #include "src/developer/forensics/exceptions/handler/minidump.h" | |
| #include "src/developer/forensics/exceptions/handler/report_builder.h" | |
| #include "src/developer/forensics/exceptions/handler/wake_lease.h" | |
| #include "src/developer/forensics/utils/errors.h" | |
| #include "src/developer/forensics/utils/promise_timeout.h" | |
| #include "src/lib/fidl/contrib/fpromise/client.h" | |
| #include "src/lib/fsl/handles/object_info.h" | |
| namespace forensics { | |
| namespace exceptions { | |
| namespace handler { | |
| namespace { | |
| using fuchsia_power_system::LeaseToken; | |
| // Either resets the exception immediately if the process only has one thread or with a 5s delay | |
| // otherwise. | |
| void ResetException(async_dispatcher_t* dispatcher, zx::exception exception, | |
| const zx::process& process) { | |
| if (!exception.is_valid()) { | |
| return; | |
| } | |
| if (!process.is_valid()) { | |
| FX_LOGS(ERROR) << "Process for exception is invalid"; | |
| exception.reset(); | |
| return; | |
| } | |
| size_t num_threads{0}; | |
| if (const zx_status_t status = zx_object_get_info(process.get(), ZX_INFO_PROCESS_THREADS, nullptr, | |
| 0u, nullptr, &num_threads); | |
| status != ZX_OK) { | |
| FX_PLOGS(ERROR, status) << "Failed to get thread info from process " << process.get(); | |
| exception.reset(); | |
| return; | |
| } | |
| if (num_threads > 1) { | |
| // If the process has multiple threads, delay resetting |exception| for 5 seconds. If one of the | |
| // other threads is in an exception, releasing |exception| immediately may result in the process | |
| // being terminated by the kernel before the minidump for the other thread is generated. | |
| async::PostDelayedTask( | |
| dispatcher, [exception = std::move(exception)]() mutable { exception.reset(); }, | |
| zx::sec(5)); | |
| } else { | |
| exception.reset(); | |
| } | |
| } | |
| // Returns true if the process is terminated. | |
| bool IsProcessTerminated(zx::process& crashed_process) { | |
| zx_info_process_t process_info; | |
| if (const zx_status_t status = crashed_process.get_info(ZX_INFO_PROCESS, &process_info, | |
| sizeof(process_info), nullptr, nullptr); | |
| status != ZX_OK) { | |
| FX_PLOGS(ERROR, status) << "Failed to get info for process " << crashed_process.get(); | |
| return false; | |
| } | |
| return process_info.flags & ZX_INFO_PROCESS_FLAG_EXITED; | |
| } | |
| fpromise::promise<> Delay(async_dispatcher_t* dispatcher, const zx::duration duration) { | |
| fpromise::bridge<> bridge; | |
| if (const zx_status_t status = async::PostDelayedTask( | |
| dispatcher, | |
| [completer = std::move(bridge.completer)]() mutable { completer.complete_ok(); }, | |
| zx::sec(5)); | |
| status != ZX_OK) { | |
| // When |bridge.completer| gets destroyed, the completer is considered abandoned and the | |
| // consumer will receive an error result. | |
| FX_PLOGS(ERROR, status) << "Failed to delay connecting to the crash reporter, connecting now"; | |
| } | |
| return bridge.consumer.promise_or(fpromise::error()); | |
| } | |
| // Connects to fuchsia.feedback.CrashReporter and files |report|. The connection is held until the | |
| // returned promise completes. | |
| fpromise::promise<void, Error> FileCrashReport( | |
| async_dispatcher_t* dispatcher, const std::shared_ptr<sys::ServiceDirectory>& services, | |
| fuchsia_feedback::CrashReport report) { | |
| using FileReportErrors = fidl::ErrorsIn<fuchsia_feedback::CrashReporter::FileReport>; | |
| auto [client_end, server_end] = fidl::Endpoints<fuchsia_feedback::CrashReporter>::Create(); | |
| services->Connect(fidl::DiscoverableProtocolName<fuchsia_feedback::CrashReporter>, | |
| server_end.TakeChannel()); | |
| fidl::Client<fuchsia_feedback::CrashReporter> client(std::move(client_end), dispatcher); | |
| fpromise::promise<void, Error> file_report_promise = | |
| fidl_fpromise::as_promise(client->FileReport({std::move(report)})) | |
| .or_else([](FileReportErrors& error) { | |
| FX_LOGS(WARNING) << "Failed to file crash report: " << error; | |
| return fpromise::error(error.is_framework_error() | |
| ? FidlErrorToForensicsError(error.framework_error()) | |
| : Error::kBadValue); | |
| }) | |
| .and_then([](const fuchsia_feedback::CrashReporterFileReportResponse&) { | |
| return fpromise::ok(); | |
| }); | |
| // Keep |client| alive until the flow completes. | |
| return MakePromiseTimeout<void>(std::move(file_report_promise), dispatcher, kFileReportTimeout) | |
| .then([client = std::move(client)](fpromise::result<void, Error>& result) { | |
| return std::move(result); | |
| }); | |
| } | |
| } // namespace | |
| CrashReporter::CrashReporter( | |
| async_dispatcher_t* dispatcher, std::shared_ptr<sys::ServiceDirectory> services, | |
| zx::duration component_lookup_timeout, std::unique_ptr<WakeLeaseBase> wake_lease, | |
| fidl::ClientEnd<fuchsia_driver_crash::CrashIntrospect> driver_crash_introspect) | |
| : dispatcher_(dispatcher), | |
| executor_(dispatcher_), | |
| services_(std::move(services)), | |
| component_lookup_timeout_(component_lookup_timeout), | |
| wake_lease_(std::move(wake_lease)) { | |
| if (driver_crash_introspect) { | |
| driver_crash_introspect_.Bind(std::move(driver_crash_introspect), dispatcher_); | |
| } | |
| } | |
| void CrashReporter::Send(SendRequest& request, SendCompleter::Sync& completer) { | |
| CrashReportBuilder builder; | |
| builder.SetProcess(request.crashed_process()).SetThread(request.crashed_thread()); | |
| if (request.exception().is_valid()) { | |
| std::optional<ExceptionReason> exception_reason{std::nullopt}; | |
| std::optional<std::string> gwp_asan_exception_type; | |
| zx::vmo minidump = | |
| GenerateMinidump(request.exception(), &exception_reason, &gwp_asan_exception_type); | |
| if (minidump.is_valid()) { | |
| builder.SetMinidump(std::move(minidump)); | |
| } else if (exception_reason == ExceptionReason::kPageFaultTimedOut) { | |
| builder.SetMinidumpSkipped(); | |
| } else { | |
| builder.SetProcessTerminated(); | |
| } | |
| builder.SetExceptionReason(exception_reason); | |
| if (gwp_asan_exception_type.has_value()) { | |
| builder.SetGwpAsanExceptionType(gwp_asan_exception_type.value()); | |
| } | |
| } else { | |
| builder.SetExceptionExpired(); | |
| } | |
| if (IsProcessTerminated(request.crashed_process())) { | |
| builder.SetProcessTerminated(); | |
| } | |
| // If suspend is enabled, acquire a wake lease before releasing the exception. The wake lease | |
| // should be kept in scope until after we're done filing the crash report. | |
| fpromise::promise<LeaseToken, Error> wake_lease_promise = | |
| wake_lease_ != nullptr | |
| ? wake_lease_->Acquire(kWakeLeaseAcquisitionTimeout) | |
| : fpromise::make_result_promise<LeaseToken, Error>(fpromise::ok(LeaseToken())); | |
| const zx_koid_t thread_koid = fsl::GetKoid(request.crashed_thread().get()); | |
| const zx_koid_t process_koid = fsl::GetKoid(request.crashed_process().get()); | |
| auto join = fpromise::join_promises( | |
| std::move(wake_lease_promise), | |
| GetComponentInfo(dispatcher_, services_, driver_crash_introspect_, component_lookup_timeout_, | |
| process_koid, thread_koid)); | |
| auto join_promise = join.and_then( | |
| [dispatcher = dispatcher_, services = services_, builder = std::move(builder), | |
| exception = std::move(request.exception()), | |
| crashed_process = std::move(request.crashed_process()), completer = completer.ToAsync()]( | |
| std::tuple<fpromise::result<LeaseToken, Error>, fpromise::result<ComponentInfo>>& | |
| results) mutable { | |
| fpromise::result<LeaseToken, Error>& wake_lease_result = std::get<0>(results); | |
| fpromise::result<ComponentInfo>& component_info_result = std::get<1>(results); | |
| if (wake_lease_result.is_error()) { | |
| FX_LOGS(ERROR) << "Wake lease not acquired: " + ToString(wake_lease_result.error()); | |
| } | |
| // Don't release the exception until after we attempted to acquire a wake lease, if | |
| // applicable. | |
| ResetException(dispatcher, std::move(exception), crashed_process); | |
| ComponentInfo component_info; | |
| if (component_info_result.is_ok()) { | |
| component_info = component_info_result.take_value(); | |
| } | |
| builder.SetComponentInfo(component_info); | |
| fpromise::promise<> delay = (builder.ProcessName() != "feedback.cm") | |
| ? fpromise::make_ok_promise() | |
| : Delay(dispatcher, /*duration=*/zx::sec(5)); | |
| return delay | |
| .then([dispatcher, services, | |
| builder = std::move(builder)](const fpromise::result<>& result) mutable { | |
| return FileCrashReport(dispatcher, services, builder.Consume()); | |
| }) | |
| .then([wake_lease_result = std::move(wake_lease_result), | |
| component_info = std::move(component_info), completer = std::move(completer)]( | |
| const fpromise::result<void, Error>& result) mutable { | |
| std::optional<std::string> moniker; | |
| if (!component_info.moniker.empty()) { | |
| moniker = std::move(component_info.moniker); | |
| } | |
| completer.Reply({{.moniker = std::move(moniker)}}); | |
| return ::fpromise::ok(); | |
| }); | |
| }); | |
| executor_.schedule_task(std::move(join_promise)); | |
| } | |
| } // namespace handler | |
| } // namespace exceptions | |
| } // namespace forensics |