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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

use core::marker::PhantomPinned;

#[cfg(all(windows, feature = "etw"))]
use core::cell::UnsafeCell;
#[cfg(all(windows, feature = "etw"))]
use core::sync::atomic;

use crate::descriptors::EventDataDescriptor;
use crate::descriptors::EventDescriptor;
use crate::enums::Level;
use crate::guid::Guid;

/// Possible configurations under which this crate can be compiled: `Windows` or `Other`.
pub enum NativeImplementation {
    /// Crate compiled for other configuration (no logging is performed).
    Other,
    /// Crate compiled for Windows (ETW) configuration (logging is performed via ETW
    /// APIs).
    Windows,
}

/// The configuration under which this crate was compiled: `Windows` or `Other`.
pub const NATIVE_IMPLEMENTATION: NativeImplementation = if cfg!(all(windows, feature = "etw")) {
    NativeImplementation::Windows
} else {
    NativeImplementation::Other
};

/// Signature for a custom
/// [provider enable callback](https://docs.microsoft.com/windows/win32/api/evntprov/nc-evntprov-penablecallback).
pub type ProviderEnableCallback = fn(
    source_id: &Guid,
    event_control_code: u32,
    level: Level,
    match_any_keyword: u64,
    match_all_keyword: u64,
    filter_data: usize,
    callback_context: usize,
);

#[cfg(all(windows, feature = "etw"))]
type OuterEnableCallback = unsafe extern "system" fn(
    source_id: &Guid,
    event_control_code: u32,
    level: u8,
    match_any_keyword: u64,
    match_all_keyword: u64,
    filter_data: usize,
    outer_context: usize, // = &ProviderContextInner
);

/// Data needed to manage an ETW registration with callback.
pub struct ProviderContext {
    _pinned: PhantomPinned,

    #[cfg(all(windows, feature = "etw"))]
    cell: UnsafeCell<ProviderContextInner>,
}

impl ProviderContext {
    /// Windows: return EventActivityIdControl(...);
    /// Other: return ERROR_NOT_SUPPORTED;
    pub fn activity_id_control(_control_code: u32, _activity_id: &mut Guid) -> u32 {
        let result;
        #[cfg(not(all(windows, feature = "etw")))]
        {
            result = 50; // ERROR_NOT_SUPPORTED
        }
        #[cfg(all(windows, feature = "etw"))]
        {
            result = unsafe { EventActivityIdControl(_control_code, _activity_id) };
        }
        return result;
    }

    /// Creates a new provider context.
    pub const fn new() -> ProviderContext {
        return ProviderContext {
            _pinned: PhantomPinned,

            #[cfg(all(windows, feature = "etw"))]
            cell: UnsafeCell::new(ProviderContextInner::new()),
        };
    }

    /// Returns the registration handle. For diagnostic purposes only.
    pub const fn reg_handle(&self) -> u64 {
        let result;
        #[cfg(not(all(windows, feature = "etw")))]
        {
            result = 0;
        }
        #[cfg(all(windows, feature = "etw"))]
        {
            let inner_ptr: *const ProviderContextInner = self.cell.get();
            let inner = unsafe { &*inner_ptr };
            result = inner.reg_handle;
        }
        return result;
    }

    /// Returns true if the provider is enabled at the specified level and keyword.
    #[inline(always)]
    pub const fn enabled(&self, _level: Level, _keyword: u64) -> bool {
        let result;
        #[cfg(not(all(windows, feature = "etw")))]
        {
            result = false;
        }
        #[cfg(all(windows, feature = "etw"))]
        {
            let inner_ptr: *const ProviderContextInner = self.cell.get();
            let inner = unsafe { &*inner_ptr };
            result = (_level.0 as i32) <= inner.level && inner.enabled_keyword(_keyword);
        }
        return result;
    }

    /// Calls EventUnregister and sets reg_handle = 0.
    ///
    /// # Preconditions
    /// - This will panic if it overlaps with another thread simultaneously calling
    ///   register or unregister.
    pub fn unregister(&self) -> u32 {
        let result;
        #[cfg(not(all(windows, feature = "etw")))]
        {
            result = 0;
        }
        #[cfg(all(windows, feature = "etw"))]
        {
            let inner_ptr: *mut ProviderContextInner = self.cell.get();
            let inner_mut = unsafe { &mut *inner_ptr };
            result = inner_mut.unregister();
        }
        return result;
    }

    /// Calls EventRegister.
    ///
    /// # Preconditions
    /// - This will panic if provider is currently registered.
    /// - This will panic if it overlaps with another thread simultaneously calling
    ///   register or unregister.
    ///
    /// # Safety
    /// 1. Pinning: Context must not be moved-from as long as provider is registered.
    /// 2. Unregister: If a provider is registered in a DLL, unregister **must** be
    ///    called before the DLL unloads.
    pub unsafe fn register(
        &self,
        _provider_id: &Guid,
        _callback_fn: Option<ProviderEnableCallback>,
        _callback_context: usize,
    ) -> u32 {
        let result;
        #[cfg(not(all(windows, feature = "etw")))]
        {
            result = 0;
        }
        #[cfg(all(windows, feature = "etw"))]
        {
            result = /* unsafe */ { &mut *self.cell.get() }.register(
                _provider_id,
                _callback_fn,
                _callback_context);
        }
        return result;
    }

    /// Calls EventSetInformation.
    pub fn set_information(&self, _information_class: u32, _information: &[u8]) -> u32 {
        let result;
        #[cfg(not(all(windows, feature = "etw")))]
        {
            result = 0;
        }
        #[cfg(all(windows, feature = "etw"))]
        {
            result = unsafe {
                EventSetInformation(
                    self.reg_handle(),
                    _information_class,
                    _information.as_ptr(),
                    _information.len() as u32,
                )
            };
        }
        return result;
    }

    /// Calls EventWriteTransfer.
    pub fn write_transfer(
        &self,
        _descriptor: &EventDescriptor,
        _activity_id: Option<&[u8; 16]>,
        _related_id: Option<&[u8; 16]>,
        _data: &[EventDataDescriptor],
    ) -> u32 {
        let result;
        #[cfg(not(all(windows, feature = "etw")))]
        {
            result = 0;
        }
        #[cfg(all(windows, feature = "etw"))]
        {
            result = unsafe {
                EventWriteTransfer(
                    self.reg_handle(),
                    _descriptor,
                    _activity_id,
                    _related_id,
                    _data.len() as u32,
                    _data.as_ptr(),
                )
            };
        }
        return result;
    }
}

unsafe impl Sync for ProviderContext {}

impl Default for ProviderContext {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for ProviderContext {
    /// Calls unregister.
    fn drop(&mut self) {
        self.unregister();
    }
}

#[cfg(all(windows, feature = "etw"))]
struct ProviderContextInner {
    level: i32, // -1 means not enabled by anybody.
    busy: atomic::AtomicBool,
    reg_handle: u64,
    keyword_any: u64,
    keyword_all: u64,
    callback_fn: Option<ProviderEnableCallback>,
    callback_context: usize,
}

#[cfg(all(windows, feature = "etw"))]
impl ProviderContextInner {
    const fn new() -> Self {
        return Self {
            level: -1,
            busy: atomic::AtomicBool::new(false),
            reg_handle: 0,
            keyword_any: 0,
            keyword_all: 0,
            callback_fn: None,
            callback_context: 0,
        };
    }

    /// Returns true if the provider is enabled at the specified keyword.
    const fn enabled_keyword(&self, keyword: u64) -> bool {
        return keyword == 0
            || ((keyword & self.keyword_any) != 0
                && (keyword & self.keyword_all) == self.keyword_all);
    }

    fn unregister(&mut self) -> u32 {
        let result;

        let was_busy = self.busy.swap(true, atomic::Ordering::Acquire);
        if was_busy {
            result = 0;
        } else {
            if self.reg_handle == 0 {
                result = 0;
            } else {
                result = unsafe { EventUnregister(self.reg_handle) };
                self.level = -1;
                self.reg_handle = 0;
            }

            self.busy.swap(false, atomic::Ordering::Release);
        }

        return result;
    }

    fn register(
        &mut self,
        provider_id: &Guid,
        callback_fn: Option<ProviderEnableCallback>,
        callback_context: usize,
    ) -> u32 {
        let was_busy = self.busy.swap(true, atomic::Ordering::Acquire);
        if was_busy {
            panic!("provider.register called simultaneously with another call to register or unregister.");
        }

        if self.reg_handle != 0 {
            self.busy.swap(false, atomic::Ordering::Relaxed);
            panic!("provider.register called when provider is already registered");
        }

        self.callback_fn = callback_fn;
        self.callback_context = callback_context;

        let self_ptr: *mut Self = self;
        let result = unsafe {
            EventRegister(
                provider_id,
                Self::outer_callback,
                self_ptr as usize,
                &mut self.reg_handle,
            )
        };

        self.busy.swap(false, atomic::Ordering::Release);

        return result;
    }

    #[cfg(all(windows, feature = "etw"))]
    fn outer_callback_impl(
        &mut self,
        source_id: &Guid,
        event_control_code: u32,
        level: u8,
        match_any_keyword: u64,
        match_all_keyword: u64,
        filter_data: usize,
    ) {
        match event_control_code {
            0 => {
                self.level = -1;
            }
            1 => {
                self.level = level as i32;
                self.keyword_any = match_any_keyword;
                self.keyword_all = match_all_keyword;
            }
            _ => {}
        }

        if let Some(callback_fn) = self.callback_fn {
            callback_fn(
                source_id,
                event_control_code,
                Level(level),
                match_any_keyword,
                match_all_keyword,
                filter_data,
                self.callback_context,
            );
        }
    }

    /// Implements the native ETW provider enable callback.
    unsafe extern "system" fn outer_callback(
        source_id: &Guid,
        event_control_code: u32,
        level: u8,
        match_any_keyword: u64,
        match_all_keyword: u64,
        filter_data: usize,
        outer_context: usize,
    ) {
        (*(outer_context as *mut Self)).outer_callback_impl(
            source_id,
            event_control_code,
            level,
            match_any_keyword,
            match_all_keyword,
            filter_data,
        );
    }
}

#[cfg(all(windows, feature = "etw"))]
extern "system" {
    fn EventUnregister(reg_handle: u64) -> u32;
    fn EventRegister(
        provider_id: &Guid,
        outer_callback: OuterEnableCallback,
        outer_context: usize,
        reg_handle: &mut u64,
    ) -> u32;
    fn EventSetInformation(
        reg_handle: u64,
        information_class: u32,
        information: *const u8,
        information_length: u32,
    ) -> u32;
    fn EventWriteTransfer(
        reg_handle: u64,
        descriptor: &EventDescriptor,
        activity_id: Option<&[u8; 16]>,
        related_id: Option<&[u8; 16]>,
        data_count: u32,
        data: *const EventDataDescriptor,
    ) -> u32;
    fn EventActivityIdControl(control_code: u32, activity_id: &mut Guid) -> u32;
}