|
| 1 | +use crate::types::{ |
| 2 | + ArgRecord, CallRecord, EventLogKind, FullValueRecord, FunctionId, FunctionRecord, Line, PathId, RecordEvent, ReturnRecord, StepRecord, |
| 3 | + TraceLowLevelEvent, TypeId, ValueRecord, VariableId, TypeSpecificInfo, TypeRecord, TypeKind, |
| 4 | +}; |
| 5 | +use std::collections::HashMap; |
| 6 | +use std::env; |
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | + |
| 9 | +pub struct Tracer { |
| 10 | + // trace metadata: |
| 11 | + workdir: PathBuf, |
| 12 | + program: String, |
| 13 | + args: Vec<String>, |
| 14 | + // trace events |
| 15 | + pub events: Vec<TraceLowLevelEvent>, |
| 16 | + |
| 17 | + // internal tracer state: |
| 18 | + paths: HashMap<PathBuf, PathId>, |
| 19 | + functions: HashMap<String, FunctionId>, |
| 20 | + variables: HashMap<String, VariableId>, |
| 21 | + types: HashMap<String, TypeId>, |
| 22 | +} |
| 23 | + |
| 24 | +// we ensure in start they are registered with those id-s |
| 25 | + |
| 26 | +// pub const EXAMPLE_INT_TYPE_ID: TypeId = TypeId(0); |
| 27 | +// pub const EXAMPLE_FLOAT_TYPE_ID: TypeId = TypeId(1); |
| 28 | +// pub const EXAMPLE_BOOL_TYPE_ID: TypeId = TypeId(2); |
| 29 | +// pub const EXAMPLE_STRING_TYPE_ID: TypeId = TypeId(3); |
| 30 | +pub const NONE_TYPE_ID: TypeId = TypeId(0); |
| 31 | +pub const NONE_VALUE: ValueRecord = ValueRecord::None { type_id: NONE_TYPE_ID }; |
| 32 | + |
| 33 | +impl Tracer { |
| 34 | + pub fn new(program: &str, args: &[String]) -> Self { |
| 35 | + Tracer { |
| 36 | + workdir: env::current_dir().unwrap(), |
| 37 | + program: program.to_string(), |
| 38 | + args: args.to_vec(), |
| 39 | + events: vec![], |
| 40 | + |
| 41 | + paths: HashMap::new(), |
| 42 | + functions: HashMap::new(), |
| 43 | + variables: HashMap::new(), |
| 44 | + types: HashMap::new(), |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + pub fn start(&mut self, path: &PathBuf, line: Line) { |
| 49 | + let path_id = self.ensure_path_id(path); |
| 50 | + let function_id = self.ensure_function_id("<toplevel>", path, line); |
| 51 | + self.register_call(function_id, vec![]); |
| 52 | + |
| 53 | + // probably we let the user choose, as different languages have |
| 54 | + // different base types/names |
| 55 | + // assert!(EXAMPLE_INT_TYPE_ID == self.load_type_id(TypeKind::Int, "Int")); |
| 56 | + // assert!(EXAMPLE_FLOAT_TYPE_ID == self.load_type_id(TypeKind::Float, "Float")); |
| 57 | + // assert!(EXAMPLE_BOOL_TYPE_ID == self.load_type_id(TypeKind::Bool, "Bool")); |
| 58 | + // assert!(EXAMPLE_STRING_TYPE_ID == self.load_type_id(TypeKind::Bool, "String")); |
| 59 | + assert!(NONE_TYPE_ID == self.ensure_type_id(TypeKind::None, "None")); |
| 60 | + } |
| 61 | + |
| 62 | + pub fn ensure_path_id(&mut self, path: &Path) -> PathId { |
| 63 | + if !self.paths.contains_key(path) { |
| 64 | + self.paths.insert(path.to_path_buf(), PathId(self.paths.len())); |
| 65 | + self.register_path(path); |
| 66 | + } |
| 67 | + *self.paths.get(path).unwrap() |
| 68 | + } |
| 69 | + |
| 70 | + pub fn ensure_function_id(&mut self, function_name: &str, path: &Path, line: Line) -> FunctionId { |
| 71 | + if !self.functions.contains_key(function_name) { |
| 72 | + // same function names for different path line? TODO |
| 73 | + self.functions.insert(function_name.to_string(), FunctionId(self.functions.len())); |
| 74 | + self.register_function(function_name, path, line); |
| 75 | + } |
| 76 | + *self.functions.get(function_name).unwrap() |
| 77 | + } |
| 78 | + |
| 79 | + pub fn ensure_type_id(&mut self, kind: TypeKind, lang_type: &str) -> TypeId { |
| 80 | + if !self.types.contains_key(lang_type) { |
| 81 | + self.types.insert(lang_type.to_string(), TypeId(self.types.len())); |
| 82 | + self.register_type(kind, lang_type); |
| 83 | + } |
| 84 | + *self.types.get(lang_type).unwrap() |
| 85 | + } |
| 86 | + |
| 87 | + pub fn ensure_variable_id(&mut self, variable_name: &str) -> VariableId { |
| 88 | + if !self.variables.contains_key(variable_name) { |
| 89 | + self.variables.insert(variable_name.to_string(), VariableId(self.variables.len())); |
| 90 | + self.register_variable_event(variable_name); |
| 91 | + } |
| 92 | + *self.variables.get(variable_name).unwrap() |
| 93 | + } |
| 94 | + |
| 95 | + pub fn register_path(&mut self, path: &Path) { |
| 96 | + self.events.push(TraceLowLevelEvent::Path(path.to_path_buf())); |
| 97 | + } |
| 98 | + |
| 99 | + pub fn register_function(&mut self, name: &str, path: &Path, line: Line) { |
| 100 | + let path_id = self.ensure_path_id(path); |
| 101 | + self.events.push(TraceLowLevelEvent::Function(FunctionRecord { |
| 102 | + name: name.to_string(), |
| 103 | + path_id, |
| 104 | + line, |
| 105 | + })); |
| 106 | + } |
| 107 | + |
| 108 | + pub fn register_step(&mut self, path: &PathBuf, line: Line) { |
| 109 | + let path_id = self.ensure_path_id(path); |
| 110 | + self.events.push(TraceLowLevelEvent::Step(StepRecord { path_id, line: line })); |
| 111 | + } |
| 112 | + |
| 113 | + pub fn register_call(&mut self, function_id: FunctionId, args: Vec<ArgRecord>) { |
| 114 | + self.events.push(TraceLowLevelEvent::Call(CallRecord { function_id, args })); |
| 115 | + } |
| 116 | + |
| 117 | + pub fn register_return(&mut self, return_value: ValueRecord) { |
| 118 | + self.events.push(TraceLowLevelEvent::Return(ReturnRecord { return_value })); |
| 119 | + } |
| 120 | + |
| 121 | + pub fn register_special_event(&mut self, kind: EventLogKind, content: &str) { |
| 122 | + self.events.push(TraceLowLevelEvent::Event(RecordEvent { |
| 123 | + kind, |
| 124 | + content: content.to_string(), |
| 125 | + })); |
| 126 | + } |
| 127 | + |
| 128 | + pub fn register_type(&mut self, kind: TypeKind, lang_type: &str) { |
| 129 | + let typ = TypeRecord { |
| 130 | + kind, |
| 131 | + lang_type: lang_type.to_string(), |
| 132 | + specific_info: TypeSpecificInfo::None, |
| 133 | + }; |
| 134 | + self.events.push(TraceLowLevelEvent::Type(typ)); |
| 135 | + } |
| 136 | + |
| 137 | + pub fn register_variable_with_full_value(&mut self, name: &str, value: ValueRecord) { |
| 138 | + let variable_id = self.ensure_variable_id(name); |
| 139 | + self.register_full_value(variable_id, value); |
| 140 | + } |
| 141 | + |
| 142 | + pub fn register_variable_event(&mut self, variable_name: &str) { |
| 143 | + self.events.push(TraceLowLevelEvent::Variable(variable_name.to_string())); |
| 144 | + } |
| 145 | + |
| 146 | + pub fn register_full_value(&mut self, variable_id: VariableId, value: ValueRecord) { |
| 147 | + self.events.push( |
| 148 | + TraceLowLevelEvent::Value(FullValueRecord { |
| 149 | + variable_id, |
| 150 | + value, |
| 151 | + }) |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments