Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Last active September 14, 2018 14:28
Show Gist options
  • Select an option

  • Save mattwarren/59643221a891d35e62855d02b3ec7973 to your computer and use it in GitHub Desktop.

Select an option

Save mattwarren/59643221a891d35e62855d02b3ec7973 to your computer and use it in GitHub Desktop.

Revisions

  1. mattwarren revised this gist Sep 14, 2018. 1 changed file with 86 additions and 78 deletions.
    164 changes: 86 additions & 78 deletions CLRMD-Tutorial.md
    Original file line number Diff line number Diff line change
    @@ -9,86 +9,94 @@
    ----

    ``` csharp
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("ClrMD Demo");

    // https://github.com/Microsoft/clrmd/blob/master/Documentation/GettingStarted.md
    // https://github.com/Microsoft/clrmd/blob/master/Documentation/ClrRuntime.md
    var path = @"C:\Users\Owner\Downloads\__Matt__\HelloWorld.DMP";
    using (DataTarget dataTarget = DataTarget.LoadCrashDump(path))
    {
    EnumerateVersions(dataTarget.ClrVersions);

    ClrInfo runtimeInfo = dataTarget.ClrVersions[0]; // just using the first runtime
    ClrRuntime runtime = runtimeInfo.CreateRuntime();

    // AppDomains
    Console.WriteLine("AppDomains:");
    foreach (ClrAppDomain domain in runtime.AppDomains)
    {
    Console.WriteLine(" ID: {0}", domain.Id);
    Console.WriteLine(" Name: {0}", domain.Name);
    Console.WriteLine(" Address: {0}", domain.Address);
    }
    Console.WriteLine("");

    // Walking the stack
    foreach (ClrThread thread in runtime.Threads)
    {
    if (!thread.IsAlive)
    continue;

    Console.WriteLine("Thread {0:X}:", thread.OSThreadId);

    foreach (ClrStackFrame frame in thread.StackTrace)
    Console.WriteLine("{0,12:X} {1,12:X} {2}", frame.StackPointer, frame.InstructionPointer, frame.ToString());

    Console.WriteLine();
    }

    // CLR Memory Regions
    foreach (var region in (from r in runtime.EnumerateMemoryRegions()
    where r.Type != ClrMemoryRegionType.ReservedGCSegment
    group r by r.Type into g
    let total = g.Sum(p => (uint)p.Size)
    orderby total descending
    select new
    {
    TotalSize = total,
    Count = g.Count(),
    Type = g.Key
    }))
    {
    Console.WriteLine("{0,6:n0} {1,12:n0} {2}", region.Count, region.TotalSize, region.Type.ToString());
    }
    }
    }

    private static void EnumerateVersions(IList<ClrInfo> versions)
    {
    foreach (ClrInfo version in versions)
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using Microsoft.Diagnostics.Runtime;

    namespace CLRMD_Demo
    {
    class Program
    {
    Console.WriteLine("Found CLR Version: " + version.Version.ToString());

    // This is the data needed to request the dac from the symbol server:
    ModuleInfo dacInfo = version.DacInfo;
    Console.WriteLine("Filesize: {0:X}", dacInfo.FileSize);
    Console.WriteLine("Timestamp: {0:X}", dacInfo.TimeStamp);
    Console.WriteLine("Dac File: {0}", dacInfo.FileName);

    // If we just happen to have the correct dac file installed on the machine,
    // the "LocalMatchingDac" property will return its location on disk:
    string dacLocation = version.LocalMatchingDac;
    if (!string.IsNullOrEmpty(dacLocation))
    Console.WriteLine("Local dac location: " + dacLocation);

    // You may also download the dac from the symbol server, which is covered
    // in a later section of this tutorial.
    static void Main(string[] args)
    {
    Console.WriteLine("ClrMD Demo");

    // https://github.com/Microsoft/clrmd/blob/master/Documentation/GettingStarted.md
    // https://github.com/Microsoft/clrmd/blob/master/Documentation/ClrRuntime.md
    var path = @"C:\Users\Owner\Downloads\__Matt__\HelloWorld.DMP";
    using (DataTarget dataTarget = DataTarget.LoadCrashDump(path))
    {
    EnumerateVersions(dataTarget.ClrVersions);

    ClrInfo runtimeInfo = dataTarget.ClrVersions[0]; // just using the first runtime
    ClrRuntime runtime = runtimeInfo.CreateRuntime();

    // AppDomains
    Console.WriteLine("AppDomains:");
    foreach (ClrAppDomain domain in runtime.AppDomains)
    {
    Console.WriteLine(" ID: {0}", domain.Id);
    Console.WriteLine(" Name: {0}", domain.Name);
    Console.WriteLine(" Address: {0}", domain.Address);
    }
    Console.WriteLine("");

    // Walking the stack
    foreach (ClrThread thread in runtime.Threads)
    {
    if (!thread.IsAlive)
    continue;

    Console.WriteLine("Thread {0:X}:", thread.OSThreadId);

    foreach (ClrStackFrame frame in thread.StackTrace)
    Console.WriteLine("{0,12:X} {1,12:X} {2}", frame.StackPointer, frame.InstructionPointer, frame.ToString());

    Console.WriteLine();
    }

    // CLR Memory Regions
    foreach (var region in (from r in runtime.EnumerateMemoryRegions()
    where r.Type != ClrMemoryRegionType.ReservedGCSegment
    group r by r.Type into g
    let total = g.Sum(p => (uint)p.Size)
    orderby total descending
    select new
    {
    TotalSize = total,
    Count = g.Count(),
    Type = g.Key
    }))
    {
    Console.WriteLine("{0,6:n0} {1,12:n0} {2}", region.Count, region.TotalSize, region.Type.ToString());
    }
    }
    }

    private static void EnumerateVersions(IList<ClrInfo> versions)
    {
    foreach (ClrInfo version in versions)
    {
    Console.WriteLine("Found CLR Version: " + version.Version.ToString());

    // This is the data needed to request the dac from the symbol server:
    ModuleInfo dacInfo = version.DacInfo;
    Console.WriteLine("Filesize: {0:X}", dacInfo.FileSize);
    Console.WriteLine("Timestamp: {0:X}", dacInfo.TimeStamp);
    Console.WriteLine("Dac File: {0}", dacInfo.FileName);

    // If we just happen to have the correct dac file installed on the machine,
    // the "LocalMatchingDac" property will return its location on disk:
    string dacLocation = version.LocalMatchingDac;
    if (!string.IsNullOrEmpty(dacLocation))
    Console.WriteLine("Local dac location: " + dacLocation);

    // You may also download the dac from the symbol server, which is covered
    // in a later section of this tutorial.
    }
    }
    }
    }
    }
    ```
    ----
  2. mattwarren revised this gist Sep 13, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions CLRMD-Tutorial.md
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    - https://github.com/Microsoft/clrmd
    - http://mattwarren.org/2018/06/15/Tools-for-Exploring-.NET-Internals/#tools-based-on-clr-memory-diagnostics-clrmd
    - https://github.com/Microsoft/clrmd/blob/master/Documentation/GettingStarted.md
    - https://github.com/dotnet/symstore/tree/master/src/dotnet-symbol

    ----

  3. mattwarren revised this gist Sep 13, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions CLRMD-Tutorial.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    ## Background Info

    https://www.nuget.org/packages/Microsoft.Diagnostics.Runtime
    https://github.com/Microsoft/clrmd
    http://mattwarren.org/2018/06/15/Tools-for-Exploring-.NET-Internals/#tools-based-on-clr-memory-diagnostics-clrmd
    https://github.com/Microsoft/clrmd/blob/master/Documentation/GettingStarted.md
    - https://www.nuget.org/packages/Microsoft.Diagnostics.Runtime
    - https://github.com/Microsoft/clrmd
    - http://mattwarren.org/2018/06/15/Tools-for-Exploring-.NET-Internals/#tools-based-on-clr-memory-diagnostics-clrmd
    - https://github.com/Microsoft/clrmd/blob/master/Documentation/GettingStarted.md

    ----

  4. mattwarren renamed this gist Sep 13, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. mattwarren revised this gist Sep 13, 2018. 1 changed file with 87 additions and 0 deletions.
    87 changes: 87 additions & 0 deletions CLRMD-Tutorial
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,93 @@ https://github.com/Microsoft/clrmd
    http://mattwarren.org/2018/06/15/Tools-for-Exploring-.NET-Internals/#tools-based-on-clr-memory-diagnostics-clrmd
    https://github.com/Microsoft/clrmd/blob/master/Documentation/GettingStarted.md

    ----

    ``` csharp
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("ClrMD Demo");

    // https://github.com/Microsoft/clrmd/blob/master/Documentation/GettingStarted.md
    // https://github.com/Microsoft/clrmd/blob/master/Documentation/ClrRuntime.md
    var path = @"C:\Users\Owner\Downloads\__Matt__\HelloWorld.DMP";
    using (DataTarget dataTarget = DataTarget.LoadCrashDump(path))
    {
    EnumerateVersions(dataTarget.ClrVersions);

    ClrInfo runtimeInfo = dataTarget.ClrVersions[0]; // just using the first runtime
    ClrRuntime runtime = runtimeInfo.CreateRuntime();

    // AppDomains
    Console.WriteLine("AppDomains:");
    foreach (ClrAppDomain domain in runtime.AppDomains)
    {
    Console.WriteLine(" ID: {0}", domain.Id);
    Console.WriteLine(" Name: {0}", domain.Name);
    Console.WriteLine(" Address: {0}", domain.Address);
    }
    Console.WriteLine("");

    // Walking the stack
    foreach (ClrThread thread in runtime.Threads)
    {
    if (!thread.IsAlive)
    continue;

    Console.WriteLine("Thread {0:X}:", thread.OSThreadId);

    foreach (ClrStackFrame frame in thread.StackTrace)
    Console.WriteLine("{0,12:X} {1,12:X} {2}", frame.StackPointer, frame.InstructionPointer, frame.ToString());

    Console.WriteLine();
    }

    // CLR Memory Regions
    foreach (var region in (from r in runtime.EnumerateMemoryRegions()
    where r.Type != ClrMemoryRegionType.ReservedGCSegment
    group r by r.Type into g
    let total = g.Sum(p => (uint)p.Size)
    orderby total descending
    select new
    {
    TotalSize = total,
    Count = g.Count(),
    Type = g.Key
    }))
    {
    Console.WriteLine("{0,6:n0} {1,12:n0} {2}", region.Count, region.TotalSize, region.Type.ToString());
    }
    }
    }

    private static void EnumerateVersions(IList<ClrInfo> versions)
    {
    foreach (ClrInfo version in versions)
    {
    Console.WriteLine("Found CLR Version: " + version.Version.ToString());

    // This is the data needed to request the dac from the symbol server:
    ModuleInfo dacInfo = version.DacInfo;
    Console.WriteLine("Filesize: {0:X}", dacInfo.FileSize);
    Console.WriteLine("Timestamp: {0:X}", dacInfo.TimeStamp);
    Console.WriteLine("Dac File: {0}", dacInfo.FileName);

    // If we just happen to have the correct dac file installed on the machine,
    // the "LocalMatchingDac" property will return its location on disk:
    string dacLocation = version.LocalMatchingDac;
    if (!string.IsNullOrEmpty(dacLocation))
    Console.WriteLine("Local dac location: " + dacLocation);

    // You may also download the dac from the symbol server, which is covered
    // in a later section of this tutorial.
    }
    }
    }
    ```
    ----

    ## Challenges

    ## Useful Links
  6. mattwarren revised this gist Sep 13, 2018. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions CLRMD-Tutorial
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    ## Background Info

    https://www.nuget.org/packages/Microsoft.Diagnostics.Runtime
    https://github.com/Microsoft/clrmd
    http://mattwarren.org/2018/06/15/Tools-for-Exploring-.NET-Internals/#tools-based-on-clr-memory-diagnostics-clrmd
    https://github.com/Microsoft/clrmd/blob/master/Documentation/GettingStarted.md

    ## Challenges

    ## Useful Links
  7. mattwarren created this gist Sep 13, 2018.
    5 changes: 5 additions & 0 deletions CLRMD-Tutorial
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    ## Background Info

    ## Challenges

    ## Useful Links