sdt.c 2.8 KB

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