I, too, want to debug into Gradle code to better understand test report generation. I was having problems getting GRADLE_OPTS to propagate to spawned JVMs (I must have been doing something wrong). By that I mean that the JVM that handles the command line invocation of Gradle is not the same one that handles running your tests, and I could not get my debugger client (IntelliJ) to stop at a breakpoint in Gradle code in the test-running VM.
Here is how I solved it. Add the JVM debugging config to $HOME/.gradle/gradle.properties like this:
$ cat ~/.gradle/gradle.properties
org.gradle.daemon=true
org.gradle.jvmargs=-XX:MaxPermSize=4g -XX:+HeapDumpOnOutOfMemoryError -Xmx4g -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5006
Do something that starts the daemon:
$ gradle --daemon
Then attach your debugger client to port 5006, set your breakpoint, then run your test.
Works for me.