HOW TO REMOTELY DEBUG APPLICATION RUNNING ON TOMCAT FROM WITHIN INTELLIJ IDEA
This post would look into how to tackle and debug issues in scenarios where they only occur in production (or other remote environment) but not in development environment. As anybody who has been in this kind of situation would acknowledge, trying to pinpoint the cause of these kind of “issues” might quickly end up being a practice at taking shots in the dark: a very time-consuming and inefficient process.
It was this kind of situation I recently found myself, where, I had to rectify certain issues that were occurring in the production environment but could not be reproduced on the development machine.
Fortunately enough, the said issues could be reproduced in the testing environments (which is as close to the production environment as possible). But having the issues reproducible in the test environment was good In that it confirms the issues needed to be fixed, but it was of little help in actually tracking the issues down, finding the cause and fixing it. Relying just on log outputs was not enough…What if I could debug the test environment from my machine?
It was at this stage that I thought about remote debugging and if there exist the possibility of having an application run in a remote JVM and still be able to intercept its execution from the copy of the source code running on a local IDE: sounds just like what would get the job done.
And sure this is very possible. I looked into what is needed; the set up and all. It did not sound complicated. So, together with the help of a colleague, Thijs Schnitger, was able to get it up and running without much hassles.
This post thus describes the procedure of setting up the ability to remotely debug a JVM application from within an IDE. The post outlines the procedure using IntelliJ IDEA as the IDE, and the remote application to be debugged would be web application running on Tomcat. The steps outlined below should apply with any remote JVM application and any IDE, although the exact steps may differ, the general idea would remain same. The post also gives a brief overview of the technologies that makes remote debugging possible.
The Configuration Instructions
The process of getting remote debugging working involves two steps.
- Starting Tomcat with remote debugging enabled and
- having your IDE, in our case IntelliJ IDEA, to be able to debug the remote tomcat application.
There are couple of ways to get the first part done and it slightly differs depending on which OS environment your Tomcat instance is running on. But, regardless of the method used, the main idea behind the configuration remains the same; which is: pass specific start up options to the JVM that would enable remote debugging.
The JVM start up arguments needed to have remote debugging activated can be set via JPDA_OPTS,CATALINA_OPTS and JAVA_OPTS although using JAVA_OPTS is not usually advised, reason being that the setting specified via JAVA_OPTS, is exposed to all JVM applications, but withCATALINA_OPTS the settings defined is limited only to Tomcat.
USING JPDA_OPTS
Using the JPDA_OPTS, option you would have the needed start-up argument set in a file named setenv.sh (or setenv.bat if on windows). Create the file if it does not exist already. Have it in theCATALINA_HOME/bin directory. And add this to the content:
1 |
export JPDA_OPTS= "-agentlib:jdwp=transport=dt_socket, address=1043, server=y, suspend=n" |
and if on Windows:
1 |
set JPDA_OPTS= "-agentlib:jdwp=transport=dt_socket, address=1043, server=y, suspend=n" |
What these settings basically do is to enable remote debugging and configure available options: specifying the communication protocol between the running application and the debugger, (ietransport=dt_socket) the port at which the remote application should debugged (ie address=1403). The server=y setting indicates that this JVM would be the one to be debugged while suspend=n is used to tell the JVM to execute right away and not wait for an attached debugger. If set to “y” then the application would be suspended and not run until a debugger is attached.
A good situation where you would want to have suspend=y is when debugging an issue that prevents an application from starting successfully, having suspend=y would make sure that the JVM waits for the remote debugger to connect before attempting to start and run the application.
Although the settings can be put directly inside catalina.sh (or catalina.bat) it is always preferable to have extra configurations in the setenv.* file. It would be automatically be picked up by Tomcat.
Note that another options you may encounter that may be used to enable remote debugging is:
1 |
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=1043, suspend=n |
The difference between this and the recommended setting is that the -Xdebug and -Xrunjdwp option is the old way of enabling remote debugging; it applies to JVM prior to JAVA 5.0 (JAVA 1.5.0) while -agentlib:jdwp option applies to JAVA 5.0 and beyond…And with this configuration in place, you can then start Tomcat via the command line arguments:
1 |
$CATALINA_HOME/bin/catalina.sh jpda start |
USING JAVA_OPTS OR CATALINA_OPTS
If you have Tomcat running as a windows service, then configuring Tomcat to start up with ability to be debugged remotely is done by simply specifying the start up arguments in the run properties.
Open up the Apache Tomcat properties dialog box, and under the Java tab add the required start up option:
1 |
-agentlib:jdwp=transport=dt_socket,address=1043,server=y,suspend=n |
Make sure that each entry is on a new line and there are no spaces between the options
With this added to the options, starting the Tomcat service would have remote debugging enabled.
If per chance you are not running Tomcat on Windows as a service, to enable remote debugging, you need to specify the required options in the setenv.bat file:
1 |
set CATALINA_OPTS= "-agentlib:jdwp=transport=dt_socket,address=1043,server=y,suspend=n" |
If you are running on linux you have:
1 |
export CATALINA_OPTS= "-agentlib:jdwp=transport=dt_socket,address=1043,server=y,suspend=n" |
Start Tomcat like you would normally then do by running the catalina.bat
or catalina.sh
script.
STARTING TOMCAT WITH JPDA
Another way of running Tomcat with remote debugging is to use the JPDA switch, to start Tomcat; this would automactically have remote debugging enabled with its options set to default values.
For example, this:
1 |
catalina jpda start |
would start Tomcat with the following settings:
1 |
-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n |
But what if you want to change any of these default settings and still use the JPDA start switch? This can be done by setting the appropiate environment variable for the options as required by Tomcat. The environment variables are:
JPDA_TRANSPORT: to specify JPDA transport used
JPDA_ADDRESS: to specify port for remote debugging
JPDA_SUSPEND: to specify if to suspend JVM after startup
so running:
1 |
export JPDA_ADDRESS= "8080" |
and then
1 |
catalina jpda start |
would have remote debugging possible on port 8080.
Configuring IntelliJ IDEA
With the remote JVM running the Tomcat started with the required start up arguments, the next thing to do is to configure the debugger in IntelliJ IDEA.
There are two ways to this configuration: The Remote Tomcat settings options or Remote settingsoption.
Using The Remote Tomcat Settings Option.
Click on Run ➝ Edit Configuration:
The Edit Configuration… settings dialog box, pops up.
Click on the + button on the top left ➝ Tomcat Server ➝ Remote. As shown below:
Next is to fill in the required settings.
On the Server tab, specify the host details and the port the remote Tomcat is running on:
Then switch to the Startup/Connection tab, where you would specify the details of the remote JVM needed to be debugged from intellij IDEA.
Click on Debug and specify the port you specified while configuring the Tomcat; 1043 in our case.
And that is it. Click on Ok to save your changes, then start the debugging session by pressing the debug icon or by using the keyboard shortcut (shift + f9)
If all the configuration is set up correctly, you should see a notice that the IDE has successfully connected to the target VM in the Debug tab in the bottom console:
Using the Remote settings option
Open the Edit Configuration settings as done in the previous option but instead of selecting the Remote Tomcat, select the Remote option:
The Remote settings dialog box appears where you can specify the required configuration; remote host, port, project etc:
As you can see from above, the interesting thing about the Remote option is that, apart from allowing you to configure intelliJ IDEA, it also list the various configurations required to start up the remote JVM in other for remote debugging to work. Might come in handy.
Specify the required settings, click Ok to save changes, and start the debugging session. You should also see the notice that IntelliJ has successfully connected to the remote VM.
Once this is done, you should then open the source code of the application you have running on the remote Tomcat, put a breakpoint where required and you can go ahead and start debugging as if the application is running on your local machine.
So that is it with the configuration, the next question is, what makes all these fit and work together?
Let us take a quick work through of the technology at play that makes remote debugging JVM possible.
How Remote JVM Debugging Works
It all starts with what is referred to as Agents.
The JVM, which runs the complied .class
sources has a feature that allows externally libraries (written in either Java or C++) to be injected into the JVM, just about runtime. These external libraries are referred to as Agents and they have the ability to modify the content of the .class files been run. These Agents have access to functionality of the JVM that is not accessible from within a regular Java code running inside the JVM and they can be used to do interesting stuff like injecting and modify the running source code, profiling etc. Tools like JRebel makes use of this piece of functionality to achieve their magic.
And to pass an Agent Lib to a JVM, you do so via start up arguments, using the -agentlib:libname[=options] format.
So in the configuration we had above:
1 |
-agentlib:jdwp=transport=dt_socket,address=1043,server=y,suspend=n |
We were actually passing an Agent Lib named jdwp to the JVM running Tomcat. The jdwp is a JVM specific, optional implementation of the JDWP (Java Debug Wire Protocol) that is used for defining communication between a debugger and a running JVM. It’s implementation, if present is supplied as a native library of the JVM as either jdwp.so or jdwp.dll
So what does it do?
In simple terms, the jdwp agent we pass is basically serving the function of being a link between the JVM instance running an application and a Debugger (which can be located either remote or local). Since it is an Agent Library, It does have the ability to intercept the running code, create a bridge between the JVM and a debugger, and have the functionality of a debugger applied on the JVM.
Since in the JVM architecture, the debugging functionality is not found within the JVM itself but is abstracted away into external tools (that are aptly referred to as debuggers), these tools can either reside on the local machine running the JVM being debugged or be run from am external machine. It is this de-coupled, modular architecture that allows us to have a JVM running on a remote machine and using the JDWP, have a remote debugger be able to communicate with it.
When IntelliJ IDEA was configured above, what was actually been done is to specify to the debugger tool within IntelliJ IDEA the host where the running JVM it needs to debug resides and the port through which it should connect with.
All the specification that outlines this modular architecture is contained in what is referred to as the Java Platform, Debugger Architecture, JPDA (this explains the JPDA in the JPDA_OPTS method used above) and you can read a much detailed overview of it here: Java Platform Debugger Architecture Overview.
HOW TO REMOTELY DEBUG APPLICATION RUNNING ON TOMCAT FROM WITHIN INTELLIJ IDEA的更多相关文章
- Mac下Tomcat安装与Intellij IDEA配置Tomcat
Mac下Tomcat安装与Intellij IDEA配置Tomcat 一 安装 1 下载地址:https://tomcat.apache.org/download-90.cgi 2 将压缩包解压后移至 ...
- Tasker to detect application running in background
We used to be told that tasker is only capable of detecting foreground application, if the app gets ...
- Why is an 'Any CPU' application running as x86 on a x64 machine?
It's likely that you linked some assemblies that are not Any CPU, but include native code (or are ...
- 运行 Tomcat, 在 Intellij IDEA 控制台输出中文乱码的解决方法
打开 Run/Debug Configurations → Tomcat Server → 要运行的 Tomcat → Server 页签,在 VM options 中输入: -Dfile.encod ...
- Debug with Eclipse
In this post we are going to see how to develop applications using Eclipse and Portofino 4. The trad ...
- Tomcat翻译--Tomcat Web Application Deployment(Tomcat中部署web应用)
原文:http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html Introduction(介绍) Deployment is the te ...
- How to: Debug X++ Code Running in .NET Business Connector [AX 2012]
This topic has not yet been rated - Rate this topic http://msdn.microsoft.com/EN-US/library/bb19006 ...
- 调用shutdown.sh后出现could not contact localhost8005 tomcat may not be running报错问题
之前调用tomcat的shutdown.sh无法关闭tomcat,一直报could not contact localhost8005 tomcat may not be running错. 在网上找 ...
- Tomcat使用IDEA远程Debug调试
Tomcat运行环境:CentOS6.5.Tomcat7.0.IDEA 远程Tomcat设置 1.在tomcat/bin下的catalina.sh上边添加下边的一段设置 CATALINA_OPTS=& ...
随机推荐
- 如何在WIN7下进行LINUX虚拟机搭建
Linux是一套免费使用和自由传播的类Unix操作系统,非常适用于搭建网络服务器等,我本人日常工作时,是使用的LINUX和WIN7双操作系统,但每次更换系统总要关机重启很不方便,所以也在WIN7下搭建 ...
- 聚合数据全国天气预报api接口
查询天气预报在APP中常用的一个常用功能,聚合数据全国天气预报api接口可以根据根据城市名/id查询天气.根据IP查询天气.据GPS坐标查询天气.查询城市天气三小时预报,并且支持全国不同城市天气预报查 ...
- zookeeper适用场景:zookeeper解决了哪些问题
问题导读:1.master挂机,传统做法备份必然是以前数据,该如何保证挂机数据与备份数据一致?2.分布式系统如何实现对同一资源的访问,保证数据的强一致性?3.集群中的worker挂了,传统做法是什么? ...
- NP完全问题 NP-Completeness
原创翻译加学习笔记,方便国人学习算法知识! 原文链接http://www.geeksforgeeks.org/np-completeness-set-1/ 我们已经找到很多很高效的算法来解决很难得问题 ...
- 边工作边刷题:70天一遍leetcode: day 81
Encode and Decode Strings 要点:题的特点:不是压缩,而是encode为字节流.所以需要找delimiter来分割每个word,但是delimiter可能是字符本身,所以可以用 ...
- PNG文件
png格式主要由六大块组成:文件头.IHDR块.PLTE块.tRNS块.IDAT块.文件尾文件头一般是 8950 4E47 0D0A 1A0A而本题提示中的IHDR块是png中用来描述图片的基本信息, ...
- C#综合揭秘——细说多线程(下)
引言 本文主要从线程的基础用法,CLR线程池当中工作者线程与I/O线程的开发,并行操作PLINQ等多个方面介绍多线程的开发.其中委托的BeginInvoke方法以及回调函数最为常用.而 I/O线程可能 ...
- Iron man
儿子的手办在近期又新增一套钢铁侠,来自于淘宝的玩具推荐,这个推荐也得益于小美和他平日在淘宝商城里的各种玩具浏览,充分体现了现阶段对复仇者联盟成员的喜爱. 一套共六个,有着不同的颜色,但造型基本一致带L ...
- 21Spring_JdbcTemplatem模板工具类的使用——配置文件(连接三种数据库连接池)
上一篇文章提到过DriverManagerDataSource只是Spring内置的数据库连接池,我们可选的方案还有c3p0数据库连接池以及DBCP数据库连接池. 所以这篇文章讲一下上面三种数据库连接 ...
- with(nolock)的用法
with(nolock)的介绍 大家在写查询时,为了性能,往往会在表后面加一个nolock,或者是with(nolock),其目的就是查询是不锁定表,从而达到提高查询速度的目的. 当同一时间有多个用户 ...