MaeBPF.Globals (maebpf v0.1.0)

Copy Markdown View Source

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

entry()

@type entry() :: %{
  name: String.t(),
  map_name: String.t(),
  offset: non_neg_integer(),
  size: non_neg_integer(),
  type: global_type(),
  readonly: boolean()
}

global_type()

@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

apply_globals(object, overrides, opts \\ [])

@spec apply_globals(term(), map() | keyword(), keyword()) :: :ok | {:error, term()}

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 - when true, skips keys absent from object BTF (default false, failing with {:error, {:unknown_global, name, available}})
  • :layout - optional pre-fetched %{name => entry} map
  • :read_initial - optional fn(obj, map_name) -> {:ok, bin} | {:error, term()}

  • :write_initial - optional fn(obj, map_name, bin) -> :ok | {:error, term()}

decode_value(type, expected_size, bin)

@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>>)

encode_value(type, expected_size, value)

@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")

get_global(object, name, opts \\ [])

@spec get_global(term(), atom() | String.t(), keyword()) ::
  {:ok, term()} | {:error, term()}

Reads and decodes a single global variable from a loaded object.

{:ok, "nginx"} = MaeBPF.Globals.get_global(obj, :target_comm)

get_globals(object, opts \\ [])

@spec get_globals(term(), keyword()) ::
  {:ok, %{required(String.t()) => term()}} | {:error, term()}

Reads and decodes all BTF-described global variables from a loaded object.

{:ok, %{"enabled" => true, "target_pid" => 1234}} = MaeBPF.Globals.get_globals(obj)

info(object)

@spec info(reference()) ::
  {:ok, %{required(String.t()) => entry()}} | {:error, term()}

Returns a map of %{global_name => entry} indexed by string variable name.

{:ok, %{"target_pid" => entry}} = MaeBPF.Globals.info(obj)

list(object)

@spec list(reference()) :: {:ok, [entry()]} | {:error, term()}

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)

normalize_overrides(overrides)

@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()}.

put_global(object, name, value, opts \\ [])

@spec put_global(term(), atom() | String.t(), term(), keyword()) ::
  :ok | {:error, 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)