blob: 10939ddfb8479dafcbd0e48cff9ecb1d68332eb6 [file] [log] [blame]
Alan Donovan73473792016-02-11 17:57:17 -05001// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import (
8 "fmt"
9 "go/token"
10
Alan Donovan37bb37e2016-02-11 22:57:18 -050011 "golang.org/x/tools/cmd/guru/serial"
Alan Donovan73473792016-02-11 17:57:17 -050012 "golang.org/x/tools/go/callgraph"
Alan Donovan608d57b2016-02-12 15:39:35 -050013 "golang.org/x/tools/go/callgraph/static"
Alan Donovan73473792016-02-11 17:57:17 -050014 "golang.org/x/tools/go/loader"
15 "golang.org/x/tools/go/ssa"
16 "golang.org/x/tools/go/ssa/ssautil"
Alan Donovan73473792016-02-11 17:57:17 -050017)
18
Koichi Shiraishibe3ddff2017-07-10 14:54:58 +090019// The callstack function displays an arbitrary path from a root of the callgraph
Alan Donovan73473792016-02-11 17:57:17 -050020// to the function at the current position.
21//
22// The information may be misleading in a context-insensitive
23// analysis. e.g. the call path X->Y->Z might be infeasible if Y never
24// calls Z when it is called from X. TODO(adonovan): think about UI.
25//
26// TODO(adonovan): permit user to specify a starting point other than
27// the analysis root.
28//
29func callstack(q *Query) error {
30 fset := token.NewFileSet()
31 lconf := loader.Config{Fset: fset, Build: q.Build}
32
33 if err := setPTAScope(&lconf, q.Scope); err != nil {
34 return err
35 }
36
37 // Load/parse/type-check the program.
Rebecca Stamblere64f1a42016-11-17 16:44:22 -050038 lprog, err := loadWithSoftErrors(&lconf)
Alan Donovan73473792016-02-11 17:57:17 -050039 if err != nil {
40 return err
41 }
42
43 qpos, err := parseQueryPos(lprog, q.Pos, false)
44 if err != nil {
45 return err
46 }
47
48 prog := ssautil.CreateProgram(lprog, 0)
49
50 ptaConfig, err := setupPTA(prog, lprog, q.PTALog, q.Reflection)
51 if err != nil {
52 return err
53 }
54
55 pkg := prog.Package(qpos.info.Pkg)
56 if pkg == nil {
57 return fmt.Errorf("no SSA package")
58 }
59
60 if !ssa.HasEnclosingFunction(pkg, qpos.path) {
61 return fmt.Errorf("this position is not inside a function")
62 }
63
64 // Defer SSA construction till after errors are reported.
65 prog.Build()
66
67 target := ssa.EnclosingFunction(pkg, qpos.path)
68 if target == nil {
69 return fmt.Errorf("no SSA function built for this location (dead code?)")
70 }
71
Alan Donovan608d57b2016-02-12 15:39:35 -050072 var callpath []*callgraph.Edge
Alan Donovan73473792016-02-11 17:57:17 -050073 isEnd := func(n *callgraph.Node) bool { return n.Func == target }
Alan Donovan608d57b2016-02-12 15:39:35 -050074
75 // First, build a callgraph containing only static call edges,
76 // and search for an arbitrary path from a root to the target function.
77 // This is quick, and the user wants a static path if one exists.
78 cg := static.CallGraph(prog)
79 cg.DeleteSyntheticNodes()
80 for _, ep := range entryPoints(ptaConfig.Mains) {
81 callpath = callgraph.PathSearch(cg.CreateNode(ep), isEnd)
82 if callpath != nil {
83 break
84 }
85 }
86
87 // No fully static path found.
88 // Run the pointer analysis and build a complete call graph.
89 if callpath == nil {
90 ptaConfig.BuildCallGraph = true
91 cg := ptrAnalysis(ptaConfig).CallGraph
92 cg.DeleteSyntheticNodes()
93 callpath = callgraph.PathSearch(cg.Root, isEnd)
94 if callpath != nil {
95 callpath = callpath[1:] // remove synthetic edge from <root>
96 }
Alan Donovan73473792016-02-11 17:57:17 -050097 }
98
Alan Donovan2da07202016-04-01 15:04:45 -040099 q.Output(fset, &callstackResult{
Alan Donovan73473792016-02-11 17:57:17 -0500100 qpos: qpos,
101 target: target,
102 callpath: callpath,
Alan Donovan2da07202016-04-01 15:04:45 -0400103 })
Alan Donovan73473792016-02-11 17:57:17 -0500104 return nil
105}
106
107type callstackResult struct {
108 qpos *queryPos
109 target *ssa.Function
110 callpath []*callgraph.Edge
111}
112
Alan Donovan2da07202016-04-01 15:04:45 -0400113func (r *callstackResult) PrintPlain(printf printfFunc) {
Alan Donovan73473792016-02-11 17:57:17 -0500114 if r.callpath != nil {
115 printf(r.qpos, "Found a call path from root to %s", r.target)
116 printf(r.target, "%s", r.target)
117 for i := len(r.callpath) - 1; i >= 0; i-- {
118 edge := r.callpath[i]
119 printf(edge, "%s from %s", edge.Description(), edge.Caller.Func)
120 }
121 } else {
122 printf(r.target, "%s is unreachable in this analysis scope", r.target)
123 }
124}
125
Alan Donovan2da07202016-04-01 15:04:45 -0400126func (r *callstackResult) JSON(fset *token.FileSet) []byte {
Alan Donovan73473792016-02-11 17:57:17 -0500127 var callers []serial.Caller
128 for i := len(r.callpath) - 1; i >= 0; i-- { // (innermost first)
129 edge := r.callpath[i]
130 callers = append(callers, serial.Caller{
131 Pos: fset.Position(edge.Pos()).String(),
132 Caller: edge.Caller.Func.String(),
133 Desc: edge.Description(),
134 })
135 }
Alan Donovan2da07202016-04-01 15:04:45 -0400136 return toJSON(&serial.CallStack{
Alan Donovan73473792016-02-11 17:57:17 -0500137 Pos: fset.Position(r.target.Pos()).String(),
138 Target: r.target.String(),
139 Callers: callers,
Alan Donovan2da07202016-04-01 15:04:45 -0400140 })
Alan Donovan73473792016-02-11 17:57:17 -0500141}