Fix: multiple consumer locking problems
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
... / ...
CommitLineData
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 "trace-ust.h"
28
29/*
30 * Find the channel in the hashtable.
31 */
32struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
33 char *name)
34{
35 struct lttng_ht_node_str *node;
36 struct lttng_ht_iter iter;
37
38 rcu_read_lock();
39 lttng_ht_lookup(ht, (void *)name, &iter);
40 node = lttng_ht_iter_get_node_str(&iter);
41 if (node == NULL) {
42 rcu_read_unlock();
43 goto error;
44 }
45 rcu_read_unlock();
46
47 DBG2("Trace UST channel %s found by name", name);
48
49 return caa_container_of(node, struct ltt_ust_channel, node);
50
51error:
52 DBG2("Trace UST channel %s not found by name", name);
53 return NULL;
54}
55
56/*
57 * Find the event in the hashtable.
58 */
59struct ltt_ust_event *trace_ust_find_event_by_name(struct lttng_ht *ht,
60 char *name)
61{
62 struct lttng_ht_node_str *node;
63 struct lttng_ht_iter iter;
64
65 rcu_read_lock();
66 lttng_ht_lookup(ht, (void *) name, &iter);
67 node = lttng_ht_iter_get_node_str(&iter);
68 if (node == NULL) {
69 rcu_read_unlock();
70 goto error;
71 }
72 rcu_read_unlock();
73
74 DBG2("Trace UST event found by name %s", name);
75
76 return caa_container_of(node, struct ltt_ust_event, node);
77
78error:
79 DBG2("Trace UST event NOT found by name %s", name);
80 return NULL;
81}
82
83/*
84 * Allocate and initialize a ust session data structure.
85 *
86 * Return pointer to structure or NULL.
87 */
88struct ltt_ust_session *trace_ust_create_session(char *path,
89 unsigned int session_id, struct lttng_domain *domain)
90{
91 int ret;
92 struct ltt_ust_session *lus;
93
94 /* Allocate a new ltt ust session */
95 lus = zmalloc(sizeof(struct ltt_ust_session));
96 if (lus == NULL) {
97 PERROR("create ust session zmalloc");
98 goto error;
99 }
100
101 /* Init data structure */
102 lus->id = session_id;
103 lus->start_trace = 0;
104
105 /* Alloc UST domain hash tables */
106 lus->domain_pid = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
107 lus->domain_exec = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
108
109 /* Alloc UST global domain channels' HT */
110 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
111
112 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
113 if (lus->consumer == NULL) {
114 goto error_free_session;
115 }
116
117 /*
118 * The tmp_consumer stays NULL until a set_consumer_uri command is
119 * executed. At this point, the consumer should be nullify until an
120 * enable_consumer command. This assignment is symbolic since we've zmalloc
121 * the struct.
122 */
123 lus->tmp_consumer = NULL;
124
125 /* Use the default consumer output which is the tracing session path. */
126 if (path && strlen(path) > 0) {
127 ret = snprintf(lus->consumer->dst.trace_path, PATH_MAX,
128 "%s" DEFAULT_UST_TRACE_DIR, path);
129 if (ret < 0) {
130 PERROR("snprintf UST consumer trace path");
131 goto error;
132 }
133
134 /* Set session path */
135 ret = snprintf(lus->pathname, PATH_MAX, "%s" DEFAULT_UST_TRACE_DIR,
136 path);
137 if (ret < 0) {
138 PERROR("snprintf kernel traces path");
139 goto error_free_session;
140 }
141 }
142
143 DBG2("UST trace session create successful");
144
145 return lus;
146
147error_free_session:
148 lttng_ht_destroy(lus->domain_global.channels);
149 lttng_ht_destroy(lus->domain_exec);
150 lttng_ht_destroy(lus->domain_pid);
151 free(lus);
152error:
153 return NULL;
154}
155
156/*
157 * Allocate and initialize a ust channel data structure.
158 *
159 * Return pointer to structure or NULL.
160 */
161struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
162 char *path)
163{
164 int ret;
165 struct ltt_ust_channel *luc;
166
167 luc = zmalloc(sizeof(struct ltt_ust_channel));
168 if (luc == NULL) {
169 PERROR("ltt_ust_channel zmalloc");
170 goto error;
171 }
172
173 /* Copy UST channel attributes */
174 luc->attr.overwrite = chan->attr.overwrite;
175 luc->attr.subbuf_size = chan->attr.subbuf_size;
176 luc->attr.num_subbuf = chan->attr.num_subbuf;
177 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
178 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
179 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
180
181 /* Translate to UST output enum */
182 switch (luc->attr.output) {
183 default:
184 luc->attr.output = LTTNG_UST_MMAP;
185 break;
186 }
187
188 /* Copy channel name */
189 strncpy(luc->name, chan->name, sizeof(luc->name));
190 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
191
192 /* Init node */
193 lttng_ht_node_init_str(&luc->node, luc->name);
194 /* Alloc hash tables */
195 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
196 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
197
198 /* Set trace output path */
199 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
200 if (ret < 0) {
201 PERROR("asprintf ust create channel");
202 goto error_free_channel;
203 }
204
205 DBG2("Trace UST channel %s created", luc->name);
206
207 return luc;
208
209error_free_channel:
210 lttng_ht_destroy(luc->ctx);
211 lttng_ht_destroy(luc->events);
212 free(luc);
213error:
214 return NULL;
215}
216
217/*
218 * Allocate and initialize a ust event. Set name and event type.
219 *
220 * Return pointer to structure or NULL.
221 */
222struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
223{
224 struct ltt_ust_event *lue;
225
226 lue = zmalloc(sizeof(struct ltt_ust_event));
227 if (lue == NULL) {
228 PERROR("ust event zmalloc");
229 goto error;
230 }
231
232 switch (ev->type) {
233 case LTTNG_EVENT_PROBE:
234 lue->attr.instrumentation = LTTNG_UST_PROBE;
235 break;
236 case LTTNG_EVENT_FUNCTION:
237 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
238 break;
239 case LTTNG_EVENT_FUNCTION_ENTRY:
240 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
241 break;
242 case LTTNG_EVENT_TRACEPOINT:
243 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
244 break;
245 default:
246 ERR("Unknown ust instrumentation type (%d)", ev->type);
247 goto error_free_event;
248 }
249
250 /* Copy event name */
251 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
252 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
253
254 switch (ev->loglevel_type) {
255 case LTTNG_EVENT_LOGLEVEL_ALL:
256 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
257 lue->attr.loglevel = -1; /* Force to -1 */
258 break;
259 case LTTNG_EVENT_LOGLEVEL_RANGE:
260 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
261 lue->attr.loglevel = ev->loglevel;
262 break;
263 case LTTNG_EVENT_LOGLEVEL_SINGLE:
264 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
265 lue->attr.loglevel = ev->loglevel;
266 break;
267 default:
268 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
269 goto error_free_event;
270 }
271
272
273 /* Init node */
274 lttng_ht_node_init_str(&lue->node, lue->attr.name);
275 /* Alloc context hash tables */
276 lue->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
277 if (lue->ctx == NULL) {
278 ERR("Unable to create context hash table for event %s", ev->name);
279 goto error_free_event;
280 }
281
282 DBG2("Trace UST event %s, loglevel (%d,%d) created",
283 lue->attr.name, lue->attr.loglevel_type,
284 lue->attr.loglevel);
285
286 return lue;
287
288error_free_event:
289 free(lue);
290error:
291 return NULL;
292}
293
294/*
295 * Allocate and initialize a ust metadata.
296 *
297 * Return pointer to structure or NULL.
298 */
299struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
300{
301 int ret;
302 struct ltt_ust_metadata *lum;
303
304 lum = zmalloc(sizeof(struct ltt_ust_metadata));
305 if (lum == NULL) {
306 PERROR("ust metadata zmalloc");
307 goto error;
308 }
309
310 /* Set default attributes */
311 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
312 lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
313 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
314 lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
315 lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
316 lum->attr.output = LTTNG_UST_MMAP;
317
318 lum->handle = -1;
319 /* Set metadata trace path */
320 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
321 if (ret < 0) {
322 PERROR("asprintf ust metadata");
323 goto error_free_metadata;
324 }
325
326 return lum;
327
328error_free_metadata:
329 free(lum);
330error:
331 return NULL;
332}
333
334/*
335 * Allocate and initialize an UST context.
336 *
337 * Return pointer to structure or NULL.
338 */
339struct ltt_ust_context *trace_ust_create_context(
340 struct lttng_event_context *ctx)
341{
342 struct ltt_ust_context *uctx;
343 enum lttng_ust_context_type utype;
344
345 switch (ctx->ctx) {
346 case LTTNG_EVENT_CONTEXT_VTID:
347 utype = LTTNG_UST_CONTEXT_VTID;
348 break;
349 case LTTNG_EVENT_CONTEXT_VPID:
350 utype = LTTNG_UST_CONTEXT_VPID;
351 break;
352 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
353 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
354 break;
355 case LTTNG_EVENT_CONTEXT_PROCNAME:
356 utype = LTTNG_UST_CONTEXT_PROCNAME;
357 break;
358 default:
359 ERR("Invalid UST context");
360 return NULL;
361 }
362
363 uctx = zmalloc(sizeof(struct ltt_ust_context));
364 if (uctx == NULL) {
365 PERROR("zmalloc ltt_ust_context");
366 goto error;
367 }
368
369 uctx->ctx.ctx = utype;
370 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
371
372 return uctx;
373
374error:
375 return NULL;
376}
377
378/*
379 * RCU safe free context structure.
380 */
381static void destroy_context_rcu(struct rcu_head *head)
382{
383 struct lttng_ht_node_ulong *node =
384 caa_container_of(head, struct lttng_ht_node_ulong, head);
385 struct ltt_ust_context *ctx =
386 caa_container_of(node, struct ltt_ust_context, node);
387
388 free(ctx);
389}
390
391/*
392 * Cleanup UST context hash table.
393 */
394static void destroy_contexts(struct lttng_ht *ht)
395{
396 int ret;
397 struct lttng_ht_node_ulong *node;
398 struct lttng_ht_iter iter;
399
400 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
401 ret = lttng_ht_del(ht, &iter);
402 if (!ret) {
403 call_rcu(&node->head, destroy_context_rcu);
404 }
405 }
406
407 lttng_ht_destroy(ht);
408}
409
410/*
411 * Cleanup ust event structure.
412 */
413void trace_ust_destroy_event(struct ltt_ust_event *event)
414{
415 DBG2("Trace destroy UST event %s", event->attr.name);
416 destroy_contexts(event->ctx);
417 free(event->filter);
418 free(event);
419}
420
421/*
422 * URCU intermediate call to complete destroy event.
423 */
424static void destroy_event_rcu(struct rcu_head *head)
425{
426 struct lttng_ht_node_str *node =
427 caa_container_of(head, struct lttng_ht_node_str, head);
428 struct ltt_ust_event *event =
429 caa_container_of(node, struct ltt_ust_event, node);
430
431 trace_ust_destroy_event(event);
432}
433
434/*
435 * Cleanup UST events hashtable.
436 */
437static void destroy_events(struct lttng_ht *events)
438{
439 int ret;
440 struct lttng_ht_node_str *node;
441 struct lttng_ht_iter iter;
442
443 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
444 ret = lttng_ht_del(events, &iter);
445 assert(!ret);
446 call_rcu(&node->head, destroy_event_rcu);
447 }
448
449 lttng_ht_destroy(events);
450}
451
452/*
453 * Cleanup ust channel structure.
454 */
455void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
456{
457 DBG2("Trace destroy UST channel %s", channel->name);
458
459 rcu_read_lock();
460
461 /* Destroying all events of the channel */
462 destroy_events(channel->events);
463 /* Destroying all context of the channel */
464 destroy_contexts(channel->ctx);
465
466 free(channel);
467
468 rcu_read_unlock();
469}
470
471/*
472 * URCU intermediate call to complete destroy channel.
473 */
474static void destroy_channel_rcu(struct rcu_head *head)
475{
476 struct lttng_ht_node_str *node =
477 caa_container_of(head, struct lttng_ht_node_str, head);
478 struct ltt_ust_channel *channel =
479 caa_container_of(node, struct ltt_ust_channel, node);
480
481 trace_ust_destroy_channel(channel);
482}
483
484/*
485 * Cleanup ust metadata structure.
486 */
487void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
488{
489 if (!metadata->handle) {
490 return;
491 }
492 DBG2("Trace UST destroy metadata %d", metadata->handle);
493 free(metadata);
494}
495
496/*
497 * Iterate over a hash table containing channels and cleanup safely.
498 */
499static void destroy_channels(struct lttng_ht *channels)
500{
501 int ret;
502 struct lttng_ht_node_str *node;
503 struct lttng_ht_iter iter;
504
505 rcu_read_lock();
506
507 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
508 ret = lttng_ht_del(channels, &iter);
509 assert(!ret);
510 call_rcu(&node->head, destroy_channel_rcu);
511 }
512
513 lttng_ht_destroy(channels);
514
515 rcu_read_unlock();
516}
517
518/*
519 * Cleanup UST pid domain.
520 */
521static void destroy_domain_pid(struct lttng_ht *ht)
522{
523 int ret;
524 struct lttng_ht_iter iter;
525 struct ltt_ust_domain_pid *dpid;
526
527 cds_lfht_for_each_entry(ht->ht, &iter.iter, dpid, node.node) {
528 ret = lttng_ht_del(ht , &iter);
529 assert(!ret);
530 destroy_channels(dpid->channels);
531 }
532
533 lttng_ht_destroy(ht);
534}
535
536/*
537 * Cleanup UST exec name domain.
538 */
539static void destroy_domain_exec(struct lttng_ht *ht)
540{
541 int ret;
542 struct lttng_ht_iter iter;
543 struct ltt_ust_domain_exec *dexec;
544
545 cds_lfht_for_each_entry(ht->ht, &iter.iter, dexec, node.node) {
546 ret = lttng_ht_del(ht , &iter);
547 assert(!ret);
548 destroy_channels(dexec->channels);
549 }
550
551 lttng_ht_destroy(ht);
552}
553
554/*
555 * Cleanup UST global domain.
556 */
557static void destroy_domain_global(struct ltt_ust_domain_global *dom)
558{
559 destroy_channels(dom->channels);
560}
561
562/*
563 * Cleanup ust session structure
564 */
565void trace_ust_destroy_session(struct ltt_ust_session *session)
566{
567 /* Extra protection */
568 if (session == NULL) {
569 return;
570 }
571
572 rcu_read_lock();
573
574 DBG2("Trace UST destroy session %u", session->id);
575
576 /* Cleaning up UST domain */
577 destroy_domain_global(&session->domain_global);
578 destroy_domain_pid(session->domain_pid);
579 destroy_domain_exec(session->domain_exec);
580
581 consumer_destroy_output(session->consumer);
582 consumer_destroy_output(session->tmp_consumer);
583
584 free(session);
585
586 rcu_read_unlock();
587}
This page took 0.026624 seconds and 4 git commands to generate.