public static extern int StartTrace(out ulong traceHandle,
                                     string instanceName,   // case-sensitive, must be unique
                                     [In][Out] EventTraceProperties properties);
 public static extern int ControlTrace(ulong traceHandle,
                                       string instanceName,
                                       [In][Out] EventTraceProperties properties,
                                       EventTraceControl controlCode);
예제 #3
0
        private static void _StartTracing(EventProvider ep)
        {
            try
            {
                // N.B. Not using Util.Assert here, since Util.Assert traces.
                Debug.Assert(!String.IsNullOrEmpty(sm_logFile), "Must have a log file to start tracing.");
                sm_maxLogFileSizeMB = RegistryUtils.GetRegValue("MaxTraceFileSizeMB", 8, (v) => Math.Max(1, v));
                // If we are doing "low-priority" tracing, let's use a smaller file. And for
                // testing logging, we'll make it so we can wrap around a little quicker
                if (sm_useLowPriFile ||
                    !String.IsNullOrEmpty(Environment.GetEnvironmentVariable("_DBGSHELL_TEST_MIN_TRACE_FILE_SIZE")))
                {
                    sm_maxLogFileSizeMB = 1;
                }
                int    bufSizeKb  = 0;
                string bufSizeStr = Environment.GetEnvironmentVariable("_DBGSHELL_TEST_BUF_SIZE");
                if (!String.IsNullOrEmpty(bufSizeStr))
                {
                    if (Int32.TryParse(bufSizeStr, out bufSizeKb))
                    {
                        if (bufSizeKb < 0)
                        {
                            // N.B. Not using Util.Fail here, since Util.Fail traces.
                            Debug.Fail("need a value >= 0");
                            bufSizeKb = 0;
                        }
                    }
                }

                int    bufsPerProc    = 0;
                string bufsPerProcStr = Environment.GetEnvironmentVariable("_DBGSHELL_TEST_BUFS_PER_PROC");
                if (!String.IsNullOrEmpty(bufsPerProcStr))
                {
                    if (Int32.TryParse(bufsPerProcStr, out bufsPerProc))
                    {
                        if (bufsPerProc < 0)
                        {
                            // N.B. Not using Util.Fail here, since Util.Fail traces.
                            Debug.Fail("need a value >= 0");
                            bufsPerProc = 0;
                        }
                    }
                }

                sm_etp = new EventTraceProperties(DbgProviderEtwProviderGuid, sm_logFile, sm_maxLogFileSizeMB, bufSizeKb, bufsPerProc);

                int err = NativeMethods.StartTrace(out sm_traceHandle, "Microsoft.DbgProvider.TraceSession", sm_etp);
                if (0 != err)
                {
                    var e = new Win32Exception(err);
                    e.Data["sm_logFile"] = sm_logFile;
                    throw e;
                }

                Guid tmp = DbgProviderEtwProviderGuid;
                err = NativeMethods.EnableTraceEx2(sm_traceHandle,
                                                   ref tmp,
                                                   ControlCode.ENABLE_PROVIDER,
                                                   TraceLevel.Verbose,
                                                   0,  // matchAnyKeyword
                                                   0,  // matchAllKeyword
                                                   0,  // timeout, zero means trace async
                                                   IntPtr.Zero);
                if (0 != err)
                {
                    throw new Win32Exception(err);
                }

                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
                lock (sm_syncRoot)
                    if (!sm_shuttingDown)
                    {
                        // Can't call Trace directly because this code is called in the code path that
                        // lazily initializes the sm_eventProvider field.
                        ep.WriteMessageEvent("==========================================================");
                        ep.WriteMessageEvent(Util.Sprintf("DbgProvider version {0}", fvi.FileVersion));
                        ep.WriteMessageEvent(Util.Sprintf("CurrentCulture/CurrentUICulture: {0} / {1}", CultureInfo.CurrentCulture.Name, CultureInfo.CurrentUICulture.Name));
                        ep.WriteMessageEvent(Util.Sprintf("Process ID: {0} (0x{0:x4})", NativeMethods.GetCurrentProcessId()));
                        ep.WriteMessageEvent(Util.Sprintf("Process command line: {0}", Environment.CommandLine));
                        ep.WriteMessageEvent(Util.Sprintf("User type: {0}, interactive: {1}",
                                                          _GetUserType(),
                                                          Environment.UserInteractive));
                        ep.WriteMessageEvent(Util.Sprintf("Current machine: {0}", Environment.MachineName));
                        Flush();
                    }
            }
            catch (Win32Exception w32e)
            {
                MulticulturalString mcsErrMsg = Util.McSprintf(Resources.ErrMsgTraceFailureFmt,
                                                               sm_logFile,
                                                               Util.GetExceptionMessages(w32e));
                Exception e2 = Util.TryConvertWin32ExceptionToIOException(w32e, mcsErrMsg);
                if (null != e2)
                {
                    throw e2;
                }
                else
                {
                    throw;
                }
            }
        } // end _StartTracing()