| 1 | /* |
| 2 | * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; version 2.1 of |
| 7 | * the License. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <error.h> |
| 21 | #include <dlfcn.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <usterr-signal-safe.h> |
| 24 | #include <lttng/ust-clock.h> |
| 25 | #include <urcu/system.h> |
| 26 | #include <urcu/arch.h> |
| 27 | |
| 28 | #include "clock.h" |
| 29 | #include "getenv.h" |
| 30 | |
| 31 | struct lttng_trace_clock *lttng_trace_clock; |
| 32 | |
| 33 | static |
| 34 | struct lttng_trace_clock user_tc; |
| 35 | |
| 36 | static |
| 37 | void *clock_handle; |
| 38 | |
| 39 | int lttng_ust_trace_clock_set_read64_cb(uint64_t (*read64)(void)) |
| 40 | { |
| 41 | if (CMM_LOAD_SHARED(lttng_trace_clock)) |
| 42 | return -EBUSY; |
| 43 | user_tc.read64 = read64; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | int lttng_ust_trace_clock_set_freq_cb(uint64_t (*freq)(void)) |
| 48 | { |
| 49 | if (CMM_LOAD_SHARED(lttng_trace_clock)) |
| 50 | return -EBUSY; |
| 51 | user_tc.freq = freq; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | int lttng_ust_trace_clock_set_uuid_cb(int (*uuid)(char *uuid)) |
| 56 | { |
| 57 | if (CMM_LOAD_SHARED(lttng_trace_clock)) |
| 58 | return -EBUSY; |
| 59 | user_tc.uuid = uuid; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int lttng_ust_trace_clock_set_name_cb(const char *(*name)(void)) |
| 64 | { |
| 65 | if (CMM_LOAD_SHARED(lttng_trace_clock)) |
| 66 | return -EBUSY; |
| 67 | user_tc.name = name; |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int lttng_ust_trace_clock_set_description_cb(const char *(*description)(void)) |
| 72 | { |
| 73 | if (CMM_LOAD_SHARED(lttng_trace_clock)) |
| 74 | return -EBUSY; |
| 75 | user_tc.description = description; |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int lttng_ust_enable_trace_clock_override(void) |
| 80 | { |
| 81 | if (CMM_LOAD_SHARED(lttng_trace_clock)) |
| 82 | return -EBUSY; |
| 83 | if (!user_tc.read64) |
| 84 | return -EINVAL; |
| 85 | if (!user_tc.freq) |
| 86 | return -EINVAL; |
| 87 | if (!user_tc.name) |
| 88 | return -EINVAL; |
| 89 | if (!user_tc.description) |
| 90 | return -EINVAL; |
| 91 | /* Use default uuid cb when NULL */ |
| 92 | cmm_smp_mb(); /* Store callbacks before trace clock */ |
| 93 | CMM_STORE_SHARED(lttng_trace_clock, &user_tc); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | void lttng_ust_clock_init(void) |
| 98 | { |
| 99 | const char *libname; |
| 100 | void (*libinit)(void); |
| 101 | |
| 102 | if (clock_handle) |
| 103 | return; |
| 104 | libname = lttng_secure_getenv("LTTNG_UST_CLOCK_PLUGIN"); |
| 105 | if (!libname) |
| 106 | return; |
| 107 | clock_handle = dlopen(libname, RTLD_NOW); |
| 108 | if (!clock_handle) { |
| 109 | PERROR("Cannot load LTTng UST clock override library %s", |
| 110 | libname); |
| 111 | return; |
| 112 | } |
| 113 | dlerror(); |
| 114 | libinit = (void (*)(void)) dlsym(clock_handle, |
| 115 | "lttng_ust_clock_plugin_init"); |
| 116 | if (!libinit) { |
| 117 | PERROR("Cannot find LTTng UST clock override library %s initialization function lttng_ust_clock_plugin_init()", |
| 118 | libname); |
| 119 | return; |
| 120 | } |
| 121 | libinit(); |
| 122 | } |