|
| 1 | +using System.IO; |
| 2 | +using System.Text; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | + |
| 5 | +using Java.Interop; |
| 6 | + |
| 7 | +namespace Microsoft.Android.Runtime; |
| 8 | + |
| 9 | +struct DiagnosticSettings { |
| 10 | + |
| 11 | + public bool LogJniLocalReferences; |
| 12 | + private string? LrefPath; |
| 13 | + |
| 14 | + public bool LogJniGlobalReferences; |
| 15 | + private string? GrefPath; |
| 16 | + |
| 17 | + private TextWriter? GrefLrefLog; |
| 18 | + |
| 19 | + |
| 20 | + public TextWriter? GrefLog { |
| 21 | + get { |
| 22 | + if (!LogJniGlobalReferences) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + return ((LrefPath != null && LrefPath == GrefPath) |
| 26 | + ? GrefLrefLog ??= CreateWriter (LrefPath) |
| 27 | + : null) |
| 28 | + ?? |
| 29 | + ((GrefPath != null) |
| 30 | + ? CreateWriter (GrefPath) |
| 31 | + : null) |
| 32 | + ?? |
| 33 | + new LogcatTextWriter (AndroidLogLevel.Debug, "NativeAot:GREF"); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public TextWriter? LrefLog { |
| 38 | + get { |
| 39 | + if (!LogJniLocalReferences) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + return ((LrefPath != null && LrefPath == GrefPath) |
| 43 | + ? GrefLrefLog ??= CreateWriter (LrefPath) |
| 44 | + : null) |
| 45 | + ?? |
| 46 | + ((LrefPath != null) |
| 47 | + ? CreateWriter (LrefPath) |
| 48 | + : null) |
| 49 | + ?? |
| 50 | + new LogcatTextWriter (AndroidLogLevel.Debug, "NativeAot:LREF"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + TextWriter? CreateWriter (string path) |
| 55 | + { |
| 56 | + try { |
| 57 | + return File.CreateText (path); |
| 58 | + } |
| 59 | + catch (Exception e) { |
| 60 | + AndroidLog.Print (AndroidLogLevel.Error, "NativeAot", $"Failed to open log file `{path}`: {e}"); |
| 61 | + return null; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void AddDebugDotnetLog () |
| 66 | + { |
| 67 | + Span<byte> value = stackalloc byte [RuntimeNativeMethods.PROP_VALUE_MAX]; |
| 68 | + if (!RuntimeNativeMethods.TryGetSystemProperty ("debug.dotnet.log"u8, ref value)) { |
| 69 | + return; |
| 70 | + } |
| 71 | + AddParse (value); |
| 72 | + } |
| 73 | + |
| 74 | + void AddParse (ReadOnlySpan<byte> value) |
| 75 | + { |
| 76 | + while (TryGetNextValue (ref value, out var v)) { |
| 77 | + if (v.SequenceEqual ("lref"u8)) { |
| 78 | + LogJniLocalReferences = true; |
| 79 | + } |
| 80 | + else if (v.StartsWith ("lref="u8)) { |
| 81 | + LogJniLocalReferences = true; |
| 82 | + var path = v.Slice ("lref=".Length); |
| 83 | + LrefPath = Encoding.UTF8.GetString (path); |
| 84 | + } |
| 85 | + else if (v.SequenceEqual ("gref"u8)) { |
| 86 | + LogJniGlobalReferences = true; |
| 87 | + } |
| 88 | + else if (v.StartsWith ("gref="u8)) { |
| 89 | + LogJniGlobalReferences = true; |
| 90 | + var path = v.Slice ("gref=".Length); |
| 91 | + GrefPath = Encoding.UTF8.GetString (path); |
| 92 | + } |
| 93 | + else if (v.SequenceEqual ("all"u8)) { |
| 94 | + LogJniLocalReferences = true; |
| 95 | + LogJniGlobalReferences = true; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + bool TryGetNextValue (ref ReadOnlySpan<byte> value, out ReadOnlySpan<byte> next) |
| 100 | + { |
| 101 | + if (value.Length == 0) { |
| 102 | + next = default; |
| 103 | + return false; |
| 104 | + } |
| 105 | + int c = value.IndexOf ((byte) ','); |
| 106 | + if (c >= 0) { |
| 107 | + next = value.Slice (0, c); |
| 108 | + value = value.Slice (c + 1); |
| 109 | + } |
| 110 | + else { |
| 111 | + next = value; |
| 112 | + value = default; |
| 113 | + } |
| 114 | + return true; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +static partial class RuntimeNativeMethods { |
| 120 | + |
| 121 | + [LibraryImport ("c", EntryPoint="__system_property_get")] |
| 122 | + static private partial int system_property_get (ReadOnlySpan<byte> name, Span<byte> value); |
| 123 | + |
| 124 | + internal const int PROP_VALUE_MAX = 92; |
| 125 | + |
| 126 | + internal static bool TryGetSystemProperty (ReadOnlySpan<byte> name, ref Span<byte> value) |
| 127 | + { |
| 128 | + int len = system_property_get (name, value); |
| 129 | + if (len <= 0) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + value = value.Slice (0, len); |
| 134 | + return true; |
| 135 | + } |
| 136 | +} |
0 commit comments