登录
首页 >  Golang >  Go问答

使用错误包时,Go 会显示与运行时调用器返回的程序计数器不匹配的函数名称

来源:stackoverflow

时间:2024-02-19 21:27:30 214浏览 收藏

哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《使用错误包时,Go 会显示与运行时调用器返回的程序计数器不匹配的函数名称》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

问题内容

代码1:

package main

import (
    "fmt"
    "github.com/pkg/errors"
    "runtime"
)

func main() {
    ptrs, _ := test1("arg1", "arg2")
    for _, pc := range *ptrs {
        f := runtime.funcforpc(pc)
        if f == nil {
            continue
        }
        filename, line := f.fileline(pc)
        fmt.printf("func name: %s, entry: %x, filename: %s, line: %d\n", f.name(), f.entry(), filename, line)
    }
}

func test0(args ...interface{}) *[]uintptr {
    const depth = 32
    var pcs [depth]uintptr
    n := runtime.callers(0, pcs[:])
    st := pcs[0:n]
    return &st
}

func test1(args ...interface{}) (*[]uintptr, error) {
    return test0(args...), errors.new("test")
}

代码2:

package main

import (
    "fmt"
    "runtime"
)

func main() {
    ptrs, _ := test1("arg1", "arg2")
    for _, pc := range *ptrs {
        f := runtime.funcforpc(pc)
        if f == nil {
            continue
        }
        filename, line := f.fileline(pc)
        fmt.printf("func name: %s, entry: %x, filename: %s, line: %d\n", f.name(), f.entry(), filename, line)
    }
}

func test0(args ...interface{}) *[]uintptr {
    const depth = 32
    var pcs [depth]uintptr
    n := runtime.callers(0, pcs[:])
    st := pcs[0:n]
    return &st
}

func test1(args ...interface{}) (*[]uintptr, error) {
    return test0(args...), nil//, errors.new("test")
}

唯一不同的部分是返回 test1() 的错误值。在code1中它返回erors.new("test"),在code2中它返回nil。在main函数的for循环中,程序打印函数名,结果不同:

代码1的结果:

func name: runtime.callers, entry: 6a20c0, filename: c:/program files/go/src/runtime/extern.go, line: 247
func name: runtime.callers, entry: 6a20c0, filename: c:/program files/go/src/runtime/extern.go, line: 247
func name: main.test1, entry: 6a2180, filename: d:/develop/go-testbed/cmd/slicetest/slicetest.go, line: 30
func name: main.main, entry: 6a1ea0, filename: d:/develop/go-testbed/cmd/slicetest/slicetest.go, line: 11
func name: runtime.main, entry: 6476a0, filename: c:/program files/go/src/runtime/proc.go, line: 259
func name: runtime.goexit, entry: 66eee0, filename: c:/program files/go/src/runtime/asm_amd64.s, line: 1595

代码2的结果

func name: runtime.Callers, entry: b30020, filename: C:/Program Files/Go/src/runtime/extern.go, line: 247
func name: runtime.Callers, entry: b30020, filename: C:/Program Files/Go/src/runtime/extern.go, line: 247
func name: main.main, entry: b2fe00, filename: D:/develop/go-testbed/cmd/slicetest/slicetest.go, line: 10
func name: main.main, entry: b2fe00, filename: D:/develop/go-testbed/cmd/slicetest/slicetest.go, line: 9
func name: runtime.main, entry: ad6fc0, filename: C:/Program Files/Go/src/runtime/proc.go, line: 259
func name: runtime.goexit, entry: afe580, filename: C:/Program Files/Go/src/runtime/asm_amd64.s, line: 1595

如您所见,code1 打印了正确的函数名称 main.test1code2 没有。 我读到了 runtime.callers 根据其运行位置打印不同的程序计数器,建议使用 runtime.callersframes() 但我认为这不是问题,因为 code1code2 不要使用它。我还看到了 errors.new() 函数,但我没有发现什么特别的。它通过 caller() 函数创建调用堆栈,该函数与我的代码中的 test0() 几乎相同 - 实际上,我从 caller() 复制了 test0() 的内容。

有人可以告诉我是什么原因导致了差异吗?

go版本:go1.19 windows/amd64

操作系统:windonws 11

github.com/pkg/errors 版本:v0.9.1

如果我在 goland ide 的调试模式下运行这两个代码,它们会与 main.test1 打印相同的结果。

谢谢!


正确答案


来自runtime.Callers的文档

要将这些 PC 转换为符号信息,例如函数名称和行号,使用 CallersFrames。 CallersFrames 考虑内联函数并将返回程序计数器调整为调用程序计数器。 不鼓励直接迭代返回的 PC 切片,就像在任何返回的 PC 上使用 FuncForPC 一样,因为它们无法考虑内联或返回程序计数器调整。

(emph我的) 您不得尝试自己解释 runtime.Callers 的结果,因为您无法正确执行此操作。您的有缺陷的尝试清楚地表明文档是正确的。

这实际上与 github.com/pkg/errors 没有任何关系(无论如何,它都不受维护并被包错误中的新内容取代)。这是编译器优化代码的结果(调试时有所不同)。

经验法则:阅读您使用的函数的文档并按照文档的指示进行操作。这是“有时在实验室呆 3 个月可以让你在图书馆节省 6 个小时”的一个例子。

以上就是《使用错误包时,Go 会显示与运行时调用器返回的程序计数器不匹配的函数名称》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>