2014-11-16 14:30:08 +00:00
|
|
|
// +build debug
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
package restic
|
2014-11-16 14:30:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2015-01-11 13:09:44 +00:00
|
|
|
"path"
|
2014-11-16 14:30:08 +00:00
|
|
|
"path/filepath"
|
2015-01-11 13:09:44 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2014-11-16 14:30:08 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var debugLogger = initDebugLogger()
|
2015-01-11 13:09:44 +00:00
|
|
|
var debugTags = make(map[string]bool)
|
|
|
|
var debugBreak = make(map[string]bool)
|
2014-11-16 14:30:08 +00:00
|
|
|
|
|
|
|
func initDebugLogger() *log.Logger {
|
|
|
|
// create new log file
|
2014-12-05 20:45:49 +00:00
|
|
|
filename := fmt.Sprintf("restic-lib-debug-%d-%s",
|
2014-11-16 14:30:08 +00:00
|
|
|
os.Getpid(), time.Now().Format("20060201-150405"))
|
2015-01-11 13:09:44 +00:00
|
|
|
debugfile := filepath.Join(os.TempDir(), filename)
|
|
|
|
f, err := os.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE, 0600)
|
2014-11-16 14:30:08 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "unable to create debug log file: %v", err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// open logger
|
2015-01-11 13:09:44 +00:00
|
|
|
l := log.New(f, "DEBUG: ", log.LstdFlags)
|
|
|
|
fmt.Fprintf(os.Stderr, "debug log for restic library activated, writing log file %s\n", debugfile)
|
|
|
|
|
|
|
|
// defaults
|
|
|
|
debugTags["break"] = true
|
|
|
|
|
|
|
|
// initialize tags
|
|
|
|
env := os.Getenv("DEBUG_TAGS")
|
|
|
|
if len(env) > 0 {
|
|
|
|
tags := []string{}
|
|
|
|
|
|
|
|
for _, tag := range strings.Split(env, ",") {
|
|
|
|
t := strings.TrimSpace(tag)
|
|
|
|
val := true
|
|
|
|
if t[0] == '-' {
|
|
|
|
val = false
|
|
|
|
t = t[1:]
|
|
|
|
} else if t[0] == '+' {
|
|
|
|
val = true
|
|
|
|
t = t[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// test pattern
|
|
|
|
_, err := path.Match(t, "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: invalid pattern %q: %v\n", t, err)
|
|
|
|
os.Exit(5)
|
|
|
|
}
|
|
|
|
|
|
|
|
debugTags[t] = val
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "debug log enabled for: %v\n", tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize break tags
|
|
|
|
env = os.Getenv("DEBUG_BREAK")
|
|
|
|
if len(env) > 0 {
|
|
|
|
breaks := []string{}
|
|
|
|
|
|
|
|
for _, tag := range strings.Split(env, ",") {
|
|
|
|
t := strings.TrimSpace(tag)
|
|
|
|
debugBreak[t] = true
|
|
|
|
breaks = append(breaks, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "debug breaks enabled for: %v\n", breaks)
|
|
|
|
}
|
2014-11-16 14:30:08 +00:00
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:09:44 +00:00
|
|
|
func debug(tag string, f string, args ...interface{}) {
|
|
|
|
dbgprint := func() {
|
|
|
|
fmt.Fprintf(os.Stderr, tag+": "+f, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
debugLogger.Printf(f, args...)
|
|
|
|
|
|
|
|
// check if tag is enabled directly
|
|
|
|
if v, ok := debugTags[tag]; ok {
|
|
|
|
if v {
|
|
|
|
dbgprint()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for globbing
|
|
|
|
for k, v := range debugTags {
|
|
|
|
if m, _ := path.Match(k, tag); m {
|
|
|
|
if v {
|
|
|
|
dbgprint()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if tag "all" is enabled
|
|
|
|
if v, ok := debugTags["all"]; ok && v {
|
|
|
|
dbgprint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func debug_break(tag string) {
|
|
|
|
// check if breaking is enabled
|
|
|
|
if v, ok := debugBreak[tag]; !ok || !v {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
debug("break", "stopping process %d at %s (%v:%v)\n", os.Getpid(), tag, file, line)
|
|
|
|
p, err := os.FindProcess(os.Getpid())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.Signal(syscall.SIGSTOP)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-11-16 14:30:08 +00:00
|
|
|
}
|