zen/core/zcaller/zcaller.go

41 lines
926 B
Go
Raw Normal View History

2024-08-23 12:29:47 +07:00
package zcaller
import (
"runtime"
)
// Current returns the ProgramCounter of the caller.
//
// It gives information about the function that called
// this.
//
// Returns 0 if caller information is not available.
func Current() uintptr {
return Get(3)
}
// Get works just like Current, but it allows you to
// specify the number of frames to skip.
//
// For reference, current is equivalent to Get(3).
// (use Get(2) because Current wraps this function and has to add +1 to the skip value)
//
// Returns 0 if caller information is not available.
func Get(skip int) uintptr {
pcs := make([]uintptr, 1)
n := runtime.Callers(skip, pcs)
if n == 0 {
return 0
}
return pcs[0]
}
// Frame creates a runtime.Frame from a ProgramCounter.
//
// Return zero value if the frame is not available.
func Frame(pc uintptr) runtime.Frame {
frames := runtime.CallersFrames([]uintptr{pc})
frame, _ := frames.Next()
return frame
}