Implement filter bytecode support in lttng-session, and parse filter string
[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) {
114 goto error;
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 ret = snprintf(lus->consumer->dst.trace_path, PATH_MAX, "%s/ust", path);
127 if (ret < 0) {
128 PERROR("snprintf UST consumer trace path");
129 goto error;
130 }
131
0177d773 132 /* Set session path */
48842b30 133 ret = snprintf(lus->pathname, PATH_MAX, "%s/ust", path);
0177d773 134 if (ret < 0) {
44d3bd01 135 PERROR("snprintf kernel traces path");
44844c29 136 goto error_free_session;
0177d773
DG
137 }
138
44d3bd01
DG
139 DBG2("UST trace session create successful");
140
97ee3a89
DG
141 return lus;
142
44844c29 143error_free_session:
a2c0da86
MD
144 lttng_ht_destroy(lus->domain_global.channels);
145 lttng_ht_destroy(lus->domain_exec);
146 lttng_ht_destroy(lus->domain_pid);
44844c29 147 free(lus);
97ee3a89
DG
148error:
149 return NULL;
150}
151
152/*
153 * Allocate and initialize a ust channel data structure.
154 *
155 * Return pointer to structure or NULL.
156 */
44d3bd01
DG
157struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
158 char *path)
97ee3a89
DG
159{
160 int ret;
161 struct ltt_ust_channel *luc;
162
37357452 163 luc = zmalloc(sizeof(struct ltt_ust_channel));
97ee3a89 164 if (luc == NULL) {
df0f840b 165 PERROR("ltt_ust_channel zmalloc");
97ee3a89
DG
166 goto error;
167 }
168
44d3bd01 169 /* Copy UST channel attributes */
48842b30
DG
170 luc->attr.overwrite = chan->attr.overwrite;
171 luc->attr.subbuf_size = chan->attr.subbuf_size;
172 luc->attr.num_subbuf = chan->attr.num_subbuf;
173 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
174 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
6775595e 175 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
44d3bd01
DG
176
177 /* Translate to UST output enum */
178 switch (luc->attr.output) {
179 default:
180 luc->attr.output = LTTNG_UST_MMAP;
181 break;
97ee3a89 182 }
97ee3a89 183
97ee3a89 184 /* Copy channel name */
0a1459bb 185 strncpy(luc->name, chan->name, sizeof(luc->name));
97ee3a89
DG
186 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
187
f6a9efaa 188 /* Init node */
bec39940 189 lttng_ht_node_init_str(&luc->node, luc->name);
f6a9efaa 190 /* Alloc hash tables */
bec39940
DG
191 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
192 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa 193
97ee3a89 194 /* Set trace output path */
48842b30 195 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
97ee3a89 196 if (ret < 0) {
df0f840b 197 PERROR("asprintf ust create channel");
44844c29 198 goto error_free_channel;
97ee3a89
DG
199 }
200
f6a9efaa
DG
201 DBG2("Trace UST channel %s created", luc->name);
202
97ee3a89
DG
203 return luc;
204
44844c29 205error_free_channel:
a2c0da86
MD
206 lttng_ht_destroy(luc->ctx);
207 lttng_ht_destroy(luc->events);
44844c29 208 free(luc);
97ee3a89
DG
209error:
210 return NULL;
211}
212
213/*
214 * Allocate and initialize a ust event. Set name and event type.
215 *
216 * Return pointer to structure or NULL.
217 */
218struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
219{
220 struct ltt_ust_event *lue;
97ee3a89 221
37357452 222 lue = zmalloc(sizeof(struct ltt_ust_event));
44d3bd01 223 if (lue == NULL) {
ba7f0ae5 224 PERROR("ust event zmalloc");
97ee3a89
DG
225 goto error;
226 }
227
228 switch (ev->type) {
229 case LTTNG_EVENT_PROBE:
44d3bd01 230 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
231 break;
232 case LTTNG_EVENT_FUNCTION:
44d3bd01 233 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
234 break;
235 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 236 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
237 break;
238 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 239 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
240 break;
241 default:
242 ERR("Unknown ust instrumentation type (%d)", ev->type);
44844c29 243 goto error_free_event;
97ee3a89
DG
244 }
245
246 /* Copy event name */
44d3bd01
DG
247 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
248 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 249
0cda4f28 250 switch (ev->loglevel_type) {
8005f29a
MD
251 case LTTNG_EVENT_LOGLEVEL_ALL:
252 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
22e25b71 253 lue->attr.loglevel = -1; /* Force to -1 */
0cda4f28 254 break;
8005f29a
MD
255 case LTTNG_EVENT_LOGLEVEL_RANGE:
256 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
22e25b71 257 lue->attr.loglevel = ev->loglevel;
8005f29a
MD
258 break;
259 case LTTNG_EVENT_LOGLEVEL_SINGLE:
260 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
22e25b71 261 lue->attr.loglevel = ev->loglevel;
0cda4f28
MD
262 break;
263 default:
4431494b 264 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
0cda4f28
MD
265 goto error_free_event;
266 }
267
0cda4f28 268
f6a9efaa 269 /* Init node */
bec39940 270 lttng_ht_node_init_str(&lue->node, lue->attr.name);
f6a9efaa 271 /* Alloc context hash tables */
bec39940 272 lue->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
af061a47
DG
273 if (lue->ctx == NULL) {
274 ERR("Unable to create context hash table for event %s", ev->name);
275 goto error_free_event;
276 }
97ee3a89 277
300b8fd5
MD
278 DBG2("Trace UST event %s, loglevel (%d,%d) created",
279 lue->attr.name, lue->attr.loglevel_type,
280 lue->attr.loglevel);
284d8f55 281
97ee3a89
DG
282 return lue;
283
44844c29
MD
284error_free_event:
285 free(lue);
97ee3a89
DG
286error:
287 return NULL;
288}
289
290/*
291 * Allocate and initialize a ust metadata.
292 *
293 * Return pointer to structure or NULL.
294 */
295struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
296{
297 int ret;
298 struct ltt_ust_metadata *lum;
97ee3a89 299
3a009adb 300 lum = zmalloc(sizeof(struct ltt_ust_metadata));
44d3bd01 301 if (lum == NULL) {
df0f840b 302 PERROR("ust metadata zmalloc");
97ee3a89
DG
303 goto error;
304 }
305
306 /* Set default attributes */
44d3bd01
DG
307 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
308 lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
309 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
310 lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
311 lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
d41f73b7 312 lum->attr.output = LTTNG_UST_MMAP;
44d3bd01 313
97ee3a89
DG
314 lum->handle = -1;
315 /* Set metadata trace path */
f6a9efaa 316 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
97ee3a89 317 if (ret < 0) {
df0f840b 318 PERROR("asprintf ust metadata");
44844c29 319 goto error_free_metadata;
97ee3a89
DG
320 }
321
322 return lum;
323
44844c29
MD
324error_free_metadata:
325 free(lum);
97ee3a89
DG
326error:
327 return NULL;
328}
329
55cc08a6
DG
330/*
331 * Allocate and initialize an UST context.
332 *
333 * Return pointer to structure or NULL.
334 */
335struct ltt_ust_context *trace_ust_create_context(
336 struct lttng_event_context *ctx)
337{
338 struct ltt_ust_context *uctx;
9197c5c4
MD
339 enum lttng_ust_context_type utype;
340
341 switch (ctx->ctx) {
342 case LTTNG_EVENT_CONTEXT_VTID:
343 utype = LTTNG_UST_CONTEXT_VTID;
344 break;
345 case LTTNG_EVENT_CONTEXT_VPID:
346 utype = LTTNG_UST_CONTEXT_VPID;
347 break;
348 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
349 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
350 break;
351 case LTTNG_EVENT_CONTEXT_PROCNAME:
352 utype = LTTNG_UST_CONTEXT_PROCNAME;
353 break;
354 default:
355 ERR("Invalid UST context");
356 return NULL;
357 }
55cc08a6
DG
358
359 uctx = zmalloc(sizeof(struct ltt_ust_context));
360 if (uctx == NULL) {
361 PERROR("zmalloc ltt_ust_context");
362 goto error;
363 }
364
9197c5c4 365 uctx->ctx.ctx = utype;
bec39940 366 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
55cc08a6
DG
367
368 return uctx;
369
370error:
371 return NULL;
372}
373
f6a9efaa
DG
374/*
375 * RCU safe free context structure.
376 */
377static void destroy_context_rcu(struct rcu_head *head)
378{
bec39940
DG
379 struct lttng_ht_node_ulong *node =
380 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa
DG
381 struct ltt_ust_context *ctx =
382 caa_container_of(node, struct ltt_ust_context, node);
383
384 free(ctx);
385}
386
387/*
388 * Cleanup UST context hash table.
389 */
7bd39047 390static void destroy_contexts(struct lttng_ht *ht)
f6a9efaa
DG
391{
392 int ret;
bec39940
DG
393 struct lttng_ht_node_ulong *node;
394 struct lttng_ht_iter iter;
f6a9efaa 395
bec39940
DG
396 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
397 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
398 if (!ret) {
399 call_rcu(&node->head, destroy_context_rcu);
400 }
f6a9efaa 401 }
ed52805d 402
bec39940 403 lttng_ht_destroy(ht);
f6a9efaa
DG
404}
405
97ee3a89
DG
406/*
407 * Cleanup ust event structure.
408 */
409void trace_ust_destroy_event(struct ltt_ust_event *event)
410{
f6a9efaa 411 DBG2("Trace destroy UST event %s", event->attr.name);
7bd39047 412 destroy_contexts(event->ctx);
53a80697 413 free(event->filter);
97ee3a89
DG
414 free(event);
415}
416
f6a9efaa
DG
417/*
418 * URCU intermediate call to complete destroy event.
419 */
420static void destroy_event_rcu(struct rcu_head *head)
421{
bec39940
DG
422 struct lttng_ht_node_str *node =
423 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
424 struct ltt_ust_event *event =
425 caa_container_of(node, struct ltt_ust_event, node);
426
427 trace_ust_destroy_event(event);
428}
429
284d8f55
DG
430/*
431 * Cleanup UST events hashtable.
432 */
7bd39047 433static void destroy_events(struct lttng_ht *events)
ed52805d
DG
434{
435 int ret;
bec39940
DG
436 struct lttng_ht_node_str *node;
437 struct lttng_ht_iter iter;
ed52805d 438
bec39940
DG
439 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
440 ret = lttng_ht_del(events, &iter);
7bd39047
DG
441 assert(!ret);
442 call_rcu(&node->head, destroy_event_rcu);
ed52805d
DG
443 }
444
bec39940 445 lttng_ht_destroy(events);
ed52805d
DG
446}
447
97ee3a89
DG
448/*
449 * Cleanup ust channel structure.
450 */
451void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
452{
f6a9efaa 453 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 454
f6a9efaa
DG
455 rcu_read_lock();
456
7bd39047
DG
457 /* Destroying all events of the channel */
458 destroy_events(channel->events);
459 /* Destroying all context of the channel */
460 destroy_contexts(channel->ctx);
97ee3a89 461
97ee3a89 462 free(channel);
f6a9efaa
DG
463
464 rcu_read_unlock();
465}
466
467/*
468 * URCU intermediate call to complete destroy channel.
469 */
470static void destroy_channel_rcu(struct rcu_head *head)
471{
bec39940
DG
472 struct lttng_ht_node_str *node =
473 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
474 struct ltt_ust_channel *channel =
475 caa_container_of(node, struct ltt_ust_channel, node);
476
477 trace_ust_destroy_channel(channel);
97ee3a89
DG
478}
479
480/*
481 * Cleanup ust metadata structure.
482 */
483void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
484{
abc9138a
MD
485 if (!metadata->handle) {
486 return;
487 }
f6a9efaa 488 DBG2("Trace UST destroy metadata %d", metadata->handle);
97ee3a89
DG
489 free(metadata);
490}
491
492/*
f6a9efaa 493 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 494 */
bec39940 495static void destroy_channels(struct lttng_ht *channels)
f6a9efaa
DG
496{
497 int ret;
bec39940
DG
498 struct lttng_ht_node_str *node;
499 struct lttng_ht_iter iter;
f6a9efaa 500
24d1723f
DG
501 rcu_read_lock();
502
bec39940
DG
503 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
504 ret = lttng_ht_del(channels, &iter);
7bd39047
DG
505 assert(!ret);
506 call_rcu(&node->head, destroy_channel_rcu);
f6a9efaa 507 }
ed52805d 508
bec39940 509 lttng_ht_destroy(channels);
24d1723f
DG
510
511 rcu_read_unlock();
f6a9efaa
DG
512}
513
514/*
515 * Cleanup UST pid domain.
516 */
bec39940 517static void destroy_domain_pid(struct lttng_ht *ht)
f6a9efaa
DG
518{
519 int ret;
bec39940 520 struct lttng_ht_iter iter;
ed52805d 521 struct ltt_ust_domain_pid *dpid;
f6a9efaa 522
bec39940
DG
523 cds_lfht_for_each_entry(ht->ht, &iter.iter, dpid, node.node) {
524 ret = lttng_ht_del(ht , &iter);
7bd39047
DG
525 assert(!ret);
526 destroy_channels(dpid->channels);
f6a9efaa 527 }
ed52805d 528
bec39940 529 lttng_ht_destroy(ht);
f6a9efaa
DG
530}
531
532/*
533 * Cleanup UST exec name domain.
534 */
bec39940 535static void destroy_domain_exec(struct lttng_ht *ht)
97ee3a89 536{
f6a9efaa 537 int ret;
bec39940 538 struct lttng_ht_iter iter;
ed52805d 539 struct ltt_ust_domain_exec *dexec;
f6a9efaa 540
bec39940
DG
541 cds_lfht_for_each_entry(ht->ht, &iter.iter, dexec, node.node) {
542 ret = lttng_ht_del(ht , &iter);
7bd39047
DG
543 assert(!ret);
544 destroy_channels(dexec->channels);
f6a9efaa 545 }
ed52805d 546
bec39940 547 lttng_ht_destroy(ht);
f6a9efaa 548}
97ee3a89 549
f6a9efaa
DG
550/*
551 * Cleanup UST global domain.
552 */
553static void destroy_domain_global(struct ltt_ust_domain_global *dom)
554{
555 destroy_channels(dom->channels);
556}
97ee3a89 557
f6a9efaa
DG
558/*
559 * Cleanup ust session structure
560 */
561void trace_ust_destroy_session(struct ltt_ust_session *session)
562{
ed52805d 563 /* Extra protection */
97ee3a89
DG
564 if (session == NULL) {
565 return;
566 }
567
f6a9efaa
DG
568 rcu_read_lock();
569
fc4705fe 570 DBG2("Trace UST destroy session %u", session->id);
f6a9efaa 571
f6a9efaa
DG
572 /* Cleaning up UST domain */
573 destroy_domain_global(&session->domain_global);
574 destroy_domain_pid(session->domain_pid);
575 destroy_domain_exec(session->domain_exec);
44d3bd01 576
97ee3a89 577 free(session);
f6a9efaa
DG
578
579 rcu_read_unlock();
97ee3a89 580}
This page took 0.09457 seconds and 4 git commands to generate.