Eval4j @ JVMLS 2014
-
Upload
andrey-breslav -
Category
Technology
-
view
222 -
download
0
description
Transcript of Eval4j @ JVMLS 2014
![Page 1: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/1.jpg)
eval4j
compiling expressions to be evaluated by the debugger
andrey.breslav@ .com
![Page 2: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/2.jpg)
![Page 3: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/3.jpg)
Stop at a breakpoint
Inspect memoryInspect call stacks
![Page 4: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/4.jpg)
Evaluate given expression
Evaluate given expression
![Page 5: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/5.jpg)
![Page 6: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/6.jpg)
Working Horse: Bare JDI
• Access Variables• Access Fields• Call Methods
• No Evaluate Expression out-of-the-box JDI
![Page 7: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/7.jpg)
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
![Page 8: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/8.jpg)
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…
![Page 9: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/9.jpg)
Reuse the Compiler (Take 1)
a + b.class
fileDebugeeprocess
compile
loadrun
IllegalAccessError
![Page 10: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/10.jpg)
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
JDI
![Page 11: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/11.jpg)
Compiling an expression
free variables
Extract Method Refactoring
![Page 12: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/12.jpg)
Summary so far
Extract Method +
Compiler +
eval4j =
Evaluate Expression
![Page 13: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/13.jpg)
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
![Page 14: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/14.jpg)
Challenge: New Classes
private?
• Also: How do you load a class into the debugee?
?
![Page 15: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/15.jpg)
Loading a class…
• protected Class<?> defineClass(byte[], …)
![Page 16: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/16.jpg)
How Privates Work Normally
• Special synthetic accessor methods– INVOKESTATIC Foo.access$001()– can’t insert a new method into the outer class
![Page 17: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/17.jpg)
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;
}
![Page 18: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/18.jpg)
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
![Page 19: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/19.jpg)
Workaround
• Start a special thread
while (true) { nop();}• To stop:
boolean done = false;while (!done) {
nop();}
![Page 20: Eval4j @ JVMLS 2014](https://reader035.fdocuments.net/reader035/viewer/2022062613/540d73118d7f728d7e8b4947/html5/thumbnails/20.jpg)
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