Exemple #1
0
        /// <summary>
        /// This is the Code Pulse .NET Tracer application.
        /// </summary>
        /// <param name="args">Application arguments - see usage statement</param>
        /// <returns>Return code adjusted by optional offset.</returns>
        private static int Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;

            try
            {
                if (!ParseCommandLine(args, out var parser))
                {
                    return(ProgramExitCodes.CannotParseCommandLine);
                }
                _returnCodeOffset = parser.ReturnCodeOffset;

                LogManager.GetRepository().Threshold = parser.LogLevel;

                if (!DoesUserHaveReadAndExecuteAccess(parser.Profiler32Path, parser.ExpectedOwnerOfApplicationUnderTest, out var new32BitAccessRule))
                {
                    LogMandatoryFatal($"The application cannot start because expected owner of the application under test ({parser.ExpectedOwnerOfApplicationUnderTest}) does not have read and execute permissions for the 32-bit profiler library ({parser.Profiler32Path}).");
                    return(MakeExitCode(ProgramExitCodes.ProfilerNoReadPermission));
                }

                try
                {
                    FileSystemAccessRule new64BitAccessRule = null;
                    if (Environment.Is64BitOperatingSystem && !DoesUserHaveReadAndExecuteAccess(parser.Profiler64Path, parser.ExpectedOwnerOfApplicationUnderTest, out new64BitAccessRule))
                    {
                        LogMandatoryFatal($"The application cannot start because expected owner of the application under test ({parser.ExpectedOwnerOfApplicationUnderTest}) does not have read and execute permissions for the 64-bit profiler library ({parser.Profiler64Path}).");
                        return(MakeExitCode(ProgramExitCodes.ProfilerNoReadPermission));
                    }

                    try
                    {
                        Logger.Debug($"32-bit Profiler Path: {parser.Profiler32Path}");
                        if (Environment.Is64BitOperatingSystem)
                        {
                            Logger.Debug($"64-bit Profiler Path: {parser.Profiler64Path}");
                        }
                        Logger.Debug($"Expected owner of application under test: {parser.ExpectedOwnerOfApplicationUnderTest}");

                        Logger.Info("Starting...");

                        var filter      = BuildFilter(parser);
                        var perfCounter = CreatePerformanceCounter(parser);

                        using (var container = new Bootstrapper(Logger))
                        {
                            Logger.Info("Connecting to Code Pulse...");
                            LogMandatoryInfo("Open Code Pulse, select a project, wait for the connection, and start a trace.");

                            _persistence = new CodePulsePersistence(parser, Logger);
                            container.Initialise(filter, parser, _persistence, perfCounter);
                            if (!_persistence.Initialize(new StaticAgentConfiguration(parser.CodePulsePort, parser.CodePulseHost, parser.CodePulseConnectTimeout, Logger)))
                            {
                                LogMandatoryFatal("Failed to initialize Code Pulse connection. Is Code Pulse running?");
                                return(MakeExitCode(ProgramExitCodes.CannotInitializeCodePulseConnection));
                            }

                            var returnCode = RunWithContainer(parser, container, _persistence);

                            perfCounter.ResetCounters();

                            return(returnCode);
                        }
                    }
                    finally
                    {
                        RemoveFileAccessRule(parser.Profiler64Path, new64BitAccessRule);
                    }
                }
                finally
                {
                    RemoveFileAccessRule(parser.Profiler32Path, new32BitAccessRule);
                }
            }
            catch (ExitApplicationWithoutReportingException)
            {
                LogMandatoryFatal(GitHubIssuesStatement);
                LogMandatoryFatal(GitHubIssuesListUrl);
                return(MakeExitCode(ProgramExitCodes.ApplicationExitDueToError));
            }
            catch (Exception ex)
            {
                LogMandatoryFatal("At: Program.Main");
                LogMandatoryFatal($"An {ex.GetType()} occured: {ex.Message}.");
                LogMandatoryFatal($"stack: {ex.StackTrace}");
                LogMandatoryFatal(GitHubIssuesStatement);
                LogMandatoryFatal(GitHubIssuesListUrl);
                return(MakeExitCode(ProgramExitCodes.ApplicationExitDueToUnexpectedException));
            }
        }
        public void WhenCommitOccursWithoutContextEnd_TraceDataAdded()
        {
            // arrange
            const string className       = "ClassName";
            const string classFilename   = "ClassName.cs";
            const string methodSignature = "void ClassName::MethodName();";
            const int    startLine       = 1;
            const int    endLine         = 1;

            var module = new Module
            {
                ModuleName = "ModuleName",
                ModulePath = @"C:\Module.dll",
                Aliases    = { @"C:\Module.dll" },
                Classes    = new[]
                {
                    new Class
                    {
                        FullName = className,
                        Files    = new [] { new File {
                                                FullPath = classFilename
                                            } },
                        Methods = new[]
                        {
                            new Method
                            {
                                FullName        = "MethodName",
                                MethodSignature = methodSignature,
                                MetadataToken   = 1
                            }
                        }
                    }
                }
            };

            module.Classes[0].Methods[0].DeclaringClass = module.Classes[0];
            module.Classes[0].DeclaringModule           = module;

            _traceAgent.Setup(x => x.Connect()).Returns(true);
            _traceAgent.Setup(x => x.Prepare()).Returns(true);

            _traceDataCollector.Setup(x => x.AddMethodVisit(
                                          className,
                                          classFilename,
                                          "",
                                          methodSignature,
                                          startLine,
                                          endLine));

            var persistence = new CodePulsePersistence(_mockCommandLine.Object, _mockLogger.Object);

            persistence.Initialize(_traceAgent.Object);

            persistence.PersistModule(module);

            var instrumentationPoint = new SequencePoint
            {
                DeclaringMethod = module.Classes[0].Methods[0],
                StartLine       = startLine,
                EndLine         = endLine
            };

            var visitData = new List <byte>();

            visitData.AddRange(BitConverter.GetBytes(5u));
            visitData.AddRange(BitConverter.GetBytes(1u));
            visitData.AddRange(BitConverter.GetBytes(2ul));
            visitData.AddRange(BitConverter.GetBytes(3ul));

            // act
            persistence.SaveVisitData(visitData.ToArray());
            persistence.Commit();

            // assert
            Assert.AreEqual(1, instrumentationPoint.UniqueSequencePoint);
            _traceDataCollector.Verify(x => x.AddMethodVisit(className,
                                                             classFilename,
                                                             "",
                                                             methodSignature,
                                                             startLine,
                                                             endLine));
        }