Merge tag 'block-5.9-2020-08-14' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / perf-hooks.c
blob7a0ab3507bd5f4cfe471a11ceb20d6d9a58b300c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * perf_hooks.c
5 * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com>
6 * Copyright (C) 2016 Huawei Inc.
7 */
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/debug.h"
16 #include "util/perf-hooks.h"
18 static sigjmp_buf jmpbuf;
19 static const struct perf_hook_desc *current_perf_hook;
21 void perf_hooks__invoke(const struct perf_hook_desc *desc)
23 if (!(desc && desc->p_hook_func && *desc->p_hook_func))
24 return;
26 if (sigsetjmp(jmpbuf, 1)) {
27 pr_warning("Fatal error (SEGFAULT) in perf hook '%s'\n",
28 desc->hook_name);
29 *(current_perf_hook->p_hook_func) = NULL;
30 } else {
31 current_perf_hook = desc;
32 (**desc->p_hook_func)(desc->hook_ctx);
34 current_perf_hook = NULL;
37 void perf_hooks__recover(void)
39 if (current_perf_hook)
40 siglongjmp(jmpbuf, 1);
43 #define PERF_HOOK(name) \
44 perf_hook_func_t __perf_hook_func_##name = NULL; \
45 struct perf_hook_desc __perf_hook_desc_##name = \
46 {.hook_name = #name, \
47 .p_hook_func = &__perf_hook_func_##name, \
48 .hook_ctx = NULL};
49 #include "perf-hooks-list.h"
50 #undef PERF_HOOK
52 #define PERF_HOOK(name) \
53 &__perf_hook_desc_##name,
55 static struct perf_hook_desc *perf_hooks[] = {
56 #include "perf-hooks-list.h"
58 #undef PERF_HOOK
60 int perf_hooks__set_hook(const char *hook_name,
61 perf_hook_func_t hook_func,
62 void *hook_ctx)
64 unsigned int i;
66 for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
67 if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
68 continue;
70 if (*(perf_hooks[i]->p_hook_func))
71 pr_warning("Overwrite existing hook: %s\n", hook_name);
72 *(perf_hooks[i]->p_hook_func) = hook_func;
73 perf_hooks[i]->hook_ctx = hook_ctx;
74 return 0;
76 return -ENOENT;
79 perf_hook_func_t perf_hooks__get_hook(const char *hook_name)
81 unsigned int i;
83 for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
84 if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
85 continue;
87 return *(perf_hooks[i]->p_hook_func);
89 return ERR_PTR(-ENOENT);