EdgeProfiler sample program is broken

Topics: Metadata Model
Aug 19, 2011 at 6:22 AM

I made a very simple shell (nothing in it) class library called TestLibrary--in the same solution as ccimetadata samples download.  Then I used that library as the first parameter of args [] in the EdgeProfiler sample Program.Main static method.  I set argument [0] to "C:\\Users\\[user name]\\Downloads\\ccimetadata-64629\\TestLibrary\\bin\\Debug\\TestLibrary.dll". 

The code fails here:

C:\Users\[user name]\Downloads\ccimetadata-64629\Samples\EdgeProfiler\AddInstrumentation.cs

    private IMethodReference InjectNewEntryPoint(IMethodDefinition oldEntryPoint) {
      var containingType = (NamespaceTypeDefinition)oldEntryPoint.ContainingTypeDefinition;  // <=== get InvalidCastException

While debugging, I checked to see what the type was for "oldEntryPoint.ContainingTypeDefinition" object--it's in bold below.  That's why it can't be cast.

+        oldEntryPoint.ContainingTypeDefinition.UnderlyingType    {Microsoft.Cci.DummyTypeReference}    Microsoft.Cci.ITypeReference {Microsoft.Cci.DummyTypeReference}

Call Stack (line count might be off in Program--I added a few lines in Main, but nothing that would effect how it normally functions):

>    EdgeProfiler.exe!EdgeProfiler.Instrumenter.InjectNewEntryPoint(Microsoft.Cci.IMethodDefinition oldEntryPoint = {Microsoft.Cci.DummyMethodDefinition}) Line 126    C#
     EdgeProfiler.exe!EdgeProfiler.Instrumenter.RewriteChildren(Microsoft.Cci.MutableCodeModel.Assembly assembly = {Microsoft.Cci.MutableCodeModel.Assembly}) Line 42 + 0x36 bytes    C#
     Microsoft.Cci.MutableMetadataModel.dll!Microsoft.Cci.MutableCodeModel.MetadataRewriter.Rewrite(Microsoft.Cci.IAssembly assembly = {Microsoft.Cci.MutableCodeModel.Assembly}) Line 260 + 0xe bytes    C#
     Microsoft.Cci.MutableMetadataModel.dll!Microsoft.Cci.MutableCodeModel.MetadataRewriter.Rewrite(Microsoft.Cci.IModule module = {Microsoft.Cci.MutableCodeModel.Assembly}) Line 623 + 0x1e bytes    C#
     EdgeProfiler.exe!EdgeProfiler.Instrumenter.GetInstrumented(Microsoft.Cci.IMetadataHost host = {Microsoft.Cci.PeReader.DefaultHost}, Microsoft.Cci.IModule module = {Microsoft.Cci.MetadataReader.ObjectModelImplementation.Assembly}, Microsoft.Cci.PdbReader pdbReader = {Microsoft.Cci.PdbReader}, Microsoft.Cci.INamespaceTypeDefinition logger = {Microsoft.Cci.MetadataReader.ObjectModelImplementation.NonGenericNamespaceTypeWithoutPrimitiveType}) Line 21 + 0x35 bytes    C#
     EdgeProfiler.exe!EdgeProfiler.Program.Main(string[] args = {string[1]}) Line 47 + 0x11 bytes    C#
     [External Code]   

Aug 19, 2011 at 2:12 PM

Unfortunately the edgeprofiler sample is too simplistic to deal with libraries. It can only profile executables.

Aug 19, 2011 at 7:09 PM
Edited Aug 19, 2011 at 7:11 PM

Is there a sample in CCI to traverse libraries?  How about one that traverses code in a *.csproj or *.vbproj that's not a library (ie: web application or web service application or something)?  And secondly, would you be able to give me some tips/tricks to do so if there's not a solution yet?  I will spend the effort on it.

Aug 22, 2011 at 2:52 PM

I was able to figure out how to tokenize a class library using standard .NET libraries this weekend, but not anywhere near what your edge profiler is doing.  I also didn't do any of the instrumentation you have in that sample project.  I ordered the compiler book you mentioned.  I'll learn more about compilers before I ask you anymore questions that the book may help me with.