Skip to content

尝试使用 dlv (delve) 查看 go 程序产生的 core dump #22

@binderclip

Description

@binderclip

#16 中产生了 core dump 文件,接下来尝试用 dlv 工具查看一下;

dlv core <executable> <core> [flags]

在 dlv 中:

bt # 打印 stack trace,定位 panic 的位置
frame 9 # 切换到对应 frame
locals # 打印一下局部变量
vars main # 打印一下 package 级别 main 相关变量

第一次错误执行的结果:

(dlv) frame 13
> runtime.raise() /usr/lib/go-1.17/src/runtime/sys_linux_arm64.s:167 (PC: 0x69398)
Warning: debugging optimized function
Frame 13: ./app.go:5 (PC: 6a818)
     1:	package main
     2:
     3:	func main() {
     4:		for i := 0; i < 10; i++ {
=>   5:			println(100/(i-8))
     6:		}
     7:	}
(dlv) locals
i = (unreadable could not find loclist entry at 0x5e05a for address 0x6a818)

关于 locals 看不到的猜测:

  1. 由于 core dump 最后几次运行没生成,实际生成的是 go run 临时编译的版本,但 core dump 的是 go build 生产的版本,所以对应不上
  2. 因为 Warning: debugging optimized function,被优化过,所以不能正确查看

Unreadable arguments and "debugging optimized function" warning · Issue #1368 · go-delve/delve

go build -gcflags="all=-N -l" app.go 之后生成的文件可以用 dlv 查看变量值了:

(dlv) frame 12
> runtime.raise() /usr/lib/go-1.17/src/runtime/sys_linux_arm64.s:167 (PC: 0x6e648)
Warning: debugging optimized function
Frame 12: ./app.go:5 (PC: 6fa50)
     1:	package main
     2:
     3:	func main() {
     4:		for i := 0; i < 10; i++ {
=>   5:			println(100/(i-8))
     6:		}
     7:	}
(dlv) locals
i = 273884

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions