0

I'm trying to exceute strace command for a PID from the android app. the code segment is below:

Runtime.getRuntime().exec("strace -p 20436 -o /mnt/sdcard/sampleTrace11.txt");

The file sampleTrace11.txt is created but it is empty. When I execute the same command from shell command prompt, file is written with data. Can anyone please help me..

srooth
  • 33
  • 2
  • 11
  • Have you tested with the full path of `strace` ? – Gilles Quénot Feb 10 '13 at 16:58
  • @sputnick if `strace` didn't start, it wouldn't create an empty file (note that `-o` option is used, not an output redirection or something like that) – Anton Kovalenko Feb 10 '13 at 17:19
  • Can you plz tell what to do then? – srooth Feb 10 '13 at 17:35
  • @srooth If only I knew java... Well, do you have a way to `exec` a program and get its standard error and standard output? Maybe `strace` tells something about why it refuses to work? And if `getRuntime().exec` supports shell redirections, maybe add `2> /mnt/sdcard/straceerr.txt >/mnt/sdcard/straceout.txt` and see if something useful appears there? – Anton Kovalenko Feb 10 '13 at 17:37
  • @Anton I tried with a 2> option..But no file is created then.. – srooth Feb 10 '13 at 17:54
  • Can you write shell scripts on Android? Can you `chmod +x` a script and then `exec` it? Then you can make a wrapper for strace, adding necessary redirections. – Anton Kovalenko Feb 10 '13 at 17:57

1 Answers1

0

I expect it's permissions-related. A good way is to use run-as from adb shell. For example, supposing your application is com.srooth.fancyapp, do this:

adb shell run-as com.srooth.fancyapp strace -p 20436 -o /mnt/sdcard/sampleTrace11.txt

If this doesn't work, you know it's something to do with the permissions of com.srooth.fancyapp, and you can expect to get the same result when running from within Java.

Some possible permissions problems:

  • Your app does not have permission to write to the SD card. This capability will be inherited by all other apps running as your user, so it's important to ensure that your AndroidManifest.xml specifies the permission:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  • If the process in question is not part of your application, it will be run as a different user. As such your application will not have permission to inspect it. There's nothing you can do about this, except for using a rooted phone and prepending su -c to your command line.

A couple of other ideas:

  • Try launching the process instead of attaching to it, if you can. This shouldn't make any difference but may help you debug the issue.
  • Instead of using Runtime.getRuntime().exec(), try using the ProcessBuilder class. This will enable you to do all sorts of useful things:

    • Wait until the process has finished
    • Look at its standard output
    • Look at its standard error
    • Get its return code.
Adrian Taylor
  • 4,054
  • 2
  • 24
  • 26
  • Thanks for replying Adrian...Seems it has something with what you pointed out...It's working fine for the processId of the main app(app in which this code is written) . But when it's trying to strace any other app, the problem arises...I'm working with an emulator and it's rooted..I tried with su -c option also, but not working :( – srooth Feb 12 '13 at 20:12