From 5dd43cf9b45297d5b337308411c5085f5059b866 Mon Sep 17 00:00:00 2001 From: Jooyung Han Date: Thu, 5 Sep 2019 15:48:27 +0900 Subject: [PATCH] Fix checkCalledFromInit This function checks if it is called from init() by looking into callers. By the way, it may fail when init() is inlined. To fix this, CallersFrames() is used to translate PCs into symbolic information accounting for inlined functions. Test: go test ./... Change-Id: I18b3c3ffdaf71f775e3522c87607aec5df7b09c5 --- package_ctx.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/package_ctx.go b/package_ctx.go index e0a03c8..c55152a 100644 --- a/package_ctx.go +++ b/package_ctx.go @@ -158,8 +158,9 @@ func callerName(skip int) (pkgPath, funcName string, ok bool) { if n != 1 { return "", "", false } - - f := runtime.FuncForPC(pc[0]).Name() + frames := runtime.CallersFrames(pc[:]) + frame, _ := frames.Next() + f := frame.Function s := pkgPathRe.FindStringSubmatch(f) if len(s) < 3 { panic(fmt.Errorf("failed to extract package path and function name from %q", f))