在这个 关联 rustyx
提到他使用调试虚拟机和以下标志-XX:+PrintOptoAssembly
来获取他的 java 字节码的汇编指令
我安装了调试虚拟机sudo apt-get install openjdk-17-dbg
我使用命令java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly Main
我习惯使用 gdb 在 C++ 中执行此操作,但无法使其与 java 一起使用 printAssembly 仅打印指令的十六进制值,如下面的示例,它重复多次,没有我的实际代码函数或任何有意义的符号的迹象
abstract class Base {
Base(){
System.out.println("Base Constructor Called");
}
abstract void fun();
}
class Dervied extends Base {
Dervied(){
System.out.println("Dervied Constructor called");
}
void fun(){
int x = 0;
x = x + 1;
System.out.println("Dervied fun() called " + x);
}
}
class Main {
public static void main(String args[]){
Dervied d = new Dervied();
d.fun();
}
}
32 1 3 java.lang.Object::<init> (1 bytes)
============================= C1-compiled nmethod ==============================
----------------------------------- Assembly -----------------------------------
Compiled method (c1) 32 1 3 java.lang.Object::<init> (1 bytes)
total in heap [0x00007fca25878010,0x00007fca25878320] = 784
relocation [0x00007fca25878170,0x00007fca258781a0] = 48
main code [0x00007fca258781a0,0x00007fca25878280] = 224
stub code [0x00007fca25878280,0x00007fca258782b0] = 48
metadata [0x00007fca258782b0,0x00007fca258782c0] = 16
scopes data [0x00007fca258782c0,0x00007fca258782d8] = 24
scopes pcs [0x00007fca258782d8,0x00007fca25878318] = 64
dependencies [0x00007fca25878318,0x00007fca25878320] = 8
[Constant Pool (empty)]
[MachCode]
[Entry Point]
# {method} {0x0000000800448920} '<init>' '()V' in 'java/lang/Object'
# [sp+0x40] (sp of caller)
0x00007fca258781a0: 448b 5608 | 49bb 0000 | 0000 0800 | 0000 4d03 | d34c 3bd0
0x00007fca258781b4: ; {runtime_call ic_miss_stub}
0x00007fca258781b4: 0f85 c6a5 | abff 660f | 1f44 0000
[Verified Entry Point]
0x00007fca258781c0: 8984 2400 | c0fe ff55 | 4883 ec30
0x00007fca258781cc: ; {metadata(method data for {method} {0x0000000800448920} '<init>' '()V' in 'java/lang/Object')}
0x00007fca258781cc: 48bf 1888 | 801c ca7f | 0000 8b9f | f400 0000 | 83c3 0289 | 9ff4 0000 | 0081 e3fe | 0700 0083
0x00007fca258781ec: fb00 0f84
0x00007fca258781f0: ;*return {reexecute=0 rethrow=0 return_oop=0}
; - java.lang.Object::<init>@0 (line 44)
0x00007fca258781f0: 1300 0000 | 4883 c430
0x00007fca258781f8: ; {poll_return}
0x00007fca258781f8: 5d49 3ba7 | 4003 0000 | 0f87 1f00
0x00007fca25878204: ; {metadata({method} {0x0000000800448920} '<init>' '()V' in 'java/lang/Object')}
0x00007fca25878204: 0000 c349 | ba20 8944 | 0008 0000 | 004c 8954 | 2408 48c7 | 0424 ffff
0x00007fca2587821c: ; {runtime_call counter_overflow Runtime1 stub}
0x00007fca2587821c: ffff e8dd
0x00007fca25878220: ; ImmutableOopMap {rsi=Oop }
;*synchronization entry
; - java.lang.Object::<init>@-1 (line 44)
0x00007fca25878220: 6ab6 ffeb
0x00007fca25878224: ; {internal_word}
0x00007fca25878224: cf49 baf9 | 8187 25ca | 7f00 004d | 8997 5803
0x00007fca25878234: ; {runtime_call SafepointBlob}
0x00007fca25878234: 0000 e9c5 | 0eac ff90 | 9049 8b87 | d003 0000 | 49c7 87d0 | 0300 0000 | 0000 0049 | c787 d803
0x00007fca25878254: 0000 0000 | 0000 4883
0x00007fca2587825c: ; {runtime_call unwind_exception Runtime1 stub}
0x00007fca2587825c: c430 5de9 | 9c01 b6ff | f4f4 f4f4 | f4f4 f4f4 | f4f4 f4f4 | f4f4 f4f4 | f4f4 f4f4 | f4f4 f4f4
0x00007fca2587827c: f4f4 f4f4
[Exception Handler]
0x00007fca25878280: ; {no_reloc}
0x00007fca25878280: e87b 31b6
0x00007fca25878284: ; {external_word}
0x00007fca25878284: ff48 bf6a | 7482 41ca | 7f00 0048
0x00007fca25878290: ; {runtime_call}
0x00007fca25878290: 83e4 f0e8 | 080a ae1b
0x00007fca25878298: ; {section_word}
0x00007fca25878298: f449 ba99 | 8287 25ca | 7f00 0041
0x00007fca258782a4: ; {runtime_call DeoptimizationBlob}
0x00007fca258782a4: 52e9 f600 | acff f4f4 | f4f4 f4f4
[/MachCode]
34 2 3 java.lang.String::hashCode (60 bytes)
答案1
使用+PrintAssembly
,您需要一个反汇编器插件。如果您使用的是 Debian 或其衍生版本,请安装libhsdis0-fcml
.
请注意,这openjdk-17-dbg
不是一个调试 JDK;而是一个调试 JDK。它是一个包含常规 JDK 调试符号的包。