strace shell command issue in Android - android

I'm building a monitor app, which runs in background and logs the system calls executed by currently running application using the strace command.
String cmd="strace -p "+processID+" -o /mnt/sdcard/traceFile_"+processID+".txt";
Runtime.getRuntime().exec(cmd);
Here processID is the PID of currently running process which is got from some other method implemented. It logs the system calls of the first app it monitors properly with all executed system call information. But when a new app is started(second one onwards), the processID is updated correctly, but the file traceFile_processID is written as an empty file.
I'm not able to figure out why its happening. Is it because the strace execution of first app monitored still there?? If so how I can execute a ^C to terminate that session and start a new one as in adb shell command prompt?? Plz help me.....

If you want to "^C" as you say, what you're really asking for is how to raise the signal called SIGINT to the given processID. You can do that simply by kill(processID, SIGINT); - this is equivalent to pressing Ctrl-C on the keyboard for the target process.

Related

How can I redirect android's getevent output to a file?

I'm trying to redirect the getevent command in Android to a file on the device.
I tried. getevent > /path/to/file, but that didn't work. When I CTRL+C'd, the file was empty.
After further investigation, I discovered that it works if I use getevent's -c flag, which tells the process to terminate after a certain number of events are received. This leads me to believe that the problem is related to the fact that I'm killing the process. When I use tee instead of a >, I can see that the output does not show in stdout until the process finishes gracefully.
The problem of course is that getevent never terminates, and I can't use the -c flag because I won't know beforehand how many events are going to be received.
How can I redirect the getevent to a file in such a way that it is there even when the process is killed?
It's fine if the solution requires the device to be rooted.
Edit: I've discovered through further investigation, that if I ensure that at least 25 events have been sent, it writes to the file. But if I ensure 30 events have been sent, only those 25 events will be written to the file. This confirmed my suspicion, that it's a buffer issue.
In the end, I had to write a script calling getevent -c 1 in a loop.

Accept 'Force Close' dialog via ADB shell

I'm trying to figure out how (or if it's possible) to accept the 'Force Close' dialog via the adb shell when an Android app crashes with a hard error (specifically out_of_memory). I'm hoping to basically loop an app with a bash script, so when it crashes I want to start it running again. The missing step here is that I can't simulate pressing the 'Force Close' button that shows up in the middle of the dialog.
At the same time, the process doesn't seem to actually be running (trying to kill the PID doesn't work), so it's a bit of a weird situation because it seems to have already stopped, but launching it again (via adb shell am ...) just gives me 'current task has been brought to the front'.
Any thoughts would be appreciated!
+1 to DarkXphenomenon for the UncaughtExceptionHandler.
However to kill the process you should use am:
am force-stop: force stop everything associated with <PACKAGE>.
am kill: Kill all processes associated with <PACKAGE>. Only kills.
processes that are safe to kill -- that is, will not impact the user
experience.
for example:
adb shell am force-stop <YOUR.PACKAGE.NAME>
If it is your own app, you can add an UncaughtExceptionHandler and not show the force close dialog. If not, the following might work.
You can also kill the process from adb. (Credit)
adb shell kill $(adb shell ps | grep YOUR.PACKAGE.NAME | awk '{ print $2 }')
After that, you can use adb shell am ... to respawn the process.

Shell script run through runtime.exec() behaves differently than if I run it through terminal

I have this snippet of shell script:
am start -n com.android.gallery3d/com.android.gallery3d.app.MovieActivity -d /sdcard/movie.mp4
sleep 5
input keyevent 4
This script basically reads as follow:
1. Open the gallery application to start movie.mp4
2. Sleep 5 seconds
3. Press back key
When I run this script through adb shell, things work out as expected which is the video plays only for the first 5 seconds and then the back key is pressed which essentially quits the gallery app.
However, when I run this though runtime.exec()
The gallery does start, but then nothing else happen. It seems like the system prevents me from running the script when the application is in the background.
Anybody happens to know any work around? Or is there any way to use INTENT to achieve the same result?
Thanks in advance
A script run by an application runs as the application userid, which does not have the extra
debug privileges that the adb shell (running as shell or even root) does.
For security reasons, applications are not supposed to be able to feed fake keystrokes to other applications and especially not to the system itself.
Perhaps instead of backing out of the gallery, you should try to start something else with an Intent.

Stop running application with ADB

is there a simple way to stop a running application using ADB.
Szenario:
Working on App
Have a script which uploads, installs and starts App on change
Problem:
Currently running version gets killed (not shutdown), which make testing cleanup very hard. Option would be to "do cleanup in between", like after a certain time, but I would prefer to do it in the correct location (so like with the OS, as long as the App is still running, so need to save value, as soon as the OS tells me e.g. memory low or calls onDestroy, I want to save stuff)
Chris
I'm not aware of a way to do this. I was hoping there might be a way to send an intent to tell the app to exit using adb shell e.g.
adb shell am start -a [intent] -n [class]
However, I found that somebody asked this question on a Google forum but they haven't got an answer:
http://groups.google.com/group/android-platform/browse_thread/thread/3fd02d01c6c3b41a/56814e518503efd6

How can I get root permissions through the Android SDK?

I'm learning Android programming, and I want to make an application which has to run as root. The logical thing would be to add a root permission in the Android Manifest.
I saw this link in the documentation, and especially noted the FACTORY_TEST permission:
public static final String FACTORY_TEST
Since: API Level 1
Run as a manufacturer test
application, running as the root user.
Only available when the device is
running in manufacturer test mode.
Constant Value:
"android.permission.FACTORY_TEST"
Is that the best way?
If it's not possible using the SDK, how can I make a "root" application work?
What you need to do is something like:
Process root = Runtime.getRuntime().exec("su");
That causes SuperUser to show, which lets you either Allow or Block it from root access. This approach might not work if the user is not rooted. Here is a way you can test it.
First lets us get the basics right. Android run Linux kernel underneath. Now if you have to run your process on it with super user privileges(run it as root) the only way is to execute your process is via command line because it is the only way you can directly interact with the kernel. Also you need to use su before running any command. Also as Chris has mentioned in his comment on the 1st answer
Process process = Runtime.getRuntime().exec("su");
will accomplish nearly nothing. It will just ask for super use privilege using dialog. What you can do is instead of just executing su you can execute your process with su as following
Process process = Runtime.getRuntime().exec(new String[] { "su", "-c", yourCommand});
The -c Option
Among the most commonly used of su's few options is -c, which tells su to execute the command that directly follows it on the same line. Such command is executed as the new user, and then the terminal window or console from which su was run immediately returns to the account of the former user after the command has completed execution or after any program that it has launched has been closed.(More details)
Alternate Option
Alternative to above method one another way that might work is to use command line to copy you app to /system/app/ directory. Then your application will run automatically with root privileges(same as System apps)
The SDK does not offer a way to run an app as root.

Categories

Resources