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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#![allow(warnings, unused)]

use std::collections::HashMap;
use std::path::PathBuf;
use std::time::{Duration, Instant};

/// Platform-specific symbol resolution
pub struct PlatformSymbolResolver {
    /// Resolver configuration
    config: ResolverConfig,
    /// Symbol cache
    symbol_cache: HashMap<usize, CachedSymbol>,
    /// Module information cache
    module_cache: HashMap<usize, ModuleInfo>,
    /// Performance statistics
    stats: ResolverStats,
    /// Platform-specific context
    platform_context: ResolverContext,
}

/// Configuration for symbol resolution
#[derive(Debug, Clone)]
pub struct ResolverConfig {
    /// Whether to enable symbol caching
    pub enable_caching: bool,
    /// Maximum cache size
    pub max_cache_size: usize,
    /// Search paths for debug symbols
    pub symbol_search_paths: Vec<PathBuf>,
    /// Whether to perform demangling
    pub enable_demangling: bool,
    /// Maximum time to spend resolving a symbol
    pub max_resolve_time: Duration,
    /// Whether to load symbols eagerly
    pub eager_loading: bool,
}

/// Platform-specific resolver context
#[derive(Debug)]
struct ResolverContext {
    /// Whether resolver is initialized
    initialized: bool,

    #[cfg(target_os = "linux")]
    linux_context: LinuxResolverContext,

    #[cfg(target_os = "windows")]
    windows_context: WindowsResolverContext,

    #[cfg(target_os = "macos")]
    macos_context: MacOSResolverContext,
}

/// Linux-specific resolver context
#[cfg(target_os = "linux")]
#[derive(Debug)]
struct LinuxResolverContext {
    /// Whether addr2line is available
    addr2line_available: bool,
    /// Whether DWARF debug info is loaded
    dwarf_loaded: bool,
}

/// Windows-specific resolver context
#[cfg(target_os = "windows")]
#[derive(Debug)]
struct WindowsResolverContext {
    /// Whether symbol APIs are initialized
    symbols_initialized: bool,
    /// Whether PDB files are loaded
    pdb_loaded: bool,
    /// Symbol search paths
    symbol_paths: Vec<PathBuf>,
}

/// macOS-specific resolver context
#[cfg(target_os = "macos")]
#[derive(Debug)]
struct MacOSResolverContext {
    /// Whether atos utility is available
    atos_available: bool,
    /// Whether dSYM files are loaded
    dsym_loaded: bool,
}

/// Cached symbol information
#[derive(Debug, Clone)]
struct CachedSymbol {
    /// Symbol information
    symbol: SymbolInfo,
    /// Access count
    access_count: usize,
}

/// Detailed symbol information
#[derive(Debug, Clone)]
pub struct SymbolInfo {
    /// Symbol name
    pub name: String,
    /// Demangled name if available
    pub demangled_name: Option<String>,
    /// Source file path
    pub file_path: Option<PathBuf>,
    /// Line number in source
    pub line_number: Option<u32>,
    /// Column number
    pub column_number: Option<u32>,
    /// Function start address
    pub function_start: Option<usize>,
    /// Function size
    pub function_size: Option<usize>,
    /// Module/library name
    pub module_name: Option<String>,
    /// Compilation unit
    pub compilation_unit: Option<String>,
}

/// Module/library information
#[derive(Debug, Clone)]
pub struct ModuleInfo {
    /// Module name
    pub name: String,
    /// Base address where module is loaded
    pub base_address: usize,
    /// Size of module
    pub size: usize,
    /// Path to module file
    pub file_path: PathBuf,
    /// Whether debug symbols are available
    pub has_symbols: bool,
    /// Symbol file path if separate
    pub symbol_file: Option<PathBuf>,
}

/// Performance statistics for symbol resolution
#[derive(Debug)]
struct ResolverStats {
    /// Total resolution attempts
    total_resolutions: std::sync::atomic::AtomicUsize,
    /// Successful resolutions
    successful_resolutions: std::sync::atomic::AtomicUsize,
    /// Cache hits
    cache_hits: std::sync::atomic::AtomicUsize,
    /// Total resolution time
    total_resolve_time: std::sync::atomic::AtomicU64,
}

/// Errors that can occur during symbol resolution
#[derive(Debug, Clone, PartialEq)]
pub enum ResolveError {
    /// Platform not supported
    UnsupportedPlatform,
    /// Symbol not found
    SymbolNotFound,
    /// Debug information not available
    NoDebugInfo,
    /// File access error
    FileAccessError(String),
    /// Parse error
    ParseError(String),
    /// Timeout during resolution
    Timeout,
    /// Memory error
    MemoryError,
    /// Unknown error
    Unknown(String),
}

impl PlatformSymbolResolver {
    /// Create new symbol resolver
    pub fn new() -> Self {
        Self {
            config: ResolverConfig::default(),
            symbol_cache: HashMap::new(),
            module_cache: HashMap::new(),
            stats: ResolverStats::new(),
            platform_context: ResolverContext::new(),
        }
    }

    /// Initialize symbol resolver for current platform
    pub fn initialize(&mut self) -> Result<(), ResolveError> {
        #[cfg(target_os = "linux")]
        {
            self.initialize_linux()
        }

        #[cfg(target_os = "windows")]
        {
            self.initialize_windows()
        }

        #[cfg(target_os = "macos")]
        {
            self.initialize_macos()
        }

        #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
        {
            Err(ResolveError::UnsupportedPlatform)
        }
    }

    /// Resolve symbol for given address
    pub fn resolve_symbol(&mut self, address: usize) -> Result<SymbolInfo, ResolveError> {
        let start_time = Instant::now();

        // Check cache first
        if self.config.enable_caching {
            if let Some(cached) = self.symbol_cache.get_mut(&address) {
                cached.access_count += 1;
                self.stats
                    .cache_hits
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                return Ok(cached.symbol.clone());
            }
        }

        // Perform resolution
        self.stats
            .total_resolutions
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        let result = self.perform_resolution(address);
        let resolve_time = start_time.elapsed();

        // Update statistics
        self.stats.total_resolve_time.fetch_add(
            resolve_time.as_nanos() as u64,
            std::sync::atomic::Ordering::Relaxed,
        );

        if let Ok(symbol) = &result {
            self.stats
                .successful_resolutions
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

            // Cache successful resolution
            if self.config.enable_caching && self.symbol_cache.len() < self.config.max_cache_size {
                self.symbol_cache.insert(
                    address,
                    CachedSymbol {
                        symbol: symbol.clone(),
                        access_count: 1,
                    },
                );
            }
        }

        result
    }

    /// Resolve symbols for multiple addresses in batch
    pub fn resolve_batch(&mut self, addresses: &[usize]) -> Vec<Result<SymbolInfo, ResolveError>> {
        addresses
            .iter()
            .map(|&addr| self.resolve_symbol(addr))
            .collect()
    }

    /// Get module information for address
    pub fn get_module_info(&mut self, address: usize) -> Option<ModuleInfo> {
        // Check cache first
        for (base_addr, module) in &self.module_cache {
            if address >= *base_addr && address < (*base_addr + module.size) {
                return Some(module.clone());
            }
        }

        // Load module info
        self.load_module_info(address)
    }

    /// Get resolver statistics
    pub fn get_statistics(&self) -> ResolverStatistics {
        let total = self
            .stats
            .total_resolutions
            .load(std::sync::atomic::Ordering::Relaxed);
        let successful = self
            .stats
            .successful_resolutions
            .load(std::sync::atomic::Ordering::Relaxed);
        let cache_hits = self
            .stats
            .cache_hits
            .load(std::sync::atomic::Ordering::Relaxed);
        let total_time_ns = self
            .stats
            .total_resolve_time
            .load(std::sync::atomic::Ordering::Relaxed);

        ResolverStatistics {
            total_resolutions: total,
            successful_resolutions: successful,
            failed_resolutions: total.saturating_sub(successful),
            cache_hits,
            cache_misses: total.saturating_sub(cache_hits),
            cache_hit_rate: if total > 0 {
                cache_hits as f64 / total as f64
            } else {
                0.0
            },
            success_rate: if total > 0 {
                successful as f64 / total as f64
            } else {
                0.0
            },
            average_resolve_time: if total > 0 {
                Duration::from_nanos(total_time_ns / total as u64)
            } else {
                Duration::ZERO
            },
            current_cache_size: self.symbol_cache.len(),
        }
    }

    /// Clear symbol cache
    pub fn clear_cache(&mut self) {
        self.symbol_cache.clear();
    }

    /// Update resolver configuration
    pub fn update_config(&mut self, config: ResolverConfig) {
        self.config = config;
    }

    fn perform_resolution(&self, address: usize) -> Result<SymbolInfo, ResolveError> {
        if !self.platform_context.initialized {
            return Err(ResolveError::NoDebugInfo);
        }

        #[cfg(target_os = "linux")]
        return self.resolve_linux_symbol(address);

        #[cfg(target_os = "windows")]
        return self.resolve_windows_symbol(address);

        #[cfg(target_os = "macos")]
        return self.resolve_macos_symbol(address);

        #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
        Err(ResolveError::UnsupportedPlatform)
    }

    #[cfg(target_os = "linux")]
    fn initialize_linux(&mut self) -> Result<(), ResolveError> {
        // Initialize Linux symbol resolution using addr2line, DWARF, etc.
        self.platform_context.linux_context.addr2line_available = true; // Simplified
        self.platform_context.linux_context.dwarf_loaded = true; // Simplified
        self.platform_context.initialized = true;
        Ok(())
    }

    #[cfg(target_os = "windows")]
    fn initialize_windows(&mut self) -> Result<(), ResolveError> {
        // Initialize Windows symbol resolution using dbghelp.dll
        self.platform_context.windows_context.symbols_initialized = true; // Simplified
        self.platform_context.windows_context.pdb_loaded = true; // Simplified
        self.platform_context.initialized = true;
        Ok(())
    }

    #[cfg(target_os = "macos")]
    fn initialize_macos(&mut self) -> Result<(), ResolveError> {
        // Initialize macOS symbol resolution using atos, dSYM files
        self.platform_context.macos_context.atos_available = true; // Simplified
        self.platform_context.macos_context.dsym_loaded = true; // Simplified
        self.platform_context.initialized = true;
        Ok(())
    }

    #[cfg(target_os = "linux")]
    fn resolve_linux_symbol(&self, address: usize) -> Result<SymbolInfo, ResolveError> {
        // Linux symbol resolution using dladdr API
        use std::ffi::CStr;

        let addr = address as *const libc::c_void;

        unsafe {
            let mut info: libc::Dl_info = std::mem::zeroed();

            if libc::dladdr(addr, &mut info) == 0 {
                return Err(ResolveError::SymbolNotFound);
            }

            let name = if !info.dli_sname.is_null() {
                CStr::from_ptr(info.dli_sname).to_string_lossy().to_string()
            } else {
                format!("unknown_0x{:x}", address)
            };

            let file_path = if !info.dli_fname.is_null() {
                Some(PathBuf::from(
                    CStr::from_ptr(info.dli_fname).to_string_lossy().to_string(),
                ))
            } else {
                None
            };

            let base_address = info.dli_fbase as usize;
            let _offset = address.saturating_sub(base_address);

            // Extract module_name before moving file_path
            let module_name = file_path
                .as_ref()
                .and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()));

            Ok(SymbolInfo {
                name: name.clone(),
                demangled_name: Some(name), // dladdr returns demangled names on Linux
                file_path,
                line_number: None, // dladdr doesn't provide line numbers, would need DWARF parsing
                column_number: None,
                function_start: Some(base_address),
                function_size: None, // Not available from dladdr
                module_name,
                compilation_unit: None, // Not available from dladdr
            })
        }
    }

    #[cfg(target_os = "windows")]
    fn resolve_windows_symbol(&self, address: usize) -> Result<SymbolInfo, ResolveError> {
        use windows_sys::Win32::Foundation::GetLastError;
        use windows_sys::Win32::System::Diagnostics::Debug::{
            SymCleanup, SymFromAddrW, SymGetModuleBase64, SymInitializeW, SymSetContext,
            SYMBOL_INFO,
        };
        use windows_sys::Win32::System::ProcessStatus::EnumProcessModules;

        let process: windows_sys::Win32::Foundation::HANDLE = -1isize as _;

        unsafe {
            if SymInitializeW(process, std::ptr::null(), 1) == 0 {
                return Err(ResolveError::Unknown(format!(
                    "SymInitializeW failed: {}",
                    GetLastError()
                )));
            }

            let mut symbol_info: SYMBOL_INFO = std::mem::zeroed();
            symbol_info.SizeOfStruct = std::mem::size_of::<SYMBOL_INFO>() as u32;
            symbol_info.MaxNameLen = 256;

            let mut displacement = 0u64;

            let result = if SymFromAddrW(
                process,
                address as u64,
                &mut displacement,
                &mut symbol_info as *mut SYMBOL_INFO as *mut _,
            ) == 0
            {
                Err(ResolveError::SymbolNotFound)
            } else {
                let name = if symbol_info.Name[0] != 0 {
                    let name_len = symbol_info.NameLen as usize;
                    let name_bytes = &symbol_info.Name[..name_len.min(symbol_info.Name.len())];
                    // Convert i8 to u16 for Windows API
                    let name_u16: Vec<u16> = name_bytes.iter().map(|&b| b as u16).collect();
                    String::from_utf16_lossy(&name_u16)
                } else {
                    format!("unknown_0x{:x}", address)
                };

                let module_base = SymGetModuleBase64(process, address as u64);

                Ok(SymbolInfo {
                    name: name.clone(),
                    demangled_name: Some(name),
                    file_path: None,
                    line_number: None,
                    column_number: None,
                    function_start: Some(symbol_info.Address as usize),
                    function_size: None,
                    module_name: if module_base != 0 {
                        Some(format!("module_0x{:x}", module_base))
                    } else {
                        None
                    },
                    compilation_unit: None,
                })
            };

            SymCleanup(process);

            result
        }
    }

    #[cfg(target_os = "macos")]
    fn resolve_macos_symbol(&self, address: usize) -> Result<SymbolInfo, ResolveError> {
        // macOS symbol resolution using dladdr API
        use std::ffi::CStr;

        let addr = address as *const libc::c_void;

        unsafe {
            let mut info: libc::Dl_info = std::mem::zeroed();

            if libc::dladdr(addr, &mut info) == 0 {
                return Err(ResolveError::SymbolNotFound);
            }

            let name = if !info.dli_sname.is_null() {
                CStr::from_ptr(info.dli_sname).to_string_lossy().to_string()
            } else {
                format!("unknown_0x{:x}", address)
            };

            let file_path = if !info.dli_fname.is_null() {
                Some(PathBuf::from(
                    CStr::from_ptr(info.dli_fname).to_string_lossy().to_string(),
                ))
            } else {
                None
            };

            let base_address = info.dli_fbase as usize;
            let _offset = address.saturating_sub(base_address);

            // Extract module_name before moving file_path
            let module_name = file_path
                .as_ref()
                .and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()));

            Ok(SymbolInfo {
                name: name.clone(),
                demangled_name: Some(name), // dladdr returns demangled names on macOS
                file_path: file_path.clone(),
                line_number: None, // dladdr doesn't provide line numbers, would need dSYM + atos
                column_number: None,
                function_start: Some(base_address),
                function_size: None, // Not available from dladdr
                module_name,
                compilation_unit: None, // Not available from dladdr
            })
        }
    }

    fn load_module_info(&mut self, _address: usize) -> Option<ModuleInfo> {
        // Platform-specific module loading
        // This feature is not yet implemented - would require:
        // - Linux: reading /proc/self/maps or dl_iterate_phdr
        // - Windows: EnumProcessModules or CreateToolhelp32Snapshot
        // - macOS: _dyld_image_count and _dyld_get_image_name
        None
    }
}

impl ResolverContext {
    fn new() -> Self {
        Self {
            initialized: false,
            #[cfg(target_os = "linux")]
            linux_context: LinuxResolverContext {
                addr2line_available: false,
                dwarf_loaded: false,
            },
            #[cfg(target_os = "windows")]
            windows_context: WindowsResolverContext {
                symbols_initialized: false,
                pdb_loaded: false,
                symbol_paths: Vec::new(),
            },
            #[cfg(target_os = "macos")]
            macos_context: MacOSResolverContext {
                atos_available: false,
                dsym_loaded: false,
            },
        }
    }
}

impl ResolverStats {
    fn new() -> Self {
        Self {
            total_resolutions: std::sync::atomic::AtomicUsize::new(0),
            successful_resolutions: std::sync::atomic::AtomicUsize::new(0),
            cache_hits: std::sync::atomic::AtomicUsize::new(0),
            total_resolve_time: std::sync::atomic::AtomicU64::new(0),
        }
    }
}

/// Statistics about symbol resolution performance
#[derive(Debug, Clone)]
pub struct ResolverStatistics {
    /// Total resolution attempts
    pub total_resolutions: usize,
    /// Number of successful resolutions
    pub successful_resolutions: usize,
    /// Number of failed resolutions
    pub failed_resolutions: usize,
    /// Number of cache hits
    pub cache_hits: usize,
    /// Number of cache misses
    pub cache_misses: usize,
    /// Cache hit rate (0.0 to 1.0)
    pub cache_hit_rate: f64,
    /// Resolution success rate (0.0 to 1.0)
    pub success_rate: f64,
    /// Average time per resolution
    pub average_resolve_time: Duration,
    /// Current cache size
    pub current_cache_size: usize,
}

impl Default for ResolverConfig {
    fn default() -> Self {
        Self {
            enable_caching: true,
            max_cache_size: 10000,
            symbol_search_paths: Vec::new(),
            enable_demangling: true,
            max_resolve_time: Duration::from_millis(100),
            eager_loading: false,
        }
    }
}

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

impl std::fmt::Display for ResolveError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ResolveError::UnsupportedPlatform => {
                write!(f, "Platform not supported for symbol resolution")
            }
            ResolveError::SymbolNotFound => write!(f, "Symbol not found"),
            ResolveError::NoDebugInfo => write!(f, "Debug information not available"),
            ResolveError::FileAccessError(msg) => write!(f, "File access error: {}", msg),
            ResolveError::ParseError(msg) => write!(f, "Parse error: {}", msg),
            ResolveError::Timeout => write!(f, "Symbol resolution timed out"),
            ResolveError::MemoryError => write!(f, "Memory error during symbol resolution"),
            ResolveError::Unknown(msg) => write!(f, "Unknown error: {}", msg),
        }
    }
}

impl std::error::Error for ResolveError {}

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

    #[test]
    fn test_symbol_resolver_creation() {
        let resolver = PlatformSymbolResolver::new();
        assert!(!resolver.platform_context.initialized);
        assert!(resolver.symbol_cache.is_empty());

        let stats = resolver.get_statistics();
        assert_eq!(stats.total_resolutions, 0);
        assert_eq!(stats.cache_hit_rate, 0.0);
    }

    #[test]
    fn test_resolver_initialization() {
        let mut resolver = PlatformSymbolResolver::new();
        let result = resolver.initialize();

        #[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
        assert!(result.is_ok());

        #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
        assert_eq!(result, Err(ResolveError::UnsupportedPlatform));
    }

    #[test]
    fn test_symbol_resolution() {
        let mut resolver = PlatformSymbolResolver::new();
        let _ = resolver.initialize();

        // Use real function address instead of hardcoded value
        let address = test_symbol_resolution as *const () as usize;
        let result = resolver.resolve_symbol(address);

        #[cfg(target_os = "linux")]
        {
            if resolver.platform_context.initialized {
                // On Linux, dladdr should find at least some info
                // Line numbers may not be available from dladdr
                match result {
                    Ok(symbol) => {
                        assert!(!symbol.name.is_empty());
                        // Line number is optional from dladdr
                    }
                    Err(_) => {
                        // Symbol resolution may fail in some environments
                        // This is acceptable as long as the error is handled gracefully
                    }
                }
            }
        }
    }

    #[test]
    fn test_symbol_caching() {
        let mut resolver = PlatformSymbolResolver::new();
        let _ = resolver.initialize();

        // Use real function address
        let address = test_symbol_caching as *const () as usize;

        let _ = resolver.resolve_symbol(address);
        let stats1 = resolver.get_statistics();

        let _ = resolver.resolve_symbol(address);
        let stats2 = resolver.get_statistics();

        #[cfg(target_os = "linux")]
        {
            if resolver.platform_context.initialized {
                // Cache hits should increase after second call
                assert!(stats2.cache_hits >= stats1.cache_hits);
            }
        }
    }

    #[test]
    fn test_batch_resolution() {
        let mut resolver = PlatformSymbolResolver::new();
        let _ = resolver.initialize();

        // Use real function addresses
        let addresses = vec![
            test_batch_resolution as *const () as usize,
            test_symbol_caching as *const () as usize,
            test_module_info as *const () as usize,
        ];
        let results = resolver.resolve_batch(&addresses);

        assert_eq!(results.len(), 3);

        #[cfg(target_os = "linux")]
        {
            if resolver.platform_context.initialized {
                // At least some symbols should resolve successfully
                let success_count = results.iter().filter(|r| r.is_ok()).count();
                assert!(success_count > 0, "At least one symbol should resolve");
            }
        }
    }

    #[test]
    fn test_module_info() {
        let mut resolver = PlatformSymbolResolver::new();
        let _ = resolver.initialize();

        // Use real function address
        let address = test_module_info as *const () as usize;
        let module = resolver.get_module_info(address);

        #[cfg(target_os = "linux")]
        {
            if resolver.platform_context.initialized {
                // Module info may or may not be available depending on dladdr
                if let Some(module) = module {
                    assert!(!module.name.is_empty());
                    assert!(module.size > 0);
                }
                // It's also acceptable if module info is not available
            }
        }
    }
}