aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorWang Nan <wangnan0@huawei.com>2016-11-26 02:03:29 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2016-12-05 13:51:42 -0500
commit8ad85e9e6fdaf996bf3ff60303ea00e696bcdd36 (patch)
treedb08e2b766f375c3037d8dcc39d730c3b58b1dec /tools
parentbaa1973ebcf6a7bd15522a5b6a35a8fefd6cb232 (diff)
perf tools: Pass context to perf hook functions
Pass a pointer to perf hook functions so they receive context information during setup. Signed-off-by: Wang Nan <wangnan0@huawei.com> Cc: Alexei Starovoitov <ast@fb.com> Cc: He Kuang <hekuang@huawei.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Joe Stringer <joe@ovn.org> Cc: Zefan Li <lizefan@huawei.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/20161126070354.141764-6-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/tests/perf-hooks.c14
-rw-r--r--tools/perf/util/perf-hooks.c10
-rw-r--r--tools/perf/util/perf-hooks.h6
3 files changed, 20 insertions, 10 deletions
diff --git a/tools/perf/tests/perf-hooks.c b/tools/perf/tests/perf-hooks.c
index 9338cb2c25ab..665ecc19671c 100644
--- a/tools/perf/tests/perf-hooks.c
+++ b/tools/perf/tests/perf-hooks.c
@@ -15,13 +15,13 @@ static void sigsegv_handler(int sig __maybe_unused)
15 exit(-1); 15 exit(-1);
16} 16}
17 17
18static int hook_flags;
19 18
20static void the_hook(void) 19static void the_hook(void *_hook_flags)
21{ 20{
21 int *hook_flags = _hook_flags;
22 int *p = NULL; 22 int *p = NULL;
23 23
24 hook_flags = 1234; 24 *hook_flags = 1234;
25 25
26 /* Generate a segfault, test perf_hooks__recover */ 26 /* Generate a segfault, test perf_hooks__recover */
27 *p = 0; 27 *p = 0;
@@ -29,13 +29,17 @@ static void the_hook(void)
29 29
30int test__perf_hooks(int subtest __maybe_unused) 30int test__perf_hooks(int subtest __maybe_unused)
31{ 31{
32 int hook_flags = 0;
33
32 signal(SIGSEGV, sigsegv_handler); 34 signal(SIGSEGV, sigsegv_handler);
33 perf_hooks__set_hook("test", the_hook); 35 perf_hooks__set_hook("test", the_hook, &hook_flags);
34 perf_hooks__invoke_test(); 36 perf_hooks__invoke_test();
35 37
36 /* hook is triggered? */ 38 /* hook is triggered? */
37 if (hook_flags != 1234) 39 if (hook_flags != 1234) {
40 pr_debug("Setting failed: %d (%p)\n", hook_flags, &hook_flags);
38 return TEST_FAIL; 41 return TEST_FAIL;
42 }
39 43
40 /* the buggy hook is removed? */ 44 /* the buggy hook is removed? */
41 if (perf_hooks__get_hook("test")) 45 if (perf_hooks__get_hook("test"))
diff --git a/tools/perf/util/perf-hooks.c b/tools/perf/util/perf-hooks.c
index 4ce88e37dd63..cb368306b12b 100644
--- a/tools/perf/util/perf-hooks.c
+++ b/tools/perf/util/perf-hooks.c
@@ -27,7 +27,7 @@ void perf_hooks__invoke(const struct perf_hook_desc *desc)
27 *(current_perf_hook->p_hook_func) = NULL; 27 *(current_perf_hook->p_hook_func) = NULL;
28 } else { 28 } else {
29 current_perf_hook = desc; 29 current_perf_hook = desc;
30 (**desc->p_hook_func)(); 30 (**desc->p_hook_func)(desc->hook_ctx);
31 } 31 }
32 current_perf_hook = NULL; 32 current_perf_hook = NULL;
33} 33}
@@ -41,7 +41,9 @@ void perf_hooks__recover(void)
41#define PERF_HOOK(name) \ 41#define PERF_HOOK(name) \
42perf_hook_func_t __perf_hook_func_##name = NULL; \ 42perf_hook_func_t __perf_hook_func_##name = NULL; \
43struct perf_hook_desc __perf_hook_desc_##name = \ 43struct perf_hook_desc __perf_hook_desc_##name = \
44 {.hook_name = #name, .p_hook_func = &__perf_hook_func_##name}; 44 {.hook_name = #name, \
45 .p_hook_func = &__perf_hook_func_##name, \
46 .hook_ctx = NULL};
45#include "perf-hooks-list.h" 47#include "perf-hooks-list.h"
46#undef PERF_HOOK 48#undef PERF_HOOK
47 49
@@ -54,7 +56,8 @@ static struct perf_hook_desc *perf_hooks[] = {
54#undef PERF_HOOK 56#undef PERF_HOOK
55 57
56int perf_hooks__set_hook(const char *hook_name, 58int perf_hooks__set_hook(const char *hook_name,
57 perf_hook_func_t hook_func) 59 perf_hook_func_t hook_func,
60 void *hook_ctx)
58{ 61{
59 unsigned int i; 62 unsigned int i;
60 63
@@ -65,6 +68,7 @@ int perf_hooks__set_hook(const char *hook_name,
65 if (*(perf_hooks[i]->p_hook_func)) 68 if (*(perf_hooks[i]->p_hook_func))
66 pr_warning("Overwrite existing hook: %s\n", hook_name); 69 pr_warning("Overwrite existing hook: %s\n", hook_name);
67 *(perf_hooks[i]->p_hook_func) = hook_func; 70 *(perf_hooks[i]->p_hook_func) = hook_func;
71 perf_hooks[i]->hook_ctx = hook_ctx;
68 return 0; 72 return 0;
69 } 73 }
70 return -ENOENT; 74 return -ENOENT;
diff --git a/tools/perf/util/perf-hooks.h b/tools/perf/util/perf-hooks.h
index 1d482b26b4b9..838d5797bc1e 100644
--- a/tools/perf/util/perf-hooks.h
+++ b/tools/perf/util/perf-hooks.h
@@ -5,10 +5,11 @@
5extern "C" { 5extern "C" {
6#endif 6#endif
7 7
8typedef void (*perf_hook_func_t)(void); 8typedef void (*perf_hook_func_t)(void *ctx);
9struct perf_hook_desc { 9struct perf_hook_desc {
10 const char * const hook_name; 10 const char * const hook_name;
11 perf_hook_func_t * const p_hook_func; 11 perf_hook_func_t * const p_hook_func;
12 void *hook_ctx;
12}; 13};
13 14
14extern void perf_hooks__invoke(const struct perf_hook_desc *); 15extern void perf_hooks__invoke(const struct perf_hook_desc *);
@@ -26,7 +27,8 @@ static inline void perf_hooks__invoke_##name(void) \
26 27
27extern int 28extern int
28perf_hooks__set_hook(const char *hook_name, 29perf_hooks__set_hook(const char *hook_name,
29 perf_hook_func_t hook_func); 30 perf_hook_func_t hook_func,
31 void *hook_ctx);
30 32
31extern perf_hook_func_t 33extern perf_hook_func_t
32perf_hooks__get_hook(const char *hook_name); 34perf_hooks__get_hook(const char *hook_name);