blob: 4ce88e37dd63d73c0c0bd8fced63e05f11b517bd [file] [log] [blame]
Wang Nana0748652016-11-26 07:03:28 +00001/*
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)();
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, .p_hook_func = &__perf_hook_func_##name};
45#include "perf-hooks-list.h"
46#undef PERF_HOOK
47
48#define PERF_HOOK(name) \
49 &__perf_hook_desc_##name,
50
51static struct perf_hook_desc *perf_hooks[] = {
52#include "perf-hooks-list.h"
53};
54#undef PERF_HOOK
55
56int perf_hooks__set_hook(const char *hook_name,
57 perf_hook_func_t hook_func)
58{
59 unsigned int i;
60
61 for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
62 if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
63 continue;
64
65 if (*(perf_hooks[i]->p_hook_func))
66 pr_warning("Overwrite existing hook: %s\n", hook_name);
67 *(perf_hooks[i]->p_hook_func) = hook_func;
68 return 0;
69 }
70 return -ENOENT;
71}
72
73perf_hook_func_t perf_hooks__get_hook(const char *hook_name)
74{
75 unsigned int i;
76
77 for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
78 if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
79 continue;
80
81 return *(perf_hooks[i]->p_hook_func);
82 }
83 return ERR_PTR(-ENOENT);
84}