2013-09-12 16:51:09 +00:00
|
|
|
/*
|
2018-01-04 00:52:49 +00:00
|
|
|
Copyright 2013 The Perkeep Authors
|
2013-09-12 16:51:09 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-01-02 04:48:34 +00:00
|
|
|
package leak // import "perkeep.org/internal/leak"
|
2013-09-12 16:51:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Checker checks for leaks.
|
|
|
|
type Checker struct {
|
|
|
|
pc []uintptr // nil once closed
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChecker returns a Checker, remembering the stack trace.
|
|
|
|
func NewChecker() *Checker {
|
|
|
|
pc := make([]uintptr, 50)
|
|
|
|
ch := &Checker{pc[:runtime.Callers(0, pc)]}
|
|
|
|
runtime.SetFinalizer(ch, (*Checker).finalize)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Checker) Close() {
|
|
|
|
if c != nil {
|
|
|
|
c.pc = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Checker) finalize() {
|
2014-02-26 00:06:50 +00:00
|
|
|
if testHookFinalize != nil {
|
|
|
|
defer testHookFinalize()
|
|
|
|
}
|
2013-09-12 16:51:09 +00:00
|
|
|
if c == nil || c.pc == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("Leak at:\n")
|
|
|
|
for _, pc := range c.pc {
|
|
|
|
f := runtime.FuncForPC(pc)
|
|
|
|
if f == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
file, line := f.FileLine(f.Entry())
|
|
|
|
fmt.Fprintf(&buf, " %s:%d\n", file, line)
|
|
|
|
}
|
2014-02-26 00:06:50 +00:00
|
|
|
onLeak(c, buf.String())
|
2013-09-12 16:51:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 00:06:50 +00:00
|
|
|
// testHookFinalize optionally specifies a function to run after
|
|
|
|
// finalization. For tests.
|
|
|
|
var testHookFinalize func()
|
|
|
|
|
|
|
|
// onLeak is changed by tests.
|
|
|
|
var onLeak = logLeak
|
|
|
|
|
|
|
|
func logLeak(c *Checker, stack string) {
|
|
|
|
log.Println(stack)
|
|
|
|
}
|