Skip to content

Commit d648c2e

Browse files
aclementsdims
authored andcommittedSep 2, 2021
fix file-based filtering symbolization
If file-filtered logging is enabled, V looks up its caller's source file path using runtime.Callers and runtime.FuncForPC. However, runtime.Callers returns return PCs, which aren't necessarily in the same source file as the call site. For example, if F calls V, and F's call to V is immediately followed by an inlined called to another function, say G, then symbolizing V's return PC will actually return G's file and line number, not F's. Fix this by subtracting 1 from the PC returned by runtime.Callers to back up from the return PC to the call PC. An arguably better fix would be to use runtime.CallersFrames, which abstracts away such details. Unfortunately, CallersFrames allocates (at least as of Go 1.15), so we avoid it here.
1 parent e1f317b commit d648c2e

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed
 

‎klog.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1328,9 +1328,14 @@ func V(level Level) Verbose {
13281328
if runtime.Callers(2, logging.pcs[:]) == 0 {
13291329
return newVerbose(level, false)
13301330
}
1331-
v, ok := logging.vmap[logging.pcs[0]]
1331+
// runtime.Callers returns "return PCs", but we want
1332+
// to look up the symbolic information for the call,
1333+
// so subtract 1 from the PC. runtime.CallersFrames
1334+
// would be cleaner, but allocates.
1335+
pc := logging.pcs[0] - 1
1336+
v, ok := logging.vmap[pc]
13321337
if !ok {
1333-
v = logging.setV(logging.pcs[0])
1338+
v = logging.setV(pc)
13341339
}
13351340
return newVerbose(level, v >= level)
13361341
}

0 commit comments

Comments
 (0)
Please sign in to comment.