Exemplo n.º 1
0
        /// <summary>
        /// Trace debug information for specified PInvoke calls.
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="moduleName">Module name</param>
        /// <param name="pinvokeName"></param>
        /// <param name="callerName"></param>
        /// <param name="returnValue"></param>
        /// <param name="errorReturnValue"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        internal static PInvokeDebugInfo TraceDebugInfo(PInvokeCaptureFilters filter, string moduleName, string pinvokeName, string callerName, object returnValue, object errorReturnValue, params object[] args)
        {
            PInvokeDebugInfo debugInfo = SetupDebugInfo(filter, moduleName, pinvokeName, callerName, returnValue, args);

            // check if type of returnValue is NTSTATUS
            if (returnValue is NTSTATUS)
            {
                NTSTATUS status = (NTSTATUS)returnValue;

                if (status.IsError() || status.IsWarning())
                {
                    if (filter.HasFlag(PInvokeCaptureFilters.ErrorCode))
                    {
                        debugInfo.ErrorCode = (int)status;
                    }
                    if (filter.HasFlag(PInvokeCaptureFilters.ErrorDescription))
                    {
                        debugInfo.ErrorDescription = PInvokeDebugger.TranslateError((int)returnValue, ModuleManager.NtDll);
                    }
                    debugInfo.IsWarning = status.IsWarning();
                    debugInfo.IsError   = status.IsError();
                }
            }
            else
            {
                if (returnValue == errorReturnValue && returnValue != null)
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    if (filter.HasFlag(PInvokeCaptureFilters.ErrorCode))
                    {
                        debugInfo.ErrorCode = errorCode;
                    }
                    if (filter.HasFlag(PInvokeCaptureFilters.ErrorDescription))
                    {
                        debugInfo.ErrorDescription = PInvokeDebugger.TranslateError(errorCode);
                    }
                    debugInfo.IsError = true;
                }
            }

            return(debugInfo);
        }
Exemplo n.º 2
0
        private static PInvokeDebugInfo SetupDebugInfo(PInvokeCaptureFilters filter, string moduleName, string pinvokeName, string callerName, object returnValue, params object[] args)
        {
            string moduleInfo      = filter.HasFlag(PInvokeCaptureFilters.ModuleName) ? moduleName : InfoNotTraced;
            string pinvokeInfo     = filter.HasFlag(PInvokeCaptureFilters.PInvokeName) ? pinvokeName : InfoNotTraced;
            string callerInfo      = filter.HasFlag(PInvokeCaptureFilters.CallerName) ? callerName : InfoNotTraced;
            object returnValueInfo = filter.HasFlag(PInvokeCaptureFilters.ReturnValue) ? returnValue : InfoNotTraced;

            PInvokeDebugInfo debugInfo = new PInvokeDebugInfo {
                ModuleName  = moduleInfo,
                PInvokeName = pinvokeInfo,
                CallerName  = callerInfo,
                ReturnValue = returnValueInfo
            };

            if (filter.HasFlag(PInvokeCaptureFilters.Parameters))
            {
                if (args == null)
                {
                    throw new ArgumentNullException(nameof(args));
                }
                if (args.Length % 2 != 0)
                {
                    throw new ArgumentException(SR.GetString("SR_InvalidParameterArgsLength"));
                }

                Dictionary <string, object> parameters = new Dictionary <string, object>();
                for (int i = 0; i < args.Length; i += 2)
                {
                    string paramName  = (string)args[i];
                    object paramValue = args[i + 1];

                    parameters[paramName] = paramValue;
                }

                debugInfo.Parameters = parameters;
            }

            return(debugInfo);
        }
 internal static void SafeCapture(PInvokeDebugInfo debugInfo)
 {
     lock (threadLock) {
         PInvokeCaptured?.Invoke(debugInfo);
     }
 }