Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 2 | #include <signal.h> |
| 3 | #include <stdlib.h> |
| 4 | |
| 5 | #include "tests.h" |
| 6 | #include "debug.h" |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 7 | #include "perf-hooks.h" |
| 8 | |
| 9 | static void sigsegv_handler(int sig __maybe_unused) |
| 10 | { |
| 11 | pr_debug("SIGSEGV is observed as expected, try to recover.\n"); |
| 12 | perf_hooks__recover(); |
| 13 | signal(SIGSEGV, SIG_DFL); |
| 14 | raise(SIGSEGV); |
| 15 | exit(-1); |
| 16 | } |
| 17 | |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 18 | |
Wang Nan | 8ad85e9 | 2016-11-26 07:03:29 +0000 | [diff] [blame] | 19 | static void the_hook(void *_hook_flags) |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 20 | { |
Wang Nan | 8ad85e9 | 2016-11-26 07:03:29 +0000 | [diff] [blame] | 21 | int *hook_flags = _hook_flags; |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 22 | |
Wang Nan | 8ad85e9 | 2016-11-26 07:03:29 +0000 | [diff] [blame] | 23 | *hook_flags = 1234; |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 24 | |
| 25 | /* Generate a segfault, test perf_hooks__recover */ |
Ian Rogers | e3e2cf3 | 2019-09-25 12:59:24 -0700 | [diff] [blame] | 26 | raise(SIGSEGV); |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 29 | static int test__perf_hooks(struct test_suite *test __maybe_unused, int subtest __maybe_unused) |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 30 | { |
Wang Nan | 8ad85e9 | 2016-11-26 07:03:29 +0000 | [diff] [blame] | 31 | int hook_flags = 0; |
| 32 | |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 33 | signal(SIGSEGV, sigsegv_handler); |
Wang Nan | 8ad85e9 | 2016-11-26 07:03:29 +0000 | [diff] [blame] | 34 | perf_hooks__set_hook("test", the_hook, &hook_flags); |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 35 | perf_hooks__invoke_test(); |
| 36 | |
| 37 | /* hook is triggered? */ |
Wang Nan | 8ad85e9 | 2016-11-26 07:03:29 +0000 | [diff] [blame] | 38 | if (hook_flags != 1234) { |
| 39 | pr_debug("Setting failed: %d (%p)\n", hook_flags, &hook_flags); |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 40 | return TEST_FAIL; |
Wang Nan | 8ad85e9 | 2016-11-26 07:03:29 +0000 | [diff] [blame] | 41 | } |
Wang Nan | a074865 | 2016-11-26 07:03:28 +0000 | [diff] [blame] | 42 | |
| 43 | /* the buggy hook is removed? */ |
| 44 | if (perf_hooks__get_hook("test")) |
| 45 | return TEST_FAIL; |
| 46 | return TEST_OK; |
| 47 | } |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 48 | |
| 49 | DEFINE_SUITE("perf hooks", perf_hooks); |