编程语言
首页 > 编程语言> > Java代码转汇编代码的方法

Java代码转汇编代码的方法

作者:互联网

1、下载hsdis-amd64.dylib工具

链接: https://pan.baidu.com/s/1Qpww4ysxMQXgdekiSuxdIA 密码: 1jva


2、将hsdis-amd64.dylib放到Jdk对应文件夹中

将工具放到
…/jdk1.8.0_181.jdk/Contents/Home/jre/lib/server/


3、使用hsdis-amd64.dylib工具

通过在执行java命令的时候增加配置参数,执行java文件的同时输出其汇编代码。
参数为:-server -Xcomp -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand=compileonly

以代码V.java为例

public class V {
    private static volatile int a_value;
    public static void a_test(){
        a_value = 1;
    }

    public static void main(String[] args) {
        a_test();
        System.out.println("aaaaa");
    }
}
方法一 命令行执行

java -server -Xcomp -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand=compileonly …/V.java

除了输出aaaaa外,还输出了汇编代码,代码非常多就是了。

如果觉得代码太多,可以针对某个方法进行汇编代码的输出。在XX:CompileCommand=compileonly后增加对应方法即可。
例如:… XX:CompileCommand=compileonly,*V.a_test()
该操作我没做命令行试过,我都是在ide里边配置的。

aaaaa
server jjleong$ java -server -Xcomp -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand=compileonly .../V.java
Java HotSpot(TM) 64-Bit Server VM warning: PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output
CompileCommand: An error occurred during parsing
Line: compileonly
Error: Could not parse method pattern

Usage: '-XX:CompileCommand=command,"package/Class.method()"'
Use:   '-XX:CompileCommand=help' for more information.

Compiled method (c1)      22    1       3       java.lang.invoke.MethodHandle::<clinit> (30 bytes)
 total in heap  [0x000000011a7cf010,0x000000011a7cf860] = 2128
 relocation     [0x000000011a7cf188,0x000000011a7cf208] = 128
 main code      [0x000000011a7cf220,0x000000011a7cf5a0] = 896
 stub code      [0x000000011a7cf5a0,0x000000011a7cf658] = 184
 oops           [0x000000011a7cf658,0x000000011a7cf668] = 16
 metadata       [0x000000011a7cf668,0x000000011a7cf678] = 16
 scopes data    [0x000000011a7cf678,0x000000011a7cf6f0] = 120
 scopes pcs     [0x000000011a7cf6f0,0x000000011a7cf840] = 336
 dependencies   [0x000000011a7cf840,0x000000011a7cf848] = 8
 nul chk table  [0x000000011a7cf848,0x000000011a7cf860] = 24
Loaded disassembler from hsdis-amd64.dylib
----------------------------------------------------------------------
java/lang/invoke/MethodHandle.<clinit>()V  [0x000000011a7cf220, 0x000000011a7cf658]  1080 bytes
[Disassembling for mach='i386:x86-64']
[Entry Point]
[Verified Entry Point]
[Constants]
  # {method} {0x00000008000a7400} '<clinit>' '()V' in 'java/lang/invoke/MethodHandle'
  #           [sp+0x60]  (sp of caller)
  0x000000011a7cf220: mov    %eax,-0x14000(%rsp)
  0x000000011a7cf227: push   %rbp
  0x000000011a7cf228: sub    $0x50,%rsp
  0x000000011a7cf22c: movabs $0x133245bf0,%rsi              ;   {metadata(method data for {method} {0x00000008000a7400} '<clinit>' '()V' in 'java/lang/invoke/MethodHandle')}
  0x000000011a7cf236: mov    0x104(%rsi),%edi
  0x000000011a7cf23c: add    $0x8,%edi
  0x000000011a7cf23f: mov    %edi,0x104(%rsi)
  0x000000011a7cf245: jmpq   0x000000011a7cf3ae             ;*ldc {reexecute=0 rethrow=0 return_oop=0}
                                                            ; - java.lang.invoke.MethodHandle::<clinit>@0 (line 440)

  0x000000011a7cf24a: movabs $0x7bfe2fd18,%rsi              ;   {oop(a 'java/lang/Class'{0x00000007bfe2fd18} = 'java/lang/invoke/MethodHandle')}
  0x000000011a7cf254: mov    %rsi,%rdi
  0x000000011a7cf257: movabs $0x133245bf0,%rbx              ;   {metadata(method data for {method} {0x00000008000a7400} '<clinit>' '()V' in 'java/lang/invoke/MethodHandle')}
  0x000000011a7cf261: addq   $0x1,0x140(%rbx)
  0x000000011a7cf269: nopw   0x0(%rax,%rax,1)
  0x000000011a7cf26f: callq  0x000000011a265900             ; ImmutableOopMap{}
                                                            ;*invokevirtual desiredAssertionStatus {reexecute=0 rethrow=0 return_oop=0}
                                                            ; - java.lang.invoke.MethodHandle::<clinit>@2 (line 440)
                                                            ;   {optimized virtual_call}
  0x000000011a7cf274: cmp    $0x0,%eax
  0x000000011a7cf277: movabs $0x133245bf0,%rdx              ;   {metadata(method data for {method} {0x00000008000a7400} '<clinit>' '()V' in 'java/lang/invoke/MethodHandle')}
  0x000000011a7cf281: movabs $0x178,%rcx
  0x000000011a7cf28b: jne    0x000000011a7cf29b
  0x000000011a7cf291: movabs $0x188,%rcx
  0x000000011a7cf29b: mov    (%rdx,%rcx,1),%rsi
  0x000000011a7cf29f: lea    0x1(%rsi),%rsi
  ...

方法二 ide执行

以android studio为例,在Run Config里边配置VM options即可
在这里插入图片描述

标签:lang,汇编,Java,invoke,代码,XX,MethodHandle,java,method
来源: https://blog.csdn.net/yudan505/article/details/117551604