Created
January 27, 2009 18:09
-
-
Save atomicbird/53453 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/sysctl.h> | |
#include <sys/proc.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
// Return 1 if being debugged, 0 if not (or if debug status can't be determined). | |
int beingDebugged() | |
{ | |
struct kinfo_proc *result; | |
pid_t mypid = getpid(); | |
// Set up four-level query as described in man page. Fourth arg is the PID of interest. | |
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0, 0 }; | |
name[3] = mypid; | |
int debugged = 0; | |
result = kinfo_ptr(name, ((sizeof(name) / sizeof(*name)) - 1), NULL); | |
if (result == NULL) { | |
return debugged; | |
} | |
if (result->kp_proc.p_flag & P_TRACED) { | |
debugged = 1; | |
} | |
free(result); | |
return debugged; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment