2022-09-06 19:14:07 -04:00
|
|
|
|
// Copyright 2020 The Crashpad Authors
|
2020-02-18 14:49:10 -05:00
|
|
|
|
//
|
|
|
|
|
// 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 "client/crashpad_client.h"
|
|
|
|
|
|
2022-03-22 14:02:22 -04:00
|
|
|
|
#include <signal.h>
|
2020-02-18 14:49:10 -05:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2022-05-24 10:26:22 -06:00
|
|
|
|
#include <atomic>
|
2020-04-25 17:16:10 -04:00
|
|
|
|
#include <ios>
|
2022-02-28 20:57:19 -08:00
|
|
|
|
#include <iterator>
|
2020-04-25 17:16:10 -04:00
|
|
|
|
|
2023-08-16 16:30:40 -04:00
|
|
|
|
#include "base/apple/mach_logging.h"
|
|
|
|
|
#include "base/apple/scoped_mach_port.h"
|
2020-02-18 14:49:10 -05:00
|
|
|
|
#include "base/logging.h"
|
2021-08-11 12:40:54 -04:00
|
|
|
|
#include "client/ios_handler/exception_processor.h"
|
2021-10-14 10:55:03 -04:00
|
|
|
|
#include "client/ios_handler/in_process_handler.h"
|
2022-03-22 14:02:22 -04:00
|
|
|
|
#include "util/ios/raw_logging.h"
|
2020-04-25 17:16:10 -04:00
|
|
|
|
#include "util/mach/exc_server_variants.h"
|
|
|
|
|
#include "util/mach/exception_ports.h"
|
|
|
|
|
#include "util/mach/mach_extensions.h"
|
|
|
|
|
#include "util/mach/mach_message.h"
|
|
|
|
|
#include "util/mach/mach_message_server.h"
|
|
|
|
|
#include "util/misc/initialization_state_dcheck.h"
|
2020-02-18 14:49:10 -05:00
|
|
|
|
#include "util/posix/signals.h"
|
2020-04-25 17:16:10 -04:00
|
|
|
|
#include "util/thread/thread.h"
|
2020-02-18 14:49:10 -05:00
|
|
|
|
|
2021-10-14 10:55:03 -04:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
bool IsBeingDebugged() {
|
|
|
|
|
kinfo_proc kern_proc_info;
|
|
|
|
|
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
|
|
|
|
|
size_t len = sizeof(kern_proc_info);
|
2022-02-28 20:57:19 -08:00
|
|
|
|
if (sysctl(mib, std::size(mib), &kern_proc_info, &len, nullptr, 0) == 0)
|
2021-10-14 10:55:03 -04:00
|
|
|
|
return kern_proc_info.kp_proc.p_flag & P_TRACED;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2020-02-18 14:49:10 -05:00
|
|
|
|
namespace crashpad {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2024-04-01 12:44:31 -06:00
|
|
|
|
// Thread-safe version of `base::apple::ScopedMachReceiveRight` which allocates
|
|
|
|
|
// the Mach port upon construction and deallocates it upon destruction.
|
|
|
|
|
class ThreadSafeScopedMachPortWithReceiveRight {
|
|
|
|
|
public:
|
|
|
|
|
ThreadSafeScopedMachPortWithReceiveRight()
|
|
|
|
|
: port_(NewMachPort(MACH_PORT_RIGHT_RECEIVE)) {}
|
|
|
|
|
|
|
|
|
|
ThreadSafeScopedMachPortWithReceiveRight(
|
|
|
|
|
const ThreadSafeScopedMachPortWithReceiveRight&) = delete;
|
|
|
|
|
ThreadSafeScopedMachPortWithReceiveRight& operator=(
|
|
|
|
|
const ThreadSafeScopedMachPortWithReceiveRight&) = delete;
|
|
|
|
|
|
|
|
|
|
~ThreadSafeScopedMachPortWithReceiveRight() { reset(); }
|
|
|
|
|
|
|
|
|
|
mach_port_t get() { return port_.load(); }
|
|
|
|
|
void reset() {
|
|
|
|
|
mach_port_t old_port = port_.exchange(MACH_PORT_NULL);
|
|
|
|
|
if (old_port == MACH_PORT_NULL) {
|
|
|
|
|
// Already reset, nothing to do.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
kern_return_t kr = mach_port_mod_refs(
|
|
|
|
|
mach_task_self(), old_port, MACH_PORT_RIGHT_RECEIVE, -1);
|
|
|
|
|
MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr)
|
|
|
|
|
<< "ThreadSafeScopedMachPortWithReceiveRight mach_port_mod_refs";
|
|
|
|
|
kr = mach_port_deallocate(mach_task_self(), old_port);
|
|
|
|
|
MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr)
|
|
|
|
|
<< "ThreadSafeScopedMachPortWithReceiveRight mach_port_deallocate";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::atomic<mach_port_t> port_;
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-25 17:16:10 -04:00
|
|
|
|
// A base class for signal handler and Mach exception server.
|
2021-08-11 12:40:54 -04:00
|
|
|
|
class CrashHandler : public Thread,
|
|
|
|
|
public UniversalMachExcServer::Interface,
|
|
|
|
|
public ObjcExceptionDelegate {
|
2020-02-18 14:49:10 -05:00
|
|
|
|
public:
|
2021-09-20 12:55:12 -07:00
|
|
|
|
CrashHandler(const CrashHandler&) = delete;
|
|
|
|
|
CrashHandler& operator=(const CrashHandler&) = delete;
|
|
|
|
|
|
2020-04-25 17:16:10 -04:00
|
|
|
|
static CrashHandler* Get() {
|
2022-02-02 12:24:47 -05:00
|
|
|
|
if (!instance_)
|
|
|
|
|
instance_ = new CrashHandler();
|
|
|
|
|
return instance_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ResetForTesting() {
|
|
|
|
|
delete instance_;
|
|
|
|
|
instance_ = nullptr;
|
2020-02-18 14:49:10 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 15:25:08 -06:00
|
|
|
|
bool Initialize(
|
|
|
|
|
const base::FilePath& database,
|
|
|
|
|
const std::string& url,
|
|
|
|
|
const std::map<std::string, std::string>& annotations,
|
|
|
|
|
internal::InProcessHandler::ProcessPendingReportsObservationCallback
|
|
|
|
|
callback) {
|
2020-04-25 17:16:10 -04:00
|
|
|
|
INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
|
2022-09-09 15:25:08 -06:00
|
|
|
|
if (!in_process_handler_.Initialize(database, url, annotations, callback) ||
|
2021-10-14 10:55:03 -04:00
|
|
|
|
!InstallMachExceptionHandler() ||
|
2022-01-20 13:26:49 -05:00
|
|
|
|
// xnu turns hardware faults into Mach exceptions, so the only signal
|
|
|
|
|
// left to register is SIGABRT, which never starts off as a hardware
|
|
|
|
|
// fault. Installing a handler for other signals would lead to
|
|
|
|
|
// recording exceptions twice. As a consequence, Crashpad will not
|
|
|
|
|
// generate intermediate dumps for anything manually calling
|
|
|
|
|
// raise(SIG*). In practice, this doesn’t actually happen for crash
|
|
|
|
|
// signals that originate as hardware faults.
|
2022-03-08 18:20:41 -05:00
|
|
|
|
!Signals::InstallHandler(
|
|
|
|
|
SIGABRT, CatchAndReraiseSignal, 0, &old_action_)) {
|
2021-10-14 10:55:03 -04:00
|
|
|
|
LOG(ERROR) << "Unable to initialize Crashpad.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-03-08 18:20:41 -05:00
|
|
|
|
|
|
|
|
|
// For applications that haven't ignored or set a handler for SIGPIPE:
|
|
|
|
|
// It’s OK for an application to set its own SIGPIPE handler (including
|
|
|
|
|
// SIG_IGN) before initializing Crashpad, because Crashpad will discover the
|
|
|
|
|
// existing handler and not install its own.
|
|
|
|
|
// It’s OK for Crashpad to install its own SIGPIPE handler and for the
|
|
|
|
|
// application to subsequently install its own (including SIG_IGN)
|
|
|
|
|
// afterwards, because its handler will replace Crashpad’s.
|
|
|
|
|
// This is useful to cover the default situation where nobody installs a
|
|
|
|
|
// SIGPIPE handler and the disposition is at SIG_DFL, because SIGPIPE is a
|
|
|
|
|
// “kill” signal (bsd/sys/signalvar.h sigprop). In that case, without
|
|
|
|
|
// Crashpad, SIGPIPE results in a silent and unreported kill (and not even
|
|
|
|
|
// ReportCrash will record it), but developers probably want to be alerted
|
|
|
|
|
// to the conditon.
|
|
|
|
|
struct sigaction sa;
|
|
|
|
|
if (sigaction(SIGPIPE, nullptr, &sa) == 0 && sa.sa_handler == SIG_DFL) {
|
|
|
|
|
Signals::InstallHandler(
|
|
|
|
|
SIGPIPE, CatchAndReraiseSignalDefaultAction, 0, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 17:08:41 -04:00
|
|
|
|
InstallObjcExceptionPreprocessor(this);
|
2020-04-25 17:16:10 -04:00
|
|
|
|
INITIALIZATION_STATE_SET_VALID(initialized_);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
return true;
|
2020-02-18 14:49:10 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 23:20:30 -05:00
|
|
|
|
void ProcessIntermediateDumps(
|
2021-10-14 10:55:03 -04:00
|
|
|
|
const std::map<std::string, std::string>& annotations) {
|
|
|
|
|
in_process_handler_.ProcessIntermediateDumps(annotations);
|
|
|
|
|
}
|
2021-02-03 23:20:30 -05:00
|
|
|
|
|
2021-07-14 20:28:17 -04:00
|
|
|
|
void ProcessIntermediateDump(
|
|
|
|
|
const base::FilePath& file,
|
2021-10-14 10:55:03 -04:00
|
|
|
|
const std::map<std::string, std::string>& annotations) {
|
|
|
|
|
in_process_handler_.ProcessIntermediateDump(file, annotations);
|
|
|
|
|
}
|
2021-07-14 20:28:17 -04:00
|
|
|
|
|
2021-10-14 10:55:03 -04:00
|
|
|
|
void DumpWithoutCrash(NativeCPUContext* context, bool process_dump) {
|
|
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
2022-01-20 10:04:00 -07:00
|
|
|
|
base::FilePath path;
|
2022-03-24 19:43:01 -04:00
|
|
|
|
if (!in_process_handler_.DumpExceptionFromSimulatedMachException(
|
|
|
|
|
context, kMachExceptionSimulated, &path)) {
|
2022-03-08 18:20:41 -05:00
|
|
|
|
return;
|
2022-01-20 10:04:00 -07:00
|
|
|
|
}
|
2022-03-08 18:20:41 -05:00
|
|
|
|
|
2022-01-20 10:04:00 -07:00
|
|
|
|
if (process_dump) {
|
|
|
|
|
in_process_handler_.ProcessIntermediateDump(path);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DumpWithoutCrashAtPath(NativeCPUContext* context,
|
|
|
|
|
const base::FilePath& path) {
|
2022-03-24 19:43:01 -04:00
|
|
|
|
in_process_handler_.DumpExceptionFromSimulatedMachExceptionAtPath(
|
|
|
|
|
context, kMachExceptionSimulated, path);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 13:14:19 -06:00
|
|
|
|
void StartProcessingPendingReports(UploadBehavior upload_behavior) {
|
2021-10-14 10:55:03 -04:00
|
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
2022-09-15 13:14:19 -06:00
|
|
|
|
in_process_handler_.StartProcessingPendingReports(upload_behavior);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-08 18:20:41 -05:00
|
|
|
|
void SetMachExceptionCallbackForTesting(void (*callback)()) {
|
|
|
|
|
in_process_handler_.SetMachExceptionCallbackForTesting(callback);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-08 20:09:09 -05:00
|
|
|
|
uint64_t GetThreadIdForTesting() { return Thread::GetThreadIdForTesting(); }
|
|
|
|
|
|
2020-02-18 14:49:10 -05:00
|
|
|
|
private:
|
2020-04-25 17:16:10 -04:00
|
|
|
|
CrashHandler() = default;
|
|
|
|
|
|
2022-02-02 12:24:47 -05:00
|
|
|
|
~CrashHandler() {
|
|
|
|
|
UninstallObjcExceptionPreprocessor();
|
|
|
|
|
Signals::InstallDefaultHandler(SIGABRT);
|
|
|
|
|
UninstallMachExceptionHandler();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 10:55:03 -04:00
|
|
|
|
bool InstallMachExceptionHandler() {
|
2024-04-01 12:44:31 -06:00
|
|
|
|
mach_port_t exception_port = exception_port_.get();
|
|
|
|
|
if (exception_port == MACH_PORT_NULL) {
|
2021-10-14 10:55:03 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-04-25 17:16:10 -04:00
|
|
|
|
|
|
|
|
|
kern_return_t kr = mach_port_insert_right(mach_task_self(),
|
2024-04-01 12:44:31 -06:00
|
|
|
|
exception_port,
|
|
|
|
|
exception_port,
|
2020-04-25 17:16:10 -04:00
|
|
|
|
MACH_MSG_TYPE_MAKE_SEND);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
if (kr != KERN_SUCCESS) {
|
|
|
|
|
MACH_LOG(ERROR, kr) << "mach_port_insert_right";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-04-25 17:16:10 -04:00
|
|
|
|
|
|
|
|
|
// TODO: Use SwapExceptionPort instead and put back EXC_MASK_BREAKPOINT.
|
2021-10-14 10:55:03 -04:00
|
|
|
|
// Until then, remove |EXC_MASK_BREAKPOINT| while attached to a debugger.
|
|
|
|
|
exception_mask_t mask =
|
2020-04-25 17:16:10 -04:00
|
|
|
|
ExcMaskAll() &
|
2021-10-14 10:55:03 -04:00
|
|
|
|
~(EXC_MASK_EMULATION | EXC_MASK_SOFTWARE | EXC_MASK_RPC_ALERT |
|
|
|
|
|
EXC_MASK_GUARD | (IsBeingDebugged() ? EXC_MASK_BREAKPOINT : 0));
|
|
|
|
|
|
2020-04-25 17:16:10 -04:00
|
|
|
|
ExceptionPorts exception_ports(ExceptionPorts::kTargetTypeTask, TASK_NULL);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
if (!exception_ports.GetExceptionPorts(mask, &original_handlers_) ||
|
|
|
|
|
!exception_ports.SetExceptionPort(
|
|
|
|
|
mask,
|
2024-04-01 12:44:31 -06:00
|
|
|
|
exception_port,
|
2021-10-14 10:55:03 -04:00
|
|
|
|
EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES,
|
|
|
|
|
MACHINE_THREAD_STATE)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-04-25 17:16:10 -04:00
|
|
|
|
|
2022-02-02 12:24:47 -05:00
|
|
|
|
mach_handler_running_ = true;
|
2020-04-25 17:16:10 -04:00
|
|
|
|
Start();
|
2021-10-14 10:55:03 -04:00
|
|
|
|
return true;
|
2020-04-25 17:16:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 12:24:47 -05:00
|
|
|
|
void UninstallMachExceptionHandler() {
|
|
|
|
|
mach_handler_running_ = false;
|
|
|
|
|
exception_port_.reset();
|
|
|
|
|
Join();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 17:16:10 -04:00
|
|
|
|
// Thread:
|
|
|
|
|
|
|
|
|
|
void ThreadMain() override {
|
|
|
|
|
UniversalMachExcServer universal_mach_exc_server(this);
|
2022-02-02 12:24:47 -05:00
|
|
|
|
while (mach_handler_running_) {
|
2020-04-25 17:16:10 -04:00
|
|
|
|
mach_msg_return_t mr =
|
|
|
|
|
MachMessageServer::Run(&universal_mach_exc_server,
|
|
|
|
|
exception_port_.get(),
|
|
|
|
|
MACH_MSG_OPTION_NONE,
|
|
|
|
|
MachMessageServer::kPersistent,
|
|
|
|
|
MachMessageServer::kReceiveLargeIgnore,
|
|
|
|
|
kMachMessageTimeoutWaitIndefinitely);
|
2022-05-24 10:26:22 -06:00
|
|
|
|
MACH_CHECK(
|
|
|
|
|
mach_handler_running_
|
|
|
|
|
? mr == MACH_SEND_INVALID_DEST // This shouldn't happen for
|
|
|
|
|
// exception messages that come
|
|
|
|
|
// from the kernel itself, but if
|
|
|
|
|
// something else in-process sends
|
|
|
|
|
// exception messages and breaks,
|
|
|
|
|
// handle that case.
|
|
|
|
|
: (mr == MACH_RCV_PORT_CHANGED || // Port was closed while the
|
|
|
|
|
// thread was listening.
|
|
|
|
|
mr == MACH_RCV_INVALID_NAME), // Port was closed before the
|
|
|
|
|
// thread started listening.
|
|
|
|
|
mr)
|
2022-02-02 12:24:47 -05:00
|
|
|
|
<< "MachMessageServer::Run";
|
2020-04-25 17:16:10 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UniversalMachExcServer::Interface:
|
|
|
|
|
|
|
|
|
|
kern_return_t CatchMachException(exception_behavior_t behavior,
|
|
|
|
|
exception_handler_t exception_port,
|
|
|
|
|
thread_t thread,
|
|
|
|
|
task_t task,
|
|
|
|
|
exception_type_t exception,
|
|
|
|
|
const mach_exception_data_type_t* code,
|
|
|
|
|
mach_msg_type_number_t code_count,
|
|
|
|
|
thread_state_flavor_t* flavor,
|
|
|
|
|
ConstThreadState old_state,
|
|
|
|
|
mach_msg_type_number_t old_state_count,
|
|
|
|
|
thread_state_t new_state,
|
|
|
|
|
mach_msg_type_number_t* new_state_count,
|
|
|
|
|
const mach_msg_trailer_t* trailer,
|
|
|
|
|
bool* destroy_complex_request) override {
|
|
|
|
|
*destroy_complex_request = true;
|
|
|
|
|
|
|
|
|
|
// TODO(justincohen): Forward exceptions to original_handlers_ with
|
|
|
|
|
// UniversalExceptionRaise.
|
|
|
|
|
|
|
|
|
|
// iOS shouldn't have any child processes, but just in case, those will
|
|
|
|
|
// inherit the task exception ports, and this process isn’t prepared to
|
|
|
|
|
// handle them
|
|
|
|
|
if (task != mach_task_self()) {
|
2022-03-22 14:02:22 -04:00
|
|
|
|
CRASHPAD_RAW_LOG("MachException task != mach_task_self()");
|
2020-04-25 17:16:10 -04:00
|
|
|
|
return KERN_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HandleMachException(behavior,
|
|
|
|
|
thread,
|
|
|
|
|
exception,
|
|
|
|
|
code,
|
|
|
|
|
code_count,
|
|
|
|
|
*flavor,
|
|
|
|
|
old_state,
|
|
|
|
|
old_state_count);
|
|
|
|
|
|
|
|
|
|
// Respond with KERN_FAILURE so the system will continue to handle this
|
2022-03-22 14:02:22 -04:00
|
|
|
|
// exception. xnu will turn this Mach exception into a signal and take the
|
|
|
|
|
// default action to terminate the process. However, if sigprocmask is
|
|
|
|
|
// called before this Mach exception returns (such as by another thread
|
|
|
|
|
// calling abort, see: Libc-1506.40.4/stdlib/FreeBSD/abort.c), the Mach
|
|
|
|
|
// exception will be converted into a signal but delivery will be blocked.
|
|
|
|
|
// Since concurrent exceptions lead to the losing thread sleeping
|
|
|
|
|
// indefinitely, if the abort thread never returns, the thread that
|
|
|
|
|
// triggered this Mach exception will repeatedly trap and the process will
|
|
|
|
|
// never terminate. If the abort thread didn’t have a user-space signal
|
|
|
|
|
// handler that slept forever, the abort would terminate the process even if
|
|
|
|
|
// all other signals had been blocked. Instead, unblock all signals
|
|
|
|
|
// corresponding to all Mach exceptions Crashpad is registered for before
|
|
|
|
|
// returning KERN_FAILURE. There is still racy behavior possible with this
|
|
|
|
|
// call to sigprocmask, but the repeated calls to CatchMachException here
|
|
|
|
|
// will eventually lead to termination.
|
|
|
|
|
sigset_t unblock_set;
|
|
|
|
|
sigemptyset(&unblock_set);
|
|
|
|
|
sigaddset(&unblock_set, SIGILL); // EXC_BAD_INSTRUCTION
|
|
|
|
|
sigaddset(&unblock_set, SIGTRAP); // EXC_BREAKPOINT
|
|
|
|
|
sigaddset(&unblock_set, SIGFPE); // EXC_ARITHMETIC
|
|
|
|
|
sigaddset(&unblock_set, SIGBUS); // EXC_BAD_ACCESS
|
|
|
|
|
sigaddset(&unblock_set, SIGSEGV); // EXC_BAD_ACCESS
|
|
|
|
|
if (sigprocmask(SIG_UNBLOCK, &unblock_set, nullptr) != 0) {
|
|
|
|
|
CRASHPAD_RAW_LOG("sigprocmask");
|
|
|
|
|
}
|
2020-04-25 17:16:10 -04:00
|
|
|
|
return KERN_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandleMachException(exception_behavior_t behavior,
|
|
|
|
|
thread_t thread,
|
|
|
|
|
exception_type_t exception,
|
|
|
|
|
const mach_exception_data_type_t* code,
|
|
|
|
|
mach_msg_type_number_t code_count,
|
|
|
|
|
thread_state_flavor_t flavor,
|
|
|
|
|
ConstThreadState old_state,
|
|
|
|
|
mach_msg_type_number_t old_state_count) {
|
2022-03-10 14:12:57 -05:00
|
|
|
|
in_process_handler_.DumpExceptionFromMachException(behavior,
|
2021-10-14 10:55:03 -04:00
|
|
|
|
thread,
|
|
|
|
|
exception,
|
|
|
|
|
code,
|
|
|
|
|
code_count,
|
|
|
|
|
flavor,
|
|
|
|
|
old_state,
|
|
|
|
|
old_state_count);
|
2020-04-25 17:16:10 -04:00
|
|
|
|
}
|
2020-03-25 16:12:22 -04:00
|
|
|
|
|
2021-08-11 12:40:54 -04:00
|
|
|
|
void HandleUncaughtNSException(const uint64_t* frames,
|
|
|
|
|
const size_t num_frames) override {
|
2022-03-10 14:12:57 -05:00
|
|
|
|
in_process_handler_.DumpExceptionFromNSExceptionWithFrames(frames,
|
|
|
|
|
num_frames);
|
2021-08-11 12:40:54 -04:00
|
|
|
|
// After uncaught exceptions are reported, the system immediately triggers a
|
|
|
|
|
// call to std::terminate()/abort(). Remove the abort handler so a second
|
|
|
|
|
// dump isn't generated.
|
|
|
|
|
CHECK(Signals::InstallDefaultHandler(SIGABRT));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandleUncaughtNSExceptionWithContext(
|
|
|
|
|
NativeCPUContext* context) override {
|
2022-03-24 19:43:01 -04:00
|
|
|
|
base::FilePath path;
|
|
|
|
|
in_process_handler_.DumpExceptionFromSimulatedMachException(
|
|
|
|
|
context, kMachExceptionFromNSException, &path);
|
2021-08-11 12:40:54 -04:00
|
|
|
|
|
|
|
|
|
// After uncaught exceptions are reported, the system immediately triggers a
|
|
|
|
|
// call to std::terminate()/abort(). Remove the abort handler so a second
|
|
|
|
|
// dump isn't generated.
|
|
|
|
|
CHECK(Signals::InstallDefaultHandler(SIGABRT));
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-24 19:43:01 -04:00
|
|
|
|
void HandleUncaughtNSExceptionWithContextAtPath(
|
|
|
|
|
NativeCPUContext* context,
|
|
|
|
|
const base::FilePath& path) override {
|
|
|
|
|
in_process_handler_.DumpExceptionFromSimulatedMachExceptionAtPath(
|
|
|
|
|
context, kMachExceptionFromNSException, path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MoveIntermediateDumpAtPathToPending(
|
|
|
|
|
const base::FilePath& path) override {
|
|
|
|
|
if (in_process_handler_.MoveIntermediateDumpAtPathToPending(path)) {
|
|
|
|
|
// After uncaught exceptions are reported, the system immediately triggers
|
|
|
|
|
// a call to std::terminate()/abort(). Remove the abort handler so a
|
|
|
|
|
// second dump isn't generated.
|
|
|
|
|
CHECK(Signals::InstallDefaultHandler(SIGABRT));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 10:55:03 -04:00
|
|
|
|
// The signal handler installed at OS-level.
|
2022-03-08 18:20:41 -05:00
|
|
|
|
static void CatchAndReraiseSignal(int signo,
|
|
|
|
|
siginfo_t* siginfo,
|
|
|
|
|
void* context) {
|
|
|
|
|
Get()->HandleAndReraiseSignal(signo,
|
|
|
|
|
siginfo,
|
|
|
|
|
reinterpret_cast<ucontext_t*>(context),
|
|
|
|
|
&(Get()->old_action_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void CatchAndReraiseSignalDefaultAction(int signo,
|
|
|
|
|
siginfo_t* siginfo,
|
|
|
|
|
void* context) {
|
2021-10-14 10:55:03 -04:00
|
|
|
|
Get()->HandleAndReraiseSignal(
|
2022-03-08 18:20:41 -05:00
|
|
|
|
signo, siginfo, reinterpret_cast<ucontext_t*>(context), nullptr);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandleAndReraiseSignal(int signo,
|
|
|
|
|
siginfo_t* siginfo,
|
2022-03-08 18:20:41 -05:00
|
|
|
|
ucontext_t* context,
|
|
|
|
|
struct sigaction* old_action) {
|
2022-03-10 14:12:57 -05:00
|
|
|
|
in_process_handler_.DumpExceptionFromSignal(siginfo, context);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
|
|
|
|
|
// Always call system handler.
|
2022-03-08 18:20:41 -05:00
|
|
|
|
Signals::RestoreHandlerAndReraiseSignalOnReturn(siginfo, old_action);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 12:44:31 -06:00
|
|
|
|
ThreadSafeScopedMachPortWithReceiveRight exception_port_;
|
2020-04-25 17:16:10 -04:00
|
|
|
|
ExceptionPorts::ExceptionHandlerVector original_handlers_;
|
|
|
|
|
struct sigaction old_action_ = {};
|
2021-10-14 10:55:03 -04:00
|
|
|
|
internal::InProcessHandler in_process_handler_;
|
2022-02-02 12:24:47 -05:00
|
|
|
|
static CrashHandler* instance_;
|
2022-05-24 10:26:22 -06:00
|
|
|
|
std::atomic<bool> mach_handler_running_ = false;
|
2020-04-25 17:16:10 -04:00
|
|
|
|
InitializationStateDcheck initialized_;
|
2020-02-18 14:49:10 -05:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-02 12:24:47 -05:00
|
|
|
|
CrashHandler* CrashHandler::instance_ = nullptr;
|
|
|
|
|
|
2020-02-18 14:49:10 -05:00
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
CrashpadClient::CrashpadClient() {}
|
|
|
|
|
|
|
|
|
|
CrashpadClient::~CrashpadClient() {}
|
|
|
|
|
|
2021-02-03 23:20:30 -05:00
|
|
|
|
// static
|
2021-10-14 10:55:03 -04:00
|
|
|
|
bool CrashpadClient::StartCrashpadInProcessHandler(
|
2021-02-02 09:02:28 -05:00
|
|
|
|
const base::FilePath& database,
|
|
|
|
|
const std::string& url,
|
2022-09-09 15:25:08 -06:00
|
|
|
|
const std::map<std::string, std::string>& annotations,
|
|
|
|
|
ProcessPendingReportsObservationCallback callback) {
|
2020-04-25 17:16:10 -04:00
|
|
|
|
CrashHandler* crash_handler = CrashHandler::Get();
|
|
|
|
|
DCHECK(crash_handler);
|
2022-09-09 15:25:08 -06:00
|
|
|
|
return crash_handler->Initialize(database, url, annotations, callback);
|
2020-02-18 14:49:10 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 23:20:30 -05:00
|
|
|
|
// static
|
|
|
|
|
void CrashpadClient::ProcessIntermediateDumps(
|
|
|
|
|
const std::map<std::string, std::string>& annotations) {
|
|
|
|
|
CrashHandler* crash_handler = CrashHandler::Get();
|
|
|
|
|
DCHECK(crash_handler);
|
|
|
|
|
crash_handler->ProcessIntermediateDumps(annotations);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 20:28:17 -04:00
|
|
|
|
// static
|
|
|
|
|
void CrashpadClient::ProcessIntermediateDump(
|
|
|
|
|
const base::FilePath& file,
|
|
|
|
|
const std::map<std::string, std::string>& annotations) {
|
|
|
|
|
CrashHandler* crash_handler = CrashHandler::Get();
|
|
|
|
|
DCHECK(crash_handler);
|
|
|
|
|
crash_handler->ProcessIntermediateDump(file, annotations);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 22:38:31 -05:00
|
|
|
|
// static
|
2022-09-15 13:14:19 -06:00
|
|
|
|
void CrashpadClient::StartProcessingPendingReports(
|
|
|
|
|
UploadBehavior upload_behavior) {
|
2021-10-14 10:55:03 -04:00
|
|
|
|
CrashHandler* crash_handler = CrashHandler::Get();
|
|
|
|
|
DCHECK(crash_handler);
|
2022-09-15 13:14:19 -06:00
|
|
|
|
crash_handler->StartProcessingPendingReports(upload_behavior);
|
2021-02-16 22:38:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 15:19:46 -05:00
|
|
|
|
// static
|
2020-04-25 17:16:10 -04:00
|
|
|
|
void CrashpadClient::DumpWithoutCrash(NativeCPUContext* context) {
|
|
|
|
|
CrashHandler* crash_handler = CrashHandler::Get();
|
|
|
|
|
DCHECK(crash_handler);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
crash_handler->DumpWithoutCrash(context, /*process_dump=*/true);
|
2020-03-04 15:19:46 -05:00
|
|
|
|
}
|
2020-04-08 15:37:55 -04:00
|
|
|
|
|
2021-03-24 18:22:41 -04:00
|
|
|
|
// static
|
|
|
|
|
void CrashpadClient::DumpWithoutCrashAndDeferProcessing(
|
|
|
|
|
NativeCPUContext* context) {
|
|
|
|
|
CrashHandler* crash_handler = CrashHandler::Get();
|
|
|
|
|
DCHECK(crash_handler);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
crash_handler->DumpWithoutCrash(context, /*process_dump=*/false);
|
2021-03-24 18:22:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 20:28:17 -04:00
|
|
|
|
// static
|
|
|
|
|
void CrashpadClient::DumpWithoutCrashAndDeferProcessingAtPath(
|
|
|
|
|
NativeCPUContext* context,
|
|
|
|
|
const base::FilePath path) {
|
|
|
|
|
CrashHandler* crash_handler = CrashHandler::Get();
|
|
|
|
|
DCHECK(crash_handler);
|
2021-10-14 10:55:03 -04:00
|
|
|
|
crash_handler->DumpWithoutCrashAtPath(context, path);
|
2021-07-14 20:28:17 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 12:24:47 -05:00
|
|
|
|
void CrashpadClient::ResetForTesting() {
|
|
|
|
|
CrashHandler* crash_handler = CrashHandler::Get();
|
|
|
|
|
DCHECK(crash_handler);
|
|
|
|
|
crash_handler->ResetForTesting();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-08 18:20:41 -05:00
|
|
|
|
void CrashpadClient::SetMachExceptionCallbackForTesting(void (*callback)()) {
|
|
|
|
|
CrashHandler* crash_handler = CrashHandler::Get();
|
|
|
|
|
DCHECK(crash_handler);
|
|
|
|
|
crash_handler->SetMachExceptionCallbackForTesting(callback);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-08 20:09:09 -05:00
|
|
|
|
uint64_t CrashpadClient::GetThreadIdForTesting() {
|
|
|
|
|
CrashHandler* crash_handler = CrashHandler::Get();
|
|
|
|
|
DCHECK(crash_handler);
|
|
|
|
|
return crash_handler->GetThreadIdForTesting();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 14:49:10 -05:00
|
|
|
|
} // namespace crashpad
|