From 7ecfa733868562569de00623efd11fb87f0aae5a Mon Sep 17 00:00:00 2001 From: sheng <277715243@qq.com> Date: Wed, 10 Nov 2021 14:50:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=87=8F=E5=B0=91dyld=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E7=9A=84runtime=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GrowingAnalytics.podspec | 2 +- .../Core/GrowingAnnotationCore.m | 23 ++-- .../Core/GrowingModuleManager.h | 11 +- .../Core/GrowingModuleManager.m | 117 +++++++++++------- .../Core/GrowingServiceManager.h | 2 + .../Core/GrowingServiceManager.m | 6 + GrowingTrackerCore/GrowingRealTracker.m | 2 +- 7 files changed, 99 insertions(+), 64 deletions(-) diff --git a/GrowingAnalytics.podspec b/GrowingAnalytics.podspec index 35ba7f54d..72925add1 100644 --- a/GrowingAnalytics.podspec +++ b/GrowingAnalytics.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'GrowingAnalytics' - s.version = '3.3.1-hotfix.1' + s.version = '3.3.1-hotfix.2' s.summary = 'iOS SDK of GrowingIO.' s.description = <<-DESC GrowingAnalytics具备自动采集基本的用户行为事件,比如访问和行为数据等。目前支持代码埋点、无埋点、可视化圈选、热图等功能。 diff --git a/GrowingTrackerCore/Core/GrowingAnnotationCore.m b/GrowingTrackerCore/Core/GrowingAnnotationCore.m index d249ade9a..1f89bd083 100644 --- a/GrowingTrackerCore/Core/GrowingAnnotationCore.m +++ b/GrowingTrackerCore/Core/GrowingAnnotationCore.m @@ -34,7 +34,6 @@ #endif NSArray *GrowingReadConfiguration(char *sectionName, const struct mach_header *mhp) { - NSMutableArray *configs = [NSMutableArray array]; unsigned long size = 0; #ifndef __LP64__ uintptr_t *memory = (uintptr_t *)getsectiondata(mhp, SEG_DATA, sectionName, &size); @@ -44,11 +43,15 @@ #endif unsigned long counter = size / sizeof(void *); + if (counter == 0) { + return nil; + } + NSMutableArray *configs = [NSMutableArray array]; for (int idx = 0; idx < counter; ++idx) { char *string = (char *)memory[idx]; NSString *str = [NSString stringWithUTF8String:string]; if (!str) continue; - GrowingLog(@"config = %@", str); + GrowingLog(@"[Growing] %@", str); if (str) [configs addObject:str]; } @@ -56,15 +59,12 @@ } static void dyld_callback(const struct mach_header *mhp, intptr_t vmaddr_slide) { + NSArray *mods = GrowingReadConfiguration(GrowingModSectName, mhp); for (NSString *modName in mods) { - Class cls; if (modName) { - cls = NSClassFromString(modName); - - if (cls) { - [[GrowingModuleManager sharedInstance] registerDynamicModule:cls]; - } + // 这里不进行 name -> class 转换,且只存储NSString + [[GrowingModuleManager sharedInstance] addLocalModule:modName]; } } @@ -78,16 +78,15 @@ static void dyld_callback(const struct mach_header *mhp, intptr_t vmaddr_slide) if ([json isKindOfClass:[NSDictionary class]] && [json allKeys].count) { NSString *protocol = [json allKeys][0]; NSString *clsName = [json allValues][0]; - if (protocol && clsName) { - [[GrowingServiceManager sharedInstance] registerService:NSProtocolFromString(protocol) - implClass:NSClassFromString(clsName)]; + // 这里不进行 name -> class 转换,且只存储NSString + [[GrowingServiceManager sharedInstance] registerServiceName:protocol implClassName:clsName]; } } } } } - +// add callback before main() __attribute__((constructor)) void GrowingInitProphet(void) { _dyld_register_func_for_add_image(dyld_callback); } diff --git a/GrowingTrackerCore/Core/GrowingModuleManager.h b/GrowingTrackerCore/Core/GrowingModuleManager.h index 5ba01b1f3..05862dc8e 100644 --- a/GrowingTrackerCore/Core/GrowingModuleManager.h +++ b/GrowingTrackerCore/Core/GrowingModuleManager.h @@ -62,15 +62,22 @@ NS_ASSUME_NONNULL_BEGIN + (instancetype)sharedInstance; +#pragma mark - dyld期间添加module信息 + +- (void)addLocalModule:(NSString *)modulename; + +#pragma mark - 调用方法 // If you do not comply with set Level protocol, the default Normal +// 将 class 存入Module Info,并不会初始化 - (void)registerDynamicModule:(Class)moduleClass; - (void)unRegisterDynamicModule:(Class)moduleClass; -//- (void)loadLocalModules; - +// 初始化各个Module Info - (void)registedAllModules; +#pragma mark - 事件和module关联 + - (void)registerCustomEvent:(NSInteger)eventType withModuleInstance:(id)moduleInstance andSelectorStr:(NSString *)selectorStr; diff --git a/GrowingTrackerCore/Core/GrowingModuleManager.m b/GrowingTrackerCore/Core/GrowingModuleManager.m index 3bcd46e55..af55b5110 100644 --- a/GrowingTrackerCore/Core/GrowingModuleManager.m +++ b/GrowingTrackerCore/Core/GrowingModuleManager.m @@ -58,14 +58,16 @@ @interface GrowingModuleManager() +/// 存module名,load之后清空 +@property(nonatomic, strong) NSMutableArray *growingModuleNames; +/// 存module class +@property(nonatomic, strong) NSMutableArray *growingModuleDynamicClasses; +/// 存封装有module class的info类 +@property(nonatomic, strong) NSMutableArray *growingModuleInfos; +@property(nonatomic, strong) NSMutableArray *growingModules; -@property(nonatomic, strong) NSMutableArray *GrowingModuleDynamicClasses; - -@property(nonatomic, strong) NSMutableArray *GrowingModuleInfos; -@property(nonatomic, strong) NSMutableArray *GrowingModules; - -@property(nonatomic, strong) NSMutableDictionary> *> *GrowingModulesByEvent; -@property(nonatomic, strong) NSMutableDictionary *GrowingSelectorByEvent; +@property(nonatomic, strong) NSMutableDictionary> *> *growingModulesByEvent; +@property(nonatomic, strong) NSMutableDictionary *growingSelectorByEvent; @end @@ -81,7 +83,18 @@ + (instancetype)sharedInstance }); return sharedManager; } +// 仅添加string,规避运行时转换class +- (void)addLocalModule:(NSString *)modulename { + [self.growingModuleNames addObject:modulename]; +} +// 从存储的name数组中读取所有的module +- (void)loadLocalModules { + for (NSString *name in self.growingModuleNames) { + [self registerDynamicModule:NSClassFromString(name)]; + } + [self.growingModuleNames removeAllObjects]; +} - (void)registerDynamicModule:(Class)moduleClass { @@ -93,18 +106,18 @@ - (void)unRegisterDynamicModule:(Class)moduleClass { if (!moduleClass) { return; } - [self.GrowingModuleInfos filterUsingPredicate:[NSPredicate predicateWithFormat:@"%@!=%@", kModuleInfoNameKey, NSStringFromClass(moduleClass)]]; + [self.growingModuleInfos filterUsingPredicate:[NSPredicate predicateWithFormat:@"%@!=%@", kModuleInfoNameKey, NSStringFromClass(moduleClass)]]; __block NSInteger index = -1; - [self.GrowingModules enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + [self.growingModules enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:moduleClass]) { index = idx; *stop = YES; } }]; if (index >= 0) { - [self.GrowingModules removeObjectAtIndex:index]; + [self.growingModules removeObjectAtIndex:index]; } - [self.GrowingModulesByEvent enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, NSMutableArray> * _Nonnull obj, BOOL * _Nonnull stop) { + [self.growingModulesByEvent enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, NSMutableArray> * _Nonnull obj, BOOL * _Nonnull stop) { __block NSInteger index = -1; [obj enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:moduleClass]) { @@ -120,8 +133,8 @@ - (void)unRegisterDynamicModule:(Class)moduleClass { - (void)registedAllModules { - - [self.GrowingModuleInfos sortUsingComparator:^NSComparisonResult(NSDictionary *module1, NSDictionary *module2) { + [self loadLocalModules]; + [self.growingModuleInfos sortUsingComparator:^NSComparisonResult(NSDictionary *module1, NSDictionary *module2) { NSNumber *module1Level = (NSNumber *)[module1 objectForKey:kModuleInfoLevelKey]; NSNumber *module2Level = (NSNumber *)[module2 objectForKey:kModuleInfoLevelKey]; if (module1Level.integerValue != module2Level.integerValue) { @@ -136,7 +149,7 @@ - (void)registedAllModules NSMutableArray *tmpArray = [NSMutableArray array]; //module init - [self.GrowingModuleInfos enumerateObjectsUsingBlock:^(NSDictionary *module, NSUInteger idx, BOOL * _Nonnull stop) { + [self.growingModuleInfos enumerateObjectsUsingBlock:^(NSDictionary *module, NSUInteger idx, BOOL * _Nonnull stop) { NSString *classStr = [module objectForKey:kModuleInfoNameKey]; @@ -150,9 +163,9 @@ - (void)registedAllModules }]; - [self.GrowingModules removeAllObjects]; + [self.growingModules removeAllObjects]; - [self.GrowingModules addObjectsFromArray:tmpArray]; + [self.growingModules addObjectsFromArray:tmpArray]; [self registerAllSystemEvents]; } @@ -200,7 +213,7 @@ - (instancetype)init { self = [super init]; if (self) { - self.GrowingModuleDynamicClasses = [NSMutableArray array]; + self.growingModuleDynamicClasses = [NSMutableArray array]; } return self; } @@ -253,7 +266,7 @@ - (void)addModuleFromObject:(id)object [moduleInfo setObject:moduleName forKey:kModuleInfoNameKey]; } - [self.GrowingModuleInfos addObject:moduleInfo]; + [self.growingModuleInfos addObject:moduleInfo]; [moduleInfo setObject:@(NO) forKey:kModuleInfoHasInstantiatedKey]; } @@ -261,16 +274,16 @@ - (void)addModuleFromObject:(id)object - (void)registerAllSystemEvents { - [self.GrowingModules enumerateObjectsUsingBlock:^(id moduleInstance, NSUInteger idx, BOOL * _Nonnull stop) { + [self.growingModules enumerateObjectsUsingBlock:^(id moduleInstance, NSUInteger idx, BOOL * _Nonnull stop) { [self registerEventsByModuleInstance:moduleInstance]; }]; } - (void)registerEventsByModuleInstance:(id)moduleInstance { - NSArray *events = self.GrowingSelectorByEvent.allKeys; + NSArray *events = self.growingSelectorByEvent.allKeys; [events enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - [self registerEvent:obj.integerValue withModuleInstance:moduleInstance andSelectorStr:self.GrowingSelectorByEvent[obj]]; + [self registerEvent:obj.integerValue withModuleInstance:moduleInstance andSelectorStr:self.growingSelectorByEvent[obj]]; }]; } @@ -282,13 +295,13 @@ - (void)registerEvent:(NSInteger)eventType return; } NSNumber *eventTypeNumber = @(eventType); - if (!self.GrowingSelectorByEvent[eventTypeNumber]) { - [self.GrowingSelectorByEvent setObject:selectorStr forKey:eventTypeNumber]; + if (!self.growingSelectorByEvent[eventTypeNumber]) { + [self.growingSelectorByEvent setObject:selectorStr forKey:eventTypeNumber]; } - if (!self.GrowingModulesByEvent[eventTypeNumber]) { - [self.GrowingModulesByEvent setObject:@[].mutableCopy forKey:eventTypeNumber]; + if (!self.growingModulesByEvent[eventTypeNumber]) { + [self.growingModulesByEvent setObject:@[].mutableCopy forKey:eventTypeNumber]; } - NSMutableArray *eventModules = [self.GrowingModulesByEvent objectForKey:eventTypeNumber]; + NSMutableArray *eventModules = [self.growingModulesByEvent objectForKey:eventTypeNumber]; if (![eventModules containsObject:moduleInstance]) { [eventModules addObject:moduleInstance]; [eventModules sortUsingComparator:^NSComparisonResult(id moduleInstance1, id moduleInstance2) { @@ -318,33 +331,41 @@ - (void)registerEvent:(NSInteger)eventType } #pragma mark - property setter or getter -- (NSMutableArray *)GrowingModuleInfos { - if (!_GrowingModuleInfos) { - _GrowingModuleInfos = @[].mutableCopy; + +- (NSMutableArray *)growingModuleNames { + if (!_growingModuleNames) { + _growingModuleNames = @[].mutableCopy; + } + return _growingModuleNames; +} + +- (NSMutableArray *)growingModuleInfos { + if (!_growingModuleInfos) { + _growingModuleInfos = @[].mutableCopy; } - return _GrowingModuleInfos; + return _growingModuleInfos; } -- (NSMutableArray *)GrowingModules +- (NSMutableArray *)growingModules { - if (!_GrowingModules) { - _GrowingModules = [NSMutableArray array]; + if (!_growingModules) { + _growingModules = [NSMutableArray array]; } - return _GrowingModules; + return _growingModules; } -- (NSMutableDictionary> *> *)GrowingModulesByEvent +- (NSMutableDictionary> *> *)growingModulesByEvent { - if (!_GrowingModulesByEvent) { - _GrowingModulesByEvent = @{}.mutableCopy; + if (!_growingModulesByEvent) { + _growingModulesByEvent = @{}.mutableCopy; } - return _GrowingModulesByEvent; + return _growingModulesByEvent; } -- (NSMutableDictionary *)GrowingSelectorByEvent +- (NSMutableDictionary *)growingSelectorByEvent { - if (!_GrowingSelectorByEvent) { - _GrowingSelectorByEvent = @{ + if (!_growingSelectorByEvent) { + _growingSelectorByEvent = @{ @(GrowingMSetupEvent):kSetupSelector, @(GrowingMInitEvent):kInitSelector, @(GrowingMTearDownEvent):kTearDownSelector, @@ -380,7 +401,7 @@ - (NSMutableArray *)GrowingModules @(GrowingMDidCustomEvent):kAppCustomSelector, }.mutableCopy; } - return _GrowingSelectorByEvent; + return _growingSelectorByEvent; } #pragma mark - module protocol @@ -398,7 +419,7 @@ - (void)handleModuleEvent:(NSInteger)eventType [self handleModulesTearDownEventForTarget:nil withCustomParam:customParam]; break; default: { - NSString *selectorStr = [self.GrowingSelectorByEvent objectForKey:@(eventType)]; + NSString *selectorStr = [self.growingSelectorByEvent objectForKey:@(eventType)]; [self handleModuleEvent:eventType forTarget:nil withSeletorStr:selectorStr andCustomParam:customParam]; } break; @@ -417,7 +438,7 @@ - (void)handleModulesInitEventForTarget:(id)target if (target) { moduleInstances = @[target]; } else { - moduleInstances = [self.GrowingModulesByEvent objectForKey:@(GrowingMInitEvent)]; + moduleInstances = [self.growingModulesByEvent objectForKey:@(GrowingMInitEvent)]; } [moduleInstances enumerateObjectsUsingBlock:^(id moduleInstance, NSUInteger idx, BOOL * _Nonnull stop) { @@ -460,7 +481,7 @@ - (void)handleModulesTearDownEventForTarget:(id)target if (target) { moduleInstances = @[target]; } else { - moduleInstances = [self.GrowingModulesByEvent objectForKey:@(GrowingMTearDownEvent)]; + moduleInstances = [self.growingModulesByEvent objectForKey:@(GrowingMTearDownEvent)]; } //Reverse Order to unload @@ -478,18 +499,18 @@ - (void)handleModuleEvent:(NSInteger)eventType andCustomParam:(NSDictionary *)customParam { if (!selectorStr.length) { - selectorStr = [self.GrowingSelectorByEvent objectForKey:@(eventType)]; + selectorStr = [self.growingSelectorByEvent objectForKey:@(eventType)]; } SEL seletor = NSSelectorFromString(selectorStr); if (!seletor) { - selectorStr = [self.GrowingSelectorByEvent objectForKey:@(eventType)]; + selectorStr = [self.growingSelectorByEvent objectForKey:@(eventType)]; seletor = NSSelectorFromString(selectorStr); } NSArray> *moduleInstances; if (target) { moduleInstances = @[target]; } else { - moduleInstances = [self.GrowingModulesByEvent objectForKey:@(eventType)]; + moduleInstances = [self.growingModulesByEvent objectForKey:@(eventType)]; } [moduleInstances enumerateObjectsUsingBlock:^(id moduleInstance, NSUInteger idx, BOOL * _Nonnull stop) { if ([moduleInstance respondsToSelector:seletor]) { diff --git a/GrowingTrackerCore/Core/GrowingServiceManager.h b/GrowingTrackerCore/Core/GrowingServiceManager.h index 582565c27..19a885b6c 100644 --- a/GrowingTrackerCore/Core/GrowingServiceManager.h +++ b/GrowingTrackerCore/Core/GrowingServiceManager.h @@ -29,6 +29,8 @@ NS_ASSUME_NONNULL_BEGIN + (instancetype)sharedInstance; +- (void)registerServiceName:(NSString *)serviceName implClassName:(NSString *)serviceClassName; + - (void)registerService:(Protocol*)service implClass:(Class)serviceClass; - (id)createService:(Protocol*)service; diff --git a/GrowingTrackerCore/Core/GrowingServiceManager.m b/GrowingTrackerCore/Core/GrowingServiceManager.m index 3df2a27a8..ea0137407 100644 --- a/GrowingTrackerCore/Core/GrowingServiceManager.m +++ b/GrowingTrackerCore/Core/GrowingServiceManager.m @@ -47,6 +47,12 @@ - (instancetype)init { #pragma mark - private +- (void)registerServiceName:(NSString *)serviceName implClassName:(NSString *)serviceClassName { + dispatch_semaphore_wait(_signallock, DISPATCH_TIME_FOREVER); + [_allServiceDict setValue:serviceClassName forKey:serviceName]; + dispatch_semaphore_signal(_signallock); +} + - (void)registerService:(Protocol*)service implClass:(Class)serviceClass { dispatch_semaphore_wait(_signallock, DISPATCH_TIME_FOREVER); [_allServiceDict setValue:NSStringFromClass(serviceClass) forKey:NSStringFromProtocol(service)]; diff --git a/GrowingTrackerCore/GrowingRealTracker.m b/GrowingTrackerCore/GrowingRealTracker.m index 55ca6fc92..1d27987f3 100644 --- a/GrowingTrackerCore/GrowingRealTracker.m +++ b/GrowingTrackerCore/GrowingRealTracker.m @@ -37,7 +37,7 @@ #import "GrowingModuleManager.h" #import "GrowingEventManager.h" -NSString *const GrowingTrackerVersionName = @"3.3.1-hotfix.1"; +NSString *const GrowingTrackerVersionName = @"3.3.1-hotfix.2"; const int GrowingTrackerVersionCode = 30301; @interface GrowingRealTracker ()