Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
signalapp
GitHub Repository: signalapp/Signal-iOS
Path: blob/main/SignalServiceKit/Debugging/DebuggerUtils.m
1 views
1
//
2
// Copyright 2024 Signal Messenger, LLC
3
// SPDX-License-Identifier: AGPL-3.0-only
4
//
5
6
#import "DebuggerUtils.h"
7
8
#ifdef DEBUG
9
10
#include <sys/sysctl.h>
11
#include <sys/types.h>
12
#include <unistd.h>
13
14
// Inspired by https://developer.apple.com/library/archive/qa/qa1361/_index.html.
15
BOOL IsDebuggerAttached(void)
16
{
17
int name[4] = {
18
CTL_KERN,
19
KERN_PROC,
20
KERN_PROC_PID, // Requesting info about a specific process
21
getpid(), // And that process is this one.
22
};
23
24
struct kinfo_proc old = { 0 };
25
size_t oldlen = sizeof(old);
26
const int rc = sysctl(name, sizeof(name) / sizeof(*name), &old, &oldlen, NULL /* newp */, 0 /* newlen */);
27
28
if (rc != 0) {
29
// There's no good reason for this to happen.
30
return NO;
31
}
32
33
return (old.kp_proc.p_flag & P_TRACED) != 0;
34
}
35
36
void TrapDebugger(void)
37
{
38
// __builtin_debugtrap doesn't respect lldb's breakpoints enabled setting.
39
// To temporarily disable this "breakpoint" set enabled to NO.
40
static BOOL enabled = YES;
41
if (!enabled) {
42
return;
43
}
44
45
__builtin_debugtrap();
46
}
47
48
#endif // DEBUG
49
50