cmd/compile: make GOSSAHASH package-sensitive, also append to log files

Turns out if your failure is in a function with a name like "Reset()"
there will be a lot of hits on the same hashcode.  Adding package sensitivity
solves this problem.

In additionm, it turned out that in the case that a logfile was specified
for the GOSSAHASH logging, that it was opened in create mode, which meant
that multiple compiler invocations would reset the file to zero length.
Opening in append mode works better; the automated harness
(github.com/dr2chase/gossahash) takes care of truncating the file before use.

Change-Id: I5601bc280faa94cbd507d302448831849db6c842
Reviewed-on: https://go-review.googlesource.com/c/go/+/246937
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
diff --git a/src/cmd/compile/internal/gc/ssa.go b/src/cmd/compile/internal/gc/ssa.go
index 956569b..c8fb013 100644
--- a/src/cmd/compile/internal/gc/ssa.go
+++ b/src/cmd/compile/internal/gc/ssa.go
@@ -329,8 +329,8 @@
 	s.f.Config = ssaConfig
 	s.f.Cache = &ssaCaches[worker]
 	s.f.Cache.Reset()
-	s.f.DebugTest = s.f.DebugHashMatch("GOSSAHASH", name)
 	s.f.Name = name
+	s.f.DebugTest = s.f.DebugHashMatch("GOSSAHASH")
 	s.f.PrintOrHtmlSSA = printssa
 	if fn.Func.Pragma&Nosplit != 0 {
 		s.f.NoSplit = true
@@ -6863,6 +6863,10 @@
 	e.curfn.Func.setWBPos(pos)
 }
 
+func (e *ssafn) MyImportPath() string {
+	return myimportpath
+}
+
 func (n *Node) Typ() *types.Type {
 	return n.Type
 }
diff --git a/src/cmd/compile/internal/ssa/config.go b/src/cmd/compile/internal/ssa/config.go
index fdff3bb..4b2f06d 100644
--- a/src/cmd/compile/internal/ssa/config.go
+++ b/src/cmd/compile/internal/ssa/config.go
@@ -173,6 +173,9 @@
 	// SetWBPos indicates that a write barrier has been inserted
 	// in this function at position pos.
 	SetWBPos(pos src.XPos)
+
+	// MyImportPath provides the import name (roughly, the package) for the function being compiled.
+	MyImportPath() string
 }
 
 // interface used to hold a *gc.Node (a stack variable).
diff --git a/src/cmd/compile/internal/ssa/export_test.go b/src/cmd/compile/internal/ssa/export_test.go
index a94cce4..51665c6 100644
--- a/src/cmd/compile/internal/ssa/export_test.go
+++ b/src/cmd/compile/internal/ssa/export_test.go
@@ -146,6 +146,10 @@
 func (d DummyFrontend) Warnl(_ src.XPos, msg string, args ...interface{})  { d.t.Logf(msg, args...) }
 func (d DummyFrontend) Debug_checknil() bool                               { return false }
 
+func (d DummyFrontend) MyImportPath() string {
+	return "my/import/path"
+}
+
 var dummyTypes Types
 
 func init() {
diff --git a/src/cmd/compile/internal/ssa/func.go b/src/cmd/compile/internal/ssa/func.go
index 9e40b62..6718b77 100644
--- a/src/cmd/compile/internal/ssa/func.go
+++ b/src/cmd/compile/internal/ssa/func.go
@@ -678,7 +678,8 @@
 //  GSHS_LOGFILE
 // or standard out if that is empty or there is an error
 // opening the file.
-func (f *Func) DebugHashMatch(evname, name string) bool {
+func (f *Func) DebugHashMatch(evname string) bool {
+	name := f.fe.MyImportPath() + "." + f.Name
 	evhash := os.Getenv(evname)
 	switch evhash {
 	case "":
@@ -727,7 +728,7 @@
 		file = os.Stdout
 		if tmpfile := os.Getenv("GSHS_LOGFILE"); tmpfile != "" {
 			var err error
-			file, err = os.Create(tmpfile)
+			file, err = os.OpenFile(tmpfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
 			if err != nil {
 				f.Fatalf("could not open hash-testing logfile %s", tmpfile)
 			}