Fetch the repository succeeded.
package trace
import (
"runtime"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
"time"
)
const (
wrapPackage = `; import . "gitee.com/qyang_1/g0/trace";`
wrapDeferFuncName = ` defer AstTrace()();`
)
var (
version = time.Now().UnixNano()
idx = atomic.Int64{}
stack = make(map[string][]*node)
lock = sync.Mutex{}
)
func AstTrace() func() {
pc, file, line, ok := runtime.Caller(1)
if !ok {
return func() {}
}
funcName := runtime.FuncForPC(pc).Name()
goid, pGoid := getGoroutineInfo()
n := &node{
Id: idx.Add(1),
Version: version,
Goid: goid,
PGoid: pGoid,
FuncName: funcName,
File: file,
Line: line,
StartTime: time.Now(),
Runed: true,
Running: true,
}
enter(n)
return func() {
defer func() {
if r := recover(); r != nil {
n.IsCrash = true
n.Err = r
n.Stack = string(debug.Stack())
}
exit(n)
}()
n.EndTime = time.Now()
}
}
func getGoroutineInfo() (currentID, parentID string) {
stack := string(debug.Stack())
idx := "goroutine 444444 [running]:"
id0 := stack[:len(idx)]
currentID = strings.Split(id0, " ")[1]
id1 := strings.Split(stack, "in goroutine ")
if len(id1) < 2 {
parentID = "root"
return
}
parentID = strings.Split(id1[1], " ")[0]
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。