task_fd_query_user.c 11 KB

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