Skip to main content

get_module_info

Macro get_module_info 

Source
macro_rules! get_module_info {
    (@__extract $symbol:ident) => { ... };
    (@__add_to_map $info_map:ident, $symbol:ident, $key:literal) => { ... };
    (ModuleInfoField::Binary) => { ... };
    (ModuleInfoField::Version) => { ... };
    (ModuleInfoField::ModuleVersion) => { ... };
    (ModuleInfoField::Maintainer) => { ... };
    (ModuleInfoField::Name) => { ... };
    (ModuleInfoField::Type) => { ... };
    (ModuleInfoField::Repo) => { ... };
    (ModuleInfoField::Branch) => { ... };
    (ModuleInfoField::Hash) => { ... };
    (ModuleInfoField::Copyright) => { ... };
    (ModuleInfoField::Os) => { ... };
    (ModuleInfoField::OsVersion) => { ... };
    () => { ... };
}
Expand description

Read the embedded metadata at runtime.

Two call shapes:

  • get_module_info!() returns ModuleInfoResult<HashMap<String, String>> covering every field that resolved. Fields disabled at build time appear with value "". Fields whose symbols failed to resolve (.note.package stripped from the binary, or no module_info::embed!() invocation reached the linker) are omitted from the map, so check contains_key before assuming a key is present.
  • get_module_info!(ModuleInfoField::Binary) returns ModuleInfoResult<String> for a single field. Disabled fields yield Ok(""); unresolved symbols yield Err(ModuleInfoError::NotAvailable).

If the process has already crashed, the same data is reachable via coredumpctl info, readelf -n, or WinDbg against the core dump, without going through this macro.

§Examples

Retrieve all module info:

use module_info::{get_module_info, ModuleInfoResult};

fn print_all() -> ModuleInfoResult<()> {
    for (key, value) in get_module_info!()? {
        println!("{key}: {value}");
    }
    Ok(())
}
use module_info::{get_module_info, ModuleInfoResult};

fn binary_name() -> ModuleInfoResult<String> {
    get_module_info!(ModuleInfoField::Binary)
}

ModuleInfoField::Foo is matched as tokens by the macro, so the enum itself does not need to be imported when it appears only inside get_module_info!(...).

§Safety

The macro reads extern "C" static: u8 symbols emitted by the build script’s linker script. The symbols are placed inside the read-only .note.package payload by the linker; the macro takes their address and hands it to crate::extract_module_info, which scans for the terminating NUL. Memory is never written, and the bound check inside extract_module_info keeps a stripped or corrupted section from reading past the cap.

§Availability

This form is active when embed-module-info is enabled and the target is Linux. The fallback form (declared below) is active otherwise and returns ModuleInfoError::NotAvailable for every variant.