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