Example #1
0
        private static unsafe void GenerateErrorReportForDump(LowLevelList <byte[]> serializedExceptions)
        {
            checked
            {
                int loadedModuleCount = (int)RuntimeImports.RhGetLoadedOSModules(null);
                int cbModuleHandles   = sizeof(System.IntPtr) * loadedModuleCount;
                int cbFinalBuffer     = sizeof(ERROR_REPORT_BUFFER_HEADER) + sizeof(SERIALIZED_ERROR_REPORT_HEADER) + cbModuleHandles;
                for (int i = 0; i < serializedExceptions.Count; i++)
                {
                    cbFinalBuffer += serializedExceptions[i].Length;
                }

                byte[] finalBuffer = new byte[cbFinalBuffer];
                fixed(byte *pBuffer = &finalBuffer[0])
                {
                    byte *pCursor     = pBuffer;
                    int   cbRemaining = cbFinalBuffer;

                    ERROR_REPORT_BUFFER_HEADER *pDacHeader = (ERROR_REPORT_BUFFER_HEADER *)pCursor;

                    pDacHeader->WriteHeader(cbFinalBuffer);
                    pCursor     += sizeof(ERROR_REPORT_BUFFER_HEADER);
                    cbRemaining -= sizeof(ERROR_REPORT_BUFFER_HEADER);

                    SERIALIZED_ERROR_REPORT_HEADER *pPayloadHeader = (SERIALIZED_ERROR_REPORT_HEADER *)pCursor;

                    pPayloadHeader->WriteHeader(serializedExceptions.Count, loadedModuleCount);
                    pCursor     += sizeof(SERIALIZED_ERROR_REPORT_HEADER);
                    cbRemaining -= sizeof(SERIALIZED_ERROR_REPORT_HEADER);

                    // copy the serialized exceptions to report buffer
                    for (int i = 0; i < serializedExceptions.Count; i++)
                    {
                        int cbChunk = serializedExceptions[i].Length;
                        serializedExceptions[i].AsSpan().CopyTo(new Span <byte>(pCursor, cbChunk));
                        cbRemaining -= cbChunk;
                        pCursor     += cbChunk;
                    }

                    // copy the module-handle array to report buffer
                    IntPtr[] loadedModuleHandles = new IntPtr[loadedModuleCount];
                    RuntimeImports.RhGetLoadedOSModules(loadedModuleHandles);
                    loadedModuleHandles.AsSpan().CopyTo(new Span <IntPtr>(pCursor, loadedModuleHandles.Length));
                    cbRemaining -= cbModuleHandles;
                    pCursor     += cbModuleHandles;

                    Debug.Assert(cbRemaining == 0);
                }

                UpdateErrorReportBuffer(finalBuffer);
            }
        }