Strace help

I need to run and monitor applications on Android Emulator. I am using the strace utility to monitor system calls. Everytime to start strace i need to manually start the application , get the process Id of the application and then give it to strace to start logging all the system calls.

So is there a way by which i can automate all the process, i mean the moment i install an application , it should start the application and internally start the strace for the current application and start logging.

The application will be a user apllication , which i will install using adb install command.

So if somebody could help me with this, would be great .. Thanks in advance

Sure, you just put the command at the end of the strace line. I have seen some apps balk at this manner of calling, so you can also do this at the shell prompt or in a wrapper script you execute in place of the app:

$ (sleep 2; run_app_with_args)& strace ... -o /tmp/this_trace.tr -p $!
1 Like

Hello DGPickett, do u mean this way

(sleep 2; am start com.android.mail)& strace  -o /tmp/this_trace.tr -p $!

because it says invalid PID $!

Maybe the android shell has no '$!' for pid of last '&' ? Here is my hp-ux run, trussx being a wrapper for tusc, which is like strace, with lots of options:

$ (sleep 2;date)&trussx -o /tmp/date.tr -p $!
[1]     3554
tusc: ttrace feature level 6.
Mon Feb 18 11:28:05 EST 2013
[1] +  Done                    (sleep 2;date)&trussx -o /tmp/date.tr -p $!
$

Once we have $! happy, you need options like -aefl for args, env, fork and threads, and -rall -wall for full io reporting.

Does here sleep 2 means run for 2 seconds ???? .

Yes, the subshell sleeps 2 seconds and then takes on the target command. The subshell runs in the background, and the tusc attaches to the subshell process. When the sleep ends, the subshell trace will show that and all the startup of the next command and its children, since I specify -f to follow forked children.

Hii , DGPickett , thanks you code is working. A little more help , i am supposed to provide the package name in the command as input , so is there a way to read the package name from a path store in some variable and give as input to this??

My googling says android has bourne sh, which has $! normally.

---------- Post updated at 11:55 AM ---------- Previous update was at 11:48 AM ----------

If it is reading stdin, no problem. You can strace in the background, too, so the command is in the foreground and you can type:

$ strace -fvxs 99999 -o /tmp/this_trace.tr -p $$ & am start com.android.mail
1 Like

what is 99999 used for??

---------- Post updated at 12:29 PM ---------- Previous update was at 12:22 PM ----------

I used the command as you mentioned except 99999 as it didnt accept the argument. Everything is fine , but the strace exits as soon as the application opens. Is there a way to contine running the strace even after the application opens

What does the log say as to why it stopped?

-s strsize Specify the maximum string size to print (the default is 32).
Note that filenames are not considered strings and are always printed in full.

I think strace command is being run for /bin/sh and '&' command is executing the second part of command by just starting the application. so basically its not stracing the app. two commands are indipendtly executing

Well, for all I know android strace has no -f, as docs are hard to come by.

Ohk Thanks DGPickett , got atleast some idea..

Does strace have internal help?

have a look at this

Try something simple like "strace -fvxs9999 -o /tmp/date.tr ksh -c date"

ksh command not found. I got this error . and i have tried strace works on ls, cat here.

---------- Post updated at 02:10 PM ---------- Previous update was at 02:07 PM ----------

everything works perfect if i give a pid,

but what i want is , automate the process. so it should be i will start the application in emulator , but internally it should get the pid of application and give it to strace and start logging

make it sh.

Replace the app with an executable sh script that does a strace of every call, logging to a separate file with date time pid to be unique.

it outputs date. for stanard command like date , ls ,cat it works fine

everything works perfect if i give a pid,

but what i want is , automate the process. so it should be i will start the application in emulator , but internally it should get the pid of application and give it to strace and start logging

---------- Post updated at 02:19 PM ---------- Previous update was at 02:12 PM ----------

I am new to linux. Can u show me and example for sh script , it would be of real help

To be executable, a script must have the execution permission bit set for the intended audience, it must have a #!full_path_to_interpreter_is_sh as the first line (optionally with one argument; for awk and sed you need -f), and off you go. It's in the execvp() man page.