public async Task <bool> ShouldAttachToIncosistentCoreFileAsync(DumpReadWarning warning)
        {
            await _taskContext.Factory.SwitchToMainThreadAsync();

            string dialogMessage = null;
            string dialogTitle   = null;

            switch (warning)
            {
            case DumpReadWarning.FileDoesNotExist:
                _dialogUtil.ShowError(ErrorStrings.CoreFileDoesNotExist);
                return(false);

            case DumpReadWarning.ElfHeaderIsCorrupted:
                dialogTitle   = ErrorStrings.DialogTitleWarning;
                dialogMessage = ErrorStrings.CoreFileCorruptedWarningMessage;
                break;

            case DumpReadWarning.FileIsTruncated:
                dialogTitle   = ErrorStrings.DialogTitleWarning;
                dialogMessage = ErrorStrings.CoreFileTruncatedWarningMessage;
                break;

            case DumpReadWarning.ExecutableBuildIdMissing:
                dialogTitle   = ErrorStrings.CoreAttachBuildIdMissingWarningTitle;
                dialogMessage = ErrorStrings.CoreAttachBuildIdMissingWarningMessage;
                break;

            case DumpReadWarning.None:
                break;
            }

            return(_dialogUtil.ShowYesNoWarning(dialogMessage, dialogTitle));
        }
 public DumpReadResult(IEnumerable <DumpModule> modules, DumpReadWarning warning)
 {
     Modules = modules;
     Warning = warning;
 }
        DumpReadResult GetModules(BinaryReader dumpReader)
        {
            ulong dumpSize   = (ulong)dumpReader.BaseStream.Length;
            var   moduleList = new List <DumpModule>();

            if (!ElfHeader.TryRead(dumpReader, out ElfHeader elfHeader))
            {
                return(new DumpReadResult(moduleList, DumpReadWarning.ElfHeaderIsCorrupted));
            }

            var             fileSections = new List <FileSection>();
            var             loadSegments = new List <ProgramHeader>();
            DumpReadWarning warning      = DumpReadWarning.None;

            // Go through each program header sections, look for the notes sections and the
            // loadable segments.
            for (ulong i = 0; i < elfHeader.EntriesCount; ++i)
            {
                ulong headerOffset = elfHeader.StartOffset + i * elfHeader.EntrySize;
                dumpReader.BaseStream.Seek((long)headerOffset, SeekOrigin.Begin);

                if (!ProgramHeader.TryRead(dumpReader, out ProgramHeader header))
                {
                    return(new DumpReadResult(moduleList, DumpReadWarning.FileIsTruncated));
                }

                // Set the warning if the program header is outside of the file.
                if (header.OffsetInFile + header.SizeInFile > dumpSize &&
                    warning == DumpReadWarning.None)
                {
                    warning = DumpReadWarning.FileIsTruncated;
                }

                switch (header.HeaderType)
                {
                // We found the notes section. Now we need to extract module section from
                // the NT_FILE notes.
                case ProgramHeader.Type.NoteSegment:
                    if (header.SizeInFile > int.MaxValue)
                    {
                        Trace.WriteLine("Can't extract note segment sections from program" +
                                        "header. Note size is more then int.Max.");
                        continue;
                    }

                    int size = (int)header.SizeInFile;
                    dumpReader.BaseStream.Seek((long)header.OffsetInFile, SeekOrigin.Begin);
                    byte[] notesBytes  = dumpReader.ReadBytes(size);
                    var    notesStream = new MemoryStream(notesBytes);
                    using (var notesReader = new BinaryReader(notesStream))
                    {
                        IEnumerable <FileSection> moduleSections =
                            NoteSection.ReadModuleSections(notesReader, size);
                        fileSections.AddRange(moduleSections);
                    }

                    break;

                // Collect memory mappings for the core files.
                case ProgramHeader.Type.LoadableSegment:
                    loadSegments.Add(header);
                    break;
                }
            }

            var loadableSegmentsReader = new ModuleReader(loadSegments, dumpReader);

            // Go through each module and try to find build id in the mapped regions.
            foreach (FileSection file in fileSections)
            {
                DumpModule module = loadableSegmentsReader.GetModule(file);
                if (module.Id == BuildId.Empty)
                {
                    Trace.WriteLine($"Can't find build id for module {module.Path}.");
                }
                else
                {
                    moduleList.Add(module);
                }
            }

            return(new DumpReadResult(moduleList, warning));
        }