> > In any case, I'd like to have a proper fix for the problem.
>
> Same goes for me.

Okay okay ... I will try to put together a clean fix for this problem in
a couple days.

> And I'd also like to know, what these image flags mean, after all.

Which image flags do you mean?

> And, by the way, is Get/SetILFunctionBody only broken in Rotor, or is
it
> the same with the CLR? Because I was getting BadImageFormatExceptions 
> (at least I think so, they might also have been
InvalidProgramExceptions)
> with the "real" CLR, too: when I tried to replace a method in
mscorlib,
> with Rotor I had the problem described by Nam, with the CLR I got
these
> exceptions.

The "real" CLR should be fine. I have tried the following with the CLR
v1.0.3705:

1. download the dnprofiler sample from
http://msdn.microsoft.com/msdnmag/issues/01/12/hood/default.aspx

2. Replaced CProfilerCallback::JITCompilationStarted method in the
sample with the attached code fragment. This code fragment expects
Console.WriteLine(int) in mscorlib to be called, and changes it to
always print 1.

3. Build the modified dnprofiler sample and register it by running
profiling_on.bat. 

4. Compile and run the following program:

using System;

class My {
   static void Main() {
       Console.WriteLine(2);
       Console.WriteLine(3);
       Console.WriteLine(4);
   }
}

It prints "1 1 1" as expected.

5. Don't forget to unregister the profiler once you are done with the
experiments...


Can you try to reproduce this on your machine? If you can come up with a
short example of what did not worked for you, I can take a look...

-Jan

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------
HRESULT CProfilerCallback::JITCompilationStarted(UINT functionId, BOOL
fIsSafeToBlock) {
    wchar_t wszClass[512];
    wchar_t wszMethod[512];

    if (GetMethodNameFromFunctionId(functionId, wszClass, wszMethod))
    {
        if (wcscmp(L"System.Console", wszClass) == 0 && 
            wcscmp(L"WriteLine", wszMethod) == 0) {
            ClassID classId = 0;
            ModuleID moduleId = 0;
            mdToken tkMethod = 0;
            IMethodMalloc* pMalloc = NULL;
            LPBYTE pReplacedMethod;
            LPCBYTE pMethodBytes;
            ULONG cbMethodSize;

            Check(m_pICorProfilerInfo->GetFunctionInfo(
                functionId, &classId, &moduleId, &tkMethod));

            // get the code of the original function
            Check(m_pICorProfilerInfo->GetILFunctionBody(
                moduleId, tkMethod, &pMethodBytes, &cbMethodSize));

            Check(m_pICorProfilerInfo->GetILFunctionBodyAllocator(
moduleId, &pMalloc ));
            pReplacedMethod = (LPBYTE)pMalloc->Alloc(cbMethodSize);

            memcpy(pReplacedMethod, pMethodBytes, cbMethodSize);

            // replate ldarg.0 (opcode 02) with ldc.i4.1 (opcode 17)
            if (pReplacedMethod[6] != 0x02) {
                fprintf(stderr, "Unexpected opcode");
                exit(1);
            }

            pReplacedMethod[6] = 0x17;
            
            Check(m_pICorProfilerInfo->SetILFunctionBody(
                moduleId, tkMethod, (LPCBYTE)pReplacedMethod));

            pMalloc->Release();
            fprintf(stderr, "Replaced\n");
        }

        ProfilerPrintf("JITCompilationStarted:
%ls::%ls\n",wszClass,wszMethod);
    }
    else
    {
        ProfilerPrintf( "JITCompilationStarted\n" );
    }

    ChangeNestingLevel( 1 );
    return S_OK;
}
--------------

Reply via email to