Open iNevd opened 3 years ago
example: https://github.com/iNevd/go-callvis/commit/3aee0ac40d1a2b9e86993be5014958f0138d8f18
First, find the main function Node
var newRoot *callgraph.Node for _, r := range cg.Nodes { if r.Func.Name() == "main" { newRoot = r break } }
Second, visit all the Node in depth-first order start from newRoot with set depth.
newRoot
replace callgraph.GraphVisitEdges to GraphVisitNodeDFS https://github.com/ofabry/go-callvis/blob/fbe7e9f07309d21b548a61ca04dda073234ec8d6/output.go#L132 func GraphVisitNodeDFS(maxDepth int, n *callgraph.Node, edge func(*callgraph.Edge) error) error { seen := make(map[*callgraph.Node]bool) var visit func(d int, n *callgraph.Node) error visit = func(d int, n *callgraph.Node) error { if maxDepth > 0 && d > maxDepth { return nil } if !seen[n] { seen[n] = true d += 1 for _, e := range n.Out { if err := visit(d, e.Callee); err != nil { return err } if err := edge(e); err != nil { return err } } } return nil } for _, e := range n.Out { if err := visit(0, e.Callee); err != nil { return err } if err := edge(e); err != nil { return err } } return nil }
replace callgraph.GraphVisitEdges to GraphVisitNodeDFS
callgraph.GraphVisitEdges
GraphVisitNodeDFS
https://github.com/ofabry/go-callvis/blob/fbe7e9f07309d21b548a61ca04dda073234ec8d6/output.go#L132
func GraphVisitNodeDFS(maxDepth int, n *callgraph.Node, edge func(*callgraph.Edge) error) error { seen := make(map[*callgraph.Node]bool) var visit func(d int, n *callgraph.Node) error visit = func(d int, n *callgraph.Node) error { if maxDepth > 0 && d > maxDepth { return nil } if !seen[n] { seen[n] = true d += 1 for _, e := range n.Out { if err := visit(d, e.Callee); err != nil { return err } if err := edge(e); err != nil { return err } } } return nil } for _, e := range n.Out { if err := visit(0, e.Callee); err != nil { return err } if err := edge(e); err != nil { return err } } return nil }
example: https://github.com/iNevd/go-callvis/commit/3aee0ac40d1a2b9e86993be5014958f0138d8f18
First, find the main function Node
Second, visit all the Node in depth-first order start from
newRoot
with set depth.