So I want to debug a golang application that is running on k8s cluster, but I get the error message when I want to attach delve to the app. "could not attach to pid XXX: could not open debug info "
In the k8s deployments I added the needed privileges:
securityContext:
capabilities:
add:
- SYS_PTRACE
privileged: true
runAsUser: 0
allowPrivilegeEscalation: true
I compiled my application with the needed gcflags "all=-N -l"
go build -mod vendor -gcflags "all=-N -l" --ldflags -w -s -o app
I start the pod with:
dlv --listen=:40000 --headless=true --api-version=2 --accept-multiclient exec /app
I verified that I run the right container image, it is the same SHA hash that I push. I verified that it is the right binary the hash matches here as well.
I have set:
echo 0 > /proc/sys/kernel/yama/ptrace_scope
cat /proc/sys/kernel/yama/ptrace_scope
0
Here is the readelf -S app
output of the binary.
>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)
I am out of ideas on how to resolve.
You're passing -s -w
as flags to the linker.
According to the documentation of cmd/link
:
-s: Omit the symbol table and debug information.
-w: Omit the DWARF symbol table.
In short, your build command removes the information your debugger requires for debugging.
If you remove -ldflags
(or only -s -w
), it should work as expected.