|
| 1 | +use std::sync::{Mutex, OnceLock}; |
| 2 | +use pyo3::{ |
| 3 | + exceptions::PyRuntimeError, |
| 4 | + prelude::*, |
| 5 | + types::{PyAny, PyCFunction, PyModule}, |
| 6 | +}; |
| 7 | + |
| 8 | +const MONITORING_TOOL_NAME: &str = "codetracer"; |
| 9 | + |
| 10 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 11 | +#[repr(transparent)] |
| 12 | +pub struct EventId(pub i32); |
| 13 | + |
| 14 | +#[allow(non_snake_case)] |
| 15 | +#[derive(Clone, Copy, Debug)] |
| 16 | +pub struct MonitoringEvents { |
| 17 | + pub BRANCH: EventId, |
| 18 | + pub CALL: EventId, |
| 19 | + pub C_RAISE: EventId, |
| 20 | + pub C_RETURN: EventId, |
| 21 | + pub EXCEPTION_HANDLED: EventId, |
| 22 | + pub INSTRUCTION: EventId, |
| 23 | + pub JUMP: EventId, |
| 24 | + pub LINE: EventId, |
| 25 | + pub PY_RESUME: EventId, |
| 26 | + pub PY_RETURN: EventId, |
| 27 | + pub PY_START: EventId, |
| 28 | + pub PY_THROW: EventId, |
| 29 | + pub PY_UNWIND: EventId, |
| 30 | + pub PY_YIELD: EventId, |
| 31 | + pub RAISE: EventId, |
| 32 | + pub RERAISE: EventId, |
| 33 | + pub STOP_ITERATION: EventId, |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 37 | +pub struct ToolId { |
| 38 | + pub id: u8, |
| 39 | +} |
| 40 | + |
| 41 | +pub type CallbackFn<'py> = Bound<'py, PyCFunction>; |
| 42 | + |
| 43 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 44 | +pub struct EventSet(pub i32); |
| 45 | + |
| 46 | +pub const NO_EVENTS: EventSet = EventSet(0); |
| 47 | + |
| 48 | +impl EventSet { |
| 49 | + pub const fn empty() -> Self { |
| 50 | + NO_EVENTS |
| 51 | + } |
| 52 | + pub fn contains(&self, ev: &EventId) -> bool { |
| 53 | + (self.0 & ev.0) != 0 |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +pub fn acquire_tool_id(py: Python<'_>) -> PyResult<ToolId> { |
| 58 | + let monitoring = py.import("sys")?.getattr("monitoring")?; |
| 59 | + const FALLBACK_ID: u8 = 5; |
| 60 | + monitoring.call_method1("use_tool_id", (FALLBACK_ID, MONITORING_TOOL_NAME))?; |
| 61 | + Ok(ToolId { id: FALLBACK_ID }) |
| 62 | +} |
| 63 | + |
| 64 | +pub fn load_monitoring_events(py: Python<'_>) -> PyResult<MonitoringEvents> { |
| 65 | + let monitoring = py.import("sys")?.getattr("monitoring")?; |
| 66 | + let events = monitoring.getattr("events")?; |
| 67 | + Ok(MonitoringEvents { |
| 68 | + BRANCH: EventId(events.getattr("BRANCH")?.extract()?), |
| 69 | + CALL: EventId(events.getattr("CALL")?.extract()?), |
| 70 | + C_RAISE: EventId(events.getattr("C_RAISE")?.extract()?), |
| 71 | + C_RETURN: EventId(events.getattr("C_RETURN")?.extract()?), |
| 72 | + EXCEPTION_HANDLED: EventId(events.getattr("EXCEPTION_HANDLED")?.extract()?), |
| 73 | + INSTRUCTION: EventId(events.getattr("INSTRUCTION")?.extract()?), |
| 74 | + JUMP: EventId(events.getattr("JUMP")?.extract()?), |
| 75 | + LINE: EventId(events.getattr("LINE")?.extract()?), |
| 76 | + PY_RESUME: EventId(events.getattr("PY_RESUME")?.extract()?), |
| 77 | + PY_RETURN: EventId(events.getattr("PY_RETURN")?.extract()?), |
| 78 | + PY_START: EventId(events.getattr("PY_START")?.extract()?), |
| 79 | + PY_THROW: EventId(events.getattr("PY_THROW")?.extract()?), |
| 80 | + PY_UNWIND: EventId(events.getattr("PY_UNWIND")?.extract()?), |
| 81 | + PY_YIELD: EventId(events.getattr("PY_YIELD")?.extract()?), |
| 82 | + RAISE: EventId(events.getattr("RAISE")?.extract()?), |
| 83 | + RERAISE: EventId(events.getattr("RERAISE")?.extract()?), |
| 84 | + STOP_ITERATION: EventId(events.getattr("STOP_ITERATION")?.extract()?), |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +static MONITORING_EVENTS: OnceLock<MonitoringEvents> = OnceLock::new(); |
| 89 | + |
| 90 | +pub fn monitoring_events(py: Python<'_>) -> PyResult<&'static MonitoringEvents> { |
| 91 | + if let Some(ev) = MONITORING_EVENTS.get() { |
| 92 | + return Ok(ev); |
| 93 | + } |
| 94 | + let ev = load_monitoring_events(py)?; |
| 95 | + let _ = MONITORING_EVENTS.set(ev); |
| 96 | + Ok(MONITORING_EVENTS.get().unwrap()) |
| 97 | +} |
| 98 | + |
| 99 | +pub fn register_callback( |
| 100 | + py: Python<'_>, |
| 101 | + tool: &ToolId, |
| 102 | + event: &EventId, |
| 103 | + cb: Option<&CallbackFn<'_>>, |
| 104 | +) -> PyResult<()> { |
| 105 | + let monitoring = py.import("sys")?.getattr("monitoring")?; |
| 106 | + match cb { |
| 107 | + Some(cb) => { |
| 108 | + monitoring.call_method("register_callback", (tool.id, event.0, cb), None)?; |
| 109 | + } |
| 110 | + None => { |
| 111 | + monitoring.call_method("register_callback", (tool.id, event.0, py.None()), None)?; |
| 112 | + } |
| 113 | + } |
| 114 | + Ok(()) |
| 115 | +} |
| 116 | + |
| 117 | +pub fn events_union(ids: &[EventId]) -> EventSet { |
| 118 | + let mut bits = 0i32; |
| 119 | + for id in ids { |
| 120 | + bits |= id.0; |
| 121 | + } |
| 122 | + EventSet(bits) |
| 123 | +} |
| 124 | + |
| 125 | +pub fn set_events(py: Python<'_>, tool: &ToolId, set: EventSet) -> PyResult<()> { |
| 126 | + let monitoring = py.import("sys")?.getattr("monitoring")?; |
| 127 | + monitoring.call_method1("set_events", (tool.id, set.0))?; |
| 128 | + Ok(()) |
| 129 | +} |
| 130 | + |
| 131 | +pub fn free_tool_id(py: Python<'_>, tool: &ToolId) -> PyResult<()> { |
| 132 | + let monitoring = py.import("sys")?.getattr("monitoring")?; |
| 133 | + monitoring.call_method1("free_tool_id", (tool.id,))?; |
| 134 | + Ok(()) |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +/// Trait implemented by tracing backends. |
| 139 | +/// |
| 140 | +/// Each method corresponds to an event from `sys.monitoring`. Default |
| 141 | +/// implementations allow implementers to only handle the events they care |
| 142 | +/// about. |
| 143 | +pub trait Tracer: Send { |
| 144 | + /// Return the set of events the tracer wants to receive. |
| 145 | + fn interest(&self, _events: &MonitoringEvents) -> EventSet { |
| 146 | + NO_EVENTS |
| 147 | + } |
| 148 | + |
| 149 | + /// Called on Python function calls. |
| 150 | + fn on_call( |
| 151 | + &mut self, |
| 152 | + _py: Python<'_>, |
| 153 | + _code: &Bound<'_, PyAny>, |
| 154 | + _offset: i32, |
| 155 | + _callable: &Bound<'_, PyAny>, |
| 156 | + _arg0: Option<&Bound<'_, PyAny>>, |
| 157 | + ) { |
| 158 | + } |
| 159 | + |
| 160 | + /// Called on line execution. |
| 161 | + fn on_line(&mut self, _py: Python<'_>, _code: &Bound<'_, PyAny>, _lineno: u32) {} |
| 162 | +} |
| 163 | + |
| 164 | +struct Global { |
| 165 | + tracer: Box<dyn Tracer>, |
| 166 | + mask: EventSet, |
| 167 | + tool: ToolId, |
| 168 | +} |
| 169 | + |
| 170 | +static GLOBAL: Mutex<Option<Global>> = Mutex::new(None); |
| 171 | + |
| 172 | +/// Install a tracer and hook it into Python's `sys.monitoring`. |
| 173 | +pub fn install_tracer(py: Python<'_>, tracer: Box<dyn Tracer>) -> PyResult<()> { |
| 174 | + let mut guard = GLOBAL.lock().unwrap(); |
| 175 | + if guard.is_some() { |
| 176 | + return Err(PyRuntimeError::new_err("tracer already installed")); |
| 177 | + } |
| 178 | + |
| 179 | + let tool = acquire_tool_id(py)?; |
| 180 | + let events = monitoring_events(py)?; |
| 181 | + |
| 182 | + let module = PyModule::new(py, "_codetracer_callbacks")?; |
| 183 | + |
| 184 | + let mask = tracer.interest(events); |
| 185 | + |
| 186 | + if mask.contains(&events.CALL) { |
| 187 | + let cb = wrap_pyfunction!(callback_call, &module)?; |
| 188 | + |
| 189 | + register_callback(py, &tool, &events.CALL, Some(&cb))?; |
| 190 | + |
| 191 | + } |
| 192 | + if mask.contains(&events.LINE) { |
| 193 | + let cb = wrap_pyfunction!(callback_line, &module)?; |
| 194 | + register_callback(py, &tool, &events.LINE, Some(&cb))?; |
| 195 | + } |
| 196 | + set_events(py, &tool, mask)?; |
| 197 | + |
| 198 | + |
| 199 | + *guard = Some(Global { |
| 200 | + tracer, |
| 201 | + mask, |
| 202 | + tool, |
| 203 | + }); |
| 204 | + Ok(()) |
| 205 | +} |
| 206 | + |
| 207 | +/// Remove the installed tracer if any. |
| 208 | +pub fn uninstall_tracer(py: Python<'_>) -> PyResult<()> { |
| 209 | + let mut guard = GLOBAL.lock().unwrap(); |
| 210 | + if let Some(global) = guard.take() { |
| 211 | + let events = monitoring_events(py)?; |
| 212 | + if global.mask.contains(&events.CALL) { |
| 213 | + register_callback(py, &global.tool, &events.CALL, None)?; |
| 214 | + } |
| 215 | + if global.mask.contains(&events.LINE) { |
| 216 | + register_callback(py, &global.tool, &events.LINE, None)?; |
| 217 | + } |
| 218 | + set_events(py, &global.tool, NO_EVENTS)?; |
| 219 | + free_tool_id(py, &global.tool)?; |
| 220 | + } |
| 221 | + Ok(()) |
| 222 | +} |
| 223 | + |
| 224 | +#[pyfunction] |
| 225 | +fn callback_call( |
| 226 | + py: Python<'_>, |
| 227 | + code: Bound<'_, PyAny>, |
| 228 | + offset: i32, |
| 229 | + callable: Bound<'_, PyAny>, |
| 230 | + arg0: Option<Bound<'_, PyAny>>, |
| 231 | +) -> PyResult<()> { |
| 232 | + if let Some(global) = GLOBAL.lock().unwrap().as_mut() { |
| 233 | + global.tracer.on_call(py, &code, offset, &callable, arg0.as_ref()); |
| 234 | + } |
| 235 | + Ok(()) |
| 236 | +} |
| 237 | + |
| 238 | +#[pyfunction] |
| 239 | +fn callback_line(py: Python<'_>, code: Bound<'_, PyAny>, lineno: u32) -> PyResult<()> { |
| 240 | + if let Some(global) = GLOBAL.lock().unwrap().as_mut() { |
| 241 | + global.tracer.on_line(py, &code, lineno); |
| 242 | + } |
| 243 | + Ok(()) |
| 244 | +} |
0 commit comments