mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
37 lines
699 B
Go
37 lines
699 B
Go
package runtimex
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// 获取指定堆栈描述信息
|
|
//
|
|
// @param skip: 跳过堆栈个数
|
|
// @param nFrames: 需要描述的堆栈个数
|
|
func StatckStr(skip, nFrames int) string {
|
|
pcs := make([]uintptr, nFrames+1)
|
|
n := runtime.Callers(skip+1, pcs)
|
|
if n == 0 {
|
|
return "(no stack)"
|
|
}
|
|
frames := runtime.CallersFrames(pcs[:n])
|
|
var b strings.Builder
|
|
i := 0
|
|
for {
|
|
frame, more := frames.Next()
|
|
fmt.Fprintf(&b, "called from %s (%s:%d)\n\t", frame.Function, filepath.Base(frame.File), frame.Line)
|
|
if !more {
|
|
break
|
|
}
|
|
i++
|
|
if i >= nFrames {
|
|
fmt.Fprintf(&b, "(rest of stack elided...)\n")
|
|
break
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|