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-25 12:34:39 -06:00
|
|
|
"fmt"
|
2013-09-03 13:29:02 -06:00
|
|
|
"go/token"
|
|
|
|
|
2013-09-24 13:08:14 -06:00
|
|
|
"code.google.com/p/go.tools/oracle/serial"
|
2013-08-27 15:58:26 -06:00
|
|
|
"code.google.com/p/go.tools/pointer"
|
|
|
|
"code.google.com/p/go.tools/ssa"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Callstack displays an arbitrary path from a root of the callgraph
|
|
|
|
// 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.
|
|
|
|
//
|
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 callstack(o *Oracle, qpos *QueryPos) (queryResult, error) {
|
|
|
|
pkg := o.prog.Package(qpos.info.Pkg)
|
2013-08-27 15:58:26 -06:00
|
|
|
if pkg == nil {
|
2013-09-25 12:34:39 -06:00
|
|
|
return nil, fmt.Errorf("no SSA package")
|
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
|
|
|
if !ssa.HasEnclosingFunction(pkg, qpos.path) {
|
2013-09-25 12:34:39 -06:00
|
|
|
return nil, fmt.Errorf("this position is not inside a function")
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
buildSSA(o)
|
|
|
|
|
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
|
|
|
target := ssa.EnclosingFunction(pkg, qpos.path)
|
2013-08-27 15:58:26 -06:00
|
|
|
if target == nil {
|
2013-09-25 12:34:39 -06:00
|
|
|
return nil, fmt.Errorf("no SSA function built for this location (dead code?)")
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the pointer analysis and build the complete call graph.
|
|
|
|
callgraph := make(pointer.CallGraph)
|
|
|
|
o.config.Call = callgraph.AddEdge
|
|
|
|
root := ptrAnalysis(o)
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
seen := make(map[pointer.CallGraphNode]bool)
|
|
|
|
var callstack []pointer.CallSite
|
|
|
|
|
|
|
|
// Use depth-first search to find an arbitrary path from a
|
|
|
|
// root to the target function.
|
|
|
|
var search func(cgn pointer.CallGraphNode) bool
|
|
|
|
search = func(cgn pointer.CallGraphNode) bool {
|
|
|
|
if !seen[cgn] {
|
|
|
|
seen[cgn] = true
|
|
|
|
if cgn.Func() == target {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for callee, site := range callgraph[cgn] {
|
|
|
|
if search(callee) {
|
|
|
|
callstack = append(callstack, site)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for toplevel := range callgraph[root] {
|
|
|
|
if search(toplevel) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
return &callstackResult{
|
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
|
|
|
qpos: qpos,
|
2013-08-27 15:58:26 -06:00
|
|
|
target: target,
|
2013-09-03 13:29:02 -06:00
|
|
|
callstack: callstack,
|
2013-08-27 15:58:26 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type callstackResult struct {
|
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
|
|
|
qpos *QueryPos
|
2013-08-27 15:58:26 -06:00
|
|
|
target *ssa.Function
|
2013-09-03 13:29:02 -06:00
|
|
|
callstack []pointer.CallSite
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
func (r *callstackResult) display(printf printfFunc) {
|
|
|
|
if r.callstack != nil {
|
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
|
|
|
printf(r.qpos, "Found a call path from root to %s", r.target)
|
2013-09-03 13:29:02 -06:00
|
|
|
printf(r.target, "%s", r.target)
|
|
|
|
for _, site := range r.callstack {
|
|
|
|
printf(site, "%s from %s", site.Description(), site.Caller().Func())
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
} else {
|
|
|
|
printf(r.target, "%s is unreachable in this analysis scope", r.target)
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-24 13:08:14 -06:00
|
|
|
func (r *callstackResult) toSerial(res *serial.Result, fset *token.FileSet) {
|
|
|
|
var callers []serial.Caller
|
2013-09-03 13:29:02 -06:00
|
|
|
for _, site := range r.callstack {
|
2013-09-24 13:08:14 -06:00
|
|
|
callers = append(callers, serial.Caller{
|
2013-09-10 12:11:42 -06:00
|
|
|
Pos: fset.Position(site.Pos()).String(),
|
2013-09-03 13:29:02 -06:00
|
|
|
Caller: site.Caller().Func().String(),
|
|
|
|
Desc: site.Description(),
|
|
|
|
})
|
|
|
|
}
|
2013-09-24 13:08:14 -06:00
|
|
|
res.Callstack = &serial.CallStack{
|
2013-09-10 12:11:42 -06:00
|
|
|
Pos: fset.Position(r.target.Pos()).String(),
|
2013-09-03 13:29:02 -06:00
|
|
|
Target: r.target.String(),
|
|
|
|
Callers: callers,
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
}
|