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

How to create a hit product on Xiaohongshu?

If you want to increase the traffic of your Xiaoh...

How does Xiaohongshu operate its community?

This article provides a brief analysis of Xiaohon...

Accelerating the AI ​​world: a super battle between CPU, GPU and TPU!

When it comes to CPUs, GPUs, and TPUs, we are usu...

Feng Zhulong teaches you how to learn PS from scratch

Feng Zhulong teaches you how to learn PS from scr...

The secrets of Douyin’s tens of millions of DOU+ placements

When operating a Tik Tok account, what you look f...

Every time a bunch of bananas is picked, a banana tree dies?

gossip "For every bunch of bananas picked, a...

17 thoughts on marketing, copywriting, and life!

1. Please accept this marketing trick! Frederick ...

Download the complete works of Jin Yong's novels in txt format for free

The complete collection of Jin Yong's martial...

360 Vizza Mobile Review: A Phone More Worth Its Price Than iPhone X

In 2011, the domestic smartphone market ushered i...

The traffic operation logic of Taobao, Douyin and Kuaishou!

In the traditional centralized e-commerce model, ...