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-08-27 15:58:26 -06:00
|
|
|
"strings"
|
|
|
|
|
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-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.
|
|
|
|
Some nodes may appear multiple times due to context-sensitive
|
|
|
|
treatment of some calls.
|
|
|
|
`)
|
|
|
|
|
2013-09-25 15:17:42 -06:00
|
|
|
seen := make(map[call.GraphNode]int)
|
|
|
|
var print func(cgn call.GraphNode, indent int)
|
|
|
|
print = func(cgn call.GraphNode, indent int) {
|
|
|
|
fn := cgn.Func()
|
|
|
|
if num, ok := seen[cgn]; !ok {
|
|
|
|
num = len(seen)
|
|
|
|
seen[cgn] = num
|
|
|
|
printf(fn, "%d\t%s%s", num, strings.Repeat(" ", indent), fn)
|
|
|
|
// Don't use Edges(), which distinguishes callees by call site.
|
|
|
|
for callee := range call.CalleesOf(cgn) {
|
2013-09-03 13:29:02 -06:00
|
|
|
print(callee, indent+1)
|
|
|
|
}
|
|
|
|
} else {
|
2013-09-25 15:17:42 -06:00
|
|
|
printf(fn, "\t%s%s (%d)", strings.Repeat(" ", indent), fn, num)
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
}
|
2013-09-25 15:17:42 -06:00
|
|
|
print(r.callgraph.Root(), 0)
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|