Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / August 2003

Tip: Looking for answers? Try searching our database.

Can ICorProfilerCallback be implemented in managed code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony A. - 05 Aug 2003 03:25 GMT
I've been looking into the CLR profiling interfaces for a project I'm
working on, and I was wondering if it's possible at all to implement them in
C# instead of unmanaged C++?  I'd prefer to work in C# if possible, just for
design reasons, but I'm thinking that it may not be possible to have the
profiler run in the CLR, for technical reasons.  At least, I haven't been
able to get it to work so far.

Here's what I've done:

1) Declared the interface in C#, taken from the IDL file:

[ComImport,
Guid("176FBED1-A55C-4796-98CA-A9DA0EF883E7"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ICorProfilerCallback
{
   [PreserveSig] int Initialize([In] object pICorProfilerInfoUnk);

   /* ...and the rest of the functions */
}

2) Implement this interface in a class:

[ComVisible(true),
Guid("055FD9E4-195E-4d8b-823F-9AE287803EC8")]
public class ProfilerCallback : ICorProfilerCallback
{
   public int Initialize(object pICorProfilerInfoUnk)
   {
       return 0;
   }

   /* ...and the rest of the functions */
}

3) Register the assembly DLL containing this class with "regasm".  I've
checked the registry afterwards, and my class is properly registered in the
CLSID section.  So far so good.

4) Try to invoke a .NET executable using the profiler:

  ProcessStartInfo si = new ProcessStartInfo(@"D:\someprogram.exe");

  si.EnvironmentVariables.Add("Cor_Enable_Profiling", "1");
  si.EnvironmentVariables.Add("Cor_Profiler",
"{055FD9E4-195E-4d8b-823F-9AE287803EC8}");
  si.UseShellExecute = false;
  Process process = Process.Start(si);

Unfortunately, at this point, the spawned process just crashes with an
access violation--I've put some dumb debugging code inside the
ProfilerCallback constructor and the ICorProfilerCallback.Initialize method,
and neither apparently is getting called by the point it crashes.  I've
tried going through it in the VS.NET debugger and it just hangs badly, so I
can't get anywhere that way.

Is this simply not possible, and I'll have to implement the profiler
interface in C++?  Or have I mucked up the COM interop somewhere, and the
problem has nothing to do with the profiler?

Also, are there any suggestions of a way to get data communicated back and
forth between the process that will be spawning the process to be profiled
(the spawner will be a .NET app), and the profiler callback itself?  I've
been thinking of just using a named memory-mapped file to pass info.

Thanks, any advice would be greatly appreciated.

--
Tony A.
Per larsen - 05 Aug 2003 12:54 GMT
Tony,

No, that's not possible. The profiler support logic is an integral part of
the .NET run-time, and it supplies event hooks for many events that are
global to the CLR session, including managed to unmanaged transitions, JIT
compiles, and so on. If the profiler itself were in managed code, then when
the CLR would attempt to notify the profiler call-back about the first JIT
compile, the CLR would then have to compile the notification stub in the
profiler (it also being a managed piece of code) before it could notify it,
that compile in turn should be notified, and so we'd have infinite
recursion. There are lots of other, similar technical obstacles.

In theory, it would be possible to implement the profiler call-back as an
out-of-process COM object, but since the overhead of cross-process method
calls is huge, that solution was not considered.

I suppose in theory it would be possible for the CLR to allow a managed
profiler in one domain to profile code in another domain (in the same
process), but AFAIK that's not (yet?) supported - most likely in part
because at least some of the profiler support code is mapped to the old
WIN32 debugging support.

Bottom line: the profiler call-back must be unmanaged.

That being said, there is nothing preventing you from having a nice profiler
GUI front-end, written in managed C#. As you yourself suggest, I'd choose
memory mapped files for communicating data between the managed UI and the
unmanaged profiler call-back.

IHTH

- Per

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2012 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.