Commit | Line | Data |
---|---|---|
e11fcffc MD |
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" | |
730bf2af | 29 | #include "getenv.h" |
e11fcffc MD |
30 | |
31 | struct lttng_trace_clock *lttng_trace_clock; | |
32 | ||
33 | static | |
34 | struct lttng_trace_clock user_tc; | |
35 | ||
36 | int lttng_ust_trace_clock_set_read64_cb(uint64_t (*read64)(void)) | |
37 | { | |
38 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
39 | return -EBUSY; | |
40 | user_tc.read64 = read64; | |
41 | return 0; | |
42 | } | |
43 | ||
44 | int lttng_ust_trace_clock_set_freq_cb(uint64_t (*freq)(void)) | |
45 | { | |
46 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
47 | return -EBUSY; | |
48 | user_tc.freq = freq; | |
49 | return 0; | |
50 | } | |
51 | ||
52 | int lttng_ust_trace_clock_set_uuid_cb(int (*uuid)(char *uuid)) | |
53 | { | |
54 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
55 | return -EBUSY; | |
56 | user_tc.uuid = uuid; | |
57 | return 0; | |
58 | } | |
59 | ||
60 | int lttng_ust_trace_clock_set_name_cb(const char *(*name)(void)) | |
61 | { | |
62 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
63 | return -EBUSY; | |
64 | user_tc.name = name; | |
65 | return 0; | |
66 | } | |
67 | ||
68 | int lttng_ust_trace_clock_set_description_cb(const char *(*description)(void)) | |
69 | { | |
70 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
71 | return -EBUSY; | |
72 | user_tc.description = description; | |
73 | return 0; | |
74 | } | |
75 | ||
76 | int lttng_ust_enable_trace_clock_override(void) | |
77 | { | |
78 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
79 | return -EBUSY; | |
80 | if (!user_tc.read64) | |
81 | return -EINVAL; | |
82 | if (!user_tc.freq) | |
83 | return -EINVAL; | |
84 | if (!user_tc.name) | |
85 | return -EINVAL; | |
86 | if (!user_tc.description) | |
87 | return -EINVAL; | |
88 | /* Use default uuid cb when NULL */ | |
89 | cmm_smp_mb(); /* Store callbacks before trace clock */ | |
90 | CMM_STORE_SHARED(lttng_trace_clock, &user_tc); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | void lttng_ust_clock_init(void) | |
95 | { | |
96 | const char *libname; | |
97 | void *handle; | |
98 | void (*libinit)(void); | |
99 | ||
100 | ||
730bf2af | 101 | libname = lttng_secure_getenv("LTTNG_UST_CLOCK_PLUGIN"); |
e11fcffc MD |
102 | if (!libname) |
103 | return; | |
104 | handle = dlopen(libname, RTLD_NOW); | |
105 | if (!handle) { | |
106 | PERROR("Cannot load LTTng UST clock override library %s", | |
107 | libname); | |
108 | return; | |
109 | } | |
110 | dlerror(); | |
111 | libinit = (void (*)(void)) dlsym(handle, | |
112 | "lttng_ust_clock_plugin_init"); | |
113 | if (!libinit) { | |
114 | PERROR("Cannot find LTTng UST clock override library %s initialization function lttng_ust_clock_plugin_init()", | |
115 | libname); | |
116 | return; | |
117 | } | |
118 | libinit(); | |
119 | } |