Skip to content

Instantly share code, notes, and snippets.

@Criteo-dotnet-blog
Last active June 28, 2017 09:51
Show Gist options
  • Select an option

  • Save Criteo-dotnet-blog/911cc25f3c43caaba6317afff5a523c3 to your computer and use it in GitHub Desktop.

Select an option

Save Criteo-dotnet-blog/911cc25f3c43caaba6317afff5a523c3 to your computer and use it in GitHub Desktop.
private static ulong GetTaskStateFromAddress(ulong address)
{
    var type = Runtime.GetHeap().GetObjectType(address);
    if ((type != null) && (type.Name.StartsWith("System.Threading.Task")))
    {
        // try to get the m_stateFlags field value
        ClrInstanceField field = type.GetFieldByName("m_stateFlags");
        if (field != null)
        {
            var val = field.GetValue(address);
            if (val != null)
            {
                try
                {
                    return (ulong)(int)val;
                }
                catch (InvalidCastException)
                {
                }
            }
        }
    }
    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment