2017-12-06 10:08:11 -08:00
|
|
|
// Copyright 2017 The Crashpad Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "handler/fuchsia/crash_report_exception_handler.h"
|
|
|
|
|
2018-07-30 15:50:17 -07:00
|
|
|
#include <lib/zx/thread.h>
|
2019-04-04 14:59:54 -07:00
|
|
|
#include <zircon/errors.h>
|
|
|
|
#include <zircon/status.h>
|
2018-05-04 16:59:20 -07:00
|
|
|
#include <zircon/syscalls/exception.h>
|
|
|
|
|
|
|
|
#include "base/fuchsia/fuchsia_logging.h"
|
|
|
|
#include "client/settings.h"
|
|
|
|
#include "minidump/minidump_file_writer.h"
|
|
|
|
#include "minidump/minidump_user_extension_stream_data_source.h"
|
|
|
|
#include "snapshot/fuchsia/process_snapshot_fuchsia.h"
|
|
|
|
#include "util/fuchsia/koid_utilities.h"
|
2019-03-22 13:59:56 -07:00
|
|
|
#include "util/fuchsia/scoped_task_suspend.h"
|
2018-05-04 16:59:20 -07:00
|
|
|
|
2017-12-06 10:08:11 -08:00
|
|
|
namespace crashpad {
|
|
|
|
|
2019-04-04 14:59:54 -07:00
|
|
|
CrashReportExceptionHandler::CrashReportExceptionHandler(
|
|
|
|
CrashReportDatabase* database,
|
|
|
|
CrashReportUploadThread* upload_thread,
|
|
|
|
const std::map<std::string, std::string>* process_annotations,
|
|
|
|
const std::map<std::string, fuchsia::mem::Buffer>* process_attachments,
|
|
|
|
const UserStreamDataSources* user_stream_data_sources)
|
|
|
|
: database_(database),
|
|
|
|
upload_thread_(upload_thread),
|
|
|
|
process_annotations_(process_annotations),
|
2019-04-10 13:14:43 -07:00
|
|
|
process_attachments_(process_attachments),
|
2018-05-04 16:59:20 -07:00
|
|
|
user_stream_data_sources_(user_stream_data_sources) {}
|
2017-12-06 10:08:11 -08:00
|
|
|
|
|
|
|
CrashReportExceptionHandler::~CrashReportExceptionHandler() {}
|
|
|
|
|
2019-06-14 09:17:27 -07:00
|
|
|
bool CrashReportExceptionHandler::HandleException(const zx::process& process,
|
|
|
|
const zx::thread& thread,
|
|
|
|
UUID* local_report_id) {
|
2019-05-06 15:18:50 -07:00
|
|
|
ScopedTaskSuspend suspend(process);
|
2018-05-04 16:59:20 -07:00
|
|
|
|
|
|
|
ProcessSnapshotFuchsia process_snapshot;
|
2018-05-14 12:45:51 -07:00
|
|
|
if (!process_snapshot.Initialize(process)) {
|
2018-05-04 16:59:20 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CrashpadInfoClientOptions client_options;
|
|
|
|
process_snapshot.GetCrashpadOptions(&client_options);
|
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
if (client_options.crashpad_handler_behavior == TriState::kDisabled) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-04 16:59:20 -07:00
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
zx_exception_report_t report;
|
|
|
|
zx_status_t status = thread.get_info(ZX_INFO_THREAD_EXCEPTION_REPORT,
|
|
|
|
&report,
|
|
|
|
sizeof(report),
|
|
|
|
nullptr,
|
|
|
|
nullptr);
|
|
|
|
if (status != ZX_OK) {
|
|
|
|
ZX_LOG(ERROR, status)
|
|
|
|
<< "zx_object_get_info ZX_INFO_THREAD_EXCEPTION_REPORT";
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-04 16:59:20 -07:00
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
zx_koid_t thread_id = GetKoidForHandle(thread);
|
|
|
|
if (!process_snapshot.InitializeException(thread_id, report)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-04 16:59:20 -07:00
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
UUID client_id;
|
|
|
|
Settings* const settings = database_->GetSettings();
|
|
|
|
if (settings) {
|
|
|
|
// If GetSettings() or GetClientID() fails, something else will log a
|
|
|
|
// message and client_id will be left at its default value, all zeroes,
|
|
|
|
// which is appropriate.
|
|
|
|
settings->GetClientID(&client_id);
|
|
|
|
}
|
2018-05-04 16:59:20 -07:00
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
process_snapshot.SetClientID(client_id);
|
|
|
|
process_snapshot.SetAnnotationsSimpleMap(*process_annotations_);
|
2018-05-04 16:59:20 -07:00
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
std::unique_ptr<CrashReportDatabase::NewReport> new_report;
|
|
|
|
CrashReportDatabase::OperationStatus database_status =
|
|
|
|
database_->PrepareNewCrashReport(&new_report);
|
|
|
|
if (database_status != CrashReportDatabase::kNoError) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-04 16:59:20 -07:00
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
process_snapshot.SetReportID(new_report->ReportID());
|
2018-05-04 16:59:20 -07:00
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
MinidumpFileWriter minidump;
|
|
|
|
minidump.InitializeFromSnapshot(&process_snapshot);
|
|
|
|
AddUserExtensionStreams(
|
|
|
|
user_stream_data_sources_, &process_snapshot, &minidump);
|
|
|
|
|
|
|
|
if (!minidump.WriteEverything(new_report->Writer())) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-04 16:59:20 -07:00
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
if (process_attachments_) {
|
2019-04-04 14:59:54 -07:00
|
|
|
// Note that attachments are read at this point each time rather than once
|
|
|
|
// so that if the contents of the VMO has changed it will be re-read for
|
|
|
|
// each upload (e.g. in the case of a log file).
|
2019-04-10 13:14:43 -07:00
|
|
|
for (const auto& it : *process_attachments_) {
|
2019-04-04 14:59:54 -07:00
|
|
|
// TODO(frousseau): make FileWriter VMO-aware.
|
|
|
|
FileWriter* writer = new_report->AddAttachment(it.first);
|
|
|
|
if (!writer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto data = std::make_unique<uint8_t[]>(it.second.size);
|
|
|
|
const zx_status_t read_status =
|
|
|
|
it.second.vmo.read(data.get(), 0u, it.second.size);
|
|
|
|
if (read_status != ZX_OK) {
|
|
|
|
ZX_LOG(ERROR, read_status)
|
|
|
|
<< "could not read VMO for attachment " << it.first;
|
|
|
|
// Not being able to read the VMO isn't considered fatal, and
|
|
|
|
// should not prevent the report from being processed.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
writer->Write(data.get(), it.second.size);
|
|
|
|
}
|
2018-10-30 15:33:39 -07:00
|
|
|
}
|
2018-05-18 15:53:54 -07:00
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
UUID uuid;
|
|
|
|
database_status =
|
|
|
|
database_->FinishedWritingCrashReport(std::move(new_report), &uuid);
|
|
|
|
if (database_status != CrashReportDatabase::kNoError) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (local_report_id != nullptr) {
|
|
|
|
*local_report_id = uuid;
|
|
|
|
}
|
2018-05-04 16:59:20 -07:00
|
|
|
|
2018-10-30 15:33:39 -07:00
|
|
|
if (upload_thread_) {
|
|
|
|
upload_thread_->ReportPending(uuid);
|
2018-05-04 16:59:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-06 10:08:11 -08:00
|
|
|
} // namespace crashpad
|