2016-02-11 15:57:17 -07:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/token"
|
|
|
|
|
2016-02-11 20:57:18 -07:00
|
|
|
"golang.org/x/tools/cmd/guru/serial"
|
2016-02-11 15:57:17 -07:00
|
|
|
"golang.org/x/tools/go/callgraph"
|
2016-02-12 13:39:35 -07:00
|
|
|
"golang.org/x/tools/go/callgraph/static"
|
2016-02-11 15:57:17 -07:00
|
|
|
"golang.org/x/tools/go/loader"
|
|
|
|
"golang.org/x/tools/go/ssa"
|
|
|
|
"golang.org/x/tools/go/ssa/ssautil"
|
|
|
|
)
|
|
|
|
|
2017-07-09 23:54:58 -06:00
|
|
|
// The callstack function displays an arbitrary path from a root of the callgraph
|
2016-02-11 15:57:17 -07:00
|
|
|
// to the function at the current position.
|
|
|
|
//
|
|
|
|
// The information may be misleading in a context-insensitive
|
|
|
|
// analysis. e.g. the call path X->Y->Z might be infeasible if Y never
|
|
|
|
// calls Z when it is called from X. TODO(adonovan): think about UI.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): permit user to specify a starting point other than
|
|
|
|
// the analysis root.
|
|
|
|
//
|
|
|
|
func callstack(q *Query) error {
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
lconf := loader.Config{Fset: fset, Build: q.Build}
|
|
|
|
|
|
|
|
if err := setPTAScope(&lconf, q.Scope); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load/parse/type-check the program.
|
2016-11-17 14:44:22 -07:00
|
|
|
lprog, err := loadWithSoftErrors(&lconf)
|
2016-02-11 15:57:17 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
qpos, err := parseQueryPos(lprog, q.Pos, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
prog := ssautil.CreateProgram(lprog, 0)
|
|
|
|
|
|
|
|
ptaConfig, err := setupPTA(prog, lprog, q.PTALog, q.Reflection)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg := prog.Package(qpos.info.Pkg)
|
|
|
|
if pkg == nil {
|
|
|
|
return fmt.Errorf("no SSA package")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ssa.HasEnclosingFunction(pkg, qpos.path) {
|
|
|
|
return fmt.Errorf("this position is not inside a function")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defer SSA construction till after errors are reported.
|
|
|
|
prog.Build()
|
|
|
|
|
|
|
|
target := ssa.EnclosingFunction(pkg, qpos.path)
|
|
|
|
if target == nil {
|
|
|
|
return fmt.Errorf("no SSA function built for this location (dead code?)")
|
|
|
|
}
|
|
|
|
|
2016-02-12 13:39:35 -07:00
|
|
|
var callpath []*callgraph.Edge
|
|
|
|
isEnd := func(n *callgraph.Node) bool { return n.Func == target }
|
|
|
|
|
|
|
|
// First, build a callgraph containing only static call edges,
|
|
|
|
// and search for an arbitrary path from a root to the target function.
|
|
|
|
// This is quick, and the user wants a static path if one exists.
|
|
|
|
cg := static.CallGraph(prog)
|
2016-02-11 15:57:17 -07:00
|
|
|
cg.DeleteSyntheticNodes()
|
2016-02-12 13:39:35 -07:00
|
|
|
for _, ep := range entryPoints(ptaConfig.Mains) {
|
|
|
|
callpath = callgraph.PathSearch(cg.CreateNode(ep), isEnd)
|
|
|
|
if callpath != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 15:57:17 -07:00
|
|
|
|
2016-02-12 13:39:35 -07:00
|
|
|
// No fully static path found.
|
|
|
|
// Run the pointer analysis and build a complete call graph.
|
|
|
|
if callpath == nil {
|
|
|
|
ptaConfig.BuildCallGraph = true
|
|
|
|
cg := ptrAnalysis(ptaConfig).CallGraph
|
|
|
|
cg.DeleteSyntheticNodes()
|
|
|
|
callpath = callgraph.PathSearch(cg.Root, isEnd)
|
|
|
|
if callpath != nil {
|
|
|
|
callpath = callpath[1:] // remove synthetic edge from <root>
|
|
|
|
}
|
2016-02-11 15:57:17 -07:00
|
|
|
}
|
|
|
|
|
2016-04-01 13:04:45 -06:00
|
|
|
q.Output(fset, &callstackResult{
|
2016-02-11 15:57:17 -07:00
|
|
|
qpos: qpos,
|
|
|
|
target: target,
|
|
|
|
callpath: callpath,
|
2016-04-01 13:04:45 -06:00
|
|
|
})
|
2016-02-11 15:57:17 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type callstackResult struct {
|
|
|
|
qpos *queryPos
|
|
|
|
target *ssa.Function
|
|
|
|
callpath []*callgraph.Edge
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:04:45 -06:00
|
|
|
func (r *callstackResult) PrintPlain(printf printfFunc) {
|
2016-02-11 15:57:17 -07:00
|
|
|
if r.callpath != nil {
|
|
|
|
printf(r.qpos, "Found a call path from root to %s", r.target)
|
|
|
|
printf(r.target, "%s", r.target)
|
|
|
|
for i := len(r.callpath) - 1; i >= 0; i-- {
|
|
|
|
edge := r.callpath[i]
|
|
|
|
printf(edge, "%s from %s", edge.Description(), edge.Caller.Func)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf(r.target, "%s is unreachable in this analysis scope", r.target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:04:45 -06:00
|
|
|
func (r *callstackResult) JSON(fset *token.FileSet) []byte {
|
2016-02-11 15:57:17 -07:00
|
|
|
var callers []serial.Caller
|
|
|
|
for i := len(r.callpath) - 1; i >= 0; i-- { // (innermost first)
|
|
|
|
edge := r.callpath[i]
|
|
|
|
callers = append(callers, serial.Caller{
|
|
|
|
Pos: fset.Position(edge.Pos()).String(),
|
|
|
|
Caller: edge.Caller.Func.String(),
|
|
|
|
Desc: edge.Description(),
|
|
|
|
})
|
|
|
|
}
|
2016-04-01 13:04:45 -06:00
|
|
|
return toJSON(&serial.CallStack{
|
2016-02-11 15:57:17 -07:00
|
|
|
Pos: fset.Position(r.target.Pos()).String(),
|
|
|
|
Target: r.target.String(),
|
|
|
|
Callers: callers,
|
2016-04-01 13:04:45 -06:00
|
|
|
})
|
2016-02-11 15:57:17 -07:00
|
|
|
}
|