コード例 #1
0
        protected InstrumentationHelper _Save(InstrumentationPoint ip)
        {
            instrumentationAPI.updateAssembly(ip);
            instrumentationAPI.writeAssembly(ip);

            return(this);
        }
コード例 #2
0
        public void writeAssembly(InstrumentationPoint ip)
        {
            AssemblyDefinition assemblyToWrite = mapAssemblyNamesToDefinitions[ip.AssemblyName];
            string             writePath       = mapAssemblyNamesToWritePaths[ip.AssemblyName];

            assemblyToWrite.Write(writePath);
        }
コード例 #3
0
        public InstrumentationHelper UntilExit(InstrumentationPoint ip)
        {
            List <Instruction> closingInstructions = InstrumentationHelperClosingInstructions(ip);

            InstrumentationPositionInMethodHelper.WeaveInstructionsAtMethodExit(ip.instrumentationPointMethodDefinition, closingInstructions);
            return(_Save(ip));
        }
コード例 #4
0
        protected override List <Instruction> InstrumentationHelperOpeningInstructions(
            InstrumentationPoint ip
            )
        {
            List <Instruction> weaveOpeningInstructions = new List <Instruction>();

            ip.instrumentationPointMethodDefinition.Body.SimplifyMacros();
            ILProcessor ilp = ip.instrumentationPointMethodDefinition.Body.GetILProcessor();

            // Call default remote test wrapper handler
            Instruction loadRemoteTestDriverInstance =
                ilp.Create(OpCodes.Call,
                           ip.instrumentationPointMethodDefinition.Module.Import(
                               typeof(StandaloneInstrumentationMessageHandler).GetMethod("get_Instance", new Type[] { })));

            Instruction callInstanceBootstrap =
                ilp.Create(OpCodes.Callvirt,
                           ip.instrumentationPointMethodDefinition.Module.Import(
                               typeof(StandaloneInstrumentationMessageHandler).GetMethod(
                                   "Bootstrap", new Type[] { })));

            ip.instrumentationPointMethodDefinition.Body.OptimizeMacros();

            weaveOpeningInstructions.Add(loadRemoteTestDriverInstance);
            weaveOpeningInstructions.Add(callInstanceBootstrap);

            return(weaveOpeningInstructions);
        }
コード例 #5
0
        protected override List <Instruction> InstrumentationHelperOpeningInstructions(
            InstrumentationPoint ip
            )
        {
            // TODO Make sure to error check for valid stopwatch field definition
            List <Instruction> weaveOpeningInstructions = new List <Instruction>();

            ILProcessor ilp = ip.instrumentationPointMethodDefinition.Body.GetILProcessor();

            ip.instrumentationPointMethodDefinition.Body.SimplifyMacros();

            Instruction loadThis = ilp.Create(OpCodes.Ldarg_0);
            Instruction callStopwatchStartNew = ilp.Create(
                OpCodes.Call,
                ip.instrumentationPointMethodDefinition.Module.Import(
                    typeof(Stopwatch).GetMethod("StartNew", new Type[] { })));
            Instruction storeStopwatchInFieldDef = ilp.Create(
                OpCodes.Stfld,
                mStopwatchDefinition
                );

            ip.instrumentationPointMethodDefinition.Body.OptimizeMacros();

            weaveOpeningInstructions.Add(loadThis);
            weaveOpeningInstructions.Add(callStopwatchStartNew);
            weaveOpeningInstructions.Add(storeStopwatchInFieldDef);

            return(weaveOpeningInstructions);
        }
コード例 #6
0
        public InstrumentationHelper StartingAtExit(InstrumentationPoint ip)
        {
            InstrumentationHelperInitialization(ip);
            List <Instruction> openingInstructions = InstrumentationHelperOpeningInstructions(ip);

            InstrumentationPositionInMethodHelper.WeaveInstructionsAtMethodExit(ip.instrumentationPointMethodDefinition, openingInstructions);
            return(_Save(ip));
        }
コード例 #7
0
        protected override void InstrumentationHelperInitialization(
            InstrumentationPoint ip
            )
        {
            // TODO --- need a more graceful way to name these
            mSnapshotFieldDefinition =
                new FieldDefinition(
                    "snapshot_" + ip.Name,
                    FieldAttributes.Public,
                    ip.instrumentationPointTypeDefinition.Module.Import(typeof(WovenSnapshot))
                    );

            ip.instrumentationPointTypeDefinition.Fields.Add(mSnapshotFieldDefinition);

            // TODO might want a special case of this for constructors, static classes/constructors get messy
            List <Instruction> wovenFieldInitializationInstructions = new List <Instruction>();
            MethodDefinition   ipParentTypeConstructor = ip.instrumentationPointTypeDefinition.Methods.Single(m => m.Name == ".ctor");
            FieldDefinition    snapshotTargetField     =
                ip.instrumentationPointTypeDefinition.Fields
                .Single(f => f.Name == snapshotFieldName);
            ILProcessor ctorIlp = ipParentTypeConstructor.Body.GetILProcessor();

            // Load self
            wovenFieldInitializationInstructions.Add(
                ctorIlp.Create(OpCodes.Ldarg_0)
                );

            // Load arguments to WovenSnapshot constructor: IP Name
            wovenFieldInitializationInstructions.Add(
                ctorIlp.Create(OpCodes.Ldstr, ip.Name)
                );

            // Load arguments to WovenSnapshot constructor: Target field
            wovenFieldInitializationInstructions.Add(
                ctorIlp.Create(OpCodes.Ldarg_0)
                );
            wovenFieldInitializationInstructions.Add(
                ctorIlp.Create(OpCodes.Ldfld, snapshotTargetField)
                );
            wovenFieldInitializationInstructions.Add(
                ctorIlp.Create(OpCodes.Box, snapshotTargetField.FieldType)
                );

            // Create with constructor call and store into field
            wovenFieldInitializationInstructions.Add(
                ctorIlp.Create(
                    OpCodes.Newobj,
                    ipParentTypeConstructor.Module.Import(
                        typeof(WovenSnapshot).GetConstructor(new Type[] { typeof(string), typeof(object) }))
                    )
                );

            wovenFieldInitializationInstructions.Add(
                ctorIlp.Create(OpCodes.Stfld, mSnapshotFieldDefinition)
                );

            InstrumentationPositionInMethodHelper.WeaveInstructionsAtMethodExit(ipParentTypeConstructor, wovenFieldInitializationInstructions);
        }
コード例 #8
0
        protected override List <Instruction> InstrumentationHelperClosingInstructions(
            InstrumentationPoint ip
            )
        {
            // TODO Make sure to error check for valid stopwatch field definition
            List <Instruction> weaveClosingInstructions = new List <Instruction>();

            ILProcessor ilp = ip.instrumentationPointMethodDefinition.Body.GetILProcessor();

            ip.instrumentationPointMethodDefinition.Body.SimplifyMacros();

            // Stop stopwatch
            Instruction loadThis           = ilp.Create(OpCodes.Ldarg_0);
            Instruction loadStopwatchField = ilp.Create(OpCodes.Ldfld, mStopwatchDefinition);
            Instruction callStop           =
                ilp.Create(OpCodes.Callvirt,
                           ip.instrumentationPointMethodDefinition.Module.Import(
                               typeof(Stopwatch).GetMethod("Stop", new Type[] { })));

            // Load stopwatch, and ip id again
            Instruction loadStopwatchForCapture = ilp.Create(OpCodes.Ldarg_0);
            Instruction reloadStopwatchField    = ilp.Create(OpCodes.Ldfld, mStopwatchDefinition);

            // Load instrumentation point id
            Instruction loadIpName = ilp.Create(OpCodes.Ldstr, ip.Name);

            // Load and call wrapper
            Instruction LoadRemoteTestDriverInstance =
                ilp.Create(OpCodes.Call,
                           ip.instrumentationPointMethodDefinition.Module.Import(
                               typeof(StandaloneInstrumentationMessageHandler).GetMethod("get_Instance", new Type[] { })));

            Instruction callCaptureIpContents =
                ilp.Create(OpCodes.Callvirt,
                           ip.instrumentationPointMethodDefinition.Module.Import(
                               typeof(StandaloneInstrumentationMessageHandler).GetMethod(
                                   "CaptureStopwatchEndPoint",
                                   new Type[] { typeof(Stopwatch), typeof(string) })));

            ip.instrumentationPointMethodDefinition.Body.OptimizeMacros();

            weaveClosingInstructions.Add(loadThis);
            weaveClosingInstructions.Add(loadStopwatchField);
            weaveClosingInstructions.Add(callStop);

            weaveClosingInstructions.Add(LoadRemoteTestDriverInstance);
            weaveClosingInstructions.Add(loadStopwatchForCapture);
            weaveClosingInstructions.Add(reloadStopwatchField);
            weaveClosingInstructions.Add(loadIpName);
            weaveClosingInstructions.Add(callCaptureIpContents);

            return(weaveClosingInstructions);
        }
コード例 #9
0
        public void updateAssembly(InstrumentationPoint ip)
        {
            mapAssemblyNamesToDefinitions[ip.AssemblyName] = ip.instrumentationPointAssemblyDefinition;

            // Add bootstrapper code to get around the async deadlock bug

            /*
             * InstrumentationPoint ctor =
             *  new InstrumentationPoint("ctor", this)
             *      .FindInAssemblyNamed(ip.instrumentationPointAssemblyDefinition.Name.Name)
             *      .FindInTypeNamed(ip.instrumentationPointTypeDefinition.Name)
             *      .FindMethodNamed(".ctor");
             * bootstrapHelper.StartingAtEntry(ctor);*/
        }
コード例 #10
0
        public void EnableBootstrap()
        {
            BootstrapBrokerInstanceHelper bootstrapHelper = new BootstrapBrokerInstanceHelper(this);
            InstrumentationAPI            iapi            = this;

            foreach (InstrumentationPoint i in mapInstrumentationPointNamesToSpecifications.Values)
            {
                InstrumentationPoint ctor =
                    new InstrumentationPoint("ctor", iapi)
                    .FindInAssemblyNamed(i.instrumentationPointAssemblyDefinition.Name.Name)
                    .FindInTypeNamed(i.instrumentationPointTypeDefinition.Name)
                    .FindMethodNamed(".ctor");
                bootstrapHelper.StartingAtEntry(ctor);
            }
        }
コード例 #11
0
        protected override void InstrumentationHelperInitialization(
            InstrumentationPoint ip
            )
        {
            // TODO handle case of multiple stopwatch, need a naming policy
            string fieldName = "stopwatch_" + ip.Name;

            mStopwatchDefinition =
                new FieldDefinition(
                    fieldName,
                    FieldAttributes.Private,
                    ip.instrumentationPointTypeDefinition.Module.Import(typeof(Stopwatch))
                    );

            ip.instrumentationPointTypeDefinition.Fields.Add(mStopwatchDefinition);
        }
コード例 #12
0
        protected override List <Instruction> InstrumentationHelperOpeningInstructions(
            InstrumentationPoint ip
            )
        {
            List <Instruction> weaveOpeningInstructions = new List <Instruction>();

            ILProcessor ilp = ip.instrumentationPointMethodDefinition.Body.GetILProcessor();

            ip.instrumentationPointMethodDefinition.Body.SimplifyMacros();

            // Load value of interest and box as object
            FieldDefinition snapshotFieldDefinition =
                ip.instrumentationPointTypeDefinition.Fields
                .Single(f => f.Name == snapshotFieldName);
            Instruction loadThis          = ilp.Create(OpCodes.Ldarg_0);
            Instruction loadSnapshotValue =
                ilp.Create(OpCodes.Ldfld, snapshotFieldDefinition);
            Instruction boxSnapshotField =
                ilp.Create(OpCodes.Box, snapshotFieldDefinition.FieldType);

            // Load IP name
            Instruction loadIpId = ilp.Create(OpCodes.Ldstr, ip.Name);

            // Call default remote test wrapper handler
            Instruction loadRemoteTestDriverInstance =
                ilp.Create(OpCodes.Call,
                           ip.instrumentationPointMethodDefinition.Module.Import(
                               typeof(StandaloneInstrumentationMessageHandler).GetMethod("get_Instance", new Type[] { })));

            Instruction callCaptureIpContents =
                ilp.Create(OpCodes.Callvirt,
                           ip.instrumentationPointMethodDefinition.Module.Import(
                               typeof(StandaloneInstrumentationMessageHandler).GetMethod(
                                   "CaptureInstrumentationPoint",
                                   new Type[] { typeof(object), typeof(string) })));

            ip.instrumentationPointMethodDefinition.Body.OptimizeMacros();

            weaveOpeningInstructions.Add(loadRemoteTestDriverInstance);
            weaveOpeningInstructions.Add(loadThis);
            weaveOpeningInstructions.Add(loadSnapshotValue);
            weaveOpeningInstructions.Add(boxSnapshotField);
            weaveOpeningInstructions.Add(loadIpId);
            weaveOpeningInstructions.Add(callCaptureIpContents);

            return(weaveOpeningInstructions);
        }
コード例 #13
0
        // TODO make something more elegant than just passing a list
        // TODO improved error checking
        public static void WeaveInstructionsAtMethodEntry(
            InstrumentationPoint ip,
            List <Instruction> instructionsToWeave
            )
        {
            if (instructionsToWeave.Count == 0)
            {
                return;
            }

            ILProcessor instructionProcessor = ip.instrumentationPointMethodDefinition.Body.GetILProcessor();

            ip.instrumentationPointMethodDefinition.Body.SimplifyMacros();
            Instruction originalFirstInstruction = ip.instrumentationPointMethodDefinition.Body.Instructions.First();

            foreach (Instruction weaveInstruction in instructionsToWeave)
            {
                instructionProcessor.InsertBefore(originalFirstInstruction, weaveInstruction);
            }

            ip.instrumentationPointMethodDefinition.Body.OptimizeMacros();
        }
コード例 #14
0
        public object CaptureValueBlocking(InstrumentationPoint ip)
        {
            using (UdpClient client = new UdpClient(0)) {
                IPEndPoint responseEndPoint = new IPEndPoint(IPAddress.Any, 0);
                InstrumentationPointExchangeMessage requestContents = new InstrumentationPointExchangeMessage {
                    instrumentationPointName = ip.Name,
                    instrumentationPointType = ip.GetType().ToString(),
                    value = ""
                };
                byte[] requestData =
                    Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(
                                               requestContents, Formatting.Indented));

                // This will block
                client.Send(requestData, requestData.Length, "127.0.0.1", RemoteTestingWrapper.RemoteConfigurations.RemoteListenerPort);
                byte[] encoded = client.Receive(ref responseEndPoint);
                InstrumentationPointExchangeMessage response =
                    JsonConvert.DeserializeObject <InstrumentationPointExchangeMessage>(
                        Encoding.UTF8.GetString(encoded));

                return(response.value);
            }
        }
コード例 #15
0
        protected override List <Instruction> InstrumentationHelperOpeningInstructions(
            InstrumentationPoint ip
            )
        {
            List <Instruction> weaveOpeningInstructions = new List <Instruction>();

            ILProcessor ilp = ip.instrumentationPointMethodDefinition.Body.GetILProcessor();

            ip.instrumentationPointMethodDefinition.Body.SimplifyMacros();

            Instruction loadIntSleepQuantity = ilp.Create(OpCodes.Ldc_I4, nSecondsSleep * 1000);
            Instruction loadCallThreadSleep  =
                ilp.Create(
                    OpCodes.Call,
                    ip.instrumentationPointMethodDefinition.Module.Import(
                        typeof(System.Threading.Thread).GetMethod("Sleep", new Type[] { typeof(int) })));

            ip.instrumentationPointMethodDefinition.Body.OptimizeMacros();

            weaveOpeningInstructions.Add(loadIntSleepQuantity);
            weaveOpeningInstructions.Add(loadCallThreadSleep);

            return(weaveOpeningInstructions);
        }
コード例 #16
0
 protected virtual List <Instruction> InstrumentationHelperClosingInstructions(InstrumentationPoint ip)
 {
     return(new List <Instruction>());
 }
コード例 #17
0
 protected abstract List <Instruction> InstrumentationHelperOpeningInstructions(InstrumentationPoint ip);
コード例 #18
0
 protected virtual void InstrumentationHelperInitialization(InstrumentationPoint ip)
 {
 }