Fix: hash table cleanup call_rcu deadlock
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <common/common.h>
25 #include <common/defaults.h>
26
27 #include "buffer-registry.h"
28 #include "trace-ust.h"
29 #include "utils.h"
30
31 /*
32 * Match function for the events hash table lookup.
33 *
34 * Matches by name only. Used by the disable command.
35 */
36 int trace_ust_ht_match_event_by_name(struct cds_lfht_node *node,
37 const void *_key)
38 {
39 struct ltt_ust_event *event;
40 const char *name;
41
42 assert(node);
43 assert(_key);
44
45 event = caa_container_of(node, struct ltt_ust_event, node.node);
46 name = _key;
47
48 /* Event name */
49 if (strncmp(event->attr.name, name, sizeof(event->attr.name)) != 0) {
50 goto no_match;
51 }
52
53 /* Match */
54 return 1;
55
56 no_match:
57 return 0;
58 }
59
60 /*
61 * Match function for the hash table lookup.
62 *
63 * It matches an ust event based on three attributes which are the event name,
64 * the filter bytecode and the loglevel.
65 */
66 int trace_ust_ht_match_event(struct cds_lfht_node *node, const void *_key)
67 {
68 struct ltt_ust_event *event;
69 const struct ltt_ust_ht_key *key;
70
71 assert(node);
72 assert(_key);
73
74 event = caa_container_of(node, struct ltt_ust_event, node.node);
75 key = _key;
76
77 /* Match the 3 elements of the key: name, filter and loglevel. */
78
79 /* Event name */
80 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
81 goto no_match;
82 }
83
84 /* Event loglevel. */
85 if (event->attr.loglevel != key->loglevel) {
86 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
87 && key->loglevel == 0 && event->attr.loglevel == -1) {
88 /*
89 * Match is accepted. This is because on event creation, the
90 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
91 * -1 are accepted for this loglevel type since 0 is the one set by
92 * the API when receiving an enable event.
93 */
94 } else {
95 goto no_match;
96 }
97 }
98
99 /* Only one of the filters is NULL, fail. */
100 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
101 goto no_match;
102 }
103
104 if (key->filter && event->filter) {
105 /* Both filters exists, check length followed by the bytecode. */
106 if (event->filter->len != key->filter->len ||
107 memcmp(event->filter->data, key->filter->data,
108 event->filter->len) != 0) {
109 goto no_match;
110 }
111 }
112
113 /* Match. */
114 return 1;
115
116 no_match:
117 return 0;
118 }
119
120 /*
121 * Find the channel in the hashtable and return channel pointer. RCU read side
122 * lock MUST be acquired before calling this.
123 */
124 struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
125 char *name)
126 {
127 struct lttng_ht_node_str *node;
128 struct lttng_ht_iter iter;
129
130 lttng_ht_lookup(ht, (void *)name, &iter);
131 node = lttng_ht_iter_get_node_str(&iter);
132 if (node == NULL) {
133 goto error;
134 }
135
136 DBG2("Trace UST channel %s found by name", name);
137
138 return caa_container_of(node, struct ltt_ust_channel, node);
139
140 error:
141 DBG2("Trace UST channel %s not found by name", name);
142 return NULL;
143 }
144
145 /*
146 * Find the event in the hashtable and return event pointer. RCU read side lock
147 * MUST be acquired before calling this.
148 */
149 struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht,
150 char *name, struct lttng_filter_bytecode *filter, int loglevel)
151 {
152 struct lttng_ht_node_str *node;
153 struct lttng_ht_iter iter;
154 struct ltt_ust_ht_key key;
155
156 assert(name);
157 assert(ht);
158
159 key.name = name;
160 key.filter = filter;
161 key.loglevel = loglevel;
162
163 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
164 trace_ust_ht_match_event, &key, &iter.iter);
165 node = lttng_ht_iter_get_node_str(&iter);
166 if (node == NULL) {
167 goto error;
168 }
169
170 DBG2("Trace UST event %s found", key.name);
171
172 return caa_container_of(node, struct ltt_ust_event, node);
173
174 error:
175 DBG2("Trace UST event %s NOT found", key.name);
176 return NULL;
177 }
178
179 /*
180 * Allocate and initialize a ust session data structure.
181 *
182 * Return pointer to structure or NULL.
183 */
184 struct ltt_ust_session *trace_ust_create_session(unsigned int session_id)
185 {
186 struct ltt_ust_session *lus;
187
188 /* Allocate a new ltt ust session */
189 lus = zmalloc(sizeof(struct ltt_ust_session));
190 if (lus == NULL) {
191 PERROR("create ust session zmalloc");
192 goto error;
193 }
194
195 /* Init data structure */
196 lus->id = session_id;
197 lus->start_trace = 0;
198
199 /*
200 * Default buffer type. This can be changed through an enable channel
201 * requesting a different type. Note that this can only be changed once
202 * during the session lifetime which is at the first enable channel and
203 * only before start. The flag buffer_type_changed indicates the status.
204 */
205 lus->buffer_type = LTTNG_BUFFER_PER_PID;
206 /* Once set to 1, the buffer_type is immutable for the session. */
207 lus->buffer_type_changed = 0;
208 /* Init it in case it get used after allocation. */
209 CDS_INIT_LIST_HEAD(&lus->buffer_reg_uid_list);
210
211 /* Alloc UST global domain channels' HT */
212 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
213
214 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
215 if (lus->consumer == NULL) {
216 goto error_consumer;
217 }
218
219 /*
220 * The tmp_consumer stays NULL until a set_consumer_uri command is
221 * executed. At this point, the consumer should be nullify until an
222 * enable_consumer command. This assignment is symbolic since we've zmalloc
223 * the struct.
224 */
225 lus->tmp_consumer = NULL;
226
227 DBG2("UST trace session create successful");
228
229 return lus;
230
231 error_consumer:
232 ht_cleanup_push(lus->domain_global.channels);
233 free(lus);
234 error:
235 return NULL;
236 }
237
238 /*
239 * Allocate and initialize a ust channel data structure.
240 *
241 * Return pointer to structure or NULL.
242 */
243 struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan)
244 {
245 struct ltt_ust_channel *luc;
246
247 assert(chan);
248
249 luc = zmalloc(sizeof(struct ltt_ust_channel));
250 if (luc == NULL) {
251 PERROR("ltt_ust_channel zmalloc");
252 goto error;
253 }
254
255 /* Copy UST channel attributes */
256 luc->attr.overwrite = chan->attr.overwrite;
257 luc->attr.subbuf_size = chan->attr.subbuf_size;
258 luc->attr.num_subbuf = chan->attr.num_subbuf;
259 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
260 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
261 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
262
263 /* Translate to UST output enum */
264 switch (luc->attr.output) {
265 default:
266 luc->attr.output = LTTNG_UST_MMAP;
267 break;
268 }
269
270 /* Copy channel name */
271 strncpy(luc->name, chan->name, sizeof(luc->name));
272 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
273
274 /* Init node */
275 lttng_ht_node_init_str(&luc->node, luc->name);
276 /* Alloc hash tables */
277 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
278 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
279
280 /* On-disk circular buffer parameters */
281 luc->tracefile_size = chan->attr.tracefile_size;
282 luc->tracefile_count = chan->attr.tracefile_count;
283
284 DBG2("Trace UST channel %s created", luc->name);
285
286 error:
287 return luc;
288 }
289
290 /*
291 * Allocate and initialize a ust event. Set name and event type.
292 *
293 * Return pointer to structure or NULL.
294 */
295 struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev,
296 struct lttng_filter_bytecode *filter)
297 {
298 struct ltt_ust_event *lue;
299
300 assert(ev);
301
302 lue = zmalloc(sizeof(struct ltt_ust_event));
303 if (lue == NULL) {
304 PERROR("ust event zmalloc");
305 goto error;
306 }
307
308 switch (ev->type) {
309 case LTTNG_EVENT_PROBE:
310 lue->attr.instrumentation = LTTNG_UST_PROBE;
311 break;
312 case LTTNG_EVENT_FUNCTION:
313 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
314 break;
315 case LTTNG_EVENT_FUNCTION_ENTRY:
316 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
317 break;
318 case LTTNG_EVENT_TRACEPOINT:
319 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
320 break;
321 default:
322 ERR("Unknown ust instrumentation type (%d)", ev->type);
323 goto error_free_event;
324 }
325
326 /* Copy event name */
327 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
328 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
329
330 switch (ev->loglevel_type) {
331 case LTTNG_EVENT_LOGLEVEL_ALL:
332 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
333 lue->attr.loglevel = -1; /* Force to -1 */
334 break;
335 case LTTNG_EVENT_LOGLEVEL_RANGE:
336 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
337 lue->attr.loglevel = ev->loglevel;
338 break;
339 case LTTNG_EVENT_LOGLEVEL_SINGLE:
340 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
341 lue->attr.loglevel = ev->loglevel;
342 break;
343 default:
344 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
345 goto error_free_event;
346 }
347
348 /* Same layout. */
349 lue->filter = (struct lttng_ust_filter_bytecode *) filter;
350
351 /* Init node */
352 lttng_ht_node_init_str(&lue->node, lue->attr.name);
353
354 DBG2("Trace UST event %s, loglevel (%d,%d) created",
355 lue->attr.name, lue->attr.loglevel_type,
356 lue->attr.loglevel);
357
358 return lue;
359
360 error_free_event:
361 free(lue);
362 error:
363 return NULL;
364 }
365
366 /*
367 * Allocate and initialize a ust metadata.
368 *
369 * Return pointer to structure or NULL.
370 */
371 struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
372 {
373 int ret;
374 struct ltt_ust_metadata *lum;
375
376 assert(path);
377
378 lum = zmalloc(sizeof(struct ltt_ust_metadata));
379 if (lum == NULL) {
380 PERROR("ust metadata zmalloc");
381 goto error;
382 }
383
384 /* Set default attributes */
385 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
386 lum->attr.subbuf_size = default_get_metadata_subbuf_size();
387 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
388 lum->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
389 lum->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;
390 lum->attr.output = LTTNG_UST_MMAP;
391
392 lum->handle = -1;
393 /* Set metadata trace path */
394 ret = snprintf(lum->pathname, PATH_MAX, "%s/" DEFAULT_METADATA_NAME, path);
395 if (ret < 0) {
396 PERROR("asprintf ust metadata");
397 goto error_free_metadata;
398 }
399
400 return lum;
401
402 error_free_metadata:
403 free(lum);
404 error:
405 return NULL;
406 }
407
408 /*
409 * Allocate and initialize an UST context.
410 *
411 * Return pointer to structure or NULL.
412 */
413 struct ltt_ust_context *trace_ust_create_context(
414 struct lttng_event_context *ctx)
415 {
416 struct ltt_ust_context *uctx;
417 enum lttng_ust_context_type utype;
418
419 assert(ctx);
420
421 switch (ctx->ctx) {
422 case LTTNG_EVENT_CONTEXT_VTID:
423 utype = LTTNG_UST_CONTEXT_VTID;
424 break;
425 case LTTNG_EVENT_CONTEXT_VPID:
426 utype = LTTNG_UST_CONTEXT_VPID;
427 break;
428 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
429 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
430 break;
431 case LTTNG_EVENT_CONTEXT_PROCNAME:
432 utype = LTTNG_UST_CONTEXT_PROCNAME;
433 break;
434 default:
435 ERR("Invalid UST context");
436 return NULL;
437 }
438
439 uctx = zmalloc(sizeof(struct ltt_ust_context));
440 if (uctx == NULL) {
441 PERROR("zmalloc ltt_ust_context");
442 goto error;
443 }
444
445 uctx->ctx.ctx = utype;
446 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
447
448 return uctx;
449
450 error:
451 return NULL;
452 }
453
454 /*
455 * RCU safe free context structure.
456 */
457 static void destroy_context_rcu(struct rcu_head *head)
458 {
459 struct lttng_ht_node_ulong *node =
460 caa_container_of(head, struct lttng_ht_node_ulong, head);
461 struct ltt_ust_context *ctx =
462 caa_container_of(node, struct ltt_ust_context, node);
463
464 free(ctx);
465 }
466
467 /*
468 * Cleanup UST context hash table.
469 */
470 static void destroy_contexts(struct lttng_ht *ht)
471 {
472 int ret;
473 struct lttng_ht_node_ulong *node;
474 struct lttng_ht_iter iter;
475
476 assert(ht);
477
478 rcu_read_lock();
479 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
480 ret = lttng_ht_del(ht, &iter);
481 if (!ret) {
482 call_rcu(&node->head, destroy_context_rcu);
483 }
484 }
485 rcu_read_unlock();
486
487 ht_cleanup_push(ht);
488 }
489
490 /*
491 * Cleanup ust event structure.
492 */
493 void trace_ust_destroy_event(struct ltt_ust_event *event)
494 {
495 assert(event);
496
497 DBG2("Trace destroy UST event %s", event->attr.name);
498 free(event->filter);
499 free(event);
500 }
501
502 /*
503 * URCU intermediate call to complete destroy event.
504 */
505 static void destroy_event_rcu(struct rcu_head *head)
506 {
507 struct lttng_ht_node_str *node =
508 caa_container_of(head, struct lttng_ht_node_str, head);
509 struct ltt_ust_event *event =
510 caa_container_of(node, struct ltt_ust_event, node);
511
512 trace_ust_destroy_event(event);
513 }
514
515 /*
516 * Cleanup UST events hashtable.
517 */
518 static void destroy_events(struct lttng_ht *events)
519 {
520 int ret;
521 struct lttng_ht_node_str *node;
522 struct lttng_ht_iter iter;
523
524 assert(events);
525
526 rcu_read_lock();
527 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
528 ret = lttng_ht_del(events, &iter);
529 assert(!ret);
530 call_rcu(&node->head, destroy_event_rcu);
531 }
532 rcu_read_unlock();
533
534 ht_cleanup_push(events);
535 }
536
537 /*
538 * Cleanup ust channel structure.
539 *
540 * Should _NOT_ be called with RCU read lock held.
541 */
542 static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
543 {
544 assert(channel);
545
546 DBG2("Trace destroy UST channel %s", channel->name);
547
548 /* Destroying all events of the channel */
549 destroy_events(channel->events);
550 /* Destroying all context of the channel */
551 destroy_contexts(channel->ctx);
552
553 free(channel);
554 }
555
556 /*
557 * URCU intermediate call to complete destroy channel.
558 */
559 static void destroy_channel_rcu(struct rcu_head *head)
560 {
561 struct lttng_ht_node_str *node =
562 caa_container_of(head, struct lttng_ht_node_str, head);
563 struct ltt_ust_channel *channel =
564 caa_container_of(node, struct ltt_ust_channel, node);
565
566 _trace_ust_destroy_channel(channel);
567 }
568
569 void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
570 {
571 call_rcu(&channel->node.head, destroy_channel_rcu);
572 }
573
574 /*
575 * Cleanup ust metadata structure.
576 */
577 void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
578 {
579 assert(metadata);
580
581 if (!metadata->handle) {
582 return;
583 }
584 DBG2("Trace UST destroy metadata %d", metadata->handle);
585 free(metadata);
586 }
587
588 /*
589 * Iterate over a hash table containing channels and cleanup safely.
590 */
591 static void destroy_channels(struct lttng_ht *channels)
592 {
593 int ret;
594 struct lttng_ht_node_str *node;
595 struct lttng_ht_iter iter;
596
597 assert(channels);
598
599 rcu_read_lock();
600
601 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
602 ret = lttng_ht_del(channels, &iter);
603 assert(!ret);
604 call_rcu(&node->head, destroy_channel_rcu);
605 }
606 rcu_read_unlock();
607
608 ht_cleanup_push(channels);
609 }
610
611 /*
612 * Cleanup UST global domain.
613 */
614 static void destroy_domain_global(struct ltt_ust_domain_global *dom)
615 {
616 assert(dom);
617
618 destroy_channels(dom->channels);
619 }
620
621 /*
622 * Cleanup ust session structure
623 *
624 * Should *NOT* be called with RCU read-side lock held.
625 */
626 void trace_ust_destroy_session(struct ltt_ust_session *session)
627 {
628 struct buffer_reg_uid *reg, *sreg;
629
630 assert(session);
631
632 DBG2("Trace UST destroy session %u", session->id);
633
634 /* Cleaning up UST domain */
635 destroy_domain_global(&session->domain_global);
636
637 /* Cleanup UID buffer registry object(s). */
638 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
639 lnode) {
640 cds_list_del(&reg->lnode);
641 buffer_reg_uid_remove(reg);
642 buffer_reg_uid_destroy(reg, session->consumer);
643 }
644
645 consumer_destroy_output(session->consumer);
646 consumer_destroy_output(session->tmp_consumer);
647
648 free(session);
649 }
This page took 0.04643 seconds and 6 git commands to generate.