Native层和Java层frida打印调用堆栈

一、Java层打印调用堆栈

方法1.


function LogPrint(log) {
    var theDate = new Date();
    var hour = theDate.getHours();
    var minute = theDate.getMinutes();
    var second = theDate.getSeconds();
    var mSecond = theDate.getMilliseconds();

    hour < 10 ? hour = "0" + hour : hour;
    minute < 10 ? minute = "0" + minute : minute;
    second < 10 ? second = "0" + second : second;
    mSecond < 10 ? mSecond = "00" + mSecond : mSecond < 100 ? mSecond = "0" + mSecond : mSecond;
    var time = hour + ":" + minute + ":" + second + ":" + mSecond;
    var threadid = Process.getCurrentThreadId();
    console.log("[" + time + "]" + "->threadid:" + threadid + "--" + log);

}

function printJavaStack(name) {
    Java.perform(function () {
        var Exception = Java.use("java.lang.Exception");
        var ins = Exception.$new("Exception");
        var straces = ins.getStackTrace();
        if (straces != undefined && straces != null) {
            var strace = straces.toString();
            var replaceStr = strace.replace(/,/g, " \n ");
            LogPrint("=============================" + name + " Stack strat=======================");
            LogPrint(replaceStr);
            LogPrint("=============================" + name + " Stack end======================= \n ");
            Exception.$dispose();
        }
    });
}


#调用
printJavaStack("name");

方法2.根据需求修改类名和方法名

//JAVA层打印堆栈信息
function printStackTrace(){
    Java.perform(function () {
        //类名
        var HttpRequestEntity = Java.use('org.cocos2dx.javascript.sdk.KuoYouManager');
            var threadef = Java.use('java.lang.Thread');
            var threadinstance = threadef.$new();
     
            function Where(stack){
                for(var i = 0; i < stack.length; ++i){
                    send(stack[i].toString());
                }
            }
            // loadCacheSP 方法名
            HttpRequestEntity.loadCacheSP.implementation = function (arg1) {
                var ret = this.loadCacheSP(arg1);
                var stack = threadinstance.currentThread().getStackTrace();
                send("Full call stack:" + Where(stack));
                //send("Deobfuscated " + ret + " @ " + stack[3].toString());
                return ret;
        };
    });
}

二、打印native层调用栈

function LogPrint(log) {
    var theDate = new Date();
    var hour = theDate.getHours();
    var minute = theDate.getMinutes();
    var second = theDate.getSeconds();
    var mSecond = theDate.getMilliseconds();

    hour < 10 ? hour = "0" + hour : hour;
    minute < 10 ? minute = "0" + minute : minute;
    second < 10 ? second = "0" + second : second;
    mSecond < 10 ? mSecond = "00" + mSecond : mSecond < 100 ? mSecond = "0" + mSecond : mSecond;
    var time = hour + ":" + minute + ":" + second + ":" + mSecond;
    var threadid = Process.getCurrentThreadId();
    console.log("[" + time + "]" + "->threadid:" + threadid + "--" + log);

}

function printNativeStack(context, name) {
    var trace = Thread.backtrace(context, Backtracer.ACCURATE).map(DebugSymbol.fromAddress).join("\n");
    LogPrint("-----------start:" + name + "--------------");
    LogPrint(trace);
    LogPrint("-----------end:" + name + "--------------");

}
printNativeStack(this.context, Process.getCurrentThreadId() + "read");

你可能感兴趣的:(安卓,android逆向,frida打印调用栈,android调用栈)