Groovy Introspective is a developer tool that attaches to an existing Java process and opens an interactive Groovy environment inside its runtime, allowing developers to inspect, debug, and interact with a live JVM.
Knowing what's going on inside a large deployed system as you're developing it can be challenging. Before I created this tool, I always wished I could just know certain values without having to restart my service or look through a giant log file.
With this tool, I've been able to quickly test code paths, grab random JVM values, and inspect any program's state much more clearly. Every time I exit this tool, I know more than I did before. It has directly solved a large pain-point for me, so I decided to open-source it for anyone else to use.
- Attaches to running JVM processes
- Interactive Groovy execution
- JVM process discovery
- External agent attachment
- Runtime classpath discovery
- Code completion
- SSH-based interactive sessions
- Live runtime inspection/debugging
When the core JAR is invoked from a terminal and a target process is selected, the following process occurs:
- The JAR extracts its bundled agent JAR to a deterministic cache location, based on its SHA-256 message digest
- The JVM invoked from the terminal performs an external-attach onto the target JVM, supplying it the agent JAR and a port argument.
- The agent constructs its central classloader, and starts the SSHD session on the designated port.
- The terminal JVM connects to and controls this SSHD session
- The Groovy REPL and dashboard processes begin
- Download the
core-1.0.0.jarartifact from releases - Put it inside your user home directory, in
.grin/bin/ - Take one of the startup scripts inside the
scriptsfolder on this repo, and put it there too - Put that
.grin/bin/directory on your PATH - Try running the
grincommand
Alternatively, you can clone this repository and run the installGrin
Gradle task, which will build everything and put core-1.0.0.jar in the
right directory for you. All that is required afterward is the script and PATH setup.
Since there's a lot going on, there are a few dependencies. The REPL itself is built on top of groovysh internals, with multiple differing design choices at play. Outside of this, grin relocates a lot of external packages, but the important ones are namely jline, SSHD, and most of Groovy 5.
The reason that it relocates any dependencies at all is for compatibility.
If some running JVM already has org.jline, it may have a very different
version. Therefore, I found the best option to be relocating grin's
dependencies explicitly to me.redot.grin.shaded
which nobody will ever use (probably).
I chose Groovy for the REPL for a few reasons:
- Its compiler's availability and compatibility. The core JAR for this tool sits at around 17MB. It contains all the Groovy dependencies it needs to work its magic.
- It's fun and easy. Nearly anything that works in Java will work in Groovy, and Groovy has a lot of healthy abstractions on top of standard Java syntax. The learning curve is flat.
- Speed. Due to Groovy's syntax and metaprogramming support, writing scripts on top of a live JVM is smooth and quick.
- Support for multiple SSHD sessions is non-existent; new SSHD sessions will close the previous one
- Every script is stored in memory for the entire runtime; there is no supported way to purge them
