[Go] Debugging 代码时无法获取Go plugin(.so文件)的内存地址

697 阅读1分钟

一、问题描述

Go 程序中调用了一个 Go plugin 插件,在调试这个调用了插件的程序时,发现无法获取插件的内存地址。

run 时能通过plugin.Open(filename)正确加载 wc.so 插件:

image.png 上图可见:log能打印出该插件的指针指向一个非零地址。

但 debugger 就无法获取 plugin 的内存地址:

image.png

二、原因

因为这个 go plugin(实质是 Linux 下的一个 .so 共享目标文件)压根就没有编译/打包到被调试的二进制码中。自然在调试时无法访问其内存地址。

如果一定要调试,取消 plugin 模式,直接把里面的Go 源码放到调试程序源代码中。

三、解决方案

  1. Go plugin debugging - Stack Overflow :Stackoverflow 给出了两种解决方案:
  • 要么干脆别用 plugin模式,源码整合进待调试Go文件中
  • 要么使用runtime.BreakPoint(),并在构建时添加参数:go build -trimpath -gcflags "all=-N -l"
  1. Cannot debug go plugins on mac · Issue #1628 · go-delve/delve (github.com) :Go 的调试器DelveGithub仓库也有人提了这个 issue(2019年),但是官方表示这和Go语言本身某个提案冲突。
  2. Unable to debug plugin within precompiled binary using dlv exec · Issue #3042 · go-delve/delve :2022年1月的的issue,在 linux/amd64 + Go1.16 + Delve1.8.3 环境下希望调试 plugin文件,但是被告知需要等待 #1653 issue,即下面这个:
  3. Breakpoints in plugins are not working if set before plugin is loaded · Issue #1653 · go-delve/delve (github.com) :提到,问题和GoLand无关,就是Delve 的问题。