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

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

查看汇编需要加入虚拟机参数
-XX:+UnlockDiagnosticVMOptions -XX:+PrintAssmbly

运行后可能会出现
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/

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 /lib/amd64/hsdis-amd64.so
6. sudo ln -s /usr/local/lib/libhsdis.so /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. 复制到搜索的目录中

最终效果:
编译的java代码

public class Test{
    public static void main(String[] args){
        System.out.println(1);
    }
}

显示的汇编代码:

Java HotSpot(TM) 64-Bit Server VM warning: PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output
Loaded disassembler from /var/software/java/jdk1.8.0_151/jre/lib/amd64/hsdis-amd64.so
Decoding compiled method 0x00007f56f9102150:
Code:
Argument 0 is unknown.RIP: 0x7f56f91022a0 Code size: 0x00000110
[Disassembling for mach='amd64']
[Entry Point]
[Constants]
  # {method} {0x00007f56f3211c38} 'getSnapshotTransformerList' '()[Lsun/instrument/TransformerManager$TransformerInfo;' in 'sun/instrument/TransformerManager'
  #           [sp+0x40]  (sp of caller)
  0x00007f56f91022a0: mov     0x8(%rsi),%r10d
  0x00007f56f91022a4: shl     $0x3,%r10
  0x00007f56f91022a8: cmp     %rax,%r10
  0x00007f56f91022ab: jne     0x7f56f9045e20    ;   {runtime_call}
  0x00007f56f91022b1: nopw    0x0(%rax,%rax)
  0x00007f56f91022bc: nop
[Verified Entry Point]
  0x00007f56f91022c0: mov     %eax,0xfffffffffffec000(%rsp)
  0x00007f56f91022c7: push    %rbp
  0x00007f56f91022c8: sub     $0x30,%rsp        ;*aload_0
                                                ; - sun.instrument.TransformerManager::getSnapshotTransformerList@0 (line 166)

  0x00007f56f91022cc: mov     0x10(%rsi),%eax
  0x00007f56f91022cf: shl     $0x3,%rax         ;*getfield mTransformerList
                                                ; - sun.instrument.TransformerManager::getSnapshotTransformerList@1 (line 166)

  0x00007f56f91022d3: add     $0x30,%rsp
  0x00007f56f91022d7: pop     %rbp
  0x00007f56f91022d8: test    %eax,0x16e0ee22(%rip)  ;   {poll_return}
  0x00007f56f91022de: retq
  0x00007f56f91022df: nop
  0x00007f56f91022e0: nop
  0x00007f56f91022e1: mov     0x2a8(%r15),%rax
  0x00007f56f91022e8: movabs  $0x0,%r10
  0x00007f56f91022f2: mov     %r10,0x2a8(%r15)
  0x00007f56f91022f9: movabs  $0x0,%r10
  0x00007f56f9102303: mov     %r10,0x2b0(%r15)
  0x00007f56f910230a: add     $0x30,%rsp
  0x00007f56f910230e: pop     %rbp
  0x00007f56f910230f: jmpq    0x7f56f906e2e0    ;   {runtime_call}
  0x00007f56f9102314: hlt
  0x00007f56f9102315: hlt
  0x00007f56f9102316: hlt
  0x00007f56f9102317: hlt
  0x00007f56f9102318: hlt
  0x00007f56f9102319: hlt
  0x00007f56f910231a: hlt
  0x00007f56f910231b: hlt
  0x00007f56f910231c: hlt
  0x00007f56f910231d: hlt
  0x00007f56f910231e: hlt
  0x00007f56f910231f: hlt
[Exception Handler]
[Stub Code]
  0x00007f56f9102320: callq   0x7f56f90fd660    ;   {no_reloc}
  0x00007f56f9102325: mov     %rsp,0xffffffffffffffd8(%rsp)
  0x00007f56f910232a: sub     $0x80,%rsp
  0x00007f56f9102331: mov     %rax,0x78(%rsp)
  0x00007f56f9102336: mov     %rcx,0x70(%rsp)
  0x00007f56f910233b: mov     %rdx,0x68(%rsp)
  0x00007f56f9102340: mov     %rbx,0x60(%rsp)
  0x00007f56f9102345: mov     %rbp,0x50(%rsp)
  0x00007f56f910234a: mov     %rsi,0x48(%rsp)
  0x00007f56f910234f: mov     %rdi,0x40(%rsp)
  0x00007f56f9102354: mov     %r8,0x38(%rsp)
  0x00007f56f9102359: mov     %r9,0x30(%rsp)
  0x00007f56f910235e: mov     %r10,0x28(%rsp)
  0x00007f56f9102363: mov     %r11,0x20(%rsp)
  0x00007f56f9102368: mov     %r12,0x18(%rsp)
  0x00007f56f910236d: mov     %r13,0x10(%rsp)
  0x00007f56f9102372: mov     %r14,0x8(%rsp)
  0x00007f56f9102377: mov     %r15,(%rsp)
  0x00007f56f910237b: movabs  $0x7f570ee0a316,%rdi  ;   {external_word}
  0x00007f56f9102385: movabs  $0x7f56f9102325,%rsi  ;   {internal_word}
  0x00007f56f910238f: mov     %rsp,%rdx
  0x00007f56f9102392: and     $0xfffffffffffffff0,%rsp
  0x00007f56f9102396: callq   0x7f570eb35d80    ;   {runtime_call}
  0x00007f56f910239b: hlt

内容翻译转载,原答案出自:
https://stackoverflow.com/questions/1503479/how-to-see-jit-compiled-code-in-jvm/24524285#24524285?newreg=de4f506929a54c10b77d7e9e1d1bb94e

内容过多只显示部分
趁着时光还没有关注你的时候多看看,多学一点

你可能感兴趣的:(linux系统,java语言)