Debugging

Cloud Debugger

Cloud Debugger lets you inspect the state of an application, at any code location, without stopping or slowing down the running application.
Cloud Debugger is supported in all Google Cloud runtime environments (except for Cloud Functions) and can also be used when running applications in non-Google Cloud environments (on-premises, other clouds).

Enable API

1
gcloud services enable clouddebugger.googleapis.com
Copied!

Snapshot

A Snapshot can introspect the context information on a given line of code as user go through the code flow.
Example of a Snapshot

Logpoint

A Logpoint can add additional log messages to a running application without modifying the code nor redeploying the code.
Example of a Logpoint

Conditions

In both Snapshot and Logpoint, you can specify conditionals so you can capture specific information for a specific request (e.g., match against a session ID, or request ID).

Java Agent

Cloud Debugger works by adding a Java agent to your JVM startup argument, and the agent can communicate with the Cloud Debugger service in the Cloud. Through the Cloud Console, you can then instruct your JVM instances to take a Snapshot of the application state at a specific line of code, or to add an additional log message on a specific line.

Agent Files

There are 2 types of Cloud Debugger Java agents that authenticates with Cloud Debugger service differently:
Type
When to use?
Latest Version
Versioned URL
Machine Credentials
Google Cloud runtime environments
Download
https://storage.googleapis.com/cloud-debugger/archive/java/${VERSION}/cdbg_java_agent_gce.tar.gz
Service Account Key
Non-Google Cloud environments
Download
https://storage.googleapis.com/cloud-debugger/archive/java/${VERSION}/cdbg_java_agent_service_account.tar.gz
You can find all the versions in the cloud-debug-java GitHub repository. For example, Cloud Debugger agent version 2.25 using Machine Credentials can be downloaded with URL: https://storage.googleapis.com/cloud-debugger/archive/java/2.25/cdbg_java_agent_gce.tar.gz

Agent Configurations

Agent Path

To use the agent, you'll need to configure the JVM command line using the standard -agentpath , e.g.:
1
java -agentpath:/opt/cdbg/cdbg_java_agent.so \
2
-jar ...
Copied!
Rather than hard coding the startup command line, you can also configure it with the JAVA_TOOL_OPTIONS environmental variable:
1
JAVA_TOOL_OPTIONS="-agentpath:/opt/cdbg/cdbg_java_agent.so"
2
java -jar ...
Copied!

System Properties

There are additional flags you can pass to the Java agent using Java's system properties.
System Properties
Description
Required
com.google.cdbg
.module
The name of your application.
Not required for Cloud Run or App Engine.
com.google.cdbg
.version
The version of your application.
Not required for Cloud Run or App Engine.
com.google.cdbg
.breakpoints
.enable_canary
true or false.Whether to turn on debugger for a subset of the running instances. See Canary snapshots and logpoints documentation.
Not required and defaults to false.
com.google.cdbg
.auth.serviceaccount.enable
true or false. Whether to authenticate with a Service Account key file.
Required when running outside of Google Cloud.
com.google.cdbg.auth
.serviceaccount.jsonfile
File path to the Service Account key file.
Required when running outside of Google Cloud.
For example, you can enable the snapshot using the system property:
1
JAVA_TOOL_OPTIONS="-agentpath:/opt/cdbg/cdbg_java_agent.so \
2
-Dcom.google.cdbg.breakpoints.enable_canary=true"
Copied!

Logging

By default the Cloud Debugger agent writes its logs to cdbg_java_agent.INFO file in the default logging directory. You can overwrite the log file path:
1
java -agentpath:/opt/cdbg/cdbg_java_agent.so=--log_dir=/tmp/cdbg.log \
2
-jar ...
Copied!
Alternatively you can make the Java Cloud Debugger log to stderr:
1
java -agentpath:/opt/cdbg/cdbg_java_agent.so=--logtostderr=1 \
2
-jar ...
Copied!
See Setting Up Cloud Debugger for Java documentation for more information.

Runtime Configuration

App Engine
Cloud Run
Kubernetes Engine
Compute Engine
Non-Google Cloud Environment
Follow App Engine Hello World! instructions to deploy an application to App Engine.
Cloud Debugger agent is automatically added to your App Engine application.
In Cloud Debugger console, you can see the Default service in the drop down:
Add the Cloud Debugger Java agent to the container, and configure the agent in the startup command line.

Clone

1
# Clone the sample repository manually
2
git clone https://github.com/saturnism/jvm-helloworld-by-example
3
cd jvm-helloworld-by-example/helloworld-springboot-tomcat
Copied!

Containerize with a Dockerfile

In the Dockerfile, download the Cloud Debugger and build it as part of the container image:
Dockerfile
1
FROM openjdk:11
2
3
# Create a directory for the Debugger. Add and unzip the agent in the directory.
4
RUN mkdir /opt/cdbg && \
5
wget -qO- https://storage.googleapis.com/cloud-debugger/compute-java/debian-wheezy/cdbg_java_agent_gce.tar.gz | \
6
tar xvz -C /opt/cdbg
7
8
COPY target/helloworld.jar /app.jar
9
10
ENTRYPOINT ["java", "-jar", "/app.jar"]
Copied!
Then build and push the container:
1
mvn package
2
3
PROJECT_ID=$(gcloud config get-value project)
4
docker build -t gcr.io/${PROJECT_ID}/helloworld .
5
docker push gcr.io/${PROJECT_ID}/helloworld
Copied!

Containerize with Jib

Download the Cloud Debugger Java agent into src/main/jib directory so that Jib can include the agent files as part of the container image:
1
# Make a directory to store the Java agent
2
mkdir -p src/main/jib/opt/cdbg
3
4
# Download and extract the Java agent to the directory
5
wget -qO- https://storage.googleapis.com/cloud-debugger/compute-java/debian-wheezy/cdbg_java_agent_gce.tar.gz | \
6
tar xvz -C src/main/jib/opt/cdbg
Copied!
Create the container image with Jib:
1
PROJECT_ID=$(gcloud config get-value project)
2
mvn compile com.google.cloud.tools:jib-maven-plugin:2.4.0:build \
3
-Dimage=gcr.io/${PROJECT_ID}/helloworld
Copied!

Deploy

Deploy to Cloud Run with Debugger Enabled using the environmental variable:
1
gcloud run deploy helloworld \
2
--region=us-central1 \
3
--platform=managed \
4
--allow-unauthenticated \
5
--set-env-vars='JAVA_TOOL_OPTIONS="-agentpath:/opt/cdbg/cdbg_java_agent.so=--logtostderr=1"' \
6
--image=gcr.io/${PROJECT_ID}/helloworld
Copied!
In Cloud Debugger console, you can see the helloworld service in the drop down:
Add the Cloud Debugger Java agent to the container, and configure the agent in the startup command line.

Clone

1
# Clone the sample repository manually
2
git clone https://github.com/saturnism/jvm-helloworld-by-example
3
cd jvm-helloworld-by-example/helloworld-springboot-tomcat
Copied!

Containerize with a Dockerfile

In the Dockerfile, download the Cloud Debugger and build it as part of the container image:
Dockerfile
1
FROM openjdk:11
2
3
# Create a directory for the Debugger. Add and unzip the agent in the directory.
4
RUN mkdir /opt/cdbg && \
5
wget -qO- https://storage.googleapis.com/cloud-debugger/compute-java/debian-wheezy/cdbg_java_agent_gce.tar.gz | \
6
tar xvz -C /opt/cdbg
7
8
COPY target/helloworld.jar /app.jar
9
10
ENTRYPOINT ["java", "-jar", "/app.jar"]
Copied!
Then build and push the container:
1
mvn package
2
3
PROJECT_ID=$(gcloud config get-value project)
4
docker build -t gcr.io/${PROJECT_ID}/helloworld .
5
docker push gcr.io/${PROJECT_ID}/helloworld
Copied!

Containerize with Jib

Download the Cloud Debugger Java agent into src/main/jib directory so that Jib can include the agent files as part of the container image:
1
# Make a directory to store the Java agent
2
mkdir -p src/main/jib/opt/cdbg
3
4
# Download and extract the Java agent to the directory
5
wget -qO- https://storage.googleapis.com/cloud-debugger/compute-java/debian-wheezy/cdbg_java_agent_gce.tar.gz | \
6
tar xvz -C src/main/jib/opt/cdbg
Copied!
Create the image with Jib:
1
PROJECT_ID=$(gcloud config get-value project)
2
mvn compile com.google.cloud.tools:jib-maven-plugin:2.4.0:build \
3
-Dimage=gcr.io/${PROJECT_ID}/helloworld
Copied!

Deploy

Deploy to Kubernetes Engine with Debugger Enabled using the environmental variable using a Deployment YAML:
1
# Make a directory to store Kubernetes YAMLs
2
mkdir k8s/
Copied!
Create a Deployment YAML file and configure the environmental variable:
k8s/deployment.yaml
1
apiVersion: apps/v1
2
kind: Deployment
3
metadata:
4
labels:
5
app: helloworld
6
name: helloworld
7
spec:
8
replicas: 1
9
selector:
10
matchLabels:
11
app: helloworld
12
template:
13
metadata:
14
labels:
15
app: helloworld
16
spec:
17
containers:
18
- image: gcr.io/YOUR_PROJECT_ID/helloworld
19
name: helloworld
20
env:
21
- name: JAVA_TOOL_OPTIONS
22
value: "-agentpath:/opt/cdbg/cdbg_java_agent.so=--logtostderr=1 -Dcom.google.cdbg.module=helloworld-gke -Dcom.google.cdbg.version=1.0"
Copied!
Deploy the YAML file:
1
kubectl apply -f k8s/deployment.yaml
Copied!
In Cloud Debugger console, you can see the helloworld-gke service in the drop down:
Follow the Compute Engine Hello World! to deploy an application in Compute Engine.
SSH into the Compute Engine instance:
1
gcloud compute ssh helloworld
Copied!
From the Compute Engine instance, download the Java agent:
1
sudo mkdir -p /opt/cdbg
2
curl -s -o- https://storage.googleapis.com/cloud-debugger/compute-java/debian-wheezy/cdbg_java_agent_gce.tar.gz \
3
| sudo tar xvz -C /opt/cdbg
Copied!
Run the Java application with the Cloud Debugger agent:
1
java -agentpath:/opt/cdbg/cdbg_java_agent.so=--logtostderr=1 \
2
-Dcom.google.cdbg.module=helloworld-gce \
3
-Dcom.google.cdbg.version=1.0 \
4
-jar helloworld.jar
Copied!
In Cloud Debugger console, you can see the helloworld-gce service in the drop down:
You can attach the Cloud Debugger agent to any Java application even if it runs outside of the Google Cloud environment (whether it's in a container, or on your local laptop, or in another cloud). Authentication has to be done using Service Account key file rather than using the Machine Credentials.
This only works on a Linux x86 based system.

Clone

1
# Clone the sample repository manually
2
git clone https://github.com/saturnism/jvm-helloworld-by-example
3
cd jvm-helloworld-by-example/helloworld-springboot-tomcat
Copied!

Build

1
mvn package
Copied!

Download Agent

1
sudo mkdir -p /opt/cdbg
2
curl -s -o- https://storage.googleapis.com/cloud-debugger/compute-java/debian-wheezy/cdbg_java_agent_gce.tar.gz \
3
| sudo tar xvz -C /opt/cdbg
Copied!

Create a Service Account

1
PROJECT_ID=$(gcloud config get-value project)
2
gcloud iam service-accounts create helloworld-app
Copied!

Add IAM Permission

1
PROJECT_ID=$(gcloud config get-value project)
2
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
3
--member serviceAccount:[email protected]${PROJECT_ID}.iam.gserviceaccount.com \
4
--role roles/clouddebugger.agent
Copied!

Create a Service Account Key File

1
PROJECT_ID=$(gcloud config get-value project)
2
gcloud iam service-accounts keys create \
3
$HOME/helloworld-app-sa.json \
4
--iam-account [email protected]${PROJECT_ID}.iam.gserviceaccount.com
Copied!

Use Service Account Cloud Debugger Agent

1
java -agentpath:/opt/cdbg/cdbg_java_agent.so=--logtostderr=1 \
2
-Dcom.google.cdbg.module=helloworld-local \
3
-Dcom.google.cdbg.version=1.0 \
4
-Dcom.google.cdbg.auth.serviceaccount.enable=true \
5
-Dcom.google.cdbg.auth.serviceaccount.jsonfile=$HOME/hellworld-app-sa.json \
6
-jar target/helloworld.jar
Copied!

Source Code

Cloud Debugger needs to have access to the application's source code in order for you to add a Snapshot or Logpoint from the Cloud Debugger console. There are severals ways to associating the source code:
  • Existing Git Repository
  • Source code capture / upload
  • Git repository reference from git.properties
  • IntelliJ Cloud Code plugin

Git Repository

From Cloud Debugger console, navigate to Deployed Files > Add source code.
Cloud Debugger console and find "Add source code"
Choose an Alternative source code.
Alternative source code choices
For example, using an existing GitHub repository:
Select source from GitHub.com
Once selected, the contents of the Git repository will be available to navigate.

Upload

Upload from Browser

From Cloud Debugger console, navigate to Deployed Files > Add source code.
Cloud Debugger console and find "Add source code"
Choose an Alternative source code.
Click on Local files's Select Source, then simply select the folder/directory that contains the source code.

Upload from Command Line

You can use gcloud CLI to upload the source code into a Source Captures repository.
Create a Source Captures repository:
1
# Enable API
2
gcloud services enable sourcerepo.googleapis.com
3
4
# Create a source capture repository
5
gcloud source repos create google-source-captures
Copied!
In the Alternative source code choices, scroll to the very bottom is Upload a source code capture to Google servers.
Do not click on Select source yet.
Alternative source code choices
Use the command line to upload the source code (for example, if you deployed the Helloworld Application):
1
# Clone the sample repository manually
2
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
3
cd java-docs-samples/appengine-java11/springboot-helloworld
4
5
# Upload just the `src/` directory.
6
# Note that the `branch` value is important and you must use the same value
7
# that's shown in the UI
8
gcloud beta debug source upload \
9
--project=<FROM THE UI> \
10
--branch=<FROM THE UI> \
11
src/
Copied!
Once uploaded, click Select source.

Use git.properties

You can associate a Git repository using the git-commit-plugin that generates a git.properties file, which contains the information to the Git repository. This only works if the repository is publicly accessible.
1
<plugin>
2
<groupId>pl.project13.maven</groupId>
3
<artifactId>git-commit-id-plugin</artifactId>
4
<version>4.0.1</version>
5
<executions>
6
<execution>
7
<goals>
8
<goal>revision</goal>
9
</goals>
10
</execution>
11
</executions>
12
</plugin>
Copied!
Cloud Debugger service will automatically examine this file, and clone the code, and checkout the corresponding revision.

IntelliJ with Cloud Code

You can use the Cloud Code plugin to directly add a Snapshot point without using the Cloud Debugger console.
Navigate to Tools > Cloud Code > Attach Cloud Debugger.
Once configured the IntelliJ profile, you can add Snapshot to source code directly from the IDE.
Debug in the Cloud

Learn More