Search code examples
androidlinuxbinaryexecutestrace

Android strace (or any linux binary) execution


I've got a problem which is probably very easy to solve but I can't seem to find the answer.

I'm trying to execute strace on different processes running on an Android device. I've succeded in doing this by copying the strace binary to /system/xbin (just like the emulator) and works just fine. My problem is I copied the binary through the bootloader and I don't want users to have to go through that.

So I found this great tutorial to run binaries from within an app:

http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App

I follow the tutorial and do this:

  • Copy the binary of strace to /data/data/com.mypackage
  • Change execution permissions to the binary (I've tryed with 777 too)
  • Try to execute it with

    Process process = Runtime.getRuntime().exec("/data/data/com.mypackage/strace -p PID -o /sdcard/folder_I_know_it_works");

I know the command is right because when I execute it from /xbin/system it works perfectly. When I run the one I copied from within my app I get empty outputs. I only get the proper output for my own process, so I understand it's a permission problem.

I didn't mention yet that I have root permission on my app of course. I also tryed changing the owner to the strace binary to root:root and system:system and nothing.

To sum up: I need to reach the same permission on a binary I copy from my app to the phone memory than the one I have on a binary found on /xbin/strace

What am I doing wrong?

Another good solution would be finding a way to copy strace to /system/xbin from within an app with root permission. Any ideas?

Thanks a lot in advance!


Solution

  • Found the answer to this in case it's useful for anyone else.

    Process process = Runtime.getRuntime().exec("su");
    OutputStream os;
    os = process.getOutputStream();
    os.write("your linux command here".getBytes());
    os.close();
    

    You are basically simply running su as a process and sending commands through its output stream.

    Later you may want to call process.wait()