Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / MSIL

ILRewriting for beginners

Rate me:
Please Sign up or sign in to vote.
4.56/5 (15 votes)
27 Sep 2012Ms-PL11 min read 62K   1.4K   46  
Runtime IL-Rewriting can be used to add behavior such as logging to applications, or redirect calls from one API to another. This article and accompanying source code explains how to substitute a method call at runtime.
// ----------------------------------------------------------------------------------------------
// Copyright (c) Mattias H�gstr�m.
// ----------------------------------------------------------------------------------------------
// This source code is subject to terms and conditions of the Microsoft Public License. A 
// copy of the license can be found in the License.html file at the root of this distribution. 
// If you cannot locate the Microsoft Public License, please send an email to 
// dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
// by the terms of the Microsoft Public License.
// ----------------------------------------------------------------------------------------------
// You must not remove this notice, or any other, from this software.
// ----------------------------------------------------------------------------------------------

#pragma once

#include <Windows.h>

#include <cor.h>
#include <corprof.h>

#include "ILRewriteProfiler_h.h"
#include "ILRewriteHelper.h"

class ILRewriteProfilerImpl : 
   public ICorProfilerCallback3,
   public ILRewriteProfiler
{
public :
   ILRewriteProfilerImpl() {};
   ~ILRewriteProfilerImpl() {};

   STDMETHOD(QueryInterface)(REFIID riid, void **ppObj);
   ULONG STDMETHODCALLTYPE AddRef();
   ULONG STDMETHODCALLTYPE Release();
   STDMETHOD(Initialize)(IUnknown *pICorProfilerInfoUnk);
   STDMETHOD(Shutdown)();
   STDMETHOD(AppDomainCreationStarted)(AppDomainID appDomainID);
   STDMETHOD(AppDomainCreationFinished)(AppDomainID appDomainID, HRESULT hrStatus);
   STDMETHOD(AppDomainShutdownStarted)(AppDomainID appDomainID);
   STDMETHOD(AppDomainShutdownFinished)(AppDomainID appDomainID, HRESULT hrStatus);
   STDMETHOD(AssemblyLoadStarted)(AssemblyID assemblyID);
   STDMETHOD(AssemblyLoadFinished)(AssemblyID assemblyID, HRESULT hrStatus);
   STDMETHOD(AssemblyUnloadStarted)(AssemblyID assemblyID);
   STDMETHOD(AssemblyUnloadFinished)(AssemblyID assemblyID, HRESULT hrStatus);
   STDMETHOD(ModuleLoadStarted)(ModuleID moduleID);
   STDMETHOD(ModuleLoadFinished)(ModuleID moduleID, HRESULT hrStatus);
   STDMETHOD(ModuleUnloadStarted)(ModuleID moduleID);
   STDMETHOD(ModuleUnloadFinished)(ModuleID moduleID, HRESULT hrStatus);
   STDMETHOD(ModuleAttachedToAssembly)(ModuleID moduleID, AssemblyID assemblyID);
   STDMETHOD(ClassLoadStarted)(ClassID classID);
   STDMETHOD(ClassLoadFinished)(ClassID classID, HRESULT hrStatus);
   STDMETHOD(ClassUnloadStarted)(ClassID classID);
   STDMETHOD(ClassUnloadFinished)(ClassID classID, HRESULT hrStatus);
   STDMETHOD(FunctionUnloadStarted)(FunctionID functionID);
   STDMETHOD(JITCompilationStarted)(FunctionID functionID, BOOL fIsSafeToBlock);
   STDMETHOD(JITCompilationFinished)(FunctionID functionID, HRESULT hrStatus, BOOL fIsSafeToBlock);
   STDMETHOD(JITCachedFunctionSearchStarted)(FunctionID functionID, BOOL *pbUseCachedFunction);
   STDMETHOD(JITCachedFunctionSearchFinished)(FunctionID functionID, COR_PRF_JIT_CACHE result);
   STDMETHOD(JITFunctionPitched)(FunctionID functionID);
   STDMETHOD(JITInlining)(FunctionID callerID, FunctionID calleeID, BOOL *pfShouldInline);
   STDMETHOD(ThreadCreated)(ThreadID threadID);
   STDMETHOD(ThreadDestroyed)(ThreadID threadID);
   STDMETHOD(ThreadAssignedToOSThread)(ThreadID managedThreadID, DWORD osThreadID);
   STDMETHOD(RemotingClientInvocationStarted)();
   STDMETHOD(RemotingClientSendingMessage)(GUID *pCookie, BOOL fIsAsync);
   STDMETHOD(RemotingClientReceivingReply)(GUID *pCookie, BOOL fIsAsync);
   STDMETHOD(RemotingClientInvocationFinished)();
   STDMETHOD(RemotingServerReceivingMessage)(GUID *pCookie, BOOL fIsAsync);
   STDMETHOD(RemotingServerInvocationStarted)();
   STDMETHOD(RemotingServerInvocationReturned)();
   STDMETHOD(RemotingServerSendingReply)(GUID *pCookie, BOOL fIsAsync);
   STDMETHOD(UnmanagedToManagedTransition)(FunctionID functionID, COR_PRF_TRANSITION_REASON reason);
   STDMETHOD(ManagedToUnmanagedTransition)(FunctionID functionID, COR_PRF_TRANSITION_REASON reason);
   STDMETHOD(RuntimeSuspendStarted)(COR_PRF_SUSPEND_REASON suspendReason);
   STDMETHOD(RuntimeSuspendFinished)();
   STDMETHOD(RuntimeSuspendAborted)();
   STDMETHOD(RuntimeResumeStarted)();
   STDMETHOD(RuntimeResumeFinished)();
   STDMETHOD(RuntimeThreadSuspended)(ThreadID threadid);
   STDMETHOD(RuntimeThreadResumed)(ThreadID threadid);
   STDMETHOD(MovedReferences)(ULONG cmovedObjectIDRanges, ObjectID oldObjectIDRangeStart[], ObjectID newObjectIDRangeStart[], ULONG cObjectIDRangeLength[]);
   STDMETHOD(ObjectAllocated)(ObjectID objectID, ClassID classID);
   STDMETHOD(ObjectsAllocatedByClass)(ULONG classCount, ClassID classIDs[], ULONG objects[]);
   STDMETHOD(ObjectReferences)(ObjectID objectID, ClassID classID, ULONG cObjectRefs, ObjectID objectRefIDs[]);
   STDMETHOD(RootReferences)(ULONG cRootRefs, ObjectID rootRefIDs[]);
   STDMETHOD(ExceptionThrown)(ObjectID thrownObjectID);
   STDMETHOD(ExceptionSearchFunctionEnter)(FunctionID functionID);
   STDMETHOD(ExceptionSearchFunctionLeave)();
   STDMETHOD(ExceptionSearchFilterEnter)(FunctionID functionID);
   STDMETHOD(ExceptionSearchFilterLeave)();
   STDMETHOD(ExceptionSearchCatcherFound)(FunctionID functionID);
   STDMETHOD(ExceptionCLRCatcherFound)();
   STDMETHOD(ExceptionCLRCatcherExecute)();
   STDMETHOD(ExceptionOSHandlerEnter)(FunctionID functionID);
   STDMETHOD(ExceptionOSHandlerLeave)(FunctionID functionID);
   STDMETHOD(ExceptionUnwindFunctionEnter)(FunctionID functionID);
   STDMETHOD(ExceptionUnwindFunctionLeave)();
   STDMETHOD(ExceptionUnwindFinallyEnter)(FunctionID functionID);
   STDMETHOD(ExceptionUnwindFinallyLeave)();
   STDMETHOD(ExceptionCatcherEnter)(FunctionID functionID, ObjectID objectID);
   STDMETHOD(ExceptionCatcherLeave)();
   STDMETHOD(COMClassicVTableCreated)(ClassID wrappedClassID, REFGUID implementedIID, void *pVTable, ULONG cSlots);
   STDMETHOD(COMClassicVTableDestroyed)(ClassID wrappedClassID, REFGUID implementedIID, void *pVTable);
   STDMETHOD(ThreadNameChanged)(ThreadID threadId, ULONG cchName, WCHAR name[]);
   STDMETHOD(GarbageCollectionStarted)(int cGenerations, BOOL generationCollected[], COR_PRF_GC_REASON reason);
   STDMETHOD(SurvivingReferences)(ULONG cSurvivingObjectIDRanges, ObjectID objectIDRangeStart[], ULONG cObjectIDRangeLength[]);
   STDMETHOD(GarbageCollectionFinished)();
   STDMETHOD(FinalizeableObjectQueued)(DWORD finalizerFlags, ObjectID objectID);
   STDMETHOD(RootReferences2)(ULONG cRootRefs, ObjectID rootRefIds[], COR_PRF_GC_ROOT_KIND rootKinds[], COR_PRF_GC_ROOT_FLAGS rootFlags[], UINT_PTR rootIds[]);
   STDMETHOD(HandleCreated)(GCHandleID handleId, ObjectID initialObjectId);
   STDMETHOD(HandleDestroyed)(GCHandleID handleId);
   STDMETHOD(InitializeForAttach)(IUnknown *pCorProfilerInfoUnk, void *pvClientData, UINT cbClientData);
   STDMETHOD(ProfilerAttachComplete)(void);        
   STDMETHOD(ProfilerDetachSucceeded)(void);


   HRESULT SetEventMask(void);
   //HRESULT GetMethodNameFromFunctionId(FunctionID functionId, WCHAR className[512], WCHAR methodName[512]);
   //HRESULT WriteCodeBytes(FunctionID functionID);
   void Check(HRESULT hr);


private:
   ICorProfilerInfo* m_corProfilerInfo;
   ICorProfilerInfo2* m_corProfilerInfo2;
   ILRewriteHelper m_rewritehelper;

   long m_nRefCount;   //for managing the reference count
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect Visma Software AB
Sweden Sweden
Mattias works at Visma, a leading Nordic ERP solution provider. He has good knowledge in C++/.Net development, test tool development, and debugging. His great passion is memory dump analysis. He likes giving talks and courses.

Comments and Discussions