DEV Community

Cover image for Find your Stack
Luis Mierez
Luis Mierez

Posted on

Find your Stack

We recently had to debug some old (very very old) code because of some new issues arising from it. One of the things we were investigating was whether the code was starting activities in the wrong order due to a race condition. So we had to debug our activity stack. We quickly realized that there was no easy way to debug that from android studio. Then we turned to the trusty adb which this command:

adb shell dumpsys activity activities

The dumpsys activity activities command gives you a ton of info about the current activities. But we only care about the activities related to our app, so we pipe the output to grep package.name.
This still gives us a little too much into if you just want to look at the activity stack, so we pipe output again to grep Hist to only get the activity stack. The full command ends up looking something like this:

adb shell dumpsys activity activities | grep package.name | grep Hist

This works well if you want a snapshot of the current activity stack, but doesn't really help if you want to continuously log the activity stack. This is where we turned to the watch command. This command is not on mac by default so you have to install it through brew like brew install watch. The watch command runs a command at whatever interval you want.
With watch installed your full command turns into something like:

watch -n 0.1 "adb shell dumpsys activity activities | grep package.name | grep Hist"

The -n flag sets the time interval at which we run our adb command. So this means run the command every .1 secs.
This ended up working perfectly and we were able to continuously debug our activity stack and figure out if there was some problem there. Turns out the activity stack was fine but this helped us rule out what and pointed us in the right direction to eventually fix the issue.

Top comments (0)