Hook函数Thread.currentThread().getStackTrace()

StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        for (StackTraceElement ele : stackTrace) {
            System.out.println(ele);
        }

I/System.out(13958): dalvik.system.VMStack.getThreadStackTrace(Native Method)
I/System.out(13958): java.lang.Thread.getStackTrace(Thread.java:579)
I/System.out(13958): kaiqi.cn.xposed.MainActivity.onCreate(MainActivity.java:47)
I/System.out(13958): android.app.Activity.performCreate(Activity.java:5263)
I/System.out(13958): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
I/System.out(13958): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
I/System.out(13958): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
I/System.out(13958): android.app.ActivityThread.access$900(ActivityThread.java:137)
I/System.out(13958): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
I/System.out(13958): android.os.Handler.dispatchMessage(Handler.java:102)
I/System.out(13958): android.os.Looper.loop(Looper.java:136)
I/System.out(13958): android.app.ActivityThread.main(ActivityThread.java:5089)
I/System.out(13958): java.lang.reflect.Method.invokeNative(Native Method)
I/System.out(13958): java.lang.reflect.Method.invoke(Method.java:515)
I/System.out(13958): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
I/System.out(13958): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
I/System.out(13958): de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
I/System.out(13958): dalvik.system.NativeStart.main(Native Method)

de.robv.android.xposed.XposedBridge 隐藏关键字

 try {
findAndHookMethod("java.lang.Thread", lpparam.classLoader, "getStackTrace", new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod (MethodHookParam param) throws Throwable {
                        StackTraceElement[] result = (StackTraceElement[]) param.getResult();
                        //                if (result != null) {
                        //                    for (StackTraceElement element : result) {
                        //                        System.out.println("============start=============");
                        //                        log("============start=============");
                        //                        System.out.println(element.toString());
                        //                        log(element.toString());
                        //                        System.out.println(element.getClassName());
                        //                        log(element.getClassName());
                        //                        System.out.println("============e n d============");
                        //                        log("============e n d============");
                        //                    }
                        //                }

                        //                StackTraceElement[] hooks = new StackTraceElement[result.length];
                        //                for (int i = 0; i < hooks.length; i++) {
                        //                    StackTraceElement element = result[i];
                        //                    String className = element.getClassName();
                        //                    if (className.contains("de.robv.android.xposed.XposedBridge")) {
                        //                        Log.e("tst", "类名命中:" + className);
                        //                        className = className.replaceAll("de.robv.android.xposed.XposedBridge", "xxx.xxx.yy");
                        //                        Class aClass = element.getClass();
                        //                        Field field = aClass.getDeclaredField("declaringClass");
                        //                        field.setAccessible(true); // 暴力反射
                        //                        field.set(element, className);
                        //                        Log.e("tst", "类名命中处理后:" + className);
                        //                        Log.e("tst", "类名命中处理后,对象获取:" + element.getClassName());
                        //                    }
                        //                    Log.e("tst", "类名:" + className);
                        //                    hooks[i] = element;
                        //                }
                        //                param.setResult(hooks);
                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        //de.robv.android.xposed.XposedBridge
                        StackTraceElement[] result = (StackTraceElement[]) param.getResult();
                        log("当前类信息:------------------------------------------->"+new Gson().toJson(result));
                        if (result != null) {
                            StackTraceElement[] hooks = new StackTraceElement[result.length];
                            for (int i = 0; i < hooks.length; i++) {
                                StackTraceElement element = result[i];
                                log("当前类信息:" + new Gson().toJson(element));
                                String className = element.getClassName();
                                Class aClass = element.getClass();
                                if (className.contains("de.robv.android.xposed.XposedBridge")) {
                                    log("类名命中:" + className);
                                    className = className.replaceAll("de.robv.android.xposed.XposedBridge", "xxx.xxx.yy");
                                    Field field = aClass.getDeclaredField("declaringClass");
                                    field.setAccessible(true); // 暴力反射
                                    field.set(element, className);
                                    //methodName
                                    //fileName
                                    //                            aClass.getDeclaredMethod()
                                    log("类名命中处理后:" + className);
                                    log("类名命中处理后,对象获取:" + element.getClassName());
                                }
                                log("类名:" + className);
                                String fileName = element.getFileName();
                                String methodName = element.getMethodName();
                                if (fileName.contains("XposedBridge")) {
                                    Field fieldII = aClass.getDeclaredField("fileName");
                                    fieldII.setAccessible(true); // 暴力反射
                                    fieldII.set(element, "sub");
                                }
                                if (methodName.contains("invokeOriginalMethodNative") || methodName.contains("handleHookedMethod")) {
                                    Field fieldI = aClass.getDeclaredField("methodName");
                                    fieldI.setAccessible(true); // 暴力反射
                                    fieldI.set(element, "metheds");
                                }
                                hooks[i] = element;
                            }
                            param.setResult(hooks);
                            result = (StackTraceElement[]) param.getResult();
                            for (StackTraceElement element : result) {
                                String className = element.getClassName();
                                if (className.contains("xxx.xxx.yy")) {
                                    log("包名过滤命中======>>>>>>>:" + className + "|===>>>" + element.toString());
                                }
                            }
                        }
                    }
                });
            } catch (Throwable e) {
                e.printStackTrace();
                log("#堆栈跟踪过滤#" + e);
            }
I/System.out(21891): dalvik.system.VMStack.getThreadStackTrace(Native Method)
I/System.out(21891): java.lang.Thread.getStackTrace(Thread.java:579)
I/System.out(21891): xxx.xxx.yy.metheds(Native Method)
I/System.out(21891): xxx.xxx.yy.metheds(sub:631)
I/System.out(21891): java.lang.Thread.getStackTrace(Native Method)
I/System.out(21891): kaiqi.cn.xposed.MainActivity.onCreate(MainActivity.java:47)
I/System.out(21891): android.app.Activity.performCreate(Activity.java:5263)
I/System.out(21891): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
I/System.out(21891): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
I/System.out(21891): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
I/System.out(21891): android.app.ActivityThread.access$900(ActivityThread.java:137)
I/System.out(21891): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
I/System.out(21891): android.os.Handler.dispatchMessage(Handler.java:102)
I/System.out(21891): android.os.Looper.loop(Looper.java:136)
I/System.out(21891): android.app.ActivityThread.main(ActivityThread.java:5089)
I/System.out(21891): java.lang.reflect.Method.invokeNative(Native Method)
I/System.out(21891): java.lang.reflect.Method.invoke(Method.java:515)
I/System.out(21891): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
I/System.out(21891): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
I/System.out(21891): xxx.xxx.yy.main(sub:132)
I/System.out(21891): dalvik.system.NativeStart.main(Native Method)

StackTraceElement[] thisMethodStack = new Exception().getStackTrace();
        System.out.println("==================方式二===================");
        for (StackTraceElement ele : thisMethodStack) {
            System.out.println(ele);
        }
        System.out.println("==================方式二===================");

I/System.out( 1470): ==================方式二===================
I/System.out( 1470): kaiqi.cn.xposed.MainActivity.onCreate(MainActivity.java:51)
I/System.out( 1470): android.app.Activity.performCreate(Activity.java:5263)
I/System.out( 1470): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
I/System.out( 1470): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
I/System.out( 1470): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
I/System.out( 1470): android.app.ActivityThread.access$900(ActivityThread.java:137)
I/System.out( 1470): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
I/System.out( 1470): android.os.Handler.dispatchMessage(Handler.java:102)
I/System.out( 1470): android.os.Looper.loop(Looper.java:136)
I/System.out( 1470): android.app.ActivityThread.main(ActivityThread.java:5089)
I/System.out( 1470): java.lang.reflect.Method.invokeNative(Native Method)
I/System.out( 1470): java.lang.reflect.Method.invoke(Method.java:515)
I/System.out( 1470): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
I/System.out( 1470): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
I/System.out( 1470): de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
I/System.out( 1470): dalvik.system.NativeStart.main(Native Method)
I/System.out( 1470): ==================方式二===================


I/System.out( 2800): ==================方式二===================
I/System.out( 2800): kaiqi.cn.xposed.MainActivity.onCreate(MainActivity.java:51)
I/System.out( 2800): android.app.Activity.performCreate(Activity.java:5263)
I/System.out( 2800): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
I/System.out( 2800): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
I/System.out( 2800): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
I/System.out( 2800): android.app.ActivityThread.access$900(ActivityThread.java:137)
I/System.out( 2800): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
I/System.out( 2800): android.os.Handler.dispatchMessage(Handler.java:102)
I/System.out( 2800): android.os.Looper.loop(Looper.java:136)
I/System.out( 2800): android.app.ActivityThread.main(ActivityThread.java:5089)
I/System.out( 2800): java.lang.reflect.Method.invokeNative(Native Method)
I/System.out( 2800): java.lang.reflect.Method.invoke(Method.java:515)
I/System.out( 2800): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
I/System.out( 2800): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
I/System.out( 2800): xxx.xxx.yy.main(sub:132)
I/System.out( 2800): dalvik.system.NativeStart.main(Native Method)
I/System.out( 2800): ==================方式二===================

    //java.lang.Exception
            //java.lang.Throwable
            try {
                XposedBridge.log("#堆栈跟踪过滤开始");
                XposedHelpers.findAndHookMethod("java.lang.Throwable", lpparam.classLoader, "getStackTrace", new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        hideTraces(param, "Exception");
                        XposedBridge.log("堆栈处理进入....................");
                    }


                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        super.beforeHookedMethod(param);
                        XposedBridge.log("异常类============>>>>beforeHookedMethod");
                    }
                });
                XposedBridge.log("#堆栈跟踪过滤结束#");
            } catch (Throwable throwable) {
                throwable.printStackTrace();
                XposedBridge.log("堆栈处理失败" + throwable);
                Logger.e(TAG, "#堆栈处理失败#" + throwable);
            }

    private void hideTraces(XC_MethodHook.MethodHookParam param, String tag) throws NoSuchFieldException, IllegalAccessException {
        StackTraceElement[] result = (StackTraceElement[]) param.getResult();
        log("当前tag是===============:" + tag + ":当前类信息:------------------------------------------->" + new Gson().toJson(result));
        if (result != null) {
            StackTraceElement[] hooks = new StackTraceElement[result.length];
            for (int i = 0; i < hooks.length; i++) {
                StackTraceElement element = result[i];
                log("当前类信息:" + new Gson().toJson(element));
                String className = element.getClassName();
                Class aClass = element.getClass();
                if (className.contains("de.robv.android.xposed.XposedBridge")) {
                    log("类名命中:" + className);
                    className = className.replaceAll("de.robv.android.xposed.XposedBridge", "xxx.xxx.yy");
                    Field field = aClass.getDeclaredField("declaringClass");
                    field.setAccessible(true); // 暴力反射
                    field.set(element, className);
                    //methodName
                    //fileName
                    //                            aClass.getDeclaredMethod()
                    log("类名命中处理后:" + className);
                    log("类名命中处理后,对象获取:" + element.getClassName());
                }
                log("类名:" + className);
                String fileName = element.getFileName();
                String methodName = element.getMethodName();
                if (fileName.contains("XposedBridge")) {
                    Field fieldII = aClass.getDeclaredField("fileName");
                    fieldII.setAccessible(true); // 暴力反射
                    fieldII.set(element, "sub");
                }
                if (methodName.contains("invokeOriginalMethodNative") || methodName.contains("handleHookedMethod")) {
                    Field fieldI = aClass.getDeclaredField("methodName");
                    fieldI.setAccessible(true); // 暴力反射
                    fieldI.set(element, "metheds");
                }
                hooks[i] = element;
            }
            param.setResult(hooks);
            result = (StackTraceElement[]) param.getResult();
            for (StackTraceElement element : result) {
                String className = element.getClassName();
                if (className.contains("xxx.xxx.yy")) {
                    log("包名过滤命中======>>>>>>>:" + className + "|===>>>" + element.toString());
                }
            }
        }
    }

我们来看 Thread.currentThread().getAllStackTraces(), 已经不需要hook处理了

Map allStackTraces = Thread.currentThread().getAllStackTraces();
        System.out.println("==================方式三===================");
        for (Map.Entry entry:
        allStackTraces.entrySet()) {
            StackTraceElement[] value = entry.getValue();
            for (StackTraceElement ele : value) {
                System.out.println(ele);
            }
        }
        System.out.println("==================方式三===================");
I/System.out( 3408): ==================方式三===================
I/System.out( 3408): dalvik.system.NativeStart.run(Native Method)
I/System.out( 3408): dalvik.system.VMStack.getThreadStackTrace(Native Method)
I/System.out( 3408): java.lang.Thread.getStackTrace(Thread.java:579)
I/System.out( 3408): xxx.xxx.yy.metheds(Native Method)
I/System.out( 3408): xxx.xxx.yy.metheds(sub:631)
I/System.out( 3408): java.lang.Thread.getStackTrace(Native Method)
I/System.out( 3408): java.lang.Thread.getAllStackTraces(Thread.java:521)
I/System.out( 3408): kaiqi.cn.xposed.MainActivity.onCreate(MainActivity.java:58)
I/System.out( 3408): android.app.Activity.performCreate(Activity.java:5263)
I/System.out( 3408): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
I/System.out( 3408): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
I/System.out( 3408): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
I/System.out( 3408): android.app.ActivityThread.access$900(ActivityThread.java:137)
I/System.out( 3408): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
I/System.out( 3408): android.os.Handler.dispatchMessage(Handler.java:102)
I/System.out( 3408): android.os.Looper.loop(Looper.java:136)
I/System.out( 3408): android.app.ActivityThread.main(ActivityThread.java:5089)
I/System.out( 3408): java.lang.reflect.Method.invokeNative(Native Method)
I/System.out( 3408): java.lang.reflect.Method.invoke(Method.java:515)
I/System.out( 3408): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
I/System.out( 3408): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
I/System.out( 3408): xxx.xxx.yy.main(sub:132)
I/System.out( 3408): dalvik.system.NativeStart.main(Native Method)
I/System.out( 3408): dalvik.system.NativeStart.run(Native Method)
I/System.out( 3408): dalvik.system.NativeStart.run(Native Method)
I/System.out( 3408): dalvik.system.NativeStart.run(Native Method)
I/System.out( 3408): java.lang.Object.wait(Native Method)
I/System.out( 3408): java.lang.Object.wait(Object.java:401)
I/System.out( 3408): java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:102)
I/System.out( 3408): java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:73)
I/System.out( 3408): java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170)
I/System.out( 3408): java.lang.Thread.run(Thread.java:841)
I/System.out( 3408): dalvik.system.NativeStart.run(Native Method)
I/System.out( 3408): java.lang.Object.wait(Native Method)
I/System.out( 3408): java.lang.Object.wait(Object.java:364)
I/System.out( 3408): java.lang.Daemons$ReferenceQueueDaemon.run(Daemons.java:130)
I/System.out( 3408): java.lang.Thread.run(Thread.java:841)
I/System.out( 3408): java.lang.VMThread.sleep(Native Method)
I/System.out( 3408): java.lang.Thread.sleep(Thread.java:1013)
I/System.out( 3408): java.lang.Thread.sleep(Thread.java:995)
I/System.out( 3408): java.lang.Daemons$FinalizerWatchdogDaemon.sleepFor(Daemons.java:248)
I/System.out( 3408): java.lang.Daemons$FinalizerWatchdogDaemon.waitForFinalization(Daemons.java:258)
I/System.out( 3408): java.lang.Daemons$FinalizerWatchdogDaemon.run(Daemons.java:212)
I/System.out( 3408): java.lang.Thread.run(Thread.java:841)
I/System.out( 3408): java.lang.Object.wait(Native Method)
I/System.out( 3408): java.lang.Object.wait(Object.java:364)
I/System.out( 3408): java.lang.ProcessManager.waitForMoreChildren(ProcessManager.java:140)
I/System.out( 3408): java.lang.ProcessManager.watchChildren(ProcessManager.java:105)
I/System.out( 3408): java.lang.ProcessManager.access$000(ProcessManager.java:40)
I/System.out( 3408): java.lang.ProcessManager$1.run(ProcessManager.java:58)
I/System.out( 3408): ==================方式三===================

你可能感兴趣的:(Hook函数Thread.currentThread().getStackTrace())