Chan Chen Coding...

          Java Web Application 遠程調試

          最近在開發java web application的時候,因為很多原因,無法對自己開發的項目在本地進行調試,常常需要進行遠程調試,之前一直通過打logger的方式進行,每次都要重新部署,相當的痛苦,今天下午研究了以下,如果進行遠程調試。

          開發的web application是部署在tomcat上面的,那么問題就轉化為如何調試tomcat。其實調試tomcat,本質上就是調試JVM。JVM的強大,從J2SE1.4.2開始,就實現了JPDA (Java Platform Debug Architecture)。

          tomcat默認情況下,是沒有啟用jpda的,如果要啟用,需要傳入參數
          -Xdebug -Xrunjdwp:transport=dt_socket, address=8000,server=y,suspend=y

          那么問題是這些參數如何在tomcat啟動的使用傳入呢?這是時候,需要了解tomcat的啟動腳本,在TOMCAT_HOME/bin

          目錄下,有三個腳本catalina.sh, startup.sh, 和 shutdown.sh。如果查看startup.sh和shutdown.sh,都是通過catalina.sh來啟動的。腳本如下:

          EXECUTABLE=catalina.sh

          exec "$PRGDIR"/"$EXECUTABLE" start "$@"
          于是,我們可以查看下catalina.sh的腳本是如何實現的。

          #   JPDA_TRANSPORT  (Optional) JPDA transport used when the "jpda start"
          #                   command is executed. The default is "dt_socket".
          #
          #   JPDA_ADDRESS    (Optional) Java runtime options used when the "jpda start"
          #                   command is executed. The default is 8000.
          #
          #   JPDA_SUSPEND    (Optional) Java runtime options used when the "jpda start"
          #                   command is executed. Specifies whether JVM should suspend
          #                   execution immediately after startup. Default is "n".
          #
          #   JPDA_OPTS       (Optional) Java runtime options used when the "jpda start"
          #                   command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,
          #                   and JPDA_SUSPEND are ignored. Thus, all required jpda
          #                   options MUST be specified. The default is:
          #
          #                   -agentlib:jdwp=transport=$JPDA_TRANSPORT,
          #                       address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND

          if [ "$1" = "jpda" ] ; then
            if [ -z "$JPDA_TRANSPORT" ]; then
              JPDA_TRANSPORT="dt_socket"
            fi  
            if [ -z "$JPDA_ADDRESS" ]; then
              JPDA_ADDRESS="8000"
            fi  
            if [ -z "$JPDA_SUSPEND" ]; then
              JPDA_SUSPEND="n"
            fi  
            if [ -z "$JPDA_OPTS" ]; then
              JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND"
            fi  
            CATALINA_OPTS="$CATALINA_OPTS $JPDA_OPTS"
            shift
          fi

          通過這個代碼,我們可以看出,其實要啟動jpda, 最主要的是要對JPDA_SUSPEND的值進行設置,由N改為Y。

          借鑒start.sh的啟動,在linux下,我們可以自己創建一個jpda.sh的腳本,用來啟動開啟debug模式的tomcat,具體腳本如下,黑體為修改部分。

          os400=false
          darwin=false
          case "`uname`" in
          CYGWIN*) cygwin=true;;
          OS400*) os400=true;;
          Darwin*) darwin=true;;
          esac

          # resolve links - $0 may be a softlink
          PRG="$0"

          while [ -h "$PRG" ] ; do
            ls=`ls -ld "$PRG"`
            link=`expr "$ls" : '.*-> \(.*\)$'`
            if expr "$link" : '/.*' > /dev/null; then
              PRG="$link"
            else
              PRG=`dirname "$PRG"`/"$link"
            fi
          done
           
          PRGDIR=`dirname "$PRG"`
          EXECUTABLE=catalina.sh

          # Check that target executable exists
          if $os400; then
            # -x will Only work on the os400 if the files are: 
            # 1. owned by the user
            # 2. owned by the PRIMARY group of the user
            # this will not work if the user belongs in secondary groups
            eval
          else
            if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
              echo "Cannot find $PRGDIR/$EXECUTABLE"
              echo "The file is absent or does not have execute permission"
              echo "This file is needed to run this program"
              exit 1
            fi
          fi 

          export JPDA_SUSPEND=y

          exec "$PRGDIR"/"$EXECUTABLE" jpda start "$@"

           

          在Eclipse中遠程調試Tomcat

          首先將Tomcat 5.5.26的源代碼分為container connectors jasper servletapi build五個項目,導入到Eclipse中。啟動相關的代碼主要在container中,就以它為當前項目,打開”Debug Configurations“對話框。

          然后創建一個”Remote Java Application“,Connection Type選擇”Standard (Socket Attach)“,Host填寫localhost(Tomcat所在的主機地址),Port填寫8000。最后點擊”Apply“保存。

          Eclipse的Debug Configurations對話框中配置遠程調試

           

          首先確保已經執行了jpda.bat,Tomcat正在等待調試器連接;然后執行上述的Debug Configuration,Eclipse就可以連上Tomcat。

           

          Tomcat的啟動是從Bootstrap的main方法開始,我在第一行代碼處設置了斷點,Tomcat的啟動就停在了這一行:

           

          斷點調試Tomcat的啟動過程

           

          接著,讓Tomcat繼續執行,我們可以看到,控制臺輸出了啟動信息。

          Tomcat在JPDA模式下繼續啟動



          -----------------------------------------------------
          Silence, the way to avoid many problems;
          Smile, the way to solve many problems;

          posted on 2013-05-05 16:49 Chan Chen 閱讀(1630) 評論(0)  編輯  收藏 所屬分類: Linux

          主站蜘蛛池模板: 丁青县| 常熟市| 闵行区| 吉木萨尔县| 昌宁县| 长宁区| 抚州市| 滨州市| 龙南县| 慈利县| 淮安市| 庄河市| 陇西县| 铜梁县| 涿鹿县| 大方县| 新沂市| 仪陇县| 余姚市| 湘潭县| 海淀区| 扬州市| 忻城县| 武宣县| 邯郸县| 调兵山市| 中牟县| 新野县| 盐源县| 万山特区| 敦煌市| 乐至县| 丰台区| 荔波县| 横山县| 洛阳市| 景东| 赤水市| 竹溪县| 深泽县| 临沭县|