Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Wang Nan and committed by
Arnaldo Carvalho de Melo
8ad85e9e baa1973e

+20 -10
+9 -5
tools/perf/tests/perf-hooks.c
··· 15 15 exit(-1); 16 16 } 17 17 18 - static int hook_flags; 19 18 20 - static void the_hook(void) 19 + static void the_hook(void *_hook_flags) 21 20 { 21 + int *hook_flags = _hook_flags; 22 22 int *p = NULL; 23 23 24 - hook_flags = 1234; 24 + *hook_flags = 1234; 25 25 26 26 /* Generate a segfault, test perf_hooks__recover */ 27 27 *p = 0; ··· 29 29 30 30 int test__perf_hooks(int subtest __maybe_unused) 31 31 { 32 + int hook_flags = 0; 33 + 32 34 signal(SIGSEGV, sigsegv_handler); 33 - perf_hooks__set_hook("test", the_hook); 35 + perf_hooks__set_hook("test", the_hook, &hook_flags); 34 36 perf_hooks__invoke_test(); 35 37 36 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 41 return TEST_FAIL; 42 + } 39 43 40 44 /* the buggy hook is removed? */ 41 45 if (perf_hooks__get_hook("test"))
+7 -3
tools/perf/util/perf-hooks.c
··· 27 27 *(current_perf_hook->p_hook_func) = NULL; 28 28 } else { 29 29 current_perf_hook = desc; 30 - (**desc->p_hook_func)(); 30 + (**desc->p_hook_func)(desc->hook_ctx); 31 31 } 32 32 current_perf_hook = NULL; 33 33 } ··· 41 41 #define PERF_HOOK(name) \ 42 42 perf_hook_func_t __perf_hook_func_##name = NULL; \ 43 43 struct 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 47 #include "perf-hooks-list.h" 46 48 #undef PERF_HOOK 47 49 ··· 56 54 #undef PERF_HOOK 57 55 58 56 int perf_hooks__set_hook(const char *hook_name, 59 - perf_hook_func_t hook_func) 57 + perf_hook_func_t hook_func, 58 + void *hook_ctx) 60 59 { 61 60 unsigned int i; 62 61 ··· 68 65 if (*(perf_hooks[i]->p_hook_func)) 69 66 pr_warning("Overwrite existing hook: %s\n", hook_name); 70 67 *(perf_hooks[i]->p_hook_func) = hook_func; 68 + perf_hooks[i]->hook_ctx = hook_ctx; 71 69 return 0; 72 70 } 73 71 return -ENOENT;
+4 -2
tools/perf/util/perf-hooks.h
··· 5 5 extern "C" { 6 6 #endif 7 7 8 - typedef void (*perf_hook_func_t)(void); 8 + typedef void (*perf_hook_func_t)(void *ctx); 9 9 struct perf_hook_desc { 10 10 const char * const hook_name; 11 11 perf_hook_func_t * const p_hook_func; 12 + void *hook_ctx; 12 13 }; 13 14 14 15 extern void perf_hooks__invoke(const struct perf_hook_desc *); ··· 27 26 28 27 extern int 29 28 perf_hooks__set_hook(const char *hook_name, 30 - perf_hook_func_t hook_func); 29 + perf_hook_func_t hook_func, 30 + void *hook_ctx); 31 31 32 32 extern perf_hook_func_t 33 33 perf_hooks__get_hook(const char *hook_name);