Extend API and remove lttng_uri from lttng.h
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
CommitLineData
97ee3a89
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
97ee3a89 7 *
d14d33bf
AM
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.
97ee3a89 12 *
d14d33bf
AM
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.
97ee3a89
DG
16 */
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
990570ed
DG
24#include <common/common.h>
25#include <common/defaults.h>
97ee3a89
DG
26
27#include "trace-ust.h"
28
0177d773 29/*
f6a9efaa 30 * Find the channel in the hashtable.
0177d773 31 */
bec39940 32struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
f6a9efaa 33 char *name)
0177d773 34{
bec39940
DG
35 struct lttng_ht_node_str *node;
36 struct lttng_ht_iter iter;
0177d773 37
f6a9efaa 38 rcu_read_lock();
bec39940
DG
39 lttng_ht_lookup(ht, (void *)name, &iter);
40 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa
DG
41 if (node == NULL) {
42 rcu_read_unlock();
44d3bd01
DG
43 goto error;
44 }
f6a9efaa 45 rcu_read_unlock();
44d3bd01 46
f6a9efaa 47 DBG2("Trace UST channel %s found by name", name);
0177d773 48
f6a9efaa 49 return caa_container_of(node, struct ltt_ust_channel, node);
97ee3a89
DG
50
51error:
f6a9efaa 52 DBG2("Trace UST channel %s not found by name", name);
97ee3a89
DG
53 return NULL;
54}
55
56/*
f6a9efaa 57 * Find the event in the hashtable.
97ee3a89 58 */
bec39940 59struct ltt_ust_event *trace_ust_find_event_by_name(struct lttng_ht *ht,
f6a9efaa 60 char *name)
97ee3a89 61{
bec39940
DG
62 struct lttng_ht_node_str *node;
63 struct lttng_ht_iter iter;
97ee3a89 64
f6a9efaa 65 rcu_read_lock();
bec39940
DG
66 lttng_ht_lookup(ht, (void *) name, &iter);
67 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa
DG
68 if (node == NULL) {
69 rcu_read_unlock();
97ee3a89
DG
70 goto error;
71 }
f6a9efaa 72 rcu_read_unlock();
97ee3a89 73
284d8f55 74 DBG2("Trace UST event found by name %s", name);
f6a9efaa
DG
75
76 return caa_container_of(node, struct ltt_ust_event, node);
97ee3a89
DG
77
78error:
284d8f55 79 DBG2("Trace UST event NOT found by name %s", name);
97ee3a89
DG
80 return NULL;
81}
82
83/*
84 * Allocate and initialize a ust session data structure.
85 *
86 * Return pointer to structure or NULL.
87 */
fc4705fe
DG
88struct ltt_ust_session *trace_ust_create_session(char *path,
89 unsigned int session_id, struct lttng_domain *domain)
97ee3a89 90{
0177d773 91 int ret;
97ee3a89
DG
92 struct ltt_ust_session *lus;
93
94 /* Allocate a new ltt ust session */
ba7f0ae5 95 lus = zmalloc(sizeof(struct ltt_ust_session));
97ee3a89 96 if (lus == NULL) {
ba7f0ae5 97 PERROR("create ust session zmalloc");
97ee3a89
DG
98 goto error;
99 }
100
101 /* Init data structure */
a991f516 102 lus->id = session_id;
36dc12cc 103 lus->start_trace = 0;
97ee3a89 104
f6a9efaa 105 /* Alloc UST domain hash tables */
bec39940
DG
106 lus->domain_pid = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
107 lus->domain_exec = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa
DG
108
109 /* Alloc UST global domain channels' HT */
bec39940 110 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
44d3bd01 111
00e2e675
DG
112 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
113 if (lus->consumer == NULL) {
a4b92340 114 goto error_free_session;
00e2e675
DG
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. */
a4b92340
DG
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 }
0177d773
DG
141 }
142
44d3bd01
DG
143 DBG2("UST trace session create successful");
144
97ee3a89
DG
145 return lus;
146
44844c29 147error_free_session:
a2c0da86
MD
148 lttng_ht_destroy(lus->domain_global.channels);
149 lttng_ht_destroy(lus->domain_exec);
150 lttng_ht_destroy(lus->domain_pid);
44844c29 151 free(lus);
97ee3a89
DG
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 */
44d3bd01
DG
161struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
162 char *path)
97ee3a89
DG
163{
164 int ret;
165 struct ltt_ust_channel *luc;
166
37357452 167 luc = zmalloc(sizeof(struct ltt_ust_channel));
97ee3a89 168 if (luc == NULL) {
df0f840b 169 PERROR("ltt_ust_channel zmalloc");
97ee3a89
DG
170 goto error;
171 }
172
44d3bd01 173 /* Copy UST channel attributes */
48842b30
DG
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;
6775595e 179 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
44d3bd01
DG
180
181 /* Translate to UST output enum */
182 switch (luc->attr.output) {
183 default:
184 luc->attr.output = LTTNG_UST_MMAP;
185 break;
97ee3a89 186 }
97ee3a89 187
97ee3a89 188 /* Copy channel name */
0a1459bb 189 strncpy(luc->name, chan->name, sizeof(luc->name));
97ee3a89
DG
190 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
191
f6a9efaa 192 /* Init node */
bec39940 193 lttng_ht_node_init_str(&luc->node, luc->name);
f6a9efaa 194 /* Alloc hash tables */
bec39940
DG
195 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
196 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa 197
97ee3a89 198 /* Set trace output path */
48842b30 199 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
97ee3a89 200 if (ret < 0) {
df0f840b 201 PERROR("asprintf ust create channel");
44844c29 202 goto error_free_channel;
97ee3a89
DG
203 }
204
f6a9efaa
DG
205 DBG2("Trace UST channel %s created", luc->name);
206
97ee3a89
DG
207 return luc;
208
44844c29 209error_free_channel:
a2c0da86
MD
210 lttng_ht_destroy(luc->ctx);
211 lttng_ht_destroy(luc->events);
44844c29 212 free(luc);
97ee3a89
DG
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;
97ee3a89 225
37357452 226 lue = zmalloc(sizeof(struct ltt_ust_event));
44d3bd01 227 if (lue == NULL) {
ba7f0ae5 228 PERROR("ust event zmalloc");
97ee3a89
DG
229 goto error;
230 }
231
232 switch (ev->type) {
233 case LTTNG_EVENT_PROBE:
44d3bd01 234 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
235 break;
236 case LTTNG_EVENT_FUNCTION:
44d3bd01 237 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
238 break;
239 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 240 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
241 break;
242 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 243 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
244 break;
245 default:
246 ERR("Unknown ust instrumentation type (%d)", ev->type);
44844c29 247 goto error_free_event;
97ee3a89
DG
248 }
249
250 /* Copy event name */
44d3bd01
DG
251 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
252 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 253
0cda4f28 254 switch (ev->loglevel_type) {
8005f29a
MD
255 case LTTNG_EVENT_LOGLEVEL_ALL:
256 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
22e25b71 257 lue->attr.loglevel = -1; /* Force to -1 */
0cda4f28 258 break;
8005f29a
MD
259 case LTTNG_EVENT_LOGLEVEL_RANGE:
260 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
22e25b71 261 lue->attr.loglevel = ev->loglevel;
8005f29a
MD
262 break;
263 case LTTNG_EVENT_LOGLEVEL_SINGLE:
264 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
22e25b71 265 lue->attr.loglevel = ev->loglevel;
0cda4f28
MD
266 break;
267 default:
4431494b 268 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
0cda4f28
MD
269 goto error_free_event;
270 }
271
0cda4f28 272
f6a9efaa 273 /* Init node */
bec39940 274 lttng_ht_node_init_str(&lue->node, lue->attr.name);
f6a9efaa 275 /* Alloc context hash tables */
bec39940 276 lue->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
af061a47
DG
277 if (lue->ctx == NULL) {
278 ERR("Unable to create context hash table for event %s", ev->name);
279 goto error_free_event;
280 }
97ee3a89 281
300b8fd5
MD
282 DBG2("Trace UST event %s, loglevel (%d,%d) created",
283 lue->attr.name, lue->attr.loglevel_type,
284 lue->attr.loglevel);
284d8f55 285
97ee3a89
DG
286 return lue;
287
44844c29
MD
288error_free_event:
289 free(lue);
97ee3a89
DG
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;
97ee3a89 303
3a009adb 304 lum = zmalloc(sizeof(struct ltt_ust_metadata));
44d3bd01 305 if (lum == NULL) {
df0f840b 306 PERROR("ust metadata zmalloc");
97ee3a89
DG
307 goto error;
308 }
309
310 /* Set default attributes */
44d3bd01
DG
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;
d41f73b7 316 lum->attr.output = LTTNG_UST_MMAP;
44d3bd01 317
97ee3a89
DG
318 lum->handle = -1;
319 /* Set metadata trace path */
f6a9efaa 320 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
97ee3a89 321 if (ret < 0) {
df0f840b 322 PERROR("asprintf ust metadata");
44844c29 323 goto error_free_metadata;
97ee3a89
DG
324 }
325
326 return lum;
327
44844c29
MD
328error_free_metadata:
329 free(lum);
97ee3a89
DG
330error:
331 return NULL;
332}
333
55cc08a6
DG
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;
9197c5c4
MD
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 }
55cc08a6
DG
362
363 uctx = zmalloc(sizeof(struct ltt_ust_context));
364 if (uctx == NULL) {
365 PERROR("zmalloc ltt_ust_context");
366 goto error;
367 }
368
9197c5c4 369 uctx->ctx.ctx = utype;
bec39940 370 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
55cc08a6
DG
371
372 return uctx;
373
374error:
375 return NULL;
376}
377
f6a9efaa
DG
378/*
379 * RCU safe free context structure.
380 */
381static void destroy_context_rcu(struct rcu_head *head)
382{
bec39940
DG
383 struct lttng_ht_node_ulong *node =
384 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa
DG
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 */
7bd39047 394static void destroy_contexts(struct lttng_ht *ht)
f6a9efaa
DG
395{
396 int ret;
bec39940
DG
397 struct lttng_ht_node_ulong *node;
398 struct lttng_ht_iter iter;
f6a9efaa 399
bec39940
DG
400 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
401 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
402 if (!ret) {
403 call_rcu(&node->head, destroy_context_rcu);
404 }
f6a9efaa 405 }
ed52805d 406
bec39940 407 lttng_ht_destroy(ht);
f6a9efaa
DG
408}
409
97ee3a89
DG
410/*
411 * Cleanup ust event structure.
412 */
413void trace_ust_destroy_event(struct ltt_ust_event *event)
414{
f6a9efaa 415 DBG2("Trace destroy UST event %s", event->attr.name);
7bd39047 416 destroy_contexts(event->ctx);
53a80697 417 free(event->filter);
97ee3a89
DG
418 free(event);
419}
420
f6a9efaa
DG
421/*
422 * URCU intermediate call to complete destroy event.
423 */
424static void destroy_event_rcu(struct rcu_head *head)
425{
bec39940
DG
426 struct lttng_ht_node_str *node =
427 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
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
284d8f55
DG
434/*
435 * Cleanup UST events hashtable.
436 */
7bd39047 437static void destroy_events(struct lttng_ht *events)
ed52805d
DG
438{
439 int ret;
bec39940
DG
440 struct lttng_ht_node_str *node;
441 struct lttng_ht_iter iter;
ed52805d 442
bec39940
DG
443 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
444 ret = lttng_ht_del(events, &iter);
7bd39047
DG
445 assert(!ret);
446 call_rcu(&node->head, destroy_event_rcu);
ed52805d
DG
447 }
448
bec39940 449 lttng_ht_destroy(events);
ed52805d
DG
450}
451
97ee3a89
DG
452/*
453 * Cleanup ust channel structure.
454 */
455void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
456{
f6a9efaa 457 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 458
f6a9efaa
DG
459 rcu_read_lock();
460
7bd39047
DG
461 /* Destroying all events of the channel */
462 destroy_events(channel->events);
463 /* Destroying all context of the channel */
464 destroy_contexts(channel->ctx);
97ee3a89 465
97ee3a89 466 free(channel);
f6a9efaa
DG
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{
bec39940
DG
476 struct lttng_ht_node_str *node =
477 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
478 struct ltt_ust_channel *channel =
479 caa_container_of(node, struct ltt_ust_channel, node);
480
481 trace_ust_destroy_channel(channel);
97ee3a89
DG
482}
483
484/*
485 * Cleanup ust metadata structure.
486 */
487void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
488{
abc9138a
MD
489 if (!metadata->handle) {
490 return;
491 }
f6a9efaa 492 DBG2("Trace UST destroy metadata %d", metadata->handle);
97ee3a89
DG
493 free(metadata);
494}
495
496/*
f6a9efaa 497 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 498 */
bec39940 499static void destroy_channels(struct lttng_ht *channels)
f6a9efaa
DG
500{
501 int ret;
bec39940
DG
502 struct lttng_ht_node_str *node;
503 struct lttng_ht_iter iter;
f6a9efaa 504
24d1723f
DG
505 rcu_read_lock();
506
bec39940
DG
507 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
508 ret = lttng_ht_del(channels, &iter);
7bd39047
DG
509 assert(!ret);
510 call_rcu(&node->head, destroy_channel_rcu);
f6a9efaa 511 }
ed52805d 512
bec39940 513 lttng_ht_destroy(channels);
24d1723f
DG
514
515 rcu_read_unlock();
f6a9efaa
DG
516}
517
518/*
519 * Cleanup UST pid domain.
520 */
bec39940 521static void destroy_domain_pid(struct lttng_ht *ht)
f6a9efaa
DG
522{
523 int ret;
bec39940 524 struct lttng_ht_iter iter;
ed52805d 525 struct ltt_ust_domain_pid *dpid;
f6a9efaa 526
bec39940
DG
527 cds_lfht_for_each_entry(ht->ht, &iter.iter, dpid, node.node) {
528 ret = lttng_ht_del(ht , &iter);
7bd39047
DG
529 assert(!ret);
530 destroy_channels(dpid->channels);
f6a9efaa 531 }
ed52805d 532
bec39940 533 lttng_ht_destroy(ht);
f6a9efaa
DG
534}
535
536/*
537 * Cleanup UST exec name domain.
538 */
bec39940 539static void destroy_domain_exec(struct lttng_ht *ht)
97ee3a89 540{
f6a9efaa 541 int ret;
bec39940 542 struct lttng_ht_iter iter;
ed52805d 543 struct ltt_ust_domain_exec *dexec;
f6a9efaa 544
bec39940
DG
545 cds_lfht_for_each_entry(ht->ht, &iter.iter, dexec, node.node) {
546 ret = lttng_ht_del(ht , &iter);
7bd39047
DG
547 assert(!ret);
548 destroy_channels(dexec->channels);
f6a9efaa 549 }
ed52805d 550
bec39940 551 lttng_ht_destroy(ht);
f6a9efaa 552}
97ee3a89 553
f6a9efaa
DG
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}
97ee3a89 561
f6a9efaa
DG
562/*
563 * Cleanup ust session structure
564 */
565void trace_ust_destroy_session(struct ltt_ust_session *session)
566{
ed52805d 567 /* Extra protection */
97ee3a89
DG
568 if (session == NULL) {
569 return;
570 }
571
f6a9efaa
DG
572 rcu_read_lock();
573
fc4705fe 574 DBG2("Trace UST destroy session %u", session->id);
f6a9efaa 575
f6a9efaa
DG
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);
44d3bd01 580
97ee3a89 581 free(session);
f6a9efaa
DG
582
583 rcu_read_unlock();
97ee3a89 584}
This page took 0.056162 seconds and 4 git commands to generate.