BTF-driven pre-load patching and post-load read/write access for eBPF
.rodata, .data, and .bss global variables.
{:ok, srv} =
MaeBPF.attach({:c, src}, :auto,
globals: %{target_pid: 1234, enabled: true, target_comm: "nginx", ports: [80, 443]}
)
{:ok, 1234} = MaeBPF.get_global(srv, :target_pid)
:ok = MaeBPF.put_global(srv, :enabled, false)
Summary
Functions
Patches global variable overrides into an opened (unloaded) object's internal
.rodata, .data, and .bss maps prior to bpf_object__load.
Decodes exact bpfel bytes into an Elixir value for a BTF global variable
type.
Encodes an Elixir value into exact little-endian bpfel bytes for a BTF
global variable type.
Reads and decodes a single global variable from a loaded object.
Reads and decodes all BTF-described global variables from a loaded object.
Returns a map of %{global_name => entry} indexed by string variable name.
Lists all BTF-described global variables in an opened or loaded object.
Normalizes a globals override map or keyword list into %{String.t() => term()}.
Updates a mutable (.data or .bss) global variable on a loaded object via
read-modify-write.
Types
@type entry() :: %{ name: String.t(), map_name: String.t(), offset: non_neg_integer(), size: non_neg_integer(), type: global_type(), readonly: boolean() }
@type global_type() :: :bool | :u8 | :u16 | :u32 | :u64 | :i8 | :i16 | :i32 | :i64 | :be16 | :be32 | :be64 | {:string, pos_integer()} | {:bytes, pos_integer()} | {:array, global_type(), pos_integer()} | {:raw, non_neg_integer()}
Functions
Patches global variable overrides into an opened (unloaded) object's internal
.rodata, .data, and .bss maps prior to bpf_object__load.
:ok = MaeBPF.Globals.apply_globals(obj, %{target_pid: 42, target_comm: "sshd"})Options:
:ignore_unknown- whentrue, skips keys absent from object BTF (defaultfalse, failing with{:error, {:unknown_global, name, available}}):layout- optional pre-fetched%{name => entry}map:read_initial- optionalfn(obj, map_name) -> {:ok, bin} | {:error, term()}:write_initial- optionalfn(obj, map_name, bin) -> :ok | {:error, term()}
@spec decode_value(global_type(), non_neg_integer(), binary()) :: {:ok, term()} | {:error, term()}
Decodes exact bpfel bytes into an Elixir value for a BTF global variable
type.
{:ok, "nginx"} = MaeBPF.Globals.decode_value({:string, 8}, 8, <<"nginx", 0, 0, 0>>)
@spec encode_value(global_type(), non_neg_integer(), term()) :: {:ok, binary()} | {:error, term()}
Encodes an Elixir value into exact little-endian bpfel bytes for a BTF
global variable type.
{:ok, <<1>>} = MaeBPF.Globals.encode_value(:bool, 1, true)
{:ok, <<"nginx", 0, 0, 0>>} = MaeBPF.Globals.encode_value({:string, 8}, 8, "nginx")
Reads and decodes a single global variable from a loaded object.
{:ok, "nginx"} = MaeBPF.Globals.get_global(obj, :target_comm)
Reads and decodes all BTF-described global variables from a loaded object.
{:ok, %{"enabled" => true, "target_pid" => 1234}} = MaeBPF.Globals.get_globals(obj)
Returns a map of %{global_name => entry} indexed by string variable name.
{:ok, %{"target_pid" => entry}} = MaeBPF.Globals.info(obj)
Lists all BTF-described global variables in an opened or loaded object.
{:ok, [%{name: "target_pid", map_name: ".rodata", offset: 0, size: 4, type: :u32, readonly: true}]} =
MaeBPF.Globals.list(obj)
@spec normalize_overrides(map() | keyword() | nil) :: {:ok, %{required(String.t()) => term()}} | {:error, term()}
Normalizes a globals override map or keyword list into %{String.t() => term()}.
Updates a mutable (.data or .bss) global variable on a loaded object via
read-modify-write.
Rejects .rodata globals with {:error, {:readonly_global, name, hint}}.
:ok = MaeBPF.Globals.put_global(obj, :enabled, true)