iOS uses Metrickit to collect crash logs

iOS uses Metrickit to collect crash logs

What is Metrickit

MetricKit is a tool introduced by Apple in iOS 13 that aggregates and analyzes reports on each device regarding exception and crash diagnostics as well as power and performance metrics.

Why use MetricKit to collect crash logs

Currently, some open source frameworks on the market, such as KSCrash and PLCrashReport, cannot capture some crashes. For example, PLCrashReport cannot capture stack overflow crashes, SIGKILL, SIGQUIT and other signal crashes. KSCrash can only capture part of SIGKILL crashes.

Metrickit flaws

  • Currently only supports crash log collection after iOS14
  • The crash log does not return the specific crash time and startup time, and the crash scene information does not contain any other information except the stack.
  • If segment migration compilation technology is used, the address and uuid of the main program macho cannot match.

The crash log of iOS14 will be called back once every 24 hours, which has low timeliness. After iOS15, the crash log will be returned after the next startup, but it has been verified that some will be called back immediately, while others will not, and the pattern is unpredictable.

Start access

1. Add MetricKit

2. Add MetricKit listeners

 if ( @ available ( iOS 14.0 , * )) {

MXMetricManager * manager = [ MXMetricManager sharedManager ];

if ( self && manager && [ manager respondsToSelector : @ selector ( addSubscriber :)]) {

[ manager addSubscriber : self ];

}

}

3. The listener implements the MXMetricManagerSubscriber protocol method. The payloadDic contains the last crash log stack and information of this application.

 // If Apple has data, it will call back after registering the monitor

- ( void ) didReceiveDiagnosticPayloads :( NSArray < MXDiagnosticPayload * > * _Nonnull ) payloads API_AVAILABLE ( ios ( 14.0 )) {

if ( @ available ( iOS 14.0 , * )) {

for ( MXDiagnosticPayload * payload in payloads ) {

NSDictionary * payloadDic = [ payload dictionaryRepresentation ];



});

}

}

}

4. Log assembly key code examples

 NSArray * callStackRootFrames = [ dicFrame ArrayValueForKey : kMetrkitCallStackRootFramesKey ];

if ( callStackRootFrames . count <= 0 ) {

continue ;

}

NSDictionary * dicZero = [ callStackRootFrames ObjectAtIndex : 0 ];

int rootIndex = 0 ;

while ( dicZero && dicZero . count > 0 ) {

NSString * binaryUUID = [ dicZero stringValueForKey : kMetrkitBinaryUUIDKey ];

NSString * binaryName = [ dicZero stringValueForKey : kMetrkitBinaryNameKey ];

long long baseAdd = [[ dicZero NumberValueForKey : kMetrkitOffsetIntoBinaryTextSegmentKey ] longLongValue ];

long long address = [[ dicZero numberValueForKey : kMetrkitAddressKey ] longLongValue ];

NSArray * subFrames = [ dicZero arrayValueForKey : kMetrkitSubFramesKey ];

[ strStack appendFormat : @ "%d %@ 0x%llx 0x%llx + %lld\n" , rootIndex , binaryName , baseAdd , address , address - model . baseAddress ];

rootIndex ++ ;

if ( subFrames && subFrames . count >= 0 ) {

dicZero = [ subFrames ObjectAtIndex : 0 ];

} else {

dicZero = nil ;

}

MetricKit return fields meaning and details

JSON overall format

crashDiagnostics Details

Each crash is a dictionary with the following contents:

diagnosticMetaData dictionary details

 | terminationReason | String | Crash reason | RBSTerminateContext domain : 10 code : 0x8BADF00D
explanation : scene - update watchdog transgression : application : 6308
exhausted real ( wall clock ) time allowance of 10.00 seconds |

callStackTree dictionary details

callStacks array details

The elements in the array are dictionaries, as follows

callStackRootFrames array details

The elements in the array are dictionaries, as follows

diskWriteExceptionDiagnostics Details

Each crash is a dictionary with the following contents:

diagnosticMetaData dictionary details

callStackTree dictionary details

callStacks array details

The elements in the array are dictionaries, as follows

callStackRootFrames array details

The elements in the array are dictionaries, as follows

cpuExceptionDiagnostics Details

Each crash is a dictionary with the following contents:

diagnosticMetaData dictionary details

callStackTree dictionary details

callStacks array details

The elements in the array are dictionaries, as follows

callStackRootFrames array details

The elements in the array are dictionaries, as follows

hangDiagnostics Details

diagnosticMetaData dictionary details

callStackTree dictionary details

callStacks array details


callStackRootFrames array details

<<:  Let's talk about phantom types in Swift

>>:  Apple discontinues iPod Touch, ending 20-year iPod legend

Recommend

Why would Google open source its most important secrets?

Google's technology is one of the main drivin...

2020 Zhihu promotion and traffic generation skills!

Choice is greater than effort? No comment. I don’...

How to retain high-value users? Here are some suggestions for you!

The user life cycle of each product is a process ...

It’s 2020 now. Which is better, fingerprint recognition or facial recognition?

Eight years ago, mobile fingerprint recognition w...