使用错误包时,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.test1
但 code2 没有。
我读到了 runtime.callers 根据其运行位置打印不同的程序计数器,建议使用 runtime.callersframes()
但我认为这不是问题,因为 code1 和 code2 不要使用它。我还看到了 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学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习