brahma-adshonor / gohook

a funny library to hook golang function at runtime

Home Page:https://www.cnblogs.com/catch/p/10973611.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

某些函数无法Hook

nonths opened this issue · comments

commented

如下代码,MyFunc里面实现比较简单,就是返回一个变量,Hook会失败;如果把函数的实现改为从环境变量获取一个值来决定返回值,就可以Hook成功了

`
package main

import (
"fmt"
"github.com/brahma-adshonor/gohook"
"os"
)

var (
isTrue bool
)

func MyFunc() bool {
fmt.Println("MyFunc enter")
return isTrue
}

func MyFunc1() bool {
fmt.Println("MyFunc1 enter")
value := os.Getenv("key")
return value != ""
}

func MyFuncHook() bool {
fmt.Println("MyFuncHook enter")
return true
}

func main() {
MyFunc()
MyFunc1()
gohook.Hook(MyFunc, MyFuncHook, nil)
gohook.Hook(MyFunc1, MyFuncHook, nil)
MyFunc()
MyFunc1()
}
`

应该是 inline 了,编译时加上 -gcflags=-l