memscope-rs 0.2.3

A memory tracking library for Rust applications.
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! MemScope - Unified facade for all engines
//!
//! This module provides the MemScope facade which unifies all engines
//! into a simple, easy-to-use interface.

use crate::analysis::detectors::Detector;
use crate::analysis_engine::{AnalysisEngine, Analyzer};
use crate::capture::{CaptureBackendType, CaptureEngine};
use crate::event_store::EventStore;
use crate::metadata::MetadataEngine;
use crate::query::QueryEngine;
use crate::render_engine::RenderEngine;
use crate::snapshot::SnapshotEngine;
use crate::timeline::TimelineEngine;
use std::sync::{Arc, Mutex};

/// MemScope - Unified facade for all engines
///
/// MemScope provides a simple, unified interface for memory tracking,
/// analysis, and visualization. It integrates all 9 engines into a
/// single, easy-to-use API.
///
/// Key properties:
/// - Simple: One-stop interface for all functionality
/// - Powerful: Access to all engines when needed
/// - Type-safe: Strong typing throughout
/// - Thread-safe: All operations are thread-safe
pub struct MemScope {
    /// Event Store - Centralized event storage
    pub event_store: Arc<EventStore>,
    /// Capture Engine - Event capture backend
    pub capture: Arc<CaptureEngine>,
    /// Metadata Engine - Centralized metadata management
    pub metadata: Arc<MetadataEngine>,
    /// Snapshot Engine - Snapshot construction and aggregation
    pub snapshot: Arc<SnapshotEngine>,
    /// Query Engine - Unified query interface
    pub query: Arc<QueryEngine>,
    /// Analysis Engine - Memory analysis logic (wrapped in Mutex for interior mutability)
    pub analysis: Arc<Mutex<AnalysisEngine>>,
    /// Timeline Engine - Time-based memory analysis
    pub timeline: Arc<TimelineEngine>,
    /// Render Engine - Output rendering
    pub render: Arc<RenderEngine>,
}

impl MemScope {
    /// Convert ActiveAllocation to AllocationInfo (helper function)
    fn to_allocation_info(
        active: &crate::snapshot::ActiveAllocation,
    ) -> crate::capture::types::AllocationInfo {
        let thread_id_u64 = active.thread_id;
        // Note: Rust's ThreadId type cannot be reconstructed from u64.
        // We use the current thread's ID as a placeholder.
        // The actual thread ID value is available in thread_id_u64 field.
        // This is a known limitation that requires tracking ThreadId at allocation time.
        let thread_id = std::thread::current().id();

        // For Container/Value types, ptr is None (no real heap allocation).
        // We use 0 as a sentinel value, which indicates "not a real pointer".
        // Callers should check TrackKind to determine if this is a real allocation.
        let ptr = match active.kind {
            crate::core::types::TrackKind::HeapOwner { ptr, .. } => ptr,
            crate::core::types::TrackKind::StackOwner { heap_ptr, .. } => heap_ptr,
            crate::core::types::TrackKind::Container | crate::core::types::TrackKind::Value => 0,
        };

        crate::capture::types::AllocationInfo {
            ptr,
            size: active.size,
            var_name: active.var_name.clone(),
            type_name: active.type_name.clone(),
            scope_name: None,
            timestamp_alloc: active.allocated_at,
            timestamp_dealloc: None,
            thread_id,
            thread_id_u64,
            borrow_count: 0,
            stack_trace: None,
            is_leaked: false,
            lifetime_ms: None,
            module_path: None,
            borrow_info: None,
            clone_info: None,
            ownership_history_available: false,
            smart_pointer_info: None,
            memory_layout: None,
            generic_info: None,
            dynamic_type_info: None,
            runtime_state: None,
            stack_allocation: None,
            temporary_object: None,
            fragmentation_analysis: None,
            generic_instantiation: None,
            type_relationships: None,
            type_usage: None,
            function_call_tracking: None,
            lifecycle_tracking: None,
            access_tracking: None,
            drop_chain_analysis: None,
            stack_ptr: active.stack_ptr,
            task_id: None,
        }
    }

    /// Create a new MemScope instance
    ///
    /// This creates all engines with default settings and connects them
    /// together in the correct configuration.
    pub fn new() -> Self {
        // Create EventStore (the foundation)
        let event_store = Arc::new(EventStore::new());

        // Create Capture Engine
        let capture = Arc::new(CaptureEngine::new(
            CaptureBackendType::Unified,
            event_store.clone(),
        ));

        // Create Metadata Engine
        let metadata = Arc::new(MetadataEngine::new());

        // Create Snapshot Engine
        let snapshot = Arc::new(SnapshotEngine::new(event_store.clone()));

        // Create Query Engine
        let query = Arc::new(QueryEngine::new(snapshot.clone()));

        // Create Analysis Engine
        let analysis = Arc::new(Mutex::new(AnalysisEngine::new(snapshot.clone())));

        // Create Timeline Engine
        let timeline = Arc::new(TimelineEngine::new(event_store.clone()));

        // Create Render Engine
        let render = Arc::new(RenderEngine::new(snapshot.clone()));

        Self {
            event_store,
            capture,
            metadata,
            snapshot,
            query,
            analysis,
            timeline,
            render,
        }
    }

    /// Create a new MemScope instance with a specific capture backend
    ///
    /// # Arguments
    /// * `backend_type` - The type of capture backend to use
    pub fn with_backend(backend_type: CaptureBackendType) -> Self {
        // Create EventStore (the foundation)
        let event_store = Arc::new(EventStore::new());

        // Create Capture Engine with specified backend
        let capture = Arc::new(CaptureEngine::new(backend_type, event_store.clone()));

        // Create Metadata Engine
        let metadata = Arc::new(MetadataEngine::new());

        // Create Snapshot Engine
        let snapshot = Arc::new(SnapshotEngine::new(event_store.clone()));

        // Create Query Engine
        let query = Arc::new(QueryEngine::new(snapshot.clone()));

        // Create Analysis Engine
        let analysis = Arc::new(Mutex::new(AnalysisEngine::new(snapshot.clone())));

        // Create Timeline Engine
        let timeline = Arc::new(TimelineEngine::new(event_store.clone()));

        // Create Render Engine
        let render = Arc::new(RenderEngine::new(snapshot.clone()));

        Self {
            event_store,
            capture,
            metadata,
            snapshot,
            query,
            analysis,
            timeline,
            render,
        }
    }

    /// Register an analyzer with the analysis engine
    ///
    /// # Arguments
    /// * `analyzer` - The analyzer to register
    pub fn register_analyzer(&self, analyzer: Box<dyn Analyzer>) {
        if let Ok(mut analysis) = self.analysis.lock() {
            analysis.register_analyzer(analyzer);
            tracing::info!("Analyzer registered successfully");
        } else {
            tracing::error!("Failed to acquire analysis engine lock for registration");
        }
    }

    /// Get a summary of current memory usage
    pub fn summary(&self) -> crate::query::QueryResult {
        self.query.summary()
    }

    /// Get top allocations by size
    ///
    /// # Arguments
    /// * `limit` - Maximum number of allocations to return
    pub fn top_allocations(&self, limit: usize) -> crate::query::QueryResult {
        self.query.top_allocations(limit)
    }

    /// Render current snapshot to JSON
    ///
    /// # Arguments
    /// * `verbose` - Whether to include verbose output
    pub fn render_json(&self, verbose: bool) -> Result<crate::render_engine::RenderResult, String> {
        let snapshot = self.snapshot.build_snapshot();
        self.render.render_json(&snapshot, verbose)
    }

    /// Clear all events and reset state
    pub fn clear(&self) {
        self.event_store.clear();
    }

    /// Get the total number of events
    pub fn event_count(&self) -> usize {
        self.event_store.len()
    }

    // ===== Detector Methods =====

    /// Register a detector with the analysis engine
    ///
    /// This method automatically wraps the detector in an adapter
    /// and registers it as an analyzer.
    ///
    /// # Arguments
    /// * `detector` - The detector to register
    pub fn register_detector<D>(&self, detector: D)
    where
        D: crate::analysis::detectors::Detector + Send + Sync + 'static,
    {
        use crate::analysis_engine::DetectorToAnalyzer;
        let adapter = Box::new(DetectorToAnalyzer::new(detector));
        self.register_analyzer(adapter);
    }

    /// Run all registered detectors and return the results
    ///
    /// # Returns
    /// A vector of analysis results from all detectors
    pub fn run_detectors(&self) -> Vec<crate::analysis_engine::analyzer::AnalysisResult> {
        if let Ok(analysis) = self.analysis.lock() {
            analysis.analyze()
        } else {
            tracing::error!("Failed to acquire analysis engine lock");
            vec![]
        }
    }

    /// Run the leak detector on the current snapshot
    ///
    /// # Returns
    /// Detection results from the leak detector
    pub fn run_leak_detector(&self) -> crate::analysis::detectors::DetectionResult {
        use crate::analysis::detectors::{LeakDetector, LeakDetectorConfig};
        let detector = LeakDetector::new(LeakDetectorConfig::default());
        let snapshot = self.snapshot.build_snapshot();
        let allocations: Vec<crate::capture::types::AllocationInfo> = snapshot
            .active_allocations
            .values()
            .map(Self::to_allocation_info)
            .collect();
        detector.detect(&allocations)
    }

    /// Run the UAF (Use-After-Free) detector on the current snapshot
    ///
    /// # Returns
    /// Detection results from the UAF detector
    pub fn run_uaf_detector(&self) -> crate::analysis::detectors::DetectionResult {
        use crate::analysis::detectors::{UafDetector, UafDetectorConfig};
        let detector = UafDetector::new(UafDetectorConfig::default());
        let snapshot = self.snapshot.build_snapshot();
        let allocations: Vec<crate::capture::types::AllocationInfo> = snapshot
            .active_allocations
            .values()
            .map(Self::to_allocation_info)
            .collect();
        detector.detect(&allocations)
    }

    /// Run the overflow detector on the current snapshot
    ///
    /// # Returns
    /// Detection results from the overflow detector
    pub fn run_overflow_detector(&self) -> crate::analysis::detectors::DetectionResult {
        use crate::analysis::detectors::{OverflowDetector, OverflowDetectorConfig};
        let detector = OverflowDetector::new(OverflowDetectorConfig::default());
        let snapshot = self.snapshot.build_snapshot();
        let allocations: Vec<crate::capture::types::AllocationInfo> = snapshot
            .active_allocations
            .values()
            .map(Self::to_allocation_info)
            .collect();
        detector.detect(&allocations)
    }

    /// Run the safety detector on the current snapshot
    ///
    /// # Returns
    /// Detection results from the safety detector
    pub fn run_safety_detector(&self) -> crate::analysis::detectors::DetectionResult {
        use crate::analysis::detectors::{SafetyDetector, SafetyDetectorConfig};
        let detector = SafetyDetector::new(SafetyDetectorConfig::default());
        let snapshot = self.snapshot.build_snapshot();
        let allocations: Vec<crate::capture::types::AllocationInfo> = snapshot
            .active_allocations
            .values()
            .map(Self::to_allocation_info)
            .collect();
        detector.detect(&allocations)
    }

    /// Run the lifecycle detector on the current snapshot
    ///
    /// # Returns
    /// Detection results from the lifecycle detector
    pub fn run_lifecycle_detector(&self) -> crate::analysis::detectors::DetectionResult {
        use crate::analysis::detectors::{LifecycleDetector, LifecycleDetectorConfig};
        let detector = LifecycleDetector::new(LifecycleDetectorConfig::default());
        let snapshot = self.snapshot.build_snapshot();
        let allocations: Vec<crate::capture::types::AllocationInfo> = snapshot
            .active_allocations
            .values()
            .map(Self::to_allocation_info)
            .collect();
        detector.detect(&allocations)
    }

    // ===== Export Methods =====

    /// Export the current memory snapshot as an HTML dashboard with a specific template
    ///
    /// # Arguments
    /// * `path` - Directory path where the HTML file will be saved
    /// * `template` - The dashboard template to use
    ///
    /// # Returns
    /// Result indicating success or failure
    pub fn export_html_with_template<P: AsRef<std::path::Path>>(
        &self,
        path: P,
        template: crate::render_engine::export::DashboardTemplate,
    ) -> Result<(), String> {
        use crate::render_engine::export::export_dashboard_html_with_template;
        use crate::tracker::Tracker;
        use std::sync::Arc;

        // Create a new tracker instance for export
        let tracker = Tracker::new();
        let passport_tracker =
            Arc::new(crate::analysis::memory_passport_tracker::get_global_passport_tracker());

        export_dashboard_html_with_template(path, &tracker, &passport_tracker, template, None)
            .map_err(|e| format!("Failed to export HTML: {}", e))
    }

    /// Export the current memory snapshot as an HTML dashboard
    ///
    /// This method automatically detects program characteristics and selects
    /// the most appropriate template:
    /// - Multithread: If the program uses multiple threads
    /// - Binary: Default template for single-threaded programs
    ///
    /// # Arguments
    /// * `path` - Directory path where the HTML file will be saved
    ///
    /// # Returns
    /// Result indicating success or failure
    pub fn export_html<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), String> {
        use crate::render_engine::export::DashboardTemplate;

        let _snapshot = self.snapshot.build_snapshot();
        self.export_html_with_template(path, DashboardTemplate::Unified)
    }

    /// Export all JSON files
    ///
    /// This method exports 9 JSON files containing comprehensive memory analysis:
    /// - memory_analysis.json: Complete memory allocation analysis
    /// - lifetime.json: Ownership and lifetime tracking
    /// - ownership_graph.json: Ownership graph analysis with cycle detection
    /// - system_resources.json: System resource monitoring
    /// - thread_analysis.json: Thread-specific memory stats
    /// - unsafe_ffi.json: Unsafe FFI boundary tracking
    /// - memory_passports.json: Memory lifecycle passports
    /// - leak_detection.json: Potential memory leaks
    /// - async_analysis.json: Async task analysis
    ///
    /// # Arguments
    /// * `path` - Directory path where JSON files will be saved
    ///
    /// # Returns
    /// Result indicating success or failure
    pub fn export_json<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), String> {
        use crate::render_engine::export::export_all_json;
        use crate::tracker::Tracker;
        use std::sync::Arc;

        let tracker = Tracker::new();
        let passport_tracker =
            Arc::new(crate::analysis::memory_passport_tracker::get_global_passport_tracker());
        let async_tracker = Arc::new(crate::capture::backends::async_tracker::AsyncTracker::new());

        export_all_json(path, &tracker, &passport_tracker, &async_tracker)
            .map_err(|e| format!("Failed to export JSON files: {}", e))
    }
}

impl Default for MemScope {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_memscope_creation() {
        let memscope = MemScope::new();
        assert_eq!(memscope.event_count(), 0);
    }

    #[test]
    fn test_memscope_default() {
        let memscope = MemScope::default();
        assert_eq!(memscope.event_count(), 0);
    }

    #[test]
    fn test_memscope_with_backend() {
        let memscope = MemScope::with_backend(CaptureBackendType::Core);
        assert_eq!(memscope.event_count(), 0);
    }

    #[test]
    fn test_summary() {
        let memscope = MemScope::new();
        let result = memscope.summary();
        match result {
            crate::query::QueryResult::Summary(_) => (),
            _ => panic!("Expected summary result"),
        }
    }

    #[test]
    fn test_top_allocations() {
        let memscope = MemScope::new();
        let result = memscope.top_allocations(10);
        match result {
            crate::query::QueryResult::Allocations(_) => (),
            _ => panic!("Expected allocations result"),
        }
    }

    #[test]
    fn test_render_json() {
        let memscope = MemScope::new();
        let result = memscope.render_json(false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_clear() {
        let memscope = MemScope::new();
        // Simulate some events
        memscope.capture.capture_alloc(0x1000, 1024, 1);
        assert_eq!(memscope.event_count(), 1);

        memscope.clear();
        assert_eq!(memscope.event_count(), 0);
    }
}