Commit | Line | Data |
---|---|---|
ce5aef0b MD |
1 | /* |
2 | * ltt-probes.c | |
3 | * | |
ce5aef0b MD |
4 | * Holds LTTng probes registry. |
5 | * | |
e92f3e28 MD |
6 | * Copyright 2010-2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; only | |
11 | * version 2.1 of the License. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
ce5aef0b MD |
21 | */ |
22 | ||
8d8a24c8 MD |
23 | #include <string.h> |
24 | #include <errno.h> | |
25 | #include <urcu/list.h> | |
1f18504e | 26 | #include <urcu/hlist.h> |
4318ae1b | 27 | #include <lttng/ust-events.h> |
902e1379 | 28 | #include <lttng/tracepoint.h> |
d8de1354 | 29 | #include "tracepoint-internal.h" |
a3bb4b27 | 30 | #include <assert.h> |
c8fcf224 | 31 | #include <helper.h> |
48740cab | 32 | #include <ctype.h> |
ce5aef0b | 33 | |
8165c8da | 34 | #include "ltt-tracer-core.h" |
1f18504e MD |
35 | #include "jhash.h" |
36 | #include "error.h" | |
8165c8da MD |
37 | |
38 | /* | |
17dfb34b | 39 | * probe list is protected by ust_lock()/ust_unlock(). |
8165c8da | 40 | */ |
8d8a24c8 | 41 | static CDS_LIST_HEAD(probe_list); |
ce5aef0b | 42 | |
df854e41 MD |
43 | static |
44 | const struct lttng_probe_desc *find_provider(const char *provider) | |
45 | { | |
46 | struct lttng_probe_desc *iter; | |
47 | ||
48 | cds_list_for_each_entry(iter, &probe_list, head) { | |
49 | if (!strcmp(iter->provider, provider)) | |
50 | return iter; | |
51 | } | |
52 | return NULL; | |
53 | } | |
54 | ||
ce5aef0b MD |
55 | static |
56 | const struct lttng_event_desc *find_event(const char *name) | |
57 | { | |
58 | struct lttng_probe_desc *probe_desc; | |
59 | int i; | |
60 | ||
8d8a24c8 | 61 | cds_list_for_each_entry(probe_desc, &probe_list, head) { |
ce5aef0b | 62 | for (i = 0; i < probe_desc->nr_events; i++) { |
ff412fb5 MD |
63 | if (!strncmp(probe_desc->event_desc[i]->name, name, |
64 | LTTNG_UST_SYM_NAME_LEN - 1)) | |
df854e41 | 65 | return probe_desc->event_desc[i]; |
ce5aef0b MD |
66 | } |
67 | } | |
68 | return NULL; | |
69 | } | |
70 | ||
71 | int ltt_probe_register(struct lttng_probe_desc *desc) | |
72 | { | |
df854e41 | 73 | struct lttng_probe_desc *iter; |
ce5aef0b MD |
74 | int ret = 0; |
75 | int i; | |
76 | ||
17dfb34b | 77 | ust_lock(); |
df854e41 MD |
78 | if (find_provider(desc->provider)) { |
79 | ret = -EEXIST; | |
80 | goto end; | |
81 | } | |
ce5aef0b MD |
82 | /* |
83 | * TODO: This is O(N^2). Turn into a hash table when probe registration | |
84 | * overhead becomes an issue. | |
85 | */ | |
86 | for (i = 0; i < desc->nr_events; i++) { | |
df854e41 | 87 | if (find_event(desc->event_desc[i]->name)) { |
ce5aef0b MD |
88 | ret = -EEXIST; |
89 | goto end; | |
90 | } | |
91 | } | |
df854e41 MD |
92 | |
93 | /* | |
94 | * We sort the providers by struct lttng_probe_desc pointer | |
95 | * address. | |
96 | */ | |
97 | cds_list_for_each_entry_reverse(iter, &probe_list, head) { | |
98 | BUG_ON(iter == desc); /* Should never be in the list twice */ | |
99 | if (iter < desc) { | |
100 | /* We belong to the location right after iter. */ | |
101 | cds_list_add(&desc->head, &iter->head); | |
102 | goto desc_added; | |
103 | } | |
104 | } | |
105 | /* We should be added at the head of the list */ | |
8d8a24c8 | 106 | cds_list_add(&desc->head, &probe_list); |
df854e41 | 107 | desc_added: |
e81a53af MD |
108 | DBG("just registered probe %s containing %u events", |
109 | desc->provider, desc->nr_events); | |
8165c8da MD |
110 | /* |
111 | * fix the events awaiting probe load. | |
112 | */ | |
113 | for (i = 0; i < desc->nr_events; i++) { | |
68755429 MD |
114 | const struct lttng_event_desc *ed; |
115 | ||
116 | ed = desc->event_desc[i]; | |
117 | DBG("Registered event probe \"%s\" with signature \"%s\"", | |
118 | ed->name, ed->signature); | |
119 | ret = pending_probe_fix_events(ed); | |
8165c8da MD |
120 | assert(!ret); |
121 | } | |
ce5aef0b | 122 | end: |
17dfb34b | 123 | ust_unlock(); |
ce5aef0b MD |
124 | return ret; |
125 | } | |
ce5aef0b MD |
126 | |
127 | void ltt_probe_unregister(struct lttng_probe_desc *desc) | |
128 | { | |
17dfb34b | 129 | ust_lock(); |
8d8a24c8 | 130 | cds_list_del(&desc->head); |
e81a53af | 131 | DBG("just unregistered probe %s", desc->provider); |
17dfb34b | 132 | ust_unlock(); |
ce5aef0b | 133 | } |
ce5aef0b | 134 | |
8165c8da MD |
135 | /* |
136 | * called with UST lock held. | |
137 | */ | |
ce5aef0b MD |
138 | const struct lttng_event_desc *ltt_event_get(const char *name) |
139 | { | |
140 | const struct lttng_event_desc *event; | |
ce5aef0b | 141 | |
ce5aef0b | 142 | event = find_event(name); |
ce5aef0b MD |
143 | if (!event) |
144 | return NULL; | |
ce5aef0b MD |
145 | return event; |
146 | } | |
ce5aef0b MD |
147 | |
148 | void ltt_event_put(const struct lttng_event_desc *event) | |
149 | { | |
ce5aef0b | 150 | } |
c8fcf224 MD |
151 | |
152 | void ltt_probes_prune_event_list(struct lttng_ust_tracepoint_list *list) | |
153 | { | |
154 | struct tp_list_entry *list_entry, *tmp; | |
155 | ||
156 | cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) { | |
157 | cds_list_del(&list_entry->head); | |
158 | free(list_entry); | |
159 | } | |
160 | } | |
161 | ||
162 | /* | |
163 | * called with UST lock held. | |
164 | */ | |
165 | int ltt_probes_get_event_list(struct lttng_ust_tracepoint_list *list) | |
166 | { | |
167 | struct lttng_probe_desc *probe_desc; | |
168 | int i; | |
169 | ||
170 | CDS_INIT_LIST_HEAD(&list->head); | |
171 | cds_list_for_each_entry(probe_desc, &probe_list, head) { | |
172 | for (i = 0; i < probe_desc->nr_events; i++) { | |
173 | struct tp_list_entry *list_entry; | |
174 | ||
175 | list_entry = zmalloc(sizeof(*list_entry)); | |
176 | if (!list_entry) | |
177 | goto err_nomem; | |
178 | cds_list_add(&list_entry->head, &list->head); | |
179 | strncpy(list_entry->tp.name, | |
180 | probe_desc->event_desc[i]->name, | |
181 | LTTNG_UST_SYM_NAME_LEN); | |
182 | list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
183 | if (!probe_desc->event_desc[i]->loglevel) { | |
882a56d7 | 184 | list_entry->tp.loglevel = TRACE_DEFAULT; |
c8fcf224 | 185 | } else { |
882a56d7 | 186 | list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel); |
c8fcf224 MD |
187 | } |
188 | } | |
189 | } | |
190 | if (cds_list_empty(&list->head)) | |
191 | list->iter = NULL; | |
192 | else | |
193 | list->iter = | |
194 | cds_list_first_entry(&list->head, struct tp_list_entry, head); | |
195 | return 0; | |
196 | ||
197 | err_nomem: | |
198 | ltt_probes_prune_event_list(list); | |
199 | return -ENOMEM; | |
200 | } | |
201 | ||
202 | /* | |
203 | * Return current iteration position, advance internal iterator to next. | |
204 | * Return NULL if end of list. | |
205 | */ | |
206 | struct lttng_ust_tracepoint_iter * | |
207 | lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list) | |
208 | { | |
209 | struct tp_list_entry *entry; | |
210 | ||
211 | if (!list->iter) | |
212 | return NULL; | |
213 | entry = list->iter; | |
214 | if (entry->head.next == &list->head) | |
215 | list->iter = NULL; | |
216 | else | |
217 | list->iter = cds_list_entry(entry->head.next, | |
218 | struct tp_list_entry, head); | |
219 | return &entry->tp; | |
220 | } | |
1f18504e | 221 | |
e6c12e3d MD |
222 | /* |
223 | * marshall all probes/all events and create those that fit the | |
224 | * wildcard. Add them to the events list as created. | |
225 | */ | |
457a6b58 | 226 | void ltt_probes_create_wildcard_events(struct wildcard_entry *entry, |
e6c12e3d MD |
227 | struct session_wildcard *wildcard) |
228 | { | |
229 | struct lttng_probe_desc *probe_desc; | |
230 | struct lttng_ust_event event_param; | |
231 | int i; | |
232 | ||
233 | cds_list_for_each_entry(probe_desc, &probe_list, head) { | |
234 | for (i = 0; i < probe_desc->nr_events; i++) { | |
235 | const struct lttng_event_desc *event_desc; | |
236 | int match = 0; | |
237 | ||
238 | event_desc = probe_desc->event_desc[i]; | |
239 | /* compare excluding final '*' */ | |
240 | assert(strlen(entry->name) > 0); | |
241 | if (strcmp(event_desc->name, "lttng_ust:metadata") | |
242 | && (strlen(entry->name) == 1 | |
243 | || !strncmp(event_desc->name, entry->name, | |
244 | strlen(entry->name) - 1))) { | |
457a6b58 MD |
245 | if (ltt_loglevel_match(event_desc, |
246 | entry->loglevel_type, | |
247 | entry->loglevel)) { | |
248 | match = 1; | |
249 | } | |
e6c12e3d MD |
250 | } |
251 | if (match) { | |
252 | struct ltt_event *ev; | |
253 | int ret; | |
254 | ||
255 | memcpy(&event_param, &wildcard->event_param, | |
256 | sizeof(event_param)); | |
7a673d99 | 257 | strncpy(event_param.name, |
e6c12e3d MD |
258 | event_desc->name, |
259 | sizeof(event_param.name)); | |
7a673d99 | 260 | event_param.name[sizeof(event_param.name) - 1] = '\0'; |
e6c12e3d MD |
261 | /* create event */ |
262 | ret = ltt_event_create(wildcard->chan, | |
263 | &event_param, NULL, | |
264 | &ev); | |
265 | if (ret) { | |
266 | DBG("Error creating event"); | |
267 | continue; | |
268 | } | |
269 | cds_list_add(&ev->wildcard_list, | |
270 | &wildcard->events); | |
271 | } | |
272 | } | |
273 | } | |
274 | } | |
275 |