Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 7 additions & 83 deletions src/System.Management.Automation/engine/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
using System.Reflection.Emit;
using System.Threading;

#if !CORECLR
// StackFrame and SymbolStore related types are not available in CoreCLR.
using System.Diagnostics.SymbolStore;
using System.Diagnostics;
#endif

namespace System.Management.Automation
{
/// <summary>
Expand Down Expand Up @@ -359,9 +353,6 @@ internal PSLocalEventManager(ExecutionContext context)
private AssemblyBuilder _eventAssembly = null;
private ModuleBuilder _eventModule = null;
private int _typeId = 0;
#if !CORECLR
private bool debugMode = false;
#endif

/// <summary>
/// Gets the list of event subscribers.
Expand Down Expand Up @@ -673,22 +664,10 @@ private void ProcessNewSubscriber(PSEventSubscriber subscriber, Object source, s

if (_eventAssembly == null)
{
#if !CORECLR
// Define the assembly that will hold our event handlers
StackFrame callStack = new StackFrame(0, true);
debugMode = (callStack.GetFileName() != null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: callstack.GetFileName() return null if the corresponding .pdb file is not put beside the .dll file. So this is actually to see if we are currently debugging the .dll.

#endif
_eventAssembly = AssemblyBuilder.DefineDynamicAssembly(
new AssemblyName("PSEventHandler"),
AssemblyBuilderAccess.Run);
}
if (_eventModule == null)
{
#if CORECLR
_eventModule = _eventAssembly.DefineDynamicModule("PSGenericEventModule");
#else
_eventModule = _eventAssembly.DefineDynamicModule("PSGenericEventModule", debugMode);
Copy link
Member Author

@daxian-dbw daxian-dbw Apr 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: DefineDynamicModule doesn't have this overload in .NET Core.

#endif
}

string engineEventSourceIdentifier = null;
Expand Down Expand Up @@ -748,14 +727,7 @@ private void ProcessNewSubscriber(PSEventSubscriber subscriber, Object source, s
}
}
}
#if !CORECLR
// If it is a ManagementEventWatcher, enable it
ManagementEventWatcher eventWatcher = source as ManagementEventWatcher;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: WMIv1 (System.Management) is not available in .NET Core by default.

if (eventWatcher != null)
{
eventWatcher.Start();
}
#endif

// Get its invoke method, and register ourselves as a handler
MethodInfo invokeMethod = eventInfo.EventHandlerType.GetMethod("Invoke");

Expand All @@ -767,7 +739,6 @@ private void ProcessNewSubscriber(PSEventSubscriber subscriber, Object source, s
if (invokeMethod.ReturnType != typeof(void))
{
string errorMessage = EventingResources.NonVoidDelegateNotSupported;

throw new ArgumentException(errorMessage, "eventName");
}

Expand Down Expand Up @@ -1423,16 +1394,7 @@ private IEnumerable<PSEventSubscriber> GetEventSubscribers(string sourceIdentifi
private Type GenerateEventHandler(MethodInfo invokeSignature)
{
int parameterCount = invokeSignature.GetParameters().Length;
#if !CORECLR
StackFrame callStack = new StackFrame(0, true);

// Get the filename to associate with the debug symbols
ISymbolDocumentWriter doc = null;
if (debugMode)
{
doc = _eventModule.DefineDocument(callStack.GetFileName(), Guid.Empty, Guid.Empty, Guid.Empty);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: API does not exist in .NET Core.

}
#endif
// Define the type that will respond to the event. It
// derives from PSEventHandler so that complex
// functionality can go into its base class.
Expand All @@ -1445,21 +1407,6 @@ private Type GenerateEventHandler(MethodInfo invokeSignature)
typeof(PSEventHandler).GetConstructor(
new Type[] { typeof(PSEventManager), typeof(Object), typeof(string), typeof(PSObject) });

#if !CORECLR
if (debugMode)
{
// Mark as debuggable
Type debugAttributeType = typeof(DebuggableAttribute);
ConstructorInfo debugAttributeCtor = debugAttributeType.GetConstructor(new Type[] { typeof(DebuggableAttribute.DebuggingModes) });

CustomAttributeBuilder debugAttributeBuilder = new CustomAttributeBuilder(debugAttributeCtor,
new object[] {
DebuggableAttribute.DebuggingModes.DisableOptimizations |
DebuggableAttribute.DebuggingModes.Default
});
_eventAssembly.SetCustomAttribute(debugAttributeBuilder);
}
#endif
// Define the new constructor
// public TestEventHandler(PSEventManager eventManager, Object sender, string sourceIdentifier, PSObject extraData)
// : base(eventManager, sender, sourceIdentifier, extraData)
Expand Down Expand Up @@ -1499,35 +1446,19 @@ private Type GenerateEventHandler(MethodInfo invokeSignature)

ILGenerator methodContents = eventMethod.GetILGenerator();

// Object[] args =
LocalBuilder argsBuilder = methodContents.DeclareLocal(typeof(object[]));

#if !CORECLR
if (debugMode)
{
argsBuilder.SetLocalSymInfo("args");
// Declare a local variable of the type 'object[]' at index 0, say 'object[] args'
methodContents.DeclareLocal(typeof(object[]));

// new Object[ invokeSignature.GetParameters().Length ]
methodContents.MarkSequencePoint(doc, callStack.GetFileLineNumber() - 1, 1, callStack.GetFileLineNumber(), 100);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: API doesn't exist in .NET Core.

}
#endif
methodContents.Emit(OpCodes.Ldc_I4, parameterCount);
methodContents.Emit(OpCodes.Newarr, typeof(Object));

// Retrieve the args variable from local variable index 0
// Store the new array to the local variable 'args'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this an error in the previous comment?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see -- never mind

methodContents.Emit(OpCodes.Stloc_0);

// Inline, this converts into a series of setting args[n] to
// the argument at the same parameter index
for (int counter = 1; counter <= parameterCount; counter++)
{
#if !CORECLR
if (debugMode)
{
// args[n] = argument[n]
methodContents.MarkSequencePoint(doc, callStack.GetFileLineNumber() - 1, 1, callStack.GetFileLineNumber(), 100);
}
#endif
methodContents.Emit(OpCodes.Ldloc_0);
methodContents.Emit(OpCodes.Ldc_I4, counter - 1);
methodContents.Emit(OpCodes.Ldarg, counter);
Expand Down Expand Up @@ -1572,16 +1503,9 @@ private Type GenerateEventHandler(MethodInfo invokeSignature)

// Finally, invoke the method
MethodInfo generateEventMethod = typeof(PSEventManager).GetMethod(
"GenerateEvent",
new Type[] { typeof(string), typeof(object), typeof(object[]), typeof(PSObject) }
);
#if !CORECLR
if (debugMode)
{
// GenerateEvent(sourceIdentifier, args, extraData);
methodContents.MarkSequencePoint(doc, callStack.GetFileLineNumber() - 1, 1, callStack.GetFileLineNumber(), 100);
}
#endif
nameof(PSEventManager.GenerateEvent),
new Type[] { typeof(string), typeof(object), typeof(object[]), typeof(PSObject) });

methodContents.Emit(OpCodes.Callvirt, generateEventMethod);

// Discard the return value, and return
Expand Down