4 * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com>
5 * Copyright (C) 2016 Huawei Inc.
11 #include <linux/err.h>
12 #include "util/util.h"
13 #include "util/debug.h"
14 #include "util/perf-hooks.h"
16 static sigjmp_buf jmpbuf;
17 static const struct perf_hook_desc *current_perf_hook;
19 void perf_hooks__invoke(const struct perf_hook_desc *desc)
21 if (!(desc && desc->p_hook_func && *desc->p_hook_func))
24 if (sigsetjmp(jmpbuf, 1)) {
25 pr_warning("Fatal error (SEGFAULT) in perf hook '%s'\n",
27 *(current_perf_hook->p_hook_func) = NULL;
29 current_perf_hook = desc;
30 (**desc->p_hook_func)(desc->hook_ctx);
32 current_perf_hook = NULL;
35 void perf_hooks__recover(void)
37 if (current_perf_hook)
38 siglongjmp(jmpbuf, 1);
41 #define PERF_HOOK(name) \
42 perf_hook_func_t __perf_hook_func_##name = NULL; \
43 struct perf_hook_desc __perf_hook_desc_##name = \
44 {.hook_name = #name, \
45 .p_hook_func = &__perf_hook_func_##name, \
47 #include "perf-hooks-list.h"
50 #define PERF_HOOK(name) \
51 &__perf_hook_desc_##name,
53 static struct perf_hook_desc *perf_hooks[] = {
54 #include "perf-hooks-list.h"
58 int perf_hooks__set_hook(const char *hook_name,
59 perf_hook_func_t hook_func,
64 for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
65 if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
68 if (*(perf_hooks[i]->p_hook_func))
69 pr_warning("Overwrite existing hook: %s\n", hook_name);
70 *(perf_hooks[i]->p_hook_func) = hook_func;
71 perf_hooks[i]->hook_ctx = hook_ctx;
77 perf_hook_func_t perf_hooks__get_hook(const char *hook_name)
81 for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
82 if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
85 return *(perf_hooks[i]->p_hook_func);
87 return ERR_PTR(-ENOENT);