How to print assembly for your Java code in OS X

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.

Advertisements

4 thoughts on “How to print assembly for your Java code in OS X

  1. Hi Dhruba,

    I really enjoy your blog, you have some great tips in here.

    I’m working again on some java code that need a good concurrent MultiMap, and some time ago you showed some implementations on that topic: http://dhruba.name/2011/06/11/concurrency-pattern-concurrent-multimaps-in-java/

    I just wanted to ask you, on the final implementation, you mentioned a striped lock provider, but you never showed the implementation for that class, you even mentioned that you were going to write a post on that topic later.

    I was hoping you could share that implementation with us, your readers.

    Thanks!

    1. You’re awesome!

      Thanks so much! and I’m looking forward if you could someday do the article on explaining striped locking as I can’t understand what that is all about.

  2. Hi Raul,

    I cannot take the credit for this. It’s derived from EhCache written by Greg Luck who in turn derived it from Doug Lea (as you can probably tell by the variable naming).

    If you want to see the original code read through the classes here.

    http://svn.terracotta.org/svn/ehcache/trunk/ehcache/ehcache-core/src/main/java/net/sf/ehcache/concurrent/

    Specifically look at the two classes below.

    http://svn.terracotta.org/svn/ehcache/trunk/ehcache/ehcache-core/src/main/java/net/sf/ehcache/concurrent/StripedReadWriteLockSync.java

    http://svn.terracotta.org/svn/ehcache/trunk/ehcache/ehcache-core/src/main/java/net/sf/ehcache/concurrent/ConcurrencyUtil.java

    Enjoy the reading. We build on the shoulders of giants.

    Dhruba.

    P.S. Forgot to say the hash function is actually identical to the one in java.util.HashMap.hash() in Java 7 and earlier and guarantees a bounded number of collisions and thereby a good distribution of value resolution.

Leave a Reply to Raul Guerrero Cancel reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s