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
//! ETW Event Schema locator and handler //! //! This module contains the means needed to locate and interact with the Schema of an ETW event use crate::native::etw_types::{DecodingSource, EventRecord, TraceEventInfoRaw}; use crate::native::tdh; use crate::native::tdh_types::Property; use std::collections::HashMap; use std::sync::Arc; use windows::Guid; /// Schema module errors #[derive(Debug)] pub enum SchemaError { /// Represents a Parser error ParseError, /// Represents an internal [TdhNativeError] /// /// [TdhNativeError]: tdh::TdhNativeError TdhNativeError(tdh::TdhNativeError), } impl From<tdh::TdhNativeError> for SchemaError { fn from(err: tdh::TdhNativeError) -> Self { SchemaError::TdhNativeError(err) } } type SchemaResult<T> = Result<T, SchemaError>; #[derive(Debug, Eq, PartialEq, Hash)] struct SchemaKey { // For now, lazy to wrap Guid around an implement Hash // TODO: wrap Guid and implement hash provider: String, id: u16, opcode: u8, version: u8, level: u8, } impl SchemaKey { pub fn new(event: &EventRecord) -> Self { let provider = format!("{:?}", event.EventHeader.ProviderId); SchemaKey { provider, id: event.EventHeader.EventDescriptor.Id, opcode: event.EventHeader.EventDescriptor.Opcode, version: event.EventHeader.EventDescriptor.Version, level: event.EventHeader.EventDescriptor.Level, } } } /// Represents a cache of Schemas already located /// /// This cache is implemented as a [HashMap] where the key is a combination of the following elements /// of an [Event Record](https://docs.microsoft.com/en-us/windows/win32/api/evntcons/ns-evntcons-event_record) /// * EventHeader.ProviderId /// * EventHeader.EventDescriptor.Id /// * EventHeader.EventDescriptor.Opcode /// * EventHeader.EventDescriptor.Version /// * EventHeader.EventDescriptor.Level /// /// Credits: [KrabsETW::schema_locator](https://github.com/microsoft/krabsetw/blob/master/krabs/krabs/schema_locator.hpp) #[derive(Default)] pub struct SchemaLocator { schemas: HashMap<SchemaKey, Arc<TraceEventInfoRaw>>, } impl std::fmt::Debug for SchemaLocator { fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { todo!() } } impl SchemaLocator { pub(crate) fn new() -> Self { SchemaLocator { schemas: HashMap::new(), } } /// Use the `event_schema` function to retrieve the Schema of an ETW Event /// /// # Arguments /// * `event` - The [EventRecord] that's passed to the callback /// /// # Remark /// This is the first function that should be called within a Provider callback, if everything /// works as expected this function will return a Result with the [Schema] that represents /// the ETW event that triggered the callback /// /// This function can fail, if it does it will return a [SchemaError] /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// }; /// ``` pub fn event_schema(&mut self, event: EventRecord) -> SchemaResult<Schema> { let key = SchemaKey::new(&event); let info: Arc<_>; if !self.schemas.contains_key(&key) { // TODO: Cloning for now, should be a reference at some point... info = Arc::from(tdh::schema_from_tdh(event.clone())?); self.schemas.insert(key, Arc::clone(&info)); } else { info = Arc::clone(self.schemas.get(&key).unwrap()); } Ok(Schema::new(event, info)) } } /// Represents a Schema /// /// This structure holds a [TraceEventInfo](https://docs.microsoft.com/en-us/windows/win32/api/tdh/ns-tdh-trace_event_info) /// which let us obtain information from the ETW event pub struct Schema { record: EventRecord, schema: Arc<TraceEventInfoRaw>, } impl Schema { pub(crate) fn new(record: EventRecord, schema: Arc<TraceEventInfoRaw>) -> Self { Schema { record, schema } } pub(crate) fn user_buffer(&self) -> Vec<u8> { unsafe { std::slice::from_raw_parts( self.record.UserData as *mut _, self.record.UserDataLength.into(), ) .to_vec() } } // Horrible getters FTW!! :D // TODO: Not a big fan of this, think a better way.. pub(crate) fn record(&self) -> EventRecord { self.record } /// Use the `event_id` function to obtain the EventId of the Event Record /// /// This getter returns the EventId of the ETW Event that triggered the registered callback /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let event_id = schema.event_id(); /// }; /// ``` pub fn event_id(&self) -> u16 { self.record.EventHeader.EventDescriptor.Id } /// Use the `opcode` function to obtain the Opcode of the Event Record /// /// This getter returns the opcode of the ETW Event that triggered the registered callback /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let event_id = schema.opcode(); /// }; /// ``` pub fn opcode(&self) -> u8 { self.record.EventHeader.EventDescriptor.Opcode } /// Use the `event_flags` function to obtain the Event Flags of the [EventRecord] /// /// This getter returns the Event Flags of the ETW Event that triggered the registered callback /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let event_flags = schema.event_flags(); /// }; /// ``` pub fn event_flags(&self) -> u16 { self.record.EventHeader.Flags } /// Use the `event_version` function to obtain the Version of the [EventRecord] /// /// This getter returns the Version of the ETW Event that triggered the registered callback /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let event_version = schema.event_version(); /// }; /// ``` pub fn event_version(&self) -> u8 { self.record.EventHeader.EventDescriptor.Version } /// Use the `process_id` function to obtain the ProcessId of the [EventRecord] /// /// This getter returns the ProcessId of the process that triggered the ETW Event /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let pid = schema.process_id(); /// }; /// ``` pub fn process_id(&self) -> u32 { self.record.EventHeader.ProcessId } /// Use the `thread_id` function to obtain the ThreadId of the [EventRecord] /// /// This getter returns the ThreadId of the thread that triggered the ETW Event /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let tid = schema.thread_id(); /// }; /// ``` pub fn thread_id(&self) -> u32 { self.record.EventHeader.ThreadId } /// Use the `timestamp` function to obtain the TimeStamp of the [EventRecord] /// /// This getter returns the TimeStamp of the ETW Event /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let timestamp = schema.timestamp(); /// }; /// ``` pub fn timestamp(&self) -> i64 { self.record.EventHeader.TimeStamp } /// Use the `activity_id` function to obtain the ActivityId of the [EventRecord] /// /// This getter returns the ActivityId from the ETW Event, this value is used to related Two events /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let activity_id = schema.activity_id(); /// }; /// ``` /// [TraceEventInfo]: crate::native::etw_types::TraceEventInfo pub fn activity_id(&self) -> Guid { self.record.EventHeader.ActivityId } /// Use the `decoding_source` function to obtain the [DecodingSource] from the [TraceEventInfo] /// /// This getter returns the DecodingSource from the event, this value identifies the source used /// parse the event data /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let decoding_source = schema.decoding_source(); /// }; /// ``` /// [TraceEventInfo]: crate::native::etw_types::TraceEventInfo pub fn decoding_source(&self) -> DecodingSource { self.schema.decoding_source() } /// Use the `provider_name` function to obtain the Provider name from the [TraceEventInfo] /// /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let provider_name = schema.provider_name(); /// }; /// ``` /// [TraceEventInfo]: crate::native::etw_types::TraceEventInfo pub fn provider_name(&self) -> String { self.schema.provider_name() } /// Use the `task_name` function to obtain the Task name from the [TraceEventInfo] /// /// See: [TaskType](https://docs.microsoft.com/en-us/windows/win32/wes/eventmanifestschema-tasktype-complextype) /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let task_name = schema.task_name(); /// }; /// ``` /// [TraceEventInfo]: crate::native::etw_types::TraceEventInfo pub fn task_name(&self) -> String { self.schema.task_name() } /// Use the `opcode_name` function to obtain the Opcode name from the [TraceEventInfo] /// /// See: [OpcodeType](https://docs.microsoft.com/en-us/windows/win32/wes/eventmanifestschema-opcodetype-complextype) /// # Example /// ```rust /// let my_callback = |record: EventRecord, schema_locator: &mut SchemaLocator| { /// let schema = schema_locator.event_schema(record)?; /// let opcode_name = schema.opcode_name(); /// }; /// ``` /// [TraceEventInfo]: crate::native::etw_types::TraceEventInfo pub fn opcode_name(&self) -> String { self.schema.opcode_name() } pub(crate) fn property_count(&self) -> u32 { self.schema.property_count() } pub(crate) fn property(&self, index: u32) -> Property { self.schema.property(index) } } impl PartialEq for Schema { fn eq(&self, other: &Self) -> bool { self.schema.event_id() == other.schema.event_id() && self.schema.provider_guid() == other.schema.provider_guid() && self.schema.event_version() == other.schema.event_version() } } impl Eq for Schema {} #[cfg(test)] mod test { use super::*; fn test_getters() { todo!() } fn test_schema_key() { todo!() } }