task_fd_query_user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <stdbool.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. #include <fcntl.h>
  10. #include <linux/bpf.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <linux/perf_event.h>
  15. #include <bpf/bpf.h>
  16. #include <bpf/libbpf.h>
  17. #include "bpf_util.h"
  18. #include "perf-sys.h"
  19. #include "trace_helpers.h"
  20. static struct bpf_program *progs[2];
  21. static struct bpf_link *links[2];
  22. #define CHECK_PERROR_RET(condition) ({ \
  23. int __ret = !!(condition); \
  24. if (__ret) { \
  25. printf("FAIL: %s:\n", __func__); \
  26. perror(" "); \
  27. return -1; \
  28. } \
  29. })
  30. #define CHECK_AND_RET(condition) ({ \
  31. int __ret = !!(condition); \
  32. if (__ret) \
  33. return -1; \
  34. })
  35. static __u64 ptr_to_u64(void *ptr)
  36. {
  37. return (__u64) (unsigned long) ptr;
  38. }
  39. #define PMU_TYPE_FILE "/sys/bus/event_source/devices/%s/type"
  40. static int bpf_find_probe_type(const char *event_type)
  41. {
  42. char buf[256];
  43. int fd, ret;
  44. ret = snprintf(buf, sizeof(buf), PMU_TYPE_FILE, event_type);
  45. CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
  46. fd = open(buf, O_RDONLY);
  47. CHECK_PERROR_RET(fd < 0);
  48. ret = read(fd, buf, sizeof(buf));
  49. close(fd);
  50. CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
  51. errno = 0;
  52. ret = (int)strtol(buf, NULL, 10);
  53. CHECK_PERROR_RET(errno);
  54. return ret;
  55. }
  56. #define PMU_RETPROBE_FILE "/sys/bus/event_source/devices/%s/format/retprobe"
  57. static int bpf_get_retprobe_bit(const char *event_type)
  58. {
  59. char buf[256];
  60. int fd, ret;
  61. ret = snprintf(buf, sizeof(buf), PMU_RETPROBE_FILE, event_type);
  62. CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
  63. fd = open(buf, O_RDONLY);
  64. CHECK_PERROR_RET(fd < 0);
  65. ret = read(fd, buf, sizeof(buf));
  66. close(fd);
  67. CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
  68. CHECK_PERROR_RET(strlen(buf) < strlen("config:"));
  69. errno = 0;
  70. ret = (int)strtol(buf + strlen("config:"), NULL, 10);
  71. CHECK_PERROR_RET(errno);
  72. return ret;
  73. }
  74. static int test_debug_fs_kprobe(int link_idx, const char *fn_name,
  75. __u32 expected_fd_type)
  76. {
  77. __u64 probe_offset, probe_addr;
  78. __u32 len, prog_id, fd_type;
  79. int err, event_fd;
  80. char buf[256];
  81. len = sizeof(buf);
  82. event_fd = bpf_link__fd(links[link_idx]);
  83. err = bpf_task_fd_query(getpid(), event_fd, 0, buf, &len,
  84. &prog_id, &fd_type, &probe_offset,
  85. &probe_addr);
  86. if (err < 0) {
  87. printf("FAIL: %s, for event_fd idx %d, fn_name %s\n",
  88. __func__, link_idx, fn_name);
  89. perror(" :");
  90. return -1;
  91. }
  92. if (strcmp(buf, fn_name) != 0 ||
  93. fd_type != expected_fd_type ||
  94. probe_offset != 0x0 || probe_addr != 0x0) {
  95. printf("FAIL: bpf_trace_event_query(event_fd[%d]):\n",
  96. link_idx);
  97. printf("buf: %s, fd_type: %u, probe_offset: 0x%llx,"
  98. " probe_addr: 0x%llx\n",
  99. buf, fd_type, probe_offset, probe_addr);
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. static int test_nondebug_fs_kuprobe_common(const char *event_type,
  105. const char *name, __u64 offset, __u64 addr, bool is_return,
  106. char *buf, __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
  107. __u64 *probe_offset, __u64 *probe_addr)
  108. {
  109. int is_return_bit = bpf_get_retprobe_bit(event_type);
  110. int type = bpf_find_probe_type(event_type);
  111. struct perf_event_attr attr = {};
  112. struct bpf_link *link;
  113. int fd, err = -1;
  114. if (type < 0 || is_return_bit < 0) {
  115. printf("FAIL: %s incorrect type (%d) or is_return_bit (%d)\n",
  116. __func__, type, is_return_bit);
  117. return err;
  118. }
  119. attr.sample_period = 1;
  120. attr.wakeup_events = 1;
  121. if (is_return)
  122. attr.config |= 1 << is_return_bit;
  123. if (name) {
  124. attr.config1 = ptr_to_u64((void *)name);
  125. attr.config2 = offset;
  126. } else {
  127. attr.config1 = 0;
  128. attr.config2 = addr;
  129. }
  130. attr.size = sizeof(attr);
  131. attr.type = type;
  132. fd = sys_perf_event_open(&attr, -1, 0, -1, 0);
  133. link = bpf_program__attach_perf_event(progs[0], fd);
  134. if (libbpf_get_error(link)) {
  135. printf("ERROR: bpf_program__attach_perf_event failed\n");
  136. link = NULL;
  137. close(fd);
  138. goto cleanup;
  139. }
  140. CHECK_PERROR_RET(bpf_task_fd_query(getpid(), fd, 0, buf, buf_len,
  141. prog_id, fd_type, probe_offset, probe_addr) < 0);
  142. err = 0;
  143. cleanup:
  144. bpf_link__destroy(link);
  145. return err;
  146. }
  147. static int test_nondebug_fs_probe(const char *event_type, const char *name,
  148. __u64 offset, __u64 addr, bool is_return,
  149. __u32 expected_fd_type,
  150. __u32 expected_ret_fd_type,
  151. char *buf, __u32 buf_len)
  152. {
  153. __u64 probe_offset, probe_addr;
  154. __u32 prog_id, fd_type;
  155. int err;
  156. err = test_nondebug_fs_kuprobe_common(event_type, name,
  157. offset, addr, is_return,
  158. buf, &buf_len, &prog_id,
  159. &fd_type, &probe_offset,
  160. &probe_addr);
  161. if (err < 0) {
  162. printf("FAIL: %s, "
  163. "for name %s, offset 0x%llx, addr 0x%llx, is_return %d\n",
  164. __func__, name ? name : "", offset, addr, is_return);
  165. perror(" :");
  166. return -1;
  167. }
  168. if ((is_return && fd_type != expected_ret_fd_type) ||
  169. (!is_return && fd_type != expected_fd_type)) {
  170. printf("FAIL: %s, incorrect fd_type %u\n",
  171. __func__, fd_type);
  172. return -1;
  173. }
  174. if (name) {
  175. if (strcmp(name, buf) != 0) {
  176. printf("FAIL: %s, incorrect buf %s\n", __func__, buf);
  177. return -1;
  178. }
  179. if (probe_offset != offset) {
  180. printf("FAIL: %s, incorrect probe_offset 0x%llx\n",
  181. __func__, probe_offset);
  182. return -1;
  183. }
  184. } else {
  185. if (buf_len != 0) {
  186. printf("FAIL: %s, incorrect buf %p\n",
  187. __func__, buf);
  188. return -1;
  189. }
  190. if (probe_addr != addr) {
  191. printf("FAIL: %s, incorrect probe_addr 0x%llx\n",
  192. __func__, probe_addr);
  193. return -1;
  194. }
  195. }
  196. return 0;
  197. }
  198. static int test_debug_fs_uprobe(char *binary_path, long offset, bool is_return)
  199. {
  200. char buf[256], event_alias[sizeof("test_1234567890")];
  201. const char *event_type = "uprobe";
  202. struct perf_event_attr attr = {};
  203. __u64 probe_offset, probe_addr;
  204. __u32 len, prog_id, fd_type;
  205. int err = -1, res, kfd, efd;
  206. struct bpf_link *link;
  207. ssize_t bytes;
  208. snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/%s_events",
  209. event_type);
  210. kfd = open(buf, O_WRONLY | O_TRUNC, 0);
  211. CHECK_PERROR_RET(kfd < 0);
  212. res = snprintf(event_alias, sizeof(event_alias), "test_%d", getpid());
  213. CHECK_PERROR_RET(res < 0 || res >= sizeof(event_alias));
  214. res = snprintf(buf, sizeof(buf), "%c:%ss/%s %s:0x%lx",
  215. is_return ? 'r' : 'p', event_type, event_alias,
  216. binary_path, offset);
  217. CHECK_PERROR_RET(res < 0 || res >= sizeof(buf));
  218. CHECK_PERROR_RET(write(kfd, buf, strlen(buf)) < 0);
  219. close(kfd);
  220. kfd = -1;
  221. snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%ss/%s/id",
  222. event_type, event_alias);
  223. efd = open(buf, O_RDONLY, 0);
  224. CHECK_PERROR_RET(efd < 0);
  225. bytes = read(efd, buf, sizeof(buf));
  226. CHECK_PERROR_RET(bytes <= 0 || bytes >= sizeof(buf));
  227. close(efd);
  228. buf[bytes] = '\0';
  229. attr.config = strtol(buf, NULL, 0);
  230. attr.type = PERF_TYPE_TRACEPOINT;
  231. attr.sample_period = 1;
  232. attr.wakeup_events = 1;
  233. kfd = sys_perf_event_open(&attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
  234. link = bpf_program__attach_perf_event(progs[0], kfd);
  235. if (libbpf_get_error(link)) {
  236. printf("ERROR: bpf_program__attach_perf_event failed\n");
  237. link = NULL;
  238. close(kfd);
  239. goto cleanup;
  240. }
  241. len = sizeof(buf);
  242. err = bpf_task_fd_query(getpid(), kfd, 0, buf, &len,
  243. &prog_id, &fd_type, &probe_offset,
  244. &probe_addr);
  245. if (err < 0) {
  246. printf("FAIL: %s, binary_path %s\n", __func__, binary_path);
  247. perror(" :");
  248. return -1;
  249. }
  250. if ((is_return && fd_type != BPF_FD_TYPE_URETPROBE) ||
  251. (!is_return && fd_type != BPF_FD_TYPE_UPROBE)) {
  252. printf("FAIL: %s, incorrect fd_type %u\n", __func__,
  253. fd_type);
  254. return -1;
  255. }
  256. if (strcmp(binary_path, buf) != 0) {
  257. printf("FAIL: %s, incorrect buf %s\n", __func__, buf);
  258. return -1;
  259. }
  260. if (probe_offset != offset) {
  261. printf("FAIL: %s, incorrect probe_offset 0x%llx\n", __func__,
  262. probe_offset);
  263. return -1;
  264. }
  265. err = 0;
  266. cleanup:
  267. bpf_link__destroy(link);
  268. return err;
  269. }
  270. int main(int argc, char **argv)
  271. {
  272. extern char __executable_start;
  273. char filename[256], buf[256];
  274. __u64 uprobe_file_offset;
  275. struct bpf_program *prog;
  276. struct bpf_object *obj;
  277. int i = 0, err = -1;
  278. if (load_kallsyms()) {
  279. printf("failed to process /proc/kallsyms\n");
  280. return err;
  281. }
  282. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  283. obj = bpf_object__open_file(filename, NULL);
  284. if (libbpf_get_error(obj)) {
  285. fprintf(stderr, "ERROR: opening BPF object file failed\n");
  286. return err;
  287. }
  288. /* load BPF program */
  289. if (bpf_object__load(obj)) {
  290. fprintf(stderr, "ERROR: loading BPF object file failed\n");
  291. goto cleanup;
  292. }
  293. bpf_object__for_each_program(prog, obj) {
  294. progs[i] = prog;
  295. links[i] = bpf_program__attach(progs[i]);
  296. if (libbpf_get_error(links[i])) {
  297. fprintf(stderr, "ERROR: bpf_program__attach failed\n");
  298. links[i] = NULL;
  299. goto cleanup;
  300. }
  301. i++;
  302. }
  303. /* test two functions in the corresponding *_kern.c file */
  304. CHECK_AND_RET(test_debug_fs_kprobe(0, "blk_mq_start_request",
  305. BPF_FD_TYPE_KPROBE));
  306. CHECK_AND_RET(test_debug_fs_kprobe(1, "__blk_account_io_done",
  307. BPF_FD_TYPE_KRETPROBE));
  308. /* test nondebug fs kprobe */
  309. CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x0, 0x0,
  310. false, BPF_FD_TYPE_KPROBE,
  311. BPF_FD_TYPE_KRETPROBE,
  312. buf, sizeof(buf)));
  313. #ifdef __x86_64__
  314. /* set a kprobe on "bpf_check + 0x5", which is x64 specific */
  315. CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x5, 0x0,
  316. false, BPF_FD_TYPE_KPROBE,
  317. BPF_FD_TYPE_KRETPROBE,
  318. buf, sizeof(buf)));
  319. #endif
  320. CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x0, 0x0,
  321. true, BPF_FD_TYPE_KPROBE,
  322. BPF_FD_TYPE_KRETPROBE,
  323. buf, sizeof(buf)));
  324. CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
  325. ksym_get_addr("bpf_check"), false,
  326. BPF_FD_TYPE_KPROBE,
  327. BPF_FD_TYPE_KRETPROBE,
  328. buf, sizeof(buf)));
  329. CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
  330. ksym_get_addr("bpf_check"), false,
  331. BPF_FD_TYPE_KPROBE,
  332. BPF_FD_TYPE_KRETPROBE,
  333. NULL, 0));
  334. CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
  335. ksym_get_addr("bpf_check"), true,
  336. BPF_FD_TYPE_KPROBE,
  337. BPF_FD_TYPE_KRETPROBE,
  338. buf, sizeof(buf)));
  339. CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
  340. ksym_get_addr("bpf_check"), true,
  341. BPF_FD_TYPE_KPROBE,
  342. BPF_FD_TYPE_KRETPROBE,
  343. 0, 0));
  344. /* test nondebug fs uprobe */
  345. /* the calculation of uprobe file offset is based on gcc 7.3.1 on x64
  346. * and the default linker script, which defines __executable_start as
  347. * the start of the .text section. The calculation could be different
  348. * on different systems with different compilers. The right way is
  349. * to parse the ELF file. We took a shortcut here.
  350. */
  351. uprobe_file_offset = (unsigned long)main - (unsigned long)&__executable_start;
  352. CHECK_AND_RET(test_nondebug_fs_probe("uprobe", (char *)argv[0],
  353. uprobe_file_offset, 0x0, false,
  354. BPF_FD_TYPE_UPROBE,
  355. BPF_FD_TYPE_URETPROBE,
  356. buf, sizeof(buf)));
  357. CHECK_AND_RET(test_nondebug_fs_probe("uprobe", (char *)argv[0],
  358. uprobe_file_offset, 0x0, true,
  359. BPF_FD_TYPE_UPROBE,
  360. BPF_FD_TYPE_URETPROBE,
  361. buf, sizeof(buf)));
  362. /* test debug fs uprobe */
  363. CHECK_AND_RET(test_debug_fs_uprobe((char *)argv[0], uprobe_file_offset,
  364. false));
  365. CHECK_AND_RET(test_debug_fs_uprobe((char *)argv[0], uprobe_file_offset,
  366. true));
  367. err = 0;
  368. cleanup:
  369. for (i--; i >= 0; i--)
  370. bpf_link__destroy(links[i]);
  371. bpf_object__close(obj);
  372. return err;
  373. }