bpf-loader.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * bpf-loader.c
  3. *
  4. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  5. * Copyright (C) 2015 Huawei Inc.
  6. */
  7. #include <bpf/libbpf.h>
  8. #include <linux/err.h>
  9. #include "perf.h"
  10. #include "debug.h"
  11. #include "bpf-loader.h"
  12. #include "probe-event.h"
  13. #include "probe-finder.h" // for MAX_PROBES
  14. #include "llvm-utils.h"
  15. #define DEFINE_PRINT_FN(name, level) \
  16. static int libbpf_##name(const char *fmt, ...) \
  17. { \
  18. va_list args; \
  19. int ret; \
  20. \
  21. va_start(args, fmt); \
  22. ret = veprintf(level, verbose, pr_fmt(fmt), args);\
  23. va_end(args); \
  24. return ret; \
  25. }
  26. DEFINE_PRINT_FN(warning, 1)
  27. DEFINE_PRINT_FN(info, 1)
  28. DEFINE_PRINT_FN(debug, 1)
  29. struct bpf_prog_priv {
  30. struct perf_probe_event pev;
  31. };
  32. static bool libbpf_initialized;
  33. struct bpf_object *
  34. bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
  35. {
  36. struct bpf_object *obj;
  37. if (!libbpf_initialized) {
  38. libbpf_set_print(libbpf_warning,
  39. libbpf_info,
  40. libbpf_debug);
  41. libbpf_initialized = true;
  42. }
  43. obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, name);
  44. if (IS_ERR(obj)) {
  45. pr_debug("bpf: failed to load buffer\n");
  46. return ERR_PTR(-EINVAL);
  47. }
  48. return obj;
  49. }
  50. struct bpf_object *bpf__prepare_load(const char *filename, bool source)
  51. {
  52. struct bpf_object *obj;
  53. if (!libbpf_initialized) {
  54. libbpf_set_print(libbpf_warning,
  55. libbpf_info,
  56. libbpf_debug);
  57. libbpf_initialized = true;
  58. }
  59. if (source) {
  60. int err;
  61. void *obj_buf;
  62. size_t obj_buf_sz;
  63. err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
  64. if (err)
  65. return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
  66. obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
  67. free(obj_buf);
  68. } else
  69. obj = bpf_object__open(filename);
  70. if (IS_ERR(obj)) {
  71. pr_debug("bpf: failed to load %s\n", filename);
  72. return obj;
  73. }
  74. return obj;
  75. }
  76. void bpf__clear(void)
  77. {
  78. struct bpf_object *obj, *tmp;
  79. bpf_object__for_each_safe(obj, tmp) {
  80. bpf__unprobe(obj);
  81. bpf_object__close(obj);
  82. }
  83. }
  84. static void
  85. bpf_prog_priv__clear(struct bpf_program *prog __maybe_unused,
  86. void *_priv)
  87. {
  88. struct bpf_prog_priv *priv = _priv;
  89. cleanup_perf_probe_events(&priv->pev, 1);
  90. free(priv);
  91. }
  92. static int
  93. config_bpf_program(struct bpf_program *prog)
  94. {
  95. struct perf_probe_event *pev = NULL;
  96. struct bpf_prog_priv *priv = NULL;
  97. const char *config_str;
  98. int err;
  99. config_str = bpf_program__title(prog, false);
  100. if (IS_ERR(config_str)) {
  101. pr_debug("bpf: unable to get title for program\n");
  102. return PTR_ERR(config_str);
  103. }
  104. priv = calloc(sizeof(*priv), 1);
  105. if (!priv) {
  106. pr_debug("bpf: failed to alloc priv\n");
  107. return -ENOMEM;
  108. }
  109. pev = &priv->pev;
  110. pr_debug("bpf: config program '%s'\n", config_str);
  111. err = parse_perf_probe_command(config_str, pev);
  112. if (err < 0) {
  113. pr_debug("bpf: '%s' is not a valid config string\n",
  114. config_str);
  115. err = -BPF_LOADER_ERRNO__CONFIG;
  116. goto errout;
  117. }
  118. if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) {
  119. pr_debug("bpf: '%s': group for event is set and not '%s'.\n",
  120. config_str, PERF_BPF_PROBE_GROUP);
  121. err = -BPF_LOADER_ERRNO__GROUP;
  122. goto errout;
  123. } else if (!pev->group)
  124. pev->group = strdup(PERF_BPF_PROBE_GROUP);
  125. if (!pev->group) {
  126. pr_debug("bpf: strdup failed\n");
  127. err = -ENOMEM;
  128. goto errout;
  129. }
  130. if (!pev->event) {
  131. pr_debug("bpf: '%s': event name is missing. Section name should be 'key=value'\n",
  132. config_str);
  133. err = -BPF_LOADER_ERRNO__EVENTNAME;
  134. goto errout;
  135. }
  136. pr_debug("bpf: config '%s' is ok\n", config_str);
  137. err = bpf_program__set_private(prog, priv, bpf_prog_priv__clear);
  138. if (err) {
  139. pr_debug("Failed to set priv for program '%s'\n", config_str);
  140. goto errout;
  141. }
  142. return 0;
  143. errout:
  144. if (pev)
  145. clear_perf_probe_event(pev);
  146. free(priv);
  147. return err;
  148. }
  149. static int bpf__prepare_probe(void)
  150. {
  151. static int err = 0;
  152. static bool initialized = false;
  153. /*
  154. * Make err static, so if init failed the first, bpf__prepare_probe()
  155. * fails each time without calling init_probe_symbol_maps multiple
  156. * times.
  157. */
  158. if (initialized)
  159. return err;
  160. initialized = true;
  161. err = init_probe_symbol_maps(false);
  162. if (err < 0)
  163. pr_debug("Failed to init_probe_symbol_maps\n");
  164. probe_conf.max_probes = MAX_PROBES;
  165. return err;
  166. }
  167. int bpf__probe(struct bpf_object *obj)
  168. {
  169. int err = 0;
  170. struct bpf_program *prog;
  171. struct bpf_prog_priv *priv;
  172. struct perf_probe_event *pev;
  173. err = bpf__prepare_probe();
  174. if (err) {
  175. pr_debug("bpf__prepare_probe failed\n");
  176. return err;
  177. }
  178. bpf_object__for_each_program(prog, obj) {
  179. err = config_bpf_program(prog);
  180. if (err)
  181. goto out;
  182. err = bpf_program__get_private(prog, (void **)&priv);
  183. if (err || !priv)
  184. goto out;
  185. pev = &priv->pev;
  186. err = convert_perf_probe_events(pev, 1);
  187. if (err < 0) {
  188. pr_debug("bpf_probe: failed to convert perf probe events");
  189. goto out;
  190. }
  191. err = apply_perf_probe_events(pev, 1);
  192. if (err < 0) {
  193. pr_debug("bpf_probe: failed to apply perf probe events");
  194. goto out;
  195. }
  196. }
  197. out:
  198. return err < 0 ? err : 0;
  199. }
  200. #define EVENTS_WRITE_BUFSIZE 4096
  201. int bpf__unprobe(struct bpf_object *obj)
  202. {
  203. int err, ret = 0;
  204. struct bpf_program *prog;
  205. struct bpf_prog_priv *priv;
  206. bpf_object__for_each_program(prog, obj) {
  207. int i;
  208. err = bpf_program__get_private(prog, (void **)&priv);
  209. if (err || !priv)
  210. continue;
  211. for (i = 0; i < priv->pev.ntevs; i++) {
  212. struct probe_trace_event *tev = &priv->pev.tevs[i];
  213. char name_buf[EVENTS_WRITE_BUFSIZE];
  214. struct strfilter *delfilter;
  215. snprintf(name_buf, EVENTS_WRITE_BUFSIZE,
  216. "%s:%s", tev->group, tev->event);
  217. name_buf[EVENTS_WRITE_BUFSIZE - 1] = '\0';
  218. delfilter = strfilter__new(name_buf, NULL);
  219. if (!delfilter) {
  220. pr_debug("Failed to create filter for unprobing\n");
  221. ret = -ENOMEM;
  222. continue;
  223. }
  224. err = del_perf_probe_events(delfilter);
  225. strfilter__delete(delfilter);
  226. if (err) {
  227. pr_debug("Failed to delete %s\n", name_buf);
  228. ret = err;
  229. continue;
  230. }
  231. }
  232. }
  233. return ret;
  234. }
  235. int bpf__load(struct bpf_object *obj)
  236. {
  237. int err;
  238. err = bpf_object__load(obj);
  239. if (err) {
  240. pr_debug("bpf: load objects failed\n");
  241. return err;
  242. }
  243. return 0;
  244. }
  245. int bpf__foreach_tev(struct bpf_object *obj,
  246. bpf_prog_iter_callback_t func,
  247. void *arg)
  248. {
  249. struct bpf_program *prog;
  250. int err;
  251. bpf_object__for_each_program(prog, obj) {
  252. struct probe_trace_event *tev;
  253. struct perf_probe_event *pev;
  254. struct bpf_prog_priv *priv;
  255. int i, fd;
  256. err = bpf_program__get_private(prog,
  257. (void **)&priv);
  258. if (err || !priv) {
  259. pr_debug("bpf: failed to get private field\n");
  260. return -BPF_LOADER_ERRNO__INTERNAL;
  261. }
  262. pev = &priv->pev;
  263. for (i = 0; i < pev->ntevs; i++) {
  264. tev = &pev->tevs[i];
  265. fd = bpf_program__fd(prog);
  266. if (fd < 0) {
  267. pr_debug("bpf: failed to get file descriptor\n");
  268. return fd;
  269. }
  270. err = (*func)(tev, fd, arg);
  271. if (err) {
  272. pr_debug("bpf: call back failed, stop iterate\n");
  273. return err;
  274. }
  275. }
  276. }
  277. return 0;
  278. }
  279. #define ERRNO_OFFSET(e) ((e) - __BPF_LOADER_ERRNO__START)
  280. #define ERRCODE_OFFSET(c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)
  281. #define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
  282. static const char *bpf_loader_strerror_table[NR_ERRNO] = {
  283. [ERRCODE_OFFSET(CONFIG)] = "Invalid config string",
  284. [ERRCODE_OFFSET(GROUP)] = "Invalid group name",
  285. [ERRCODE_OFFSET(EVENTNAME)] = "No event name found in config string",
  286. [ERRCODE_OFFSET(INTERNAL)] = "BPF loader internal error",
  287. [ERRCODE_OFFSET(COMPILE)] = "Error when compiling BPF scriptlet",
  288. };
  289. static int
  290. bpf_loader_strerror(int err, char *buf, size_t size)
  291. {
  292. char sbuf[STRERR_BUFSIZE];
  293. const char *msg;
  294. if (!buf || !size)
  295. return -1;
  296. err = err > 0 ? err : -err;
  297. if (err >= __LIBBPF_ERRNO__START)
  298. return libbpf_strerror(err, buf, size);
  299. if (err >= __BPF_LOADER_ERRNO__START && err < __BPF_LOADER_ERRNO__END) {
  300. msg = bpf_loader_strerror_table[ERRNO_OFFSET(err)];
  301. snprintf(buf, size, "%s", msg);
  302. buf[size - 1] = '\0';
  303. return 0;
  304. }
  305. if (err >= __BPF_LOADER_ERRNO__END)
  306. snprintf(buf, size, "Unknown bpf loader error %d", err);
  307. else
  308. snprintf(buf, size, "%s",
  309. strerror_r(err, sbuf, sizeof(sbuf)));
  310. buf[size - 1] = '\0';
  311. return -1;
  312. }
  313. #define bpf__strerror_head(err, buf, size) \
  314. char sbuf[STRERR_BUFSIZE], *emsg;\
  315. if (!size)\
  316. return 0;\
  317. if (err < 0)\
  318. err = -err;\
  319. bpf_loader_strerror(err, sbuf, sizeof(sbuf));\
  320. emsg = sbuf;\
  321. switch (err) {\
  322. default:\
  323. scnprintf(buf, size, "%s", emsg);\
  324. break;
  325. #define bpf__strerror_entry(val, fmt...)\
  326. case val: {\
  327. scnprintf(buf, size, fmt);\
  328. break;\
  329. }
  330. #define bpf__strerror_end(buf, size)\
  331. }\
  332. buf[size - 1] = '\0';
  333. int bpf__strerror_prepare_load(const char *filename, bool source,
  334. int err, char *buf, size_t size)
  335. {
  336. size_t n;
  337. int ret;
  338. n = snprintf(buf, size, "Failed to load %s%s: ",
  339. filename, source ? " from source" : "");
  340. if (n >= size) {
  341. buf[size - 1] = '\0';
  342. return 0;
  343. }
  344. buf += n;
  345. size -= n;
  346. ret = bpf_loader_strerror(err, buf, size);
  347. buf[size - 1] = '\0';
  348. return ret;
  349. }
  350. int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
  351. int err, char *buf, size_t size)
  352. {
  353. bpf__strerror_head(err, buf, size);
  354. bpf__strerror_entry(EEXIST, "Probe point exist. Try use 'perf probe -d \"*\"'");
  355. bpf__strerror_entry(EACCES, "You need to be root");
  356. bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0");
  357. bpf__strerror_entry(ENOENT, "You need to check probing points in BPF file");
  358. bpf__strerror_end(buf, size);
  359. return 0;
  360. }
  361. int bpf__strerror_load(struct bpf_object *obj,
  362. int err, char *buf, size_t size)
  363. {
  364. bpf__strerror_head(err, buf, size);
  365. case LIBBPF_ERRNO__KVER: {
  366. unsigned int obj_kver = bpf_object__get_kversion(obj);
  367. unsigned int real_kver;
  368. if (fetch_kernel_version(&real_kver, NULL, 0)) {
  369. scnprintf(buf, size, "Unable to fetch kernel version");
  370. break;
  371. }
  372. if (obj_kver != real_kver) {
  373. scnprintf(buf, size,
  374. "'version' ("KVER_FMT") doesn't match running kernel ("KVER_FMT")",
  375. KVER_PARAM(obj_kver),
  376. KVER_PARAM(real_kver));
  377. break;
  378. }
  379. scnprintf(buf, size, "Failed to load program for unknown reason");
  380. break;
  381. }
  382. bpf__strerror_end(buf, size);
  383. return 0;
  384. }