(blog-of ‘Alex’)

A personal blog about software development and other things. All opinions expressed here are my own, unless explicitly stated.

Java and Assembly

Did you know that modern JVM is able to print assembly for generated machine code?

If you want to try this option you’ll need an hsdis plugin.

Then put the downloaded library to the location your OS aware of. E.g. if you’re using linux all you need is to update LD_LIBRARY_PATH:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/somewhere/on/your/computer/bin/hsdis"

Then you must be able to start java with the PrintAssembly flag, e.g.:

/usr/lib/jvm/java-6-sun/bin/java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -server -cp . MySuperCoolJavaApp

The dissassembled output might look as follows:

  0xb3a15f0f: jmp    0xb3a15f1b         ;*invokespecial RangeCheck
                                        ; - java.util.ArrayList::get@2 (line 322)
                                        ; - App$Finder::findSolution@33 (line 243)
                                        ; - App$Finder::findSolution@182 (line 263)
                                        ; - App$Finder::findSolution@182 (line 263)
  0xb3a15f11: mov    %eax,%ecx
  0xb3a15f13: jmp    0xb3a15f1b         ;*invokespecial RangeCheck
        Total: 13211984ns
                                ; - java.util.ArrayList::get@2 (line 322)
                                        ; - App$Finder::findSolution@33 (line 243)
                                        ; - App$Finder::findSolution@182 (line 263)
  0xb3a15f15: mov    %eax,%ecx
  0xb3a15f17: jmp    0xb3a15f1b         ;*invokespecial RangeCheck
                                        ; - java.util.ArrayList::get@2 (line 322)
                                        ; - App$Finder::findSolution@33 (line 243)
  0xb3a15f19: mov    %eax,%ecx          ;*synchronization entry
                                        ; - App$Finder::findSolution@-1 (line 236)
  0xb3a15f1b: add    $0x68,%esp
  0xb3a15f1e: pop    %ebp
  0xb3a15f1f: jmp    0xb3a11da0         ;*return
                                        ; - App$Finder::findSolution@27 (line 240)
                                        ;   {runtime_call}
  0xb3a15f24: hlt    
  0xb3a15f25: hlt    

Cool, isn’t it?