Skip to content

Conversation

@derekparker
Copy link
Member

@derekparker derekparker commented Oct 24, 2025

This invariant does not hold for optimized binaries. For the example program included in this patch, the disassembly of the function for an optimized build looks like the following:

TEXT main.ManyArgsWithNamedReturns(SB) /home/deparker/Code/delve/_fixtures/multinamedreturns.go
multinamedreturns.go:24 0x4a9f40 4c894c2470 mov qword ptr [rsp+0x70], r9
multinamedreturns.go:24 0x4a9f45 4c899c2480000000 mov qword ptr
[rsp+0x80], r11 => multinamedreturns.go:7 0x4a9f4d 488d1403 lea rdx, ptr
[rbx+rax*1] multinamedreturns.go:7 0x4a9f51 4801ca add rdx, rcx
multinamedreturns.go:7 0x4a9f54 4801fa add rdx, rdi
multinamedreturns.go:7 0x4a9f57 4801f2 add rdx, rsi
multinamedreturns.go:7 0x4a9f5a 4c01c2 add rdx, r8
multinamedreturns.go:7 0x4a9f5d 4c01ca add rdx, r9
multinamedreturns.go:7 0x4a9f60 4c01d2 add rdx, r10
... 

As you can see the entry is marked as line 24, whereas the first line we
really want to stop at is line 7 in this build. Enforcing that we want
to find a statement with the same line as the entry, this makes Delve
stop at the end at the ret instruction:

multinamedreturns.go:22 0x4aa01b 488d1c11 lea rbx, ptr [rcx+rdx*1]
multinamedreturns.go:22 0x4aa01f 48d1fb sar rbx, 0x1
multinamedreturns.go:24 0x4aa022 c3 ret

This invariant does not hold for optimized binaries. For the example
program included in this patch, the disassembly of the function for an
optimized build looks like the following:

``` TEXT main.ManyArgsWithNamedReturns(SB)
/home/deparker/Code/delve/_fixtures/multinamedreturns.go
multinamedreturns.go:24 0x4a9f40 4c894c2470 mov qword ptr [rsp+0x70], r9
multinamedreturns.go:24 0x4a9f45 4c899c2480000000 mov qword ptr
[rsp+0x80], r11 => multinamedreturns.go:7 0x4a9f4d 488d1403 lea rdx, ptr
[rbx+rax*1] multinamedreturns.go:7 0x4a9f51 4801ca add rdx, rcx
multinamedreturns.go:7 0x4a9f54 4801fa add rdx, rdi
multinamedreturns.go:7 0x4a9f57 4801f2 add rdx, rsi
multinamedreturns.go:7 0x4a9f5a 4c01c2 add rdx, r8
multinamedreturns.go:7 0x4a9f5d 4c01ca add rdx, r9
multinamedreturns.go:7 0x4a9f60 4c01d2 add rdx, r10 ... ```

As you can see the entry is marked as line 24, whereas the first line we
really want to stop at is line 7 in this build. Enforcing that we want
to find a statement with the same line as the entry, this makes Delve
stop at the end at the `ret` instruction:

``` multinamedreturns.go:22 0x4aa01b 488d1c11 lea rbx, ptr [rcx+rdx*1]
multinamedreturns.go:22 0x4aa01f 48d1fb sar rbx, 0x1
multinamedreturns.go:24 0x4aa022 c3 ret ```
Copy link
Member

@aarzilli aarzilli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aarzilli aarzilli merged commit 7080b62 into go-delve:master Oct 28, 2025
5 checks passed
@derekparker derekparker deleted the wip/fix-first-stmt branch October 28, 2025 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants