in tools/go-agent/instrument/agentcore/instrument.go [175:240]
func (i *Instrument) writeLinkerFile(dir string) (string, error) {
return tools.WriteFile(dir, "runtime_linker.go", tools.ExecuteTemplate(`package core
import (
_ "unsafe"
)
//go:linkname {{.TLSGetLinkMethod}} {{.TLSGetLinkMethod}}
var {{.TLSGetLinkMethod}} func() interface{}
//go:linkname {{.TLSSetLinkMethod}} {{.TLSSetLinkMethod}}
var {{.TLSSetLinkMethod}} func(interface{})
//go:linkname {{.SetGlobalOperatorLinkMethod}} {{.SetGlobalOperatorLinkMethod}}
var {{.SetGlobalOperatorLinkMethod}} func(interface{})
//go:linkname {{.GetGlobalOperatorLinkMethod}} {{.GetGlobalOperatorLinkMethod}}
var {{.GetGlobalOperatorLinkMethod}} func() interface{}
//go:linkname {{.GetGoroutineIDLinkMethod}} {{.GetGoroutineIDLinkMethod}}
var {{.GetGoroutineIDLinkMethod}} func() int64
//go:linkname {{.GetInitNotifyLinkMethod}} {{.GetInitNotifyLinkMethod}}
var {{.GetInitNotifyLinkMethod}} func() []func()
//go:linkname {{.MetricsObtainMethodName}} {{.MetricsObtainMethodName}}
var {{.MetricsObtainMethodName}} func() ([]interface{}, []func())
func init() {
if {{.TLSGetLinkMethod}} != nil && {{.TLSSetLinkMethod}} != nil {
GetGLS = {{.TLSGetLinkMethod}}
SetGLS = {{.TLSSetLinkMethod}}
}
if {{.GetGoroutineIDLinkMethod}} != nil {
GetGoID = {{.GetGoroutineIDLinkMethod}}
}
if {{.SetGlobalOperatorLinkMethod}} != nil && {{.GetGlobalOperatorLinkMethod}} != nil {
SetGlobalOperator = {{.SetGlobalOperatorLinkMethod}}
GetGlobalOperator = {{.GetGlobalOperatorLinkMethod}}
SetGlobalOperator(newTracer()) // setting the global tracer when init the agent core
}
if {{.GetInitNotifyLinkMethod}} != nil {
GetInitNotify = {{.GetInitNotifyLinkMethod}}
}
if {{.MetricsObtainMethodName}} != nil {
MetricsObtain = {{.MetricsObtainMethodName}}
}
}
`, struct {
TLSGetLinkMethod string
TLSSetLinkMethod string
SetGlobalOperatorLinkMethod string
GetGlobalOperatorLinkMethod string
GetGoroutineIDLinkMethod string
GetInitNotifyLinkMethod string
MetricsObtainMethodName string
}{
TLSGetLinkMethod: consts.TLSGetMethodName,
TLSSetLinkMethod: consts.TLSSetMethodName,
SetGlobalOperatorLinkMethod: consts.GlobalTracerSetMethodName,
GetGlobalOperatorLinkMethod: consts.GlobalTracerGetMethodName,
GetGoroutineIDLinkMethod: consts.CurrentGoroutineIDGetMethodName,
GetInitNotifyLinkMethod: consts.GlobalTracerInitGetNotifyMethodName,
MetricsObtainMethodName: consts.MetricsObtainMethodName,
}))
}