x/tools/cmd/callgraph: add -ptalog flag
Since this tool is a handy way to run the pointer analysis during debugging. Change-Id: If9a62f71dd8651ad6c782ead85bb84f09c703cba Reviewed-on: https://go-review.googlesource.com/17382 Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
eb69368e05
commit
c0008c5889
|
@ -20,12 +20,14 @@ package main // import "golang.org/x/tools/cmd/callgraph"
|
||||||
// callee file/line/col
|
// callee file/line/col
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/build"
|
"go/build"
|
||||||
"go/token"
|
"go/token"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
@ -41,15 +43,21 @@ import (
|
||||||
"golang.org/x/tools/go/ssa/ssautil"
|
"golang.org/x/tools/go/ssa/ssautil"
|
||||||
)
|
)
|
||||||
|
|
||||||
var algoFlag = flag.String("algo", "rta",
|
// flags
|
||||||
`Call graph construction algorithm (static, cha, rta, pta)`)
|
var (
|
||||||
|
algoFlag = flag.String("algo", "rta",
|
||||||
|
`Call graph construction algorithm (static, cha, rta, pta)`)
|
||||||
|
|
||||||
var testFlag = flag.Bool("test", false,
|
testFlag = flag.Bool("test", false,
|
||||||
"Loads test code (*_test.go) for imported packages")
|
"Loads test code (*_test.go) for imported packages")
|
||||||
|
|
||||||
var formatFlag = flag.String("format",
|
formatFlag = flag.String("format",
|
||||||
"{{.Caller}}\t--{{.Dynamic}}-{{.Line}}:{{.Column}}-->\t{{.Callee}}",
|
"{{.Caller}}\t--{{.Dynamic}}-{{.Line}}:{{.Column}}-->\t{{.Callee}}",
|
||||||
"A template expression specifying how to format an edge")
|
"A template expression specifying how to format an edge")
|
||||||
|
|
||||||
|
ptalogFlag = flag.String("ptalog", "",
|
||||||
|
"Location of the points-to analysis log file, or empty to disable logging.")
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
|
flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
|
||||||
|
@ -194,6 +202,25 @@ func doCallgraph(ctxt *build.Context, algo, format string, tests bool, args []st
|
||||||
cg = cha.CallGraph(prog)
|
cg = cha.CallGraph(prog)
|
||||||
|
|
||||||
case "pta":
|
case "pta":
|
||||||
|
// Set up points-to analysis log file.
|
||||||
|
var ptalog io.Writer
|
||||||
|
if *ptalogFlag != "" {
|
||||||
|
if f, err := os.Create(*ptalogFlag); err != nil {
|
||||||
|
log.Fatalf("Failed to create PTA log file: %s", err)
|
||||||
|
} else {
|
||||||
|
buf := bufio.NewWriter(f)
|
||||||
|
ptalog = buf
|
||||||
|
defer func() {
|
||||||
|
if err := buf.Flush(); err != nil {
|
||||||
|
log.Printf("flush: %s", err)
|
||||||
|
}
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
log.Printf("close: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
main, err := mainPackage(prog, tests)
|
main, err := mainPackage(prog, tests)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -201,6 +228,7 @@ func doCallgraph(ctxt *build.Context, algo, format string, tests bool, args []st
|
||||||
config := &pointer.Config{
|
config := &pointer.Config{
|
||||||
Mains: []*ssa.Package{main},
|
Mains: []*ssa.Package{main},
|
||||||
BuildCallGraph: true,
|
BuildCallGraph: true,
|
||||||
|
Log: ptalog,
|
||||||
}
|
}
|
||||||
ptares, err := pointer.Analyze(config)
|
ptares, err := pointer.Analyze(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue