Skip to content

Instantly share code, notes, and snippets.

@atomicbird
Created January 27, 2009 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomicbird/53453 to your computer and use it in GitHub Desktop.
Save atomicbird/53453 to your computer and use it in GitHub Desktop.
#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