2023年11月29日发(作者:)
如何在Go的函数中得到调⽤者函数名?
有时候在Go的函数调⽤的过程中,我们需要知道函数被谁调⽤,⽐如打印⽇志信息等。例如下⾯的函数,我们希望在⽇志中打印出调⽤者
的名字。
1func Foo(){
2fn("谁在调⽤我?")
3bar()
4}
5func Bar(){
6fn("谁⼜在调⽤我?")
7}
⾸先打印函数本⾝的名称
最简单的⽅式就是硬编码。 因为在编译之前,我们肯定知道打印的时候所在哪个函数,但是更好的⽅式是编写⼀个通⽤的函数,⽐如下⾯
的例⼦:
1packagemain
2import(
3 "fmt"
4 "runtime"
5)
6func main(){
7Foo()
8}
9func Foo(){
10f("我是 %s, 谁在调⽤我?n", printMyName())
11Bar()
12}
13func Bar(){
14f("我是 %s, 谁⼜在调⽤我?n", printMyName())
15}
16func printMyName() string{
17pc, _, _, _ := r(1)
18 returnrrPC(pc).Name()
19}
输出结果:
1我是 main.Foo, 谁在调⽤我?
2我是 main.Bar, 谁⼜在调⽤我?
可以看到函数在被调⽤的时候,printMyName把函数本⾝的名字打印出来了,注意这⾥Caller的参数是1, 因为我们将业务代码封装成了⼀个
函数。
⾸先打印函数调⽤者的名称
将上⾯的代码修改⼀下,增加⼀个新的printCallerName的函数,可以打印调⽤者的名称。
1func main(){
2Foo()
3}
4func Foo(){
5f("我是 %s, %s 在调⽤我!n", printMyName(), printCallerName())
6Bar()
7}
8func Bar(){
9f("我是 %s, %s ⼜在调⽤我!n", printMyName(), printCallerName())
10}
11func printMyName() string{
12pc, _, _, _ := r(1)
13 returnrrPC(pc).Name()
14}
15func printCallerName() string{
16pc, _, _, _ := r(2)
17 returnrrPC(pc).Name()
18}
相关函数介绍
你可以通过、s、rPC等函数更详细的跟踪函数的调⽤堆栈。
func Caller(skip int) (pc uintptr, file string, line int, ok bool)
Caller可以返回函数调⽤栈的某⼀层的程序计数器、⽂件信息、⾏号。
0 代表当前函数,也是调⽤的函数。1 代表上⼀层调⽤者,以此类推。
func Callers(skip int, pc []uintptr) int
Callers⽤来返回调⽤站的程序计数器, 放到⼀个uintptr中。
0 代表 Callers 本⾝,这和上⾯的Caller的参数的意义不⼀样,历史原因造成的。 1 才对应这上⾯的 0。
⽐如在上⾯的例⼦中增加⼀个trace函数,被函数Bar调⽤。
1……
2func Bar(){
3f("我是 %s, %s ⼜在调⽤我!n", printMyName(), printCallerName())
4trace()
5}
6func trace(){
7pc := make([]uintptr, 10) // at least 1 entry needed
8n := rs(0, pc)
9 fori := 0; i < n; i++ {
10f := rrPC(pc[i])
11file, line := f.FileLine(pc[i])
12f("%s:%d %sn", file, line, f.Name())
13}
14}
输出结果可以看到这个goroutine的整个栈都打印出来了:
1/usr/local/go/src/runtime/extern.go:218rs
2/Users/yuepan/go/src//platform/tool/g/main.go:34m
3/Users/yuepan/go/src//platform/tool/g/main.go:20m
4/Users/yuepan/go/src//platform/tool/g/main.go:15m
5/Users/yuepan/go/src//platform/tool/g/main.go:10m
6/usr/local/go/src/runtime/proc.go:210r
7/usr/local/go/src/runtime/asm_amd64.s:1334r
func CallersFrames(callers []uintptr) *Frames
上⾯的Callers只是或者栈的程序计数器,如果想获得整个栈的信息,可以使⽤CallersFrames函数,省去遍历调⽤FuncForPC。
上⾯的trace函数可以更改为下⾯的⽅式:
1func trace2() {
2 pc := make([]uintptr, 10) // at least 1entry needed
3n := runtime.Callers(0, pc)
4frames := runtime.CallersFrames(pc[:n])
5for {
6frame, more := frames.Next()
7fmt.Printf("%s:%d %sn", f, f, fon)
8if !more {
9break
10}
11}
12}
func FuncForPC(pc uintptr) *Func
FuncForPC 是⼀个有趣的函数, 它可以把程序计数器地址对应的函数的信息获取出来。如果因为内联程序计数器对应多个函数,它返回最
外⾯的函数。
它的返回值是⼀个*Func类型的值,通过*Func可以获得函数地址、⽂件⾏、函数名等信息。
除了上⾯获取程序计数器的⽅式,也可以通过反射的⽅式获取函数的地址:
1runtime.FuncForPCreflect.ValueOffoo.Pointer.Name
(()())()
获取程序堆栈
在程序panic的时候,⼀般会⾃动把堆栈打出来,如果你想在程序中获取堆栈信息,可以通过tack()打印出来。⽐如你在程序中
遇到⼀个Error,但是不期望程序panic,只是想把堆栈信息打印出来以便跟踪调试,你可以使⽤tack()。
抑或,你⾃⼰读取堆栈信息,⾃⼰处理和打印:
1func DumpStacks() {
2 buf := make([]byte, 16384)
3buf = buf[:runtime.Stack(buf, true)]
4fmt.Printf("=== BEGIN goroutine stack dump ===n%sn=== END goroutine stack dump ===", buf)
5}
参考 调试利器:dump goroutine 的 stacktrace。
利⽤堆栈信息还可以获取goroutine的id, 参考: 再谈谈获取 goroutine id 的⽅法
1func GoID() int{
2 varbuf [64]byte
3n := r(buf[:], false)
4idField := s(efix(string(buf[:n]), "goroutine "))[0]
5id, err := s(idField)
6 iferr != nil{
7 panic(f("cannot get goroutine id: %v", err))
8}
9 returnid
10}
原⽂发布时间为:2018-11-5
本⽂作者:smallnest
本⽂来⾃云栖社区合作伙伴“ ”,了解相关信息可以关注“ ”。


发布评论