跳至主要内容

Java Remote Debug

When we finish the development and test in local environment, we will deploy our applications in remote server. We hope and believe it will work but we are always too optimistic. The application met different kinds of problems.
In order to fix it, we sometimes need to access code directly in remote server container. Today, we will see how to do debug remotely.

Remote Debug

When we develop our application in local, we always using IDE to do it. We code and run and debug in IDE which provides much convenience. Even though we can debug application with jdb – a command line tools bundled into JDK, we may prefer to use IDE.
So, we only cover the content of using IDE to debug here. If you are interested in using jdb, you may like this jdb tutorial.

Runnable Jar

In order to debug a remote JVM, we have to add some parameters to let JVM opening the debug port so that we can attach to it. The following is the command to start a runnable Jar :
java "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" -jar app.jar
As far as I have tested, the order of command line parameter is very important, put -jar before the debug parameter fails the command.
Now, we can create a new run configuration in IDE (in our case, it is idea):
IntelliJ IDEA Remote Application Configuration

Input the host and port (remember to match our early configured port) and we are done. Now we can set a breakpoints and start debug session. Server application will suspends when it runs to our breakpoints. And because the help of IDE, which will search the source against current project, we can use source to debug like we are running it in IDE.

Bootstrap Debug

This is very useful techniques when we find our application can run in IDE, but can’t start up when it is packaged into a Jar file (which is often the problem of How to refer file in Jar). We cannot attach to a remote application using default configuration (attach debugger mode) if this application fails to bootstrap, because when we right click the debug button, the application may already down. And in this way, we can’t see the console output, which is also inconvenient to see stack trace.
In this case, we can change the debug mode from attach to listen:
-agentlib:jdwp=transport=dt_socket,server=n,address=slave01:5005,suspend=y,onthrow=<FQ exception class name>,onuncaught=<y/n>
We can refer to this oracle help site about how to set last parameter.
An example of config to catch remote application at startup can be:
-agentlib:jdwp=transport=dt_socket,server=n,address=slave01:5005,suspend=y,onuncaught=n
This way works, but a little bit tedious, we can use IDEA’s another run configuration if we can run this Jar locally, which make life easier:
IntelliJ IDEA Jar Application Configuration

Tomcat

If our application is not a runnable Jar but run in application server like Tomcat. We can also do remote debug. First, we also need to add some environment parameter in tomcat/bin/setenv.sh (or setenv.bat for windows)
JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"
Then, we need to run tomcat in command line like following:
catalina.sh jpda start
Finally, we create a remote tomcat startup configuration in idea, adding host and port we have specified and we are done:
IntelliJ IDEA Tomcat Remote Configuration

Ref

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Implement isdigit

It is seems very easy to implement c library function isdigit , but for a library code, performance is very important. So we will try to implement it and make it faster. Function So, first we make it right. int isdigit ( char c) { return c >= '0' && c <= '9' ; } Improvements One – Macro When it comes to performance for c code, macro can always be tried. #define isdigit (c) c >= '0' && c <= '9' Two – Table Upper version use two comparison and one logical operation, but we can do better with more space: # define isdigit(c) table[c] This works and faster, but somewhat wasteful. We need only one bit to represent true or false, but we use a int. So what to do? There are many similar functions like isalpha(), isupper ... in c header file, so we can combine them into one int and get result by table[c]&SOME_BIT , which is what source do. Source code of ctype.h : # define _ISbit(bit) (1 << (