Samveen's Blog

15th June 2013

Strace on Android

I love strace. The ability to trace a process's interaction with the underlying system is absolutely essential, if not an amazing way to understand the facilities the system provides. I can safely say that more than 50% of my knowledge of Linux's system calls comes from using strace on all sorts of programs (particularly gcc). Given that I'm trying to find/receive/build a native hosted gcc on Android, I want strace on Android as well.

There are quite a lot of resources online about compiling strace for Android, However, most of them suffer from a fatal flaw. They don't use the Android NDK to build strace, instead using the awesome standalone CodeSourcery tool chains to build it. However, the CodeSourcery tool chains are targeted at Linux with the GNU libc, requiring that strace be statically compiled to ensure that it runs on Android, which is packaged with Bionic libc. However, Android /Linux does differ from Linux in such subtle ways that even I am unsure of what they are(apart from the fact that there ARE differences in the system calls). This means that any strace not compiled by the NDK _may_ provide erroneous info about the system calls from the process being traced.

So, I decided to try and build strace using the NDK. I got strace-5.8 from, proceeded to ./configure with the correct CC and --sysroot combination and then ran make. The failure of the build on missing headers was the beginning of my problems. After ensuring that the required definitions were found using the appropriate replacement headers, the kicker came in the form of functions missing from Bionic. This meant that I was stuck. It also meant that strace unmodified would not compile using the NDK.

What saved my day was this interesting post from Lucas Tan detailing the method to build strace using the same infrastructure as that used to build the Android kernels. What caught my eye was the git repository platform/external/strace on which meant that there was a code base of strace fixed up for android. A quick git clone, a little renaming magic, a couple of -D compiler defines, and I had a nice working strace 5.6 binary (so what if it's not the latest version - 5.8).