# `MaeBPF.Globals`
[🔗](https://github.com/cocoa-xu/MaeBPF/blob/v0.1.0/maebpf/lib/maebpf/globals.ex#L1)

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)

# `entry`

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

# `global_type`

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

# `apply_globals`

```elixir
@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`

```elixir
@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`

```elixir
@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`

```elixir
@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`

```elixir
@spec get_globals(term(), keyword()) ::
  {:ok, %{required(String.t()) =&gt; 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`

```elixir
@spec info(reference()) ::
  {:ok, %{required(String.t()) =&gt; entry()}} | {:error, term()}
```

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

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

# `list`

```elixir
@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`

```elixir
@spec normalize_overrides(map() | keyword() | nil) ::
  {:ok, %{required(String.t()) =&gt; term()}} | {:error, term()}
```

Normalizes a globals override map or keyword list into `%{String.t() => term()}`.

# `put_global`

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
