Skip to content

Replace winapi #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions examples/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
name = "hello_world"
version = "0.1.1"
authors = ["Arlie Davis <ardavis@microsoft.com>"]
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
win_etw_macros = { path = "../../win_etw_macros" }
win_etw_provider = { path = "../../win_etw_provider", features = ["std"] }
widestring = "^1.0"
winapi = { version = "^0.3.8", features = ["ntstatus"] }

[dependencies.windows]
version = "0.58.0"
features = [
"Win32_Foundation",
]

[features]
default = []
9 changes: 4 additions & 5 deletions examples/hello_world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ fn main() {

#[cfg(target_os = "windows")]
{
pub use winapi::shared::ntstatus;
pub use winapi::shared::winerror;
pub use windows::Win32::Foundation::{STATUS_DEVICE_REQUIRES_CLEANING, DXGI_DDI_ERR_WASSTILLDRAWING, ERROR_OUT_OF_PAPER};

hello_provider.arg_hresult(None, winerror::DXGI_DDI_ERR_WASSTILLDRAWING);
hello_provider.arg_ntstatus(None, ntstatus::STATUS_DEVICE_REQUIRES_CLEANING as u32);
hello_provider.arg_win32error(None, winerror::ERROR_OUT_OF_PAPER);
hello_provider.arg_hresult(None, DXGI_DDI_ERR_WASSTILLDRAWING);
hello_provider.arg_ntstatus(None, STATUS_DEVICE_REQUIRES_CLEANING.0);
hello_provider.arg_win32error(None, ERROR_OUT_OF_PAPER);
}

let args = std::env::args().collect::<Vec<String>>();
Expand Down
5 changes: 3 additions & 2 deletions win_etw_logger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "win_etw_logger"
version = "0.1.7"
authors = ["Arlie Davis <ardavis@microsoft.com>"]
edition = "2018"
edition = "2021"
description = "A Rust log provider which forwards events to Event Tracing for Windows (ETW)."
license = "Apache-2.0 OR MIT"
homepage = "https://github.com/microsoft/rust_win_etw"
Expand All @@ -15,4 +15,5 @@ log = { version = "^0.4", features = ["std"] }
win_etw_provider = { version = "0.1.9", path = "../win_etw_provider" }
win_etw_macros = { version = "0.1.7", path = "../win_etw_macros" }
win_etw_metadata = { version = "0.1.2", path = "../win_etw_metadata" }
winapi = { version = "^0.3.8" }
windows-core = { version = "0.58.0" }
windows = { version = "0.58.0", features = ["Win32_Foundation", "Win32_System_Diagnostics_Etw"] }
2 changes: 1 addition & 1 deletion win_etw_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "win_etw_macros"
version = "0.1.9"
authors = ["Arlie Davis <ardavis@microsoft.com>"]
edition = "2018"
edition = "2021"
description = "Enables apps to report events to Event Tracing for Windows (ETW)."
license = "Apache-2.0 OR MIT"
homepage = "https://github.com/microsoft/rust_win_etw"
Expand Down
2 changes: 1 addition & 1 deletion win_etw_metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "win_etw_metadata"
version = "0.1.2"
authors = ["Arlie Davis <ardavis@microsoft.com>"]
edition = "2018"
edition = "2021"
description = "Provides metadata definitions for the win_etw_provider and win_etw_macros crates."
license = "Apache-2.0 OR MIT"
homepage = "https://github.com/microsoft/rust_win_etw"
Expand Down
5 changes: 3 additions & 2 deletions win_etw_provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "win_etw_provider"
version = "0.1.11"
authors = ["Arlie Davis <ardavis@microsoft.com>"]
edition = "2018"
edition = "2021"
description = "Enables apps to report events to Event Tracing for Windows (ETW)."
license = "Apache-2.0 OR MIT"
homepage = "https://github.com/microsoft/rust_win_etw"
Expand All @@ -18,7 +18,8 @@ win_etw_metadata = { version = "^0.1.2", path = "../win_etw_metadata" }
uuid = {version = "1", optional = true}

[target.'cfg(windows)'.dependencies]
winapi = { version = "^0.3", features = ["evntprov", "winerror", "evntrace"] }
windows-core = { version = "0.58.0" }
windows = { version = "0.58.0", features = ["Win32_Foundation", "Win32_System_Diagnostics_Etw"] }

[dev-dependencies]
atomic_lazy = { git = "https://github.com/sivadeilra/atomic_lazy" }
Expand Down
13 changes: 7 additions & 6 deletions win_etw_provider/src/guid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ macro_rules! guid {
/// an equivalent type from other crates in order to minimize its dependencies. `GUID` has a well-
/// defined byte representation, so converting between different implementations of `GUID` is
/// not a problem.
/// TODO: we're depending on windows-core anyway, can we drop this?
#[repr(C)]
#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, AsBytes, FromBytes, FromZeroes)]
pub struct GUID {
Expand Down Expand Up @@ -86,13 +87,13 @@ impl core::fmt::Debug for GUID {
}

#[cfg(target_os = "windows")]
impl From<winapi::shared::guiddef::GUID> for GUID {
fn from(value: winapi::shared::guiddef::GUID) -> Self {
impl From<windows_core::GUID> for GUID {
fn from(value: windows_core::GUID) -> Self {
Self {
data1: value.Data1,
data2: value.Data2,
data3: value.Data3,
data4: value.Data4,
data1: value.data1,
data2: value.data2,
data3: value.data3,
data4: value.data4,
}
}
}
Expand Down
61 changes: 30 additions & 31 deletions win_etw_provider/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::guid::GUID;
use crate::Level;
use crate::{Error, EventDataDescriptor};
use alloc::boxed::Box;
use evntprov::PENABLECALLBACK;
use windows::Win32::Foundation::ERROR_SUCCESS;
use core::convert::TryFrom;
use core::pin::Pin;
use core::ptr::null;
Expand Down Expand Up @@ -126,11 +128,11 @@ impl Provider for EtwProvider {

if let Some(options) = options {
if let Some(id) = options.activity_id.as_ref() {
activity_id_ptr = id as *const GUID as *const winapi::shared::guiddef::GUID;
activity_id_ptr = id as *const GUID as *const windows_core::GUID;
}
if let Some(id) = options.related_activity_id.as_ref() {
related_activity_id_ptr =
id as *const GUID as *const winapi::shared::guiddef::GUID;
id as *const GUID as *const windows_core::GUID;
}
if let Some(level) = options.level {
event_descriptor.Level = level.0;
Expand All @@ -142,11 +144,9 @@ impl Provider for EtwProvider {
&event_descriptor,
0, // filter
0, // flags
activity_id_ptr, // activity id
related_activity_id_ptr, // related activity id
data.len() as u32,
data.as_ptr() as *const evntprov::EVENT_DATA_DESCRIPTOR
as *mut evntprov::EVENT_DATA_DESCRIPTOR,
Some(activity_id_ptr), // activity id
Some(related_activity_id_ptr), // related activity id
Some(std::mem::transmute(data)) // TODO: assert size and aligment adds up
);
if error != 0 {
write_failed(error)
Expand All @@ -161,7 +161,7 @@ impl Provider for EtwProvider {
fn is_enabled(&self, level: u8, keyword: u64) -> bool {
#[cfg(target_os = "windows")]
{
unsafe { evntprov::EventProviderEnabled(self.handle, level, keyword) != 0 }
unsafe { evntprov::EventProviderEnabled(self.handle, level, keyword)}.as_bool()
}
#[cfg(not(target_os = "windows"))]
{
Expand All @@ -177,8 +177,8 @@ impl Provider for EtwProvider {
evntprov::EventEnabled(
self.handle,
event_descriptor as *const _ as *const evntprov::EVENT_DESCRIPTOR,
) != 0
}
)
}.as_bool()
} else {
let max_level = self.stable.as_ref().max_level.load(SeqCst);
event_descriptor.level.0 <= max_level
Expand All @@ -201,9 +201,8 @@ fn write_failed(_error: u32) {

#[cfg(target_os = "windows")]
mod win_support {
pub use winapi::shared::evntprov;
pub use winapi::shared::evntrace;
pub use winapi::shared::winerror;
use windows::Win32::System::Diagnostics::Etw::{ENABLECALLBACK_ENABLED_STATE, EVENT_CONTROL_CODE_CAPTURE_STATE, EVENT_CONTROL_CODE_DISABLE_PROVIDER, EVENT_CONTROL_CODE_ENABLE_PROVIDER};
pub use windows::Win32::System::Diagnostics::Etw as evntprov;

use super::*;

Expand All @@ -216,13 +215,13 @@ mod win_support {

/// See [PENABLECALLBACK](https://docs.microsoft.com/en-us/windows/win32/api/evntprov/nc-evntprov-penablecallback).
pub(crate) unsafe extern "system" fn enable_callback(
_source_id: *const winapi::shared::guiddef::GUID,
is_enabled_code: u32,
_source_id: *const windows_core::GUID,
is_enabled_code: ENABLECALLBACK_ENABLED_STATE,
level: u8,
_match_any_keyword: u64,
_match_all_keyword: u64,
_filter_data: *mut evntprov::EVENT_FILTER_DESCRIPTOR,
context: *mut winapi::ctypes::c_void,
_filter_data: *const evntprov::EVENT_FILTER_DESCRIPTOR,
context: *mut core::ffi::c_void,
) {
// This should never happen.
if context.is_null() {
Expand All @@ -245,21 +244,21 @@ mod win_support {
}

match is_enabled_code {
evntrace::EVENT_CONTROL_CODE_ENABLE_PROVIDER => {
EVENT_CONTROL_CODE_ENABLE_PROVIDER => {
#[cfg(feature = "dev")]
{
eprintln!("ETW is ENABLING this provider. setting level: {}", level);
}
stable_data.max_level.store(level, SeqCst);
}
evntrace::EVENT_CONTROL_CODE_DISABLE_PROVIDER => {
EVENT_CONTROL_CODE_DISABLE_PROVIDER => {
#[cfg(feature = "dev")]
{
eprintln!("ETW is DISABLING this provider. setting level: {}", level);
}
stable_data.max_level.store(level, SeqCst);
}
evntrace::EVENT_CONTROL_CODE_CAPTURE_STATE => {
EVENT_CONTROL_CODE_CAPTURE_STATE => {
// ETW is requesting that the provider log its state information. The meaning of this
// is provider-dependent. Currently, this functionality is not exposed to Rust apps.
#[cfg(feature = "dev")]
Expand All @@ -282,7 +281,7 @@ mod win_support {

pub fn new_activity_id() -> Result<GUID, Error> {
unsafe {
let mut guid: winapi::shared::guiddef::GUID = core::mem::zeroed();
let mut guid: windows_core::GUID = core::mem::zeroed();
let error = evntprov::EventActivityIdControl(
evntprov::EVENT_ACTIVITY_CTRL_CREATE_ID,
&mut guid,
Expand All @@ -307,13 +306,13 @@ impl EtwProvider {
let mut stable = Box::pin(StableProviderData {
max_level: AtomicU8::new(0),
});
let mut handle: evntprov::REGHANDLE = 0;
let mut handle = evntprov::REGHANDLE(0);
let stable_ptr: &mut StableProviderData = &mut stable;
let error = evntprov::EventRegister(
provider_id as *const _ as *const winapi::shared::guiddef::GUID,
provider_id as *const _ as *const windows_core::GUID,
Some(enable_callback),
stable_ptr as *mut StableProviderData as *mut winapi::ctypes::c_void,
&mut handle,
Some(stable_ptr as *mut StableProviderData as *mut core::ffi::c_void),
&mut handle as *mut evntprov::REGHANDLE as *mut u64,
);
if error != 0 {
Err(Error::WindowsError(error))
Expand All @@ -336,8 +335,8 @@ impl EtwProvider {
unsafe {
let error = evntprov::EventSetInformation(
self.handle,
2,
provider_metadata.as_ptr() as *mut winapi::ctypes::c_void,
windows::Win32::System::Diagnostics::Etw::EVENT_INFO_CLASS(2),
provider_metadata.as_ptr() as *mut core::ffi::c_void,
u32::try_from(provider_metadata.len()).unwrap(),
);
if error != 0 {
Expand Down Expand Up @@ -370,7 +369,7 @@ impl EtwProvider {
let error = evntprov::EventSetInformation(
self.handle,
evntprov::EventProviderSetTraits,
provider_traits.as_ptr() as *mut u8 as *mut winapi::ctypes::c_void,
provider_traits.as_ptr() as *mut u8 as *mut core::ffi::c_void,
u32::try_from(provider_traits.len()).unwrap(),
);
if error != 0 {
Expand Down Expand Up @@ -437,9 +436,9 @@ pub fn with_activity<F: FnOnce() -> R, R>(f: F) -> R {
unsafe {
let result = evntprov::EventActivityIdControl(
evntprov::EVENT_ACTIVITY_CTRL_CREATE_SET_ID,
&mut previous_activity_id as *mut _ as *mut winapi::shared::guiddef::GUID,
&mut previous_activity_id as *mut _ as *mut windows_core::GUID,
);
if result == winerror::ERROR_SUCCESS {
if result == ERROR_SUCCESS.0 {
restore.previous_activity_id = Some(previous_activity_id);
} else {
// Failed to create/replace the activity ID. There is not much we can do about this.
Expand Down Expand Up @@ -471,7 +470,7 @@ impl Drop for RestoreActivityHolder {
if let Some(previous_activity_id) = self.previous_activity_id.as_ref() {
evntprov::EventActivityIdControl(
evntprov::EVENT_ACTIVITY_CTRL_SET_ID,
previous_activity_id as *const GUID as *const winapi::shared::guiddef::GUID
previous_activity_id as *const GUID as *const windows_core::GUID
as *mut _,
);
}
Expand Down
Loading