ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java代码转汇编代码的方法

2021-06-04 11:00:48  阅读:318  来源: 互联网

标签:lang 汇编 Java invoke 代码 XX MethodHandle java method


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有