一囚似、首先獲取 重排后方法執(zhí)行順序
.h 文件
import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SymbolTools : NSObject
///寫入啟動(dòng)完成時(shí) 調(diào)用的符號(hào)表
- (void)writeSymobol;
@end
NS_ASSUME_NONNULL_END
.m 文件
import "SymbolTools.h"
include <stdint.h>
include <stdio.h>
include <sanitizer/coverage_interface.h>
import <dlfcn.h>
import <libkern/OSAtomicQueue.h>
@implementation SymbolTools
//原子隊(duì)列
static OSQueueHead symbolist = OS_ATOMIC_QUEUE_INIT;
//存放符號(hào)表的結(jié)構(gòu)體
typedef struct {
void *pc; //當(dāng)前函數(shù)棧信息
void *next; //下個(gè)函數(shù)棧信息
}SYNode;
//根據(jù)stop 地址偏移可以算出調(diào)用函數(shù)個(gè)數(shù) ---暫時(shí)用不到,但是必須實(shí)現(xiàn)該函數(shù),因?yàn)閏lang 會(huì)調(diào)用
void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
uint32_t *stop) {
if (start == stop || *start) return; // Initialize only once.
printf("INIT: %p %p\n", start, stop);
}
//返回函數(shù)調(diào)用地址 ->寄存器即將調(diào)用的下個(gè)函數(shù)地址
void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
void *PC = __builtin_return_address(0);
SYNode *node = malloc(sizeof(SYNode));
*node = (SYNode){PC,NULL};
//進(jìn)入
OSAtomicEnqueue(&symbolist, node, offsetof(SYNode,next));
}
pragma 文件寫入
- (void)writeSymobol{
NSMutableArray <NSString *> *symbolAry = [NSMutableArray array];
while (YES) {
SYNode *node = OSAtomicDequeue(&symbolist, offsetof(SYNode, next));
if (node == NULL) {
break;
}
//存放函數(shù)信息的結(jié)構(gòu)體
Dl_info info;
dladdr(node->pc,&info);
NSString *name = @(info.dli_sname);
BOOL isObjc = [name hasPrefix:@"-["] || [name hasPrefix:@"+["];
NSString *symbolName = isObjc?name:[@"" stringByAppendingString:name]; //非oc符號(hào) 缺少''
[symbolAry addObject:symbolName];
}
//字符串顛倒順序,因?yàn)榻Y(jié)構(gòu)體加入的時(shí)候 類似棧的形式先進(jìn)后出
NSArray *resultAry = [[symbolAry reverseObjectEnumerator] allObjects];
//由于方法會(huì)被多次調(diào)用,需要去重
[symbolAry removeAllObjects];
for (NSString *symbolName in resultAry) {
if (![symbolAry containsObject:symbolName]) {
[symbolAry addObject:symbolName];
}
}
[symbolAry removeObject:[NSString stringWithFormat:@"%s",FUNCTION]]; //移除本方法
//寫入文件
NSString *symbols = [symbolAry componentsJoinedByString:@"\n"];
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"pdmi.order"];
NSData *fileConten = [symbols dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:fileConten attributes:nil];
NSLog(@"filePath == %@",filePath);
}
@end
二谭贪、配置順序方法執(zhí)行條件 -fsanitize-coverage=func,trace-pc-guard
三、按路徑取到文件 放到工程目錄中 然后配置參數(shù)