Pages

Friday, December 14, 2012

Strace your Android application with adb


I have been looking on the internet for two days to figure out how can I get information about system calls being made in an Android application when I tap some button on the screen. I got it working  now, so thought I should post it also so that if somebody needs this information, (s)he can directly jump to the right solution.

I assume, you already have installed Android SDK. You can create a new project in Eclipse, with some activity and buttons on the screen because we need to tap some button on the screen (Its just an easy approach) and see system calls on the screen.

I am using DatePicker module, which comes from the Android source code and I will be testing on emulator, though I am sure it would be working on any Android phone also. From now on, I assume you have working emulator and your project ready in Eclipse. (If you have device, connect it with the USB to your laptop and enable 'USB Debugging' mode on your phone settings).

1) Click on 'Debug' icon in Eclipse. It will start an emulator to run your application. Let it run.

2) Now if you have installed Android SDK with the default settings, you should be able to find 'adb.exe' at "C:\Program Files (x86)\Android\android-sdk\platform-tools" location.

3) Go to Start button on your Windows machine, type 'cmd' and you will see command window opened up. Now type 'cd C:\Program Files (x86)\Android\android-sdk\platform-tools' on the command window and you will be directed to platform-tools directory. Type 'ls' to see content of this folder and you can find adb.exe here.

4) Type 'adb help' on the command window to see if adb is working.

5) Now type 'adb devices' which will show you all the emulators and devices attached with the adb debugger. Since we have just one emulator running in this case, we will find only one emulator information. For example, on my screen, following information is displayed.

List of devices attached
emulator-5554   device

emulator-5554 is emulator ID here. If you have devices attached, you would see their serial numbers here.

6)  Type 'adb -s emulator-5554 shell' on the command window. You will get shell of the emulator or your device. Now if you type 'ls' on the shell, you can see files of your emulator or the current directory of your smart phone.

7) Type 'ps' on the command window to find out which processes are running on your device, and one of them is our DatePicker application. It might be hard to find DatePicker application there, but if you know package name (com.datapicker in my case) in your Eclipse project, you can find out the package name there. All you need to note down is PID. Remember our goal is to find out system calls against our application, so we will use PID there.

8) Type 'strace -p <pid>' on the command window. Replace <pid> with the PID value found in the previous step. It will not show enough information about the system calls by now.

9) Now if you click on any button at your application on emulator, you will see lot of system calls being made on the command window. That's all we wanted.

Thanks.

No comments:

Post a Comment