Home > grails, groovy, programming > Adventures in Grails – Debugging

Adventures in Grails – Debugging

Update :

It was pointed out to me on the Grails user list that thegrails-debugcommand can be used to do what I posted below. However, the code below is slightly more flexible in that you can control the port, transport and suspend flag with environmental variables.

Working with Grails so far has been great. However, there is no easy way to (that I could find) to fire up Grails and attach a debugger. So I made a few changes to the startup script $GRAILS_HOME/bin/startGrails to handle a debug argument. This is *NIX systems only (including OS X), I haven’t worked with batch files in so long (and have no need for it) that I did not make the changes to $GRAILS_HOME/bin/startGrails.bat. Perhaps someone that uses that other OS to develop on could port the changes.

The script uses the environmental variables:

  • JPDA_ADDRESS #the port to listen on, default 8000
  • JPDA_TRANSPORT #transport to use, default ‘dt_socket’
  • JPDA_WAIT #if ‘y’ then wait for the debugger to attach, default ‘n’

If anyone of these is not set it’s default value is used.

To start the VM for debugging add ‘debug’ as the first arg to the grails command.

grails debug run-app

The changes are in two places. First:


if [ "$1" = "-cp" ] || [ "$1" = "-classpath" ]; then
  CP=$2
  shift 2
fi

ARGUMENTS=$@

becomes


#Test for and setup debug option
if [ ! $JPDA_ADDRESS ]
then
   JPDA_ADDRESS=8000
fi

if [ ! $JPDA_TRANSPORT ]
then
   JPDA_TRANSPORT="dt_socket"
fi

if [ ! $JPDA_WAIT ]
then
   JPDA_WAIT="n"
fi

if [ $# -ne 0 ]
then
   if [ $1 == "debug" ]
   then
      shift
      GRAILS_DEBUG_OPTS="-Xdebug -Xrunjdwp:transport=${JPDA_TRANSPORT},address=${JPDA_ADDRESS},server=y,suspend=${JPDA_WAIT}"
   fi
fi

if [ "$1" = "-cp" ] || [ "$1" = "-classpath" ]; then
  CP=$2
  shift 2
fi

ARGUMENTS=$@

and in the startGrails function:

startGrails() {
  CLASS=$1
  shift
  JAVA_OPTS="-server -Xmx512M $JAVA_OPTS"
  # Start the Profiler or the JVM
  if $useprofiler; then
      runProfiler
  else
  	if [ $# -eq 0 ] ; then         # no argument given
         exec "$JAVACMD" $JAVA_OPTS \
          -classpath "$STARTER_CLASSPATH" \
          -Dprogram.name="$PROGNAME" \
          -Dgroovy.starter.conf="$GROOVY_CONF" \
          -Dgrails.home="$GRAILS_HOME" \
          -Dbase.dir="." \
          -Dtools.jar="$TOOLS_JAR" \
          $STARTER_MAIN_CLASS \
          --main $CLASS \
          --conf "$GROOVY_CONF" \
          --classpath "$CP"
  	else
         exec "$JAVACMD" $JAVA_OPTS \
          -classpath "$STARTER_CLASSPATH" \
          -Dprogram.name="$PROGNAME" \
          -Dgroovy.starter.conf="$GROOVY_CONF" \
          -Dgrails.home="$GRAILS_HOME" \
          -Dbase.dir="." \
          -Dtools.jar="$TOOLS_JAR" \
          $STARTER_MAIN_CLASS \
          --main $CLASS \
          --conf "$GROOVY_CONF" \
          --classpath "$CP" \
          "${ARGUMENTS}"
  	fi
  fi
}

becomes

startGrails() {
  CLASS=$1
  shift
  JAVA_OPTS="-server -Xmx512M $JAVA_OPTS"
  # Start the Profiler or the JVM
  if $useprofiler; then
      runProfiler
  else
  	if [ $# -eq 0 ] ; then         # no argument given
         exec "$JAVACMD" $JAVA_OPTS \
          -classpath "$STARTER_CLASSPATH" \
          "$GRAILS_DEBUG_OPTS" \
          -Dprogram.name="$PROGNAME" \
          -Dgroovy.starter.conf="$GROOVY_CONF" \
          -Dgrails.home="$GRAILS_HOME" \
          -Dbase.dir="." \
          -Dtools.jar="$TOOLS_JAR" \
          $STARTER_MAIN_CLASS \
          --main $CLASS \
          --conf "$GROOVY_CONF" \
          --classpath "$CP"
  	else
         exec "$JAVACMD" $JAVA_OPTS \
          -classpath "$STARTER_CLASSPATH" \
          "$GRAILS_DEBUG_OPTS" \
          -Dprogram.name="$PROGNAME" \
          -Dgroovy.starter.conf="$GROOVY_CONF" \
          -Dgrails.home="$GRAILS_HOME" \
          -Dbase.dir="." \
          -Dtools.jar="$TOOLS_JAR" \
          $STARTER_MAIN_CLASS \
          --main $CLASS \
          --conf "$GROOVY_CONF" \
          --classpath "$CP" \
          "${ARGUMENTS}"
  	fi
  fi
}
Categories: grails, groovy, programming
  1. March 25, 2008 at 7:00 pm | #1

    thanks much, man

  1. No trackbacks yet.