Eval4j @ JVMLS 2014

533 views
448 views

Published on

eval4j: a JVM bytecode interpreter written in Java, used to implement "Evaluate Expression" functionality in debuggers for JVM Languages

Video: http://medianetwork.oracle.com/video/player/3731152022001

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
533
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
4
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Eval4j @ JVMLS 2014

  1. 1. eval4j compiling expressions to be evaluated by the debugger andrey.breslav@ .com
  2. 2. Stop at a breakpoint Inspect memoryInspect call stacks
  3. 3. Evaluate given expression Evaluate given expression
  4. 4. Working Horse: Bare JDI • Access Variables • Access Fields • Call Methods • No Evaluate Expression out-of-the-box
  5. 5. How it’s done for Java • IntelliJ has an intepreter for Java expressions – written from scratch – yet another definition of semantics – may disagree with the compiler’s semantics • Well, IntelliJ’s not very intimate with javac
  6. 6. When you have your own compiler? • Eclipse Java DT still has an interpreter • Scala IDE (Eclipse) — no Evaluate Expression • Ceylon IDE (Eclipse) — no Evaluate Expression • There’s some room for improvement…
  7. 7. Reuse the Compiler (Take 1) a + b .class file Debugee process compile load run
  8. 8. Eval4j: Bytecode Interpreter • foo.x + 2 – ALOAD 1 – GETFIELD Foo.x : I – ICONST_2 – IADD – INVOKESTATIC kotlin/io/IoPackage.println (I)V • https://github.com/abreslav/eval4j • Re-uses ASM’s Interpreter
  9. 9. Compiling an expression free variables Extract Method Refactoring
  10. 10. Summary so far Extract Method + Compiler + eval4j = Evaluate Expression
  11. 11. Challenge: Lambdas/Anonymous Classes • Not supported for Java/Scala • Problems: – New class that is not available in the debugee process – That class may access privates that it’s not allowed to access
  12. 12. Challenge: New Classes private? • Also: How do you load a class into the debugee?
  13. 13. Loading a class… • protected Class<?> defineClass(byte[], …)
  14. 14. How Privates Work Normally • Special synthetic accessor methods – INVOKESTATIC Foo.access$001() – can’t insert a new method into the outer class
  15. 15. Prepare the methods of the lambda • Evaluate the body of the method public Object invoke() { Object result = null; Throwable exception = null; if (exception == null) return result; else throw exception; }
  16. 16. What happens to your thread • You stopped on a bp1 • You evaluated an expression – Set another bp – Stopped on it – Computed something – Resulmed – => you are not on bp1 any more
  17. 17. Workaround • Start a special thread while (true) { nop(); } • To stop: boolean done = false; while (!done) { nop(); }
  18. 18. Summary • Prerequisites – Extract method – Ability to compile with some tweaks • Results – Any expressions/statements – Lambdas/local classes • Use it for your language! – https://github.com/abreslav/eval4j

×