博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

探索 sscli, 开启 CLR 的大门 —— 配置环境详解

Posted on 2008-03-17 19:28  Aplo  阅读(3891)  评论(6编辑  收藏  举报

    (博主:好久没有写东西了,真不知道该如何下笔,也许我的思路比较混乱,还请大家见谅。)

心得:
      以前我一直是做dotnet开发,对于整个clr的架构非常感兴趣。而且对整个运行环境也进行过一些分析与跟踪。在这里给希望研究 CLR 内部原理以及实现的用户一个建议:sscli 不等于 DotNet Framework,不等于CLR。在分析 sscli 更多的时候是参考其内部的数据结构和大体的代码流程而不是具体实现,如果你用WinDbg或是OllyDbg跟踪过CLR,你会发现 sscli 和你所看到的根本不相同。

配置及环境:
一.代码分析工具
      在 sscli 茫茫的代码中确实需要一个有效的分析工具,我这里选择的是Source Insight,它可以帮助你找到相关类型,并且有效的定位到代码的声明或实现的地方。
图1:新建一个项目,sscli,项目文件保存地点需要与你的 sscli 文件夹存放在同一个磁盘上


2.


3.选择sscli20中的CLR文件夹点击 AddTree 按钮,他会寻找到所有相关代码


4.进行文件同步,source insight就会同步所有代码文件。


3.分析后的代码


二.CLR调试工具
      WinDbg 利用微软提供的调试 symbol 你可以真实的跟踪出具体实现的函数以及类型(从这点你就可以发现 sscli 并不是微软的实现代码)。具体配置如下:

 1.设置symbol,我这里选择的 D:\WinSymbols 文件夹你可选择别的地方


2.加载dotnet程序进行调试,几个比较实用的命令 lm 列出所有加载的模块, ld 加载制定模块,例如:ld mscoree,列出指定模块symbol:x /n mscoree!*


之后就可以进行 CLR 的调试。如图:调用堆栈显示 CLR 的执行过程。


下面列表就是使用 x /n mscorjit! 命令列出 JIT 编译器实现的一部分接口(这里只粘贴了一部分):
// 获取即时编译对象
790a6082 mscorjit!getJit = <no type information>

790a60a0 mscorjit!CILJit::`vftable' = <no type information>
7906edef mscorjit!CILJit::clearCache = <no type information>
7906114f mscorjit!CILJit::compileMethod = <no type information>                    // 编译 IL 代码 (jit层源代码保护一般来说就是在这里实现的,详情请参看我以前写的文章,MaxToCode DNGuard HVM 都可以由此分析破解)
790610a0 mscorjit!CILJit::isCacheCleanupRequired = <no type information>


7908f1b2 mscorjit!Compiler::EHblkDsc::CheckIsInHandlerRegion = <no type information>
790867ff mscorjit!Compiler::EHblkDsc::CheckIsInTryRegion = <no type information>
79085d2e mscorjit!Compiler::EHblkDsc::IsInFilterRegion = <no type information>
790a5515 mscorjit!Compiler::EHblkDsc::IsInHandlerRegion = <no type information>
79066e44 mscorjit!Compiler::FlatFPAllocFPState = <no type information>
...
.....
.....
.....
7909af92 mscorjit!Compiler::verVerifyCall = <no type information>
7909bbf9 mscorjit!Compiler::verVerifyCond = <no type information>
7909ba31 mscorjit!Compiler::verVerifyField = <no type information>
790a086e mscorjit!Compiler::verVerifyLDIND = <no type information>
790a0805 mscorjit!Compiler::verVerifySTIND = <no type information>
7909aae3 mscorjit!Compiler::verVerifyThisPtrInitialised = <no type information>


以上仅仅是我经过一些分析的心得,在此与大家分享,希望大家踊跃拍砖 :>