Skip to content

Remotely Debugging the Java Agent Codebase

Jason Keller edited this page Oct 19, 2022 · 3 revisions

Debugging With the Java Agent

This document details how to remotely debug the Java agent codebase with IntelliJ IDEA, which is the IDE of choice for developers on the Java agent team. Instructions should be somewhat similar for other IDEs but you will need to refer to their documentation.

This documentation is provided as general guidance for those interested in better understanding the Java agent codebase or actively contributing to the project. The Java agent team cannot directly assist users following this guide but we encourage you to file an issue if any mistakes or points of clarification need to be addressed.

Prerequisites

The following requirements must be met before you can remotely debug the Java agent codebase:

Attach Java Agent to an Application and Remotely Debug the Agent Codebase

This example illustrates how to attach the Java agent to the Spring Boot Pet Clinic application and remotely debug into the agent code as the Pet Clinic application is exercised.

Agent Project in IntelliJ IDEA

First, create a Remote JVM Debug run configuration for the Java agent project. This will generate a command line argument that any Java application can be started with which will allow the agent project to remotely connect to it.

Note: By default the command line argument specifies suspend=n which means that the application will not wait for another process to remotely connect to it before starting up. If you want to debug early lifecycle agent logic then instead set suspend=y which will prevent your application from starting until the agent's remote debugger has been attached to the process.

agent-remote-debug-run-config

Application Project in IntelliJ IDEA

Now, create a run configuration for the Pet Clinic application. If you simply run the application a run config will automatically be created (see screenshot). Alternatively, a run config can be manually created.

create-pet-clinic-run-config

Choose Modify options in the PetClinicApplication run config and select Add VM options. Add the -agentlib remote debug option, the -javaagent option to attach the agent jar, and system properties to configure the agent with a license key and app name (see screenshot).

update-pet-clinic-run-config

Debugging in IntelliJ IDEA

At this point, everything is ready to start debugging.

Run the PetClinicApplication application using the run config created earlier. You should see log output indicating that it is listening for a remote debugger to attach as well as startup logging from the Java agent. Note: If you encounter an error stating Cannot load this JVM TI agent twice see the Caveats section below for details on a fix.

start-pet-clinic

Add debug breakpoints to any code of interest in the Java agent project. Start the debugger in the agent project using the previously created run config. You should see console output indicating that the debugger has attached to the remote application process. Exercise code paths in the application to hit your debug breakpoints.

debug-agent

Caveats

  • You cannot add debug breakpoints to weaved framework/library classes in instrumentation modules, as the debugger will not stop on them, however you can add them to any New Relic utility classes that may exist in instrumentation modules.
  • If you encounter the error Cannot load this JVM TI agent twice, check your java command line for duplicate jdwp options. when starting your application then go to the IntelliJ IDEA Preferences menu and change the gradle settings to Build and run using IntelliJ IDEA (see screenshot).
jvm-ti-agent