Working with snapshots

After you set up debugging in Stackdriver Debugger and deploy or start your app, you can take or view debug snapshots in the source console.

Overview of snapshots

Snapshots capture local variables and the call stack at a specific line location in your app's source code. You can return a snapshot of your app's state based on conditions and line locations you specify. The snapshot details can help you debug your code.

A few seconds after you set the line location, the snapshot settings are applied to all running instances of your app. The first time any instance runs the code at the snapshot location, Debugger takes a snapshot and makes it available for viewing. Debugger implements several strategies for minimizing latency caused when capturing data.

Snapshots might not be available during startup while the debugger initializes.

Take a debug snapshot

  1. In the Google Cloud Platform Console, open Cloud Source Repositories.

    Open Cloud Source Repositories

    The All repositories page opens. Alternatively, you can open the My source view.

  2. Click the name of a repository and go to the file that contains source code you want to watch.

  3. Click the line number of the source code location.

    Select snapshot location

  4. When prompted, select the app you want to debug, and then click Continue.

    Select snapshot application

  5. Click Create snapshot.

    Create snapshot

    The new snapshot appears in an adjacent pane of the Google Cloud Platform Console.

    New snapshot

Snapshot conditions (optional)

A snapshot condition is an expression that must evaluate to true for the snapshot to be taken. Expressions in Java, Python, and Go are supported. Snapshot conditions are evaluated each time any instance runs the line until the condition evaluates to true or the snapshot times out.

The condition is a full boolean expression that can include logical operators:

travelAccessory == "Towel"
ultimateAnswer <= 42
travelAccessory == "Towel" && ultimateAnswer <= 42

Set a snapshot condition

  1. In the GCP Console, click the Snapshots tab, and then select a snapshot.

    Select snapshot

  2. After the Snapshot panel opens, click Edit snapshot.

    Edit snapshot

  3. In the Condition field, enter the snapshot condition.

  4. Click Update.

You can use the following language features to express conditions.

Java

Most Java expressions are supported, including:
  • Local variables: a == 8.
  • Numerical and boolean operations: x + y < 20.
  • Instance and static fields: this.counter == 20, this.myObj.isShutdown, myStatic, or com.mycompany.MyClass.staticMember.
  • String comparisons with the equality operator: myString == "abc".
  • Function calls. Only read-only functions can be used. For example, StringBuilder.indexOf() is supported, but StringBuilder.append() is not.
  • Type casting, with fully qualified types: ((com.myprod.ClassImpl) myInterface).internalField.

The following language features are not supported:

  • Unboxing of numeric types, such as Integer; use myInteger.value instead.

Python

Most Python expressions are supported, including:
  • Reading local and global variables.
  • Reading from arrays, lists, slices, dictionaries, and objects.
  • Calling simple methods.

The following language features are not supported:

  • Calling functions that allocate new objects or use complex constructs.
  • Creating new objects inside the expression.

Go (Beta)

Most Go expression syntax is supported, including:
  • Reading local and global variables.
  • Reading from arrays, slices, maps, and structs.

The following language features are not supported:

  • Reading from interface values.
  • Type conversions and composite literals.
  • Function calls other than len.

Expressions (optional)

Snapshot expressions let you evaluate complex expressions or traverse object hierarchies when a snapshot is taken. Expressions support the same language features as snapshot conditions described above.

The following are typical uses for expressions:

  • Viewing static or global variables that are not part of the local variable set.

  • Viewing deeply nested member variables without having to expand a local variable in the debugger panel every time.

  • Avoiding repetitive mathematical calculations. For example, calculating a duration in seconds with (endTimeMillis - startTimeMillis) / 1000.0.

Add an expression

  1. In the lower pane of the GCP Console, click the Snapshots tab, and then select a snapshot.

    Select snapshot

  2. In the panel that opens, click Edit snapshot.

    Edit snapshot

  3. In the Expression field, enter the expression.

    If you want to add expressions, press the Tab key.

  4. Click Update.

View a snapshot

Snapshot data appears in the source console after the app runs the code at the location you specified.

  1. In the lower pane of the GCP Console, click the Snapshots tab, and then select a snapshot.

    Select snapshot

    A panel containing snapshot details displays values of instance variables and the stack trace at the snapshot code location.

    View snapshot

    You can examine the value of local variables at the moment the snapshot was taken and drill down into deeper data structures. You can also click on any call stack frame and examine the local variables at that level in the stack.

Retake a snapshot

A snapshot is only taken once. To capture another snapshot of your app state at the same location, follow these steps:

  1. In the Snapshots panel of the GCP Console, hold the pointer over the snapshot.

  2. Click Retake.

Retake snapshot

Remove a snapshot location

  1. In the Snapshots panel of the GCP Console, hold the pointer over the snapshot.

  2. Click Delete .

Share snapshots

You can share a snapshot with another project member by sharing the snapshot URL from your browser. You can also save this URL for future reference and come back to it again to view its results. Debugger uses a new URL for each snapshot taken. This lets you share distinct sets of results even if they were captured at the same location in the code.

What's next

Bu sayfayı yararlı buldunuz mu? Lütfen görüşünüzü bildirin:

Şunun hakkında geri bildirim gönderin...

Cloud Source Repositories Documentation