怎么看.java文件对应的汇编文件?
比如,想知道synchronized底层怎么实现的:
public class T {
public static void main(String[] args) {
Object i = new Object();
synchronized (i) {
System.out.println("===========================");
}
}
}
Java字节码的反汇编器和汇编器-asmtools
asmtools - asmtools - OpenJDK Wiki
The AsmTools open source project is used to develop tools for the production of proper and improper Java '.class' files. AsmTools are being opened in order to facilitate a community of Java .class file production for various testing and other OpenJDK development applications.
asmtools下载:
https://hg.openjdk.org/code-tools/asmtools/
解压
unzip tip.zip
安装ant
Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications.
wget https://dlcdn.apache.org//ant/binaries/apache-ant-1.9.16-bin.tar.gz
因为需要用ant build asmtools 所以安装ant
tar xzvf apache-ant-1.9.16-bin.tar.gz
vim /etc/profile
export ANT_HOME=/root/test/apache-ant-1.9.16
export PATH=${ANT_HOME}/bin:$PATH
source /etc/profile
验证ant是否安装成功并构建
ant -version
Apache Ant(TM) version 1.9.16 compiled on July 10 2021
cd /root/test/asmtools/build
ant
asmtools.jar 位置
jar:
[copy] Copying 1 file to /root/test/asmtools-7.0-build/binaries/classes
[jar] Building jar: /root/test/asmtools-7.0-build/binaries/lib/asmtools.jar
反汇编 (.class->asm)
将字节码反汇编成人类可读的代码格式
java -cp /root/test/asmtools-7.0-build/binaries/lib/asmtools.jar org.openjdk.asmtools.jdis.Main T.class > T.jasm.1
[root@localhost mycode]# cat T.jasm.1
super public class T
version 52:0
{
public Method "<init>":"()V"
stack 1 locals 1
{
aload_0;
invokespecial Method java/lang/Object."<init>":"()V";
return;
}
public static Method main:"([Ljava/lang/String;)V"
stack 2 locals 4
{
new class java/lang/Object;
dup;
invokespecial Method java/lang/Object."<init>":"()V";
astore_1;
aload_1;
dup;
astore_2;
monitorenter;
try t0;
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
ldc String "===========================";
invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
aload_2;
monitorexit;
endtry t0;
goto L30;
catch t0 #0;
catch t1 #0;
try t1;
stack_frame_type full;
locals_map class "[Ljava/lang/String;", class java/lang/Object, class java/lang/Object;
stack_map class java/lang/Throwable;
astore_3;
aload_2;
monitorexit;
endtry t1;
aload_3;
athrow;
L30: stack_frame_type chop1;
return;
}
} // end Class T
汇编 (asm->class)
将人类可读的代码格式汇编成Java字节码 .class文件
rm -rf T.class
java -cp /root/test/asmtools-7.0-build/binaries/lib/asmtools.jar org.openjdk.asmtools.jasm.Main T.jasm.1
会生成T.class
执行,可看到打印结果
java T
===========================
synchronized 相关的两个指令
monitorenter;
monitorexit;
class文件的样子
hexdump 将二进制文件转成16进制格式打印
[root@localhost test]# less T.class
"T.class" may be a binary file. See it anyway?
[root@localhost test]# hexdump T.class
0000000 feca beba 0000 3400 2900 000a 0009 0918
0000010 1900 1a00 0008 0a1b 1c00 1d00 000a 0008
0000020 0a1e 0800 1f00 0009 0008 0720 2100 0007
0000030 0122 0100 0169 0100 0149 0600 693c 696e
0000040 3e74 0001 2803 5629 0001 4304 646f 0165
0000050 0f00 694c 656e 754e 626d 7265 6154 6c62
0000060 0165 0400 616d 6e69 0001 2816 4c5b 616a
0000070 6176 6c2f 6e61 2f67 7453 6972 676e 293b
0000080 0156 0d00 7453 6361 4d6b 7061 6154 6c62
0000090 0165 0100 016d 0100 016e 0800 633c 696c
00000a0 696e 3e74 0001 530a 756f 6372 4665 6c69
00000b0 0165 0600 2e54 616a 6176 000c 000c 070d
00000c0 2300 000c 0024 0125 1000 2d2d 2d2d 2d2d
00000d0 2d2d 2d2d 2d2d 2d2d 2d2d 0007 0c26 2700
00000e0 2800 000c 0013 0c0d 1400 0d00 000c 000a
00000f0 010b 0100 0154 1000 616a 6176 6c2f 6e61
0000100 2f67 624f 656a 7463 0001 6a10 7661 2f61
0000110 616c 676e 532f 7379 6574 016d 0300 756f
0000120 0174 1500 6a4c 7661 2f61 6f69 502f 6972
0000130 746e 7453 6572 6d61 013b 1300 616a 6176
0000140 692f 2f6f 7250 6e69 5374 7274 6165 016d
0000150 0700 7270 6e69 6c74 016e 1500 4c28 616a
0000160 6176 6c2f 6e61 2f67 7453 6972 676e 293b
0000170 0056 0021 0008 0009 0000 0001 0049 000a
0000180 000b 0000 0005 0001 000c 000d 0001 000e
0000190 0000 001d 0001 0001 0000 2a05 00b7 b101
00001a0 0000 0100 0f00 0000 0600 0100 0000 0200
00001b0 0900 1000 1100 0100 0e00 0000 5900 0200
00001c0 0200 0000 1e00 00b2 1202 b603 0400 3c03
00001d0 111b e803 00a2 b80f 0500 00b8 8406 0101
00001e0 ffa7 b1f0 0000 0200 0f00 0000 1a00 0600
00001f0 0000 0600 0800 0700 1100 0800 1400 0900
0000200 1700 0700 1d00 0b00 1200 0000 0900 0200
0000210 00fc 010a 00fa 0012 0029 0013 000d 0001
0000220 000e 0000 0019 0000 0000 0000 b101 0000
0000230 0100 0f00 0000 0600 0100 0000 0e00 0900
0000240 1400 0d00 0100 0e00 0000 2100 0100 0000
0000250 0000 0500 b304 0700 00b1 0000 0001 000f
0000260 0000 000a 0002 0000 0011 0004 0012 0008
0000270 0015 000d 0001 000e 0000 001d 0001 0000
0000280 0000 0305 00b3 b107 0000 0100 0f00 0000
0000290 0600 0100 0000 0300 0100 1600 0000 0200
00002a0 1700
00002a2