mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
75
vendor/github.com/tsaikd/KDGoLib/runtimecaller/callinfo.go
generated
vendored
Normal file
75
vendor/github.com/tsaikd/KDGoLib/runtimecaller/callinfo.go
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
package runtimecaller
|
||||
|
||||
import "runtime"
|
||||
|
||||
// CallInfo contains runtime caller information
|
||||
type CallInfo interface {
|
||||
// builtin data
|
||||
PC() uintptr
|
||||
FilePath() string
|
||||
Line() int
|
||||
|
||||
// extra info after some process
|
||||
PCFunc() *runtime.Func
|
||||
PackageName() string
|
||||
FileDir() string
|
||||
FileName() string
|
||||
FuncName() string
|
||||
}
|
||||
|
||||
// CallInfoImpl implement CallInfo
|
||||
type CallInfoImpl struct {
|
||||
// builtin data
|
||||
pc uintptr
|
||||
filePath string
|
||||
line int
|
||||
|
||||
// extra info after some process
|
||||
pcFunc *runtime.Func
|
||||
packageName string
|
||||
fileDir string
|
||||
fileName string
|
||||
funcName string
|
||||
}
|
||||
|
||||
// PC return CallInfo data
|
||||
func (t CallInfoImpl) PC() uintptr {
|
||||
return t.pc
|
||||
}
|
||||
|
||||
// FilePath return CallInfo data
|
||||
func (t CallInfoImpl) FilePath() string {
|
||||
return t.filePath
|
||||
}
|
||||
|
||||
// Line return CallInfo data
|
||||
func (t CallInfoImpl) Line() int {
|
||||
return t.line
|
||||
}
|
||||
|
||||
// PCFunc return CallInfo data
|
||||
func (t CallInfoImpl) PCFunc() *runtime.Func {
|
||||
return t.pcFunc
|
||||
}
|
||||
|
||||
// PackageName return CallInfo data
|
||||
func (t CallInfoImpl) PackageName() string {
|
||||
return t.packageName
|
||||
}
|
||||
|
||||
// FileDir return CallInfo data
|
||||
func (t CallInfoImpl) FileDir() string {
|
||||
return t.fileDir
|
||||
}
|
||||
|
||||
// FileName return CallInfo data
|
||||
func (t CallInfoImpl) FileName() string {
|
||||
return t.fileName
|
||||
}
|
||||
|
||||
// FuncName return CallInfo data
|
||||
func (t CallInfoImpl) FuncName() string {
|
||||
return t.funcName
|
||||
}
|
||||
|
||||
var _ = CallInfo(CallInfoImpl{})
|
||||
26
vendor/github.com/tsaikd/KDGoLib/runtimecaller/filter.go
generated
vendored
Normal file
26
vendor/github.com/tsaikd/KDGoLib/runtimecaller/filter.go
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
package runtimecaller
|
||||
|
||||
import "strings"
|
||||
|
||||
// Filter use to filter runtime.Caller result
|
||||
type Filter func(callinfo CallInfo) (valid bool, stop bool)
|
||||
|
||||
// FilterCommons contains all common filters
|
||||
var FilterCommons = []Filter{
|
||||
FilterOnlyGoSource,
|
||||
FilterStopRuntimeCallerPackage,
|
||||
}
|
||||
|
||||
// FilterOnlyGoSource filter CallInfo FileName end with ".go"
|
||||
func FilterOnlyGoSource(callinfo CallInfo) (valid bool, stop bool) {
|
||||
filename := strings.ToLower(callinfo.FileName())
|
||||
return strings.HasSuffix(filename, ".go"), false
|
||||
}
|
||||
|
||||
// FilterStopRuntimeCallerPackage filter CallInfo to stop after reach KDGoLib/runtimecaller package
|
||||
func FilterStopRuntimeCallerPackage(callinfo CallInfo) (valid bool, stop bool) {
|
||||
if callinfo.PackageName() == "github.com/tsaikd/KDGoLib/runtimecaller" {
|
||||
return false, true
|
||||
}
|
||||
return true, false
|
||||
}
|
||||
92
vendor/github.com/tsaikd/KDGoLib/runtimecaller/runtimeCaller.go
generated
vendored
Normal file
92
vendor/github.com/tsaikd/KDGoLib/runtimecaller/runtimeCaller.go
generated
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
package runtimecaller
|
||||
|
||||
import (
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetByFilters return CallInfo until all filters are valid
|
||||
func GetByFilters(skip int, filters ...Filter) (callinfo CallInfo, ok bool) {
|
||||
filters = append(FilterCommons, filters...)
|
||||
for {
|
||||
skip++
|
||||
|
||||
if callinfo, ok = retrieveCallInfo(skip); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
valid, stop := filterAll(callinfo, filters...)
|
||||
if valid {
|
||||
return callinfo, true
|
||||
}
|
||||
if stop {
|
||||
return callinfo, false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ListByFilters return all CallInfo stack for all filters are valid
|
||||
func ListByFilters(skip int, filters ...Filter) (callinfos []CallInfo) {
|
||||
filters = append(FilterCommons, filters...)
|
||||
for {
|
||||
var callinfo CallInfo
|
||||
var ok bool
|
||||
skip++
|
||||
|
||||
if callinfo, ok = retrieveCallInfo(skip); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
valid, stop := filterAll(callinfo, filters...)
|
||||
if valid {
|
||||
callinfos = append(callinfos, callinfo)
|
||||
}
|
||||
if stop {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// http://stackoverflow.com/questions/25262754/how-to-get-name-of-current-package-in-go
|
||||
func retrieveCallInfo(skip int) (result CallInfo, ok bool) {
|
||||
callinfo := CallInfoImpl{}
|
||||
|
||||
if callinfo.pc, callinfo.filePath, callinfo.line, ok = runtime.Caller(skip + 1); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
callinfo.fileDir, callinfo.fileName = path.Split(callinfo.filePath)
|
||||
callinfo.pcFunc = runtime.FuncForPC(callinfo.pc)
|
||||
|
||||
parts := strings.Split(callinfo.pcFunc.Name(), ".")
|
||||
pl := len(parts)
|
||||
if pl < 1 {
|
||||
return result, false
|
||||
}
|
||||
callinfo.funcName = parts[pl-1]
|
||||
|
||||
if pl >= 2 && parts[pl-2] != "" && parts[pl-2][0] == '(' {
|
||||
callinfo.funcName = parts[pl-2] + "." + callinfo.funcName
|
||||
callinfo.packageName = strings.Join(parts[0:pl-2], ".")
|
||||
} else {
|
||||
callinfo.packageName = strings.Join(parts[0:pl-1], ".")
|
||||
}
|
||||
|
||||
return callinfo, true
|
||||
}
|
||||
|
||||
func filterAll(callinfo CallInfo, filters ...Filter) (allvalid bool, onestop bool) {
|
||||
allvalid = true
|
||||
for _, filter := range filters {
|
||||
valid, stop := filter(callinfo)
|
||||
allvalid = allvalid && valid
|
||||
if stop {
|
||||
return allvalid, true
|
||||
}
|
||||
if !allvalid {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user