Path: blob/main/SignalServiceKit/Debugging/DebuggerUtils.m
1 views
//1// Copyright 2024 Signal Messenger, LLC2// SPDX-License-Identifier: AGPL-3.0-only3//45#import "DebuggerUtils.h"67#ifdef DEBUG89#include <sys/sysctl.h>10#include <sys/types.h>11#include <unistd.h>1213// Inspired by https://developer.apple.com/library/archive/qa/qa1361/_index.html.14BOOL IsDebuggerAttached(void)15{16int name[4] = {17CTL_KERN,18KERN_PROC,19KERN_PROC_PID, // Requesting info about a specific process20getpid(), // And that process is this one.21};2223struct kinfo_proc old = { 0 };24size_t oldlen = sizeof(old);25const int rc = sysctl(name, sizeof(name) / sizeof(*name), &old, &oldlen, NULL /* newp */, 0 /* newlen */);2627if (rc != 0) {28// There's no good reason for this to happen.29return NO;30}3132return (old.kp_proc.p_flag & P_TRACED) != 0;33}3435void TrapDebugger(void)36{37// __builtin_debugtrap doesn't respect lldb's breakpoints enabled setting.38// To temporarily disable this "breakpoint" set enabled to NO.39static BOOL enabled = YES;40if (!enabled) {41return;42}4344__builtin_debugtrap();45}4647#endif // DEBUG484950