perf-hooks.c 1006 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <signal.h>
  3. #include <stdlib.h>
  4. #include "tests.h"
  5. #include "debug.h"
  6. #include "util.h"
  7. #include "perf-hooks.h"
  8. static void sigsegv_handler(int sig __maybe_unused)
  9. {
  10. pr_debug("SIGSEGV is observed as expected, try to recover.\n");
  11. perf_hooks__recover();
  12. signal(SIGSEGV, SIG_DFL);
  13. raise(SIGSEGV);
  14. exit(-1);
  15. }
  16. static void the_hook(void *_hook_flags)
  17. {
  18. int *hook_flags = _hook_flags;
  19. *hook_flags = 1234;
  20. /* Generate a segfault, test perf_hooks__recover */
  21. raise(SIGSEGV);
  22. }
  23. int test__perf_hooks(struct test *test __maybe_unused, int subtest __maybe_unused)
  24. {
  25. int hook_flags = 0;
  26. signal(SIGSEGV, sigsegv_handler);
  27. perf_hooks__set_hook("test", the_hook, &hook_flags);
  28. perf_hooks__invoke_test();
  29. /* hook is triggered? */
  30. if (hook_flags != 1234) {
  31. pr_debug("Setting failed: %d (%p)\n", hook_flags, &hook_flags);
  32. return TEST_FAIL;
  33. }
  34. /* the buggy hook is removed? */
  35. if (perf_hooks__get_hook("test"))
  36. return TEST_FAIL;
  37. return TEST_OK;
  38. }