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
use std::error::Error;
use std::fmt;

#[cfg(feature = "jitdump")]
mod jitdump;

#[cfg(feature = "jitdump")]
pub use crate::jitdump::JitDumpAgent;

#[cfg(not(feature = "jitdump"))]
pub type JitDumpAgent = NullProfilerAgent;

/// Select which profiling technique to use
#[derive(Debug, Clone, Copy)]
pub enum ProfilingStrategy {
    /// No profiler support
    NullProfiler,

    /// Collect profile for jitdump file format
    JitDumpProfiler,
}

/// Common interface for profiling tools.
pub trait ProfilingAgent {
    /// Notify the profiler of a new module loaded into memory
    fn module_load(
        &mut self,
        module_name: &str,
        addr: *const u8,
        len: usize,
        dbg_image: Option<&[u8]>,
    ) -> ();
}

/// Default agent for unsupported profiling build.
#[derive(Debug, Default, Clone, Copy)]
pub struct NullProfilerAgent {}

#[derive(Debug)]
struct NullProfilerAgentError;

impl fmt::Display for NullProfilerAgentError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "A profiler agent is not supported by this build")
    }
}

// This is important for other errors to wrap this one.
impl Error for NullProfilerAgentError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        // Generic error, underlying cause isn't tracked.
        None
    }
}

impl ProfilingAgent for NullProfilerAgent {
    fn module_load(
        &mut self,
        _module_name: &str,
        _addr: *const u8,
        _len: usize,
        _dbg_image: Option<&[u8]>,
    ) -> () {
    }
}