2013-08-27 16:49:13 -06: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.
|
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
package oracle
|
|
|
|
|
|
|
|
import (
|
2013-09-03 13:29:02 -06:00
|
|
|
"go/token"
|
2013-11-13 07:11:10 -07:00
|
|
|
"sort"
|
2013-08-27 15:58:26 -06:00
|
|
|
|
2013-09-25 15:17:42 -06:00
|
|
|
"code.google.com/p/go.tools/call"
|
2013-09-24 13:08:14 -06:00
|
|
|
"code.google.com/p/go.tools/oracle/serial"
|
2013-11-13 07:11:10 -07:00
|
|
|
"code.google.com/p/go.tools/ssa"
|
2013-08-27 15:58:26 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// callgraph displays the entire callgraph of the current program.
|
|
|
|
//
|
|
|
|
// Nodes may be seem to appear multiple times due to (limited)
|
|
|
|
// context sensitivity.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): add options for restricting the display to a region
|
2013-09-03 13:29:02 -06:00
|
|
|
// of interest: function, package, subgraph, dirtree, goroutine, etc.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): add an option to project away context sensitivity.
|
|
|
|
// The callgraph API should provide this feature.
|
|
|
|
//
|
2013-09-25 15:17:42 -06:00
|
|
|
// TODO(adonovan): add an option to partition edges by call site.
|
|
|
|
//
|
2013-09-03 13:29:02 -06:00
|
|
|
// TODO(adonovan): elide nodes for synthetic functions?
|
2013-08-27 15:58:26 -06:00
|
|
|
//
|
go.tools/oracle: refactor Oracle API to allow repeated queries on same scope.
The existing standalone Query function builds an importer, ssa.Program, oracle,
and query position, executes the query and returns the result.
For clients (such as Frederik Zipp's web-based github.com/fzipp/pythia tool)
that wish to load the program once and make several queries, we now expose
these as separate operations too. Here's a client, in pseudocode:
o := oracle.New(...)
for ... {
qpos := o.ParseQueryPos(...)
res := o.Query(mode, qpos)
print result
}
NB: this is a slight deoptimisation in the one-shot case since we have to
build the entire SSA program with debug info, not just the query package,
since we now don't know the query package at that time.
The 'exact' param to ParseQueryPos needs more thought since its
ideal value is a function of the query mode. This will do for now.
Details:
- expose Oracle type, New() func and Query() method.
- expose QueryPos type and ParseQueryPos func.
- improved package doc comment.
- un-exposed the "needs" bits.
- added test.
R=crawshaw
CC=frederik.zipp, golang-dev
https://golang.org/cl/13810043
2013-09-23 13:02:18 -06:00
|
|
|
func callgraph(o *Oracle, _ *QueryPos) (queryResult, error) {
|
2013-08-27 15:58:26 -06:00
|
|
|
buildSSA(o)
|
|
|
|
|
|
|
|
// Run the pointer analysis and build the complete callgraph.
|
2013-09-25 15:17:42 -06:00
|
|
|
o.config.BuildCallGraph = true
|
|
|
|
ptares := ptrAnalysis(o)
|
2013-09-03 13:29:02 -06:00
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
return &callgraphResult{
|
2013-09-25 15:17:42 -06:00
|
|
|
callgraph: ptares.CallGraph,
|
2013-08-27 15:58:26 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type callgraphResult struct {
|
2013-09-25 15:17:42 -06:00
|
|
|
callgraph call.Graph
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
func (r *callgraphResult) display(printf printfFunc) {
|
|
|
|
printf(nil, `
|
2013-08-27 15:58:26 -06:00
|
|
|
Below is a call graph of the entire program.
|
|
|
|
The numbered nodes form a spanning tree.
|
|
|
|
Non-numbered nodes indicate back- or cross-edges to the node whose
|
|
|
|
number follows in parentheses.
|
|
|
|
`)
|
2013-11-13 07:11:10 -07:00
|
|
|
root := r.callgraph.Root()
|
2013-08-27 15:58:26 -06:00
|
|
|
|
2013-11-13 07:11:10 -07:00
|
|
|
// context-insensitive (CI) call graph.
|
|
|
|
ci := make(map[*ssa.Function]map[*ssa.Function]bool)
|
|
|
|
|
|
|
|
// 1. Visit the CS call graph and build the CI call graph.
|
|
|
|
visited := make(map[call.GraphNode]bool)
|
|
|
|
var visit func(caller call.GraphNode)
|
|
|
|
visit = func(caller call.GraphNode) {
|
|
|
|
if !visited[caller] {
|
|
|
|
visited[caller] = true
|
|
|
|
|
|
|
|
cicallees := ci[caller.Func()]
|
|
|
|
if cicallees == nil {
|
|
|
|
cicallees = make(map[*ssa.Function]bool)
|
|
|
|
ci[caller.Func()] = cicallees
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range caller.Edges() {
|
|
|
|
cicallees[e.Callee.Func()] = true
|
|
|
|
visit(e.Callee)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
visit(root)
|
|
|
|
|
|
|
|
// 2. Print the CI callgraph.
|
|
|
|
printed := make(map[*ssa.Function]int)
|
|
|
|
var print func(caller *ssa.Function, indent int)
|
|
|
|
print = func(caller *ssa.Function, indent int) {
|
|
|
|
if num, ok := printed[caller]; !ok {
|
|
|
|
num = len(printed)
|
|
|
|
printed[caller] = num
|
|
|
|
|
|
|
|
// Sort the children into name order for deterministic* output.
|
|
|
|
// (*mostly: anon funcs' names are not globally unique.)
|
|
|
|
var funcs funcsByName
|
|
|
|
for callee := range ci[caller] {
|
|
|
|
funcs = append(funcs, callee)
|
|
|
|
}
|
|
|
|
sort.Sort(funcs)
|
|
|
|
|
|
|
|
printf(caller, "%d\t%*s%s", num, 4*indent, "", caller)
|
|
|
|
for _, callee := range funcs {
|
2013-09-03 13:29:02 -06:00
|
|
|
print(callee, indent+1)
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-13 07:11:10 -07:00
|
|
|
printf(caller, "\t%*s%s (%d)", 4*indent, "", caller, num)
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
}
|
2013-11-13 07:11:10 -07:00
|
|
|
print(root.Func(), 0)
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
|
2013-11-13 07:11:10 -07:00
|
|
|
type funcsByName []*ssa.Function
|
|
|
|
|
|
|
|
func (s funcsByName) Len() int { return len(s) }
|
|
|
|
func (s funcsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s funcsByName) Less(i, j int) bool { return s[i].String() < s[j].String() }
|
|
|
|
|
2013-09-24 13:08:14 -06:00
|
|
|
func (r *callgraphResult) toSerial(res *serial.Result, fset *token.FileSet) {
|
2013-09-25 15:17:42 -06:00
|
|
|
nodes := r.callgraph.Nodes()
|
|
|
|
|
|
|
|
numbering := make(map[call.GraphNode]int)
|
|
|
|
for i, n := range nodes {
|
|
|
|
numbering[n] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
cg := make([]serial.CallGraph, len(nodes))
|
|
|
|
for i, n := range nodes {
|
2013-09-03 13:29:02 -06:00
|
|
|
j := &cg[i]
|
|
|
|
fn := n.Func()
|
|
|
|
j.Name = fn.String()
|
2013-09-10 12:11:42 -06:00
|
|
|
j.Pos = fset.Position(fn.Pos()).String()
|
2013-09-25 15:17:42 -06:00
|
|
|
for callee := range call.CalleesOf(n) {
|
|
|
|
j.Children = append(j.Children, numbering[callee])
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res.Callgraph = cg
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|