Skip to content

Commit f4cbf34

Browse files
committed
cmd/compile: allow directory specification for GOSSAFUNC output
This was useful for debugging failures occurring during make.bash. The added flush also ensures that any hints in the GOSSAFUNC output are flushed before fatal exit. The environment variable GOSSADIR specifies where the SSA html debugging files should be placed. To avoid collisions, each one is written into the [package].[functionOrMethod].html, where [package] is the filepath separator separated package name, function is the function name, and method is either (*Type).Method, or Type.Method, as appropriate. Directories are created as necessary to make this work. Change-Id: I420927426b618b633bb1ffc51cf0f223b8f6d49c Reviewed-on: https://go-review.googlesource.com/c/go/+/252338 Trust: David Chase <[email protected]> Run-TryBot: David Chase <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent c78c79f commit f4cbf34

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

src/cmd/compile/internal/gc/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ func Main(archInit func(*Arch)) {
516516
}
517517

518518
ssaDump = os.Getenv("GOSSAFUNC")
519+
ssaDir = os.Getenv("GOSSADIR")
519520
if ssaDump != "" {
520521
if strings.HasSuffix(ssaDump, "+") {
521522
ssaDump = ssaDump[:len(ssaDump)-1]

src/cmd/compile/internal/gc/ssa.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"html"
1111
"os"
12+
"path/filepath"
1213
"sort"
1314

1415
"bufio"
@@ -26,6 +27,7 @@ var ssaConfig *ssa.Config
2627
var ssaCaches []ssa.Cache
2728

2829
var ssaDump string // early copy of $GOSSAFUNC; the func name to dump output for
30+
var ssaDir string // optional destination for ssa dump file
2931
var ssaDumpStdout bool // whether to dump to stdout
3032
var ssaDumpCFG string // generate CFGs for these phases
3133
const ssaDumpFile = "ssa.html"
@@ -346,7 +348,13 @@ func buildssa(fn *Node, worker int) *ssa.Func {
346348
s.f.Entry.Pos = fn.Pos
347349

348350
if printssa {
349-
s.f.HTMLWriter = ssa.NewHTMLWriter(ssaDumpFile, s.f, ssaDumpCFG)
351+
ssaDF := ssaDumpFile
352+
if ssaDir != "" {
353+
ssaDF = filepath.Join(ssaDir, myimportpath+"."+name+".html")
354+
ssaD := filepath.Dir(ssaDF)
355+
os.MkdirAll(ssaD, 0755)
356+
}
357+
s.f.HTMLWriter = ssa.NewHTMLWriter(ssaDF, s.f, ssaDumpCFG)
350358
// TODO: generate and print a mapping from nodes to values and blocks
351359
dumpSourcesColumn(s.f.HTMLWriter, fn)
352360
s.f.HTMLWriter.WriteAST("AST", astBuf)

src/cmd/compile/internal/ssa/compile.go

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ func Compile(f *Func) {
4747
stack := make([]byte, 16384)
4848
n := runtime.Stack(stack, false)
4949
stack = stack[:n]
50+
if f.HTMLWriter != nil {
51+
f.HTMLWriter.flushPhases()
52+
}
5053
f.Fatalf("panic during %s while compiling %s:\n\n%v\n\n%s\n", phaseName, f.Name, err, stack)
5154
}
5255
}()
@@ -201,6 +204,13 @@ func (p *pass) addDump(s string) {
201204
p.dump[s] = true
202205
}
203206

207+
func (p *pass) String() string {
208+
if p == nil {
209+
return "nil pass"
210+
}
211+
return p.name
212+
}
213+
204214
// Run consistency checker between each phase
205215
var (
206216
checkEnabled = false

src/cmd/compile/internal/ssa/html.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,23 @@ type HTMLWriter struct {
2828
}
2929

3030
func NewHTMLWriter(path string, f *Func, cfgMask string) *HTMLWriter {
31+
path = strings.Replace(path, "/", string(filepath.Separator), -1)
3132
out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
3233
if err != nil {
3334
f.Fatalf("%v", err)
3435
}
35-
pwd, err := os.Getwd()
36-
if err != nil {
37-
f.Fatalf("%v", err)
36+
reportPath := path
37+
if !filepath.IsAbs(reportPath) {
38+
pwd, err := os.Getwd()
39+
if err != nil {
40+
f.Fatalf("%v", err)
41+
}
42+
reportPath = filepath.Join(pwd, path)
3843
}
3944
html := HTMLWriter{
4045
w: out,
4146
Func: f,
42-
path: filepath.Join(pwd, path),
47+
path: reportPath,
4348
dot: newDotWriter(cfgMask),
4449
}
4550
html.start()

0 commit comments

Comments
 (0)