public static void ThrowIfNonPositiveArgument(TimeSpan timeout, string argumentName) { if (timeout <= TimeSpan.Zero) { throw Fx.Exception.ArgumentOutOfRange(argumentName, timeout, InternalSR.TimeoutMustBePositive(argumentName, timeout)); } }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cultureInfo, object value, Type type) { if (value == null) { throw FxTrace.Exception.ArgumentNull("value"); } if (!(value is int)) { throw FxTrace.Exception.Argument("value", InternalSR.IncompatibleArgumentType(typeof(int), value.GetType())); } if ((int)value == 0) { return(ApplicationContainerSettingsDefaults.Session0ServiceSessionString); } else if ((int)value == -1) { return(ApplicationContainerSettingsDefaults.CurrentUserSessionDefaultString); } else { return(base.ConvertTo(context, cultureInfo, value, type)); } }
public static Exception AssertAndFailFast(string description) { Fx.Assert(description); string failFastMessage = InternalSR.FailFastMessage(description); // The catch is here to force the finally to run, as finallys don't run until the stack walk gets to a catch. // The catch makes sure that the finally will run before the stack-walk leaves the frame, but the code inside is impossible to reach. try { try { Fx.Exception.TraceFailFast(failFastMessage); } finally { Environment.FailFast(failFastMessage); } } catch { throw; } return(null); // we'll never get here since we've just fail-fasted }
// handles not only the simple cast, but also value type widening, etc. public static T Convert <T>(object source) { // first check the common cases if (source is T) { return((T)source); } if (source == null) { if (typeof(T).IsValueType() && !IsNullableType(typeof(T))) { throw Fx.Exception.AsError(new InvalidCastException(InternalSR.CannotConvertObject(source, typeof(T)))); } return(default(T)); } T result; if (TryNumericConversion <T>(source, out result)) { return(result); } throw Fx.Exception.AsError(new InvalidCastException(InternalSR.CannotConvertObject(source, typeof(T)))); }
unsafe void EtwRegister() { this.etwCallback = new UnsafeNativeMethods.EtwEnableCallback(EtwEnableCallBack); uint etwRegistrationStatus = UnsafeNativeMethods.EventRegister(ref this.providerId, this.etwCallback, null, ref this.traceRegistrationHandle); if (etwRegistrationStatus != 0) { throw new InvalidOperationException(InternalSR.EtwRegistrationFailed(etwRegistrationStatus.ToString("x", CultureInfo.CurrentCulture))); } }
private void EtwRegister() { unsafe { this.etwCallback = new UnsafeNativeMethods.EtwEnableCallback(this.EtwEnableCallBack); uint num = UnsafeNativeMethods.EventRegister(ref this.providerId, this.etwCallback, 0, out this.traceRegistrationHandle); if (num == 0) { return; } else { throw new InvalidOperationException(InternalSR.EtwRegistrationFailed(num.ToString("x", CultureInfo.CurrentCulture))); } } }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cultureInfo, object value, Type type) { if (value == null) { throw FxTrace.Exception.ArgumentNull("value"); } if (!(value is TimeSpan)) { throw FxTrace.Exception.Argument("value", InternalSR.IncompatibleArgumentType(typeof(TimeSpan), value.GetType())); } if ((TimeSpan)value == TimeSpan.MaxValue) { return("Infinite"); } else { return(base.ConvertTo(context, cultureInfo, value, type)); } }
AcquireContextAsyncResult(InstanceHandle handle, Transaction hostTransaction, TimeSpan timeout, out bool setOperationPending, bool synchronous, AsyncCallback callback, object state) : base(callback, state) { // Need to report back to the caller whether or not we set OperationPending. setOperationPending = false; this.handle = handle; HostTransaction = hostTransaction; this.timeoutHelper = new TimeoutHelper(timeout); AcquireContextAsyncResult transactionWait; bool reuseContext = false; lock (this.handle.ThisLock) { if (!this.handle.IsValid) { throw Fx.Exception.AsError(new OperationCanceledException(SRCore.HandleFreed)); } if (this.handle.OperationPending) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.CommandExecutionCannotOverlap)); } setOperationPending = true; this.handle.OperationPending = true; transactionWait = this.handle.CurrentTransactionalAsyncResult; if (transactionWait != null) { Fx.Assert(this.handle.AcquirePending == null, "Overlapped acquires pending."); // If the transaction matches but is already completed (or completing), the easiest ting to do // is wait for it to complete, then try to re-enlist, and have that failure be the failure mode for Execute. // We do that by following the regular, non-matching transaction path. if (transactionWait.HostTransaction.Equals(hostTransaction) && !this.handle.TooLateToEnlist) { reuseContext = true; this.executionContext = transactionWait.ReuseContext(); this.handle.CurrentExecutionContext = this.executionContext; } else { this.handle.AcquirePending = this; } } } if (transactionWait != null) { Fx.Assert(transactionWait.IsCompleted, "Old AsyncResult must be completed by now."); // Reuse the existing InstanceExecutionContext if this is the same transaction we're waiting for. if (reuseContext) { Complete(true); return; } TimeSpan waitTimeout = this.timeoutHelper.RemainingTime(); if (synchronous) { if (!transactionWait.WaitForHostTransaction.Wait(waitTimeout)) { throw Fx.Exception.AsError(new TimeoutException(InternalSR.TimeoutOnOperation(waitTimeout))); } } else { if (!transactionWait.WaitForHostTransaction.WaitAsync(AcquireContextAsyncResult.onHostTransaction, this, waitTimeout)) { return; } } } if (DoAfterTransaction()) { Complete(true); } }
public ArgumentException ArgumentNullOrEmpty(string paramName) { return(this.Argument(paramName, InternalSR.ArgumentNullOrEmpty(paramName))); }
protected override void Invoke() { _callback(_state, this.TimedOut ? new TimeoutException(InternalSR.TimeoutOnOperation(_originalTimeout)) : null); }
public unsafe bool WriteTransferEvent(ref EventDescriptor eventDescriptor, EventTraceActivity eventTraceActivity, Guid relatedActivityId, object[] eventPayload) { if (eventTraceActivity == null) { eventTraceActivity = EventTraceActivity.Empty; } uint num = 0; if (this.IsEnabled(eventDescriptor.Level, eventDescriptor.Keywords)) { if (eventPayload == null || (int)eventPayload.Length == 0) { num = UnsafeNativeMethods.EventWriteTransfer(this.traceRegistrationHandle, ref eventDescriptor, ref eventTraceActivity.ActivityId, ref relatedActivityId, 0, 0); } else { int length = (int)eventPayload.Length; if (length <= 32) { uint size = 0; int num1 = 0; int[] numArray = new int[8]; string[] strArrays = new string[8]; UnsafeNativeMethods.EventData *eventDataPointer = (UnsafeNativeMethods.EventData *)length * sizeof(UnsafeNativeMethods.EventData); UnsafeNativeMethods.EventData *eventDataPointer1 = eventDataPointer; byte *numPointer = (byte *)16 * length; byte *numPointer1 = numPointer; for (int i = 0; i < (int)eventPayload.Length; i++) { if (eventPayload[i] != null) { string str = DiagnosticsEventProvider.EncodeObject(ref eventPayload[i], eventDataPointer1, numPointer1); numPointer1 = numPointer1 + 16; size = size + (*(eventDataPointer1)).Size; eventDataPointer1 = (UnsafeNativeMethods.EventData *)(eventDataPointer1 + sizeof(UnsafeNativeMethods.EventData)); if (str != null) { if (num1 >= 8) { throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload", InternalSR.EtwAPIMaxStringCountExceeded(8))); } else { strArrays[num1] = str; numArray[num1] = i; num1++; } } } } if (size <= 0xffca) { fixed(string str1 = strArrays[0]) { string *strPointers = &str1; char * offsetToStringData = (char *)(&str1); if (&str1 != null) { offsetToStringData = (char *)(strPointers + RuntimeHelpers.OffsetToStringData); } char *chrPointer = offsetToStringData; fixed(string str2 = strArrays[1]) { string *strPointers1 = &str2; char * offsetToStringData1 = (char *)(&str2); if (&str2 != null) { offsetToStringData1 = (char *)(strPointers1 + RuntimeHelpers.OffsetToStringData); } char *chrPointer1 = offsetToStringData1; fixed(string str3 = strArrays[2]) { string *strPointers2 = &str3; char * offsetToStringData2 = (char *)(&str3); if (&str3 != null) { offsetToStringData2 = (char *)(strPointers2 + RuntimeHelpers.OffsetToStringData); } char *chrPointer2 = offsetToStringData2; fixed(string str4 = strArrays[3]) { string *strPointers3 = &str4; char * offsetToStringData3 = (char *)(&str4); if (&str4 != null) { offsetToStringData3 = (char *)(strPointers3 + RuntimeHelpers.OffsetToStringData); } char *chrPointer3 = offsetToStringData3; fixed(string str5 = strArrays[4]) { string *strPointers4 = &str5; char * offsetToStringData4 = (char *)(&str5); if (&str5 != null) { offsetToStringData4 = (char *)(strPointers4 + RuntimeHelpers.OffsetToStringData); } char *chrPointer4 = offsetToStringData4; fixed(string str6 = strArrays[5]) { string *strPointers5 = &str6; char * offsetToStringData5 = (char *)(&str6); if (&str6 != null) { offsetToStringData5 = (char *)(strPointers5 + RuntimeHelpers.OffsetToStringData); } char *chrPointer5 = offsetToStringData5; fixed(string str7 = strArrays[6]) { string *strPointers6 = &str7; char * offsetToStringData6 = (char *)(&str7); if (&str7 != null) { offsetToStringData6 = (char *)(strPointers6 + RuntimeHelpers.OffsetToStringData); } char *chrPointer6 = offsetToStringData6; fixed(string str8 = strArrays[7]) { string *strPointers7 = &str8; char * offsetToStringData7 = (char *)(&str8); if (&str8 != null) { offsetToStringData7 = (char *)(strPointers7 + RuntimeHelpers.OffsetToStringData); } char *chrPointer7 = offsetToStringData7; eventDataPointer1 = eventDataPointer; if (strArrays[0] != null) { (eventDataPointer1 + numArray[0] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer; } if (strArrays[1] != null) { (eventDataPointer1 + numArray[1] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer1; } if (strArrays[2] != null) { (eventDataPointer1 + numArray[2] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer2; } if (strArrays[3] != null) { (eventDataPointer1 + numArray[3] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer3; } if (strArrays[4] != null) { (eventDataPointer1 + numArray[4] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer4; } if (strArrays[5] != null) { (eventDataPointer1 + numArray[5] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer5; } if (strArrays[6] != null) { (eventDataPointer1 + numArray[6] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer6; } if (strArrays[7] != null) { (eventDataPointer1 + numArray[7] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer7; } num = UnsafeNativeMethods.EventWriteTransfer(this.traceRegistrationHandle, ref eventDescriptor, ref eventTraceActivity.ActivityId, ref relatedActivityId, length, eventDataPointer); } } } } } } } } } else { DiagnosticsEventProvider.errorCode = DiagnosticsEventProvider.WriteEventErrorCode.EventTooBig; return(false); } } else { throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload", InternalSR.EtwMaxNumberArgumentsExceeded(32))); } } } if (num == 0) { return(true); } else { DiagnosticsEventProvider.SetLastError(num); return(false); } }
public InternalException(string description) : base(InternalSR.ShipAssertExceptionMessage(description)) { }
public bool WriteTransferEvent(ref EventDescriptor eventDescriptor, EventTraceActivity eventTraceActivity, Guid relatedActivityId, params object[] eventPayload) { // ActivityId is required when writing transfer event if (eventTraceActivity == null) { Fx.Assert(false, "eventTraceActivity should not be null for WriteTransferEvent"); eventTraceActivity = EventTraceActivity.Empty; } uint status = 0; if (IsEnabled(eventDescriptor.Level, eventDescriptor.Keywords)) { unsafe { if ((eventPayload != null) && (eventPayload.Length != 0)) { int argCount = eventPayload.Length; if (argCount > etwMaxNumberArguments) { // //too many arguments to log // throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload", InternalSR.EtwMaxNumberArgumentsExceeded(etwMaxNumberArguments))); } uint totalEventSize = 0; int index; int stringIndex = 0; int[] stringPosition = new int[etwAPIMaxStringCount]; //used to keep the position of strings in the eventPayload parameter string[] dataString = new string[etwAPIMaxStringCount]; // string arrays from the eventPayload parameter UnsafeNativeMethods.EventData *userData = stackalloc UnsafeNativeMethods.EventData[argCount]; // allocation for the data descriptors UnsafeNativeMethods.EventData *userDataPtr = (UnsafeNativeMethods.EventData *)userData; byte *dataBuffer = stackalloc byte[basicTypeAllocationBufferSize * argCount]; // 16 byte for unboxing non-string argument byte *currentBuffer = dataBuffer; // // The loop below goes through all the arguments and fills in the data // descriptors. For strings save the location in the dataString array. // Caculates the total size of the event by adding the data descriptor // size value set in EncodeObjec method. // for (index = 0; index < eventPayload.Length; index++) { if (eventPayload[index] != null) { string isString; isString = EncodeObject(ref eventPayload[index], userDataPtr, currentBuffer); currentBuffer += basicTypeAllocationBufferSize; totalEventSize += userDataPtr->Size; userDataPtr++; if (isString != null) { if (stringIndex < etwAPIMaxStringCount) { dataString[stringIndex] = isString; stringPosition[stringIndex] = index; stringIndex++; } else { throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload", InternalSR.EtwAPIMaxStringCountExceeded(etwAPIMaxStringCount))); } } } } if (totalEventSize > traceEventMaximumSize) { errorCode = WriteEventErrorCode.EventTooBig; return(false); } fixed(char *v0 = dataString[0], v1 = dataString[1], v2 = dataString[2], v3 = dataString[3], v4 = dataString[4], v5 = dataString[5], v6 = dataString[6], v7 = dataString[7]) { userDataPtr = (UnsafeNativeMethods.EventData *)userData; if (dataString[0] != null) { userDataPtr[stringPosition[0]].DataPointer = (ulong)v0; } if (dataString[1] != null) { userDataPtr[stringPosition[1]].DataPointer = (ulong)v1; } if (dataString[2] != null) { userDataPtr[stringPosition[2]].DataPointer = (ulong)v2; } if (dataString[3] != null) { userDataPtr[stringPosition[3]].DataPointer = (ulong)v3; } if (dataString[4] != null) { userDataPtr[stringPosition[4]].DataPointer = (ulong)v4; } if (dataString[5] != null) { userDataPtr[stringPosition[5]].DataPointer = (ulong)v5; } if (dataString[6] != null) { userDataPtr[stringPosition[6]].DataPointer = (ulong)v6; } if (dataString[7] != null) { userDataPtr[stringPosition[7]].DataPointer = (ulong)v7; } status = UnsafeNativeMethods.EventWriteTransfer(this.traceRegistrationHandle, ref eventDescriptor, ref eventTraceActivity.ActivityId, ref relatedActivityId, (uint)argCount, userData); } } else { status = UnsafeNativeMethods.EventWriteTransfer(this.traceRegistrationHandle, ref eventDescriptor, ref eventTraceActivity.ActivityId, ref relatedActivityId, 0, null); } } } if (status != 0) { SetLastError((int)status); return(false); } return(true); }
public bool WriteEvent(ref EventDescriptor eventDescriptor, EventTraceActivity eventTraceActivity, params object[] eventPayload) { uint status = 0; if (IsEnabled(eventDescriptor.Level, eventDescriptor.Keywords)) { int argCount = 0; if (eventTraceActivity != null) { SetActivityId(ref eventTraceActivity.ActivityId); } unsafe { if ((eventPayload == null) || (eventPayload.Length == 0) || (eventPayload.Length == 1)) { string dataString = null; UnsafeNativeMethods.EventData userData; byte *dataBuffer = stackalloc byte[basicTypeAllocationBufferSize]; // Assume a max of 16 chars for non-string argument userData.Size = 0; if ((eventPayload != null) && (eventPayload.Length != 0)) { // // Figure out the type and fill the data descriptor // dataString = EncodeObject(ref eventPayload[0], &userData, dataBuffer); argCount = 1; } if (userData.Size > traceEventMaximumSize) { // // Maximum size of the event payload plus header is 64k // errorCode = WriteEventErrorCode.EventTooBig; return(false); } if (dataString != null) { fixed(char *pdata = dataString) { userData.DataPointer = (ulong)pdata; status = UnsafeNativeMethods.EventWrite(this.traceRegistrationHandle, ref eventDescriptor, (uint)argCount, &userData); } } else { if (argCount == 0) { status = UnsafeNativeMethods.EventWrite(this.traceRegistrationHandle, ref eventDescriptor, 0, null); } else { status = UnsafeNativeMethods.EventWrite(this.traceRegistrationHandle, ref eventDescriptor, (uint)argCount, &userData); } } } else { argCount = eventPayload.Length; if (argCount > etwMaxNumberArguments) { // //too many arguments to log // throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload", InternalSR.EtwMaxNumberArgumentsExceeded(etwMaxNumberArguments))); } uint totalEventSize = 0; int index; int stringIndex = 0; int[] stringPosition = new int[etwAPIMaxStringCount]; string[] dataString = new string[etwAPIMaxStringCount]; UnsafeNativeMethods.EventData *userData = stackalloc UnsafeNativeMethods.EventData[argCount]; UnsafeNativeMethods.EventData *userDataPtr = (UnsafeNativeMethods.EventData *)userData; byte *dataBuffer = stackalloc byte[basicTypeAllocationBufferSize * argCount]; // Assume 16 chars for non-string argument byte *currentBuffer = dataBuffer; // // The loop below goes through all the arguments and fills in the data // descriptors. For strings save the location in the dataString array. // Caculates the total size of the event by adding the data descriptor // size value set in EncodeObjec method. // for (index = 0; index < eventPayload.Length; index++) { if (eventPayload[index] != null) { string isString; isString = EncodeObject(ref eventPayload[index], userDataPtr, currentBuffer); currentBuffer += basicTypeAllocationBufferSize; totalEventSize += userDataPtr->Size; userDataPtr++; if (isString != null) { if (stringIndex < etwAPIMaxStringCount) { dataString[stringIndex] = isString; stringPosition[stringIndex] = index; stringIndex++; } else { throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload", InternalSR.EtwAPIMaxStringCountExceeded(etwAPIMaxStringCount))); } } } } if (totalEventSize > traceEventMaximumSize) { errorCode = WriteEventErrorCode.EventTooBig; return(false); } // // now fix any string arguments and set the pointer on the data descriptor // fixed(char *v0 = dataString[0], v1 = dataString[1], v2 = dataString[2], v3 = dataString[3], v4 = dataString[4], v5 = dataString[5], v6 = dataString[6], v7 = dataString[7]) { userDataPtr = (UnsafeNativeMethods.EventData *)userData; if (dataString[0] != null) { userDataPtr[stringPosition[0]].DataPointer = (ulong)v0; } if (dataString[1] != null) { userDataPtr[stringPosition[1]].DataPointer = (ulong)v1; } if (dataString[2] != null) { userDataPtr[stringPosition[2]].DataPointer = (ulong)v2; } if (dataString[3] != null) { userDataPtr[stringPosition[3]].DataPointer = (ulong)v3; } if (dataString[4] != null) { userDataPtr[stringPosition[4]].DataPointer = (ulong)v4; } if (dataString[5] != null) { userDataPtr[stringPosition[5]].DataPointer = (ulong)v5; } if (dataString[6] != null) { userDataPtr[stringPosition[6]].DataPointer = (ulong)v6; } if (dataString[7] != null) { userDataPtr[stringPosition[7]].DataPointer = (ulong)v7; } status = UnsafeNativeMethods.EventWrite(this.traceRegistrationHandle, ref eventDescriptor, (uint)argCount, userData); } } } } if (status != 0) { SetLastError((int)status); return(false); } return(true); }
protected static void ThrowInvalidAsyncResult(IAsyncResult result) { throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.InvalidAsyncResultImplementation(result.GetType()))); }
protected void Complete(bool completedSynchronously) { if (_isCompleted) { throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.AsyncResultCompletedTwice(GetType()))); } //#if DEBUG // this.marker.AsyncResult = null; // this.marker = null; // if (!Fx.FastDebug && completeStack == null) // { // completeStack = new StackTrace(); // } //#endif _completedSynchronously = completedSynchronously; if (OnCompleting != null) { // Allow exception replacement, like a catch/throw pattern. try { OnCompleting(this, _exception); } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } _exception = exception; } } if (completedSynchronously) { // If we completedSynchronously, then there's no chance that the manualResetEvent was created so // we don't need to worry about a race condition Fx.Assert(_manualResetEvent == null, "No ManualResetEvent should be created for a synchronous AsyncResult."); _isCompleted = true; } else { lock (ThisLock) { _isCompleted = true; if (_manualResetEvent != null) { _manualResetEvent.Set(); } } } if (_callback != null) { try { if (VirtualCallback != null) { VirtualCallback(_callback, this); } else { _callback(this); } } #pragma warning disable 1634 #pragma warning suppress 56500 // transferring exception to another thread catch (Exception e) { if (Fx.IsFatal(e)) { throw; } throw Fx.Exception.AsError(new CallbackException(InternalSR.AsyncCallbackThrewException, e)); } #pragma warning restore 1634 } }