The objc4-818.2 source code is so cool, nothing is more direct and true than Apple's source code

Preface
Why do you want to debug the source code?
Apple has open sourced part of the source code, but there are too many similar contents, and it is almost impossible to find the corresponding relationship between the codes. It would be great if you can jump like your own project~~
Apple source code open source address: opensource.apple.com/This
article will be configured with the source code of macOS 11.2/objc4-818.2 Source

code configuration
First select and run the target: objc > My Mac
and then ⌘+b to compile
and look at the hot spots to see what errors there are, and then Find the corresponding solution below~
compile error 'sys/reason.h' file not found

1


Solution: Enter reason.h site:opensource.apple.com in Google for directional search

1


Click in the search results and perform "Save File As" download
After downloading, create a folder "ZKLib" in the root directory of the objc source code,
and because the reason.h file is in the sys directory, we are in the "ZKLib" directory Create a "sys" folder, and put reason.h into this directory (as shown below)

have


Next, we search for "Header Search Paths" in the source code project target > objc > Build Settings, and add $(SRCROOT)/ZKLib to both Debug and Release (no need to pull ZKLib
into the source code Xcode project! )\

236Fjpg

然后继续⌘+b编译

其它文件缺失
我们会发现相同的报错还有\

'mach-o/dyld_priv.h' file not found
'os/lock_private.h' file not found
'os/base_private.h' file not found
'pthread/tsd_private.h' file not found
'System/machine/cpu_capabilities.h' file not found
'os/tsd.h' file not found
'pthread/spinlock_private.h' file not found
'System/pthread_machdep.h' file not found
'CrashReporterClient.h' file not found
'objc-shared-cache.h' file not found
'_simple.h' file not found
'Block_private.h' file not found
复制代码

这些报错的解决方案都跟上面的一样处理

objc源码文件代码修改
CrashReporterClient明明放进去了还是"file not found"
打开CrashReporterClient.h文件,

B1A31ED240CC86F1

在#ifdef前面加上以下代码:

#define LIBC_NO_LIBCRASHREPORTERCLIENT
复制代码

注释掉一些妨碍编译的代码
总的来说一共有这么多:

CDD610A630B41F261

具体都有:
objc-cache.mm

87行:

#if TARGET_OS_OSX

//#include <Cambria/Traps.h>

//#include <Cambria/Cambria.h>

#endif



1120行:

//#if TARGET_OS_OSX

// if (oah_is_current_process_translated()) {

// kern_return_t ret = objc_thread_get_rip(threads[count], (uint64_t*)&pc);

// if (ret != KERN_SUCCESS) {

// pc = PC_SENTINEL;

// }

// } else {

// pc = _get_pc_for_thread (threads[count]);

// }

//#else

pc = _get_pc_for_thread (threads[count]);

//#endif
复制代码

NSObject.mm

42行:

//#include <os/feature_private.h>

extern "C" {

//#include <os/reason_private.h>

//#include <os/variant_private.h>

}

1185行:

// if (DebugPoolAllocation || sdkIsAtLeast(10_12, 10_0, 10_0, 3_0, 2_0)) {

// // OBJC_DEBUG_POOL_ALLOCATION or new SDK. Bad pop is fatal.

// _objc_fatal

// ("Invalid or prematurely-freed autorelease pool %p.", token);

// }
复制代码

objc-runtime.mm

36行:

//#include <os/feature_private.h> // os_feature_enabled_simple()

379行:

// if (!dyld_program_sdk_at_least(dyld_fall_2020_os_versions))

// DisableAutoreleaseCoalescingLRU = true;

444行:

// if (!os_feature_enabled_simple(objc4, preoptimizedCaches, true)) {

// DisablePreoptCaches = true;

// }
复制代码

objc-class.mm

896行:

// LINKER_SET_FOREACH(_dupi, const objc_duplicate_class **, "__objc_dupclass") {

// const objc_duplicate_class *dupi = *_dupi;

//

// if (strcmp(dupi->name, name) == 0) {

// return;

// }

// }
复制代码

objc-os.mm

31行:

//#include "objc-bp-assist.h"

\


567行:

// if (!dyld_program_sdk_at_least(dyld_platform_version_macOS_10_13)) {

// DisableInitializeForkSafety = true;

// if (PrintInitializing) {

// _objc_inform("INITIALIZE: disabling +initialize fork "

// "safety enforcement because the app is "

// "too old.)");

// }

// }
复制代码

objc-runtime-new.mm

3547行:

// if (!dyld_program_sdk_at_least(dyld_platform_version_macOS_10_11)) {

// DisableNonpointerIsa = true;

// if (PrintRawIsa) {

// _objc_inform("RAW ISA: disabling non-pointer isa because "

// "the app is too old.");

// }

// }

8328行:

if (!DisableTaggedPointerObfuscation /**&& dyld_program_sdk_at_least(dyld_fall_2018_os_versions)*/) {
复制代码



编译报错 Can't open order file: libobjc.order
具体来说是这么一个报错:

108E12F56660430B


解决方案:
在源码工程 target > objc > Build Settings中搜索"Order File", 把Debug和Release的内容都改成$(SRCROOT)/libobjc.order即可

B8565599EE3C1AA4328



编译报错 Library not found for -lCrashReporterClient
在源码工程 target > objc > Build Settings中搜索"Other Linker Flags", 把Debug和Release中的-lCrashReporterClient都删掉

12CB90BFB4CB4334F4A6D16C883



编译报错 SDK "macosx.internal" cannot be located.
具体来说是这么一个报错:

3A3F2C6FCA7907F85F246BA3FB6CE


解决方案:
在源码工程 target > objc > Build Phases中找到Run Script(markgc)里面, 把脚本中的macosx.internal改成macosx即可

3F87BCFCB85882A6577CB116AF622



编译报错 library not found for -loah
具体来说是这么一个报错:

CCD2C7382CEA4925E65623274BD


解决方案:
在源码工程 target > objc > Build Settings中搜索"Other Linker Flags", 把Debug和Release里的-loah删掉

BEA870E5A01FA2A91A7A1CF957F



编译报错 '_static_assert' declared as an array with a negative size
具体来说是这么一个报错:

75897FCD2F89530DF8C50AB02DA


解决方案:
把报错的这两行注释掉~~

编译成功!
诶嘿嘿~
是不是突然间看到"Build Successed"就很兴奋了, 反正我是兴奋了


编译调试
新建一个target
选择macOS里面的Command Line Tool (我们这里不需要界面)

53AB1F22727A3DAAF2A3D2

将target命名为ZKBuild (如下图)

F1225E24BF6F32F5C564F6C4CA019E



Binding dependencies
In the source code project target > ZKBuild > Build Phases
Add objc in Dependencies

A4F0E19E37C5EB0F94BC7748B8A



Break point for debugging
Switch running target: ZKBuild > My Mac
Break point in main.m code, and press ⌘+r to run

443F57D481E69A06BEAC21A5D11

At this point press

Screenshot 2021-06-06 AM 1.15.18.png

(step into) into this line of code

99B788741BA5DDAB5CF3A413E607


When you come to the objc source code, it means that you have successfully debugged the objc source code

Guess you like

Origin juejin.im/post/7149434056529149965