Check "Rewrite MSIL Code on the Fly with the .NET Framework Profiling
API" article on MSDN:
http://msdn.microsoft.com/msdnmag/issues/03/09/NETProfilingAPI/default.a
spx . It should have answers to all your questions.

Note that there is bug in the profiler implementation in the final Rotor
v1 release - more details are at
http://www.mail-archive.com/[EMAIL PROTECTED]/msg00511.ht
ml. 

You may also want to a look at
ICorProfilerInfo::SetEnterLeaveFunctionHooks. It gives you simple
Enter/Leave callbacks without rewriting the IL.

-Jan

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

-----Original Message-----
From: Discussion of the Rotor Shared Source CLI implementation
[mailto:[EMAIL PROTECTED] On Behalf Of SUBSCRIBE
DOTNET-ROTOR Memomana
Sent: Wednesday, November 26, 2003 8:11 PM
To: [EMAIL PROTECTED]
Subject: [DOTNET-ROTOR] Method's IL Modification

I have followed the guide to use Get/SetILFunctionBody posted a while
ago by
Jan Kotas. The code works fine in my Rotor build.

I would like to modify the code so that the Main method displays a
"Please
Help" message before returning. i.e. if the original Main method
displays
"Hello World", then the new code will make it display "Hello World" AND
"Please Help".

In other words, the difference with Jan's code is that instead of
replacing
the original Main method's IL by a new IL to display "Hello World", my
intention is to keep the original Main method's IL and add other new ILs
(in
this case, IL to display "Please Help" message).

I have managed to augment the IL code for displaying "Please Help"
before
Main returns.

Original IL code:
13 30 01 00 0b 00 00 00 00 00 00 00 72 01 00 00 70 28 02 00 00 0a 2a

My modified IL code:
13 30 01 00 0b 00 00 00 00 00 00 00 72 01 00 00 70 28 02 00 00 0a [72 1c
00
00 70 28 02 00 00 0a] 2a

The brackets are included only for easy reading.

Basically, I copied the original bytecode into a new (BYTE *) variable,
say
BYTE *X. Then, the last original bytecode (i.e. 2a) is copied to be the
last
bytecode of X. In between, I inserted the [72 1c 00 00 70 28 02 00 00
0a].

However, the Rotor complained:
"VALIDATION FAILS: inPtr + sizeof(unsigned int) <= inBuffEnd
At World.HelloWorld::Main at a
Unhandled Exception: System.InvalidProgramException: Common Language
Runtime
detected an invalid program.
   at World.HelloWorld.Main()"

I figured that the new IL is not valid since, obviously, the size of the
IL
code is larger.
#1 How would we determine how much bigger?
#2 How would we set the new size back using the SetILFunctionBody
function?
#3 Any other (better) way to instrument the method's IL code?

Thank you beforehand.

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com
NEW! ASP.NET courses you may be interested in:

2 Days of ASP.NET, 29 Sept 2003, in Redmond
http://www.develop.com/courses/2daspdotnet

Guerrilla ASP.NET, 13 Oct 2003, in Boston
http://www.develop.com/courses/gaspdotnet

View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
NEW! ASP.NET courses you may be interested in:

2 Days of ASP.NET, 29 Sept 2003, in Redmond
http://www.develop.com/courses/2daspdotnet

Guerrilla ASP.NET, 13 Oct 2003, in Boston
http://www.develop.com/courses/gaspdotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to