How do I determine if I'm being run under the debugger?

#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h>

static bool AmIBeingDebugged(void)
// Returns true if the current process is being debugged (either
// running under the debugger or has a debugger attached post facto).
{
int junk;
int mib[4];
struct kinfo_proc info;
size_t size;

// Initialize the flags so that, if sysctl fails for some bizarre
// reason, we get a predictable result.

info.kp_proc.p_flag = 0;

// Initialize mib, which tells sysctl the info we want, in this case
// we‘re looking for information about a specific process ID.

mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();

// Call sysctl.

size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
assert(junk == 0);

// We‘re being debugged if the P_TRACED flag is set.

return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}

  1. Important: Because the definition of the kinfo_proc structure (in <sys/sysctl.h>) is conditionalized by __APPLE_API_UNSTABLE, you should restrict use of the above code to the debug build of your program.

How do I determine if I'm being run under the debugger?

时间: 2024-07-28 12:25:48

How do I determine if I'm being run under the debugger?的相关文章

JUnit扩展:引入新注解

发现问题 JUnit提供了Test Suite来帮助我们组织case,还提供了Category来帮助我们来给建立大的Test Set,比如BAT,MAT, Full Testing. 那么什么情况下,这些仍然不能满足我们的需求,需要进行拓展呢? 闲话不表,直接上需求: 1. 老板希望能精确的衡量出每个Sprint写了多少条自动化case,或者每个User Story又设计了多少条case来保证覆盖率,以此来对工作量和效率有数据上的直观表示.  2. 对于云服务,通常会有不同的server来对应产

《转载》IOS高级开发~开机启动&amp;无限后台运行&amp;监听进程

非越狱情况下实现: 开机启动:App安装到IOS设备设备之后,无论App是否开启过,只要IOS设备重启,App就会随之启动: 无限后台运行:应用进入后台状态,可以无限后台运行,不被系统kill: 监听进程:可获IOS设备运行除系统外的App(包括正在运行和后台运行): 配置项目 plist文件 添加: <key>UIBackgroundModes</key> <array> <string>voip</string> </array>

解析theme()

drupal_render()只是对theme()的调用做了包装,真正做任务的还是theme(). function theme($hook, $variables = array()) { ... ... } theme()的开头检查了module_load_all()是否有执行.theme()只能在所有模块装入后才能执行. // If called before all modules are loaded, we do not necessarily have a full // them

Scheduling Jobs with Oracle Scheduler

In this chapter: About Scheduler Objects and Their Naming Creating, Running, and Managing Jobs Creating and Managing Programs to Define Jobs Creating and Managing Schedules to Define Jobs Using Events to Start Jobs Creating and Managing Job Chains Pr

备份3个判断指针是否有效的函数,以备不时之需

BOOLEAN MmIsAddressValid( _In_  PVOID VirtualAddress ); Parameters VirtualAddress [in] A pointer to the nonpaged virtual address to check. The caller must ensure that this  address cannot be paged out or deleted for the duration of this call. Even af

man bash

BASH(1) General Commands Manual BASH(1) NAME bash - GNU Bourne-Again SHell SYNOPSIS bash [options] [command_string | file] COPYRIGHT Bash is Copyright (C) 1989-2013 by the Free Software Foundation, Inc. DESCRIPTION Bash is an sh-compatible command la

//man(1)

man(1)                                                                  man(1) NAME man - format and display the on-line manual pages SYNOPSIS man  [-acdfFhkKtwW]  [--path]  [-m system] [-p string] [-C config_file] [-M pathlist] [-P pager] [-B browse

PreferenceActivity 安卓官方文档(译文)

PreferenceActivity 安卓开发者译文 类概述 这是一个向用户展示 preferences 的Activity的扩展类. 在 HONEYCOMB (android4.0)之前的版本,这个类仅仅只向用户展示单一preference集; 这个功能在之后的版本被放在 PreferenceFragment 类中. 如果你想你的 PreferenceActivity 仅仅保持之前的样式, 本文档同样适用之前APIs. 这个Activity用来向用户展示一个或者更多的preferences 的

Python定时调度--多任务同一时间开始跑 scheduler.enterabs

Event Priorities If more than one event is scheduled for the same time their priority values are used to determine the order they are run. import sched import time scheduler = sched.scheduler(time.time, time.sleep) def print_event(name): print 'EVENT