Beispiel #1
0
        protected void GetVersionFromVersionString()
        {
            GetPEInfo();

            // If we can't get the version from the PE, search for version string embedded in the module data
            if (!_version.HasValue && !IsPEImage)
            {
                string versionString = VersionString;
                if (versionString != null)
                {
                    int spaceIndex = versionString.IndexOf(' ');
                    if (spaceIndex > 0)
                    {
                        if (versionString[spaceIndex - 1] == '.')
                        {
                            spaceIndex--;
                        }
                        string versionToParse = versionString.Substring(0, spaceIndex);
                        try
                        {
                            Version version = System.Version.Parse(versionToParse);
                            _version = new VersionInfo(version.Major, version.Minor, version.Build, version.Revision);
                        }
                        catch (ArgumentException ex)
                        {
                            Trace.TraceError($"Module.Version FAILURE: '{versionToParse}' '{versionString}' {ex}");
                        }
                    }
                }
            }
        }
Beispiel #2
0
 public int CompareTo(VersionInfo?other)
 {
     return(other switch {
         not null when Major != other.Major => Major.CompareTo(other.Major),
         not null when Minor != other.Minor => Minor.CompareTo(other.Minor),
         _ => other is not null && Build != other.Build ? Build.CompareTo(other.Build) : 0
     });
Beispiel #3
0
 //--- Constructors ---
 public ModuleInfo(string ns, string name, VersionInfo?version, string?origin)
 {
     Namespace = ns ?? throw new ArgumentNullException(nameof(ns));
     Name      = name ?? throw new ArgumentNullException(nameof(name));
     FullName  = $"{Namespace}.{Name}";
     Version   = version;
     Origin    = origin;
 }
Beispiel #4
0
        // DataTarget is one of the few "internal set" properties, and is initialized as soon as DataTarget asks
        // IDataReader to create ModuleInfo.  So even though we don't set it here, we will immediately set the
        // value to non-null and never change it.

#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.

        /// <summary>
        /// Creates a ModuleInfo object with an IDataReader instance.  This is used when
        /// lazily evaluating VersionInfo.
        /// </summary>
        public ModuleInfo(ulong imgBase, int filesize, int timestamp, string?fileName, bool isVirtual, ImmutableArray <byte> buildId = default, VersionInfo?version = null)
        {
            ImageBase      = imgBase;
            IndexFileSize  = filesize;
            IndexTimeStamp = timestamp;
            FileName       = fileName;
            _isVirtual     = isVirtual;
            BuildId        = buildId;
            _version       = version;
        }
Beispiel #5
0
 /// <summary>
 /// Creates a ModuleInfo object with an IDataReader instance.  This is used when
 /// lazily evaluating VersionInfo.
 /// </summary>
 /// <param name="reader"></param>
 public ModuleInfo(IDataReader reader, ulong imgBase, uint filesize, uint timestamp, string?filename,
                   IReadOnlyList <byte>?buildId = null, VersionInfo?version = null)
 {
     _dataReader = reader ?? throw new ArgumentNullException(nameof(reader));
     ImageBase   = imgBase;
     FileSize    = filesize;
     TimeStamp   = timestamp;
     FileName    = filename;
     BuildId     = buildId;
     _version    = version;
 }
Beispiel #6
0
        internal VersionInfo GetVersionInfo(Converter converter, uint version)
        {
            // Try to get the version if it already exists.
            VersionInfo?existing = VersionCacheHandler.GetVersionOrAddNull(converter, version);

            if (existing != null)
            {
                return(existing);
            }

            // If it doesn't, generate it.
            MapGenerator?gen = GetGenerator();
            VersionInfo? res = VersionCacheHandler.AddNewVersion(converter, version, gen);

            ReleaseGenerator(gen);
            return(res);
        }
Beispiel #7
0
 /// <summary>
 /// Creates a ModuleInfo object with an IDataReader instance.  This is used when
 /// lazily evaluating VersionInfo.
 /// </summary>
 public ModuleInfo(
     IDataReader reader,
     ulong imgBase,
     int filesize,
     int timestamp,
     string?fileName,
     ImmutableArray <byte> buildId = default,
     VersionInfo?version           = null)
 {
     _dataReader = reader ?? throw new ArgumentNullException(nameof(reader));
     ImageBase   = imgBase;
     FileSize    = filesize;
     TimeStamp   = timestamp;
     FileName    = fileName;
     BuildId     = buildId;
     _version    = version;
 }
        /// <summary>
        /// Returns the PE file's PDB info from the debug directory
        /// </summary>
        /// <param name="address">module base address</param>
        /// <param name="size">module size</param>
        /// <param name="pdbInfo">the pdb record or null</param>
        /// <param name="version">the PE version or null</param>
        /// <param name="flags">module flags</param>
        /// <returns>PEImage instance or null</returns>
        internal PEImage GetPEInfo(ulong address, ulong size, ref PdbInfo pdbInfo, ref VersionInfo?version, ref Module.Flags flags)
        {
            PEImage peImage = null;

            // None of the modules that lldb (on either Linux/MacOS) provides are PEs
            if (Target.Host.HostType != HostType.Lldb)
            {
                // First try getting the PE info as load layout (native Windows DLLs and most managed PEs on Linux/MacOS).
                peImage = GetPEInfo(isVirtual: true, address, size, ref pdbInfo, ref version, ref flags);
                if (peImage == null)
                {
                    if (Target.OperatingSystem != OSPlatform.Windows)
                    {
                        // Then try getting the PE info as file layout (some managed PEs on Linux/MacOS).
                        peImage = GetPEInfo(isVirtual: false, address, size, ref pdbInfo, ref version, ref flags);
                    }
                }
            }
            return(peImage);
        }
Beispiel #9
0
        public static VersionInfo AddNewVersion(Converter converter, uint version, MapGenerator gen)
        {
            if (converter._hasOneVersion || version > converter.HighestVersion)
            {
                throw new UnsupportedVersionException(converter.ItemType, version);
            }

            VersionInfo?newVer = GetVersionInfo(converter, version, gen);

            lock (converter._versionCache.MultipleVersions)
            {
                converter._versionCache.MultipleVersions[version] = newVer;
                if (converter._versionCache.MultipleVersions.Count > converter.HighestVersion)
                {
                    converter.HandleAllVersionsGenerated();
                }
            }

            return(newVer);
        }
Beispiel #10
0
        internal void HandleVersionNumber(Converter item, out VersionInfo info, ref BitSource header)
        {
            if (item._instanceId >= _currentVersionInfos.Length)
            {
                Array.Resize(ref _currentVersionInfos, (int)Map._highestConverterInstanceId);
            }

            // If the version has already been read, do nothing
            VersionInfo?existingInfo = _currentVersionInfos[item._instanceId];

            if (existingInfo != null)
            {
                info = existingInfo;
                return;
            }

            uint version = ReadNewVersionInfo(ref header);

            info = Map.GetVersionInfo(item, version);
            _currentVersionInfos[item._instanceId] = info;
        }
Beispiel #11
0
        /// <summary>
        /// Handles the version info for a given converter. If the version hasn't been written yet, it's written now. If not, nothing is written.
        /// </summary>
        /// <returns>Whether the item already exists</returns>
        internal bool HandleVersionNumber(Converter item, out VersionInfo info, ref BitTarget header)
        {
            if (item._instanceId >= _currentVersionInfos.Length)
            {
                Array.Resize(ref _currentVersionInfos, (int)Map._highestConverterInstanceId);
            }

            // If the version has already been written, do nothing
            VersionInfo?existingInfo = _currentVersionInfos[item._instanceId];

            if (existingInfo != null)
            {
                info = existingInfo;
                return(true);
            }

            uint version = WriteNewVersionInfo(item, ref header);

            info = Map.GetVersionInfo(item, version);
            _currentVersionInfos[item._instanceId] = info;
            return(false);
        }
Beispiel #12
0
        public IEnumerable <ModuleInfo> EnumerateModules()
        {
            List <ModuleInfo> result = new List <ModuleInfo>();

            foreach (var entry in _memoryMapEntries)
            {
                if (string.IsNullOrEmpty(entry.FilePath))
                {
                    continue;
                }

                if (!result.Exists(module => module.FileName == entry.FilePath))
                {
                    int         filesize  = 0;
                    int         timestamp = 0;
                    VersionInfo?version   = null;

                    if (File.Exists(entry.FilePath))
                    {
                        try
                        {
#pragma warning disable CA2000 // Dispose objects before losing scope
                            using FileStream stream = File.OpenRead(entry.FilePath);
#pragma warning restore CA2000 // Dispose objects before losing scope
                            using PEImage pe = new PEImage(stream);
                            if (pe.IsValid)
                            {
                                filesize  = pe.IndexFileSize;
                                timestamp = pe.IndexTimeStamp;
                                version   = pe.GetFileVersionInfo()?.VersionInfo;
                            }
                        }
                        catch
                        {
                        }
                    }

                    ModuleInfo moduleInfo = new ModuleInfo(this, entry.BeginAddr, filesize, timestamp, entry.FilePath, buildId: default, version: version ?? default);
Beispiel #13
0
 public override void Invoke()
 {
     if (Runtime == null)
     {
         throw new DiagnosticsException("No CLR runtime set");
     }
     foreach (ClrModule module in Runtime.EnumerateModules())
     {
         if (Verbose)
         {
             WriteLine("{0}{1}", module.Name, module.IsDynamic ? "(Dynamic)" : "");
             WriteLine("    AssemblyName:    {0}", module.AssemblyName);
             WriteLine("    ImageBase:       {0:X16}", module.ImageBase);
             WriteLine("    Size:            {0:X8}", module.Size);
             WriteLine("    Address:         {0:X16}", module.Address);
             WriteLine("    IsPEFile:        {0}", module.IsPEFile);
             WriteLine("    Layout:          {0}", module.Layout);
             WriteLine("    IsDynamic:       {0}", module.IsDynamic);
             WriteLine("    MetadataAddress: {0:X16}", module.MetadataAddress);
             WriteLine("    MetadataSize:    {0:X16}", module.MetadataLength);
             WriteLine("    PdbInfo:         {0}", module.Pdb?.ToString() ?? "<none>");
             VersionInfo?version = null;
             try
             {
                 version = ModuleService.GetModuleFromBaseAddress(module.ImageBase).Version;
             }
             catch (DiagnosticsException)
             {
             }
             WriteLine("    Version:         {0}", version?.ToString() ?? "<none>");
         }
         else
         {
             WriteLine("{0:X16} {1:X8} {2}{3}", module.ImageBase, module.Size, module.Name, module.IsDynamic ? "(Dynamic)" : "");
         }
     }
 }
        internal bool IsInterested(ITextView textView, out ITagger <ITag> tagger)
        {
            if (!_reSharperUtil.IsInstalled)
            {
                tagger = null;
                return(false);
            }

            if (!_versionInfo.HasValue)
            {
                // There is a possible race in MEF construction which would allow this method to be
                // called before we had the list of ITaggerProvider instances to query.  In that case
                // defer to the next check.
                if (TaggerProviders == null)
                {
                    tagger = null;
                    return(false);
                }

                _versionInfo = DetectVersionInfo();
            }

            return(IsInterested(textView.TextBuffer, out tagger));
        }
Beispiel #15
0
 public bool Equals(VersionInfo?other)
 {
     return(other is not null && Major == other.Major && Minor == other.Minor && Build == other.Build);
 }
Beispiel #16
0
        /// <summary>
        /// Returns information about the PE file.
        /// </summary>
        /// <param name="isVirtual">the memory layout of the module</param>
        /// <param name="address">module base address</param>
        /// <param name="size">module size</param>
        /// <param name="pdbInfo">the pdb record or null</param>
        /// <param name="version">the PE version or null</param>
        /// <param name="flags">module flags</param>
        /// <returns>PEImage instance or null</returns>
        private PEImage GetPEInfo(bool isVirtual, ulong address, ulong size, ref PdbInfo pdbInfo, ref VersionInfo?version, ref Module.Flags flags)
        {
            Stream stream = MemoryService.CreateMemoryStream(address, size);

            try
            {
                stream.Position = 0;
                var peImage = new PEImage(stream, leaveOpen: false, isVirtual);
                if (peImage.IsValid)
                {
                    flags  |= Module.Flags.IsPEImage;
                    flags  |= peImage.IsManaged ? Module.Flags.IsManaged : Module.Flags.None;
                    pdbInfo = peImage.DefaultPdb;
                    if (!version.HasValue)
                    {
                        FileVersionInfo fileVersionInfo = peImage.GetFileVersionInfo();
                        if (fileVersionInfo != null)
                        {
                            version = fileVersionInfo.VersionInfo;
                        }
                    }
                    flags &= ~(Module.Flags.IsLoadedLayout | Module.Flags.IsFileLayout);
                    flags |= isVirtual ? Module.Flags.IsLoadedLayout : Module.Flags.IsFileLayout;
                    return(peImage);
                }
                else
                {
                    Trace.TraceError($"GetPEInfo: PE invalid {address:X16} isVirtual {isVirtual}");
                }
            }
            catch (Exception ex) when(ex is BadImageFormatException || ex is EndOfStreamException || ex is IOException)
            {
                Trace.TraceError($"GetPEInfo: loaded {address:X16} isVirtual {isVirtual} exception {ex.Message}");
            }
            return(null);
        }
Beispiel #17
0
        public static bool TryParse(string text, [NotNullWhen(true)] out VersionInfo?version)
        {
            // check for version suffix (e.g "-rc1")
            var    index = text.IndexOf('-');
            string versionText;
            string suffix;

            if (index < 0)
            {
                versionText = text;
                suffix      = "";
            }
            else
            {
                versionText = text.Substring(0, index);
                suffix      = text.Substring(index).TrimEnd('*');
            }

            // parse parts of the version number
            int major;
            int minor;
            int build;
            int revision;

            if (int.TryParse(versionText, out major))
            {
                // version is a single integral major version number
                minor    = -1;
                build    = -1;
                revision = -1;
            }
            else if (Version.TryParse(versionText, out var basicVersion))
            {
                // assign parts of the parsed version information
                major    = basicVersion.Major;
                minor    = basicVersion.Minor;
                build    = basicVersion.Build;
                revision = basicVersion.Revision;
            }
            else
            {
                version = null;
                return(false);
            }

            // if major version is 0 and minor is not 0, we encode the value as a fractional major version number
            if ((major == 0) && (minor != 0))
            {
                var fractionalMajor = int.MinValue + minor - 1;
                version = new VersionInfo(
                    fractionalMajor,
                    (build != -1) ? (int?)build : null,
                    (revision != -1) ? (int?)revision : null,
                    suffix
                    );
                return(true);
            }

            // revision is not supported in this format since it requires 4 numbers to be stored
            if (revision != -1)
            {
                version = null;
                return(false);
            }
            version = new VersionInfo(
                major,
                (minor != -1) ? (int?)minor : null,
                (build != -1) ? (int?)build : null,
                suffix
                );
            return(true);
        }
        internal void ResetForVersion(ReSharperVersion version, ITaggerProvider taggerProvider = null)
        {
            var editTagDetector = GetEditTagDetector(version);

            _versionInfo = new VersionInfo(version, editTagDetector, taggerProvider);
        }
        internal bool IsInterested(ITextView textView, out ITagger<ITag> tagger)
        {
            if (!_reSharperUtil.IsInstalled)
            {
                tagger = null;
                return false;
            }

            if (!_versionInfo.HasValue)
            {
                // There is a possible race in MEF construction which would allow this method to be 
                // called before we had the list of ITaggerProvider instances to query.  In that case
                // defer to the next check.
                if (TaggerProviders == null)
                {
                    tagger = null;
                    return false;
                }

                _versionInfo = DetectVersionInfo();
            }

            return IsInterested(textView.TextBuffer, out tagger);
        }
 internal void ResetForVersion(ReSharperVersion version, ITaggerProvider taggerProvider = null)
 {
     var editTagDetector = GetEditTagDetector(version);
     _versionInfo = new VersionInfo(version, editTagDetector, taggerProvider);
 }