bpf-loader.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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__END,
  22. };
  23. struct bpf_object;
  24. #define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
  25. typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
  26. int fd, void *arg);
  27. #ifdef HAVE_LIBBPF_SUPPORT
  28. struct bpf_object *bpf__prepare_load(const char *filename, bool source);
  29. int bpf__strerror_prepare_load(const char *filename, bool source,
  30. int err, char *buf, size_t size);
  31. struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
  32. const char *name);
  33. void bpf__clear(void);
  34. int bpf__probe(struct bpf_object *obj);
  35. int bpf__unprobe(struct bpf_object *obj);
  36. int bpf__strerror_probe(struct bpf_object *obj, int err,
  37. char *buf, size_t size);
  38. int bpf__load(struct bpf_object *obj);
  39. int bpf__strerror_load(struct bpf_object *obj, int err,
  40. char *buf, size_t size);
  41. int bpf__foreach_tev(struct bpf_object *obj,
  42. bpf_prog_iter_callback_t func, void *arg);
  43. #else
  44. static inline struct bpf_object *
  45. bpf__prepare_load(const char *filename __maybe_unused,
  46. bool source __maybe_unused)
  47. {
  48. pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
  49. return ERR_PTR(-ENOTSUP);
  50. }
  51. static inline struct bpf_object *
  52. bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
  53. size_t obj_buf_sz __maybe_unused)
  54. {
  55. return ERR_PTR(-ENOTSUP);
  56. }
  57. static inline void bpf__clear(void) { }
  58. static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
  59. static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
  60. static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
  61. static inline int
  62. bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
  63. bpf_prog_iter_callback_t func __maybe_unused,
  64. void *arg __maybe_unused)
  65. {
  66. return 0;
  67. }
  68. static inline int
  69. __bpf_strerror(char *buf, size_t size)
  70. {
  71. if (!size)
  72. return 0;
  73. strncpy(buf,
  74. "ERROR: eBPF object loading is disabled during compiling.\n",
  75. size);
  76. buf[size - 1] = '\0';
  77. return 0;
  78. }
  79. static inline
  80. int bpf__strerror_prepare_load(const char *filename __maybe_unused,
  81. bool source __maybe_unused,
  82. int err __maybe_unused,
  83. char *buf, size_t size)
  84. {
  85. return __bpf_strerror(buf, size);
  86. }
  87. static inline int
  88. bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
  89. int err __maybe_unused,
  90. char *buf, size_t size)
  91. {
  92. return __bpf_strerror(buf, size);
  93. }
  94. static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
  95. int err __maybe_unused,
  96. char *buf, size_t size)
  97. {
  98. return __bpf_strerror(buf, size);
  99. }
  100. #endif
  101. #endif