Interactive Diagnostics with the ScriptCS REPL

Drew Marsh edited this page May 16, 2013 · 4 revisions

Interactive Diagnostics with the ScriptCs REPL

Using ScriptCS.ClrMD in REPL mode is probably the most powerful way to work with it, so let's take a quick look at how that works:

  1. Start by installing the ScriptCs.ClrMD script pack:
scriptcs -install ScriptCs.ClrMD -pre

NOTE: you must use the -pre command line switch today since the package is still in beta

  1. Launch scriptcs in REPL mode:
  1. Once you are at the REPL prompt you can simply attach to a process and begin interacting with it using ClrMD plus the various extensions that are automatically imported by the ScriptCs.ClrMD script pack like so:
  // First require the ClrMdPack
  > var clrmd = Require<ClrMdPack>();

  // Now attach to a running .NET process which will give us back a ClrMD ClrRuntime instance
  > var clrRuntime = clrmd.Attach("MyDotNetApplication");

  // We can use one of the built in DumpXXX methods to dump useful, predefined statistics
  > clrmd.DumpHeapStatsByType();

Here's a sample of what the command above might output to the REPL window:

    1,672           21 System.String[]
    1,838           20 System.Char[]
    2,008           24 System.Int32[]
    2,968           53 System.RuntimeType
   12,232          249 System.String
   36,192           42 System.Object[]
  // Or we can simply use the raw ClrMD API directly
  > Console.WriteLine(clrRuntime.Threads.Count);    

  // Or we can use an extension method included by the script pack for higher level analytics and dump ourselves manually
  > clrRuntime.GetHeapStatsByType().Where(ths => ths.TypeName.StartsWith("MyNamespace")).OrderByDescending(ths => ths.TotalHeapSize).ToList().ForEach(s => Console.WriteLine("{0,12:n0} {1,12:n0} {2}", s.TotalHeapSize, s.NumberOfInstances, s.TypeName));

  // Finally we can detach from the process to let it resume executing once we're done inspecting it
  > clrmd.Detach();