如何查看JAVA程序的汇编代码

 

如何查看JAVA程序的汇编代码

查看汇编需要加入虚拟机参数 

java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand=compileonly,*TestVolatile1.getInstance TestVolatile1

java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand=print,*TestVolatile1.getInstance  TestVolatile1

运行后可能会出现 
Could not load hsdis-amd64.so; library not loadable; PrintAssembly is disabled 
很明显是缺失hsdis-amd64.so这个库(linux下)

解决方案: 
需要的库源码下载地址:https://sourceforge.net/projects/fcml/files/fcml-1.1.3/

下载hsdis-amd64.dll

Linux下: 

1. 下载源码并解压 
2. 切换到目标目录 
3. ./configure && make && sudo make install 
4. cd example/hsdis && make && sudo make install 
5. sudo ln -s /usr/local/lib/libhsdis.so <JDK PATH>/lib/amd64/hsdis-amd64.so 
6. sudo ln -s /usr/local/lib/libhsdis.so <JDK PATH>/jre/lib/amd64/hsdis-amd64.so 


接下来便可以使用

Windos下: 

1. 下载压缩包并提取dll文件 
2. 搜索Windos下JDK目录和JRE目录找到含有java.dll的目录,我的是包含在 
C:\Program Files\Java\jre1.8.0_45\bin\server 
C:\Program Files\Java\jre1.8.0_45\jre\bin\server 
3. 复制到搜索的目录中

如果不行就放在  将hsdis-amd64.dll与hsdis-amd64.lib两个文件放在%JAVA_HOME%\jre\bin\server路径下即可

windows下执行 cmd 

echo %JAVA_HOME%

cmd 下使用Java 命令太费事,这么干


package JavaThread;


public class TestVolatile1 {
    public volatile static TestVolatile1 instance;
    
    public static TestVolatile1 getInstance(){
        if(instance == null){
            synchronized(TestVolatile1.class){
                if(instance== null){
                    instance = new TestVolatile1();
                }
            }
        }
        return instance;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        TestVolatile1.getInstance();
    }

}

过滤输出

PrintAssembly 打印JIT编译后的汇编
PrintInterpreter 打印解释的汇编

-XX:CompileCommand=print,*MyClass.myMethod prints assembly for just one method
-XX:CompileCommand=option,*MyClass.myMethod,PrintOptoAssembly (debug build only) produces the old print command output
-XX:CompileCommand=option,*MyClass.myMethod,PrintNMethods produces method dumps

猜你喜欢

转载自blog.csdn.net/wangming520liwei/article/details/81561922