From c0008c5889c0d5091cdfefd2bfb08bff96527879 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 4 Dec 2015 09:51:03 -0500 Subject: [PATCH] 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 --- cmd/callgraph/main.go | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/cmd/callgraph/main.go b/cmd/callgraph/main.go index 122b15cb..eed92aaf 100644 --- a/cmd/callgraph/main.go +++ b/cmd/callgraph/main.go @@ -20,12 +20,14 @@ package main // import "golang.org/x/tools/cmd/callgraph" // callee file/line/col import ( + "bufio" "bytes" "flag" "fmt" "go/build" "go/token" "io" + "log" "os" "runtime" "text/template" @@ -41,15 +43,21 @@ import ( "golang.org/x/tools/go/ssa/ssautil" ) -var algoFlag = flag.String("algo", "rta", - `Call graph construction algorithm (static, cha, rta, pta)`) +// flags +var ( + algoFlag = flag.String("algo", "rta", + `Call graph construction algorithm (static, cha, rta, pta)`) -var testFlag = flag.Bool("test", false, - "Loads test code (*_test.go) for imported packages") + testFlag = flag.Bool("test", false, + "Loads test code (*_test.go) for imported packages") -var formatFlag = flag.String("format", - "{{.Caller}}\t--{{.Dynamic}}-{{.Line}}:{{.Column}}-->\t{{.Callee}}", - "A template expression specifying how to format an edge") + formatFlag = flag.String("format", + "{{.Caller}}\t--{{.Dynamic}}-{{.Line}}:{{.Column}}-->\t{{.Callee}}", + "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() { 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) 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) if err != nil { return err @@ -201,6 +228,7 @@ func doCallgraph(ctxt *build.Context, algo, format string, tests bool, args []st config := &pointer.Config{ Mains: []*ssa.Package{main}, BuildCallGraph: true, + Log: ptalog, } ptares, err := pointer.Analyze(config) if err != nil {