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

Source: Internet
Author: User

#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, 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>) was conditionalized by __apple_api_ UNSTABLE, you should restrict use of the above code to the debug build of your program.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.