I am trying to ptrace_attach the main process and its threads (/proc/<pid>/task) of an android unity app to avoid malicious users debugging the app(which is a game).

I developed a ndk library that forks from main process and ptrace_attach the parent process(being the main process) inside the JNI_OnLoad() function. After that periodically checks the /proc/<pid>/task folder to attach newly created threads.

The problem is, this works well in normal apps but when I try to run this inside an app made with unity, the main process stops and screen becomes black or white not responding. But if you delay attaching a few seconds just enough to see the animation working on the screen, attaching works fine.

Code is roughly something like this:

     parentPid = getppid();

     // attach parent process

          // get signal from processes
          stoppedPid = waitpid(-1,&stat_loc, 0);


          // check if stoppedPid need to be attached
          // if so, attach


          // else, just continue the stopped process

Maybe I should adjust the ptrace_setoptions ?

Thanks in advance :)

  • I forgot to add that this happens in android 6.0.0
    – Jaewon Min
    Commented Jun 28, 2016 at 2:35
  • Also with different options for waitpid like __WALL etc
    – Jaewon Min
    Commented Jun 28, 2016 at 5:25

1 Answer 1


Well somethings I found out -

When I ptrace_attach the main process of the target app and wait for signals, I get SIGSEGV signal while app loads and just hangs there(because forked process cannot handle SIGSEGV). In the java code, it seems SIGSEGV occurs while calling View related functions.

I guess UnityPlayer or Android app loader handles SIGSEGV smoothly while app loading time. Therefore, if you get a SIGSEGV, simply detaching it and attaching again does not hang the app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.