Add opt-in game debug logs and bounded log sharing

This commit is contained in:
wallentx committed 2026-10-18 02:22:15 -05:00
1 parent 798386d02a
commit 6521c3c3ad
19 files changed
+825 -74

No files matched your search

@@ -44,6 +44,7 @@ import com.droiddeck.launcher.session.ComponentsManager
import com.droiddeck.launcher.ui.ComponentsPage
import com.droiddeck.launcher.session.GameSaves
import com.droiddeck.launcher.session.SessionLogShare
import com.droiddeck.launcher.session.SessionPaths
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SteamRepair
import com.droiddeck.launcher.core.CpuCores
@@ -406,6 +407,8 @@ class MainActivity : ComponentActivity() {
private var emulatorList by mutableStateOf<List<Library.Emulator>>(emptyList())
private var runningLabel by mutableStateOf<String?>(null)
private var logsEnabled by mutableStateOf(true)
private var gameDebugLogsEnabled by mutableStateOf(false)
private var gameDebugLogsNeedsRestart by mutableStateOf(false)
private var vacDiagnostics by mutableStateOf(false)
private var agentCommands by mutableStateOf(false)
private var showRoms by mutableStateOf(false)
@@ -767,6 +770,7 @@ class MainActivity : ComponentActivity() {
desktopInstalled = desktopInstalled, desktopPresent = desktopPresent,
offlineAccount = offlineAccount, offline = offline,
frameGenLabel = frameGenLabel, romsDir = romsDir, logsEnabled = logsEnabled,
gameDebugLogsEnabled = gameDebugLogsEnabled, gameDebugLogsNeedsRestart = gameDebugLogsNeedsRestart,
vacDiagnostics = vacDiagnostics, agentCommands = agentCommands,
steamGames = steamGames, steamGamesX64 = steamGamesX64, emulators = emulatorList, running = runningLabel,
frameGen = FrameGen.mode(this),
@@ -916,6 +920,13 @@ class MainActivity : ComponentActivity() {
onLogs = {
SessionPrefs.setLogsEnabled(this, !SessionPrefs.logsEnabled(this))
logsEnabled = SessionPrefs.logsEnabled(this)
gameDebugLogsEnabled = SessionPrefs.gameDebugLogsEnabled(this)
gameDebugLogsNeedsRestart = gameDebugLogsNeedsRestart()
},
onGameDebugLogs = {
SessionPrefs.setGameDebugLogsEnabled(this, !SessionPrefs.gameDebugLogsEnabled(this))
gameDebugLogsEnabled = SessionPrefs.gameDebugLogsEnabled(this)
gameDebugLogsNeedsRestart = gameDebugLogsNeedsRestart()
},
onVacDiagnostics = {
SessionPrefs.setVacDiagnostics(this, !SessionPrefs.vacDiagnostics(this))
@@ -1875,6 +1886,10 @@ class MainActivity : ComponentActivity() {
refreshPhantomStatus()
}
private fun gameDebugLogsNeedsRestart(): Boolean =
SessionPrefs.logsEnabled(this) && SessionPrefs.gameDebugLogsEnabled(this) &&
!SessionPaths.currentSessionLogsRetained(this)
private fun refresh() {
// Until the scan finishes, the other client's games would launch in this one.
val listX64 = SessionPrefs.activeSteamClientArch(this) == "x86_64"
@@ -1895,6 +1910,8 @@ class MainActivity : ComponentActivity() {
frameGenLabel = FrameGen.label(this)
romsDir = SessionPrefs.romsDir(this).takeIf { it.isNotEmpty() }
logsEnabled = SessionPrefs.logsEnabled(this)
gameDebugLogsEnabled = SessionPrefs.gameDebugLogsEnabled(this)
gameDebugLogsNeedsRestart = gameDebugLogsNeedsRestart()
vacDiagnostics = SessionPrefs.vacDiagnostics(this)
agentCommands = com.droiddeck.launcher.agent.AgentAccess.commandsEnabled(this)
runningLabel = if (SessionState.running) when (SessionState.mode) {
@@ -1,6 +1,52 @@
package com.droiddeck.launcher.core
object LogRedactor {
internal const val MAX_LINE_LENGTH = 64 * 1024
internal const val OVERSIZED_LINE = "<oversized log line withheld>"
private data class MatcherKey(val pattern: String, val options: Set<RegexOption>)
private class MatcherCache : LinkedHashMap<MatcherKey, java.util.regex.Matcher>(32, 0.75f, true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<MatcherKey, java.util.regex.Matcher>?): Boolean =
size > 32
}
// Android matchers own native state, so reuse them instead of creating one for every log line.
private val matchers = object : ThreadLocal<MatcherCache>() {
override fun initialValue(): MatcherCache = MatcherCache()
}
private fun <T> withMatcher(
regex: Regex,
input: CharSequence,
action: (java.util.regex.Matcher) -> T,
): T {
val key = MatcherKey(regex.pattern, regex.options)
val matcher = matchers.get()!!.getOrPut(key) { regex.toPattern().matcher("") }
return try {
action(matcher.reset(input))
} finally {
matcher.reset("")
}
}
private fun replaceMatches(input: String, regex: Regex, replacement: String): String =
replaceMatches(input, regex) { replacement }
private fun replaceMatches(
input: String,
regex: Regex,
replacement: (java.util.regex.Matcher) -> String,
): String = withMatcher(regex, input) { matcher ->
if (!matcher.find()) return@withMatcher input
val out = StringBuffer(input.length)
do {
matcher.appendReplacement(out, java.util.regex.Matcher.quoteReplacement(replacement(matcher)))
} while (matcher.find())
matcher.appendTail(out)
out.toString()
}
private val EMAIL = Regex("[A-Za-z0-9._%+\\-]+@[A-Za-z0-9.\\-]+\\.[A-Za-z]{2,}")
private val JWT_LABELLED = Regex("(?i)(\\bJWT[\\s=:]+)(\\d{8,})")
private val JWT_BASE64 = Regex("ey[A-Za-z0-9_\\-]{6,}\\.[A-Za-z0-9_\\-]{6,}\\.[A-Za-z0-9_\\-]{6,}")
@@ -74,6 +120,7 @@ object LogRedactor {
java.io.File(root, "root/.local/share/Steam/config/loginusers.vdf"),
java.io.File(root, "root/.droiddeck-x64/.local/share/Steam/config/loginusers.vdf"),
)
matchers.remove()
}
@Volatile
@@ -141,13 +188,13 @@ object LogRedactor {
}
fun describeAddresses(text: String): String {
var out = IPV6.replace(text) { m -> kind(m.value)?.let { "<$it>" } ?: m.value }
out = IPV4.replace(out) { m -> kind(m.value)?.let { "<$it>" } ?: m.value }
var out = replaceMatches(text, IPV6) { m -> kind(m.group())?.let { "<$it>" } ?: m.group() }
out = replaceMatches(out, IPV4) { m -> kind(m.group())?.let { "<$it>" } ?: m.group() }
return out
}
fun isText(file: java.io.File): Boolean = try {
file.isFile && file.length() < 64L * 1024 * 1024 && file.inputStream().use { input ->
file.isFile && file.inputStream().use { input ->
val buf = ByteArray(4096)
val n = input.read(buf)
n <= 0 || (0 until n).none { buf[it].toInt() == 0 }
@@ -163,21 +210,68 @@ object LogRedactor {
)
fun scrubTo(src: java.io.File, out: java.io.Writer) {
src.forEachLine { line -> out.write(redact(line)); out.write("\n") }
scrubLines(src, out, ::redact)
}
fun redactForShare(line: String): String {
if (line.isEmpty()) return line
if (line.length > MAX_LINE_LENGTH) return OVERSIZED_LINE
return try {
val out = SecretScrub.scrub(redact(line), SecretScrub.Urls.KEEP_PATH, "<redacted:token>")
LAN_IPV4.replace(out, "<lan-address>")
replaceMatches(out, LAN_IPV4, "<lan-address>")
} catch (t: Throwable) {
"<redaction failed; line withheld>"
}
}
fun scrubForShare(src: java.io.File, out: java.io.Writer) {
src.forEachLine { line -> out.write(redactForShare(line)); out.write("\n") }
scrubLines(src, out, ::redactForShare)
}
private fun scrubLines(src: java.io.File, out: java.io.Writer, scrub: (String) -> String) {
src.bufferedReader().use { input ->
val chunk = CharArray(8192)
val line = StringBuilder(4096)
var oversized = false
var pendingContent = false
var pendingCarriageReturn = false
fun emit() {
out.write(if (oversized) OVERSIZED_LINE else scrub(line.toString()))
out.write("\n")
line.setLength(0)
oversized = false
pendingContent = false
}
while (true) {
val count = input.read(chunk)
if (count < 0) break
for (i in 0 until count) {
val char = chunk[i]
if (pendingCarriageReturn) {
emit()
pendingCarriageReturn = false
if (char == '\n') continue
}
when (char) {
'\r' -> pendingCarriageReturn = true
'\n' -> emit()
else -> {
pendingContent = true
if (!oversized) {
if (line.length < MAX_LINE_LENGTH) line.append(char)
else {
line.setLength(0)
oversized = true
}
}
}
}
}
}
if (pendingCarriageReturn || pendingContent) emit()
}
}
private fun quoted(value: String): String {
@@ -219,43 +313,44 @@ object LogRedactor {
fun redact(line: String): String {
if (line.isEmpty()) return line
if (line.length > MAX_LINE_LENGTH) return OVERSIZED_LINE
return try {
val out = Line(line)
if (out.has("mac")) out.set(MAC_FIELD.replace(out.text) { "${it.groupValues[1]}<redacted:mac>" })
if (out.holds(':', 5) || out.holds('-', 5)) out.set(MAC.replace(out.text, "<redacted:mac>"))
if (out.has("serial", "android")) out.set(DEVICE_IDENTIFIER.replace(out.text) {
val value = it.groupValues[2]
if (out.has("mac")) out.set(replaceMatches(out.text, MAC_FIELD) { "${it.group(1)}<redacted:mac>" })
if (out.holds(':', 5) || out.holds('-', 5)) out.set(replaceMatches(out.text, MAC, "<redacted:mac>"))
if (out.has("serial", "android")) out.set(replaceMatches(out.text, DEVICE_IDENTIFIER) {
val value = it.group(2)
val quote = value.first().takeIf { char -> char == '\"' || char == '\'' }?.toString().orEmpty()
"${it.groupValues[1]}${quote}<redacted:serial>${quote}"
"${it.group(1)}${quote}<redacted:serial>${quote}"
})
if (out.holds('-', 4)) out.set(GUID.replace(out.text, "<redacted:guid>"))
if (out.has("jwt")) out.set(JWT_LABELLED.replace(out.text) { "${it.groupValues[1]}<redacted:jwt>" })
if ("ey" in out.text) out.set(JWT_BASE64.replace(out.text, "<redacted:jwt>"))
if (out.holds('-', 4)) out.set(replaceMatches(out.text, GUID, "<redacted:guid>"))
if (out.has("jwt")) out.set(replaceMatches(out.text, JWT_LABELLED) { "${it.group(1)}<redacted:jwt>" })
if ("ey" in out.text) out.set(replaceMatches(out.text, JWT_BASE64, "<redacted:jwt>"))
if (out.has("token", "auth", "ticket", "sessionid", "steamloginsecure", "api", "machine", "pass", "pwd", "secret")) {
out.set(SECRET_KV.replace(out.text) { "${it.groupValues[1]}${it.groupValues[2]}<redacted:token>" })
out.set(replaceMatches(out.text, SECRET_KV) { "${it.group(1)}${it.group(2)}<redacted:token>" })
}
if (out.has("guard", "factor", "2fa")) out.set(GUARD_CODE.replace(out.text) { "${it.groupValues[1]}<redacted:code>" })
if (out.hasRun(32, ::isHex)) out.set(WEBAPI_KEY.replace(out.text, "<redacted:key>"))
if ("76561" in out.text) out.set(STEAMID64.replace(out.text) { "${it.groupValues[1]}********${it.groupValues[3]}" })
if ("[U:1:" in out.text) out.set(STEAMID3.replace(out.text) { m ->
val id = m.groupValues[1]
if (out.has("guard", "factor", "2fa")) out.set(replaceMatches(out.text, GUARD_CODE) { "${it.group(1)}<redacted:code>" })
if (out.hasRun(32, ::isHex)) out.set(replaceMatches(out.text, WEBAPI_KEY, "<redacted:key>"))
if ("76561" in out.text) out.set(replaceMatches(out.text, STEAMID64) { "${it.group(1)}********${it.group(3)}" })
if ("[U:1:" in out.text) out.set(replaceMatches(out.text, STEAMID3) { m ->
val id = m.group(1)
"[U:1:${if (id.length > 4) "*".repeat(id.length - 4) + id.takeLast(4) else id}]"
})
if (out.has("external")) out.set(EXTERNAL_ADDR.replace(out.text) { "${it.groupValues[1]}<redacted:ip>" })
for (r in own) if (r.needs == null || out.has(r.needs)) out.set(r.regex.replace(out.text, "<redacted:ip>"))
if (out.has("onloginstatechange")) out.set(LOGIN_STATE.replace(out.text) { "${it.groupValues[1]}<redacted:account>" })
if ("OnLoginUsersChanged " in out.text) out.set(LOGIN_USERS.replace(out.text) { "${it.groupValues[1]}<redacted:account>" })
if (out.has("external")) out.set(replaceMatches(out.text, EXTERNAL_ADDR) { "${it.group(1)}<redacted:ip>" })
for (r in own) if (r.needs == null || out.has(r.needs)) out.set(replaceMatches(out.text, r.regex, "<redacted:ip>"))
if (out.has("onloginstatechange")) out.set(replaceMatches(out.text, LOGIN_STATE) { "${it.group(1)}<redacted:account>" })
if ("OnLoginUsersChanged " in out.text) out.set(replaceMatches(out.text, LOGIN_USERS) { "${it.group(1)}<redacted:account>" })
if (out.has("account", "user")) {
out.set(ACCOUNT_KV.replace(out.text) { "${it.groupValues[1]}${it.groupValues[2]}${quoted(it.groupValues[3])}" })
out.set(replaceMatches(out.text, ACCOUNT_KV) { "${it.group(1)}${it.group(2)}${quoted(it.group(3))}" })
}
if (out.has("login")) out.set(LOGIN_KV.replace(out.text) { "${it.groupValues[1]}${it.groupValues[2]}${quoted(it.groupValues[3])}" })
for (r in accounts) if (r.needs == null || out.has(r.needs)) out.set(r.regex.replace(out.text, "<redacted:account>"))
if ('@' in out.text) out.set(EMAIL.replace(out.text, "<redacted:email>"))
if (out.has("login")) out.set(replaceMatches(out.text, LOGIN_KV) { "${it.group(1)}${it.group(2)}${quoted(it.group(3))}" })
for (r in accounts) if (r.needs == null || out.has(r.needs)) out.set(replaceMatches(out.text, r.regex, "<redacted:account>"))
if ('@' in out.text) out.set(replaceMatches(out.text, EMAIL, "<redacted:email>"))
if (out.has("jwt", "token", "ticket", "sessionid", "steamloginsecure", "machineauth")) {
out.set(RESIDUAL.replace(out.text) { "${it.groupValues[1]}=<redacted:token>" })
out.set(replaceMatches(out.text, RESIDUAL) { "${it.group(1)}=<redacted:token>" })
}
if (out.hasRun(88) { it in 'a'..'z' || it in 'A'..'Z' || it in '0'..'9' || it == '_' || it == '-' }) {
out.set(LONG_TOKEN.replace(out.text, "<redacted:token>"))
out.set(replaceMatches(out.text, LONG_TOKEN, "<redacted:token>"))
}
out.text
} catch (t: Throwable) {
@@ -13,6 +13,7 @@ object GameEnvironmentStore {
private const val GUEST_FILE = "root/.config/droiddeck/game-environment.json"
const val DXVK_CONFIG = "dxvkConfig"
const val DXVK_CONFIG_GAMES = "dxvkConfigGames"
const val GAME_DEBUG_LOGS = "gameDebugLogs"
@Synchronized
fun read(context: Context): GameEnvironment.Config {
@@ -102,6 +103,11 @@ object GameEnvironmentStore {
)
}
if (gameDxvk.length() > 0) json.put(DXVK_CONFIG_GAMES, gameDxvk)
json.put(
GAME_DEBUG_LOGS,
SessionPrefs.logsEnabled(context) && SessionPrefs.gameDebugLogsEnabled(context) &&
SessionPaths.currentSessionLogsRetained(context),
)
write(File(LinuxRuntime.rootDir(context), GUEST_FILE), json.toString())
}
@@ -88,15 +88,10 @@ object SessionArtifacts {
}?.toList() ?: return
if (dirs.isEmpty()) return
LogRedactor.learnFromRuntime(LinuxRuntime.rootDir(context))
val workers = (Runtime.getRuntime().availableProcessors() / 2).coerceIn(1, 4)
val pool = java.util.concurrent.Executors.newFixedThreadPool(workers) { r ->
Thread({ android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); r.run() }, "scrub-logs")
}
val done = try {
pool.invokeAll(dirs.map { dir -> java.util.concurrent.Callable { scrubAndMark(dir) } })
.count { f -> try { f.get() } catch (e: Exception) { false } }
} finally {
pool.shutdown()
var done = 0
for (dir in dirs) {
if (SessionState.running) break
if (scrubAndMark(dir, stopWhenSessionStarts = true)) done++
}
val failed = dirs.size - done
Log.i(TAG, "logs: scrubbed $done folders under r${LogRedactor.RULES_VERSION}" +
@@ -114,14 +109,18 @@ object SessionArtifacts {
private class Record(val dir: File) {
val files = HashMap<File, Scrubbed>()
var failed = false
var deferred = false
}
private fun scrubFolder(dir: File, record: Record) {
dir.listFiles { f -> f.isFile && !f.name.startsWith(".") && f !in record.files }?.forEach { f ->
if (!LogRedactor.isText(f)) return@forEach
private fun scrubFolder(dir: File, record: Record, stopWhenSessionStarts: Boolean): Boolean {
val files = dir.listFiles { f -> f.isFile && !f.name.startsWith(".") && f !in record.files } ?: return true
for (f in files) {
if (stopWhenSessionStarts && SessionState.running) return false
if (!LogRedactor.isText(f)) continue
val done = scrubFile(dir, f)
if (done == null) record.failed = true else record.files[f] = done
}
return true
}
private fun scrubFile(dir: File, f: File): Scrubbed? {
@@ -129,13 +128,16 @@ object SessionArtifacts {
return try {
val before = Scrubbed(f.length(), f.lastModified())
tmp.bufferedWriter().use { w -> LogRedactor.scrubTo(f, w) }
val scrubbed = tmp.readBytes()
if (scrubbed.contentEquals(f.readBytes())) {
val scrubbedLength = tmp.length()
if (sameContents(tmp, f)) {
tmp.delete()
before.takeIf { f.length() == it.length && f.lastModified() == it.modified }
} else {
if (!tmp.renameTo(f)) { f.writeBytes(scrubbed); tmp.delete() }
Scrubbed(scrubbed.size.toLong(), f.lastModified()).takeIf { f.length() == it.length }
if (!tmp.renameTo(f)) {
tmp.inputStream().use { input -> f.outputStream().use { output -> input.copyTo(output, 64 * 1024) } }
tmp.delete()
}
Scrubbed(scrubbedLength, f.lastModified()).takeIf { f.length() == it.length }
}
} catch (e: Exception) {
Log.w(TAG, "could not scrub ${f.name}", e)
@@ -144,6 +146,34 @@ object SessionArtifacts {
}
}
private fun sameContents(a: File, b: File): Boolean {
if (a.length() != b.length()) return false
fun readChunk(input: java.io.InputStream, buffer: ByteArray): Int {
var count = 0
while (count < buffer.size) {
val read = input.read(buffer, count, buffer.size - count)
if (read < 0) break
if (read == 0) continue
count += read
}
return count
}
return a.inputStream().buffered().use { left ->
b.inputStream().buffered().use { right ->
val leftBuffer = ByteArray(64 * 1024)
val rightBuffer = ByteArray(64 * 1024)
while (true) {
val leftCount = readChunk(left, leftBuffer)
val rightCount = readChunk(right, rightBuffer)
if (leftCount != rightCount) return false
if (leftCount == 0) break
for (i in 0 until leftCount) if (leftBuffer[i] != rightBuffer[i]) return false
}
true
}
}
}
private fun markScrubbed(record: Record): Boolean {
val dir = record.dir
val tree = File(dir, SCRUBBED_TREE_MARKER)
@@ -181,10 +211,27 @@ object SessionArtifacts {
}.toSet()
}
internal fun scrubAndMark(dir: File): Boolean = markScrubbed(scrubTree(Record(dir)))
internal fun scrubAndMark(dir: File, stopWhenSessionStarts: Boolean = false): Boolean {
val record = scrubTree(Record(dir), stopWhenSessionStarts)
if (stopWhenSessionStarts && SessionState.running) {
record.failed = true
record.deferred = true
}
return markScrubbed(record)
}
private fun scrubTree(record: Record): Record {
record.dir.walkTopDown().filter { it.isDirectory }.forEach { scrubFolder(it, record) }
private fun scrubTree(record: Record, stopWhenSessionStarts: Boolean = false): Record {
for (dir in record.dir.walkTopDown().filter { it.isDirectory }) {
if (stopWhenSessionStarts && SessionState.running || !scrubFolder(dir, record, stopWhenSessionStarts)) {
record.failed = true
record.deferred = true
break
}
}
if (stopWhenSessionStarts && SessionState.running) {
record.failed = true
record.deferred = true
}
return record
}
@@ -198,16 +245,26 @@ object SessionArtifacts {
internal fun recordedClient(dir: File): String? =
runCatching { File(dir, CLIENT_FILE).readText().trim() }.getOrNull()?.takeIf { it == "arm64" || it == "x86_64" }
private fun copySteamLogs(context: Context, dir: File, record: Record, steamArch: String? = null) {
private fun copySteamLogs(
context: Context,
dir: File,
record: Record,
steamArch: String? = null,
stopWhenSessionStarts: Boolean = false,
) {
val logs = File(steamArch?.let { SessionPrefs.steamRoot(context, it) } ?: SessionPrefs.activeSteamRoot(context), "logs")
if (!logs.isDirectory) return
val out = File(dir, "steam").apply { mkdirs() }
logs.listFiles { f -> f.isFile && f.length() < 8L * 1024 * 1024 }?.forEach { src ->
val sources = logs.listFiles { f -> f.isFile && f.length() < 8L * 1024 * 1024 } ?: return
for (src in sources) {
if (stopWhenSessionStarts && SessionState.running) {
record.failed = true
record.deferred = true
break
}
try {
val dst = File(out, src.name)
dst.bufferedWriter().use { w ->
src.forEachLine { line -> w.write(LogRedactor.redact(line)); w.newLine() }
}
dst.bufferedWriter().use { w -> LogRedactor.scrubTo(src, w) }
record.files[dst] = Scrubbed(dst.length(), dst.lastModified())
} catch (e: Exception) {
Log.w(TAG, "could not scrub ${src.name}", e)
@@ -217,36 +274,65 @@ object SessionArtifacts {
}
@Synchronized
fun finishAbandoned(context: Context) {
fun finishAbandoned(context: Context, beforeGuestStart: Boolean = false) {
val stopWhenSessionStarts = !beforeGuestStart
val parent = LinuxRuntime.logDir(context)
val abandoned = parent.listFiles { f ->
SessionPaths.isSessionFolder(f) && !File(f, COMPLETE_MARKER).exists() && !inFlight(f)
}?.sortedWith(SessionPaths.chronological) ?: return
if (abandoned.isEmpty()) return
val current = SessionPaths.current()
val latestSession = SessionPaths.sessionFolders(context).lastOrNull()
LogRedactor.learnFromRuntime(LinuxRuntime.rootDir(context))
abandoned.forEachIndexed { i, dir ->
if (dir == current) return@forEachIndexed
val newest = i == abandoned.lastIndex
for (dir in abandoned) {
if (stopWhenSessionStarts && SessionState.running) break
if (dir == current) continue
val newest = dir == latestSession
try {
File(dir, "ended-without-teardown.txt").writeText(
"This session's process ended without running its own teardown - killed by\n" +
"Android, a native crash, or the device going down - so the files below were\n" +
"gathered when the app next started, at ${now()}.\n" +
"The logs written during the session (session.log, app.log, wayland.log,\n" +
"audio.log, device.txt) were on disk already and are as they were left.\n" +
(if (newest) "" else "Steam's logs are not included: a later session has overwritten them.\n") +
"crash.log holds Android's crash buffer as of the next app start - if this\n" +
"session died of a crash, the entry is in there unless the device rebooted.\n"
)
val recoveryNote = File(dir, "ended-without-teardown.txt")
val recoveryAttempted = recoveryNote.exists()
if (!recoveryAttempted) {
recoveryNote.writeText(
"This session's process ended without running its own teardown - killed by\n" +
"Android, a native crash, or the device going down - so the files below were\n" +
"gathered when the app next started, at ${now()}.\n" +
"The logs written during the session (session.log, app.log, wayland.log,\n" +
"audio.log, device.txt) were on disk already and are as they were left.\n" +
(if (newest) "" else "Steam's logs are not included: a later session has overwritten them.\n") +
"crash.log holds Android's crash buffer as of the next app start - if this\n" +
"session died of a crash, the entry is in there unless the device rebooted.\n"
)
}
val record = Record(dir)
if (newest) {
copySteamLogs(context, dir, record, recordedClient(dir))
VacDiagnostics.collect(context, dir)
if (newest && !recoveryAttempted) {
copySteamLogs(
context,
dir,
record,
steamArch = recordedClient(dir),
stopWhenSessionStarts = stopWhenSessionStarts,
)
if (!record.deferred && (!stopWhenSessionStarts || !SessionState.running)) {
VacDiagnostics.collect(context, dir)
}
}
if (record.deferred || stopWhenSessionStarts && SessionState.running) {
record.failed = true
record.deferred = true
markScrubbed(record)
break
}
SessionLogCapture.dumpCrashBuffer(File(dir, "crash.log"))
if (stopWhenSessionStarts && SessionState.running) {
record.failed = true
record.deferred = true
markScrubbed(record)
break
}
SessionEvents.record("session.artifacts_recovered", mapOf("newest" to newest), dir)
markScrubbed(scrubTree(record))
val scrubbed = scrubTree(record, stopWhenSessionStarts = stopWhenSessionStarts)
markScrubbed(scrubbed)
if (scrubbed.deferred) break
File(dir, COMPLETE_MARKER).writeText("collected: late, at next app start, ${now()}\n")
Log.i(TAG, "finished the abandoned session folder $dir")
} catch (e: Exception) {
@@ -44,6 +44,10 @@ object SessionPaths {
fun current(): File? = dir
/** Whether the current session folder will survive teardown. No session means the next one can retain logs. */
fun currentSessionLogsRetained(context: Context): Boolean =
dir?.let { it.parentFile == SessionFiles.logDirectory(context) } ?: true
fun lastEnded(): File? = ended?.takeIf { it.isDirectory }
@Synchronized
@@ -443,6 +443,16 @@ object SessionPrefs {
fun setLogsEnabled(context: Context, on: Boolean) {
prefs(context).edit().putBoolean("logs", on).apply()
publishGameEnvironment(context)
}
/** Extra Proton, Wine and graphics diagnostics for the next game launch; off unless chosen. */
fun gameDebugLogsEnabled(context: Context): Boolean =
prefs(context).getBoolean("gameDebugLogsEnabled", false)
fun setGameDebugLogsEnabled(context: Context, on: Boolean) {
prefs(context).edit().putBoolean("gameDebugLogsEnabled", on).apply()
publishGameEnvironment(context)
}
fun storageDiagnosticsEnabled(context: Context): Boolean =
@@ -322,7 +322,7 @@ class SessionService : Service() {
SessionFiles.stage(this, root)
// Earlier sessions' reports still read the Steam logs and VAC marker this session replaces.
SessionArtifacts.awaitCollections()
SessionArtifacts.finishAbandoned(this)
SessionArtifacts.finishAbandoned(this, beforeGuestStart = true)
VacDiagnostics.arm(this)
com.droiddeck.launcher.agent.AgentGuest.reset(this)
com.droiddeck.launcher.stores.gog.GogComet.begin()
@@ -19,7 +19,7 @@ object SettingsReset {
"tuSysmem", "glThread", "noGlError", "zinkLazy", "mangoapp", "gpuClockPin", "gamescopeRealtime",
"directAudio", "mic", "wifiDiscovery", "clientCpusOverride", "clientCpus", "gameCpus",
"noXalia", "fastSync", "fsyncFirst", "syncFallback", "prootNoSeccomp", "prootFastPath",
"guestHostname", "fexPreset", "forceSsbs", "logs", "storageDiagnostics",
"guestHostname", "fexPreset", "forceSsbs", "logs", "gameDebugLogsEnabled", "storageDiagnostics",
"upscaler", "upscaleSharpness", "screenEffects", "textureAnisotropy", "textureLodBias",
"steamDownloadsInBackground", "steam_offline", "bootMovie", "bootMovieSound",
"questDefaultsV1",
@@ -98,6 +98,8 @@ class FrontEndState(
val frameGenLabel: String,
val romsDir: String?,
val logsEnabled: Boolean,
val gameDebugLogsEnabled: Boolean = false,
val gameDebugLogsNeedsRestart: Boolean = false,
val vacDiagnostics: Boolean = false,
val agentCommands: Boolean = false,
val steamGames: List<Library.SteamGame>,
@@ -195,6 +197,7 @@ class FrontEndActions(
val onFiles: () -> Unit,
val onBrowseFiles: (File) -> Unit = {},
val onLogs: () -> Unit,
val onGameDebugLogs: () -> Unit = {},
val onVacDiagnostics: () -> Unit = {},
val onAgentCommands: () -> Unit = {},
val onShareLogs: () -> Unit = {},
@@ -436,6 +436,12 @@ internal fun SetupPanel(
4 -> {
SettingsGroup(stringResource(R.string.setup_tab_diagnostics)) {
ToggleRow(host, "logs", stringResource(R.string.setup_logs), stringResource(R.string.setup_logs_hint), s.logsEnabled) { a.onLogs() }
ToggleRow(
host, "game-debug-logs", stringResource(R.string.setup_game_debug_logs),
stringResource(if (s.gameDebugLogsNeedsRestart) R.string.setup_game_debug_logs_restart else R.string.setup_game_debug_logs_hint),
s.gameDebugLogsEnabled,
enabled = s.logsEnabled,
) { a.onGameDebugLogs() }
ToggleRow(host, "vacdiag", stringResource(R.string.setup_vac_diag), stringResource(R.string.setup_vac_diag_hint), s.vacDiagnostics) { a.onVacDiagnostics() }
ToggleRow(host, "agent-commands", stringResource(R.string.setup_agent_commands), null, s.agentCommands) { a.onAgentCommands() }
ActionRow(stringResource(R.string.setup_latest_logs), stringResource(R.string.drawer_logs_hint), stringResource(R.string.drawer_share_logs), a.onShareLogs,
+3
View File
@@ -731,6 +731,9 @@
<string name="setup_session">Session</string>
<string name="setup_logs">Session logs</string>
<string name="setup_logs_hint">Saved after each session</string>
<string name="setup_game_debug_logs">Game debug logs</string>
<string name="setup_game_debug_logs_hint">Include Proton, Wine and graphics diagnostics on the next game launch. Can reduce performance and use more storage.</string>
<string name="setup_game_debug_logs_restart">Restart this session to save game debug logs</string>
<string name="setup_agent_commands">Debugging tools</string>
<string name="setup_latest_logs">Latest session logs</string>
<string name="setup_saved_logs">Saved session logs</string>
@@ -3,9 +3,14 @@ package com.droiddeck.launcher.core
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.StringWriter
class LogRedactorTest {
@get:Rule val tmp = TemporaryFolder()
@Test fun labelledJwtIsRedacted() {
val out = LogRedactor.redact("Using JWT 25484942796017334")
assertFalse(out.contains("25484942796017334"))
@@ -148,6 +153,71 @@ class LogRedactorTest {
assertEquals(once, LogRedactor.redactForShare(once))
}
@Test fun anOversizedDirectInputIsWithheldBeforeRegex() {
val secret = "sessionid=abcdef123456"
val line = "x".repeat(LogRedactor.MAX_LINE_LENGTH) + secret
assertEquals(LogRedactor.OVERSIZED_LINE, LogRedactor.redact(line))
assertEquals(LogRedactor.OVERSIZED_LINE, LogRedactor.redactForShare(line))
}
@Test fun streamWithholdsAnOversizedLogicalLineWholeAndContinues() {
val secret = "sessionid=abcdef123456"
val src = tmp.newFile("oversized.log")
src.writeText(
"before\r" +
"x".repeat(LogRedactor.MAX_LINE_LENGTH - 3) + secret + "\r\n" +
"after $secret"
)
val out = StringWriter()
LogRedactor.scrubTo(src, out)
assertEquals(
"before\n${LogRedactor.OVERSIZED_LINE}\nafter sessionid=<redacted:token>\n",
out.toString(),
)
assertFalse(out.toString().contains("abcdef123456"))
}
@Test fun aMaximumLengthLineBeforeCrLfIsStillScrubbed() {
val src = tmp.newFile("maximum.log")
val line = "x ".repeat(LogRedactor.MAX_LINE_LENGTH / 2)
src.writeText(line + "\r\n")
val out = StringWriter()
LogRedactor.scrubTo(src, out)
assertEquals(line + "\n", out.toString())
assertFalse(out.toString().contains(LogRedactor.OVERSIZED_LINE))
}
@Test fun manyLearnedAccountsKeepBoundariesAndLiteralNames() {
val users = tmp.newFile("loginusers.vdf")
users.writeText((0 until 40).joinToString("\n") {
"\"AccountName\" \"probe_account_$it\""
} + "\n\"PersonaName\" \"Player.+[40]\"\n")
try {
LogRedactor.learnAccounts(users)
assertEquals("owner=<redacted:account> persona=<redacted:account>",
LogRedactor.redact("owner=probe_account_39 persona=Player.+[40]"))
assertEquals("owner=probe_account_390", LogRedactor.redact("owner=probe_account_390"))
} finally {
LogRedactor.learnAccounts(tmp.root.resolve("missing-users"))
}
}
@Test fun manyLearnedAddressesKeepOtherAddresses() {
val link = tmp.newFile("droiddeck-net")
link.writeText((1..40).joinToString("\n") { "addr 203.0.113.$it 24" })
try {
LogRedactor.learnOwnAddresses(link)
assertEquals("local=<redacted:ip> server=203.0.113.200",
LogRedactor.redact("local=203.0.113.40 server=203.0.113.200"))
} finally {
LogRedactor.learnOwnAddresses(tmp.root.resolve("missing-network"))
}
}
@Test fun learnedAccountsComeFromBothClients() {
val root = java.nio.file.Files.createTempDirectory("redactor").toFile()
try {
@@ -4,6 +4,7 @@ import android.content.Context
import com.droiddeck.launcher.core.GameEnvironment
import com.droiddeck.launcher.runtime.LinuxRuntime
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
@@ -21,10 +22,78 @@ class GameEnvironmentStoreTest {
@Before fun setUp() {
context = RuntimeEnvironment.getApplication()
context.getSharedPreferences("session", Context.MODE_PRIVATE).edit()
.remove("logs")
.remove("gameDebugLogsEnabled")
.commit()
guest = File(LinuxRuntime.rootDir(context), "root/.config/droiddeck/game-environment.json")
File(context.filesDir, "game-environment.json").delete()
}
@After fun tearDown() {
context.getSharedPreferences("session", Context.MODE_PRIVATE).edit()
.remove("logs")
.remove("gameDebugLogsEnabled")
.commit()
}
@Test fun gameDebugLogsAreOffByDefault() {
assertFalse(SessionPrefs.gameDebugLogsEnabled(context))
GameEnvironmentStore.save(context, GameEnvironment.Config())
assertFalse(JSONObject(guest.readText()).getBoolean(GameEnvironmentStore.GAME_DEBUG_LOGS))
}
@Test fun gameDebugLogsRequireSessionLogsAndPreserveCustomDiagnostics() {
val config = GameEnvironment.Config(shared = mapOf("PROTON_LOG" to "0", "WINEDEBUG" to "+warn"))
GameEnvironmentStore.save(context, config)
SessionPrefs.setGameDebugLogsEnabled(context, true)
var published = JSONObject(guest.readText())
assertTrue(published.getBoolean(GameEnvironmentStore.GAME_DEBUG_LOGS))
assertEquals("0", published.getJSONObject("shared").getString("PROTON_LOG"))
assertEquals("+warn", published.getJSONObject("shared").getString("WINEDEBUG"))
assertEquals(config, GameEnvironmentStore.read(context))
SessionPrefs.setLogsEnabled(context, false)
published = JSONObject(guest.readText())
assertFalse(published.getBoolean(GameEnvironmentStore.GAME_DEBUG_LOGS))
assertTrue(SessionPrefs.gameDebugLogsEnabled(context))
SessionPrefs.setLogsEnabled(context, true)
assertTrue(JSONObject(guest.readText()).getBoolean(GameEnvironmentStore.GAME_DEBUG_LOGS))
}
@Test fun gameDebugLogsWaitForARetainedSessionFolder() {
assertNull(SessionPaths.current())
assertTrue(SessionPaths.currentSessionLogsRetained(context))
SessionPrefs.setLogsEnabled(context, false)
val cacheSession = SessionPaths.beginOrCurrent(context, "game-debug-logs")
try {
assertFalse(SessionPaths.currentSessionLogsRetained(context))
SessionPrefs.setLogsEnabled(context, true)
SessionPrefs.setGameDebugLogsEnabled(context, true)
assertTrue(SessionPrefs.gameDebugLogsEnabled(context))
assertFalse(JSONObject(guest.readText()).getBoolean(GameEnvironmentStore.GAME_DEBUG_LOGS))
assertEquals(cacheSession, SessionPaths.take())
SessionPaths.release(context, cacheSession)
val retainedSession = SessionPaths.beginOrCurrent(context, "game-debug-logs")
assertTrue(SessionPaths.currentSessionLogsRetained(context))
GameEnvironmentStore.publish(context)
assertTrue(JSONObject(guest.readText()).getBoolean(GameEnvironmentStore.GAME_DEBUG_LOGS))
assertEquals(retainedSession, SessionPaths.current())
} finally {
SessionPaths.take()?.let { ended ->
SessionPaths.release(context, ended)
ended.deleteRecursively()
}
}
}
@Test fun savedSettingsAndPublishedSettingsPreserveUnsetsAndLiteralValues() {
val config = GameEnvironment.Config(
shared = mapOf("LITERAL" to "a=b 'quoted' $(echo test)", "EMPTY" to ""),
@@ -131,4 +131,124 @@ class SessionLogShareTest {
assertTrue("steam/console_log.txt" in names)
assertFalse("steam/cef_log.previous.txt" in names)
}
@Test fun streamingComparisonPreservesUnchangedFileAndReplacesChangedFile() {
val folder = tmp.newFolder("2026-10-06-08-steam")
val unchanged = File(folder, "unchanged.log")
unchanged.bufferedWriter().use { out -> repeat(5000) { out.write("safe diagnostic $it\n") } }
val originalModified = 1_600_000_000_000L
assertTrue(unchanged.setLastModified(originalModified))
val changed = File(folder, "changed.log")
changed.bufferedWriter().use { out ->
repeat(5000) { out.write("safe diagnostic $it\n") }
out.write("$secret\n")
}
assertTrue(SessionArtifacts.scrubAndMark(folder))
assertEquals(originalModified, unchanged.lastModified())
assertFalse(changed.readText().contains(leaked))
assertTrue(changed.readText().contains("sessionid=<redacted:token>"))
}
@Test fun historicalScrubDefersWithoutLeavingMarkersAndResumes() {
val folder = tmp.newFolder("2026-10-06-09-steam")
val log = File(folder, "session.log").apply { writeText("$secret\n") }
val rulesMarker = File(folder, ".scrubbed-r${LogRedactor.RULES_VERSION}").apply { writeText("stale\n") }
val treeMarker = File(folder, SessionArtifacts.SCRUBBED_TREE_MARKER).apply { writeText("rules ${LogRedactor.RULES_VERSION}\n") }
SessionState.running = true
try {
assertFalse(SessionArtifacts.scrubAndMark(folder, stopWhenSessionStarts = true))
} finally {
SessionState.running = false
}
assertFalse(rulesMarker.exists())
assertFalse(treeMarker.exists())
assertTrue(log.readText().contains(leaked))
assertTrue(SessionArtifacts.scrubAndMark(folder, stopWhenSessionStarts = true))
assertFalse(log.readText().contains(leaked))
assertTrue(File(folder, ".scrubbed-r${LogRedactor.RULES_VERSION}").isFile)
}
@Test fun recoveryRetryKeepsItsSteamLogsWhenALaterSessionLeftNoFolder() {
val context: android.content.Context = RuntimeEnvironment.getApplication()
val folder = File(com.droiddeck.launcher.runtime.LinuxRuntime.logDir(context), "2026-10-08-01-steam").apply { mkdirs() }
val saved = File(folder, "steam/console_log.txt").apply {
parentFile!!.mkdirs()
writeText("session A\n")
}
File(folder, "ended-without-teardown.txt").writeText("recovery attempted before session B\n")
val runtime = File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(context), "root/.local/share/Steam/logs/console_log.txt").apply {
parentFile!!.mkdirs()
writeText("session B\n")
}
SessionArtifacts.finishAbandoned(context)
assertEquals("session A\n", saved.readText())
assertEquals("session B\n", runtime.readText())
assertTrue(File(folder, SessionArtifacts.COMPLETE_MARKER).isFile)
}
@Test fun abandonedRecoveryDoesNotCopyLogsFromANewerCompletedSession() {
val context: android.content.Context = RuntimeEnvironment.getApplication()
val logs = com.droiddeck.launcher.runtime.LinuxRuntime.logDir(context)
val folder = File(logs, "2026-10-08-02-steam").apply { mkdirs() }
val saved = File(folder, "steam/console_log.txt").apply {
parentFile!!.mkdirs()
writeText("session A\n")
}
File(logs, "2026-10-08-03-steam").apply { mkdirs() }
.resolve(SessionArtifacts.COMPLETE_MARKER).writeText("finished session B\n")
File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(context), "root/.local/share/Steam/logs/console_log.txt").apply {
parentFile!!.mkdirs()
writeText("session B\n")
}
SessionArtifacts.finishAbandoned(context)
assertEquals("session A\n", saved.readText())
assertTrue(File(folder, SessionArtifacts.COMPLETE_MARKER).isFile)
}
@Test fun startupRecoveryRunsBeforeVacMarkerArmWhileOrdinaryCleanupDefers() {
val context: android.content.Context = RuntimeEnvironment.getApplication()
val root = com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(context)
val folder = File(com.droiddeck.launcher.runtime.LinuxRuntime.logDir(context), "2999-12-31-99-startup-recovery")
val marker = File(root, "root/.droiddeck-vac-diagnostics")
val launches = File(root, "root/.droiddeck-vac-launches")
val runtimeSteamLog = File(SessionPrefs.steamRoot(context, "arm64"), "logs/startup-recovery-vac.log")
val previousRunning = SessionState.running
val previousPhase = SessionState.phase
try {
folder.deleteRecursively()
folder.mkdirs()
SessionArtifacts.recordClient(folder, "arm64")
marker.apply { parentFile!!.mkdirs(); writeText("0\narm64\n") }
launches.writeText("startup VAC evidence\n")
runtimeSteamLog.apply { parentFile!!.mkdirs(); writeText("startup VAC evidence\n") }
SessionState.running = true
SessionState.phase = SessionPhase.STARTING_GUEST
SessionArtifacts.finishAbandoned(context)
assertFalse(File(folder, "ended-without-teardown.txt").exists())
assertFalse(File(folder, SessionArtifacts.COMPLETE_MARKER).exists())
SessionArtifacts.finishAbandoned(context, beforeGuestStart = true)
assertEquals("startup VAC evidence\n", File(folder, "steam/startup-recovery-vac.log").readText())
assertTrue(File(folder, "vac.txt").readText().contains("startup VAC evidence"))
assertTrue(File(folder, SessionArtifacts.COMPLETE_MARKER).isFile)
} finally {
SessionState.running = previousRunning
SessionState.phase = previousPhase
folder.deleteRecursively()
runtimeSteamLog.delete()
marker.delete()
launches.delete()
}
}
}
@@ -2,6 +2,7 @@ package com.droiddeck.launcher.session
import com.droiddeck.launcher.runtime.LinuxRuntime
import java.io.File
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
@@ -20,6 +21,49 @@ import org.robolectric.annotation.Config
class SessionLogShareZipTest {
private val app get() = RuntimeEnvironment.getApplication()
@Test fun aLargeSteamLogIsRedactedInTheSharedArchive() {
val session = File(LinuxRuntime.logDir(app), "2026-10-08-02-steam").apply { mkdirs() }
val raw = File(session, "steam-42.log")
val earlySecret = "early-sessionid-abcdef123456"
val trailingSecret = "late-refresh-token-abcdef123456"
try {
// One long line exercises the streaming limit; the final ordinary line must still be redacted.
raw.bufferedWriter().use { out ->
out.write("sessionid=$earlySecret")
val chunk = "x".repeat(8192)
repeat(8192) { out.write(chunk) }
out.write("\nrefresh_token=$trailingSecret\n")
}
assertTrue(raw.length() > 64L * 1024 * 1024)
val zip = SessionLogShare.zipFolder(app, session)!!
ZipFile(zip).use { archive ->
val entry = archive.getEntry("${session.name}/steam-42.log")!!
archive.getInputStream(entry).use { input ->
// Only inspect the bounded scrubbed prefix: the pre-fix raw archive is over 64 MiB.
val expected = "${com.droiddeck.launcher.core.LogRedactor.OVERSIZED_LINE}\n"
val prefix = ByteArray(expected.toByteArray(StandardCharsets.UTF_8).size)
var offset = 0
while (offset < prefix.size) {
val count = input.read(prefix, offset, prefix.size - offset)
assertTrue("zip entry ended before its scrubbed prefix", count > 0)
offset += count
}
assertEquals(expected, String(prefix, StandardCharsets.UTF_8))
input.bufferedReader().use { reader ->
val trailing = requireNotNull(reader.readLine())
assertFalse("trailing secret survived", trailing.contains(trailingSecret))
assertEquals("refresh_token=<redacted:token>", trailing)
assertEquals(null, reader.readLine())
}
}
}
} finally {
SessionLogShare.clear(app)
session.deleteRecursively()
}
}
@Test fun everyTextFileInTheZipIsScrubbedAndTheStoresAndToolsLogsComeAlong() {
val logs = LinuxRuntime.logDir(app)
val session = File(logs, "2026-10-08-01-steam").apply { mkdirs() }
@@ -32,6 +32,7 @@ class SettingsResetTest {
SessionPrefs.setHdr(app, SessionService.MODE_DESKTOP, true)
SessionPrefs.setResolutionChoice(app, SessionService.MODE_STEAM, "1280x800")
SessionPrefs.setGuestHostname(app, "MyDeck")
SessionPrefs.setGameDebugLogsEnabled(app, true)
ControllerPrefs.setOpacity(app, 40)
AppUiPrefs.setScale(app, 150)
FrameGen.set(app, FrameGen.Mode(FrameGen.ENGINE_WINFG, 3))
@@ -45,6 +46,10 @@ class SettingsResetTest {
assertFalse(SessionPrefs.hdr(app, SessionService.MODE_DESKTOP))
assertFalse(SessionPrefs.resolutionChosen(app, SessionService.MODE_STEAM))
assertEquals(SessionPrefs.DEFAULT_GUEST_HOSTNAME, SessionPrefs.guestHostname(app))
assertFalse(SessionPrefs.gameDebugLogsEnabled(app))
val guest = java.io.File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(app),
"root/.config/droiddeck/game-environment.json")
assertFalse(org.json.JSONObject(guest.readText()).getBoolean(GameEnvironmentStore.GAME_DEBUG_LOGS))
assertEquals(100, ControllerPrefs.read(app).opacity)
assertEquals(AppUiPrefs.DEFAULT_SCALE, AppUiPrefs.scale(app))
assertEquals(FrameGen.Mode.OFF.engine, FrameGen.engine(app))