Can't debug golang application delve because of error: could not attach to pid XXX: could not open debug info

Can't debug golang application delve because of error: could not attach to pid XXX: could not open debug info

所以我想在 k8s 集群上调试 运行ning 的 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

我以以下方式启动广告连播:

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),它应该会按预期工作。