1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

Output panic function and line in recover func

This commit is contained in:
Timothy 2016-06-24 18:53:57 +08:00
parent 033ba4a4af
commit c6d54329c2

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"runtime"
"runtime/debug" "runtime/debug"
"strings" "strings"
"time" "time"
@ -23,10 +24,39 @@ var (
panicCount = 0 panicCount = 0
) )
func identifyPanic() string {
var name, file string
var line int
var pc [16]uintptr
n := runtime.Callers(3, pc[:])
for _, pc := range pc[:n] {
fn := runtime.FuncForPC(pc)
if fn == nil {
continue
}
file, line = fn.FileLine(pc)
name = fn.Name()
if !strings.HasPrefix(name, "runtime.") {
break
}
}
switch {
case name != "":
return fmt.Sprintf("%v:%v", name, line)
case file != "":
return fmt.Sprintf("%v:%v", file, line)
}
return fmt.Sprintf("pc:%x", pc)
}
func usage() { func usage() {
log.Println("[command] -c=[config file path]") log.Println("[command] -c=[config file path]")
flag.PrintDefaults() flag.PrintDefaults()
} }
func main() { func main() {
flag.Parse() flag.Parse()
if *optHelp { if *optHelp {
@ -57,6 +87,7 @@ func dnsLoop() {
if err := recover(); err != nil { if err := recover(); err != nil {
panicCount++ panicCount++
log.Printf("Recovered in %v: %v\n", err, debug.Stack()) log.Printf("Recovered in %v: %v\n", err, debug.Stack())
fmt.Println(identifyPanic())
if panicCount < PANIC_MAX { if panicCount < PANIC_MAX {
log.Println("Got panic in goroutine, will start a new one... :", panicCount) log.Println("Got panic in goroutine, will start a new one... :", panicCount)
go dnsLoop() go dnsLoop()