Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * perf_hooks.c
0004  *
0005  * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com>
0006  * Copyright (C) 2016 Huawei Inc.
0007  */
0008 
0009 #include <errno.h>
0010 #include <stdlib.h>
0011 #include <string.h>
0012 #include <setjmp.h>
0013 #include <linux/err.h>
0014 #include <linux/kernel.h>
0015 #include "util/debug.h"
0016 #include "util/perf-hooks.h"
0017 
0018 static sigjmp_buf jmpbuf;
0019 static const struct perf_hook_desc *current_perf_hook;
0020 
0021 void perf_hooks__invoke(const struct perf_hook_desc *desc)
0022 {
0023     if (!(desc && desc->p_hook_func && *desc->p_hook_func))
0024         return;
0025 
0026     if (sigsetjmp(jmpbuf, 1)) {
0027         pr_warning("Fatal error (SEGFAULT) in perf hook '%s'\n",
0028                desc->hook_name);
0029         *(current_perf_hook->p_hook_func) = NULL;
0030     } else {
0031         current_perf_hook = desc;
0032         (**desc->p_hook_func)(desc->hook_ctx);
0033     }
0034     current_perf_hook = NULL;
0035 }
0036 
0037 void perf_hooks__recover(void)
0038 {
0039     if (current_perf_hook)
0040         siglongjmp(jmpbuf, 1);
0041 }
0042 
0043 #define PERF_HOOK(name)                 \
0044 perf_hook_func_t __perf_hook_func_##name = NULL;    \
0045 struct perf_hook_desc __perf_hook_desc_##name =     \
0046     {.hook_name = #name,                \
0047      .p_hook_func = &__perf_hook_func_##name,   \
0048      .hook_ctx = NULL};
0049 #include "perf-hooks-list.h"
0050 #undef PERF_HOOK
0051 
0052 #define PERF_HOOK(name)     \
0053     &__perf_hook_desc_##name,
0054 
0055 static struct perf_hook_desc *perf_hooks[] = {
0056 #include "perf-hooks-list.h"
0057 };
0058 #undef PERF_HOOK
0059 
0060 int perf_hooks__set_hook(const char *hook_name,
0061              perf_hook_func_t hook_func,
0062              void *hook_ctx)
0063 {
0064     unsigned int i;
0065 
0066     for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
0067         if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
0068             continue;
0069 
0070         if (*(perf_hooks[i]->p_hook_func))
0071             pr_warning("Overwrite existing hook: %s\n", hook_name);
0072         *(perf_hooks[i]->p_hook_func) = hook_func;
0073         perf_hooks[i]->hook_ctx = hook_ctx;
0074         return 0;
0075     }
0076     return -ENOENT;
0077 }
0078 
0079 perf_hook_func_t perf_hooks__get_hook(const char *hook_name)
0080 {
0081     unsigned int i;
0082 
0083     for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
0084         if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
0085             continue;
0086 
0087         return *(perf_hooks[i]->p_hook_func);
0088     }
0089     return ERR_PTR(-ENOENT);
0090 }