惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
博客园_首页
博客园 - 【当耐特】
WordPress大学
WordPress大学
月光博客
月光博客
博客园 - 叶小钗
S
SegmentFault 最新的问题
雷峰网
雷峰网
H
Help Net Security
宝玉的分享
宝玉的分享
A
About on SuperTechFans
IT之家
IT之家
J
Java Code Geeks
Hugging Face - Blog
Hugging Face - Blog
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
B
Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog

博客园 - xwang

更好的执行 Better Communication – 你在听吗? 时间管理 or 能量管理 Stress Managment - 压力管理 Understanding WPF Template sscli google group launched Understanding Static Constructor, 理解静态构造函数 - xwang 自定义Rotor: 给Rotor添加一个CIL指令 PAL 阅读笔记 Metadata Tables, 元数据表 [Coding For Fun] 您有运行许可证吗? How To: TeamBuild 编译之前的版本 How To: Team Build 自定义版本号 混淆的概念与实践 [Debugging Tool]Windbg调试托管代码 The Anatomy Of Basic Data Structure, 基本数据结构解析 Understanding GetHashCode, 理解GetHashCode 基本数据结构解析之List 试探讨泛型实现过程
调试Rotor: 启动过程
xwang · 2009-01-08 · via 博客园 - xwang

引言:

我已经深入一些书籍好长一段时间了, 包括(Essential.Net, Shared Source CLI, Shared Source Internals, Distributed Virtual machine, ECMA335), 然而纸上得来终觉浅,绝知此事要躬行, 我的默认试验环境是:

  • Windows 2008 / Visual Studio 2008 SP1 / .Net Framework 3.5 SP1 / SSCLI2.0

在本文中,我将在Visual Studio 2008中使用一个简单的例子来解释Rotor的启动过程

例子代码文件: %Rotor_DIR%/Samples/Hello/hello.cs

如何Debug?

如果您不知道,可以看这里, 简单的步骤如下面所示

  • cd E:/Research/MyRotor                  // 请根据您的Rotor目录修改
  • env                                                // 设置环境变量
  • cd samples/hello
  • csc /debug hello.cs                         // 编译代码
  • devenv /debugexe clix hello.exe      // 调试clix

按F11就可以到程序执行的第一行代码了

图 1: 调试的第一行代码

这个文件在 %Rotor_DIR%\palrt\inc\palstartup.h

启动过程

Figure 2:  大概的启动过程

Figure 3: 启动中主要执行的过程

我已经进行一次启动过程的调试,启动过程如下图所示.

Figure 4: the loading process in sscli2.0

代码文件:

  • \pal\win32\win32pal.c
  • \clr\src\tools\clix\clix.cpp
  • \palrt\sscoree\sscoree_int.cpp
  • \clr\src\vm\ceemain.cpp
  • \clr\src\vm\appdomain.cpp

ExecuteMainMethod中的一些执行过程放在了下面的代码中,有兴趣的读者不妨一读

Code
Thread *pThread = GetThread();

FrameWithCookie

<ContextTransitionFrame> frame;
pThread
->EnterContextRestricted(SystemDomain::System()->DefaultDomain()->GetDefaultContext(), &frame);

AppDomain 

*pDomain = GetAppDomain();

FrameWithCookie

<DebuggerClassInitMarkFrame> __dcimf;

PEImageHolder pTempImage(PEImage::OpenImage(path));

if (!pTempImage->CheckILFormat())
{
    ThrowHR(COR_E_BADIMAGEFORMAT);
}
PEFileHolder pTempFile(PEFile::Open(pTempImage.Extract()));
// Check for CustomAttributes - Set up the DefaultDomain and the main thread
// Note that this has to be done before ExplicitBind() as it
// affects the bind
mdToken tkEntryPoint = pTempFile->GetEntryPointToken();
    ReleaseHolder
<IMDInternalImport> scope(pTempFile->GetMDImportWithRef());// This can potentially run managed code.
InitializeDefaultDomain(FALSE);if((!IsNilToken(tkEntryPoint)) && (TypeFromToken(tkEntryPoint) == mdtMethodDef))
    SystemDomain::SetDefaultDomainAttributes(scope, tkEntryPoint);

NewHolder

<PEFileSecurityDescriptor> pSecDesc(new PEFileSecurityDescriptor(pDomain, pTempFile));
Security::Resolve(pSecDesc);
if (Security::AllowBindingRedirects(pSecDesc))
    pDomain
->TurnOnBindingRedirects();

SafeComHolder

<IAssembly> pFusionAssembly;
SafeComHolder
<IFusionBindLog> pFusionLog;

IfFailThrow(ExplicitBind(path, pDomain

->GetFusionContext(),
                     EXPLICITBIND_FLAGS_EXE,
                     NULL, 
&pFusionAssembly, NULL, &pFusionLog));
PEAssemblyHolder pFile(PEAssembly::Open(pFusionAssembly, NULL, pFusionLog));

pDomain

->m_pRootAssembly = GetAppDomain()->LoadAssembly(NULL, pFile, FILE_ACTIVE);
if (CorCommandLine::m_pwszAppFullName == NULL) {
    StackSString friendlyName;
    StackSString assemblyPath 
= pFile->GetPath();
    SString::Iterator i 
= assemblyPath.End();if (PEAssembly::FindLastPathSeparator(assemblyPath, i)) {
        i
++;
        friendlyName.Set(assemblyPath, i, assemblyPath.End());
    }
    
else
        friendlyName.Set(assemblyPath);

    pDomain

->SetFriendlyName(friendlyName, TRUE);
}

__dcimf.Pop();

结语

本来想再细致点,慢慢来吧,累,而且俺还懒。还有很多东西值得深入,比如meta token的验证,cil代码的验证, Main 方法的即时编译(JIT),先留着,:)

参考:

distributed virtual machines - inside the rotor cli

Under the hood of the CLR Managed Execution