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