Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Runtime] load debug env vars from Android system properties #78251

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

andrurogerz
Copy link
Contributor

@andrurogerz andrurogerz commented Dec 17, 2024

Purpose

Enable setting SWIFT_DEBUG_ vars in Swift Android apps by reading debug.swift.runtime.SWIFT_DEBUG_ system properties during runtime init on Android.

Overview

  • Add a private platformInitialize() function to the env var setup in the Swift runtime and implement it only for Android
  • Call the new platformInitialize function from each of the swift::runtime::environment::initialize implementations

Background

Because all Android app processes all fork from the same process, they don't pick-up environment changes on launch. Android system properties give us an alternative, standard mechanism to do something similar. Only system properties prefixed with debug. can be set without root permissions.

Validation

Set system properties on an Android device:

adb -d shell setprop debug.org.swift.runtime.SWIFT_DEBUG_ENABLE_METADATA_BACKTRACE_LOGGING 1
adb -d shell setprop debug.org.swift.runtime.SWIFT_DEBUG_ENABLE_METADATA_ALLOCATION_ITERATION 1

Kill and re-launch a simple Android app that initializes the Swift runtime from Application.onCreate.

adb -d shell am force-stop com.example.SwiftAppPackage
adb -d shell am start com.example.SwiftAppPackage/.MainActivity

Using the debugger, confirm these variables are set to true:

swift::runtime::environment::SWIFT_DEBUG_ENABLE_METADATA_BACKTRACE_LOGGING_variable
swift::runtime::environment::SWIFT_DEBUG_ENABLE_METADATA_ALLOCATION_ITERATION _variable

@andrurogerz
Copy link
Contributor Author

Note to reviewers: this change is primarily useful for swift-inspect Android support, which I have working locally and will publish a PR for as soon as #77938 is merged.

@andrurogerz
Copy link
Contributor Author

+@compnerd

@andrurogerz
Copy link
Contributor Author

@compnerd please see my response to your comment and let me know if you have any other suggestions. If you're good with what I have here, please request CI. Thanks!

@andrurogerz andrurogerz requested a review from compnerd December 17, 2024 23:31
Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

I think that we should sink the debug. to the end of the name so that all the Swift properties are co-located (in case we add any additional ones in the future).

stdlib/public/runtime/EnvironmentVariables.cpp Outdated Show resolved Hide resolved
stdlib/public/runtime/EnvironmentVariables.cpp Outdated Show resolved Hide resolved
stdlib/public/runtime/EnvironmentVariables.cpp Outdated Show resolved Hide resolved
@andrurogerz
Copy link
Contributor Author

@compnerd would you mind triggering test runs on this one?

@compnerd
Copy link
Member

compnerd commented Jan 7, 2025

@swift-ci please test

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