0. Write program.
[java]
package name.dhruba;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
[/java]
1. Add JVM arguments to your program.
-XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly
2. Run your program. You will probably see the output below.
Java HotSpot(TM) 64-Bit Server VM warning: PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output Could not load hsdis-amd64.dylib; library not loadable; PrintAssembly is disabled Hello World! Process finished with exit code 0
3. Download the missing library from here. The direct link to the lib file itself is here. I downloaded the one named ‘gnu-bsd-libhsdis-amd64.dylib’ as I’m running 64-bit. This produces a file on your system called ‘hsdis-amd64.dylib’.
4. Move it to where, the JDK that you are running with, looks for it. I was using Java8.
sudo mv ~/Downloads/hsdis-amd64.dylib /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/hsdis-amd64.dylib
5. Run your program again and see assembly! The output for Hello World is huge so I can’t paste all of it here but here’s the initial bit where you can see that the disassembler has been loaded.
Java HotSpot(TM) 64-Bit Server VM warning: PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output Loaded disassembler from /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/hsdis-amd64.dylib Decoding compiled method 0x000000010f4c23d0: Code: [Disassembling for mach='i386:x86-64'] [Entry Point] # {method} {0x00000001280fe660} 'arraycopy' '(Ljava/lang/Object;ILjava/lang/Object;II)V' in 'java/lang/System' # parm0: rsi:rsi = 'java/lang/Object' # parm1: rdx = int # parm2: rcx:rcx = 'java/lang/Object' # parm3: r8 = int # parm4: r9 = int # [sp+0x60] (sp of caller) 0x000000010f4c2540: mov 0x8(%rsi),%r10d 0x000000010f4c2544: shl $0x3,%r10 0x000000010f4c2548: cmp %r10,%rax 0x000000010f4c254b: je 0x000000010f4c2558 0x000000010f4c2551: jmpq 0x000000010f407b60 ; {runtime_call} 0x000000010f4c2556: xchg %ax,%ax
Credit: Nitsan Wakart.