sdt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <limits.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <sys/epoll.h>
  8. #include <util/symbol.h>
  9. #include <linux/filter.h>
  10. #include "tests.h"
  11. #include "debug.h"
  12. #include "probe-file.h"
  13. #include "build-id.h"
  14. #include "util.h"
  15. /* To test SDT event, we need libelf support to scan elf binary */
  16. #if defined(HAVE_SDT_EVENT) && defined(HAVE_LIBELF_SUPPORT)
  17. #include <sys/sdt.h>
  18. static int target_function(void)
  19. {
  20. DTRACE_PROBE(perf, test_target);
  21. return TEST_OK;
  22. }
  23. /* Copied from builtin-buildid-cache.c */
  24. static int build_id_cache__add_file(const char *filename)
  25. {
  26. char sbuild_id[SBUILD_ID_SIZE];
  27. struct build_id bid;
  28. int err;
  29. err = filename__read_build_id(filename, &bid);
  30. if (err < 0) {
  31. pr_debug("Failed to read build id of %s\n", filename);
  32. return err;
  33. }
  34. build_id__sprintf(&bid, sbuild_id);
  35. err = build_id_cache__add_s(sbuild_id, filename, NULL, false, false);
  36. if (err < 0)
  37. pr_debug("Failed to add build id cache of %s\n", filename);
  38. return err;
  39. }
  40. static char *get_self_path(void)
  41. {
  42. char *buf = calloc(PATH_MAX, sizeof(char));
  43. if (buf && readlink("/proc/self/exe", buf, PATH_MAX - 1) < 0) {
  44. pr_debug("Failed to get correct path of perf\n");
  45. free(buf);
  46. return NULL;
  47. }
  48. return buf;
  49. }
  50. static int search_cached_probe(const char *target,
  51. const char *group, const char *event)
  52. {
  53. struct probe_cache *cache = probe_cache__new(target, NULL);
  54. int ret = 0;
  55. if (!cache) {
  56. pr_debug("Failed to open probe cache of %s\n", target);
  57. return -EINVAL;
  58. }
  59. if (!probe_cache__find_by_name(cache, group, event)) {
  60. pr_debug("Failed to find %s:%s in the cache\n", group, event);
  61. ret = -ENOENT;
  62. }
  63. probe_cache__delete(cache);
  64. return ret;
  65. }
  66. static int test__sdt_event(struct test_suite *test __maybe_unused, int subtests __maybe_unused)
  67. {
  68. int ret = TEST_FAIL;
  69. char __tempdir[] = "./test-buildid-XXXXXX";
  70. char *tempdir = NULL, *myself = get_self_path();
  71. if (myself == NULL || mkdtemp(__tempdir) == NULL) {
  72. pr_debug("Failed to make a tempdir for build-id cache\n");
  73. goto error;
  74. }
  75. /* Note that buildid_dir must be an absolute path */
  76. tempdir = realpath(__tempdir, NULL);
  77. if (tempdir == NULL)
  78. goto error_rmdir;
  79. /* At first, scan itself */
  80. set_buildid_dir(tempdir);
  81. if (build_id_cache__add_file(myself) < 0)
  82. goto error_rmdir;
  83. /* Open a cache and make sure the SDT is stored */
  84. if (search_cached_probe(myself, "sdt_perf", "test_target") < 0)
  85. goto error_rmdir;
  86. /* TBD: probing on the SDT event and collect logs */
  87. /* Call the target and get an event */
  88. ret = target_function();
  89. error_rmdir:
  90. /* Cleanup temporary buildid dir */
  91. rm_rf(__tempdir);
  92. error:
  93. free(tempdir);
  94. free(myself);
  95. return ret;
  96. }
  97. #else
  98. static int test__sdt_event(struct test_suite *test __maybe_unused, int subtests __maybe_unused)
  99. {
  100. pr_debug("Skip SDT event test because SDT support is not compiled\n");
  101. return TEST_SKIP;
  102. }
  103. #endif
  104. DEFINE_SUITE("Probe SDT events", sdt_event);