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