github.com/xg0n/routine@v0.0.0-20240119033701-c364deb94aee/stack.go (about)

     1  package routine
     2  
     3  import (
     4  	"runtime"
     5  	"strings"
     6  )
     7  
     8  const (
     9  	runtimePkgPrefix = "runtime."
    10  	runtimePanic     = "panic"
    11  )
    12  
    13  func captureStackTrace(skip int, depth int) []uintptr {
    14  	pcs := make([]uintptr, depth)
    15  	return pcs[:runtime.Callers(skip+2, pcs)]
    16  }
    17  
    18  func showFrame(name string) bool {
    19  	return strings.IndexByte(name, '.') >= 0 && (!strings.HasPrefix(name, runtimePkgPrefix) || isExportedRuntime(name))
    20  }
    21  
    22  func skipFrame(name string, skipped bool) bool {
    23  	return !skipped && isPanicRuntime(name)
    24  }
    25  
    26  func isExportedRuntime(name string) bool {
    27  	const n = len(runtimePkgPrefix)
    28  	return len(name) > n && name[:n] == runtimePkgPrefix && 'A' <= name[n] && name[n] <= 'Z'
    29  }
    30  
    31  func isPanicRuntime(name string) bool {
    32  	const n = len(runtimePkgPrefix)
    33  	return len(name) > n && name[:n] == runtimePkgPrefix && strings.Contains(strings.ToLower(name[n:]), runtimePanic)
    34  }