github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/common/debug/debug.go (about) 1 package debug 2 3 import ( 4 "fmt" 5 "runtime" 6 ) 7 8 // Callers returns given number of callers with packages 9 func Callers(show int) []string { 10 fpcs := make([]uintptr, show) 11 12 n := runtime.Callers(2, fpcs) 13 if n == 0 { 14 return nil 15 } 16 17 callers := make([]string, 0, len(fpcs)) 18 19 for _, p := range fpcs { 20 caller := runtime.FuncForPC(p - 1) 21 if caller == nil { 22 continue 23 } 24 25 callers = append(callers, caller.Name()) 26 } 27 28 return callers 29 } 30 31 func CodeLine() (string, string, int) { 32 pc, filename, line, _ := runtime.Caller(1) 33 return runtime.FuncForPC(pc).Name(), filename, line 34 } 35 36 func CodeLineStr() string { 37 pc, filename, line, _ := runtime.Caller(1) 38 return fmt.Sprintf("%s:%d - %s", filename, line, runtime.FuncForPC(pc).Name()) 39 } 40 41 func Stack(all bool) []byte { 42 buf := make([]byte, 4096) 43 44 for { 45 n := runtime.Stack(buf, all) 46 if n < len(buf) { 47 return buf[:n] 48 } 49 50 buf = make([]byte, 2*len(buf)) 51 } 52 }