forked from ppy/osu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrashDiagnostics.cs
More file actions
418 lines (376 loc) · 19.5 KB
/
Copy pathCrashDiagnostics.cs
File metadata and controls
418 lines (376 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Debug = System.Diagnostics.Debug;
using osu.Android.Native;
using System.Collections.Concurrent;
namespace osu.Android
{
/// <summary>
/// Centralised Android crash-diagnostics plumbing.
///
/// We write everything to <b>both</b> internal app storage (<c>FilesDir</c>) and external
/// app storage (<c>GetExternalFilesDir(null)</c>) when both are available. Internal is the
/// reliable target for the very-early window where external storage may not yet be ready;
/// external is reachable by the user via the Files app on an unrooted device and receives
/// alive markers / managed-exception dumps in real time so the user does not have to wait
/// for a successful next startup to mirror the data over.
///
/// Files (relative to each storage dir):
/// <list type="bullet">
/// <item><c>native_crash.log</c> — append target for both the native handler and the managed last-chance hooks; also receives "I am alive" startup markers.</item>
/// <item><c>crash_handler_installed.txt</c> — sentinel dropped immediately after <c>nInstallCrashHandler</c> returns. Lets us distinguish "handler never installed (P/Invoke failed → libosu_native.so missing)" from "handler installed but signal bypassed it".</item>
/// </list>
/// </summary>
internal static class CrashDiagnostics
{
public const string CRASH_LOG_NAME = "native_crash.log";
public const string SENTINEL_NAME = "crash_handler_installed.txt";
private static int initialised;
private static int managedHooksInstalled;
// Global cap on FirstChanceException dumps written per process. A hot-path
// throw loop (e.g. Veldrid "surface lost" thrown every Draw frame while the
// Android Vulkan surface is unavailable during a slow startup) can otherwise
// produce hundreds of full-stack dumps, each one a synchronous file write
// on the throwing thread — which itself stalls the Draw thread and worsens
// the very condition causing the throws.
private const int first_chance_global_cap = 50;
// Per-unique-stack cap. Higher (10) for true fatal kinds caught via
// FirstChanceException-fallback or AppDomain.UnhandledException; lower (3)
// for first-chance noise where seeing the first few occurrences is enough
// to diagnose and the rest are pure log bloat.
private const int per_key_cap_default = 10;
private const int per_key_cap_first_chance = 3;
private static string? internalDir;
private static string? externalDir;
private static readonly ConcurrentDictionary<string, int> exceptionCounts = new ConcurrentDictionary<string, int>();
private static int firstChanceWriteCount;
private static string? sentinelPath;
private static string? installedLogPath;
private static bool sentinelWritten;
/// <summary>
/// Installs the native crash handler against the internal-storage log path, drops the
/// sentinel, and writes the first "I am alive" marker. Idempotent — safe to call
/// repeatedly from <c>Activity.OnCreate</c>; the underlying handler dedupes
/// via its own <c>g_installed</c> flag.
/// </summary>
/// <param name="context">Any <see cref="Context"/> — typically the host Activity.</param>
public static void InstallNativeHandler(Context context)
{
// Idempotent at the managed level: the native handler dedupes via its own
// g_installed flag, but we also avoid re-writing the sentinel and re-running
// the directory-resolution / P-Invoke path on repeat calls.
if (Interlocked.Exchange(ref initialised, 1) != 0)
return;
try
{
resolveDirs(context);
installedLogPath = internalDir != null ? Path.Combine(internalDir, CRASH_LOG_NAME) : null;
// The native handler is best-effort. Wrap so a DllNotFoundException
// (libosu_native.so missing from the APK) cannot itself crash us.
try
{
OboeAudioBridge.nInstallCrashHandler(installedLogPath);
// Sentinel: only written when nInstallCrashHandler returned without throwing.
if (internalDir != null)
{
try
{
sentinelPath = Path.Combine(internalDir, SENTINEL_NAME);
File.WriteAllText(
sentinelPath,
$"installed_at={DateTime.UtcNow:O}\nlog_path={installedLogPath ?? "<none>"}\n");
sentinelWritten = true;
}
catch (Exception e) { Debug.WriteLine($"[osu!] Could not write crash-handler sentinel: {e.Message}"); }
}
}
catch (Exception e)
{
// Most likely DllNotFoundException. Already handled defensively elsewhere
// — log to Debug and carry on so startup is unaffected.
Debug.WriteLine($"[osu!] nInstallCrashHandler P/Invoke failed: {e.Message}");
}
}
catch (Exception e)
{
Debug.WriteLine($"[osu!] CrashDiagnostics.InstallNativeHandler outer failure: {e.Message}");
}
}
/// <summary>
/// Re-install the native signal handlers from a later startup phase, after the Mono
/// runtime has installed its own SIGSEGV handler. This is what actually lets us catch
/// JIT-thread null-deref crashes — without it, Mono's handler intercepts the fault
/// first and re-raises via <c>tgkill</c> (visible in tombstones as
/// <c>si_code = SI_TKILL</c>) without ever forwarding to us.
/// </summary>
public static void ReinstallNativeHandler()
{
try
{
OboeAudioBridge.nReinstallCrashHandler();
WriteAliveMarker("CrashDiagnostics.ReinstallNativeHandler (chained on top of Mono)");
}
catch (Exception e)
{
Debug.WriteLine($"[osu!] nReinstallCrashHandler P/Invoke failed: {e.Message}");
}
}
/// <summary>
/// Append a single-line "I am alive" marker to the crash log so that, when we later
/// inspect a truncated/empty file after a crash, the last-written marker pinpoints
/// which startup phase died. Writes to both internal and external storage so the user
/// can pull the file immediately without waiting for a successful next startup to
/// mirror it over.
/// </summary>
public static void WriteAliveMarker(string phase)
{
string line = $"=== ALIVE [{DateTime.UtcNow:O}] {phase} ===\n";
appendToBoth(line);
}
/// <summary>
/// One-shot post-crash mirror: if an internal <c>native_crash.log</c> exists and is
/// non-empty, copy it to external app storage (so the user can pull it via the Files
/// app on an unrooted device) and truncate the internal copy so subsequent runs only
/// surface fresh crashes.
/// </summary>
public static void MirrorInternalLogToExternal()
{
try
{
if (internalDir == null || externalDir == null) return;
string internalPath = Path.Combine(internalDir, CRASH_LOG_NAME);
if (!File.Exists(internalPath)) return;
var info = new FileInfo(internalPath);
if (info.Length == 0) return;
string externalPath = Path.Combine(externalDir, CRASH_LOG_NAME);
try
{
// Append, not overwrite — keep external as the running historical log.
using (var src = new FileStream(internalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var dst = new FileStream(externalPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
src.CopyTo(dst);
dst.Flush();
}
}
catch (Exception e)
{
Debug.WriteLine($"[osu!] Could not mirror internal crash log to external storage: {e.Message}");
return;
}
// Truncate internal so next-startup markers start fresh.
try { File.WriteAllText(internalPath, string.Empty); }
catch (Exception e) { Debug.WriteLine($"[osu!] Could not truncate internal crash log: {e.Message}"); }
}
catch (Exception e)
{
Debug.WriteLine($"[osu!] MirrorInternalLogToExternal failed: {e.Message}");
}
}
/// <summary>
/// Hook the .NET last-chance exception paths. Mono's default unhandled-exception
/// behaviour prints to logcat and aborts; on user devices that printout is lost.
/// Catching it ourselves and writing to disk gives us the full managed stack —
/// which is what we actually need for the uptime-5s SDLThread crash class.
/// </summary>
public static void InstallManagedExceptionHooks()
{
if (Interlocked.Exchange(ref managedHooksInstalled, 1) != 0)
return;
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
{
writeManagedException("AppDomain.UnhandledException", e.ExceptionObject as Exception);
};
TaskScheduler.UnobservedTaskException += (_, e) =>
{
writeManagedException("TaskScheduler.UnobservedTaskException", e.Exception);
// Don't mark observed — the framework / sentry pipeline still wants to see it.
};
// FirstChanceException fires for *every* managed exception, even ones that get
// caught later. On non-main managed threads (e.g. the Draw thread), Mono on
// Android does not always route an unhandled exception through
// AppDomain.UnhandledException before aborting — so without this hook the
// exception that ultimately kills the process can vanish without trace. We
// record it here on every throw so the *last* recorded exception before a
// SIGSEGV/SIGABRT is the candidate culprit.
//
// Filtering policy:
// * On osu.Framework GameThreads (Draw/Update/Audio/Input): log *every*
// exception. An unhandled throw on any of these threads will tear down
// the process via Mono's tgkill(SIGSEGV) path with no managed trace
// reaching AppDomain.UnhandledException, so we cannot afford to filter.
// * On all other threads: keep the legacy "fatal-ish kinds" type filter
// so the log is not flooded by routine first-chance noise (e.g. the
// HidSharp / CFStringCreateWithCharacters EntryPointNotFoundException
// that fires every startup on .NET TP Worker).
try
{
AppDomain.CurrentDomain.FirstChanceException += (_, e) =>
{
bool isGameThread = isOsuGameThread(Thread.CurrentThread.Name);
bool isFatalKind = e.Exception is NullReferenceException
or AccessViolationException
or StackOverflowException
or TypeInitializationException
or DllNotFoundException
or EntryPointNotFoundException
or BadImageFormatException
or TypeLoadException
or MissingMethodException
or MissingFieldException
or InvalidProgramException;
if (isGameThread || isFatalKind)
{
writeManagedException($"FirstChanceException ({e.Exception.GetType().Name})", e.Exception);
}
};
}
catch (Exception e)
{
Debug.WriteLine($"[osu!] Could not install FirstChanceException hook: {e.Message}");
}
}
// osu.Framework names its game threads with stable prefixes such as
// "DrawThread", "UpdateThread", "AudioThread", "InputThread", and the
// tombstone we are debugging shows the comm name "Draw (GameThread)".
// Match any of these so an exception thrown deep inside the renderer or
// audio pipeline gets captured before Mono aborts the process.
private static bool isOsuGameThread(string? name)
{
if (string.IsNullOrEmpty(name))
return false;
return name.StartsWith("Draw", StringComparison.Ordinal)
|| name.StartsWith("Update", StringComparison.Ordinal)
|| name.StartsWith("Audio", StringComparison.Ordinal)
|| name.StartsWith("Input", StringComparison.Ordinal)
|| name.Contains("GameThread", StringComparison.Ordinal);
}
/// <summary>
/// Records a one-line summary of the native handler install state (sentinel exists?
/// log path?) so the very first thing we see in the log on the next inspection tells
/// us whether the native handler is even in place.
/// </summary>
public static void WriteInstallState()
{
try
{
string sentinelState;
if (sentinelWritten && sentinelPath != null && File.Exists(sentinelPath))
sentinelState = "present";
else if (sentinelWritten)
sentinelState = "written-but-missing";
else
sentinelState = "absent";
appendToBoth($"=== INSTALL_STATE sentinel={sentinelState} log_path={installedLogPath ?? "<none>"} internal_dir={internalDir ?? "<none>"} external_dir={externalDir ?? "<none>"} ===\n");
}
catch (Exception e)
{
Debug.WriteLine($"[osu!] WriteInstallState failed: {e.Message}");
}
}
private static void writeManagedException(string source, Exception? ex)
{
if (ex is EntryPointNotFoundException && ex.Message.Contains("CFStringCreateWithCharacters"))
return;
bool isFirstChance = source.StartsWith("FirstChanceException", StringComparison.Ordinal);
// Global cap on first-chance dumps: a hot-path throw loop on the Draw
// thread can otherwise produce unbounded synchronous file writes, which
// themselves stall the Draw thread and worsen the surface-acquisition
// problem that caused the throws.
if (isFirstChance && Interlocked.Increment(ref firstChanceWriteCount) > first_chance_global_cap)
return;
int perKeyCap = isFirstChance ? per_key_cap_first_chance : per_key_cap_default;
string key = $"{source}_{ex?.GetType().Name}_{ex?.StackTrace?.GetHashCode() ?? 0}";
if (exceptionCounts.AddOrUpdate(key, 1, (_, count) => count + 1) > perKeyCap)
return;
try
{
string block =
"\n=========================================================\n" +
"=== MANAGED EXCEPTION ===\n" +
$" source = {source}\n" +
$" utc_time = {DateTime.UtcNow:O}\n" +
$" thread_id = {Environment.CurrentManagedThreadId}\n" +
$" thread_name= {Thread.CurrentThread.Name ?? "<null>"}\n" +
"\n" +
(ex?.ToString() ?? "<no exception object>") + "\n" +
"=== END OF MANAGED EXCEPTION ===\n\n";
// For FirstChanceException we deliberately skip the external/FUSE
// write — those writes are tens of milliseconds each and run on the
// throwing thread (often the Draw thread). MirrorInternalLogToExternal
// copies the internal log to external on the next startup, which is
// sufficient for user-facing diagnostics without risking a Draw-thread
// stall in the live process.
if (isFirstChance)
tryAppend(internalDir, block);
else
appendToBoth(block);
}
catch (Exception e)
{
Debug.WriteLine($"[osu!] writeManagedException failed: {e.Message}");
}
}
// Append the same payload to both internal (FilesDir) and external (GetExternalFilesDir)
// crash logs. Either may legitimately be unavailable; failure of one path must not
// prevent the other from being written. Each write is bounded, non-blocking, and
// never throws out of this method — diagnostics must never themselves crash.
private static void appendToBoth(string payload)
{
tryAppend(internalDir, payload);
tryAppend(externalDir, payload);
}
/// <summary>
/// Public entry point for other components (e.g. <c>HangWatchdog</c>) to append
/// a diagnostic block into the same internal+external <c>native_crash.log</c>
/// targets that the native handler and managed exception hooks write to.
/// Never throws.
/// </summary>
public static void AppendDiagnosticBlock(string payload) => appendToBoth(payload);
private static void tryAppend(string? dir, string payload)
{
if (dir == null) return;
try
{
string path = Path.Combine(dir, CRASH_LOG_NAME);
using var fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
using var sw = new StreamWriter(fs);
sw.Write(payload);
sw.Flush();
}
catch (Exception e)
{
Debug.WriteLine($"[osu!] CrashDiagnostics.tryAppend({dir}) failed: {e.Message}");
}
}
private static void resolveDirs(Context context)
{
try
{
if (internalDir == null)
{
var f = context.FilesDir;
if (f != null && !string.IsNullOrEmpty(f.AbsolutePath))
internalDir = f.AbsolutePath;
}
}
catch (Exception e) { Debug.WriteLine($"[osu!] Could not resolve internal FilesDir: {e.Message}"); }
try
{
if (externalDir == null)
{
var e = context.GetExternalFilesDir(null);
if (e != null && !string.IsNullOrEmpty(e.AbsolutePath))
externalDir = e.AbsolutePath;
}
}
catch (Exception ex) { Debug.WriteLine($"[osu!] Could not resolve external files dir: {ex.Message}"); }
}
}
}