package valuecollector

import "cloud.google.com/go/cmd/go-cloud-debug-agent/internal/valuecollector"

Package valuecollector is used to collect the values of variables in a program.

Index

Functions

func LogString

func LogString(s string, evaluatedExpressions []*cd.Variable, varTable []*cd.Variable) string

LogString produces a string for a logpoint, substituting in variable values using evaluatedExpressions and varTable.

Types

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector is given references to variables from a program being debugged using AddVariable. Then when ReadValues is called, the Collector will fetch the values of those variables. Any variables referred to by those values will also be fetched; e.g. the targets of pointers, members of structs, elements of slices, etc. This continues iteratively, building a graph of values, until all the reachable values are fetched, or a size limit is reached.

Variables are passed to the Collector as debug.Var, which is used by x/debug to represent references to variables. Values are returned as cd.Variable, which is used by the Debuglet Controller to represent the graph of values.

For example, if the program has a struct variable:

foo := SomeStruct{a:42, b:"xyz"}

and we call AddVariable with a reference to foo, we will get back a result like:

cd.Variable{Name:"foo", VarTableIndex:10}

which denotes a variable named "foo" which will have its value stored in element 10 of the table that will later be returned by ReadValues. That element might be:

out[10] = &cd.Variable{Members:{{Name:"a", VarTableIndex:11},{Name:"b", VarTableIndex:12}}}

which denotes a struct with two members a and b, whose values are in elements 11 and 12 of the output table:

out[11] = &cd.Variable{Value:"42"}
out[12] = &cd.Variable{Value:"xyz"}

func NewCollector

func NewCollector(prog debug.Program, limit int) *Collector

NewCollector returns a Collector for the given program and size limit. The limit is the maximum size of the slice of values returned by ReadValues.

func (*Collector) AddVariable

func (c *Collector) AddVariable(lv debug.LocalVar) *cd.Variable

AddVariable adds another variable to be collected. The Collector doesn't get the value immediately; it returns a cd.Variable that contains an index into the table which will later be returned by ReadValues.

func (*Collector) FillValue

func (c *Collector) FillValue(v debug.Value, dcv *cd.Variable)

FillValue copies a value into a cd.Variable. Any variables referred to by that value, e.g. struct members and pointer targets, are added to the collector's queue, to be fetched later by ReadValues.

func (*Collector) ReadValues

func (c *Collector) ReadValues() (out []*cd.Variable)

ReadValues fetches values of the variables that were passed to the Collector with AddVariable. The values of any new variables found are also fetched, e.g. the targets of pointers or the members of structs, until we reach the size limit or we run out of values to fetch. The results are output as a []*cd.Variable, which is the type we need to send to the Debuglet Controller after we trigger a breakpoint.

Source Files

valuecollector.go

Version
v0.85.0
Published
Jun 30, 2021
Platform
linux/amd64
Imports
6 packages
Last checked
19 minutes ago

Tools for package owners.