Microsoft.Diagnostics.Runtime throw excepton on iOS

Girish Kumar Sharma 1 Reputation point
2022-01-21T09:49:10.07+00:00

I am looking for the Stack traces for all the threads in the current process. The following code works fine for windows but throws an exception when the same code runs on the iOS platform.

using (var target = DataTarget.CreateSnapshotAndAttach(Process.GetCurrentProcess().Id))
{
var runtime = target.ClrVersions.First().CreateRuntime();

            // We can't get the thread name from the ClrThead objects, so we'll look for
            // Thread instances on the heap and get the names from those.    
            var threadNameLookup = new Dictionary<int, string>();
            foreach (var obj in runtime.Heap.EnumerateObjects())
            {
                if (!(obj.Type is null) && obj.Type.Name == "System.Threading.Thread")
                {
                    var threadId = obj.ReadField<int>("m_ManagedThreadId");
                    var threadName = obj.ReadStringField("m_Name");
                    threadNameLookup[threadId] = threadName;
                }
            }  
            foreach (var thread in runtime.Threads)
            {
                threadNameLookup.TryGetValue(thread.ManagedThreadId, out string threadName);
                result.AppendLine(
                    $"ManagedThreadId: {thread.ManagedThreadId}, Name: {threadName}, OSThreadId: {thread.OSThreadId}, Thread: IsAlive: {thread.IsAlive}, IsBackground: {thread.IsBackground} \n");

                foreach (var clrStackFrame in thread.EnumerateStackTrace())
                    result.AppendLine($"{clrStackFrame.Method}");

                result.AppendLine($"\n");
            }

        }

For iOS, it throw error at CreateSnapshotAndAttach

Please suggest a solution.

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,115 questions
{count} votes