Run multiple commands at once in RootTools/RootShell? - android

I'm using the RootTools library, and I need to execute two commands. The first one runs a binary, the second sends SIGINT to it, to kill it.
RootTools (as far as I know) can only have one root shell open at a time, so commands can only be executed one by one. This is a problem, because I have no way to stop my binary after I've ran it.
How can I do any of the following things?
Execute two commands at once, so I can run my kill command when the binary is running
Send SIGINT to my native process some other way (e.g. with a RootTools function)
I need to use RootTools because it's the only way for me to read standard output from my program. If there's another way to do that, though, please comment.

Do you think you can concat the commands?
Let's say I want to launch a find command, but if it takes 5 seconds, I want it to stop:
find / & sleep 5 && kill $!
We can get a better suited one liner, too (i.e. ignore standard error, kill only if needed etc.).
You could also just store the PID and kill it later (be careful, if the daemon stopped to run, his PID can be reused by the OS):
run the daemon in a root shell
my-daemon >/dev/null & echo "PID: $!"
parse the output in Java and store the PID (SharedPreferences?)
var pid = outputLine.split(" ")[1]
later on, stop the daemon with a root shell
kill <pid>

Related

is it possible to stop the cts and save the current logs

I am running android-cts . The commands I run are mentioned below,
cts-tf
run-cts --plan --cts-camera
It is being running for two days . How do I stop the current task and save the existing logs .
Also it is mentioned in documentation ,that the cts logs will be stored in
CTS_ROOT/android-cts/results/start_time.zip
But I dont see a start_time.zip in the location specified .
There are few ways by which you can stop the CTS invocation and result will be generated of your runs,
Unplug the USB Cables from the devices, this will make tradefed to not detect any device and once timeout occurs, it will generate result on testcases and modules it ran.
Kill command, just write the kill in the tradefed, and it will kill and stop the invocation threads for the runs. once that is done, it will generate the result but make sure not to give kill command more than 1 or else it will exit from tradefed without generating the result.

Steal file descriptors from parent process

I am using an android system and have a slightly broken su: I get a root shell, but stdin/stdout is acting weird: One example being that ctrl+c kills all children of the su process. So since su gets all the input before passing it to its children, I don't think there is anything I could do to fix this without killing the su process.
I thought about writing a small c programm that does the following:
Find parent su process id and the file descriptors (for example
/dev/pts/1)
Fork new shell process with fd/0,fd/1,fd/2 pointing to
/dev/pts/1
Kill su process
Would this work? Is there maybe an easier way to stop the su binary from "intercepting" stdin/stdout?

How to get CPU usage of certain function in android app?

I'm trying to get CPU usage in some point of running app. I need something like i used for time measurement.
Before I called the function (witch I want to measure) I used System.currentTimeMillis() to get the start time and the difference with the same value after function ended.
Running time of this function could be from 1 to 1000ms.
Mine solutions:
I can use adb top command triggered every millisecond (but i don't think it is working properly) adb shell top -m 15 -d 0.001 > C:\something\something\results.txt
Or, I was thinking about to call this command from running app in another thread (if the function will end so the thread would). If you think this could be the right way, may I still send results of command to some file in phone?Or should I use adb shell top -m 15 -d 0.001 -n 1 and call it in while cycle until thread will end?
If by function you mean literally java function then why dont you measure CPU time of its execution (difference of end and start measurements)? You can use System.currentTimeMillis() but this will measure also time of other threads that got CPU quantum. So I believe you are after Debug.threadCpuTimeNanos() which will measure only time CPU was executing your function code, you can investigate how it works by looking into sources:
http://androidxref.com/5.1.0_r1/xref/art/runtime/utils.cc#177
I'm not sure if this is what you're looking for, I've been looking into Android debugging recently, but I haven't tried this myself.
Here's the link: Traceview War Story, from the Android Developer's blog.
It describes using the Traceview tool to analyze functions and how much time the system is devoting to each process within that function.

Enabling logcat in init.rc

I'm appending init.rc in Android root with:
service logcat /system/bin/logcat -v long -f /mnt/sdcard/logcat.log
This solution doesn't generate any logs. The logcat.log file doesn't exist.
How can i start gathering logcat output through init.rc ?
A couple of things that could be causing problems above:
1. you defined your service to be called logcat. That looks awfully close to what might be a reserved/pre-existing name. I would choose a more distinguished service name.
2. there is no explicit start trigger for the service, hence its entirely dependent on the context in which its defined (i.e. which init phase). Pick the wrong phase (i.e. too early) and /mnt may not even exist.
3. the service will by default be running as root and thus the logcat.log file will be rw only by root. Not good to run processes as root. And not good to force readers to be root in order to read the log file.
Here the approach I've used to achieve what you're looking to do.
Problem: Ordinarily, Android log messages remain in the kernel’s (volatile) memory only and thus doesn’t survive across reboots.
Solution: To retain those log messages across reboots requires them to be written to persistent storage (i.e. the filesystem). The following code defines such a service that is started by Android during init.
Step 1, define a service that the Android init process will spawn to do this activity. This goes in init.rc.
service persistentLogging /system/bin/logcat -r 1024 -n 9 -v threadTime -f /cache/logs/log
user system
group system log
disabled
Notes about the above:
it creates a service called persistentLogging (that will be referred to in the second step below) by the start trigger.
it requests logcat to do a rolling log file (consisting of 10 files / 1Mb each) in directory - /cache/logs (i.e. log, log.1, log.2, … log.9). Adjust to suit your needs.
the service is to run as system user. This means the log file will be read+write only by system. If your app has system privileges then you’ll be able to read the log file. I’ve also defined the service to be in the log group too since that seems appropriate although since the files are not readable by group its a moot point.
the service is initially disabled. It will be started by a trigger defined below
the service is NOT oneshot. Hence, should it die, Android will attempt to restart it.
Step 2, define a trigger for starting the service. This also goes in your init.rc file.
on post-fs
mkdir /cache/logs 0775 system log
start persistentLogging
Notes about the above:
the commands are triggered during the ‘post-fs’ phase so that they occur after filesystem partitions have been mounted and when other system directories are having their permissions changed. Ideally, this service should start as late as possible because its not important or used by any other start-up activity.
the trigger first creates the target directory before starting the service. Remember the mkdir command syntax is defined by the init.rc language. In Android this syntax is not a sh syntax eventhough it looks a lot like it.
eventhough the above logging service doesn’t start until the post-fs phase of init, it will nevertheless dump all logging information since the beginning of kernel's startup as these log messages are already in the kernel buffers and this logging service is merely copying those messages to a file.
although both code fragments above ultimately need to appear in init.rc file, it is more maintainable if those additions are made to the init.${ro.hardware}.rc file defined for the device. e.g. init.freescale.rc which is automatically included by init.rc.
If we need to start the logcat and collect all the log buffers after on post-fs-data/boot completed from init.rc you can use below code in init.rc file.
on property:dev.bootcomplete=1
start logging
on post-fs-data
start logging
service logging /system/bin/logcat -b all -v threadTime -f /data/directory_name/log -r 100000 -n 9
user system
group root system log
disabled

Android sendevent is really slow - how to speed it up?

I am doing some ui automation, and I am able to store screen touches using getevent, but when I try to send this using sendevent, it takes a really long time, making it hard to actually replay the inputs.
I have already trying loading the script onto the device and running the script locally on the device (a script with a bunch of sendevent commands). But this only imporved this slightly. Is there some other way to inject these commands in a quicker way?
The handler for touch is implemented differently across devices. You should cat /proc/bus/input/devices to see where the touch handler is implemented.
You can also do adb shell getevent, interact with the device and see the output for the interface name.
The reason why your replay takes a long time is because the sendevent binary opens the interface file, writes data to it and closes it for every call to sendevent. So in theory, if you have a bunch of sendevent commands, the binary is opening the interface file, writing data and closing it for every command.
The way I've solved this issue is by re-writing the sendevent.c file under /system/core/toolbox to open the file only once during replay, writing all the data and closing it at the end of the replay. It works perfectly for me!
OK.
Instead of using the getevent/sendevent you can try direct reading from the event interface
inside adb shell try:
dd if=/dev/input/event6 of=record1 # to record
dd if=./record1 of=/dev/input/event6 #to play
However, this may run too fast...

Categories

Resources