bpf-loader.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
  3. * Copyright (C) 2015, Huawei Inc.
  4. */
  5. #ifndef __BPF_LOADER_H
  6. #define __BPF_LOADER_H
  7. #include <linux/compiler.h>
  8. #include <linux/err.h>
  9. #include <string.h>
  10. #include <bpf/libbpf.h>
  11. #include "probe-event.h"
  12. #include "debug.h"
  13. enum bpf_loader_errno {
  14. __BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100,
  15. /* Invalid config string */
  16. BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START,
  17. BPF_LOADER_ERRNO__GROUP, /* Invalid group name */
  18. BPF_LOADER_ERRNO__EVENTNAME, /* Event name is missing */
  19. BPF_LOADER_ERRNO__INTERNAL, /* BPF loader internal error */
  20. BPF_LOADER_ERRNO__COMPILE, /* Error when compiling BPF scriptlet */
  21. BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */
  22. BPF_LOADER_ERRNO__PROLOGUE, /* Failed to generate prologue */
  23. BPF_LOADER_ERRNO__PROLOGUE2BIG, /* Prologue too big for program */
  24. BPF_LOADER_ERRNO__PROLOGUEOOB, /* Offset out of bound for prologue */
  25. __BPF_LOADER_ERRNO__END,
  26. };
  27. struct bpf_object;
  28. #define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
  29. typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
  30. int fd, void *arg);
  31. #ifdef HAVE_LIBBPF_SUPPORT
  32. struct bpf_object *bpf__prepare_load(const char *filename, bool source);
  33. int bpf__strerror_prepare_load(const char *filename, bool source,
  34. int err, char *buf, size_t size);
  35. struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
  36. const char *name);
  37. void bpf__clear(void);
  38. int bpf__probe(struct bpf_object *obj);
  39. int bpf__unprobe(struct bpf_object *obj);
  40. int bpf__strerror_probe(struct bpf_object *obj, int err,
  41. char *buf, size_t size);
  42. int bpf__load(struct bpf_object *obj);
  43. int bpf__strerror_load(struct bpf_object *obj, int err,
  44. char *buf, size_t size);
  45. int bpf__foreach_tev(struct bpf_object *obj,
  46. bpf_prog_iter_callback_t func, void *arg);
  47. #else
  48. static inline struct bpf_object *
  49. bpf__prepare_load(const char *filename __maybe_unused,
  50. bool source __maybe_unused)
  51. {
  52. pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
  53. return ERR_PTR(-ENOTSUP);
  54. }
  55. static inline struct bpf_object *
  56. bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
  57. size_t obj_buf_sz __maybe_unused)
  58. {
  59. return ERR_PTR(-ENOTSUP);
  60. }
  61. static inline void bpf__clear(void) { }
  62. static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
  63. static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
  64. static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
  65. static inline int
  66. bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
  67. bpf_prog_iter_callback_t func __maybe_unused,
  68. void *arg __maybe_unused)
  69. {
  70. return 0;
  71. }
  72. static inline int
  73. __bpf_strerror(char *buf, size_t size)
  74. {
  75. if (!size)
  76. return 0;
  77. strncpy(buf,
  78. "ERROR: eBPF object loading is disabled during compiling.\n",
  79. size);
  80. buf[size - 1] = '\0';
  81. return 0;
  82. }
  83. static inline
  84. int bpf__strerror_prepare_load(const char *filename __maybe_unused,
  85. bool source __maybe_unused,
  86. int err __maybe_unused,
  87. char *buf, size_t size)
  88. {
  89. return __bpf_strerror(buf, size);
  90. }
  91. static inline int
  92. bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
  93. int err __maybe_unused,
  94. char *buf, size_t size)
  95. {
  96. return __bpf_strerror(buf, size);
  97. }
  98. static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
  99. int err __maybe_unused,
  100. char *buf, size_t size)
  101. {
  102. return __bpf_strerror(buf, size);
  103. }
  104. #endif
  105. #endif