2017-01-22 18:10:32 +00:00
|
|
|
// +build debug
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
|
|
|
"os"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-01-23 16:52:26 +00:00
|
|
|
|
|
|
|
"github.com/pkg/profile"
|
2017-01-22 18:10:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
listenMemoryProfile string
|
2017-01-23 16:52:26 +00:00
|
|
|
memProfilePath string
|
|
|
|
cpuProfilePath string
|
2017-03-25 17:00:42 +00:00
|
|
|
insecure bool
|
2017-01-23 16:52:26 +00:00
|
|
|
|
|
|
|
prof interface {
|
|
|
|
Stop()
|
|
|
|
}
|
2017-01-22 18:10:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
f := cmdRoot.PersistentFlags()
|
|
|
|
f.StringVar(&listenMemoryProfile, "listen-profile", "", "listen on this `address:port` for memory profiling")
|
2017-01-23 16:52:26 +00:00
|
|
|
f.StringVar(&memProfilePath, "mem-profile", "", "write memory profile to `dir`")
|
|
|
|
f.StringVar(&cpuProfilePath, "cpu-profile", "", "write cpu profile to `dir`")
|
2017-03-25 17:00:42 +00:00
|
|
|
f.BoolVar(&insecure, "insecure-kdf", false, "use insecure KDF settings")
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeTestingTB struct{}
|
|
|
|
|
|
|
|
func (fakeTestingTB) Logf(msg string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, msg, args...)
|
2017-01-22 18:10:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 16:52:26 +00:00
|
|
|
func runDebug() error {
|
2017-01-22 18:10:32 +00:00
|
|
|
if listenMemoryProfile != "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "running memory profile HTTP server on %v\n", listenMemoryProfile)
|
|
|
|
go func() {
|
|
|
|
err := http.ListenAndServe(listenMemoryProfile, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "memory profile listen failed: %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-01-23 16:52:26 +00:00
|
|
|
|
|
|
|
if memProfilePath != "" && cpuProfilePath != "" {
|
|
|
|
return errors.Fatal("only one profile (memory or CPU) may be activated at the same time")
|
|
|
|
}
|
|
|
|
|
|
|
|
if memProfilePath != "" {
|
|
|
|
prof = profile.Start(profile.Quiet, profile.MemProfile, profile.ProfilePath(memProfilePath))
|
2017-05-17 15:07:33 +00:00
|
|
|
} else if cpuProfilePath != "" {
|
|
|
|
prof = profile.Start(profile.Quiet, profile.CPUProfile, profile.ProfilePath(cpuProfilePath))
|
2017-01-23 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 17:00:42 +00:00
|
|
|
if insecure {
|
|
|
|
repository.TestUseLowSecurityKDFParameters(fakeTestingTB{})
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:52:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func shutdownDebug() {
|
|
|
|
if prof != nil {
|
|
|
|
prof.Stop()
|
|
|
|
}
|
2017-01-22 18:10:32 +00:00
|
|
|
}
|