Skip to content

Conversation

@ktoso
Copy link
Contributor

@ktoso ktoso commented Oct 9, 2025

We should expose the demangle functionality; It's been widely used by calling into internal _swift_demangle and instead we should offer a real API. It's also already used in the Runtime module already when forming backtraces.

Previous discussions happened between 2019 and 2024, and just never materialized in a complete implementation and proposal.

Right now, even more tools are in need of this API, as we are building continious profiling solutions etc, so it is a good time to revisit this topic.

This PR is roughly based off @Azoy's
https://github.com/swiftlang/swift/pull/25314/files#diff-fd379a721cc9a1c9ef6486eae713e945da842b42170d4d069029a57334371eba from 2019, however it brings it over to the new Runtime module which is a great place to put this functionality - even Backtrace had to recently reinvent calling the demangling infra in this module.

Pending SE review, proposal here.

cc @Azoy @al45tair

@ktoso
Copy link
Contributor Author

ktoso commented Oct 9, 2025

@swift-ci please smoke test

@ktoso ktoso requested a review from Azoy October 9, 2025 11:49
@ktoso ktoso force-pushed the wip-demangle-string branch from 6402000 to 899d2e9 Compare October 10, 2025 01:08
@ktoso
Copy link
Contributor Author

ktoso commented Oct 10, 2025

@swift-ci please smoke test

2 similar comments
@ktoso
Copy link
Contributor Author

ktoso commented Oct 10, 2025

@swift-ci please smoke test

@ktoso
Copy link
Contributor Author

ktoso commented Oct 10, 2025

@swift-ci please smoke test

@ktoso ktoso added swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review concurrency Feature: umbrella label for concurrency language features labels Oct 10, 2025
@ktoso ktoso force-pushed the wip-demangle-string branch from ff6de75 to 41eb337 Compare October 27, 2025 07:15
@ktoso
Copy link
Contributor Author

ktoso commented Oct 27, 2025

@swift-ci please smoke test

@ktoso ktoso force-pushed the wip-demangle-string branch 2 times, most recently from 6352e39 to d392c27 Compare October 27, 2025 13:41
@ktoso ktoso marked this pull request as ready for review October 27, 2025 13:41
@ktoso ktoso force-pushed the wip-demangle-string branch from d392c27 to 9db938e Compare October 27, 2025 13:43

// Demangle the given raw name (supports Swift and C++)
//
// The demangled string IS a null-terminated c-string.
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't appear to be true with the current implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, good catch... this is a behavior change actually hm...

_swift_backtrace_demangle used to null-terminate, and the only user of that was in SymbolicatedBacktrace demangleRawName which now delegates directly to demangle() -> String but only in Swift 6.3...

Null terminating in the new one isn't so great since they're aimed to be used with spans which rely on the explicit count rather than strlen-ing the result hm.

I'll think how to handle this in the Backtrace lib.

Copy link
Contributor

@mikeash mikeash Oct 29, 2025

Choose a reason for hiding this comment

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

Could we return a NUL terminated string from the C interface, but provide a span which doesn't cover the terminator? Slightly wasteful, but convenient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah maybe that way around would be easier

@ktoso ktoso force-pushed the wip-demangle-string branch from 32382c5 to 5626571 Compare October 29, 2025 07:29
@ktoso
Copy link
Contributor Author

ktoso commented Oct 29, 2025

Thanks for the review! Adjusted things and fixed up the \0 story a bit, need to determine if we keep the old backtrace entrypoint at all.

I also added tests for the C++ mangling.

@ktoso ktoso force-pushed the wip-demangle-string branch from 7f91a50 to b469d3e Compare October 29, 2025 08:15
@ktoso
Copy link
Contributor Author

ktoso commented Oct 29, 2025

@swift-ci please smoke test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

concurrency Feature: umbrella label for concurrency language features swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants