go - 由于错误 : could not attach to pid XXX: could not open debug info,无法调试 golang 应用程序 delve

标签 go debugging remote-debugging delve

所以我想调试一个在 k8s 集群上运行的 golang 应用程序,但是当我想将 delve 附加到该应用程序时收到错误消息。 “无法附加到 pid XXX:无法打开调试信息”

在 k8s 部署中,我添加了所需的权限:

    securityContext:
    capabilities:
      add:
        - SYS_PTRACE
    privileged: true
    runAsUser: 0
    allowPrivilegeEscalation: true

我使用所需的 gcflags“all=-N -l”编译了我的应用程序

go build -mod vendor -gcflags“all=-N -l”--ldflags -w -s -o app

我用以下命令启动 Pod:

dlv --listen=:40000 --headless=true --api-version=2 --accept-multiclient exec/app

我验证了我运行了正确的容器镜像,它与我推送的 SHA 哈希值相同。 我验证了它是正确的二进制文件,哈希值也在这里匹配。

我已经设置:

echo 0 > /proc/sys/kernel/yama/ptrace_scope
cat /proc/sys/kernel/yama/ptrace_scope
0

这是二进制文件的 readelf -S app 输出。

>readelf -S app

There are 27 section headers, starting at offset 0x270:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .text             PROGBITS         0000000000401000  00001000
       000000000223c47d  0000000000000000  AX       0     0     16
  [ 2] .plt              PROGBITS         000000000263d480  0223d480
       0000000000000290  0000000000000010  AX       0     0     16
  [ 3] .rodata           PROGBITS         000000000263e000  0223e000
       0000000000cd4919  0000000000000000   A       0     0     32
  [ 4] .rela             RELA             0000000003312920  02f12920
       0000000000000018  0000000000000018   A      11     0     8
  [ 5] .rela.plt         RELA             0000000003312938  02f12938
       00000000000003c0  0000000000000018   A      11     2     8
  [ 6] .gnu.version_r    VERNEED          0000000003312d00  02f12d00
       0000000000000050  0000000000000000   A      10     2     8
  [ 7] .gnu.version      VERSYM           0000000003312d60  02f12d60
       000000000000005a  0000000000000002   A      11     0     2
  [ 8] .hash             HASH             0000000003312dc0  02f12dc0
       00000000000000d8  0000000000000004   A      11     0     8
  [ 9] .shstrtab         STRTAB           0000000000000000  02f12ea0
       0000000000000111  0000000000000000           0     0     1
  [10] .dynstr           STRTAB           0000000003312fc0  02f12fc0
       0000000000000268  0000000000000000   A       0     0     1
  [11] .dynsym           DYNSYM           0000000003313240  02f13240
       0000000000000438  0000000000000018   A      10     1     8
  [12] .typelink         PROGBITS         0000000003313680  02f13680
       000000000001064c  0000000000000000   A       0     0     32
  [13] .itablink         PROGBITS         0000000003323cd0  02f23cd0
       0000000000006a60  0000000000000000   A       0     0     8
  [14] .gosymtab         PROGBITS         000000000332a730  02f2a730
       0000000000000000  0000000000000000   A       0     0     1
  [15] .gopclntab        PROGBITS         000000000332a740  02f2a740
       000000000113d450  0000000000000000   A       0     0     32
  [16] .go.buildinfo     PROGBITS         0000000004468000  04068000
       0000000000000020  0000000000000000  WA       0     0     16
  [17] .dynamic          DYNAMIC          0000000004468020  04068020
       0000000000000130  0000000000000010  WA      10     0     8
  [18] .got.plt          PROGBITS         0000000004468160  04068160
       0000000000000158  0000000000000008  WA       0     0     8
  [19] .got              PROGBITS         00000000044682b8  040682b8
       0000000000000008  0000000000000008  WA       0     0     8
  [20] .noptrdata        PROGBITS         00000000044682c0  040682c0
       00000000000885c0  0000000000000000  WA       0     0     32
  [21] .data             PROGBITS         00000000044f0880  040f0880
       000000000001f630  0000000000000000  WA       0     0     32
  [22] .bss              NOBITS           000000000450fec0  0410fec0
       000000000003ba90  0000000000000000  WA       0     0     32
  [23] .noptrbss         NOBITS           000000000454b960  0414b960
       0000000000004708  0000000000000000  WA       0     0     32
  [24] .tbss             NOBITS           0000000000000000  00000000
       0000000000000008  0000000000000000 WAT       0     0     8
  [25] .interp           PROGBITS         0000000000400fe4  00000fe4
       000000000000001c  0000000000000000   A       0     0     1
  [26] .note.go.buildid  NOTE             0000000000400f80  00000f80
       0000000000000064  0000000000000000   A       0     0     4
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)

我不知道如何解决。

最佳答案

您将 -s -w 作为标志传递给链接器。

根据documentation of cmd/link :

-s: Omit the symbol table and debug information.

-w: Omit the DWARF symbol table.

简而言之,您的构建命令会删除调试器调试所需的信息。

如果删除 -ldflags(或仅删除 -s -w),它应该按预期工作。

关于go - 由于错误 : could not attach to pid XXX: could not open debug info,无法调试 golang 应用程序 delve,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66405077/

相关文章:

c - atof 产生奇怪的结果

java - 我可以从 JVM 内部知道 jdwp 传输端口吗?

java - 无法远程启动 WebLogic 进行调试

无 Main 方法的 Java 远程调试

c++ - malloc() 和 malloc_consolidate() 中的段错误

go - Google Drive SDK 版本 3 中的文件上传

go - 我们如何在 kafka 中快速写入单条消息(不是批量消息)?

Go不能调用c++函数

.net - 简单的 WCF 调用需要很多时间

go - 如何在同一包的 2 个单独文件中拆分 GO Gorilla Mux 路由