Observing Managed Execution

Because so much of what’s happening in the execution engine is low-level, self-modifying code, trying to keep track of what’s going on can be awkward. Rather than constantly walk through code in a debugger, readers can take advantage of a number of tracing and diagnostic facilities that exist in Rotor.

To demonstrate the use of tracing, we will use it to observe the JIT compiler in action. First, modify main.exe to contain a try block, as follows:

    public class MainApp {
      public static void Main(  ) {
        try {
          Echo e = new Echo(  );
          e.EchoString = "Echo THIS!";
          System.Console.WriteLine("First echo is: {0}", e.DoEcho(  ));
          e.EchoString = null;
          System.Console.WriteLine("Second echo is: {0}", e.DoEcho(  ));
        } catch {
          System.Console.WriteLine("Caught and recovered from bad Echo.");
        }
      }
    }

When you run this program, you will see:

    % csc -t:exe -r:echo.dll -debug main2.cs
    Microsoft (R) Visual C# Shared Source CLI Compiler version 1.0.0003
    for Microsoft (R) Shared Source CLI version 1.0.0
    Copyright (C) Microsoft Corporation 2002. All rights reserved.

    % clix main2.exe
    First echo is: Echo THIS!
    Caught and recovered from bad Echo.

Scattered throughout the code that implements the CLI execution engine are thousands of calls to chunks of code such as the following that are conditionally compiled for logging and debugging:

 #if defined(_DEBUG) || defined(LOGGING) const char *szDebugMethodName; const char *szDebugClassName; szDebugMethodName = compHnd->getMethodName(info->ftn, ...

Get Shared Source CLI Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.