Browse Source

fixed IL rewriting of System.Console.Write (special case?)

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5223 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 16 years ago
parent
commit
05bad56750
  1. 132
      src/AddIns/Misc/Profiler/Hook/Profiler.cpp
  2. 30
      src/AddIns/Misc/Profiler/Hook/Profiler.h

132
src/AddIns/Misc/Profiler/Hook/Profiler.cpp

@ -516,12 +516,6 @@ STDMETHODIMP CProfiler::ExceptionThrown(ObjectID) @@ -516,12 +516,6 @@ STDMETHODIMP CProfiler::ExceptionThrown(ObjectID)
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionUnwindFunctionEnter(FunctionID)
{
DebugWriteLine(L"ExceptionUnwindFunctionEnter");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionUnwindFunctionLeave()
{
DebugWriteLine(L"ExceptionUnwindFunctionLeave");
@ -529,84 +523,6 @@ STDMETHODIMP CProfiler::ExceptionUnwindFunctionLeave() @@ -529,84 +523,6 @@ STDMETHODIMP CProfiler::ExceptionUnwindFunctionLeave()
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionSearchFunctionEnter(FunctionID)
{
DebugWriteLine(L"ExceptionSearchFunctionEnter");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionSearchFunctionLeave()
{
DebugWriteLine(L"ExceptionSearchFunctionLeave");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionSearchFilterEnter(FunctionID)
{
DebugWriteLine(L"ExceptionSearchFilterEnter");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionSearchFilterLeave()
{
DebugWriteLine(L"ExceptionSearchFilterLeave");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionSearchCatcherFound(FunctionID)
{
DebugWriteLine(L"ExceptionSearchCatcherFound");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionCLRCatcherFound()
{
DebugWriteLine(L"ExceptionCLRCatcherFound");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionCLRCatcherExecute()
{
DebugWriteLine(L"ExceptionCLRCatcherExecute");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionOSHandlerEnter(FunctionID)
{
DebugWriteLine(L"ExceptionOSHandlerEnter");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionOSHandlerLeave(FunctionID)
{
DebugWriteLine(L"ExceptionOSHandlerLeave");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionUnwindFinallyEnter(FunctionID)
{
DebugWriteLine(L"ExceptionUnwindFinallyEnter");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionUnwindFinallyLeave()
{
DebugWriteLine(L"ExceptionUnwindFinallyLeave");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionCatcherEnter(FunctionID, ObjectID)
{
DebugWriteLine(L"ExceptionCatcherEnter");
return S_OK;
}
STDMETHODIMP CProfiler::ExceptionCatcherLeave()
{
DebugWriteLine(L"ExceptionCatcherLeave");
return S_OK;
}
STDMETHODIMP CProfiler::JITCompilationStarted(FunctionID functionID, BOOL /*fIsSafeToBlock*/)
{
WCHAR *name;
@ -706,17 +622,30 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId) @@ -706,17 +622,30 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId)
return;
int newLength = 0;
unsigned char newCode[64] = { 0 };
int injectionLen = SetInjectionCode(metaData, newCode, &newLength, activateCall, loggerCall, deactivateCall, type, nameId);
assert(injectionLen == newLength);
DebugWriteLine(L"rewrite size %d", injectionLen);
if (((COR_ILMETHOD_TINY *)header)->IsTiny()) {
COR_ILMETHOD_TINY *method = (COR_ILMETHOD_TINY *)header;
if (method->GetCodeSize() + sizeof(activateCall) + sizeof(loggerCall) + sizeof(deactivateCall) + 2 < MAX_CODE_SIZE_TINY) {
if (method->GetCodeSize() + injectionLen < MAX_CODE_SIZE_TINY) {
DebugWriteLine(L"rewrite normal");
// Copy the header elements.
memcpy(&codeBuf[0], method, TINY_HEADER_SIZE);
newLength = TINY_HEADER_SIZE;
SetInjectionCode(metaData, codeBuf, &newLength, activateCall, loggerCall, deactivateCall, type, nameId);
// copy new code
memcpy(&codeBuf[newLength],
&newCode[0],
injectionLen);
newLength += injectionLen;
// copy old code
memcpy(&codeBuf[newLength],
@ -730,9 +659,15 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId) @@ -730,9 +659,15 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId)
// 2 upper bits are the tiny header 6 lower bits are length
codeBuf[0] = (byte)((newLength - 1) << 2 | 0x2);
} else {
DebugWriteLine(L"rewrite convert");
ConvertToFat((byte *)codeBuf, &newLength);
SetInjectionCode(metaData, codeBuf, &newLength, activateCall, loggerCall, deactivateCall, type, nameId);
// copy new code
memcpy(&codeBuf[newLength],
&newCode[0],
injectionLen);
newLength += injectionLen;
// copy old code
memcpy(&codeBuf[newLength], &header[TINY_HEADER_SIZE], length - TINY_HEADER_SIZE);
@ -742,6 +677,7 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId) @@ -742,6 +677,7 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId)
target->CodeSize = newLength - FAT_HEADER_SIZE;
}
} else if (((COR_ILMETHOD_FAT *)header)->IsFat()) {
DebugWriteLine(L"rewrite fat");
COR_ILMETHOD_FAT *method = (COR_ILMETHOD_FAT *)header;
COR_ILMETHOD_FAT *target = (COR_ILMETHOD_FAT *)codeBuf;
@ -749,12 +685,12 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId) @@ -749,12 +685,12 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId)
newLength = FAT_HEADER_SIZE;
memcpy(&codeBuf[0], method, newLength);
int diff = newLength;
// copy new code
memcpy(&codeBuf[newLength],
&newCode[0],
injectionLen);
// insert new code
SetInjectionCode(metaData, codeBuf, &newLength, activateCall, loggerCall, deactivateCall, type, nameId);
diff = newLength - diff;
newLength += injectionLen;
// Copy the remainder of the method body.
memcpy(&codeBuf[newLength], &header[FAT_HEADER_SIZE], length - FAT_HEADER_SIZE);
@ -767,7 +703,7 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId) @@ -767,7 +703,7 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId)
target->MaxStack = (method->MaxStack > 8) ? method->MaxStack : 8;
// Fix SEH sections
FixSEHSections(target->GetSect(), diff);
FixSEHSections(target->GetSect(), injectionLen);
} else {
DebugWriteLine(L"neither fat nor tiny ... invalid header???");
}
@ -777,11 +713,13 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId) @@ -777,11 +713,13 @@ void CProfiler::Rewrite(FunctionID functionID, int type, int nameId)
}
}
void CProfiler::SetInjectionCode(IMetaDataImport *metaData, byte *buffer, int *size,
int CProfiler::SetInjectionCode(IMetaDataImport *metaData, byte *buffer, int *size,
mdMethodDef activateCall, mdMethodDef loggerCall, mdMethodDef deactivateCall,
int type, int nameId)
{
HRESULT hr = S_OK;
int start = *size;
buffer[(*size)++] = 0x28; //call
*(mdMethodDef*)(&buffer[*size]) = deactivateCall;
@ -855,6 +793,8 @@ void CProfiler::SetInjectionCode(IMetaDataImport *metaData, byte *buffer, int *s @@ -855,6 +793,8 @@ void CProfiler::SetInjectionCode(IMetaDataImport *metaData, byte *buffer, int *s
buffer[(*size)++] = 0x28; //call
*(mdMethodDef*)(&buffer[*size]) = activateCall;
*size += sizeof(activateCall);
return *size - start;
}
void CProfiler::ConvertToFat(byte *target, int *size)

30
src/AddIns/Misc/Profiler/Hook/Profiler.h

@ -72,22 +72,22 @@ public: @@ -72,22 +72,22 @@ public:
// Exception creation
STDMETHOD(ExceptionThrown)(ObjectID thrownObjectID);
// Search phase
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(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);
// Unwind phase
STDMETHOD(ExceptionUnwindFunctionEnter)(FunctionID functionID);
// STDMETHOD(ExceptionUnwindFunctionEnter)(FunctionID functionID);
STDMETHOD(ExceptionUnwindFunctionLeave)();
STDMETHOD(ExceptionUnwindFinallyEnter)(FunctionID functionID);
STDMETHOD(ExceptionUnwindFinallyLeave)();
STDMETHOD(ExceptionCatcherEnter)(FunctionID functionID, ObjectID objectID);
STDMETHOD(ExceptionCatcherLeave)();
// STDMETHOD(ExceptionUnwindFinallyEnter)(FunctionID functionID);
// STDMETHOD(ExceptionUnwindFinallyLeave)();
// STDMETHOD(ExceptionCatcherEnter)(FunctionID functionID, ObjectID objectID);
// STDMETHOD(ExceptionCatcherLeave)();
// UNLOAD EVENTS
STDMETHOD(FunctionUnloadStarted)(FunctionID functionID);
@ -137,7 +137,7 @@ private: @@ -137,7 +137,7 @@ private:
void Rewrite(FunctionID functionID, int type, int nameId);
void ConvertToFat(byte *target, int *size);
void FixSEHSections(const COR_ILMETHOD_SECT *sections, int offset);
void SetInjectionCode(IMetaDataImport *, byte *buffer, int *size, mdMethodDef activateCall, mdMethodDef loggerCall, mdMethodDef deactivateCall, int type, int nameId);
int SetInjectionCode(IMetaDataImport *, byte *buffer, int *size, mdMethodDef activateCall, mdMethodDef loggerCall, mdMethodDef deactivateCall, int type, int nameId);
bool CreateMethod(IMetaDataEmit *, const WCHAR *, PCCOR_SIGNATURE, mdMethodDef *, mdToken);
int InitializeCommunication();
};

Loading…
Cancel
Save