title: How to implement PT_DENY_ATTACH (anti-debugging in iOS )
body: <p>PT_DENY_ATTACH is an anti-debugging feature that helps prevent debuggers from attaching to the app. Following code can be implemented in main() to prevent GDB from attaching to the application. :</p>
  
  <pre><code>#import &lt;dlfcn.h&gt;
  #import &lt;sys/types.h&gt;
  typedef int (*ptrace_ptr_t)(int _request, pid_t _pid, caddr_t _addr, int _data);
  #define PT_DENY_ATTACH 31
  void disable_gdb() {
  void* handle = dlopen(0, RTLD_GLOBAL | RTLD_NOW);
  ptrace_ptr_t ptrace_ptr = dlsym(handle, "ptrace");
  ptrace_ptr(PT_DENY_ATTACH, 0, 0, 0);
  dlclose(handle);
  }
  int main(int argc, char *argv[]) {
  @autoreleasepool {
  #ifdef DEBUG
  //do nothing
  #else
  disable_gdb();
  #endif
  }}
  </code></pre>