/* if libelf is old and doesn't support mmap(), fall back to read() */ #ifndef ELF_C_READ_MMAP #define ELF_C_READ_MMAP ELF_C_READ #endif
/* Older libelf all end up in this expression, for both 32 and 64 bit */ #ifndef ELF64_ST_VISIBILITY #define ELF64_ST_VISIBILITY(o) ((o) & 0x03) #endif
/* Check whether a string `str` has prefix `pfx`, regardless if `pfx` is * a string literal known at compilation time or char * pointer known only at * runtime.
*/ #define str_has_pfx(str, pfx) \
(strncmp(str, pfx, __builtin_constant_p(pfx) ? sizeof(pfx) - 1 : strlen(pfx)) == 0)
/* Symbol versioning is different between static and shared library. * Properly versioned symbols are needed for shared library, but * only the symbol of the new version is needed for static library. * Starting with GNU C 10, use symver attribute instead of .symver assembler * directive, which works better with GCC LTO builds.
*/ #ifdefined(SHARED) && defined(__GNUC__) && __GNUC__ >= 10
struct bpf_link { int (*detach)(struct bpf_link *link); void (*dealloc)(struct bpf_link *link); char *pin_path; /* NULL, if not pinned */ int fd; /* hook FD, -1 if not applicable */ bool disconnected;
};
/* * Re-implement glibc's reallocarray() for libbpf internal-only use. * reallocarray(), unfortunately, is not available in all versions of glibc, * so requires extra feature detection and using reallocarray() stub from * <tools/libc_compat.h> and COMPAT_NEED_REALLOCARRAY. All this complicates * build of libbpf unnecessarily and is just a maintenance burden. Instead, * it's trivial to implement libbpf-specific internal version and use it * throughout libbpf.
*/ staticinlinevoid *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
{
size_t total;
/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst * is zero-terminated string no matter what (unless sz == 0, in which case * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs * in what is returned. Given this is internal helper, it's trivial to extend * this, when necessary. Use this instead of strncpy inside libbpf source code.
*/ staticinlinevoid libbpf_strlcpy(char *dst, constchar *src, size_t sz)
{
size_t i;
if (sz == 0) return;
sz--; for (i = 0; i < sz && src[i]; i++)
dst[i] = src[i];
dst[i] = '\0';
}
enum kern_feature_id { /* v4.14: kernel support for program & map names. */
FEAT_PROG_NAME, /* v5.2: kernel support for global data sections. */
FEAT_GLOBAL_DATA, /* BTF support */
FEAT_BTF, /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
FEAT_BTF_FUNC, /* BTF_KIND_VAR and BTF_KIND_DATASEC support */
FEAT_BTF_DATASEC, /* BTF_FUNC_GLOBAL is supported */
FEAT_BTF_GLOBAL_FUNC, /* BPF_F_MMAPABLE is supported for arrays */
FEAT_ARRAY_MMAP, /* kernel support for expected_attach_type in BPF_PROG_LOAD */
FEAT_EXP_ATTACH_TYPE, /* bpf_probe_read_{kernel,user}[_str] helpers */
FEAT_PROBE_READ_KERN, /* BPF_PROG_BIND_MAP is supported */
FEAT_PROG_BIND_MAP, /* Kernel support for module BTFs */
FEAT_MODULE_BTF, /* BTF_KIND_FLOAT support */
FEAT_BTF_FLOAT, /* BPF perf link support */
FEAT_PERF_LINK, /* BTF_KIND_DECL_TAG support */
FEAT_BTF_DECL_TAG, /* BTF_KIND_TYPE_TAG support */
FEAT_BTF_TYPE_TAG, /* memcg-based accounting for BPF maps and progs */
FEAT_MEMCG_ACCOUNT, /* BPF cookie (bpf_get_attach_cookie() BPF helper) support */
FEAT_BPF_COOKIE, /* BTF_KIND_ENUM64 support and BTF_KIND_ENUM kflag support */
FEAT_BTF_ENUM64, /* Kernel uses syscall wrapper (CONFIG_ARCH_HAS_SYSCALL_WRAPPER) */
FEAT_SYSCALL_WRAPPER, /* BPF multi-uprobe link support */
FEAT_UPROBE_MULTI_LINK, /* Kernel supports arg:ctx tag (__arg_ctx) for global subprogs natively */
FEAT_ARG_CTX_TAG, /* Kernel supports '?' at the front of datasec names */
FEAT_BTF_QMARK_DATASEC,
__FEAT_CNT,
};
struct btf_ext_info { /* * info points to the individual info section (e.g. func_info and * line_info) from the .BTF.ext. It does not include the __u32 rec_size.
*/ void *info;
__u32 rec_size;
__u32 len; /* optional (maintained internally by libbpf) mapping between .BTF.ext * section and corresponding ELF section. This is used to join * information like CO-RE relocation records with corresponding BPF * programs defined in ELF sections
*/
__u32 *sec_idxs; int sec_cnt;
};
#define for_each_btf_ext_rec(seg, sec, i, rec) \ for (i = 0, rec = (void *)&(sec)->data; \
i < (sec)->num_info; \
i++, rec = (void *)rec + (seg)->rec_size)
/* * The .BTF.ext ELF section layout defined as * struct btf_ext_header * func_info subsection * * The func_info subsection layout: * record size for struct bpf_func_info in the func_info subsection * struct btf_ext_info_sec for section #1 * a list of bpf_func_info records for section #1 * where struct bpf_func_info mimics one in include/uapi/linux/bpf.h * but may not be identical * struct btf_ext_info_sec for section #2 * a list of bpf_func_info records for section #2 * ...... * * Note that the bpf_func_info record size in .BTF.ext may not * be the same as the one defined in include/uapi/linux/bpf.h. * The loader should ensure that record_size meets minimum * requirement and pass the record as is to the kernel. The * kernel will handle the func_info properly based on its contents.
*/ struct btf_ext_header {
__u16 magic;
__u8 version;
__u8 flags;
__u32 hdr_len;
/* All offsets are in bytes relative to the end of this header */
__u32 func_info_off;
__u32 func_info_len;
__u32 line_info_off;
__u32 line_info_len;
/* optional part of .BTF.ext header */
__u32 core_relo_off;
__u32 core_relo_len;
};
struct btf_field_desc { /* once-per-type offsets */ int t_off_cnt, t_offs[2]; /* member struct size, or zero, if no members */ int m_sz; /* repeated per-member offsets */ int m_off_cnt, m_offs[1];
};
struct btf_field_iter { struct btf_field_desc desc; void *p; int m_idx; int off_idx; int vlen;
};
/* handle direct returned errors */ staticinlineint libbpf_err(int ret)
{ if (ret < 0)
errno = -ret; return ret;
}
/* handle errno-based (e.g., syscall or libc) errors according to libbpf's * strict mode settings
*/ staticinlineint libbpf_err_errno(int ret)
{ /* errno is already assumed to be set on error */ return ret < 0 ? -errno : ret;
}
/* handle error for pointer-returning APIs, err is assumed to be < 0 always */ staticinlinevoid *libbpf_err_ptr(int err)
{ /* set errno on error, this doesn't break anything */
errno = -err; return NULL;
}
/* handle pointer-returning APIs' error handling */ staticinlinevoid *libbpf_ptr(void *ret)
{ /* set errno on error, this doesn't break anything */ if (IS_ERR(ret))
errno = -PTR_ERR(ret);
/* Unconditionally dup FD, ensuring it doesn't use [0, 2] range. * Original FD is not closed or altered in any other way. * Preserves original FD value, if it's invalid (negative).
*/ staticinlineint dup_good_fd(int fd)
{ if (fd < 0) return fd; return fcntl(fd, F_DUPFD_CLOEXEC, 3);
}
/* if fd is stdin, stdout, or stderr, dup to a fd greater than 2 * Takes ownership of the fd passed in, and closes it if calling * fcntl(fd, F_DUPFD_CLOEXEC, 3).
*/ staticinlineint ensure_good_fd(int fd)
{ int old_fd = fd, saved_errno;
if (fd < 0) return fd; if (fd < 3) {
fd = dup_good_fd(fd);
saved_errno = errno;
close(old_fd);
errno = saved_errno; if (fd < 0) {
pr_warn("failed to dup FD %d to FD > 2: %d\n", old_fd, -saved_errno);
errno = saved_errno;
}
} return fd;
}
staticinlineint sys_dup3(int oldfd, int newfd, int flags)
{ return syscall(__NR_dup3, oldfd, newfd, flags);
}
/* Some versions of Android don't provide memfd_create() in their libc * implementation, so avoid complications and just go straight to Linux * syscall.
*/ staticinlineint sys_memfd_create(constchar *name, unsigned flags)
{ return syscall(__NR_memfd_create, name, flags);
}
/* Point *fixed_fd* to the same file that *tmp_fd* points to. * Regardless of success, *tmp_fd* is closed. * Whatever *fixed_fd* pointed to is closed silently.
*/ staticinlineint reuse_fd(int fixed_fd, int tmp_fd)
{ int err;
/* The following two functions are exposed to bpftool */ int bpf_core_add_cands(struct bpf_core_cand *local_cand,
size_t local_essent_len, conststruct btf *targ_btf, constchar *targ_btf_name, int targ_start_id, struct bpf_core_cand_list *cands); void bpf_core_free_cands(struct bpf_core_cand_list *cands);
#define PROG_LOAD_ATTEMPTS 5 int sys_bpf_prog_load(union bpf_attr *attr, unsignedint size, int attempts);
bool glob_match(constchar *str, constchar *pat);
long elf_find_func_offset(Elf *elf, constchar *binary_path, constchar *name); long elf_find_func_offset_from_file(constchar *binary_path, constchar *name);
struct elf_fd {
Elf *elf; int fd;
};
int elf_open(constchar *binary_path, struct elf_fd *elf_fd); void elf_close(struct elf_fd *elf_fd);
int elf_resolve_syms_offsets(constchar *binary_path, int cnt, constchar **syms, unsignedlong **poffsets, int st_type); int elf_resolve_pattern_offsets(constchar *binary_path, constchar *pattern, unsignedlong **poffsets, size_t *pcnt);
int probe_fd(int fd);
#endif/* __LIBBPF_LIBBPF_INTERNAL_H */
Messung V0.5
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.