linux: refactor snapshotting into CaptureSnapshot()

Change-Id: I7748f6e4097059d5f57ca7f2f4966534129bda86
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/1773773
Commit-Queue: Joshua Peraza <jperaza@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Joshua Peraza 2019-08-30 08:21:23 -07:00 committed by Commit Bot
parent b71f61f8e3
commit 23a1be41ce
4 changed files with 284 additions and 158 deletions

View File

@ -42,6 +42,8 @@ static_library("handler") {
if (crashpad_is_linux || crashpad_is_android) {
set_sources_assignment_filter([])
sources += [
"linux/capture_snapshot.cc",
"linux/capture_snapshot.h",
"linux/crash_report_exception_handler.cc",
"linux/crash_report_exception_handler.h",
"linux/exception_handler_server.cc",

View File

@ -0,0 +1,119 @@
// Copyright 2019 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/linux/capture_snapshot.h"
#include <utility>
#include "snapshot/crashpad_info_client_options.h"
#include "snapshot/sanitized/sanitization_information.h"
#include "util/misc/metrics.h"
#include "util/misc/tri_state.h"
namespace crashpad {
bool CaptureSnapshot(
PtraceConnection* connection,
const ExceptionHandlerProtocol::ClientInformation& info,
const std::map<std::string, std::string>& process_annotations,
uid_t client_uid,
VMAddress requesting_thread_stack_address,
pid_t* requesting_thread_id,
std::unique_ptr<ProcessSnapshotLinux>* snapshot,
std::unique_ptr<ProcessSnapshotSanitized>* sanitized_snapshot) {
std::unique_ptr<ProcessSnapshotLinux> process_snapshot(
new ProcessSnapshotLinux());
if (!process_snapshot->Initialize(connection)) {
Metrics::ExceptionCaptureResult(Metrics::CaptureResult::kSnapshotFailed);
return false;
}
pid_t local_requesting_thread_id = -1;
if (requesting_thread_stack_address) {
local_requesting_thread_id = process_snapshot->FindThreadWithStackAddress(
requesting_thread_stack_address);
}
if (requesting_thread_id) {
*requesting_thread_id = local_requesting_thread_id;
}
if (!process_snapshot->InitializeException(info.exception_information_address,
local_requesting_thread_id)) {
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kExceptionInitializationFailed);
return false;
}
Metrics::ExceptionCode(process_snapshot->Exception()->Exception());
CrashpadInfoClientOptions client_options;
process_snapshot->GetCrashpadOptions(&client_options);
if (client_options.crashpad_handler_behavior == TriState::kDisabled) {
return false;
}
for (auto& p : process_annotations) {
process_snapshot->AddAnnotation(p.first, p.second);
}
if (info.sanitization_information_address) {
SanitizationInformation sanitization_info;
ProcessMemoryRange range;
if (!range.Initialize(connection->Memory(), connection->Is64Bit()) ||
!range.Read(info.sanitization_information_address,
sizeof(sanitization_info),
&sanitization_info)) {
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kSanitizationInitializationFailed);
return false;
}
auto annotations_whitelist = std::make_unique<std::vector<std::string>>();
auto memory_range_whitelist =
std::make_unique<std::vector<std::pair<VMAddress, VMAddress>>>();
if (!ReadAnnotationsWhitelist(
range,
sanitization_info.annotations_whitelist_address,
annotations_whitelist.get()) ||
!ReadMemoryRangeWhitelist(
range,
sanitization_info.memory_range_whitelist_address,
memory_range_whitelist.get())) {
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kSanitizationInitializationFailed);
return false;
}
std::unique_ptr<ProcessSnapshotSanitized> sanitized(
new ProcessSnapshotSanitized());
if (!sanitized->Initialize(process_snapshot.get(),
sanitization_info.annotations_whitelist_address
? std::move(annotations_whitelist)
: nullptr,
std::move(memory_range_whitelist),
sanitization_info.target_module_address,
sanitization_info.sanitize_stacks)) {
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kSkippedDueToSanitization);
return false;
}
*sanitized_snapshot = std::move(sanitized);
}
*snapshot = std::move(process_snapshot);
return true;
}
} // namespace crashpad

View File

@ -0,0 +1,67 @@
// Copyright 2019 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.
#ifndef CRASHPAD_HANDLER_LINUX_CAPTURE_SNAPSHOT_H_
#define CRASHPAD_HANDLER_LINUX_CAPTURE_SNAPSHOT_H_
#include <sys/types.h>
#include <map>
#include <memory>
#include <string>
#include "snapshot/linux/process_snapshot_linux.h"
#include "snapshot/sanitized/process_snapshot_sanitized.h"
#include "util/linux/exception_handler_protocol.h"
#include "util/linux/ptrace_connection.h"
#include "util/misc/address_types.h"
namespace crashpad {
//! \brief Captures a snapshot of a client over \a connection.
//!
//! \param[in] connection A PtraceConnection to the client to snapshot.
//! \param[in] info Information about the client configuring the snapshot.
//! \param[in] process_annotations A map of annotations to insert as
//! process-level annotations into the snapshot.
//! \param[in] client_uid The client's user ID.
//! \param[in] requesting_thread_stack_address An address on the stack of the
//! thread requesting the snapshot. If \a info includes an exception
//! address, the exception will be assigned to the thread whose stack
//! address range contains this address. If 0, \a requesting_thread_id will
//! be -1.
//! \param[out] requesting_thread_id The thread ID of the thread corresponding
//! to \a requesting_thread_stack_address. Set to -1 if the thread ID could
//! not be determined. Optional.
//! \param[out] process_snapshot A snapshot of the client process, valid if this
//! function returns `true`.
//! \param[out] sanitized_snapshot A sanitized snapshot of the client process,
//! valid if this function returns `true` and sanitization was requested in
//! \a info.
//! \return `true` if \a process_snapshot was successfully created. A message
//! will be logged on failure, but not if the snapshot was skipped because
//! handling was disabled by CrashpadInfoClientOptions.
bool CaptureSnapshot(
PtraceConnection* connection,
const ExceptionHandlerProtocol::ClientInformation& info,
const std::map<std::string, std::string>& process_annotations,
uid_t client_uid,
VMAddress requesting_thread_stack_address,
pid_t* requesting_thread_id,
std::unique_ptr<ProcessSnapshotLinux>* process_snapshot,
std::unique_ptr<ProcessSnapshotSanitized>* sanitized_snapshot);
} // namespace crashpad
#endif // CRASHPAD_HANDLER_LINUX_CAPTURE_SNAPSHOT_H_

View File

@ -14,19 +14,19 @@
#include "handler/linux/crash_report_exception_handler.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/logging.h"
#include "client/settings.h"
#include "handler/linux/capture_snapshot.h"
#include "minidump/minidump_file_writer.h"
#include "snapshot/crashpad_info_client_options.h"
#include "snapshot/linux/process_snapshot_linux.h"
#include "snapshot/sanitized/process_snapshot_sanitized.h"
#include "snapshot/sanitized/sanitization_information.h"
#include "util/linux/direct_ptrace_connection.h"
#include "util/linux/ptrace_client.h"
#include "util/misc/metrics.h"
#include "util/misc/tri_state.h"
#include "util/misc/uuid.h"
#if defined(OS_CHROMEOS)
@ -195,188 +195,126 @@ bool CrashReportExceptionHandler::HandleExceptionWithConnection(
VMAddress requesting_thread_stack_address,
pid_t* requesting_thread_id,
UUID* local_report_id) {
ProcessSnapshotLinux process_snapshot;
if (!process_snapshot.Initialize(connection)) {
Metrics::ExceptionCaptureResult(Metrics::CaptureResult::kSnapshotFailed);
std::unique_ptr<ProcessSnapshotLinux> process_snapshot;
std::unique_ptr<ProcessSnapshotSanitized> sanitized_snapshot;
if (!CaptureSnapshot(connection,
info,
*process_annotations_,
client_uid,
requesting_thread_stack_address,
requesting_thread_id,
&process_snapshot,
&sanitized_snapshot)) {
return false;
}
pid_t local_requesting_thread_id = -1;
if (requesting_thread_stack_address) {
local_requesting_thread_id = process_snapshot.FindThreadWithStackAddress(
requesting_thread_stack_address);
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);
}
process_snapshot->SetClientID(client_id);
if (requesting_thread_id) {
*requesting_thread_id = local_requesting_thread_id;
}
UUID uuid;
#if defined(OS_CHROMEOS)
uuid.InitializeWithNew();
process_snapshot->SetReportID(uuid);
#else
if (!process_snapshot.InitializeException(info.exception_information_address,
local_requesting_thread_id)) {
std::unique_ptr<CrashReportDatabase::NewReport> new_report;
CrashReportDatabase::OperationStatus database_status =
database_->PrepareNewCrashReport(&new_report);
if (database_status != CrashReportDatabase::kNoError) {
LOG(ERROR) << "PrepareNewCrashReport failed";
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kExceptionInitializationFailed);
Metrics::CaptureResult::kPrepareNewCrashReportFailed);
return false;
}
Metrics::ExceptionCode(process_snapshot.Exception()->Exception());
CrashpadInfoClientOptions client_options;
process_snapshot.GetCrashpadOptions(&client_options);
if (client_options.crashpad_handler_behavior != TriState::kDisabled) {
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);
}
process_snapshot.SetClientID(client_id);
for (auto& p : *process_annotations_) {
process_snapshot.AddAnnotation(p.first, p.second);
}
UUID uuid;
#if defined(OS_CHROMEOS)
uuid.InitializeWithNew();
process_snapshot.SetReportID(uuid);
#else
std::unique_ptr<CrashReportDatabase::NewReport> new_report;
CrashReportDatabase::OperationStatus database_status =
database_->PrepareNewCrashReport(&new_report);
if (database_status != CrashReportDatabase::kNoError) {
LOG(ERROR) << "PrepareNewCrashReport failed";
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kPrepareNewCrashReportFailed);
return false;
}
process_snapshot.SetReportID(new_report->ReportID());
process_snapshot->SetReportID(new_report->ReportID());
#endif
ProcessSnapshot* snapshot = nullptr;
ProcessSnapshotSanitized sanitized;
if (info.sanitization_information_address) {
SanitizationInformation sanitization_info;
ProcessMemoryRange range;
if (!range.Initialize(connection->Memory(), connection->Is64Bit()) ||
!range.Read(info.sanitization_information_address,
sizeof(sanitization_info),
&sanitization_info)) {
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kSanitizationInitializationFailed);
return false;
}
ProcessSnapshot* snapshot =
sanitized_snapshot
? implicit_cast<ProcessSnapshot*>(sanitized_snapshot.get())
: implicit_cast<ProcessSnapshot*>(process_snapshot.get());
auto annotations_whitelist = std::make_unique<std::vector<std::string>>();
auto memory_range_whitelist =
std::make_unique<std::vector<std::pair<VMAddress, VMAddress>>>();
if (!ReadAnnotationsWhitelist(
range,
sanitization_info.annotations_whitelist_address,
annotations_whitelist.get()) ||
!ReadMemoryRangeWhitelist(
range,
sanitization_info.memory_range_whitelist_address,
memory_range_whitelist.get())) {
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kSanitizationInitializationFailed);
return false;
}
if (!sanitized.Initialize(&process_snapshot,
sanitization_info.annotations_whitelist_address
? std::move(annotations_whitelist)
: nullptr,
std::move(memory_range_whitelist),
sanitization_info.target_module_address,
sanitization_info.sanitize_stacks)) {
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kSkippedDueToSanitization);
return true;
}
snapshot = &sanitized;
} else {
snapshot = &process_snapshot;
}
MinidumpFileWriter minidump;
minidump.InitializeFromSnapshot(snapshot);
AddUserExtensionStreams(user_stream_data_sources_, snapshot, &minidump);
MinidumpFileWriter minidump;
minidump.InitializeFromSnapshot(snapshot);
AddUserExtensionStreams(user_stream_data_sources_, snapshot, &minidump);
#if defined(OS_CHROMEOS)
FileWriter file_writer;
if (!file_writer.OpenMemfd(base::FilePath("/tmp/minidump"))) {
Metrics::ExceptionCaptureResult(Metrics::CaptureResult::kOpenMemfdFailed);
return false;
}
FileWriter file_writer;
if (!file_writer.OpenMemfd(base::FilePath("/tmp/minidump"))) {
Metrics::ExceptionCaptureResult(Metrics::CaptureResult::kOpenMemfdFailed);
return false;
}
std::map<std::string, std::string> parameters =
BreakpadHTTPFormParametersFromMinidump(snapshot);
// Used to differenciate between breakpad and crashpad while the switch is
// ramping up.
parameters.emplace("crash_library", "crashpad");
std::map<std::string, std::string> parameters =
BreakpadHTTPFormParametersFromMinidump(snapshot);
// Used to differenciate between breakpad and crashpad while the switch is
// ramping up.
parameters.emplace("crash_library", "crashpad");
if (!WriteAnnotationsAndMinidump(parameters, minidump, file_writer)) {
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kMinidumpWriteFailed);
return false;
}
if (!WriteAnnotationsAndMinidump(parameters, minidump, file_writer)) {
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kMinidumpWriteFailed);
return false;
}
// CrOS uses crash_reporter instead of Crashpad to report crashes.
// crash_reporter needs to know the pid and uid of the crashing process.
std::vector<std::string> argv({"/sbin/crash_reporter"});
// CrOS uses crash_reporter instead of Crashpad to report crashes.
// crash_reporter needs to know the pid and uid of the crashing process.
std::vector<std::string> argv({"/sbin/crash_reporter"});
argv.push_back("--chrome_memfd=" + std::to_string(file_writer.fd()));
argv.push_back("--pid=" + std::to_string(*requesting_thread_id));
argv.push_back("--uid=" + std::to_string(client_uid));
std::string process_name = GetProcessNameFromPid(*requesting_thread_id);
argv.push_back("--exe=" + (process_name.empty() ? "chrome" : process_name));
argv.push_back("--chrome_memfd=" + std::to_string(file_writer.fd()));
argv.push_back("--pid=" + std::to_string(*requesting_thread_id));
argv.push_back("--uid=" + std::to_string(client_uid));
std::string process_name = GetProcessNameFromPid(*requesting_thread_id);
argv.push_back("--exe=" + (process_name.empty() ? "chrome" : process_name));
if (info.crash_loop_before_time != 0) {
argv.push_back("--crash_loop_before=" +
std::to_string(info.crash_loop_before_time));
}
if (info.crash_loop_before_time != 0) {
argv.push_back("--crash_loop_before=" +
std::to_string(info.crash_loop_before_time));
}
if (!DoubleForkAndExec(argv,
nullptr /* envp */,
file_writer.fd() /* preserve_fd */,
false /* use_path */,
nullptr /* child_function */)) {
LOG(ERROR) << "DoubleForkAndExec failed";
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kFinishedWritingCrashReportFailed);
return false;
}
if (!DoubleForkAndExec(argv,
nullptr /* envp */,
file_writer.fd() /* preserve_fd */,
false /* use_path */,
nullptr /* child_function */)) {
LOG(ERROR) << "DoubleForkAndExec failed";
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kFinishedWritingCrashReportFailed);
return false;
}
#else
if (!minidump.WriteEverything(new_report->Writer())) {
LOG(ERROR) << "WriteEverything failed";
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kMinidumpWriteFailed);
return false;
}
if (!minidump.WriteEverything(new_report->Writer())) {
LOG(ERROR) << "WriteEverything failed";
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kMinidumpWriteFailed);
return false;
}
database_status =
database_->FinishedWritingCrashReport(std::move(new_report), &uuid);
if (database_status != CrashReportDatabase::kNoError) {
LOG(ERROR) << "FinishedWritingCrashReport failed";
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kFinishedWritingCrashReportFailed);
return false;
}
database_status =
database_->FinishedWritingCrashReport(std::move(new_report), &uuid);
if (database_status != CrashReportDatabase::kNoError) {
LOG(ERROR) << "FinishedWritingCrashReport failed";
Metrics::ExceptionCaptureResult(
Metrics::CaptureResult::kFinishedWritingCrashReportFailed);
return false;
}
if (upload_thread_) {
upload_thread_->ReportPending(uuid);
}
if (upload_thread_) {
upload_thread_->ReportPending(uuid);
}
#endif
if (local_report_id != nullptr) {
*local_report_id = uuid;
}
if (local_report_id != nullptr) {
*local_report_id = uuid;
}
Metrics::ExceptionCaptureResult(Metrics::CaptureResult::kSuccess);