panic()stacktrace不显示函数参数 [英] panic() stacktrace does not show function parameters

查看:37
本文介绍了panic()stacktrace不显示函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中:

package main
    
func main() {
    example(make([]string, 2, 4), "hello", 10)
}
    
func example(slice []string, str string, i int) {
    panic("Want stack trace")
}


使用6个单词存储数据并传递给函数 example .

  • 3个单词表示 slice 标头
  • 2个单词表示 str 字符串
  • 一个单词表示 i 整数
  • 3 words for slice header
  • 2 words for str string
  • one word for i integer

预期的堆栈跟踪:

goroutine 1 [running]:
main.example(0xc000042748, 0x2, 0x4, 0x106abae, 0x5, 0xa)
    stack_trace/example1/example1.go:13 +0x39
main.main()
    stack_trace/example1/example1.go:8 +0x72
    
// Declaration
main.example(slice []string, str string, i int)
    
// Call
make([]string, 2, 4), "hello", 10
    
// Values (0xc000042748, 0x2, 0x4, 0x106abae, 0x5, 0xa)
Slice Value:   0xc000042748, 0x2, 0x4
String Value:  0x106abae, 0x5
Integer Value: 0xa


实际堆栈跟踪:


Actual stack trace:

panic: Want stack trace
    
goroutine 1 [running]:
main.example(...)
        /home/../Main.go:8
main.main()
        /home/../Main.go:4 +0x39
exit status 2


$ go version
go version go1.14.3 linux/amd64


为什么从 panic()生成的stacktrace不显示这6个单词?


Why does the stacktrace generated from panic() not show those 6 words?

推荐答案

Go正在显示 main.example(...),因为函数是由编译器内联的并且不再作为函数存在,它已嵌入在 main()中(打印逻辑的详细信息位于

Go is showing main.example(...) because the function is inlined by the compiler and doesn't exist as a function anymore, it's been embedded in main() (details of the printing logic are in traceback.go).

您可以使用 go:noinline 在禁用内联的情况下,默认的回溯输出显示 main.example 及其参数:

With inlining disabled, the default traceback output shows main.example and its parameters:

panic: Want stack trace

goroutine 1 [running]:
main.example(0xc000046738, 0x2, 0x4, 0x473f27, 0x5, 0xa)
    /home/me/stuff/src/github.com/me/testing/panic/main.go:9 +0x39
main.main()
    /home/me/stuff/src/github.com/me/testing/panic/main.go:4 +0x72

请注意, make 不会出现在堆栈跟踪中,也不会出现:它在达到 panic 之前就已经返回.

Note that make does not appear in the stack trace, nor can it: it has returned before panic is ever reached.

一个忠告: go:noinline 在此处用于说明为什么堆栈跟踪不包含函数参数.通常,除非调试编译器或处理需要它的运行时函数,否则通常不应该强制执行编译器优化决策.

A word of advice: go:noinline is used here to show why the stack trace does not contain the function arguments. One should generally not force compiler optimization decisions unless debugging the compiler or dealing with runtime functions that need it.

这篇关于panic()stacktrace不显示函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆