Skip to content

Commit

Permalink
[feature] store titleframe into anr record
Browse files Browse the repository at this point in the history
  • Loading branch information
haowenwu committed Apr 24, 2019
1 parent 60e6cd3 commit 8122fcf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
17 changes: 17 additions & 0 deletions MTHawkeye/TimeConsumingPlugins/ANRTrace/Core/MTHANRObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import "MTHANRRecord.h"

#import <MTHawkeye/mth_stack_backtrace.h>
#import <MTHawkeye/MTHawkeyeDyldImagesUtils.h>
#import <pthread.h>


Expand Down Expand Up @@ -84,6 +85,7 @@ - (void)main {
mth_stack_backtrace_of_thread(main_thread, stackframes, sizeof(mth_stack_backtrace), 0);
threadStack->stackframes = stackframes->frames;
threadStack->stackframesSize = stackframes->frames_size;
threadStack->titleFrame = [self titleFrameForStackframes:stackframes->frames size:stackframes->frames_size];
}
}
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
Expand All @@ -107,6 +109,21 @@ - (void)main {
}
}

- (uintptr_t)titleFrameForStackframes:(uintptr_t *)frames size:(size_t)size {
for (int fi = 0; fi < size; ++fi) {
uintptr_t frame = frames[fi];
if (!mtha_addr_is_in_sys_libraries(frame)) {
return frame;
}
}

if (size > 0) {
uintptr_t frame = frames[0];
return frame;
}
return 0;
}

@end


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

@interface MTHANRRecordRaw : NSObject {
@public
uintptr_t titleFrame;
uintptr_t *stackframes;
size_t stackframesSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ - (void)storeANRRecord:(MTHANRRecordRaw *)anrRecord {
NSDictionary *dict = @{
@"time" : @(anrRecord.time),
@"duration" : [NSString stringWithFormat:@"%@", @(anrRecord.duration * 1000)],
@"stackframes" : stackInStr.copy
@"stackframes" : stackInStr.copy,
@"titleframe" : [NSString stringWithFormat:@"%p", anrRecord->titleFrame],
};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict.copy options:0 error:&error];
Expand Down Expand Up @@ -166,6 +167,11 @@ - (void)storeANRRecord:(MTHANRRecordRaw *)anrRecord {
[scanner scanHexLongLong:&frame];
record->stackframes[i] = (uintptr_t)frame;
}
unsigned long long titleframe = 0;
NSScanner *scanner = [NSScanner scannerWithString:dict[@"titleframe"]];
[scanner setScanLocation:2];
[scanner scanHexLongLong:&titleframe];
record->titleFrame = titleframe;
[anrRecords addObject:record];
} else {
MTHLogWarn(@"[storage] read anr record failed, %@", error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#import <MTHawkeye/MTHStackFrameSymbolics.h>
#import <MTHawkeye/MTHStackFrameSymbolicsRemote.h>
#import <MTHawkeye/MTHawkeyeDyldImagesStorage.h>
#import <MTHawkeye/MTHawkeyeDyldImagesUtils.h>
#import <MTHawkeye/MTHawkeyeLogMacros.h>
#import <MTHawkeye/MTHawkeyeSettingCell.h>
#import <MTHawkeye/MTHawkeyeSettingTableEntity.h>
Expand Down Expand Up @@ -141,9 +140,8 @@ - (void)symbolicateRecordTitles {
MTHANRRecordRaw *record = self.records[ri];
NSString *riStr = [NSString stringWithFormat:@"%ld", (long)ri];

uintptr_t titleFrame = [self titleFrameForRecord:record];
@synchronized(self.recordTitles) {
self.recordTitles[riStr] = [self recordFrameStringFrom:titleFrame withoutFnameIfExistSname:YES];
self.recordTitles[riStr] = [self recordFrameStringFrom:record->titleFrame withoutFnameIfExistSname:YES];
}
dispatch_async(dispatch_get_main_queue(), ^(void) {
if ([self.tableView numberOfRowsInSection:1] < ri) {
Expand All @@ -155,21 +153,6 @@ - (void)symbolicateRecordTitles {
});
}

- (uintptr_t)titleFrameForRecord:(MTHANRRecordRaw *)record {
for (int fi = 0; fi < record->stackframesSize; ++fi) {
uintptr_t frame = record->stackframes[fi];
if (!mtha_addr_is_in_sys_libraries(frame)) {
return frame;
}
}

if (record->stackframesSize > 0) {
uintptr_t frame = record->stackframes[0];
return frame;
}
return 0;
}

- (NSString *)recordFrameStringFrom:(uintptr_t)frame withoutFnameIfExistSname:(BOOL)shortV {
NSString *title = nil;
Dl_info dlinfo = {NULL, NULL, NULL, NULL};
Expand Down

0 comments on commit 8122fcf

Please sign in to comment.