5

I've started to implement a simple Java agent that does some instrumentation before a class is loaded by JVM. The thing is I need to do some debugging on this agent, but what I tried so far had failed( I tried to remote debug the agent but it's not working)

I use IntelliJ and Maven( to generate the .jar agent).

So my question:Is there any trick to remote debug a simple java agent?

noahlz
  • 10,202
  • 7
  • 56
  • 75
Faur Ioan-Aurel
  • 791
  • 1
  • 7
  • 18
  • You can't just set a break point in your agent source code? – noahlz Apr 19 '13 at 16:22
  • I can set a break point but the execution doesn't stop when the break point is reached. – Faur Ioan-Aurel Apr 19 '13 at 17:03
  • did you add logging statements to verify that code is executing? – noahlz Apr 19 '13 at 17:07
  • Yes I did! The code is executing but it doesn't break! Because I use mvn to package the agent I also made sure maven will compile with debug option set true. Then I got back into IntelliJ and set-up a remote debug for the agent! After that I started the remote debug. Last I executed the application which is instrumented by the agent. – Faur Ioan-Aurel Apr 19 '13 at 17:48
  • 3
    Faced same problem. I was unable to stop at breakpoints, but at the same time I got messages saying I connected to the target VM. Spent quite a time, but solved the problem by putting -agentlib:jdwp option before -javaagent: ... So it appears, order matters :) – Timofei Davydik Sep 25 '17 at 20:11
  • 1
    @TimofeiDavydik Thanks a lot for this. This is the correct solution, the order matters quite a lot! – Sundeep Nov 13 '18 at 13:46

2 Answers2

5

As it turns out I have a project that demos Java instrumentation, which I have tested out debugging in IntelliJ. Breakpoints work. You can use it as a reference.

https://github.com/noahlz/weaver-demo

Sample run/debug configuration from IntelliJ

Agent App Run/Debug

Also, if you are debugging remotely, the premain might be executing before you can attach to the debugger. Try changing suspend=n to suspend=y in the debug command you are passing to the JVM running the agent code. That way, it will wait until you connect with IntelliJ before proceeding.

noahlz
  • 10,202
  • 7
  • 56
  • 75
  • 1
    "unless you add the agent library to the application's JAR dependencies" - exactly what I did in my aforementioned demo application on GitHub: https://github.com/noahlz/weaver-demo/blob/master/pom.xml – noahlz Dec 17 '14 at 19:07
0

For people using VSCode, I just figured out that breakpoints in premain work for me if I use the integratedTerminal, and that they DONT WORK if I use internalConsole!!!

enter image description here

"java.debug.settings.console": "integratedTerminal"

clankill3r
  • 9,146
  • 20
  • 70
  • 126