Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing ThreadStatic field from .NET profiler

This question in stackoverflow asks how [ThreadStatic] is implemented: How does the ThreadStatic attribute work?

Some suggested that it should be viewed as an extension of the Thread object. I'm not sure if that means it is based on win32 TLS.

My question is can I somehow gain access to a value of a [ThreadStatic] from current thread in .NET profiler code? That is, in native code.

For example, if I could find using the win32 thread id the region in memory where all of the thread static fields are, and find specific fields I need to retrieve.

Thx

like image 452
user967710 Avatar asked Dec 30 '18 13:12

user967710


1 Answers

As explained in the answers you referenced, the [ThreadStatic] functionality is implemented by the .NET runtime, not by the C# compiler.

That means, you'd need to reverse-engineer the runtime's data structures to get access to the data you want.

Alternatively, you can use the ICorProfilerInfo interface. This is an unmanaged interface, as you require.

In particular, you'd need the ICorProfilerInfo2::GetThreadStaticAddress method. It accepts a class ID, a field ID, and a thread ID as input arguments and provides an address of the field's value that is local to the thread with the specified ID.

If you're interested in how this works, you can check e.g. the Core CLR's implementation (search for the GetThreadStaticAddress2 method, also look threads.cpp for GetStaticFieldAddrNoCreate). The Core CLR doesn't utilize the OS TLS; instead, it maintains its own table of so called ThreadLocalBlocks and ThreadLocalModules where the data is stored.

There's also a managed Microsoft.Diagnostics.Runtime (CLR MD) implementation. It should also provide an access to the thread-local values. However, it seems to be broken now, as a comment says:

// TODO:  Renable when thread statics are fixed.
like image 106
dymanoid Avatar answered Sep 29 '22 03:09

dymanoid