Created
March 18, 2014 20:45
-
-
Save swdunlop/9629168 to your computer and use it in GitHub Desktop.
Identify a GOLANG Panic Function and Line in Recovery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can edit this code! | |
// Click here and start typing. | |
package main | |
import "fmt" | |
import "runtime" | |
import "strings" | |
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 recoverPanic() { | |
r := recover() | |
if r == nil { | |
return | |
} | |
fmt.Println(identifyPanic()) | |
} | |
func createPanic() { | |
var s *string | |
fmt.Println(*s) | |
} | |
func main() { | |
defer recoverPanic() | |
createPanic() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try it out on http://play.golang.org/p/jxtnL79k_Z