task_fd_query_user.c 11 KB

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