xref: /linux/tools/perf/tests/perf-hooks.c (revision e7af7b15121ca08c31a0ab9df71a41b4c53365b4)
1 #include <signal.h>
2 #include <stdlib.h>
3 
4 #include "tests.h"
5 #include "debug.h"
6 #include "util.h"
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 
18 static int hook_flags;
19 
20 static void the_hook(void)
21 {
22 	int *p = NULL;
23 
24 	hook_flags = 1234;
25 
26 	/* Generate a segfault, test perf_hooks__recover */
27 	*p = 0;
28 }
29 
30 int test__perf_hooks(int subtest __maybe_unused)
31 {
32 	signal(SIGSEGV, sigsegv_handler);
33 	perf_hooks__set_hook("test", the_hook);
34 	perf_hooks__invoke_test();
35 
36 	/* hook is triggered? */
37 	if (hook_flags != 1234)
38 		return TEST_FAIL;
39 
40 	/* the buggy hook is removed? */
41 	if (perf_hooks__get_hook("test"))
42 		return TEST_FAIL;
43 	return TEST_OK;
44 }
45