Skip to content

Instantly share code, notes, and snippets.

@lrytz
Last active October 15, 2020 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrytz/a6a139491e3dad997c2d78d99b5bf504 to your computer and use it in GitHub Desktop.
Save lrytz/a6a139491e3dad997c2d78d99b5bf504 to your computer and use it in GitHub Desktop.
jsr45 scala
object A {
def main(args: Array[String]): Unit = {
val testVar = "hai"
val res = B.foo(testVar)
println(res)
}
}
object B {
@inline def foo(x: String) = {
val fooVar = "mix"
C.bar(x + fooVar)
}
}
object C {
@inline def bar(x: String) = {
val barVar = "mux"
x + barVar
}
}
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