in tool/instrument/trampoline.go [278:321]
func (rp *RuleProcessor) addHookFuncVar(t *resource.InstFuncRule,
traits []ParamTrait, onEnter bool) error {
paramTypes := &dst.FieldList{List: []*dst.Field{}}
if rp.exact {
paramTypes = rp.buildTrampolineType(onEnter)
}
addCallContext(paramTypes)
if rp.exact {
// Hook functions may uses interface{} as parameter type, as some types of
// raw function is not exposed
err := rectifyAnyType(paramTypes, traits)
if err != nil {
return err
}
}
// Generate var decl and append it to the target file, note that many target
// functions may match the same hook function, it's a fatal error to append
// multiple hook function declarations to the same file, so we need to check
// if the hook function variable is already declared in the target file
exist := false
fnName := makeOnXName(t, onEnter)
funcDecl := &dst.FuncDecl{
Name: &dst.Ident{
Name: fnName,
},
Type: &dst.FuncType{
Func: false,
Params: paramTypes,
},
}
for _, decl := range rp.target.Decls {
if fDecl, ok := decl.(*dst.FuncDecl); ok {
if fDecl.Name.Name == fnName {
exist = true
break
}
}
}
if !exist {
rp.addDecl(funcDecl)
}
return nil
}