xref: /openbmc/linux/tools/perf/util/perf-hooks.c (revision ecd25094)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * perf_hooks.c
4  *
5  * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com>
6  * Copyright (C) 2016 Huawei Inc.
7  */
8 
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <setjmp.h>
13 #include <linux/err.h>
14 #include <linux/kernel.h>
15 #include "util/util.h"
16 #include "util/debug.h"
17 #include "util/perf-hooks.h"
18 
19 static sigjmp_buf jmpbuf;
20 static const struct perf_hook_desc *current_perf_hook;
21 
22 void perf_hooks__invoke(const struct perf_hook_desc *desc)
23 {
24 	if (!(desc && desc->p_hook_func && *desc->p_hook_func))
25 		return;
26 
27 	if (sigsetjmp(jmpbuf, 1)) {
28 		pr_warning("Fatal error (SEGFAULT) in perf hook '%s'\n",
29 			   desc->hook_name);
30 		*(current_perf_hook->p_hook_func) = NULL;
31 	} else {
32 		current_perf_hook = desc;
33 		(**desc->p_hook_func)(desc->hook_ctx);
34 	}
35 	current_perf_hook = NULL;
36 }
37 
38 void perf_hooks__recover(void)
39 {
40 	if (current_perf_hook)
41 		siglongjmp(jmpbuf, 1);
42 }
43 
44 #define PERF_HOOK(name)					\
45 perf_hook_func_t __perf_hook_func_##name = NULL;	\
46 struct perf_hook_desc __perf_hook_desc_##name =		\
47 	{.hook_name = #name,				\
48 	 .p_hook_func = &__perf_hook_func_##name,	\
49 	 .hook_ctx = NULL};
50 #include "perf-hooks-list.h"
51 #undef PERF_HOOK
52 
53 #define PERF_HOOK(name)		\
54 	&__perf_hook_desc_##name,
55 
56 static struct perf_hook_desc *perf_hooks[] = {
57 #include "perf-hooks-list.h"
58 };
59 #undef PERF_HOOK
60 
61 int perf_hooks__set_hook(const char *hook_name,
62 			 perf_hook_func_t hook_func,
63 			 void *hook_ctx)
64 {
65 	unsigned int i;
66 
67 	for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
68 		if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
69 			continue;
70 
71 		if (*(perf_hooks[i]->p_hook_func))
72 			pr_warning("Overwrite existing hook: %s\n", hook_name);
73 		*(perf_hooks[i]->p_hook_func) = hook_func;
74 		perf_hooks[i]->hook_ctx = hook_ctx;
75 		return 0;
76 	}
77 	return -ENOENT;
78 }
79 
80 perf_hook_func_t perf_hooks__get_hook(const char *hook_name)
81 {
82 	unsigned int i;
83 
84 	for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
85 		if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
86 			continue;
87 
88 		return *(perf_hooks[i]->p_hook_func);
89 	}
90 	return ERR_PTR(-ENOENT);
91 }
92