Skip to content

[NativeAOT] Add printf gref logging - #12218

Merged
jonathanpeppers merged 32 commits into
mainfrom
jonathanpeppers-finish-gref-log-formatting
Jul 28, 2026
Merged

[NativeAOT] Add printf gref logging#12218
jonathanpeppers merged 32 commits into
mainfrom
jonathanpeppers-finish-gref-log-formatting

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Jul 23, 2026

Copy link
Copy Markdown
Member

Dependency

Depends on #12211 and targets its jonathanpeppers-finish-gc-bridge-printf branch.

Part of #12139.

Design

Adds a non-exported printf-style OSBridge::_monodroid_gref_logf() path for the shared CoreCLR/NativeAOT GC bridge. It formats directly to logcat with log_writev() and to the gref file with vfprintf(), using va_copy() so both sinks can consume the arguments without allocating or transferring ownership.

The existing exported _monodroid_gref_log(const char*) entry point, P/Invoke registration, managed callers, and MonoVM implementation remain unchanged.

Scope

Migrates only the three intentionally deferred std::format calls in src/native/clr/host/bridge-processing.cc that feed the gref file/logcat pipeline.

Behavior preservation

  • Retains the existing Logger::gref_log(), Logger::gref_to_logcat(), LOG_GREF, and gc_spew_enabled() gates and surrounding control flow.
  • Preserves message text, punctuation, newlines, source locations, and sink flushing.
  • Uses explicit 0x prefixes with PRIxPTR/uintptr_t so handles retain lowercase pointer-width hexadecimal output, including 0x0 for null handles.
  • Keeps the existing null-message handling on the exported string API; the new internal format argument is also null-safe.
  • Borrows format strings and arguments only for the duration of the call and introduces no returned or transferred ownership.

Non-goals

  • Does not change the managed/native ABI or add a new P/Invoke symbol.
  • Does not modify MonoVM-only gref logging.
  • Does not migrate unrelated formatting outside the NativeAOT-reachable shared GC bridge.

Validation

Prerequisites initialized in the fresh worktree:

  • dotnet build build-tools\xa-prep-tasks\xa-prep-tasks.csproj -v:minimal
  • dotnet build build-tools\Xamarin.Android.Tools.BootstrapTasks\Xamarin.Android.Tools.BootstrapTasks.csproj -v:minimal
  • git submodule update --init -- external/libunwind

Validation run:

  • dotnet build src\native\native-nativeaot.csproj -p:AndroidSupportedTargetJitAbis=arm64-v8a -p:AndroidSupportedTargetAotAbis=arm64 -v:minimal
  • dotnet build src\native\native-clr.csproj -p:AndroidSupportedTargetJitAbis=arm64-v8a -p:AndroidSupportedTargetAotAbis=arm64 -v:minimal
  • rg "std::format" src\native\clr\host\bridge-processing.cc (no matches)
  • git diff --check

No focused existing test target covers this internal OSBridge formatting boundary.

simonrozsival and others added 29 commits July 17, 2026 00:56
Introduce printf-style native logging and abort helpers while preserving the existing std::format APIs for MonoVM and CoreCLR. Migrate the NativeAOT-specific formatted call sites to the new primitives.

Refs #12139

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 187b207a-083b-461e-9071-e9aab61c9d07
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Keep the printf logging PR focused on the runtime implementation instead of introducing new host-native logging test infrastructure.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Convert NativeAOT-reachable environment, file, mmap, and shared-host diagnostics to the printf helpers introduced by #12140.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Convert host environment and integer parsing diagnostics to #12140 printf helpers while preserving MonoVM formatting branches.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 70f63eb7-6599-414c-a947-d860705aa0fa
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 0f0df1cb-536b-4c53-902e-cbbb9d8e9a15
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 0f0df1cb-536b-4c53-902e-cbbb9d8e9a15
Resolve the logging declaration conflicts by retaining the lightweight canonical header, and preserve the reviewed NativeAOT status string constants.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 0f0df1cb-536b-4c53-902e-cbbb9d8e9a15
Include the lightweight declaration header from both implementations and the printf-only abort helper so signatures remain checked without adding the std::format surface.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 0f0df1cb-536b-4c53-902e-cbbb9d8e9a15
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: cd41994d-0fbf-4ce3-be3d-50e5ad9b74fb
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: cd41994d-0fbf-4ce3-be3d-50e5ad9b74fb
Use the APK description produced by the failed macOS BuildReleaseArm64 XForms CoreCLR test in Azure DevOps build 1520455.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: cd41994d-0fbf-4ce3-be3d-50e5ad9b74fb
Convert the remaining std::format-style GC bridge and logger diagnostics compiled into the NativeAOT host to the canonical printf logging helpers.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 13af6b96-9e20-422c-b3ba-8cba7f1aa5bb
Return static segment error messages as C strings so printf logging can consume them directly without a string_view.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 13af6b96-9e20-422c-b3ba-8cba7f1aa5bb
Convert the remaining easy formatted GC bridge diagnostics reachable by NativeAOT to the canonical shared printf logging APIs while preserving gating, no-check behavior, formatting, and null handling.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4aed776f-8743-45ba-b96b-079253595056
Express the static string-segment error mapping as a compile-time function now that it returns string literals.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 13af6b96-9e20-422c-b3ba-8cba7f1aa5bb
…gging-next' into jonathanpeppers-finish-gc-bridge-printf
Add an internal printf-style OSBridge path for shared CoreCLR and NativeAOT bridge processing while preserving the existing managed string API and MonoVM behavior.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f05fc3ac-b656-4f49-9b28-4bf39fad49c3
Copilot AI review requested due to automatic review settings July 23, 2026 20:56
Name the internal printf helper _monodroid_gref_logf to match the surrounding gref logging convention.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f05fc3ac-b656-4f49-9b28-4bf39fad49c3

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds an internal printf-style gref logging helper for the shared CoreCLR/NativeAOT GC bridge, and migrates the remaining std::format-based gref messages in bridge-processing.cc to avoid libc++ formatting dependencies while preserving existing gref logging behavior.

Changes:

  • Introduce OSBridge::gref_logf(const char* format, ...) which logs to logcat via log_writev() and to the gref file via vfprintf(), using va_copy() for dual-sink consumption.
  • Replace three std::format call sites in src/native/clr/host/bridge-processing.cc with gref_logf() and PRIxPTR-based pointer formatting.
  • Add <cinttypes> include to support PRIxPTR.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/native/clr/include/host/os-bridge.hh Declares new internal OSBridge::gref_logf() printf-style gref logging API.
src/native/clr/host/os-bridge.cc Implements gref_logf() to write to logcat and the gref file without allocating intermediate strings.
src/native/clr/host/bridge-processing.cc Migrates three gref log messages from std::format to gref_logf() with pointer-width hex output.

Comment thread src/native/clr/host/os-bridge.cc
Base automatically changed from jonathanpeppers-finish-gc-bridge-printf to main July 27, 2026 12:29
@jonathanpeppers

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Android PR Reviewer completed successfully!

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 Android PR Review — ✅ LGTM (with 1 minor suggestion)

Clean, well-scoped port of three std::format gref-logging calls to the new printf-style OSBridge::_monodroid_gref_logf() path for the shared CoreCLR/NativeAOT GC bridge.

Verified

  • Logcat gating preserved. The original _monodroid_gref_log() gated logcat on Logger::gref_to_logcat() and delegated the log_categories & LOG_GREF check to log_debugf(). Since log_writev() does not perform that internal check, the new function correctly replicates it inline via (log_categories & LOG_GREF) != 0. Net behavior is identical. ✅
  • File-sink behavior preserved. The gref-file write remains ungated by log_categories (only null-checked), matching the original. Caching Logger::gref_log() in a local is a nice small cleanup. ✅
  • va_copy correctness. Each sink consumes its own va_list (logcat_args copied for log_writev, original args for vfprintf), with matching va_end on every path including the null-format case. ✅
  • Format output. 0x%" PRIxPTR on uintptr_t reproduces the lowercase 0x-prefixed pointer-width hex of {:#x}, including 0x0 for null handles — and using uintptr_t avoids the signed -0x... rendering {:#x} would produce for high addresses. ✅
  • Text/punctuation/newlines on all three call sites match the originals (trailing \n retained where present, omitted for the "collected by a Java GC" message). ✅
  • Attribute format (printf, 1, 2) is correct for a static member function (no implicit this). ✅
  • <cinttypes> include added for PRIxPTR. ✅

Notes

  • CI shows no completed checks yet (status pending) — merge should wait for the pipeline to go green.
  • One 💡 consistency suggestion posted inline (optional use of optional_string).

No bugs, safety, or performance concerns found.

Generated by Android PR Reviewer for #12218 · 88.2 AIC · ⌖ 12.9 AIC · ⊞ 6.9K
Comment /review to run again

Comment thread src/native/clr/host/os-bridge.cc Outdated
Aligns _monodroid_gref_logf() and log_itf() with the existing
_monodroid_gref_log() null-handling style.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 34ecb766-db70-4277-a9b7-a235bc4468ad
@jonathanpeppers
jonathanpeppers enabled auto-merge (squash) July 27, 2026 19:44
@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Jul 27, 2026
@jonathanpeppers
jonathanpeppers merged commit 0f8f8fe into main Jul 28, 2026
44 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers-finish-gref-log-formatting branch July 28, 2026 07:09
@jonathanpeppers jonathanpeppers mentioned this pull request Aug 12, 2026
20 tasks
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants