File: EtwTrace.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (283 lines) | stat: -rw-r--r-- 11,525 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//------------------------------------------------------------------------------
// <copyright file="EtwTrace.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

/*
 * EtwTrace class
 */
namespace System.Web {

    using System.Web.Hosting;
    using System.Web.Util;

    internal enum EtwTraceConfigType {
        DOWNLEVEL = 0,
        IIS7_ISAPI = 1,
        IIS7_INTEGRATED = 2            
    }

    internal enum EtwTraceType {
        ETW_TYPE_START                                    = 1,
        ETW_TYPE_END                                      = 2,
        ETW_TYPE_REQ_QUEUED                               = 3,
        ETW_TYPE_REQ_DEQUEUED                             = 4,
        ETW_TYPE_GETAPPDOMAIN_ENTER                       = 5,
        ETW_TYPE_GETAPPDOMAIN_LEAVE                       = 6, 
        ETW_TYPE_APPDOMAIN_ENTER                          = 7,
        ETW_TYPE_START_HANDLER                            = 8,
        ETW_TYPE_END_HANDLER                              = 9,
        ETW_TYPE_PIPELINE_ENTER                           = 10,
        ETW_TYPE_PIPELINE_LEAVE                           = 11,
        ETW_TYPE_MAPHANDLER_ENTER                         = 12,
        ETW_TYPE_MAPHANDLER_LEAVE                         = 13,
        ETW_TYPE_PARSE_ENTER                              = 14,
        ETW_TYPE_PARSE_LEAVE                              = 15,
        ETW_TYPE_COMPILE_ENTER                            = 16,
        ETW_TYPE_COMPILE_LEAVE                            = 17,
        ETW_TYPE_HTTPHANDLER_ENTER                        = 18,
        ETW_TYPE_HTTPHANDLER_LEAVE                        = 19,
        ETW_TYPE_SESSIONSTATE_PARTITION_START             = 20,
        ETW_TYPE_SESSIONSTATE_PARTITION_END               = 21,
        ETW_TYPE_PAGE_PRE_INIT_ENTER                      = 22,
        ETW_TYPE_PAGE_PRE_INIT_LEAVE                      = 23,
        ETW_TYPE_PAGE_INIT_ENTER                          = 24,
        ETW_TYPE_PAGE_INIT_LEAVE                          = 25,
        ETW_TYPE_PAGE_LOAD_VIEWSTATE_ENTER                = 26,
        ETW_TYPE_PAGE_LOAD_VIEWSTATE_LEAVE                = 27,
        ETW_TYPE_PAGE_LOAD_POSTDATA_ENTER                 = 28,
        ETW_TYPE_PAGE_LOAD_POSTDATA_LEAVE                 = 29,
        ETW_TYPE_PAGE_LOAD_ENTER                          = 30,
        ETW_TYPE_PAGE_LOAD_LEAVE                          = 31,
        ETW_TYPE_PAGE_POST_DATA_CHANGED_ENTER             = 32,
        ETW_TYPE_PAGE_POST_DATA_CHANGED_LEAVE             = 33,
        ETW_TYPE_PAGE_RAISE_POSTBACK_ENTER                = 34,
        ETW_TYPE_PAGE_RAISE_POSTBACK_LEAVE                = 35,
        ETW_TYPE_PAGE_PRE_RENDER_ENTER                    = 36,
        ETW_TYPE_PAGE_PRE_RENDER_LEAVE                    = 37,
        ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER                = 38,
        ETW_TYPE_PAGE_SAVE_VIEWSTATE_LEAVE                = 39,
        ETW_TYPE_PAGE_RENDER_ENTER                        = 40,
        ETW_TYPE_PAGE_RENDER_LEAVE                        = 41,
        ETW_TYPE_SESSION_DATA_BEGIN                       = 42,
        ETW_TYPE_SESSION_DATA_END                         = 43,
        ETW_TYPE_PROFILE_BEGIN                            = 44,
        ETW_TYPE_PROFILE_END                              = 45,
        ETW_TYPE_ROLE_IS_USER_IN_ROLE                     = 46,
        ETW_TYPE_ROLE_GET_USER_ROLES                      = 47,
        ETW_TYPE_ROLE_BEGIN                               = 48,
        ETW_TYPE_ROLE_END                                 = 49,
        ETW_TYPE_WEB_EVENT_RAISE_START                    = 50,
        ETW_TYPE_WEB_EVENT_RAISE_END                      = 51,
        ETW_TYPE_WEB_EVENT_DELIVER_START                  = 52,
        ETW_TYPE_WEB_EVENT_DELIVER_END                    = 53
    }

    struct EtwTraceLevel {
        internal const int None               = 0;
        internal const int Fatal              = 1;
        internal const int Error              = 2;
        internal const int Warning            = 3;
        internal const int Information        = 4;
        internal const int Verbose            = 5;
    }
    
    struct EtwTraceFlags {
        internal const int None              = 0;
        internal const int Infrastructure    = 1;
        internal const int Module            = 2;
        internal const int Page              = 4;
        internal const int AppSvc            = 8;
    }

    // these need to match the definitions in 
    // mgdeng:mgdhandler.hxx 
    // the internal WorkerRequest trace helpers
    // takes this enum as the type
    internal enum IntegratedTraceType {
        TraceWrite    = 0,
        TraceWarn     = 1,
        DiagCritical  = 2,
        DiagError     = 3,
        DiagWarning   = 4,
        DiagInfo      = 5,
        DiagVerbose   = 6,
        DiagStart     = 7,
        DiagStop      = 8,
        DiagSuspend   = 9,
        DiagResume    = 10,
        DiagTransfer  = 11,
    }

    internal enum EtwWorkerRequestType {
        Undefined = -1,
        InProc = 0,
        OutOfProc = 1,
        IIS7Integrated = 3,
        Unknown = 999
    }

    internal static class EtwTrace {
        private static int _traceLevel = 0;
        private static int _traceFlags = 0;
        private static EtwWorkerRequestType s_WrType = EtwWorkerRequestType.Undefined;

        internal static int InferVerbosity(IntegratedTraceType traceType) {

            int verbosity = EtwTraceLevel.Verbose;

            switch (traceType) {
                case IntegratedTraceType.TraceWrite:
                    verbosity = EtwTraceLevel.Verbose;
                    break;

                case IntegratedTraceType.TraceWarn:
                    verbosity = EtwTraceLevel.Warning;
                    break;

                case IntegratedTraceType.DiagCritical:
                    verbosity = EtwTraceLevel.Fatal;
                    break;

                case IntegratedTraceType.DiagWarning:
                    verbosity = EtwTraceLevel.Warning;
                    break;

                case IntegratedTraceType.DiagError:
                    verbosity = EtwTraceLevel.Error;
                    break;

                case IntegratedTraceType.DiagInfo:
                    verbosity = EtwTraceLevel.Information;
                    break;

                case IntegratedTraceType.DiagVerbose:
                    verbosity = EtwTraceLevel.Verbose;
                    break;

                case IntegratedTraceType.DiagStart:
                    verbosity = EtwTraceLevel.None;
                    break;

                case IntegratedTraceType.DiagStop:
                    verbosity = EtwTraceLevel.None;
                    break;

                case IntegratedTraceType.DiagResume:
                    verbosity = EtwTraceLevel.None;
                    break;

                case IntegratedTraceType.DiagSuspend:
                    verbosity = EtwTraceLevel.None;
                    break;

                case IntegratedTraceType.DiagTransfer:
                    verbosity = EtwTraceLevel.None;
                    break;

                default:
                    verbosity = EtwTraceLevel.Verbose;
                    break;

            }

            return verbosity;
        }

        internal static bool IsTraceEnabled(int level, int flag) {
            if (level < _traceLevel && ((flag & _traceFlags) != EtwTraceFlags.None))
                return true;
            return false;
        }

        private static void ResolveWorkerRequestType(HttpWorkerRequest workerRequest)
        {
            if (workerRequest is IIS7WorkerRequest) {
                s_WrType = EtwWorkerRequestType.IIS7Integrated;
            }
            else if (workerRequest is ISAPIWorkerRequestInProc) {
                    s_WrType = EtwWorkerRequestType.InProc;
            }
            else if (workerRequest is ISAPIWorkerRequestOutOfProc){
                s_WrType = EtwWorkerRequestType.OutOfProc;
            }
            else {
                s_WrType = EtwWorkerRequestType.Unknown;
            }
        }

        internal static void TraceEnableCheck(EtwTraceConfigType configType, IntPtr p)
        {
            // Don't activate if webengine.dll isn't loaded
            if (!HttpRuntime.IsEngineLoaded)
                return;
            
            switch (configType) {
                case EtwTraceConfigType.IIS7_INTEGRATED:
                    bool f;
                    UnsafeIISMethods.MgdEtwGetTraceConfig(p /*pRequestContext*/, out f, out _traceFlags, out _traceLevel);
                    break;
                case EtwTraceConfigType.IIS7_ISAPI:
                    int[] contentInfo = new int[3];
                    UnsafeNativeMethods.EcbGetTraceFlags(p /*pECB*/, contentInfo);
                    _traceFlags = contentInfo[0];
                    _traceLevel = contentInfo[1];
                    break;
                case EtwTraceConfigType.DOWNLEVEL:
                    UnsafeNativeMethods.GetEtwValues(out _traceLevel, out _traceFlags);
                    break;
                default:
                    break;
            }
        }

        internal static void Trace(EtwTraceType traceType, HttpWorkerRequest workerRequest)
        {
            Trace(traceType, workerRequest, null, null);
        }

        internal static void Trace(EtwTraceType traceType, HttpWorkerRequest workerRequest, string data1)
        {
            Trace(traceType, workerRequest, data1, null, null, null);
        }

        internal static void Trace(EtwTraceType traceType, HttpWorkerRequest workerRequest, string data1, string data2)
        {
            Trace(traceType, workerRequest, data1, data2, null, null);
        }

        internal static void Trace(EtwTraceType traceType, HttpWorkerRequest workerRequest, string data1, string data2, string data3, string data4)
        {
            if (s_WrType == EtwWorkerRequestType.Undefined) {
                ResolveWorkerRequestType(workerRequest);
            }

            if (s_WrType == EtwWorkerRequestType.Unknown) 
                return;

            if (workerRequest == null)
                return;
            
            if (s_WrType == EtwWorkerRequestType.IIS7Integrated) {
                UnsafeNativeMethods.TraceRaiseEventMgdHandler((int) traceType, ((IIS7WorkerRequest)workerRequest).RequestContext, data1, data2, data3, data4);
            }
            else if (s_WrType == EtwWorkerRequestType.InProc) {
                UnsafeNativeMethods.TraceRaiseEventWithEcb((int) traceType, ((ISAPIWorkerRequest)workerRequest).Ecb, data1, data2, data3, data4);
            }
            else if (s_WrType == EtwWorkerRequestType.OutOfProc) {
                UnsafeNativeMethods.PMTraceRaiseEvent((int) traceType, ((ISAPIWorkerRequest)workerRequest).Ecb, data1, data2, data3, data4);
            }
        }

        internal static void Trace(EtwTraceType traceType, IntPtr ecb, string data1, string data2, bool inProc)
        {
            if (inProc)
                UnsafeNativeMethods.TraceRaiseEventWithEcb((int) traceType, ecb, data1, data2, null, null);
            else 
                UnsafeNativeMethods.PMTraceRaiseEvent((int) traceType, ecb, data1, data2, null, null);
        }

    };
}