Last active
October 15, 2020 08:01
-
-
Save lrytz/a6a139491e3dad997c2d78d99b5bf504 to your computer and use it in GitHub Desktop.
jsr45 scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object A { | |
def main(args: Array[String]): Unit = { | |
val testVar = "hai" | |
val res = B.foo(testVar) | |
println(res) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object B { | |
@inline def foo(x: String) = { | |
val fooVar = "mix" | |
C.bar(x + fooVar) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object C { | |
@inline def bar(x: String) = { | |
val barVar = "mux" | |
x + barVar | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.{InputStreamReader, OutputStreamWriter} | |
import com.sun.jdi.{Bootstrap, ClassType, VMDisconnectedException} | |
import com.sun.jdi.event.{ClassPrepareEvent, EventSet} | |
import scala.jdk.CollectionConverters._ | |
/* | |
Copied and adapted from https://itsallbinary.com/java-debug-interface-api-jdi-hello-world-example-programmatic-debugging-for-beginners/ | |
qsc -cp $JAVA_HOME/lib/tools.jar JSR45.scala | |
qsc A.scala B.scala C.scala -opt:l:inline '-opt-inline-from:**' | |
qs -cp .:$JAVA_HOME/lib/tools.jar JSR45 . A 'A$' | |
*/ | |
object JSR45 { | |
def main(args: Array[String]) = { | |
val cp = args(0) | |
val main = args(1) | |
val debug = args(2) | |
val launchingConnector = Bootstrap.virtualMachineManager.defaultConnector | |
val env = launchingConnector.defaultArguments | |
env.get("main").setValue(main) | |
val options = env.get("options") | |
options.setValue(s"-cp $cp") | |
val vm = launchingConnector.launch(env) | |
val classPrepareRequest = vm.eventRequestManager.createClassPrepareRequest | |
classPrepareRequest.addClassFilter(debug) | |
classPrepareRequest.enable() | |
var eventSet: EventSet = null | |
try { | |
while ({ eventSet = vm.eventQueue.remove(100); eventSet != null }) { | |
println(eventSet.asScala.toList) | |
for (event <- eventSet.asScala) { | |
event match { | |
case evt: ClassPrepareEvent => | |
val classType = evt.referenceType.asInstanceOf[ClassType] | |
println(classType) | |
println(classType.defaultStratum) | |
println(classType.allLineLocations) | |
println(classType.allLineLocations("ScalaDebug", null)) | |
case _ => | |
} | |
vm.resume() | |
} | |
} | |
} catch { | |
case _: VMDisconnectedException => | |
System.out.println("VM is now disconnected.") | |
case e: Exception => | |
e.printStackTrace() | |
} finally { | |
val reader = new InputStreamReader(vm.process.getInputStream) | |
val writer = new OutputStreamWriter(System.out) | |
val buf = new Array[Char](1024) | |
reader.read(buf) | |
writer.write(buf) | |
writer.flush() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment