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

Ergonomic target validation and resolution for `MaeBPF` kernel attachments.

    {:ok, {:cgroup, "/sys/fs/cgroup/system.slice/nginx.service"}} =
      MaeBPF.Target.resolve({:systemd, "nginx.service"})

    {:ok, "/lib/aarch64-linux-gnu/libssl.so.3"} =
      MaeBPF.Target.find_process_library(os_pid, "libssl.so")

Supported targets:
  * `:none` - load without attaching
  * `:auto` - auto-attach via `bpf_program__attach` using ELF `SEC(...)` metadata
  * `{:xdp, ifspec}` / `{:xdp, ifspec, opts}`
  * `{:tcx, ifspec, :ingress | :egress}` / with `opts`
  * `{:cgroup, path}` | `{:systemd, unit}` | `{:pid, os_pid}`
  * `{:tracepoint, category, name}` / with `opts`
  * `{:raw_tracepoint, name}` / with `opts`
  * `{:kprobe | :kretprobe, fn_name}` / with `opts`
  * `{:uprobe | :uretprobe, bin_path, sym_or_off}` / with `opts`
  * `{:fentry | :fexit | :fmod_ret, fn_name}` / with `opts`
  * `{:lsm, hook}` / with `opts`
  * `{:freplace, target_prog_ref, fn_name}` / with `opts`
  * `:iter` | `{:iter, opts}`

# `direction`

```elixir
@type direction() :: :ingress | :egress
```

# `freplace_target`

```elixir
@type freplace_target() ::
  pos_integer()
  | {reference(), String.t()}
  | {pid() | atom(), atom() | String.t()}
  | pid()
```

# `ifspec`

```elixir
@type ifspec() :: String.t() | pos_integer()
```

# `sym_or_offset`

```elixir
@type sym_or_offset() :: String.t() | non_neg_integer()
```

# `t`

```elixir
@type t() ::
  :none
  | :auto
  | :iter
  | {:iter, keyword()}
  | {:xdp, ifspec()}
  | {:xdp, ifspec(), keyword()}
  | {:tcx, ifspec(), direction()}
  | {:tcx, ifspec(), direction(), keyword()}
  | {:cgroup, String.t()}
  | {:systemd, String.t()}
  | {:pid, pos_integer()}
  | {:tracepoint, String.t(), String.t()}
  | {:tracepoint, String.t(), String.t(), keyword()}
  | {:raw_tracepoint, String.t()}
  | {:raw_tracepoint, String.t(), keyword()}
  | {:kprobe, String.t()}
  | {:kprobe, String.t(), keyword()}
  | {:kretprobe, String.t()}
  | {:kretprobe, String.t(), keyword()}
  | {:uprobe, String.t(), sym_or_offset()}
  | {:uprobe, String.t(), sym_or_offset(), keyword()}
  | {:uretprobe, String.t(), sym_or_offset()}
  | {:uretprobe, String.t(), sym_or_offset(), keyword()}
  | {:fentry, String.t()}
  | {:fentry, String.t(), keyword()}
  | {:fexit, String.t()}
  | {:fexit, String.t(), keyword()}
  | {:fmod_ret, String.t()}
  | {:fmod_ret, String.t(), keyword()}
  | {:lsm, String.t()}
  | {:lsm, String.t(), keyword()}
  | {:freplace, freplace_target(), String.t()}
  | {:freplace, freplace_target(), String.t(), keyword()}
```

# `extract_pids`

```elixir
@spec extract_pids(t()) :: {t(), [pos_integer()] | pos_integer() | nil}
```

Extracts optional `:pids` from a `{:tracepoint, category, name, opts}` target
tuple, returning `{base_target, pids_or_nil}`.

# `find_process_library`

```elixir
@spec find_process_library(pos_integer() | :self, String.t() | Regex.t(), keyword()) ::
  {:ok, String.t()} | {:error, term()}
```

Locates the full mapped path of a shared library loaded by `os_pid` (or `:self`)
by parsing `/proc/<pid>/maps`.

    {:ok, "/lib/aarch64-linux-gnu/libssl.so.3"} =
      MaeBPF.Target.find_process_library(pid, "libssl.so")

# `join_cgroup_path`

```elixir
@spec join_cgroup_path(String.t(), String.t()) :: {:ok, String.t()} | {:error, term()}
```

Joins a relative cgroup v2 path (such as `"/system.slice/app.service"` or `"/"`)
under `cgroup_root` (default `"/sys/fs/cgroup"`).

# `named_hooks?`

```elixir
@spec named_hooks?(term()) :: boolean()
```

Returns `true` if `list` is a non-empty keyword list mapping hook names to
targets (for example `[ingress: {:xdp, "eth0"}, egress: {:cgroup, "/sys/fs/cgroup/app"}]`).

# `parse_proc_cgroup`

```elixir
@spec parse_proc_cgroup(binary()) :: {:ok, String.t()} | {:error, term()}
```

Parses `/proc/<pid>/cgroup` content and extracts the unified cgroup v2
relative path from the `0::<rel_path>` line.

Returns `{:ok, rel_path}` or `{:error, :cgroup_v2_not_found}`.

# `parse_proc_maps_library`

```elixir
@spec parse_proc_maps_library(binary(), String.t() | Regex.t()) ::
  {:ok, String.t()} | :not_found
```

Parses `/proc/<pid>/maps` text and returns `{:ok, full_path}` for the first
mapped file whose basename starts with `lib_prefix` (or matches regex), or
`:not_found`.

# `preload_attach_target`

```elixir
@spec preload_attach_target(term()) ::
  {:ok, {non_neg_integer(), String.t()}} | :none | {:error, term()}
```

Returns pre-load `bpf_program__set_attach_target` parameters for targets that
require setting a BTF or `freplace` target before `bpf_object__load`.

    {:ok, {0, "tcp_v4_connect"}} = MaeBPF.Target.preload_attach_target({:fentry, "tcp_v4_connect"})
    :none = MaeBPF.Target.preload_attach_target({:kprobe, "do_sys_openat2"})

# `resolve`

```elixir
@spec resolve(term(), keyword()) :: {:ok, t()} | {:error, term()}
```

Resolves a high-level attachment target into a concrete kernel attach spec.

Options (primarily for testing or custom mount points):
  * `:cgroup_root` - cgroup v2 mount root (default `"/sys/fs/cgroup"`)
  * `:proc_root` - procfs root (default `"/proc"`)
  * `:os_type` - OS tuple override (default `:os.type()`)
  * `:check_exists` - verify cgroup directory exists on Linux (default `true`)
  * `:dir_exists?` - predicate `fn path -> boolean()` (default `&File.dir?/1`)
  * `:path_exists?` - predicate `fn path -> boolean()` (default `&File.exists?/1`)
  * `:read_file` - reader `fn path -> {:ok, bin} | {:error, reason}` (default `&File.read/1`)
  * `:cmd_runner` - runner `fn cmd, args, opts -> {out, status}` (default `&System.cmd/3`)

# `target?`

```elixir
@spec target?(term()) :: boolean()
```

Returns `true` if `term` matches a recognized `MaeBPF.Target` tuple or `:none`.

# `target_list?`

```elixir
@spec target_list?(term()) :: boolean()
```

Returns `true` if `list` is a non-empty list of attachment targets
(for example `[{:systemd, "a.service"}, {:systemd, "b.service"}]`).

---

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