Documentation ¶
Overview ¶
Package callgraphutil implements utilities for golang.org/x/tools/go/callgraph including path searching, graph construction, printing, and more.
Index ¶
- func AddFunction(cg *callgraph.Graph, target *ssa.Function, allFns map[*ssa.Function]bool) error
- func GraphString(g *callgraph.Graph) string
- func InstructionsFor(root *callgraph.Node, v ssa.Value) (si ssa.Instruction)
- func NewGraph(root *ssa.Function, srcFns ...*ssa.Function) (*callgraph.Graph, error)
- func NewVulncheckCallGraph(ctx context.Context, prog *ssa.Program, entries []*ssa.Function) (*callgraph.Graph, error)
- func WriteCSV(w io.Writer, g *callgraph.Graph) error
- func WriteCosmograph(graph, metadata io.Writer, g *callgraph.Graph) error
- func WriteDOT(w io.Writer, g *callgraph.Graph) error
- type Edges
- type Nodes
- type Path
- type Paths
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddFunction ¶
AddFunction analyzes the given target SSA function, adding information to the call graph.
Based on the implementation of golang.org/x/tools/cmd/guru/callers.go: https://cs.opensource.google/go/x/tools/+/master:cmd/guru/callers.go;drc=3e0d083b858b3fdb7d095b5a3deb184aa0a5d35e;bpv=1;bpt=1;l=90
func GraphString ¶
GraphString returns a string representation of the call graph, which is a sequence of nodes separated by newlines, with the callees of each node indented by a tab.
func InstructionsFor ¶
InstructionsFor returns the ssa.Instruction for the given ssa.Value using the given node as the root of the call graph that is searched.
func NewGraph ¶
NewGraph returns a new Graph with the specified root node.
Typically, the root node is the main function of the program, and the srcFns are the source functions that are of interest to the caller. But, the root node can be any function, and the srcFns can be any set of functions.
This algorithm attempts to add all source functions reachable from the root node by traversing the SSA IR and adding edges to the graph; it handles calls to functions, methods, closures, and interfaces. It may miss some complex edges today, such as stucts containing function fields accessed via slice or map indexing. This is a known limitation, but something we hope to improve in the near future. https://github.com/picatz/taint/issues/23
func NewVulncheckCallGraph ¶
func NewVulncheckCallGraph(ctx context.Context, prog *ssa.Program, entries []*ssa.Function) (*callgraph.Graph, error)
NewVulncheckCallGraph builds a call graph of prog based on VTA analysis, straight from the govulncheck project. This is used to demonstrate the difference between the call graph built by this package's algorithm and govulncheck's algorithm (based on CHA and VTA analysis).
This method is based on the following: https://github.com/golang/vuln/blob/7335627909c99e391cf911fcd214badcb8aa6d7d/internal/vulncheck/utils.go#L63
func WriteCSV ¶
WriteCSV writes the given callgraph.Graph to the given io.Writer in CSV format. This format can be used to generate a visual representation of the call graph using many different tools.
func WriteCosmograph ¶
WriteComsmograph writes the given callgraph.Graph to the given io.Writer in CSV format, which can be used to generate a visual representation of the call graph using Comsmograph.
Types ¶
type Nodes ¶
Nodes is a handy alias for a slice of callgraph.Nodes.
type Path ¶
Path is a sequence of callgraph.Edges, where each edge represents a call from a caller to a callee, making up a "chain" of calls, e.g.: main → foo → bar → baz.
func PathSearch ¶
PathSearch returns the first path found from the start node to a node that matches the isMatch function. This is a depth first search, so it will return the first path found, which may not be the shortest path.
To find all paths, use PathsSearch, which returns a collection of paths.
func PathSearchCallTo ¶
PathSearchCallTo returns the first path found from the start node to a node that matches the function name.
type Paths ¶
type Paths []Path
Paths is a collection of paths, which may be logically grouped together, e.g.: all paths from main to foo, or all paths from main to bar.
func PathsSearch ¶
PathsSearch returns all paths found from the start node to a node that matches the isMatch function. Under the hood, this is a depth first search.
To find the first path (which may not be the shortest), use PathSearch.
func PathsSearchCallTo ¶
PathsSearchCallTo returns the paths that call the given function name, which uses SSA function name syntax, e.g.: "(*database/sql.DB).Query".