Skip to content

Commit

Permalink
Fix tests on other platforms. (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robo210 authored Dec 7, 2024
1 parent 56d6fcb commit 515f2d6
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ jobs:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
[![Build Status][actions-badge]][actions-url]
![maintenance status][maint-badge]

# Overview
## Overview

This layer emits tracing events as Windows ETW events or Linux user-mode tracepoints
(user_events with the [EventHeader](https://github.com/microsoft/LinuxTracepoints/tree/main/libeventheader-tracepoint)
encoding; requires a Linux 6.4+ kernel).

*Note*: Linux kernels without user_events support will not log any events.

# MSV
## MSV

Minimum supported Rust version is 1.80 (released July 2024).

Expand Down
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,24 +206,29 @@ macro_rules! etw_event {
}
);

#[used]
static ETW_META: $crate::_details::EventMetadata = $crate::_details::EventMetadata{
kw: $kw,
// TODO: Hash the callsite identity at compile time, or get Identifier to implement Ord
identity: tracing_core::identify_callsite!(&CALLSITE),
event_tag: $tags as u32
};

// These two statics need to be mut to avoid compiler errors about *const EventMetadata not being Sync.

paste! {
#[cfg(target_os = "linux")]
#[link_section = "_etw_kw"]
#[allow(non_upper_case_globals, dead_code)]
#[allow(non_upper_case_globals)]
#[used]
static mut [<ETW_META_PTR $name>]: *const $crate::_details::EventMetadata = &ETW_META;
}

paste! {
#[cfg(target_os = "windows")]
#[link_section = ".rsdata$zRSETW5"]
#[allow(non_upper_case_globals, dead_code)]
#[link_section = ".rdata$zRSETW5"]
#[allow(non_upper_case_globals)]
#[used]
static mut [<ETW_META_PTR $name>]: *const $crate::_details::EventMetadata = &ETW_META;
}

Expand Down
10 changes: 6 additions & 4 deletions src/native/etw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use tracelogging::*;
use tracelogging_dynamic::EventBuilder;
use tracing_subscriber::registry::{LookupSpan, SpanRef};

// Items within this .rdata section will be sorted alphabetically, thus the start is named with "0", the end "9", and each metadata "5".
// If these statics aren't mut then everything will silently fail to work.
#[allow(non_upper_case_globals)]
#[link_section = ".rsdata$zRSETW0"]
pub(crate) static mut _start__etw_kw: usize = usize::from_ne_bytes(*b"RSETW000");
#[link_section = ".rdata$zRSETW0"]
pub(crate) static mut _start__etw_kw: usize = 0;
#[allow(non_upper_case_globals)]
#[link_section = ".rsdata$zRSETW9"]
pub(crate) static mut _stop__etw_kw: usize = usize::from_ne_bytes(*b"RSETW999");
#[link_section = ".rdata$zRSETW9"]
pub(crate) static mut _stop__etw_kw: usize = 0;

thread_local! {static EBW: std::cell::RefCell<EventBuilder> = RefCell::new(EventBuilder::new());}

Expand Down
6 changes: 0 additions & 6 deletions src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ pub mod noop;
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
#[doc(hidden)]
pub use noop::Provider;
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
#[doc(hidden)]
pub(crate) use noop::_start__etw_kw;
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
#[doc(hidden)]
pub(crate) use noop::_stop__etw_kw;

#[cfg(target_os = "linux")]
#[doc(hidden)]
Expand Down
5 changes: 0 additions & 5 deletions src/native/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ use tracing_subscriber::registry::{LookupSpan, SpanRef};

use crate::error::EtwError;

#[allow(non_upper_case_globals)]
pub(crate) static mut _start__etw_kw: usize = 0;
#[allow(non_upper_case_globals)]
pub(crate) static mut _stop__etw_kw: usize = 0;

#[doc(hidden)]
pub struct Provider;

Expand Down
5 changes: 3 additions & 2 deletions src/native/user_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use eventheader_dynamic::EventBuilder;
use std::{cell::RefCell, ops::DerefMut, pin::Pin, sync::Arc, time::SystemTime};
use tracing_subscriber::registry::{LookupSpan, SpanRef};

// These are automagically generated by the compiler for the start and stop of section _etw_kw
extern "C" {
#[link_name = "__start__etw_kw"]
pub(crate) static mut _start__etw_kw: usize;
pub(crate) static _start__etw_kw: usize;
#[link_name = "__stop__etw_kw"]
pub(crate) static mut _stop__etw_kw: usize;
pub(crate) static _stop__etw_kw: usize;
}

#[link_section = "_etw_kw"]
Expand Down
21 changes: 17 additions & 4 deletions src/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ pub(crate) static GLOBAL_ACTIVITY_SEED: LazyLock<[u8; 16]> = LazyLock::new(|| {
data
});

static EVENT_METADATA: LazyLock<Box<[ParsedEventMetadata]>> = LazyLock::new(|| {
fn process_static_metadata() -> Box<[ParsedEventMetadata]> {
// The array of pointers are in a mutable section and can be sorted/deduped, but they are pointing to read-only static data

let start =
&raw const crate::native::_start__etw_kw as *mut *const EventMetadata;
let stop =
&raw const crate::native::_stop__etw_kw as *mut *const EventMetadata;

assert!(!start.is_null());
if start.is_null() {
return Box::new([]);
}

// SAFETY On Windows the start and stop entries are sentry values at the start and end of the linker section.
// Linux does not need these sentries.
Expand Down Expand Up @@ -94,7 +96,13 @@ static EVENT_METADATA: LazyLock<Box<[ParsedEventMetadata]>> = LazyLock::new(|| {
let mut sorted = vec.into_boxed_slice();
sorted.sort_unstable_by(|a, b| b.cmp(a));
sorted
});
}

#[cfg(any(target_os = "windows", target_os = "linux"))]
static EVENT_METADATA: LazyLock<Box<[ParsedEventMetadata]>> = LazyLock::new(process_static_metadata);

#[cfg(not(any(target_os = "windows", target_os = "linux")))]
static EVENT_METADATA: [ParsedEventMetadata; 0] = [];

impl core::cmp::PartialEq for ParsedEventMetadata {
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -212,6 +220,11 @@ mod test {
sum += event.kw;
}

assert_eq!(sum, 55);
#[cfg(any(target_os = "windows", target_os = "linux"))]
let expected = 55;
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
let expected = 0;

assert_eq!(sum, expected);
}
}

0 comments on commit 515f2d6

Please sign in to comment.