|
| 1 | +# Listing 1: Trace format selection and writer/reader interfaces |
| 2 | + |
| 3 | +This listing examines the public API surface that lets clients choose a trace format, create reader or writer instances, and record events. It covers `TraceEventsFileFormat` and related constants, factory functions, the `TraceReader` trait, `NonStreamingTraceWriter` structure, and the comprehensive `TraceWriter` trait. |
| 4 | + |
| 5 | +**Start the enumeration of supported trace file formats.** |
| 6 | +```rust |
| 7 | +#[derive(Debug, Clone, Copy)] |
| 8 | +pub enum TraceEventsFileFormat { |
| 9 | +``` |
| 10 | + |
| 11 | +**List JSON and two binary formats.** |
| 12 | +```rust |
| 13 | + Json, |
| 14 | + BinaryV0, |
| 15 | + Binary, |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +**Provide constants for representing missing values and the top-level function.** |
| 20 | +```rust |
| 21 | +pub const NONE_TYPE_ID: TypeId = TypeId(0); |
| 22 | +pub const NONE_VALUE: ValueRecord = ValueRecord::None { type_id: NONE_TYPE_ID }; |
| 23 | +pub const TOP_LEVEL_FUNCTION_ID: FunctionId = FunctionId(0); |
| 24 | +``` |
| 25 | + |
| 26 | +**Construct a trace reader for a given format.** |
| 27 | +```rust |
| 28 | +pub fn create_trace_reader(format: TraceEventsFileFormat) -> Box<dyn TraceReader> { |
| 29 | +``` |
| 30 | + |
| 31 | +**Match on the format to instantiate the proper reader.** |
| 32 | +```rust |
| 33 | + match format { |
| 34 | + TraceEventsFileFormat::Json => Box::new(JsonTraceReader {}), |
| 35 | + TraceEventsFileFormat::BinaryV0 | TraceEventsFileFormat::Binary => Box::new(BinaryTraceReader {}), |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +**Construct a trace writer for a program, arguments, and format.** |
| 41 | +```rust |
| 42 | +pub fn create_trace_writer(program: &str, args: &[String], format: TraceEventsFileFormat) -> Box<dyn TraceWriter> { |
| 43 | +``` |
| 44 | + |
| 45 | +**Begin matching on the requested format.** |
| 46 | +```rust |
| 47 | + match format { |
| 48 | +``` |
| 49 | + |
| 50 | +**Use the in-memory writer for JSON and legacy binary formats.** |
| 51 | +```rust |
| 52 | + TraceEventsFileFormat::Json | TraceEventsFileFormat::BinaryV0 => { |
| 53 | + let mut result = Box::new(NonStreamingTraceWriter::new(program, args)); |
| 54 | + result.set_format(format); |
| 55 | + result |
| 56 | + } |
| 57 | +``` |
| 58 | + |
| 59 | +**Produce a streaming CBOR writer for the current binary format and return.** |
| 60 | +```rust |
| 61 | + TraceEventsFileFormat::Binary => Box::new(crate::cbor_zstd_writer::CborZstdTraceWriter::new(program, args)), |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +**Define the trait for loading stored traces.** |
| 67 | +```rust |
| 68 | +pub trait TraceReader { |
| 69 | + fn load_trace_events(&mut self, path: &Path) -> Result<Vec<TraceLowLevelEvent>, Box<dyn Error>>; |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +**Begin declaration of a writer that accumulates events in memory.** |
| 74 | +```rust |
| 75 | +pub struct NonStreamingTraceWriter { |
| 76 | +``` |
| 77 | + |
| 78 | +**Fields track metadata, collected events, format, and output path.** |
| 79 | +```rust |
| 80 | + base: AbstractTraceWriterData, |
| 81 | + pub events: Vec<TraceLowLevelEvent>, |
| 82 | + format: TraceEventsFileFormat, |
| 83 | + trace_events_path: Option<PathBuf>, |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +**Implement constructor initializing storage and format.** |
| 88 | +```rust |
| 89 | +impl NonStreamingTraceWriter { |
| 90 | + pub fn new(program: &str, args: &[String]) -> Self { |
| 91 | + NonStreamingTraceWriter { |
| 92 | + base: AbstractTraceWriterData::new(program, args), |
| 93 | +``` |
| 94 | + |
| 95 | +**Set up empty event list, default binary format, and unset path.** |
| 96 | +```rust |
| 97 | + events: vec![], |
| 98 | + format: TraceEventsFileFormat::Binary, |
| 99 | + trace_events_path: None, |
| 100 | + } |
| 101 | + } |
| 102 | +``` |
| 103 | + |
| 104 | +**Allow callers to override output format.** |
| 105 | +```rust |
| 106 | + pub fn set_format(&mut self, format: TraceEventsFileFormat) { |
| 107 | + self.format = format; |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +**Declare the `TraceWriter` trait which extends `AbstractTraceWriter`.** |
| 113 | +```rust |
| 114 | +pub trait TraceWriter: AbstractTraceWriter { |
| 115 | +``` |
| 116 | + |
| 117 | +**Provide default metadata handling and leave event writing abstract.** |
| 118 | +```rust |
| 119 | + fn begin_writing_trace_metadata(&mut self, path: &Path) -> Result<(), Box<dyn Error>> { |
| 120 | + AbstractTraceWriter::begin_writing_trace_metadata(self, path) |
| 121 | + } |
| 122 | + fn begin_writing_trace_events(&mut self, path: &Path) -> Result<(), Box<dyn Error>>; |
| 123 | +``` |
| 124 | + |
| 125 | +**Delegate path file initialization to the underlying writer.** |
| 126 | +```rust |
| 127 | + fn begin_writing_trace_paths(&mut self, path: &Path) -> Result<(), Box<dyn Error>> { |
| 128 | + AbstractTraceWriter::begin_writing_trace_paths(self, path) |
| 129 | + } |
| 130 | +``` |
| 131 | + |
| 132 | +**Start tracing and request identifiers for paths.** |
| 133 | +```rust |
| 134 | + fn start(&mut self, path: &Path, line: Line) { |
| 135 | + AbstractTraceWriter::start(self, path, line) |
| 136 | + } |
| 137 | + fn ensure_path_id(&mut self, path: &Path) -> PathId { |
| 138 | + AbstractTraceWriter::ensure_path_id(self, path) |
| 139 | + } |
| 140 | +``` |
| 141 | + |
| 142 | +**Allocate a unique identifier for a function.** |
| 143 | +```rust |
| 144 | + fn ensure_function_id(&mut self, function_name: &str, path: &Path, line: Line) -> FunctionId { |
| 145 | + AbstractTraceWriter::ensure_function_id(self, function_name, path, line) |
| 146 | + } |
| 147 | +``` |
| 148 | + |
| 149 | +**Allocate type identifiers, either by components or explicit record.** |
| 150 | +```rust |
| 151 | + fn ensure_type_id(&mut self, kind: TypeKind, lang_type: &str) -> TypeId { |
| 152 | + AbstractTraceWriter::ensure_type_id(self, kind, lang_type) |
| 153 | + } |
| 154 | + fn ensure_raw_type_id(&mut self, typ: TypeRecord) -> TypeId { |
| 155 | + AbstractTraceWriter::ensure_raw_type_id(self, typ) |
| 156 | + } |
| 157 | +``` |
| 158 | + |
| 159 | +**Intern variable names for later reference.** |
| 160 | +```rust |
| 161 | + fn ensure_variable_id(&mut self, variable_name: &str) -> VariableId { |
| 162 | + AbstractTraceWriter::ensure_variable_id(self, variable_name) |
| 163 | + } |
| 164 | +``` |
| 165 | + |
| 166 | +**Register source file paths and function declarations.** |
| 167 | +```rust |
| 168 | + fn register_path(&mut self, path: &Path) { |
| 169 | + AbstractTraceWriter::register_path(self, path) |
| 170 | + } |
| 171 | + fn register_function(&mut self, name: &str, path: &Path, line: Line) { |
| 172 | + AbstractTraceWriter::register_function(self, name, path, line) |
| 173 | + } |
| 174 | +``` |
| 175 | + |
| 176 | +**Capture line execution and function invocation with arguments.** |
| 177 | +```rust |
| 178 | + fn register_step(&mut self, path: &Path, line: Line) { |
| 179 | + AbstractTraceWriter::register_step(self, path, line) |
| 180 | + } |
| 181 | + fn register_call(&mut self, function_id: FunctionId, args: Vec<FullValueRecord>) { |
| 182 | + AbstractTraceWriter::register_call(self, function_id, args) |
| 183 | + } |
| 184 | +``` |
| 185 | + |
| 186 | +**Helper to build an argument record and note function returns.** |
| 187 | +```rust |
| 188 | + fn arg(&mut self, name: &str, value: ValueRecord) -> FullValueRecord { |
| 189 | + AbstractTraceWriter::arg(self, name, value) |
| 190 | + } |
| 191 | + fn register_return(&mut self, return_value: ValueRecord) { |
| 192 | + AbstractTraceWriter::register_return(self, return_value) |
| 193 | + } |
| 194 | +``` |
| 195 | + |
| 196 | +**Record ad-hoc events within the trace.** |
| 197 | +```rust |
| 198 | + // TODO: add metadata arg |
| 199 | + fn register_special_event(&mut self, kind: EventLogKind, content: &str) { |
| 200 | + AbstractTraceWriter::register_special_event(self, kind, content) |
| 201 | + } |
| 202 | +``` |
| 203 | + |
| 204 | +**Convert type descriptions into records and register them.** |
| 205 | +```rust |
| 206 | + fn to_raw_type(&self, kind: TypeKind, lang_type: &str) -> TypeRecord { |
| 207 | + AbstractTraceWriter::to_raw_type(self, kind, lang_type) |
| 208 | + } |
| 209 | + fn register_type(&mut self, kind: TypeKind, lang_type: &str) { |
| 210 | + AbstractTraceWriter::register_type(self, kind, lang_type) |
| 211 | + } |
| 212 | +``` |
| 213 | + |
| 214 | +**Support registering arbitrary type records and assembly snippets.** |
| 215 | +```rust |
| 216 | + fn register_raw_type(&mut self, typ: TypeRecord) { |
| 217 | + AbstractTraceWriter::register_raw_type(self, typ) |
| 218 | + } |
| 219 | + fn register_asm(&mut self, instructions: &[String]) { |
| 220 | + AbstractTraceWriter::register_asm(self, instructions) |
| 221 | + } |
| 222 | +``` |
| 223 | + |
| 224 | +**Store a variable and its value in one step.** |
| 225 | +```rust |
| 226 | + fn register_variable_with_full_value(&mut self, name: &str, value: ValueRecord) { |
| 227 | + AbstractTraceWriter::register_variable_with_full_value(self, name, value) |
| 228 | + } |
| 229 | +``` |
| 230 | + |
| 231 | +**Separate registration of variable names and associated values.** |
| 232 | +```rust |
| 233 | + fn register_variable_name(&mut self, variable_name: &str) { |
| 234 | + AbstractTraceWriter::register_variable_name(self, variable_name) |
| 235 | + } |
| 236 | + fn register_full_value(&mut self, variable_id: VariableId, value: ValueRecord) { |
| 237 | + AbstractTraceWriter::register_full_value(self, variable_id, value) |
| 238 | + } |
| 239 | +``` |
| 240 | + |
| 241 | +**Emit compound values and individual cells.** |
| 242 | +```rust |
| 243 | + fn register_compound_value(&mut self, place: Place, value: ValueRecord) { |
| 244 | + AbstractTraceWriter::register_compound_value(self, place, value) |
| 245 | + } |
| 246 | + fn register_cell_value(&mut self, place: Place, value: ValueRecord) { |
| 247 | + AbstractTraceWriter::register_cell_value(self, place, value) |
| 248 | + } |
| 249 | +``` |
| 250 | + |
| 251 | +**Describe updates to compound items or cell contents.** |
| 252 | +```rust |
| 253 | + fn assign_compound_item(&mut self, place: Place, index: usize, item_place: Place) { |
| 254 | + AbstractTraceWriter::assign_compound_item(self, place, index, item_place) |
| 255 | + } |
| 256 | + fn assign_cell(&mut self, place: Place, new_value: ValueRecord) { |
| 257 | + AbstractTraceWriter::assign_cell(self, place, new_value) |
| 258 | + } |
| 259 | +``` |
| 260 | + |
| 261 | +**Track variables pointing to places and their removal.** |
| 262 | +```rust |
| 263 | + fn register_variable(&mut self, variable_name: &str, place: Place) { |
| 264 | + AbstractTraceWriter::register_variable(self, variable_name, place) |
| 265 | + } |
| 266 | + fn drop_variable(&mut self, variable_name: &str) { |
| 267 | + AbstractTraceWriter::drop_variable(self, variable_name) |
| 268 | + } |
| 269 | +``` |
| 270 | + |
| 271 | +**Record an assignment with explicit pass-by semantics.** |
| 272 | +```rust |
| 273 | + fn assign(&mut self, variable_name: &str, rvalue: RValue, pass_by: PassBy) { |
| 274 | + AbstractTraceWriter::assign(self, variable_name, rvalue, pass_by) |
| 275 | + } |
| 276 | +``` |
| 277 | + |
| 278 | +**Bind variables to storage locations or drop multiple at once.** |
| 279 | +```rust |
| 280 | + fn bind_variable(&mut self, variable_name: &str, place: Place) { |
| 281 | + AbstractTraceWriter::bind_variable(self, variable_name, place) |
| 282 | + } |
| 283 | + fn drop_variables(&mut self, variable_names: &[String]) { |
| 284 | + AbstractTraceWriter::drop_variables(self, variable_names) |
| 285 | + } |
| 286 | +``` |
| 287 | + |
| 288 | +**Build an rvalue from a single variable.** |
| 289 | +```rust |
| 290 | + fn simple_rvalue(&mut self, variable_name: &str) -> RValue { |
| 291 | + AbstractTraceWriter::simple_rvalue(self, variable_name) |
| 292 | + } |
| 293 | +``` |
| 294 | + |
| 295 | +**Build an rvalue from multiple variable dependencies.** |
| 296 | +```rust |
| 297 | + fn compound_rvalue(&mut self, variable_dependencies: &[String]) -> RValue { |
| 298 | + AbstractTraceWriter::compound_rvalue(self, variable_dependencies) |
| 299 | + } |
| 300 | +``` |
| 301 | + |
| 302 | +**Remove the last step event when necessary.** |
| 303 | +```rust |
| 304 | + fn drop_last_step(&mut self) { |
| 305 | + AbstractTraceWriter::drop_last_step(self) |
| 306 | + } |
| 307 | +``` |
| 308 | + |
| 309 | +**Insert an event directly into the stream.** |
| 310 | +```rust |
| 311 | + fn add_event(&mut self, event: TraceLowLevelEvent) { |
| 312 | + AbstractTraceWriter::add_event(self, event) |
| 313 | + } |
| 314 | +``` |
| 315 | + |
| 316 | +**Append multiple events in one call.** |
| 317 | +```rust |
| 318 | + fn append_events(&mut self, events: &mut Vec<TraceLowLevelEvent>) { |
| 319 | + AbstractTraceWriter::append_events(self, events) |
| 320 | + } |
| 321 | +``` |
| 322 | + |
| 323 | +**Finish writing metadata before finalizing events and paths.** |
| 324 | +```rust |
| 325 | + fn finish_writing_trace_metadata(&mut self) -> Result<(), Box<dyn Error>> { |
| 326 | + AbstractTraceWriter::finish_writing_trace_metadata(self) |
| 327 | + } |
| 328 | +``` |
| 329 | + |
| 330 | +**Leave event completion abstract and provide default path finalization.** |
| 331 | +```rust |
| 332 | + fn finish_writing_trace_events(&mut self) -> Result<(), Box<dyn Error>>; |
| 333 | + fn finish_writing_trace_paths(&mut self) -> Result<(), Box<dyn Error>> { |
| 334 | + AbstractTraceWriter::finish_writing_trace_paths(self) |
| 335 | + } |
| 336 | +} |
| 337 | +``` |
0 commit comments