Print stacktrace using Frida

Arya picture Arya · Jan 27, 2018 · Viewed 8.7k times · Source

I'm able to sucessfully hook into an Android method using Frida, but I am trying to find out who is calling that method. I can find that out if I can rewrite the method with Frida to print the stacktrace when the method gets called. I've tried a few things, but all of them have had some sort of error. This is the latest thing I have tried.

setImmediate(function() {
    console.log("[*] Starting script");
    Java.perform(function () {
      var Activity = Java.use("com.package.MyClass");
      Activity.getUpdates.overload('boolean', 'java.lang.String', 'java.lang.String').implementation  = function (v1, v2, v3) {
        console.log("v1: " + v1);
        console.log("v2: " + v2);
        console.log("v3: " + v3);
        Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new())
      };
    });
})

This is resulting in the following error

Error: Not allowed outside Java.perform() callback
    at d (frida/node_modules/frida-java/index.js:86)
    at frida/node_modules/frida-java/index.js:366
    at [anon] (repl1.js:11)
    at input:1

Any idea how I can achieve this?

Answer

Fritz picture Fritz · Jan 30, 2018

I fixed it by using this:

Java.perform(function() {
    console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});

It is necessary to add an additional Java.perform call