Commit | Line | Data |
---|---|---|
97ee3a89 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
bdf64013 | 3 | * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
97ee3a89 | 4 | * |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
97ee3a89 | 8 | * |
d14d33bf AM |
9 | * This program 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 | |
12 | * GNU General Public License for more details. | |
97ee3a89 | 13 | * |
d14d33bf AM |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
97ee3a89 DG |
17 | */ |
18 | ||
6c1c0768 | 19 | #define _LGPL_SOURCE |
97ee3a89 DG |
20 | #include <stdio.h> |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
d9bf3ca4 | 24 | #include <inttypes.h> |
97ee3a89 | 25 | |
990570ed DG |
26 | #include <common/common.h> |
27 | #include <common/defaults.h> | |
97ee3a89 | 28 | |
7972aab2 | 29 | #include "buffer-registry.h" |
97ee3a89 | 30 | #include "trace-ust.h" |
0b2dc8df | 31 | #include "utils.h" |
a9ad0c8f | 32 | #include "ust-app.h" |
7c1d2758 | 33 | #include "agent.h" |
97ee3a89 | 34 | |
025faf73 DG |
35 | /* |
36 | * Match function for the events hash table lookup. | |
37 | * | |
38 | * Matches by name only. Used by the disable command. | |
39 | */ | |
18eace3b DG |
40 | int trace_ust_ht_match_event_by_name(struct cds_lfht_node *node, |
41 | const void *_key) | |
42 | { | |
43 | struct ltt_ust_event *event; | |
44 | const char *name; | |
45 | ||
46 | assert(node); | |
47 | assert(_key); | |
48 | ||
49 | event = caa_container_of(node, struct ltt_ust_event, node.node); | |
50 | name = _key; | |
51 | ||
52 | /* Event name */ | |
53 | if (strncmp(event->attr.name, name, sizeof(event->attr.name)) != 0) { | |
54 | goto no_match; | |
55 | } | |
56 | ||
025faf73 | 57 | /* Match */ |
18eace3b DG |
58 | return 1; |
59 | ||
60 | no_match: | |
61 | return 0; | |
62 | } | |
63 | ||
025faf73 DG |
64 | /* |
65 | * Match function for the hash table lookup. | |
66 | * | |
67 | * It matches an ust event based on three attributes which are the event name, | |
68 | * the filter bytecode and the loglevel. | |
69 | */ | |
18eace3b DG |
70 | int trace_ust_ht_match_event(struct cds_lfht_node *node, const void *_key) |
71 | { | |
72 | struct ltt_ust_event *event; | |
73 | const struct ltt_ust_ht_key *key; | |
2106efa0 | 74 | int ev_loglevel_value; |
19a97244 | 75 | int ll_match; |
18eace3b DG |
76 | |
77 | assert(node); | |
78 | assert(_key); | |
79 | ||
80 | event = caa_container_of(node, struct ltt_ust_event, node.node); | |
81 | key = _key; | |
2106efa0 | 82 | ev_loglevel_value = event->attr.loglevel; |
18eace3b | 83 | |
f19e9f8b | 84 | /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */ |
18eace3b DG |
85 | |
86 | /* Event name */ | |
87 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { | |
18eace3b DG |
88 | goto no_match; |
89 | } | |
90 | ||
b953b8cd | 91 | /* Event loglevel value and type. */ |
19a97244 PP |
92 | ll_match = loglevels_match(event->attr.loglevel_type, |
93 | ev_loglevel_value, key->loglevel_type, | |
94 | key->loglevel_value, LTTNG_UST_LOGLEVEL_ALL); | |
95 | ||
96 | if (!ll_match) { | |
b953b8cd | 97 | goto no_match; |
18eace3b DG |
98 | } |
99 | ||
100 | /* Only one of the filters is NULL, fail. */ | |
101 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { | |
18eace3b DG |
102 | goto no_match; |
103 | } | |
104 | ||
025faf73 DG |
105 | if (key->filter && event->filter) { |
106 | /* Both filters exists, check length followed by the bytecode. */ | |
107 | if (event->filter->len != key->filter->len || | |
108 | memcmp(event->filter->data, key->filter->data, | |
109 | event->filter->len) != 0) { | |
110 | goto no_match; | |
111 | } | |
18eace3b DG |
112 | } |
113 | ||
f19e9f8b JI |
114 | /* If only one of the exclusions is NULL, fail. */ |
115 | if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) { | |
116 | goto no_match; | |
117 | } | |
118 | ||
119 | if (key->exclusion && event->exclusion) { | |
a5b7e00c PP |
120 | size_t i; |
121 | ||
122 | /* Check exclusion counts first. */ | |
123 | if (event->exclusion->count != key->exclusion->count) { | |
f19e9f8b JI |
124 | goto no_match; |
125 | } | |
a5b7e00c PP |
126 | |
127 | /* Compare names individually. */ | |
128 | for (i = 0; i < event->exclusion->count; ++i) { | |
129 | size_t j; | |
130 | bool found = false; | |
131 | const char *name_ev = | |
132 | LTTNG_EVENT_EXCLUSION_NAME_AT( | |
133 | event->exclusion, i); | |
134 | ||
135 | /* | |
136 | * Compare this exclusion name to all the key's | |
137 | * exclusion names. | |
138 | */ | |
139 | for (j = 0; j < key->exclusion->count; ++j) { | |
140 | const char *name_key = | |
141 | LTTNG_EVENT_EXCLUSION_NAME_AT( | |
142 | key->exclusion, j); | |
143 | ||
144 | if (!strncmp(name_ev, name_key, | |
145 | LTTNG_SYMBOL_NAME_LEN)) { | |
146 | /* Names match! */ | |
147 | found = true; | |
148 | break; | |
149 | } | |
150 | } | |
151 | ||
152 | /* | |
153 | * If the current exclusion name was not found amongst | |
154 | * the key's exclusion names, then there's no match. | |
155 | */ | |
156 | if (!found) { | |
157 | goto no_match; | |
158 | } | |
159 | } | |
f19e9f8b | 160 | } |
025faf73 DG |
161 | /* Match. */ |
162 | return 1; | |
18eace3b DG |
163 | |
164 | no_match: | |
165 | return 0; | |
18eace3b DG |
166 | } |
167 | ||
0177d773 | 168 | /* |
2223c96f DG |
169 | * Find the channel in the hashtable and return channel pointer. RCU read side |
170 | * lock MUST be acquired before calling this. | |
0177d773 | 171 | */ |
bec39940 | 172 | struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht, |
f6a9efaa | 173 | char *name) |
0177d773 | 174 | { |
bec39940 DG |
175 | struct lttng_ht_node_str *node; |
176 | struct lttng_ht_iter iter; | |
0177d773 | 177 | |
85076754 MD |
178 | /* |
179 | * If we receive an empty string for channel name, it means the | |
180 | * default channel name is requested. | |
181 | */ | |
182 | if (name[0] == '\0') | |
183 | name = DEFAULT_CHANNEL_NAME; | |
184 | ||
bec39940 DG |
185 | lttng_ht_lookup(ht, (void *)name, &iter); |
186 | node = lttng_ht_iter_get_node_str(&iter); | |
f6a9efaa | 187 | if (node == NULL) { |
44d3bd01 DG |
188 | goto error; |
189 | } | |
190 | ||
f6a9efaa | 191 | DBG2("Trace UST channel %s found by name", name); |
0177d773 | 192 | |
f6a9efaa | 193 | return caa_container_of(node, struct ltt_ust_channel, node); |
97ee3a89 DG |
194 | |
195 | error: | |
f6a9efaa | 196 | DBG2("Trace UST channel %s not found by name", name); |
97ee3a89 DG |
197 | return NULL; |
198 | } | |
199 | ||
200 | /* | |
2223c96f DG |
201 | * Find the event in the hashtable and return event pointer. RCU read side lock |
202 | * MUST be acquired before calling this. | |
97ee3a89 | 203 | */ |
18eace3b | 204 | struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht, |
2106efa0 | 205 | char *name, struct lttng_filter_bytecode *filter, |
b953b8cd PP |
206 | enum lttng_ust_loglevel_type loglevel_type, int loglevel_value, |
207 | struct lttng_event_exclusion *exclusion) | |
97ee3a89 | 208 | { |
bec39940 DG |
209 | struct lttng_ht_node_str *node; |
210 | struct lttng_ht_iter iter; | |
18eace3b | 211 | struct ltt_ust_ht_key key; |
97ee3a89 | 212 | |
18eace3b DG |
213 | assert(name); |
214 | assert(ht); | |
215 | ||
216 | key.name = name; | |
217 | key.filter = filter; | |
b953b8cd PP |
218 | key.loglevel_type = loglevel_type; |
219 | key.loglevel_value = loglevel_value; | |
10646003 | 220 | key.exclusion = exclusion; |
18eace3b | 221 | |
18eace3b DG |
222 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), |
223 | trace_ust_ht_match_event, &key, &iter.iter); | |
bec39940 | 224 | node = lttng_ht_iter_get_node_str(&iter); |
f6a9efaa | 225 | if (node == NULL) { |
97ee3a89 DG |
226 | goto error; |
227 | } | |
228 | ||
18eace3b | 229 | DBG2("Trace UST event %s found", key.name); |
f6a9efaa DG |
230 | |
231 | return caa_container_of(node, struct ltt_ust_event, node); | |
97ee3a89 DG |
232 | |
233 | error: | |
18eace3b | 234 | DBG2("Trace UST event %s NOT found", key.name); |
97ee3a89 DG |
235 | return NULL; |
236 | } | |
237 | ||
fefd409b DG |
238 | /* |
239 | * Lookup an agent in the session agents hash table by domain type and return | |
240 | * the object if found else NULL. | |
4da703ad JG |
241 | * |
242 | * RCU read side lock must be acquired before calling and only released | |
243 | * once the agent is no longer in scope or being used. | |
fefd409b DG |
244 | */ |
245 | struct agent *trace_ust_find_agent(struct ltt_ust_session *session, | |
246 | enum lttng_domain_type domain_type) | |
247 | { | |
248 | struct agent *agt = NULL; | |
249 | struct lttng_ht_node_u64 *node; | |
250 | struct lttng_ht_iter iter; | |
251 | uint64_t key; | |
252 | ||
253 | assert(session); | |
254 | ||
255 | DBG3("Trace ust agent lookup for domain %d", domain_type); | |
256 | ||
257 | key = domain_type; | |
258 | ||
259 | lttng_ht_lookup(session->agents, &key, &iter); | |
260 | node = lttng_ht_iter_get_node_u64(&iter); | |
261 | if (!node) { | |
262 | goto end; | |
263 | } | |
264 | agt = caa_container_of(node, struct agent, node); | |
265 | ||
266 | end: | |
267 | return agt; | |
268 | } | |
269 | ||
97ee3a89 DG |
270 | /* |
271 | * Allocate and initialize a ust session data structure. | |
272 | * | |
273 | * Return pointer to structure or NULL. | |
274 | */ | |
d9bf3ca4 | 275 | struct ltt_ust_session *trace_ust_create_session(uint64_t session_id) |
97ee3a89 DG |
276 | { |
277 | struct ltt_ust_session *lus; | |
278 | ||
279 | /* Allocate a new ltt ust session */ | |
ba7f0ae5 | 280 | lus = zmalloc(sizeof(struct ltt_ust_session)); |
97ee3a89 | 281 | if (lus == NULL) { |
ba7f0ae5 | 282 | PERROR("create ust session zmalloc"); |
97ee3a89 DG |
283 | goto error; |
284 | } | |
285 | ||
286 | /* Init data structure */ | |
a991f516 | 287 | lus->id = session_id; |
14fb1ebe | 288 | lus->active = 0; |
97ee3a89 | 289 | |
84ad93e8 DG |
290 | /* Set default metadata channel attribute. */ |
291 | lus->metadata_attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
292 | lus->metadata_attr.subbuf_size = default_get_metadata_subbuf_size(); | |
293 | lus->metadata_attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; | |
294 | lus->metadata_attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER; | |
295 | lus->metadata_attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER; | |
296 | lus->metadata_attr.output = LTTNG_UST_MMAP; | |
297 | ||
7972aab2 DG |
298 | /* |
299 | * Default buffer type. This can be changed through an enable channel | |
300 | * requesting a different type. Note that this can only be changed once | |
301 | * during the session lifetime which is at the first enable channel and | |
302 | * only before start. The flag buffer_type_changed indicates the status. | |
303 | */ | |
8692d4e5 | 304 | lus->buffer_type = LTTNG_BUFFER_PER_UID; |
7972aab2 DG |
305 | /* Once set to 1, the buffer_type is immutable for the session. */ |
306 | lus->buffer_type_changed = 0; | |
307 | /* Init it in case it get used after allocation. */ | |
308 | CDS_INIT_LIST_HEAD(&lus->buffer_reg_uid_list); | |
f6a9efaa DG |
309 | |
310 | /* Alloc UST global domain channels' HT */ | |
bec39940 | 311 | lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
fefd409b DG |
312 | /* Alloc agent hash table. */ |
313 | lus->agents = lttng_ht_new(0, LTTNG_HT_TYPE_U64); | |
44d3bd01 | 314 | |
00e2e675 DG |
315 | lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
316 | if (lus->consumer == NULL) { | |
09a90bcd | 317 | goto error_consumer; |
00e2e675 DG |
318 | } |
319 | ||
44d3bd01 DG |
320 | DBG2("UST trace session create successful"); |
321 | ||
97ee3a89 DG |
322 | return lus; |
323 | ||
09a90bcd | 324 | error_consumer: |
0b2dc8df | 325 | ht_cleanup_push(lus->domain_global.channels); |
fefd409b | 326 | ht_cleanup_push(lus->agents); |
44844c29 | 327 | free(lus); |
97ee3a89 DG |
328 | error: |
329 | return NULL; | |
330 | } | |
331 | ||
332 | /* | |
333 | * Allocate and initialize a ust channel data structure. | |
334 | * | |
335 | * Return pointer to structure or NULL. | |
336 | */ | |
51755dc8 JG |
337 | struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan, |
338 | enum lttng_domain_type domain) | |
97ee3a89 | 339 | { |
97ee3a89 DG |
340 | struct ltt_ust_channel *luc; |
341 | ||
0525e9ae | 342 | assert(chan); |
0525e9ae | 343 | |
37357452 | 344 | luc = zmalloc(sizeof(struct ltt_ust_channel)); |
97ee3a89 | 345 | if (luc == NULL) { |
df0f840b | 346 | PERROR("ltt_ust_channel zmalloc"); |
97ee3a89 DG |
347 | goto error; |
348 | } | |
349 | ||
51755dc8 JG |
350 | luc->domain = domain; |
351 | ||
44d3bd01 | 352 | /* Copy UST channel attributes */ |
48842b30 DG |
353 | luc->attr.overwrite = chan->attr.overwrite; |
354 | luc->attr.subbuf_size = chan->attr.subbuf_size; | |
355 | luc->attr.num_subbuf = chan->attr.num_subbuf; | |
356 | luc->attr.switch_timer_interval = chan->attr.switch_timer_interval; | |
357 | luc->attr.read_timer_interval = chan->attr.read_timer_interval; | |
6775595e | 358 | luc->attr.output = (enum lttng_ust_output) chan->attr.output; |
82b4ebce JG |
359 | luc->monitor_timer_interval = ((struct lttng_channel_extended *) |
360 | chan->attr.extended.ptr)->monitor_timer_interval; | |
491d1539 MD |
361 | luc->attr.u.s.blocking_timeout = ((struct lttng_channel_extended *) |
362 | chan->attr.extended.ptr)->blocking_timeout; | |
44d3bd01 DG |
363 | |
364 | /* Translate to UST output enum */ | |
365 | switch (luc->attr.output) { | |
366 | default: | |
367 | luc->attr.output = LTTNG_UST_MMAP; | |
368 | break; | |
97ee3a89 | 369 | } |
97ee3a89 | 370 | |
85076754 MD |
371 | /* |
372 | * If we receive an empty string for channel name, it means the | |
373 | * default channel name is requested. | |
374 | */ | |
375 | if (chan->name[0] == '\0') { | |
376 | strncpy(luc->name, DEFAULT_CHANNEL_NAME, sizeof(luc->name)); | |
377 | } else { | |
378 | /* Copy channel name */ | |
379 | strncpy(luc->name, chan->name, sizeof(luc->name)); | |
380 | } | |
97ee3a89 DG |
381 | luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
382 | ||
f6a9efaa | 383 | /* Init node */ |
bec39940 | 384 | lttng_ht_node_init_str(&luc->node, luc->name); |
31746f93 DG |
385 | CDS_INIT_LIST_HEAD(&luc->ctx_list); |
386 | ||
f6a9efaa | 387 | /* Alloc hash tables */ |
bec39940 DG |
388 | luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
389 | luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
f6a9efaa | 390 | |
1624d5b7 JD |
391 | /* On-disk circular buffer parameters */ |
392 | luc->tracefile_size = chan->attr.tracefile_size; | |
393 | luc->tracefile_count = chan->attr.tracefile_count; | |
394 | ||
f6a9efaa DG |
395 | DBG2("Trace UST channel %s created", luc->name); |
396 | ||
97ee3a89 | 397 | error: |
7972aab2 | 398 | return luc; |
97ee3a89 DG |
399 | } |
400 | ||
ad11a1d3 PP |
401 | /* |
402 | * Validates an exclusion list. | |
403 | * | |
404 | * Returns 0 if valid, negative value if invalid. | |
405 | */ | |
406 | static int validate_exclusion(struct lttng_event_exclusion *exclusion) | |
407 | { | |
408 | size_t i; | |
409 | int ret = 0; | |
410 | ||
411 | assert(exclusion); | |
412 | ||
413 | for (i = 0; i < exclusion->count; ++i) { | |
414 | size_t j; | |
415 | const char *name_a = | |
416 | LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i); | |
417 | ||
418 | for (j = 0; j < i; ++j) { | |
419 | const char *name_b = | |
420 | LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, j); | |
421 | ||
422 | if (!strncmp(name_a, name_b, LTTNG_SYMBOL_NAME_LEN)) { | |
423 | /* Match! */ | |
424 | ret = -1; | |
425 | goto end; | |
426 | } | |
427 | } | |
428 | } | |
429 | ||
430 | end: | |
431 | return ret; | |
432 | } | |
433 | ||
97ee3a89 DG |
434 | /* |
435 | * Allocate and initialize a ust event. Set name and event type. | |
49d21f93 | 436 | * We own filter_expression, filter, and exclusion. |
97ee3a89 | 437 | * |
39687410 | 438 | * Return an lttng_error_code |
97ee3a89 | 439 | */ |
39687410 | 440 | enum lttng_error_code trace_ust_create_event(struct lttng_event *ev, |
6b453b5e | 441 | char *filter_expression, |
561c6897 | 442 | struct lttng_filter_bytecode *filter, |
88f06f15 | 443 | struct lttng_event_exclusion *exclusion, |
39687410 FD |
444 | bool internal_event, |
445 | struct ltt_ust_event **ust_event) | |
97ee3a89 | 446 | { |
39687410 FD |
447 | struct ltt_ust_event *local_ust_event; |
448 | enum lttng_error_code ret = LTTNG_OK; | |
97ee3a89 | 449 | |
0525e9ae DG |
450 | assert(ev); |
451 | ||
ad11a1d3 | 452 | if (exclusion && validate_exclusion(exclusion)) { |
39687410 | 453 | ret = LTTNG_ERR_INVALID; |
ad11a1d3 PP |
454 | goto error; |
455 | } | |
456 | ||
39687410 FD |
457 | local_ust_event = zmalloc(sizeof(struct ltt_ust_event)); |
458 | if (local_ust_event == NULL) { | |
ba7f0ae5 | 459 | PERROR("ust event zmalloc"); |
39687410 | 460 | ret = LTTNG_ERR_NOMEM; |
97ee3a89 DG |
461 | goto error; |
462 | } | |
463 | ||
39687410 | 464 | local_ust_event->internal = internal_event; |
88f06f15 | 465 | |
97ee3a89 DG |
466 | switch (ev->type) { |
467 | case LTTNG_EVENT_PROBE: | |
39687410 | 468 | local_ust_event->attr.instrumentation = LTTNG_UST_PROBE; |
97ee3a89 DG |
469 | break; |
470 | case LTTNG_EVENT_FUNCTION: | |
39687410 | 471 | local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION; |
97ee3a89 DG |
472 | break; |
473 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
39687410 | 474 | local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION; |
97ee3a89 DG |
475 | break; |
476 | case LTTNG_EVENT_TRACEPOINT: | |
39687410 | 477 | local_ust_event->attr.instrumentation = LTTNG_UST_TRACEPOINT; |
97ee3a89 DG |
478 | break; |
479 | default: | |
480 | ERR("Unknown ust instrumentation type (%d)", ev->type); | |
39687410 | 481 | ret = LTTNG_ERR_INVALID; |
44844c29 | 482 | goto error_free_event; |
97ee3a89 DG |
483 | } |
484 | ||
485 | /* Copy event name */ | |
39687410 FD |
486 | strncpy(local_ust_event->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN); |
487 | local_ust_event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
97ee3a89 | 488 | |
0cda4f28 | 489 | switch (ev->loglevel_type) { |
8005f29a | 490 | case LTTNG_EVENT_LOGLEVEL_ALL: |
39687410 FD |
491 | local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL; |
492 | local_ust_event->attr.loglevel = -1; /* Force to -1 */ | |
0cda4f28 | 493 | break; |
8005f29a | 494 | case LTTNG_EVENT_LOGLEVEL_RANGE: |
39687410 FD |
495 | local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE; |
496 | local_ust_event->attr.loglevel = ev->loglevel; | |
8005f29a MD |
497 | break; |
498 | case LTTNG_EVENT_LOGLEVEL_SINGLE: | |
39687410 FD |
499 | local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE; |
500 | local_ust_event->attr.loglevel = ev->loglevel; | |
0cda4f28 MD |
501 | break; |
502 | default: | |
4431494b | 503 | ERR("Unknown ust loglevel type (%d)", ev->loglevel_type); |
39687410 | 504 | ret = LTTNG_ERR_INVALID; |
0cda4f28 MD |
505 | goto error_free_event; |
506 | } | |
507 | ||
025faf73 | 508 | /* Same layout. */ |
39687410 FD |
509 | local_ust_event->filter_expression = filter_expression; |
510 | local_ust_event->filter = filter; | |
511 | local_ust_event->exclusion = exclusion; | |
0cda4f28 | 512 | |
f6a9efaa | 513 | /* Init node */ |
39687410 | 514 | lttng_ht_node_init_str(&local_ust_event->node, local_ust_event->attr.name); |
97ee3a89 | 515 | |
300b8fd5 | 516 | DBG2("Trace UST event %s, loglevel (%d,%d) created", |
39687410 FD |
517 | local_ust_event->attr.name, local_ust_event->attr.loglevel_type, |
518 | local_ust_event->attr.loglevel); | |
519 | ||
520 | *ust_event = local_ust_event; | |
284d8f55 | 521 | |
39687410 | 522 | return ret; |
97ee3a89 | 523 | |
44844c29 | 524 | error_free_event: |
39687410 | 525 | free(local_ust_event); |
97ee3a89 | 526 | error: |
49d21f93 MD |
527 | free(filter_expression); |
528 | free(filter); | |
529 | free(exclusion); | |
39687410 | 530 | return ret; |
97ee3a89 DG |
531 | } |
532 | ||
aa3514e9 | 533 | static |
bdf64013 JG |
534 | int trace_ust_context_type_event_to_ust( |
535 | enum lttng_event_context_type type) | |
55cc08a6 | 536 | { |
aa3514e9 | 537 | int utype; |
0525e9ae | 538 | |
aa3514e9 | 539 | switch (type) { |
9197c5c4 MD |
540 | case LTTNG_EVENT_CONTEXT_VTID: |
541 | utype = LTTNG_UST_CONTEXT_VTID; | |
542 | break; | |
543 | case LTTNG_EVENT_CONTEXT_VPID: | |
544 | utype = LTTNG_UST_CONTEXT_VPID; | |
545 | break; | |
546 | case LTTNG_EVENT_CONTEXT_PTHREAD_ID: | |
547 | utype = LTTNG_UST_CONTEXT_PTHREAD_ID; | |
548 | break; | |
549 | case LTTNG_EVENT_CONTEXT_PROCNAME: | |
550 | utype = LTTNG_UST_CONTEXT_PROCNAME; | |
551 | break; | |
7c612c2e WP |
552 | case LTTNG_EVENT_CONTEXT_IP: |
553 | utype = LTTNG_UST_CONTEXT_IP; | |
554 | break; | |
aa3514e9 | 555 | case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER: |
354e561b MD |
556 | if (!ustctl_has_perf_counters()) { |
557 | utype = -1; | |
558 | WARN("Perf counters not implemented in UST"); | |
559 | } else { | |
560 | utype = LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER; | |
561 | } | |
aa3514e9 | 562 | break; |
bdf64013 JG |
563 | case LTTNG_EVENT_CONTEXT_APP_CONTEXT: |
564 | utype = LTTNG_UST_CONTEXT_APP_CONTEXT; | |
565 | break; | |
46ec7146 MJ |
566 | case LTTNG_EVENT_CONTEXT_CGROUP_NS: |
567 | utype = LTTNG_UST_CONTEXT_CGROUP_NS; | |
568 | break; | |
569 | case LTTNG_EVENT_CONTEXT_IPC_NS: | |
570 | utype = LTTNG_UST_CONTEXT_IPC_NS; | |
571 | break; | |
572 | case LTTNG_EVENT_CONTEXT_MNT_NS: | |
573 | utype = LTTNG_UST_CONTEXT_MNT_NS; | |
574 | break; | |
575 | case LTTNG_EVENT_CONTEXT_NET_NS: | |
576 | utype = LTTNG_UST_CONTEXT_NET_NS; | |
577 | break; | |
578 | case LTTNG_EVENT_CONTEXT_PID_NS: | |
579 | utype = LTTNG_UST_CONTEXT_PID_NS; | |
580 | break; | |
581 | case LTTNG_EVENT_CONTEXT_USER_NS: | |
582 | utype = LTTNG_UST_CONTEXT_USER_NS; | |
583 | break; | |
584 | case LTTNG_EVENT_CONTEXT_UTS_NS: | |
585 | utype = LTTNG_UST_CONTEXT_UTS_NS; | |
586 | break; | |
9197c5c4 | 587 | default: |
aa3514e9 MD |
588 | utype = -1; |
589 | break; | |
590 | } | |
591 | return utype; | |
592 | } | |
593 | ||
594 | /* | |
595 | * Return 1 if contexts match, 0 otherwise. | |
596 | */ | |
597 | int trace_ust_match_context(struct ltt_ust_context *uctx, | |
598 | struct lttng_event_context *ctx) | |
599 | { | |
600 | int utype; | |
601 | ||
602 | utype = trace_ust_context_type_event_to_ust(ctx->ctx); | |
603 | if (utype < 0) { | |
604 | return 0; | |
605 | } | |
606 | if (uctx->ctx.ctx != utype) { | |
607 | return 0; | |
608 | } | |
609 | switch (utype) { | |
610 | case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: | |
611 | if (uctx->ctx.u.perf_counter.type | |
612 | != ctx->u.perf_counter.type) { | |
613 | return 0; | |
614 | } | |
615 | if (uctx->ctx.u.perf_counter.config | |
616 | != ctx->u.perf_counter.config) { | |
617 | return 0; | |
618 | } | |
619 | if (strncmp(uctx->ctx.u.perf_counter.name, | |
620 | ctx->u.perf_counter.name, | |
621 | LTTNG_UST_SYM_NAME_LEN)) { | |
622 | return 0; | |
623 | } | |
624 | break; | |
54fb33cf JG |
625 | case LTTNG_UST_CONTEXT_APP_CONTEXT: |
626 | assert(uctx->ctx.u.app_ctx.provider_name); | |
627 | assert(uctx->ctx.u.app_ctx.ctx_name); | |
628 | if (strcmp(uctx->ctx.u.app_ctx.provider_name, | |
629 | ctx->u.app_ctx.provider_name) || | |
630 | strcmp(uctx->ctx.u.app_ctx.ctx_name, | |
631 | ctx->u.app_ctx.ctx_name)) { | |
632 | return 0; | |
633 | } | |
aa3514e9 MD |
634 | default: |
635 | break; | |
636 | ||
637 | } | |
638 | return 1; | |
639 | } | |
640 | ||
641 | /* | |
642 | * Allocate and initialize an UST context. | |
643 | * | |
644 | * Return pointer to structure or NULL. | |
645 | */ | |
646 | struct ltt_ust_context *trace_ust_create_context( | |
647 | struct lttng_event_context *ctx) | |
648 | { | |
bdf64013 | 649 | struct ltt_ust_context *uctx = NULL; |
aa3514e9 MD |
650 | int utype; |
651 | ||
652 | assert(ctx); | |
653 | ||
654 | utype = trace_ust_context_type_event_to_ust(ctx->ctx); | |
655 | if (utype < 0) { | |
1cf992b8 | 656 | ERR("Invalid UST context"); |
bdf64013 | 657 | goto end; |
9197c5c4 | 658 | } |
55cc08a6 DG |
659 | |
660 | uctx = zmalloc(sizeof(struct ltt_ust_context)); | |
bdf64013 | 661 | if (!uctx) { |
55cc08a6 | 662 | PERROR("zmalloc ltt_ust_context"); |
bdf64013 | 663 | goto end; |
55cc08a6 DG |
664 | } |
665 | ||
aa3514e9 MD |
666 | uctx->ctx.ctx = (enum lttng_ust_context_type) utype; |
667 | switch (utype) { | |
668 | case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: | |
669 | uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type; | |
670 | uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config; | |
671 | strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name, | |
672 | LTTNG_UST_SYM_NAME_LEN); | |
673 | uctx->ctx.u.perf_counter.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
674 | break; | |
bdf64013 JG |
675 | case LTTNG_UST_CONTEXT_APP_CONTEXT: |
676 | { | |
677 | char *provider_name = NULL, *ctx_name = NULL; | |
678 | ||
679 | provider_name = strdup(ctx->u.app_ctx.provider_name); | |
680 | if (!provider_name) { | |
681 | goto error; | |
682 | } | |
683 | uctx->ctx.u.app_ctx.provider_name = provider_name; | |
684 | ||
685 | ctx_name = strdup(ctx->u.app_ctx.ctx_name); | |
686 | if (!ctx_name) { | |
687 | goto error; | |
688 | } | |
689 | uctx->ctx.u.app_ctx.ctx_name = ctx_name; | |
690 | break; | |
691 | } | |
aa3514e9 MD |
692 | default: |
693 | break; | |
694 | } | |
bec39940 | 695 | lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx); |
bdf64013 | 696 | end: |
55cc08a6 | 697 | return uctx; |
55cc08a6 | 698 | error: |
bdf64013 | 699 | trace_ust_destroy_context(uctx); |
55cc08a6 DG |
700 | return NULL; |
701 | } | |
702 | ||
a9ad0c8f MD |
703 | static |
704 | void destroy_pid_tracker_node_rcu(struct rcu_head *head) | |
705 | { | |
706 | struct ust_pid_tracker_node *tracker_node = | |
707 | caa_container_of(head, struct ust_pid_tracker_node, node.head); | |
708 | free(tracker_node); | |
709 | } | |
710 | ||
711 | static | |
712 | void destroy_pid_tracker_node(struct ust_pid_tracker_node *tracker_node) | |
713 | { | |
714 | ||
715 | call_rcu(&tracker_node->node.head, destroy_pid_tracker_node_rcu); | |
716 | } | |
717 | ||
718 | static | |
719 | int init_pid_tracker(struct ust_pid_tracker *pid_tracker) | |
720 | { | |
721 | int ret = 0; | |
722 | ||
723 | pid_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
724 | if (!pid_tracker->ht) { | |
725 | ret = -1; | |
726 | goto end; | |
727 | } | |
728 | ||
729 | end: | |
730 | return ret; | |
731 | } | |
732 | ||
733 | /* | |
734 | * Teardown pid tracker content, but don't free pid_tracker object. | |
735 | */ | |
736 | static | |
737 | void fini_pid_tracker(struct ust_pid_tracker *pid_tracker) | |
738 | { | |
739 | struct ust_pid_tracker_node *tracker_node; | |
740 | struct lttng_ht_iter iter; | |
741 | ||
742 | if (!pid_tracker->ht) { | |
743 | return; | |
744 | } | |
745 | rcu_read_lock(); | |
746 | cds_lfht_for_each_entry(pid_tracker->ht->ht, | |
747 | &iter.iter, tracker_node, node.node) { | |
748 | int ret = lttng_ht_del(pid_tracker->ht, &iter); | |
749 | ||
750 | assert(!ret); | |
751 | destroy_pid_tracker_node(tracker_node); | |
752 | } | |
753 | rcu_read_unlock(); | |
754 | ht_cleanup_push(pid_tracker->ht); | |
755 | pid_tracker->ht = NULL; | |
756 | } | |
757 | ||
758 | static | |
759 | struct ust_pid_tracker_node *pid_tracker_lookup( | |
760 | struct ust_pid_tracker *pid_tracker, int pid, | |
761 | struct lttng_ht_iter *iter) | |
762 | { | |
763 | unsigned long _pid = (unsigned long) pid; | |
764 | struct lttng_ht_node_ulong *node; | |
765 | ||
766 | lttng_ht_lookup(pid_tracker->ht, (void *) _pid, iter); | |
767 | node = lttng_ht_iter_get_node_ulong(iter); | |
768 | if (node) { | |
769 | return caa_container_of(node, struct ust_pid_tracker_node, | |
770 | node); | |
771 | } else { | |
772 | return NULL; | |
773 | } | |
774 | } | |
775 | ||
776 | static | |
777 | int pid_tracker_add_pid(struct ust_pid_tracker *pid_tracker, int pid) | |
778 | { | |
779 | int retval = LTTNG_OK; | |
780 | struct ust_pid_tracker_node *tracker_node; | |
781 | struct lttng_ht_iter iter; | |
782 | ||
783 | if (pid < 0) { | |
784 | retval = LTTNG_ERR_INVALID; | |
785 | goto end; | |
786 | } | |
787 | tracker_node = pid_tracker_lookup(pid_tracker, pid, &iter); | |
788 | if (tracker_node) { | |
789 | /* Already exists. */ | |
b93a4a13 | 790 | retval = LTTNG_ERR_PID_TRACKED; |
a9ad0c8f MD |
791 | goto end; |
792 | } | |
793 | tracker_node = zmalloc(sizeof(*tracker_node)); | |
794 | if (!tracker_node) { | |
795 | retval = LTTNG_ERR_NOMEM; | |
796 | goto end; | |
797 | } | |
798 | lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) pid); | |
799 | lttng_ht_add_unique_ulong(pid_tracker->ht, &tracker_node->node); | |
800 | end: | |
801 | return retval; | |
802 | } | |
803 | ||
804 | static | |
805 | int pid_tracker_del_pid(struct ust_pid_tracker *pid_tracker, int pid) | |
806 | { | |
807 | int retval = LTTNG_OK, ret; | |
808 | struct ust_pid_tracker_node *tracker_node; | |
809 | struct lttng_ht_iter iter; | |
810 | ||
811 | if (pid < 0) { | |
812 | retval = LTTNG_ERR_INVALID; | |
813 | goto end; | |
814 | } | |
815 | tracker_node = pid_tracker_lookup(pid_tracker, pid, &iter); | |
816 | if (!tracker_node) { | |
817 | /* Not found */ | |
b93a4a13 | 818 | retval = LTTNG_ERR_PID_NOT_TRACKED; |
a9ad0c8f MD |
819 | goto end; |
820 | } | |
821 | ret = lttng_ht_del(pid_tracker->ht, &iter); | |
822 | assert(!ret); | |
823 | ||
824 | destroy_pid_tracker_node(tracker_node); | |
825 | end: | |
826 | return retval; | |
827 | } | |
828 | ||
829 | /* | |
830 | * The session lock is held when calling this function. | |
831 | */ | |
832 | int trace_ust_pid_tracker_lookup(struct ltt_ust_session *session, int pid) | |
833 | { | |
834 | struct lttng_ht_iter iter; | |
835 | ||
836 | if (!session->pid_tracker.ht) { | |
837 | return 1; | |
838 | } | |
839 | if (pid_tracker_lookup(&session->pid_tracker, pid, &iter)) { | |
840 | return 1; | |
841 | } | |
842 | return 0; | |
843 | } | |
844 | ||
845 | /* | |
846 | * Called with the session lock held. | |
847 | */ | |
848 | int trace_ust_track_pid(struct ltt_ust_session *session, int pid) | |
849 | { | |
850 | int retval = LTTNG_OK; | |
88e3c2f5 | 851 | bool should_update_apps = false; |
a9ad0c8f MD |
852 | |
853 | if (pid == -1) { | |
854 | /* Track all pids: destroy tracker if exists. */ | |
855 | if (session->pid_tracker.ht) { | |
856 | fini_pid_tracker(&session->pid_tracker); | |
857 | /* Ensure all apps have session. */ | |
88e3c2f5 | 858 | should_update_apps = true; |
a9ad0c8f MD |
859 | } |
860 | } else { | |
861 | int ret; | |
862 | ||
863 | if (!session->pid_tracker.ht) { | |
864 | /* Create tracker. */ | |
865 | if (init_pid_tracker(&session->pid_tracker)) { | |
866 | ERR("Error initializing PID tracker"); | |
867 | retval = LTTNG_ERR_NOMEM; | |
868 | goto end; | |
869 | } | |
870 | ret = pid_tracker_add_pid(&session->pid_tracker, pid); | |
871 | if (ret != LTTNG_OK) { | |
872 | retval = ret; | |
873 | fini_pid_tracker(&session->pid_tracker); | |
874 | goto end; | |
875 | } | |
876 | /* Remove all apps from session except pid. */ | |
88e3c2f5 | 877 | should_update_apps = true; |
a9ad0c8f MD |
878 | } else { |
879 | struct ust_app *app; | |
880 | ||
881 | ret = pid_tracker_add_pid(&session->pid_tracker, pid); | |
882 | if (ret != LTTNG_OK) { | |
883 | retval = ret; | |
884 | goto end; | |
885 | } | |
886 | /* Add session to application */ | |
887 | app = ust_app_find_by_pid(pid); | |
888 | if (app) { | |
88e3c2f5 | 889 | should_update_apps = true; |
a9ad0c8f MD |
890 | } |
891 | } | |
892 | } | |
88e3c2f5 JG |
893 | if (should_update_apps && session->active) { |
894 | ust_app_global_update_all(session); | |
895 | } | |
a9ad0c8f MD |
896 | end: |
897 | return retval; | |
898 | } | |
899 | ||
900 | /* | |
901 | * Called with the session lock held. | |
902 | */ | |
903 | int trace_ust_untrack_pid(struct ltt_ust_session *session, int pid) | |
904 | { | |
905 | int retval = LTTNG_OK; | |
906 | ||
907 | if (pid == -1) { | |
908 | /* Create empty tracker, replace old tracker. */ | |
909 | struct ust_pid_tracker tmp_tracker; | |
910 | ||
911 | tmp_tracker = session->pid_tracker; | |
912 | if (init_pid_tracker(&session->pid_tracker)) { | |
913 | ERR("Error initializing PID tracker"); | |
914 | retval = LTTNG_ERR_NOMEM; | |
915 | /* Rollback operation. */ | |
916 | session->pid_tracker = tmp_tracker; | |
917 | goto end; | |
918 | } | |
919 | fini_pid_tracker(&tmp_tracker); | |
920 | ||
921 | /* Remove session from all applications */ | |
922 | ust_app_global_update_all(session); | |
923 | } else { | |
924 | int ret; | |
925 | struct ust_app *app; | |
926 | ||
927 | if (!session->pid_tracker.ht) { | |
b894343d JG |
928 | /* No PID being tracked. */ |
929 | retval = LTTNG_ERR_PID_NOT_TRACKED; | |
a9ad0c8f MD |
930 | goto end; |
931 | } | |
932 | /* Remove PID from tracker */ | |
933 | ret = pid_tracker_del_pid(&session->pid_tracker, pid); | |
934 | if (ret != LTTNG_OK) { | |
935 | retval = ret; | |
936 | goto end; | |
937 | } | |
938 | /* Remove session from application. */ | |
939 | app = ust_app_find_by_pid(pid); | |
940 | if (app) { | |
941 | ust_app_global_update(session, app); | |
942 | } | |
943 | } | |
944 | end: | |
945 | return retval; | |
946 | } | |
947 | ||
a5dfbb9d MD |
948 | /* |
949 | * Called with session lock held. | |
950 | */ | |
951 | ssize_t trace_ust_list_tracker_pids(struct ltt_ust_session *session, | |
952 | int32_t **_pids) | |
953 | { | |
954 | struct ust_pid_tracker_node *tracker_node; | |
955 | struct lttng_ht_iter iter; | |
956 | unsigned long count, i = 0; | |
957 | long approx[2]; | |
958 | int32_t *pids; | |
959 | int ret = 0; | |
960 | ||
961 | if (!session->pid_tracker.ht) { | |
962 | /* Tracker disabled. Set first entry to -1. */ | |
963 | pids = zmalloc(sizeof(*pids)); | |
964 | if (!pids) { | |
965 | ret = -1; | |
966 | goto end; | |
967 | } | |
968 | pids[0] = -1; | |
969 | *_pids = pids; | |
970 | return 1; | |
971 | } | |
972 | ||
973 | rcu_read_lock(); | |
974 | cds_lfht_count_nodes(session->pid_tracker.ht->ht, | |
975 | &approx[0], &count, &approx[1]); | |
976 | pids = zmalloc(sizeof(*pids) * count); | |
977 | if (!pids) { | |
978 | ret = -1; | |
979 | goto end; | |
980 | } | |
981 | cds_lfht_for_each_entry(session->pid_tracker.ht->ht, | |
982 | &iter.iter, tracker_node, node.node) { | |
983 | pids[i++] = tracker_node->node.key; | |
984 | } | |
985 | *_pids = pids; | |
986 | ret = count; | |
987 | end: | |
988 | rcu_read_unlock(); | |
989 | return ret; | |
990 | } | |
991 | ||
f6a9efaa DG |
992 | /* |
993 | * RCU safe free context structure. | |
994 | */ | |
995 | static void destroy_context_rcu(struct rcu_head *head) | |
996 | { | |
bec39940 DG |
997 | struct lttng_ht_node_ulong *node = |
998 | caa_container_of(head, struct lttng_ht_node_ulong, head); | |
f6a9efaa DG |
999 | struct ltt_ust_context *ctx = |
1000 | caa_container_of(node, struct ltt_ust_context, node); | |
1001 | ||
bdf64013 | 1002 | trace_ust_destroy_context(ctx); |
f6a9efaa DG |
1003 | } |
1004 | ||
1005 | /* | |
1006 | * Cleanup UST context hash table. | |
1007 | */ | |
7bd39047 | 1008 | static void destroy_contexts(struct lttng_ht *ht) |
f6a9efaa DG |
1009 | { |
1010 | int ret; | |
bec39940 DG |
1011 | struct lttng_ht_node_ulong *node; |
1012 | struct lttng_ht_iter iter; | |
31746f93 | 1013 | struct ltt_ust_context *ctx; |
f6a9efaa | 1014 | |
0525e9ae DG |
1015 | assert(ht); |
1016 | ||
36b588ed | 1017 | rcu_read_lock(); |
bec39940 | 1018 | cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) { |
31746f93 DG |
1019 | /* Remove from ordered list. */ |
1020 | ctx = caa_container_of(node, struct ltt_ust_context, node); | |
1021 | cds_list_del(&ctx->list); | |
1022 | /* Remove from channel's hash table. */ | |
bec39940 | 1023 | ret = lttng_ht_del(ht, &iter); |
f6a9efaa DG |
1024 | if (!ret) { |
1025 | call_rcu(&node->head, destroy_context_rcu); | |
1026 | } | |
f6a9efaa | 1027 | } |
36b588ed | 1028 | rcu_read_unlock(); |
ed52805d | 1029 | |
0b2dc8df | 1030 | ht_cleanup_push(ht); |
f6a9efaa DG |
1031 | } |
1032 | ||
97ee3a89 DG |
1033 | /* |
1034 | * Cleanup ust event structure. | |
1035 | */ | |
1036 | void trace_ust_destroy_event(struct ltt_ust_event *event) | |
1037 | { | |
0525e9ae DG |
1038 | assert(event); |
1039 | ||
f6a9efaa | 1040 | DBG2("Trace destroy UST event %s", event->attr.name); |
6b453b5e | 1041 | free(event->filter_expression); |
53a80697 | 1042 | free(event->filter); |
40024f8a | 1043 | free(event->exclusion); |
97ee3a89 DG |
1044 | free(event); |
1045 | } | |
1046 | ||
bdf64013 JG |
1047 | /* |
1048 | * Cleanup ust context structure. | |
1049 | */ | |
1050 | void trace_ust_destroy_context(struct ltt_ust_context *ctx) | |
1051 | { | |
1052 | assert(ctx); | |
1053 | ||
1054 | if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) { | |
1055 | free(ctx->ctx.u.app_ctx.provider_name); | |
1056 | free(ctx->ctx.u.app_ctx.ctx_name); | |
1057 | } | |
1058 | free(ctx); | |
1059 | } | |
1060 | ||
f6a9efaa DG |
1061 | /* |
1062 | * URCU intermediate call to complete destroy event. | |
1063 | */ | |
1064 | static void destroy_event_rcu(struct rcu_head *head) | |
1065 | { | |
bec39940 DG |
1066 | struct lttng_ht_node_str *node = |
1067 | caa_container_of(head, struct lttng_ht_node_str, head); | |
f6a9efaa DG |
1068 | struct ltt_ust_event *event = |
1069 | caa_container_of(node, struct ltt_ust_event, node); | |
1070 | ||
1071 | trace_ust_destroy_event(event); | |
1072 | } | |
1073 | ||
284d8f55 DG |
1074 | /* |
1075 | * Cleanup UST events hashtable. | |
1076 | */ | |
7bd39047 | 1077 | static void destroy_events(struct lttng_ht *events) |
ed52805d DG |
1078 | { |
1079 | int ret; | |
bec39940 DG |
1080 | struct lttng_ht_node_str *node; |
1081 | struct lttng_ht_iter iter; | |
ed52805d | 1082 | |
0525e9ae DG |
1083 | assert(events); |
1084 | ||
36b588ed | 1085 | rcu_read_lock(); |
bec39940 DG |
1086 | cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) { |
1087 | ret = lttng_ht_del(events, &iter); | |
7bd39047 DG |
1088 | assert(!ret); |
1089 | call_rcu(&node->head, destroy_event_rcu); | |
ed52805d | 1090 | } |
36b588ed | 1091 | rcu_read_unlock(); |
ed52805d | 1092 | |
0b2dc8df | 1093 | ht_cleanup_push(events); |
ed52805d DG |
1094 | } |
1095 | ||
97ee3a89 DG |
1096 | /* |
1097 | * Cleanup ust channel structure. | |
36b588ed MD |
1098 | * |
1099 | * Should _NOT_ be called with RCU read lock held. | |
97ee3a89 | 1100 | */ |
36b588ed | 1101 | static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel) |
97ee3a89 | 1102 | { |
0525e9ae DG |
1103 | assert(channel); |
1104 | ||
f6a9efaa | 1105 | DBG2("Trace destroy UST channel %s", channel->name); |
97ee3a89 | 1106 | |
97ee3a89 | 1107 | free(channel); |
f6a9efaa DG |
1108 | } |
1109 | ||
1110 | /* | |
1111 | * URCU intermediate call to complete destroy channel. | |
1112 | */ | |
1113 | static void destroy_channel_rcu(struct rcu_head *head) | |
1114 | { | |
bec39940 DG |
1115 | struct lttng_ht_node_str *node = |
1116 | caa_container_of(head, struct lttng_ht_node_str, head); | |
f6a9efaa DG |
1117 | struct ltt_ust_channel *channel = |
1118 | caa_container_of(node, struct ltt_ust_channel, node); | |
1119 | ||
36b588ed MD |
1120 | _trace_ust_destroy_channel(channel); |
1121 | } | |
1122 | ||
1123 | void trace_ust_destroy_channel(struct ltt_ust_channel *channel) | |
1124 | { | |
627cbbe7 MD |
1125 | /* Destroying all events of the channel */ |
1126 | destroy_events(channel->events); | |
1127 | /* Destroying all context of the channel */ | |
1128 | destroy_contexts(channel->ctx); | |
1129 | ||
36b588ed | 1130 | call_rcu(&channel->node.head, destroy_channel_rcu); |
97ee3a89 DG |
1131 | } |
1132 | ||
d5979e4a DG |
1133 | /* |
1134 | * Remove an UST channel from a channel HT. | |
1135 | */ | |
1136 | void trace_ust_delete_channel(struct lttng_ht *ht, | |
1137 | struct ltt_ust_channel *channel) | |
1138 | { | |
1139 | int ret; | |
1140 | struct lttng_ht_iter iter; | |
1141 | ||
1142 | assert(ht); | |
1143 | assert(channel); | |
1144 | ||
1145 | iter.iter.node = &channel->node.node; | |
1146 | ret = lttng_ht_del(ht, &iter); | |
1147 | assert(!ret); | |
1148 | } | |
1149 | ||
97ee3a89 | 1150 | /* |
f6a9efaa | 1151 | * Iterate over a hash table containing channels and cleanup safely. |
97ee3a89 | 1152 | */ |
bec39940 | 1153 | static void destroy_channels(struct lttng_ht *channels) |
f6a9efaa | 1154 | { |
bec39940 DG |
1155 | struct lttng_ht_node_str *node; |
1156 | struct lttng_ht_iter iter; | |
f6a9efaa | 1157 | |
0525e9ae DG |
1158 | assert(channels); |
1159 | ||
24d1723f | 1160 | rcu_read_lock(); |
bec39940 | 1161 | cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) { |
627cbbe7 MD |
1162 | struct ltt_ust_channel *chan = |
1163 | caa_container_of(node, struct ltt_ust_channel, node); | |
1164 | ||
1165 | trace_ust_delete_channel(channels, chan); | |
1166 | trace_ust_destroy_channel(chan); | |
f6a9efaa | 1167 | } |
36b588ed | 1168 | rcu_read_unlock(); |
ed52805d | 1169 | |
0b2dc8df | 1170 | ht_cleanup_push(channels); |
f6a9efaa DG |
1171 | } |
1172 | ||
f6a9efaa DG |
1173 | /* |
1174 | * Cleanup UST global domain. | |
1175 | */ | |
1176 | static void destroy_domain_global(struct ltt_ust_domain_global *dom) | |
1177 | { | |
0525e9ae DG |
1178 | assert(dom); |
1179 | ||
f6a9efaa DG |
1180 | destroy_channels(dom->channels); |
1181 | } | |
97ee3a89 | 1182 | |
f6a9efaa DG |
1183 | /* |
1184 | * Cleanup ust session structure | |
36b588ed MD |
1185 | * |
1186 | * Should *NOT* be called with RCU read-side lock held. | |
f6a9efaa DG |
1187 | */ |
1188 | void trace_ust_destroy_session(struct ltt_ust_session *session) | |
1189 | { | |
fefd409b | 1190 | struct agent *agt; |
7972aab2 | 1191 | struct buffer_reg_uid *reg, *sreg; |
fefd409b | 1192 | struct lttng_ht_iter iter; |
7972aab2 | 1193 | |
0525e9ae | 1194 | assert(session); |
97ee3a89 | 1195 | |
d9bf3ca4 | 1196 | DBG2("Trace UST destroy session %" PRIu64, session->id); |
f6a9efaa | 1197 | |
f6a9efaa DG |
1198 | /* Cleaning up UST domain */ |
1199 | destroy_domain_global(&session->domain_global); | |
fefd409b | 1200 | |
0c0fcd77 | 1201 | rcu_read_lock(); |
fefd409b | 1202 | cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) { |
d0edf546 JG |
1203 | int ret = lttng_ht_del(session->agents, &iter); |
1204 | ||
1205 | assert(!ret); | |
fefd409b DG |
1206 | agent_destroy(agt); |
1207 | } | |
0c0fcd77 | 1208 | rcu_read_unlock(); |
7972aab2 | 1209 | |
8ada2175 MD |
1210 | ht_cleanup_push(session->agents); |
1211 | ||
7972aab2 DG |
1212 | /* Cleanup UID buffer registry object(s). */ |
1213 | cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list, | |
1214 | lnode) { | |
1215 | cds_list_del(®->lnode); | |
1216 | buffer_reg_uid_remove(reg); | |
1217 | buffer_reg_uid_destroy(reg, session->consumer); | |
1218 | } | |
44d3bd01 | 1219 | |
6addfa37 | 1220 | consumer_output_put(session->consumer); |
3f8e211f | 1221 | |
a9ad0c8f MD |
1222 | fini_pid_tracker(&session->pid_tracker); |
1223 | ||
97ee3a89 DG |
1224 | free(session); |
1225 | } |