public static ThreadSnapshot[] GetProfileWithRelease(out Int32 hr) { ThreadSnapshot[] res = new ThreadSnapshot[0]; try { res = GetProfile(out hr); } finally { ReleaseProfile(); } return(res); }
public static ThreadSnapshot[] GetProfile(out Int32 hr) { Int32 hresult = RequestProfile(out IntPtr nativeSnapshots, out int snapshotLength); hr = hresult; if (hresult >= 0 && IntPtr.Zero != nativeSnapshots && snapshotLength > 0) { ThreadSnapshot[] marshalledSnapshots = new ThreadSnapshot[snapshotLength]; for (int indx = 0; indx != snapshotLength; ++indx) { marshalledSnapshots[indx] = new ThreadSnapshot(); marshalledSnapshots[indx].ThreadId = ReadUIntPtr(nativeSnapshots); nativeSnapshots += ThreadID.Size; marshalledSnapshots[indx].ErrorCode = Marshal.ReadInt32(nativeSnapshots); nativeSnapshots += sizeof(Int32); // did we get stack walk? nominally 0 or 1 if the stack was too deep if (marshalledSnapshots[indx].ErrorCode >= 0) { Int32 countOfSnapshots = Marshal.ReadInt32(nativeSnapshots); nativeSnapshots += sizeof(Int32); marshalledSnapshots[indx].FunctionIDs = new UIntPtr[countOfSnapshots]; if (countOfSnapshots > 0) { IntPtr FunctionIDPointer = Marshal.ReadIntPtr(nativeSnapshots); for (int fidx = 0; fidx != countOfSnapshots; ++fidx, FunctionIDPointer += IntPtr.Size) { marshalledSnapshots[indx].FunctionIDs[fidx] = ReadUIntPtr(FunctionIDPointer); } } nativeSnapshots += IntPtr.Size; } else { nativeSnapshots += sizeof(Int32) + IntPtr.Size; } } return(marshalledSnapshots); } else { return(new ThreadSnapshot[0]); } }