github.com/Rookout/GoSDK@v0.1.48/pkg/utils/runtime_utils.go (about) 1 package utils 2 3 import ( 4 "github.com/Rookout/GoSDK/pkg/rookoutErrors" 5 "runtime" 6 "strings" 7 ) 8 9 func GetFunctionName(fullFunctionName string) (string, rookoutErrors.RookoutError) { 10 sliced := strings.Split(fullFunctionName, ".") 11 if 0 == len(sliced) { 12 return "", rookoutErrors.NewBadFunctionNameException(fullFunctionName) 13 } 14 return sliced[len(sliced)-1], nil 15 } 16 17 type FrameEntry struct { 18 Filename string 19 Line int 20 Function string 21 } 22 23 func GetStackTrace(maxDepth int, skip int) ([]FrameEntry, rookoutErrors.RookoutError) { 24 pc := make([]uintptr, maxDepth) 25 actualStackFrames := runtime.Callers(skip, pc) 26 if actualStackFrames == 0 { 27 return nil, rookoutErrors.NewFailedToRetrieveStackTrace() 28 } 29 30 pc = pc[:actualStackFrames] 31 frames := runtime.CallersFrames(pc) 32 33 stackTrace := make([]FrameEntry, actualStackFrames) 34 35 i := 0 36 for frame, more := frames.Next(); (more == true) && (i < actualStackFrames); frame, more = frames.Next() { 37 38 functionName, err := GetFunctionName(frame.Function) 39 if nil != err { 40 return nil, err 41 } 42 43 stackTrace[i] = FrameEntry{ 44 Filename: frame.File, 45 Line: frame.Line, 46 Function: functionName, 47 } 48 i++ 49 } 50 51 return stackTrace, nil 52 } 53 54 type Frame struct { 55 stackTrace []FrameEntry 56 locals *map[string]interface{} 57 } 58 59 func NewFrame(stackTraceInit []FrameEntry, localsInit *map[string]interface{}) *Frame { 60 if nil == localsInit { 61 localsInit = &map[string]interface{}{} 62 } 63 64 return &Frame{ 65 stackTrace: stackTraceInit, 66 locals: localsInit} 67 } 68 69 func (f Frame) GetStackTrace() []FrameEntry { 70 return f.stackTrace 71 } 72 73 func (f Frame) GetLocals() *map[string]interface{} { 74 return f.locals 75 }