category_t
struct category_t {
const char *name;
classref_t cls;
struct method_list_t *instanceMethods;
struct method_list_t *classMethods;
struct protocol_list_t *protocols;
struct property_list_t *instanceProperties;
// Fields below this point are not always present on disk.
struct property_list_t *_classProperties;
method_list_t *methodsForMeta(bool isMeta) {
if (isMeta) return classMethods;
else return instanceMethods;
}
property_list_t *propertiesForMeta(bool isMeta, struct header_info *hi);
protocol_list_t *protocolsForMeta(bool isMeta) {
if (isMeta) return nullptr;
else return protocols;
}
};
分类数据加载时机.png
一个主类,一个分类
主类有+load,分类有+load:attachToClass attachCategories方法打断点*
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
* frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
frame #3: 0x00000001002e730f libobjc.A.dylib`_read_images(hList=0x00007ffeefbf21c0, hCount=296, totalClasses=24576, unoptimizedTotalClasses=24576) at objc-runtime-new.mm:3826:13
frame #4: 0x00000001002e6148 libobjc.A.dylib`map_images_nolock(mhCount=296, mhPaths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-os.mm:596:9
frame #5: 0x00000001002e5ca3 libobjc.A.dylib`map_images(count=296, paths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-runtime-new.mm:3155:12
frame #6: 0x0000000100019d2c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1778
frame #7: 0x0000000100019ecf dyld`dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) + 63
frame #8: 0x00007fff203438ef libdyld.dylib`_dyld_objc_notify_register + 113
frame #9: 0x00000001002e575a libobjc.A.dylib`_objc_init at objc-os.mm:939:5
frame #10: 0x00000001004636db libdispatch.dylib`_os_object_init + 13
frame #11: 0x0000000100474928 libdispatch.dylib`libdispatch_init + 285
frame #12: 0x00007fff2a53965b libSystem.B.dylib`libSystem_initializer + 224
frame #13: 0x0000000100031079 dyld`ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 559
frame #14: 0x0000000100031478 dyld`ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
frame #15: 0x000000010002bd1a dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 492
frame #16: 0x000000010002bc85 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 343
frame #17: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
frame #18: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
frame #19: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
frame #20: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
frame #21: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
frame #22: 0x0000000100015025 dyld`_dyld_start + 37
(lldb)
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x000000010032332e libobjc.A.dylib`attachCategories(cls=0x0000000100008320, cats_list=0x00007ffeefbf5b68, cats_count=1, flags=10) at objc-runtime-new.mm:1432:62
frame #1: 0x0000000100325a7f libobjc.A.dylib`load_categories_nolock(this=0x00007ffeefbf5bb0, catlist=0x0000000100004060)::$_6::operator()(category_t* const*) const at objc-runtime-new.mm:3215:25
frame #2: 0x000000010030b952 libobjc.A.dylib`load_categories_nolock(hi=0x000000010180b100) at objc-runtime-new.mm:3224:5
frame #3: 0x000000010030a9ba libobjc.A.dylib`loadAllCategories() at objc-runtime-new.mm:3232:9
frame #4: 0x00000001002e8ce6 libobjc.A.dylib`load_images(path="/private/tmp/objc.dst/usr/lib/libobjc.A.dylib", mh=0x00000001002e4000) at objc-runtime-new.mm:3250:9
frame #5: 0x000000010001628a dyld`dyld::notifySingle(dyld_image_states, ImageLoader const*, ImageLoader::InitializerTimingList*) + 425
frame #6: 0x000000010002bd08 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 474
frame #7: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
frame #8: 0x0000000100029bac dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 230
frame #9: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
frame #10: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
frame #11: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
frame #12: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
frame #13: 0x0000000100015025 dyld`_dyld_start + 37
(lldb)
_objc_init --> map_images -> map_images_nolock -> _read_images -> realizeClassWithoutSwift --> methodizeClass -> attachToClass
load_images --> loadAllCategories -> load_categories_nolock -> attachCategories
在_read_images中对类做实现操作,在load_images方法中触发分类的数据加载,即rwe初始化,同时加载分类数据。
主类没有+load,分类有+load:attachToClass attachCategories方法打断点*
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
* frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
frame #3: 0x00000001002e730f libobjc.A.dylib`_read_images(hList=0x00007ffeefbf21c0, hCount=296, totalClasses=24576, unoptimizedTotalClasses=24576) at objc-runtime-new.mm:3826:13
frame #4: 0x00000001002e6148 libobjc.A.dylib`map_images_nolock(mhCount=296, mhPaths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-os.mm:596:9
frame #5: 0x00000001002e5ca3 libobjc.A.dylib`map_images(count=296, paths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-runtime-new.mm:3155:12
frame #6: 0x0000000100019d2c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1778
frame #7: 0x0000000100019ecf dyld`dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) + 63
frame #8: 0x00007fff203438ef libdyld.dylib`_dyld_objc_notify_register + 113
frame #9: 0x00000001002e575a libobjc.A.dylib`_objc_init at objc-os.mm:939:5
frame #10: 0x00000001004636db libdispatch.dylib`_os_object_init + 13
frame #11: 0x0000000100474928 libdispatch.dylib`libdispatch_init + 285
frame #12: 0x00007fff2a53965b libSystem.B.dylib`libSystem_initializer + 224
frame #13: 0x0000000100031079 dyld`ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 559
frame #14: 0x0000000100031478 dyld`ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
frame #15: 0x000000010002bd1a dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 492
frame #16: 0x000000010002bc85 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 343
frame #17: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
frame #18: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
frame #19: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
frame #20: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
frame #21: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
frame #22: 0x0000000100015025 dyld`_dyld_start + 37
_objc_init --> map_images -> map_images_nolock -> _read_images -> realizeClassWithoutSwift --> methodizeClass -> attachToClass
数据加载在_read_images就加载数据,数据来自data,data在编译时期就已经完成,即data中除了类的数据,还有分类的数据,与类绑定在一起
主类有+load,分类没有+load:attachToClass attachToClass方法打断点*
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
* frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
frame #3: 0x00000001002e730f libobjc.A.dylib`_read_images(hList=0x00007ffeefbf21c0, hCount=296, totalClasses=24576, unoptimizedTotalClasses=24576) at objc-runtime-new.mm:3826:13
frame #4: 0x00000001002e6148 libobjc.A.dylib`map_images_nolock(mhCount=296, mhPaths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-os.mm:596:9
frame #5: 0x00000001002e5ca3 libobjc.A.dylib`map_images(count=296, paths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-runtime-new.mm:3155:12
frame #6: 0x0000000100019d2c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1778
frame #7: 0x0000000100019ecf dyld`dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) + 63
frame #8: 0x00007fff203438ef libdyld.dylib`_dyld_objc_notify_register + 113
frame #9: 0x00000001002e575a libobjc.A.dylib`_objc_init at objc-os.mm:939:5
frame #10: 0x00000001004636db libdispatch.dylib`_os_object_init + 13
frame #11: 0x0000000100474928 libdispatch.dylib`libdispatch_init + 285
frame #12: 0x00007fff2a53965b libSystem.B.dylib`libSystem_initializer + 224
frame #13: 0x0000000100031079 dyld`ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 559
frame #14: 0x0000000100031478 dyld`ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
frame #15: 0x000000010002bd1a dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 492
frame #16: 0x000000010002bc85 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 343
frame #17: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
frame #18: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
frame #19: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
frame #20: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
frame #21: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
frame #22: 0x0000000100015025 dyld`_dyld_start + 37
(lldb)
_objc_init --> map_images -> map_images_nolock -> _read_images -> realizeClassWithoutSwift --> methodizeClass -> attachToClass
数据加载在_read_images就加载数据,数据来自data,data在编译时期就已经完成,即data中除了类的数据,还有分类的数据,与类绑定在一起
主类没有+load,分类没有+load:attachToClass attachToClass方法打断点*
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=0x00000001000082a0, previously=0x00000001000082a0, flags=2) at objc-runtime-new.mm:1215:66
frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=0x00000001000082a0, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=0x00000001000082a0, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
frame #3: 0x0000000100326630 libobjc.A.dylib`realizeClassMaybeSwiftMaybeRelock(cls=0x00000001000082a0, lock=0x000000010036d080, leaveLocked=true) at objc-runtime-new.mm:2918:9
frame #4: 0x00000001003264f2 libobjc.A.dylib`realizeClassMaybeSwiftAndLeaveLocked(cls=0x00000001000082a0, lock=0x000000010036d080) at objc-runtime-new.mm:2941:12
* frame #5: 0x000000010031283d libobjc.A.dylib`realizeAndInitializeIfNeeded_locked(inst=0x00000001000082c8, cls=0x00000001000082a0, initialize=true) at objc-runtime-new.mm:6373:15
frame #6: 0x000000010031231a libobjc.A.dylib`lookUpImpOrForward(inst=0x00000001000082c8, sel="alloc", cls=0x00000001000082a0, behavior=11) at objc-runtime-new.mm:6488:11
frame #7: 0x00000001002ead1b libobjc.A.dylib`_objc_msgSend_uncached at objc-msg-x86_64.s:1153
frame #8: 0x000000010034ef87 libobjc.A.dylib`objc_alloc [inlined] callAlloc(cls=LGPerson, checkNil=true, allocWithZone=false) at NSObject.mm:1940:12
frame #9: 0x000000010034eede libobjc.A.dylib`objc_alloc(cls=LGPerson) at NSObject.mm:1956
frame #10: 0x0000000100003a89 KCObjcBuild`main(argc=1, argv=0x00007ffeefbff468) at main.m:25:28 [opt]
frame #11: 0x00007fff20357621 libdyld.dylib`start + 1
frame #12: 0x00007fff20357621 libdyld.dylib`start + 1
(lldb)
objc_msgSend -> lookUpImpOrForward -> realizeClassMaybeSwiftMaybeRelock -> realizeClassWithoutSwift -> methodizeClass -> attachToClass
数据加载推迟到第一次消息时。
一个主类,两个分类
主类有+load,分类都有+load:attachToClass attachCategories方法打断点*
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
* frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
frame #3: 0x00000001002e730f libobjc.A.dylib`_read_images(hList=0x00007ffeefbf21c0, hCount=296, totalClasses=24576, unoptimizedTotalClasses=24576) at objc-runtime-new.mm:3826:13
frame #4: 0x00000001002e6148 libobjc.A.dylib`map_images_nolock(mhCount=296, mhPaths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-os.mm:596:9
frame #5: 0x00000001002e5ca3 libobjc.A.dylib`map_images(count=296, paths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-runtime-new.mm:3155:12
frame #6: 0x0000000100019d2c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1778
frame #7: 0x0000000100019ecf dyld`dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) + 63
frame #8: 0x00007fff203438ef libdyld.dylib`_dyld_objc_notify_register + 113
frame #9: 0x00000001002e575a libobjc.A.dylib`_objc_init at objc-os.mm:939:5
frame #10: 0x00000001004636db libdispatch.dylib`_os_object_init + 13
frame #11: 0x0000000100474928 libdispatch.dylib`libdispatch_init + 285
frame #12: 0x00007fff2a53965b libSystem.B.dylib`libSystem_initializer + 224
frame #13: 0x0000000100031079 dyld`ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 559
frame #14: 0x0000000100031478 dyld`ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
frame #15: 0x000000010002bd1a dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 492
frame #16: 0x000000010002bc85 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 343
frame #17: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
frame #18: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
frame #19: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
frame #20: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
frame #21: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
frame #22: 0x0000000100015025 dyld`_dyld_start + 37
(lldb)
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x000000010032332e libobjc.A.dylib`attachCategories(cls=LGPerson, cats_list=0x00007ffeefbf5b68, cats_count=1, flags=8) at objc-runtime-new.mm:1432:62
frame #1: 0x00000001003259bb libobjc.A.dylib`load_categories_nolock(this=0x00007ffeefbf5bb0, catlist=0x0000000100004060)::$_6::operator()(category_t* const*) const at objc-runtime-new.mm:3205:25
frame #2: 0x000000010030b952 libobjc.A.dylib`load_categories_nolock(hi=0x0000000100745090) at objc-runtime-new.mm:3224:5
frame #3: 0x000000010030a9ba libobjc.A.dylib`loadAllCategories() at objc-runtime-new.mm:3232:9
frame #4: 0x00000001002e8ce6 libobjc.A.dylib`load_images(path="/private/tmp/objc.dst/usr/lib/libobjc.A.dylib", mh=0x00000001002e4000) at objc-runtime-new.mm:3250:9
frame #5: 0x000000010001628a dyld`dyld::notifySingle(dyld_image_states, ImageLoader const*, ImageLoader::InitializerTimingList*) + 425
frame #6: 0x000000010002bd08 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 474
frame #7: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
frame #8: 0x0000000100029bac dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 230
frame #9: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
frame #10: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
frame #11: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
frame #12: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
frame #13: 0x0000000100015025 dyld`_dyld_start + 37
(lldb)
_objc_init --> map_images -> map_images_nolock -> _read_images -> realizeClassWithoutSwift --> methodizeClass -> attachToClass
load_images --> loadAllCategories -> load_categories_nolock -> attachCategories
在_read_images中对类做实现操作,在load_images方法中触发分类的数据加载,即rwe初始化,同时加载分类数据。
主类没有+load,分类都有+load:attachToClass attachCategories方法打断点*
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x000000010032332e libobjc.A.dylib`attachCategories(cls=LGPerson, cats_list=0x00000001011117a0, cats_count=2, flags=1) at objc-runtime-new.mm:1432:62
frame #1: 0x000000010031e24b libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1229:17
frame #2: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
frame #3: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
frame #4: 0x000000010030abc7 libobjc.A.dylib`prepare_load_methods(mhdr=0x0000000100000000) at objc-runtime-new.mm:3966:9
frame #5: 0x00000001002e8d2d libobjc.A.dylib`load_images(path="/Users/xiaokai/Library/Developer/Xcode/DerivedData/objc-aiwtubulpcfbduezvurrpiupjzfn/Build/Products/Debug/KCObjcBuild", mh=0x0000000100000000) at objc-runtime-new.mm:3261:9
frame #6: 0x000000010001628a dyld`dyld::notifySingle(dyld_image_states, ImageLoader const*, ImageLoader::InitializerTimingList*) + 425
frame #7: 0x000000010002bd08 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 474
frame #8: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
frame #9: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
frame #10: 0x000000010001662f dyld`dyld::initializeMainExecutable() + 199
frame #11: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
frame #12: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
frame #13: 0x0000000100015025 dyld`_dyld_start + 37
(lldb)
load_images --> prepare_load_methods -> realizeClassWithoutSwift -> methodizeClass -> attachToClass -> attachCategories
只要分类实现了load,会迫使主类提前加载。
主类有+load,分类都没有+load:attachToClass attachToClass方法打断点*
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
frame #3: 0x00000001002e730f libobjc.A.dylib`_read_images(hList=0x00007ffeefbf21c0, hCount=296, totalClasses=24576, unoptimizedTotalClasses=24576) at objc-runtime-new.mm:3826:13
frame #4: 0x00000001002e6148 libobjc.A.dylib`map_images_nolock(mhCount=296, mhPaths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-os.mm:596:9
* frame #5: 0x00000001002e5ca3 libobjc.A.dylib`map_images(count=296, paths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-runtime-new.mm:3155:12
frame #6: 0x0000000100019d2c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1778
frame #7: 0x0000000100019ecf dyld`dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) + 63
frame #8: 0x00007fff203438ef libdyld.dylib`_dyld_objc_notify_register + 113
frame #9: 0x00000001002e575a libobjc.A.dylib`_objc_init at objc-os.mm:939:5
frame #10: 0x00000001004636db libdispatch.dylib`_os_object_init + 13
frame #11: 0x0000000100474928 libdispatch.dylib`libdispatch_init + 285
frame #12: 0x00007fff2a53965b libSystem.B.dylib`libSystem_initializer + 224
frame #13: 0x0000000100031079 dyld`ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 559
frame #14: 0x0000000100031478 dyld`ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
frame #15: 0x000000010002bd1a dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 492
frame #16: 0x000000010002bc85 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 343
frame #17: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
frame #18: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
frame #19: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
frame #20: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
frame #21: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
frame #22: 0x0000000100015025 dyld`_dyld_start + 37
(lldb)
_objc_init -> map_images -> map_images_nolock -> _read_images -> realizeClassWithoutSwift -> methodizeClass -> attachToClass
数据加载在_read_images就加载数据,数据来自data,data在编译时期就已经完成,即data中除了类的数据,还有分类的数据,与类绑定在一起
主类没有+load,分类都没有+load:attachToClass attachToClass方法打断点
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
* frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
frame #3: 0x0000000100326630 libobjc.A.dylib`realizeClassMaybeSwiftMaybeRelock(cls=LGPerson, lock=0x000000010036d080, leaveLocked=false) at objc-runtime-new.mm:2918:9
frame #4: 0x000000010031662f libobjc.A.dylib`realizeClassMaybeSwiftAndUnlock(cls=LGPerson, lock=0x000000010036d080) at objc-runtime-new.mm:2935:12
frame #5: 0x0000000100309300 libobjc.A.dylib`initializeAndMaybeRelock(cls=0x00000001000082e8, inst=0x0000000100008310, lock=0x000000010036d080, leaveLocked=true) at objc-runtime-new.mm:2244:19
frame #6: 0x000000010032a1ea libobjc.A.dylib`initializeAndLeaveLocked(cls=0x00000001000082e8, obj=0x0000000100008310, lock=0x000000010036d080) at objc-runtime-new.mm:2269:12
frame #7: 0x0000000100312885 libobjc.A.dylib`realizeAndInitializeIfNeeded_locked(inst=0x0000000100008310, cls=0x00000001000082e8, initialize=true) at objc-runtime-new.mm:6378:15
frame #8: 0x000000010031231a libobjc.A.dylib`lookUpImpOrForward(inst=0x0000000100008310, sel="alloc", cls=0x00000001000082e8, behavior=11) at objc-runtime-new.mm:6488:11
frame #9: 0x00000001002ead1b libobjc.A.dylib`_objc_msgSend_uncached at objc-msg-x86_64.s:1153
frame #10: 0x000000010034ef87 libobjc.A.dylib`objc_alloc [inlined] callAlloc(cls=LGPerson, checkNil=true, allocWithZone=false) at NSObject.mm:1940:12
frame #11: 0x000000010034eede libobjc.A.dylib`objc_alloc(cls=LGPerson) at NSObject.mm:1956
frame #12: 0x0000000100003989 KCObjcBuild`main(argc=1, argv=0x00007ffeefbff468) at main.m:25:28 [opt]
frame #13: 0x00007fff20357621 libdyld.dylib`start + 1
frame #14: 0x00007fff20357621 libdyld.dylib`start + 1
objc_msgSend -> lookUpImpOrForward -> realizeClassMaybeSwiftMaybeRelock -> realizeClassWithoutSwift -> methodizeClass -> attachToClass
数据加载推迟到第一次消息时。
网友评论