Skip to content

proc: implement frame pointer unwinding - #4288

Merged
derekparker merged 6 commits into
go-delve:masterfrom
alexsaezm:master
Apr 28, 2026
Merged

derekparker merged 6 commits into
go-delve:masterfrom
alexsaezm:master

Conversation

@alexsaezm

Copy link
Copy Markdown
Contributor

Add hybrid stack unwinding that attempts frame-pointer-based unwinding before falling back to DWARF. Also adds BenchmarkStacktrace

Only affects AMD64 and ARM64. I tried to make it work on PPC64LE without luck. But in those two architectures, the improvement is noticeable. A reduction of almost ~50%. From ~20k allocs/op to 10k allocs/op.

I would like to add that this patch was mostly possible thanks to @derekparker. I've been trying to attempt this for a few weeks without any sucess, until he gave me the original patch this is based on. I mostly went over it to add ARM64 specifics because before that only AMD64 was using frame-pointer.

@aarzilli

Copy link
Copy Markdown
Member

Delve is already supposed to be doing frame pointer chasing. See amd64_arch.go:

		// When there's no frame descriptor entry use BP (the frame pointer) instead
		// - return register is [bp + a.PtrSize()] (i.e. [cfa-a.PtrSize()])
		// - cfa is bp + a.PtrSize()*2
		// - bp is [bp] (i.e. [cfa-a.PtrSize()*2])
		// - sp is cfa

		// When the signal handler runs it will move the execution to the signal
		// handling stack (installed using the sigaltstack system call).
		// This isn't a proper stack switch: the pointer to g in TLS will still
		// refer to whatever g was executing on that thread before the signal was
		// received.
		// Since go did not execute a stack switch the previous value of sp, pc
		// and bp is not saved inside g.sched, as it normally would.
		// The only way to recover is to either read sp/pc from the signal context
		// parameter (the ucontext_t* parameter) or to unconditionally follow the
		// frame pointer when we get to runtime.sigreturn (which is what we do
		// here).

and

	// We assume that RBP is the frame pointer, and we want to keep it updated,
	// so that we can use it to unwind the stack even when we encounter frames
	// without descriptor entries.
	// If there isn't a rule already we emit one.

Does it not work anymore?

I'm not surprised that frame pointer chasing is faster than dwarf, we're talking dereferencing a pointer vs running the bytecode for a general purpose language. However just doing that, or doing that first and then looking at dwarf, is not the correct thing to do.

@derekparker

Copy link
Copy Markdown
Member

Delve is already supposed to be doing frame pointer chasing. See amd64_arch.go:

		// When there's no frame descriptor entry use BP (the frame pointer) instead
		// - return register is [bp + a.PtrSize()] (i.e. [cfa-a.PtrSize()])
		// - cfa is bp + a.PtrSize()*2
		// - bp is [bp] (i.e. [cfa-a.PtrSize()*2])
		// - sp is cfa

		// When the signal handler runs it will move the execution to the signal
		// handling stack (installed using the sigaltstack system call).
		// This isn't a proper stack switch: the pointer to g in TLS will still
		// refer to whatever g was executing on that thread before the signal was
		// received.
		// Since go did not execute a stack switch the previous value of sp, pc
		// and bp is not saved inside g.sched, as it normally would.
		// The only way to recover is to either read sp/pc from the signal context
		// parameter (the ucontext_t* parameter) or to unconditionally follow the
		// frame pointer when we get to runtime.sigreturn (which is what we do
		// here).

and

	// We assume that RBP is the frame pointer, and we want to keep it updated,
	// so that we can use it to unwind the stack even when we encounter frames
	// without descriptor entries.
	// If there isn't a rule already we emit one.

Does it not work anymore?

No this still works, and we should reuse more of this.

I'm not surprised that frame pointer chasing is faster than dwarf, we're talking dereferencing a pointer vs running the bytecode for a general purpose language. However just doing that, or doing that first and then looking at dwarf, is not the correct thing to do.

This patch does a hybrid, similar to what the Go runtime does for the execution tracer, however we get more fidelity as we combine with DWARF or other methods when necessary (CGO transitions, frameless functions). The spirit of this patch is to try and prioritize the quick and efficient frame pointer based unwinding when possible, and fall back to DWARF or other methods when necessary. Essentially the opposite of what we do now, which is using frame pointer unwinding as the fallback. Even mentioned in the code you qouted:

  // When there's no frame descriptor entry use BP (the frame pointer) instead

Why fall back to the most efficient method instead of use it until we can't?

@aarzilli

Copy link
Copy Markdown
Member

If this is about performance, then we should only do it for go functions, where we know that it is a valid method. See the test failures too, they do look related.

Comment thread pkg/proc/stack.go Resolved
Comment thread pkg/proc/stack.go Resolved
Comment thread pkg/proc/stack.go Resolved
Comment thread pkg/proc/stack.go Resolved
@alexsaezm
alexsaezm requested a review from aarzilli March 23, 2026 17:48
Comment thread _fixtures/deeprecursion.go Resolved
Comment thread pkg/proc/stack.go Resolved
Comment thread pkg/proc/stack.go Resolved
Comment thread pkg/proc/stack.go Resolved
@aarzilli

aarzilli commented Apr 3, 2026

Copy link
Copy Markdown
Member

I believe the test failures, in TeamCity, are related to this change set.

@alexsaezm

alexsaezm commented Apr 17, 2026 •

Copy link
Copy Markdown
Contributor Author

I believe the test failures, in TeamCity, are related to this change set.

It looks like it works with Go tip but no with 1.24 and 1.25. I disable the feature for Windows ARM with Go 1.24 and 1.25. I think it might be related to golang/go#63630

Let's see if the tests pass because I don't have a Windows system to try locally.

Edit: I didn't see the other errors. I will work on them.

Add hybrid stack unwinding that attempts frame-pointer-based unwinding before falling back to DWARF.
Remove _fixtures/deeprecursion.go
@alexsaezm

Copy link
Copy Markdown
Contributor Author

I think I properly fixed the TeamCity failures with the latest commit.

I'm confused with the Windows tests... I thought they were fixed, but now 1.25 fails?

@derekparker

Copy link
Copy Markdown
Member

I think I properly fixed the TeamCity failures with the latest commit.

I'm confused with the Windows tests... I thought they were fixed, but now 1.25 fails?

We're having issues with Windows/arm64 lately with intermittent failures. Doesn't seem related to this change.

@aarzilli aarzilli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@derekparker derekparker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@derekparker
derekparker merged commit 1d5a7eb into go-delve:master Apr 28, 2026
3 of 4 checks passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
delve 1.27.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* service/dap: avoid Session.conn race in breakpoint-after-disconnect test by @alexsaezm in go-delve/delve#4317
* proc: implement frame pointer unwinding by @alexsaezm in go-delve/delve#4288
* proc: fix SP calculation for sigpanic frames on arm64 by @alexsaezm in go-delve/delve#4319
* native: propagate PtraceSetRegs error on linux/ppc64le by @cuiweixie in go-delve/delve#4321
* native: propagate PtraceGetRegs error on linux/ppc64le by @cuiweixie in go-delve/delve#4322
* proc: propagate AddrPiece ReadMemory errors in composite memory by @cuiweixie in go-delve/delve#4323
* proc/internal/ebpf: fix AddressToOffset off-by-one at section load by @cuiweixie in go-delve/delve#4324
* proc: fix OR handling in breakpointConditionSatisfiable by @cuiweixie in go-delve/delve#4325
* dwarf/reader: propagate errors during Reader entry iteration by @cuiweixie in go-delve/delve#4327
* proc: bail out of loadArrayValues after stride overflow by @cuiweixie in go-delve/delve#4328
* proc: compile hit-condition regexp once by @cuiweixie in go-delve/delve#4335
* service/dap: add newline to the build message by @sagg0t in go-delve/delve#4340
* proc: fix test for go1.27 regexp refactor by @aarzilli in go-delve/delve#4339
* proc: fix range over func stepping for go1.27 by @aarzilli in go-delve/delve#4343
* chore: fix some comments to improve readability by @box4wangjing in go-delve/delve#4344
* Teamcity: reenable riscv64 build by @aarzilli in go-delve/delve#4346
* proc: fix hasInlines in stacktrace, fix range step with inlining by @aarzilli in go-delve/delve#4345
* chore: fix comment to improve readability by @cuoguojida in go-delve/delve#4350
* service/dap: relax TestBadLaunchRequest by @aarzilli in go-delve/delve#4351
* TeamCity: adjust execution timeout by @vietage in go-delve/delve#4354
* proc/internal/ebpf: switch to header+param event ring buffer protocol by @derekparker in go-delve/delve#4352
* proc: add test for struct literals with embedded field selectors by @aarzilli in go-delve/delve#4362
* proc: disable some failing tests on riscv64 by @aarzilli in go-delve/delve#4361
* gobuild: increase wait length for deleting binaries on Windows by @aarzilli in go-delve/delve#4359
* proc: add test for accessing global C variables by @aarzilli in go-delve/delve#4358
* proc: support generic methods by @aarzilli in go-delve/delve#4356
* Revert "winarm64: remove experimental build tags (#4176)" by @aarzilli in go-delve/delve#4281
* proc/test: include GOEXPERIMENT in the fixture cache key  by @typesanitizer in go-delve/delve#4367
* proc: fix stepping test on go1.27 / arm64 by @aarzilli in go-delve/delve#4365
* service/dap: add dap write memory request handler by @DrSergei in go-delve/delve#4364
* proc: make PushPackageVarOrSelect check local variables first by @aarzilli in go-delve/delve#4181
* pkg/proc: add GOEXPERIMENT=mapsplitgroup support by @aarzilli in go-delve/delve#4370
* service/dap: send ExitedEvent before TerminatedEvent by @derekparker in go-delve/delve#4371
* service/dap: fix test failures from ExitedEvent change by @derekparker in go-delve/delve#4373
* service/dap,pkg/debugdetect: fix tests by @derekparker in go-delve/delve#4375
* v1.27.0 by @derekparker in go-delve/delve#4372

## New Contributors
* @sagg0t made their first contribution in go-delve/delve#4340
* @box4wangjing made their first contribution in go-delve/delve#4344
* @cuoguojida made their first contribution in go-delve/delve#4350

**Full Changelog**: go-delve/delve@v1.26.3...v1.27.0
**Curated Changelog**: https://github.com/go-delve/delve/blob/master/CHANGELOG.md#1270-2026-06-19</pre>
  <p>View the full release notes at <a href="https://github.com/go-delve/delve/releases/tag/v1.27.0">https://github.com/go-delve/delve/releases/tag/v1.27.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!12311
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.

3 participants