Пример #1
0
        } // end _Matches()

        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            if (0 != Address)
            {
                WriteObject(Debugger.GetModuleByAddress(Address));
                return;
            }

            //
            // If you just ran something like "lm 123`456789ab", PS will interpret that
            // address as a string. If you use "-Address", the [AddressTransformation]
            // attribute will handle the conversion, but otherwise we'll have to do it
            // manually.
            //
            // Let's check if Name is actually an address:
            //

            if (!String.IsNullOrEmpty(Name))
            {
                object addrObj = AddressTransformationAttribute.Transform(null,   // EngineIntrinsics
                                                                          SessionState.Path.CurrentProviderLocation(DbgProvider.ProviderId).ProviderPath,
                                                                          true,   // skipGlobalSymbolTest
                                                                          false,  // throwOnFailure
                                                                          false,  // dbgMemoryPassThru
                                                                          false,  // allowList
                                                                          Name);

                if ((null != addrObj) && (addrObj is ulong))
                {
                    WriteObject(Debugger.GetModuleByAddress((ulong)addrObj));
                    return;
                }
            }

            IList <DbgModuleInfo> modules;

            if (Unloaded)
            {
                modules = Debugger.UnloadedModules;
            }
            else
            {
                modules = Debugger.Modules;
            }

            foreach (var mod in modules)
            {
                if (_Matches(mod))
                {
                    WriteObject(mod);
                }
            }
        } // end ProcessRecord()
        public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
        {
            object originalInputData = inputData;

            var pso = inputData as PSObject;

            if (null != pso)
            {
                inputData = pso.BaseObject;
            }

            if (inputData is string)
            {
                return(inputData);
            }

            if (inputData is DbgModuleInfo)
            {
                return(((DbgModuleInfo)inputData).ImageName);
            }

            string path     = engineIntrinsics.SessionState.Path.CurrentProviderLocation(DbgProvider.ProviderId).ProviderPath;
            var    debugger = DbgProvider.GetDebugger(path);

            Exception e = null;

            // Maybe it is an address?
            try
            {
                object addrObj = AddressTransformationAttribute.Transform(
                    engineIntrinsics,
                    null,
                    true,                  // skipGlobalSymbolTest
                    false,                 // throwOnFailure
                    originalInputData);
                if (null != addrObj)
                {
                    var mod = new DbgModuleInfo(debugger,
                                                (ulong)addrObj,
                                                debugger.GetCurrentTarget());
                    return(mod.Name);
                }
            }
            catch (DbgEngException dee)
            {
                e = dee;
            }

            throw CreateRecoverableAtme(e,
                                        "Could not convert '{0}' to a module name.",
                                        originalInputData);
        } // end Transform()
        private static bool _TryConvertToUint(EngineIntrinsics engineIntrinsics, object inputData, out uint val)
        {
            val = 0xffffffff;

            object originalInputData = inputData;

            var pso = inputData as PSObject;

            if (null != pso)
            {
                inputData = pso.BaseObject;
            }

            if (inputData is int)
            {
                val = (uint)(int)inputData;
                return(true);
            }
            else if (inputData is uint)
            {
                val = (uint)inputData;
                return(true);
            }

            // Maybe it was hex and got parsed as a string?
            try
            {
                object addrObj = AddressTransformationAttribute.Transform(
                    engineIntrinsics,
                    null,
                    true,                  // skipGlobalSymbolTest
                    false,                 // throwOnFailure
                    originalInputData);
                if (null != addrObj)
                {
                    ulong asAddr = (ulong)addrObj;
                    val = unchecked ((uint)asAddr);
                    return(true);
                }
            }
            catch (DbgEngException)
            {
            }

            return(false);
        } // end _TryConvertToUint()
        public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
        {
            object originalInputData = inputData;

            var pso = inputData as PSObject;

            if (null != pso)
            {
                inputData = pso.BaseObject;
            }

            if (inputData is DbgModuleInfo)
            {
                return(inputData);
            }

            if (null == inputData)
            {
                return(null);
            }

            string path     = engineIntrinsics.SessionState.Path.CurrentProviderLocation(DbgProvider.ProviderId).ProviderPath;
            var    debugger = DbgProvider.GetDebugger(path);

            Exception e   = null;
            string    str = inputData as string;

            if (null != str)
            {
                if (0 == str.Length)
                {
                    return(null);
                }

                try
                {
                    var foundMod = debugger.Modules.FirstOrDefault((m) => 0 == Util.Strcmp_OI(m.Name, str));
                    if (null != foundMod)
                    {
                        return(foundMod);
                    }
                }
                catch (DbgProviderException dpe)
                {
                    e = dpe;
                }
            }

            if (null == e)
            {
                // Maybe it was an address...
                try
                {
                    object addrObj = AddressTransformationAttribute.Transform(
                        engineIntrinsics,
                        null,
                        true,                  // skipGlobalSymbolTest
                        false,                 // throwOnFailure
                        originalInputData);
                    if (null != addrObj)
                    {
                        return(new DbgModuleInfo(debugger,
                                                 (ulong)addrObj,
                                                 debugger.GetCurrentTarget()));
                    }
                }
                catch (DbgEngException dee)
                {
                    e = dee;
                }
            }

            throw CreateRecoverableAtme(e,
                                        "Could not convert '{0}' to a module.",
                                        originalInputData);
        } // end Transform()
        public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
        {
            if (0 == ElementSize)
            {
                // Pointer size.
                dynamic debugger = engineIntrinsics.SessionState.PSVariable.GetValue("Debugger", null);
                if (null != debugger)
                {
                    ElementSize = debugger.TargetIs32Bit ? 4 : 8;
                }
                else
                {
                    Util.Fail("Could not get debugger?");
                    ElementSize = 8;
                }
            } // end if( 0 == ElementSize )

            object addrObj = AddressTransformationAttribute.Transform(engineIntrinsics,
                                                                      null,
                                                                      true,   // skipGlobalSymbolTest
                                                                      false,  // throwOnFailure
                                                                      inputData);

            if (null != addrObj)
            {
                return(_CreatePso((ulong)addrObj));
            }

            //
            // It must be in "range" syntax. ("L[?][0n|0x]\d+", ex. "L100")
            //

            var pso = inputData as PSObject;

            if (null != pso)
            {
                inputData = pso.BaseObject;
            }

            // Some commands do not require a range.
            if (null == inputData)
            {
                return(_CreatePso((ulong)0));
            }

            string str = inputData as string;

            if (null != str)
            {
                // TODO: Shoule we implement "n" command and "default radix" concept? Or
                // will that be too everlastingly confusing, given that we can't change
                // the "radix" concept for the rest of PowerShell?

                // Some commands do not require a range.
                if (0 == str.Length)
                {
                    return(_CreatePso((ulong)0));
                }

                bool sizeOverride    = false;
                bool negative        = false;
                bool hasLengthPrefix = false;

                if (('L' == str[0]) || ('l' == str[0]))
                {
                    hasLengthPrefix = true;
                    int numIdx = 1;
                    if (str.Length > 1)
                    {
                        if ('?' == str[1])
                        {
                            sizeOverride = true;
                            numIdx++;
                        }
                        else if ('-' == str[1])
                        {
                            negative = true;
                            numIdx++;
                        }
                    }
                    str = str.Substring(numIdx);
                } // end if( StartsWith( 'L' ) )

                NumberStyles ns = NumberStyles.AllowHexSpecifier;
                if (str.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                {
                    str = str.Substring(2);
                }
                else if (str.StartsWith("0n", StringComparison.OrdinalIgnoreCase))
                {
                    ns  = NumberStyles.None;
                    str = str.Substring(2);
                }
                ulong rangeOrAddr;
                if (UInt64.TryParse(str, ns, CultureInfo.InvariantCulture, out rangeOrAddr))
                {
                    // 256 MB seems rather large... maybe I'll default to 256k without a size override.
                    const ulong maxRange = 1024 * 256;

                    // We only do a max size check if we know we are checking a range;
                    // otherwise it could be an end address.
                    if (hasLengthPrefix &&
                        ((rangeOrAddr > maxRange) || ((rangeOrAddr * (ulong)ElementSize) > maxRange)) &&
                        !sizeOverride)
                    {
                        throw new ArgumentTransformationMetadataException(
                                  Util.Sprintf("Requested size ({0} * {1}) is too large; probably a typo. If you really want a size that large, use the \"L?<size> syntax.",
                                               rangeOrAddr,
                                               ElementSize));
                    }
                    return(_CreatePso(rangeOrAddr, hasLengthPrefix, negative));
                }
            } // end if( it's a string )

            throw CreateRecoverableAtme("Could not convert '{0}' to a range or an address.",
                                        inputData);
        } // end Transform()