sdt.c 2.7 KB

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