简体   繁体   English

是否已在正在运行的JVM中调用此方法

[英]Has this method ever been called inside a running JVM

Is there a way to figure out if a method ever been called inside running JVM. 有没有办法弄清楚是否在运行JVM中调用了一个方法。 Let's say I have the following method information and I want to know if it has ever been called: 假设我有以下方法信息,我想知道它是否曾被调用过:

    "methodId": {
      "className": "InvokerParser",
      "filePath": "org/foo/commons/functors/InvokerParserformer.java",
      "methodName": "parser"
    }

If the application is running on HotSpot JVM, it is possible to get the information about the given method using HotSpot Serviceability Agent. 如果应用程序在HotSpot JVM上运行,则可以使用HotSpot Serviceability Agent获取有关给定方法的信息。

Here is a tool that checks whether the method has been called in a running JVM. 这是一个工具,用于检查是否已在正在运行的JVM中调用该方法。

import sun.jvm.hotspot.oops.InstanceKlass;
import sun.jvm.hotspot.oops.Method;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.tools.Tool;

public class CheckMethodCall extends Tool {
    private static final String className = "java/util/HashMap";
    private static final String methodName = "get";
    private static final String methodSig = "(Ljava/lang/Object;)Ljava/lang/Object;";

    @Override
    public void run() {
        boolean[] result = new boolean[2];

        VM.getVM().getSystemDictionary().classesDo(klass -> {
            if (klass.getName().asString().equals(className)) {
                Method method = ((InstanceKlass) klass).findMethod(methodName, methodSig);
                if (method != null) {
                    result[0] = true;
                    result[1] = method.getMethodCounters() != null &&
                        method.getInvocationCount() + method.interpreterInvocationCount() > 0;
                }
            }
        });

        if (!result[0]) {
            System.out.println("Method not found");
        } else if (result[1]) {
            System.out.println("Method has been called");
        } else {
            System.out.println("Method has NOT been called");
        }
    }

    public static void main(String[] args) {
        new CheckMethodCall().execute(args);
    }
}

It requires sa-jdi.jar in classpath (comes with JDK 8). 它需要在类路径中使用sa-jdi.jar (随JDK 8一起提供)。

Run

java -cp $JAVA_HOME/lib/sa-jdi.jar:. CheckMethodCall <pid>

where <pid> is Java process ID to check. 其中<pid>是要检查的Java进程ID。

UPDATE UPDATE

A similar tool for JDK 11+ JDK 11+的类似工具
Use --add-modules=jdk.hotspot.agent and export all the required packages. 使用--add-modules=jdk.hotspot.agent并导出所有必需的包。

import sun.jvm.hotspot.oops.InstanceKlass;
import sun.jvm.hotspot.oops.Klass;
import sun.jvm.hotspot.oops.Method;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.tools.Tool;

public class CheckMethodCall extends Tool {
    private static final String className = "java/util/HashMap";
    private static final String methodName = "get";
    private static final String methodSig = "(Ljava/lang/Object;)Ljava/lang/Object;";

    @Override
    public void run() {
        Klass klass = VM.getVM().getClassLoaderDataGraph().find(className);
        if (klass == null) {
            System.out.println("Class not found");
            return;
        }

        Method method = ((InstanceKlass) klass).findMethod(methodName, methodSig);
        if (method == null) {
            System.out.println("Method not found");
            return;
        }

        boolean called = method.getMethodCounters() != null &&
                method.getInvocationCount() + method.interpreterInvocationCount() > 0;
        System.out.println("Method " + (called ? "has been" : "has NOT been") + " called");
    }

    public static void main(String[] args) {
        new CheckMethodCall().execute(args);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM