@@ -101,18 +101,44 @@ function writeDiagnostic(name, contents) {
101101 }
102102}
103103
104+ // An empty capture keeps adb's own error, so an empty artifact says why.
105+ function adbText ( { error, stdout, stderr } ) {
106+ if ( stdout . trim ( ) ) return stdout ;
107+ return stderr || ( error ? String ( error . message ) : "" ) ;
108+ }
109+
104110function lastLines ( text , count ) {
105111 const lines = text . split ( "\n" ) ;
106112 return lines . slice ( Math . max ( 0 , lines . length - count ) ) . join ( "\n" ) ;
107113}
108114
109115// The newest ApplicationExitInfo record: its reason (native crash, crash, ANR,
110116// low memory, signaled...) is the one thing that tells a crash from a kill.
117+ // Records survive reinstalls, so one from before this run is not reported.
111118function latestExitRecord ( exitInfo ) {
112119 const start = exitInfo . indexOf ( "ApplicationExitInfo #0:" ) ;
113120 if ( start === - 1 ) return "" ;
114121 const next = exitInfo . indexOf ( "ApplicationExitInfo #1:" , start ) ;
115- return exitInfo . slice ( start , next === - 1 ? undefined : next ) . trim ( ) ;
122+ const record = exitInfo . slice ( start , next === - 1 ? undefined : next ) . trim ( ) ;
123+ const stamp = / t i m e s t a m p = ( \d { 4 } ) - ( \d { 2 } ) - ( \d { 2 } ) ( \d { 2 } ) : ( \d { 2 } ) : ( \d { 2 } ) \. ( \d { 3 } ) / . exec ( record ) ;
124+ if ( stamp && logcatCutoffTime ) {
125+ const [ , y , mo , d , h , mi , sec , ms ] = stamp . map ( ( v ) => parseInt ( v , 10 ) ) ;
126+ if ( new Date ( y , mo - 1 , d , h , mi , sec , ms ) < logcatCutoffTime ) {
127+ return "(no exit recorded during this run)" ;
128+ }
129+ }
130+ return record ;
131+ }
132+
133+ // threadtime lines: "MM-DD HH:mm:ss.mmm PID TID L TAG: message"
134+ function linesOfPid ( logcat , pid ) {
135+ return logcat
136+ . split ( "\n" )
137+ . filter ( ( line ) => {
138+ const match = / ^ \d { 2 } - \d { 2 } \s + \S + \s + ( \d + ) \s / . exec ( line ) ;
139+ return match && parseInt ( match [ 1 ] , 10 ) === pid ;
140+ } )
141+ . join ( "\n" ) ;
116142}
117143
118144async function currentAppPid ( ) {
@@ -124,29 +150,29 @@ async function currentAppPid() {
124150
125151async function collectDiagnostics ( ) {
126152 const sections = [ ] ;
153+ fs . rmSync ( diagnosticsDir , { recursive : true , force : true } ) ;
127154
128155 // Unfiltered by pid on purpose: crash_dump's backtrace (tag DEBUG), the
129156 // ActivityManager death/ANR lines and lmkd kills are logged by other pids.
157+ // Repeated -b, not a comma list, and no --pid: both need API 24.
130158 const fullLogcat = filterLogcatFromCutoff (
131- ( await adbOutput ( "logcat -d -v threadtime -b main, system, crash" ) ) . stdout
159+ adbText ( await adbOutput ( "logcat -d -v threadtime -b main -b system -b crash" ) )
132160 ) ;
133161 writeDiagnostic ( "logcat.txt" , fullLogcat ) ;
134162
135163 if ( appPID !== - 1 ) {
136- const appLogcat = filterLogcatFromCutoff (
137- ( await adbOutput ( `logcat -d -v threadtime --pid=${ appPID } ` ) ) . stdout
138- ) ;
164+ const appLogcat = linesOfPid ( fullLogcat , appPID ) ;
139165 writeDiagnostic ( "logcat-app.txt" , appLogcat ) ;
140166 sections . push ( [ "Last app logcat lines" , lastLines ( appLogcat , 150 ) ] ) ;
141167 }
142168
143169 const crashBuffer = filterLogcatFromCutoff (
144- ( await adbOutput ( "logcat -d -v threadtime -b crash" ) ) . stdout
170+ adbText ( await adbOutput ( "logcat -d -v threadtime -b crash" ) )
145171 ) ;
146172 writeDiagnostic ( "logcat-crash.txt" , crashBuffer ) ;
147173 sections . push ( [ "Crash buffer" , lastLines ( crashBuffer , 200 ) ] ) ;
148174
149- const exitInfo = ( await adbOutput ( `shell dumpsys activity exit-info ${ appId } ` ) ) . stdout ;
175+ const exitInfo = adbText ( await adbOutput ( `shell dumpsys activity exit-info ${ appId } ` ) ) ;
150176 writeDiagnostic ( "exit-info.txt" , exitInfo ) ;
151177 sections . push ( [ "Latest process exit" , latestExitRecord ( exitInfo ) ] ) ;
152178
@@ -161,8 +187,14 @@ async function collectDiagnostics() {
161187 const livePid = await currentAppPid ( ) ;
162188 if ( Number . isFinite ( livePid ) ) {
163189 const stacks = await adbOutput ( `shell debuggerd -b ${ livePid } ` ) ;
164- writeDiagnostic ( "stacks.txt" , stacks . stdout + stacks . stderr ) ;
165- sections . push ( [ "App is still alive; thread stacks written to stacks.txt" , "" ] ) ;
190+ writeDiagnostic ( "stacks.txt" , adbText ( stacks ) ) ;
191+ const captured = ! stacks . error && stacks . stdout . trim ( ) ;
192+ sections . push ( [
193+ captured
194+ ? "App is still alive; thread stacks written to stacks.txt"
195+ : "App is still alive; debuggerd failed, see stacks.txt" ,
196+ "" ,
197+ ] ) ;
166198 }
167199
168200 // stat's mtime is epoch seconds, independent of the device's time zone.
0 commit comments