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

After four tests, how is iOS15.2 polished?

This is a very strange thing. For iPhone 12 and i...

5 key points for Tik Tok monetization!

As one of the short video platforms with the larg...

5 "pitfalls" to avoid in marketing activities

As the old saying goes, "Failure is the moth...

2500 words user retention analysis

Faced with the current situation of difficulty in...

11 types of locks and performance comparison in iOS development

[[221143]] In daily development, we often use mul...

How do seagulls know that potato chips and ice cream are delicious?

Sitting on the dock on a sunny afternoon, enjoyin...

A complete analysis of Pinduoduo’s operating model and methods

Let’s talk about Pinduoduo today. New Market In t...

Why do fireflies flash synchronously?

In the wild, fireflies will always flash spontane...

Disassembling 6 ways to play red envelopes in event marketing

Red envelopes are a value-added service provided ...

On World Pangolin Day, can we wait for good news about pangolins?

Today - the third Saturday in February - is World...