在前一篇博文中,在好奇心的驅使下,探秘了 dotnet run ,發現了神秘的 corehost —— 運行 .NET Core 應用程序的幕后英雄。有時神秘就是一種誘惑,神秘的 corehost 讓人產生了新的好奇心 —— corehost 是如何加載 coreclr 的?於是,“.NET跨平台之旅”開啟了新的旅程 —— 帶着這個疑問,游覽 cli/src/corehost/ 。
corehost 的入口是 corehost.cpp 的 main() ,進來后一條大道通向 corehost.run() 。
在 corehost.run() 中,首先調用的是 libhost.cpp 中的 detect_operating_mode() ,它根據 coreclr 所在的路徑決定 corehost 的運行模式,有三種運行模式:muxer, standalone, split-fx。如果 corehost 與 coreclr 不在同一個文件夾,運行模式則是 muxer 。如果 corehost 與 coreclr 在同一個文件夾,並且文件夾下存在 .deps.json 文件或者不存在 .runtimeconfig.json 文件,則是 standalone 模式;否則是 split-fx 模式。
return ((pal::file_exists(own_deps_json) || !pal::file_exists(own_config_filename)) && pal::file_exists(own_dll)) ? host_mode_t::standalone : host_mode_t::split_fx;
dotent cli 默認使用的模式是 split-fx 模式,我們的示例站點 about.cnblogs.com 用的也是這種模式,通過下面的 tracing 信息可以看出來(export COREHOST_TRACE=2):
Checking if CoreCLR path exists=[/usr/share/dotnet-nightly/bin/libcoreclr.so] Detecting mode... CoreCLR present in own dir [/usr/share/dotnet-nightly/bin] and checking if [corehost.deps.json] file present=[0] Host operating in split mode; own dir=[/usr/share/dotnet-nightly/bin]
這次旅程也因此選擇“split-fx 模式”這條游覽路線。
針對 split-fx 模式,corehost.run() 接着會調用 hostpolicy.cpp 的 run() 方法(通過 corehost_init_t ),run() 方法中調用 deps_resolver.cpp 的 resolve_coreclr_dir() 解析 coreclr 所在的路徑,然后調用 coreclr.cpp 的 bind() 與 initialize() 方法加載 coreclr 。
在 bind() 方法中,根據之前解析出的 coreclr 路徑,調用 pal.unix.cpp(針對的是Linux運行環境)的 load_library() 打開 coreclr 的庫文件 libcoreclr.so (實際是調用 Linux 的 C 函數 dlopen() ),從而得到 coreclr 中3個函數(coreclr_initialize, coreclr_shutdown, coreclr_execute_assembly)的句柄。
在 initialize() 方法中,根據 bind() 中得到的句柄調用 coreclr 的 coreclr_initialize() 方法啟動 coreclr , 加載 coreclr 的工作就這樣完成了。
找到 coreclr 庫文件所在的位置,打開它,調用它的 coreclr_initialize() 方法,corehost 加載 coreclr 就這么簡單。如果你有興趣,可以用 C++ 寫一個自己的 corehost 。
開源的 .NET 變得更有意思,即使沒有任何文檔,即使沒有正式發布,你也可以通過源碼學習它,了解它。