2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
24 #include <common/common.h>
25 #include <common/defaults.h>
27 #include "trace-ust.h"
29 int trace_ust_ht_match_event_by_name(struct cds_lfht_node
*node
,
32 struct ltt_ust_event
*event
;
38 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
42 if (strncmp(event
->attr
.name
, name
, sizeof(event
->attr
.name
)) != 0) {
53 int trace_ust_ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
55 struct ltt_ust_event
*event
;
56 const struct ltt_ust_ht_key
*key
;
61 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
64 /* Match the 3 elements of the key: name, filter and loglevel. */
67 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
68 DBG3("[Match] name failed: %s and %s",
69 event
->attr
.name
, key
->name
);
74 if (event
->attr
.loglevel
!= key
->loglevel
) {
75 if (event
->attr
.loglevel_type
== 0 && key
->loglevel
== 0 &&
76 event
->attr
.loglevel
== -1) {
78 * This is because on event creation, the loglevel is set to -1 if
79 * the event loglevel type is ALL so 0 and -1 are accepted for this
83 DBG3("[Match] loglevel failed: %d and %d",
84 event
->attr
.loglevel
, key
->loglevel
);
89 /* Only one of the filters is NULL, fail. */
90 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
91 DBG3("[Match] filters failed: k:%p and e:%p",
92 key
->filter
, event
->filter
);
96 /* Both filters are NULL, success. */
97 if (!key
->filter
&& !event
->filter
) {
101 /* Both filters exists, check length followed by the bytecode. */
102 if (event
->filter
->len
== key
->filter
->len
&&
103 memcmp(event
->filter
->data
, key
->filter
->data
,
104 event
->filter
->len
) == 0) {
108 DBG3("[Match] filters failed: k:%p and e:%p",
109 key
->filter
, event
->filter
);
115 DBG3("[MATCH] %s", key
->name
);
120 * Find the channel in the hashtable.
122 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct lttng_ht
*ht
,
125 struct lttng_ht_node_str
*node
;
126 struct lttng_ht_iter iter
;
129 lttng_ht_lookup(ht
, (void *)name
, &iter
);
130 node
= lttng_ht_iter_get_node_str(&iter
);
137 DBG2("Trace UST channel %s found by name", name
);
139 return caa_container_of(node
, struct ltt_ust_channel
, node
);
142 DBG2("Trace UST channel %s not found by name", name
);
147 * Find the event in the hashtable.
149 struct ltt_ust_event
*trace_ust_find_event(struct lttng_ht
*ht
,
150 char *name
, struct lttng_filter_bytecode
*filter
, int loglevel
)
152 struct lttng_ht_node_str
*node
;
153 struct lttng_ht_iter iter
;
154 struct ltt_ust_ht_key key
;
155 void *orig_match_fct
;
162 key
.loglevel
= loglevel
;
164 /* Save match function so we can use the ust app event match. */
165 orig_match_fct
= (void *) ht
->match_fct
;
166 ht
->match_fct
= trace_ust_ht_match_event
;
168 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
169 trace_ust_ht_match_event
, &key
, &iter
.iter
);
170 node
= lttng_ht_iter_get_node_str(&iter
);
175 DBG2("Trace UST event %s found", key
.name
);
177 return caa_container_of(node
, struct ltt_ust_event
, node
);
180 DBG2("Trace UST event %s NOT found", key
.name
);
181 /* Put back original match function. */
182 ht
->match_fct
= orig_match_fct
;
187 * Allocate and initialize a ust session data structure.
189 * Return pointer to structure or NULL.
191 struct ltt_ust_session
*trace_ust_create_session(char *path
,
192 unsigned int session_id
, struct lttng_domain
*domain
)
195 struct ltt_ust_session
*lus
;
197 /* Allocate a new ltt ust session */
198 lus
= zmalloc(sizeof(struct ltt_ust_session
));
200 PERROR("create ust session zmalloc");
204 /* Init data structure */
205 lus
->id
= session_id
;
206 lus
->start_trace
= 0;
208 /* Alloc UST domain hash tables */
209 lus
->domain_pid
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
210 lus
->domain_exec
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
212 /* Alloc UST global domain channels' HT */
213 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
215 lus
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
216 if (lus
->consumer
== NULL
) {
221 * The tmp_consumer stays NULL until a set_consumer_uri command is
222 * executed. At this point, the consumer should be nullify until an
223 * enable_consumer command. This assignment is symbolic since we've zmalloc
226 lus
->tmp_consumer
= NULL
;
228 /* Use the default consumer output which is the tracing session path. */
229 if (path
&& strlen(path
) > 0) {
230 ret
= snprintf(lus
->consumer
->dst
.trace_path
, PATH_MAX
,
231 "%s" DEFAULT_UST_TRACE_DIR
, path
);
233 PERROR("snprintf UST consumer trace path");
237 /* Set session path */
238 ret
= snprintf(lus
->pathname
, PATH_MAX
, "%s" DEFAULT_UST_TRACE_DIR
,
241 PERROR("snprintf kernel traces path");
246 DBG2("UST trace session create successful");
251 consumer_destroy_output(lus
->consumer
);
253 lttng_ht_destroy(lus
->domain_global
.channels
);
254 lttng_ht_destroy(lus
->domain_exec
);
255 lttng_ht_destroy(lus
->domain_pid
);
262 * Allocate and initialize a ust channel data structure.
264 * Return pointer to structure or NULL.
266 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
270 struct ltt_ust_channel
*luc
;
272 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
274 PERROR("ltt_ust_channel zmalloc");
278 /* Copy UST channel attributes */
279 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
280 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
281 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
282 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
283 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
284 luc
->attr
.output
= (enum lttng_ust_output
) chan
->attr
.output
;
286 /* Translate to UST output enum */
287 switch (luc
->attr
.output
) {
289 luc
->attr
.output
= LTTNG_UST_MMAP
;
293 /* Copy channel name */
294 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
295 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
298 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
299 /* Alloc hash tables */
300 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
301 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
303 /* Set trace output path */
304 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
306 PERROR("asprintf ust create channel");
307 goto error_free_channel
;
310 DBG2("Trace UST channel %s created", luc
->name
);
315 lttng_ht_destroy(luc
->ctx
);
316 lttng_ht_destroy(luc
->events
);
323 * Allocate and initialize a ust event. Set name and event type.
325 * Return pointer to structure or NULL.
327 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
329 struct ltt_ust_event
*lue
;
331 lue
= zmalloc(sizeof(struct ltt_ust_event
));
333 PERROR("ust event zmalloc");
338 case LTTNG_EVENT_PROBE
:
339 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
341 case LTTNG_EVENT_FUNCTION
:
342 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
344 case LTTNG_EVENT_FUNCTION_ENTRY
:
345 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
347 case LTTNG_EVENT_TRACEPOINT
:
348 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
351 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
352 goto error_free_event
;
355 /* Copy event name */
356 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
357 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
359 switch (ev
->loglevel_type
) {
360 case LTTNG_EVENT_LOGLEVEL_ALL
:
361 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
362 lue
->attr
.loglevel
= -1; /* Force to -1 */
364 case LTTNG_EVENT_LOGLEVEL_RANGE
:
365 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
366 lue
->attr
.loglevel
= ev
->loglevel
;
368 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
369 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
370 lue
->attr
.loglevel
= ev
->loglevel
;
373 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
374 goto error_free_event
;
379 lttng_ht_node_init_str(&lue
->node
, lue
->attr
.name
);
381 DBG2("Trace UST event %s, loglevel (%d,%d) created",
382 lue
->attr
.name
, lue
->attr
.loglevel_type
,
394 * Allocate and initialize a ust metadata.
396 * Return pointer to structure or NULL.
398 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
401 struct ltt_ust_metadata
*lum
;
403 lum
= zmalloc(sizeof(struct ltt_ust_metadata
));
405 PERROR("ust metadata zmalloc");
409 /* Set default attributes */
410 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
411 lum
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
412 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
413 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
414 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
415 lum
->attr
.output
= LTTNG_UST_MMAP
;
418 /* Set metadata trace path */
419 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
421 PERROR("asprintf ust metadata");
422 goto error_free_metadata
;
434 * Allocate and initialize an UST context.
436 * Return pointer to structure or NULL.
438 struct ltt_ust_context
*trace_ust_create_context(
439 struct lttng_event_context
*ctx
)
441 struct ltt_ust_context
*uctx
;
442 enum lttng_ust_context_type utype
;
445 case LTTNG_EVENT_CONTEXT_VTID
:
446 utype
= LTTNG_UST_CONTEXT_VTID
;
448 case LTTNG_EVENT_CONTEXT_VPID
:
449 utype
= LTTNG_UST_CONTEXT_VPID
;
451 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
452 utype
= LTTNG_UST_CONTEXT_PTHREAD_ID
;
454 case LTTNG_EVENT_CONTEXT_PROCNAME
:
455 utype
= LTTNG_UST_CONTEXT_PROCNAME
;
458 ERR("Invalid UST context");
462 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
464 PERROR("zmalloc ltt_ust_context");
468 uctx
->ctx
.ctx
= utype
;
469 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
478 * RCU safe free context structure.
480 static void destroy_context_rcu(struct rcu_head
*head
)
482 struct lttng_ht_node_ulong
*node
=
483 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
484 struct ltt_ust_context
*ctx
=
485 caa_container_of(node
, struct ltt_ust_context
, node
);
491 * Cleanup UST context hash table.
493 static void destroy_contexts(struct lttng_ht
*ht
)
496 struct lttng_ht_node_ulong
*node
;
497 struct lttng_ht_iter iter
;
499 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
500 ret
= lttng_ht_del(ht
, &iter
);
502 call_rcu(&node
->head
, destroy_context_rcu
);
506 lttng_ht_destroy(ht
);
510 * Cleanup ust event structure.
512 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
514 DBG2("Trace destroy UST event %s", event
->attr
.name
);
520 * URCU intermediate call to complete destroy event.
522 static void destroy_event_rcu(struct rcu_head
*head
)
524 struct lttng_ht_node_str
*node
=
525 caa_container_of(head
, struct lttng_ht_node_str
, head
);
526 struct ltt_ust_event
*event
=
527 caa_container_of(node
, struct ltt_ust_event
, node
);
529 trace_ust_destroy_event(event
);
533 * Cleanup UST events hashtable.
535 static void destroy_events(struct lttng_ht
*events
)
538 struct lttng_ht_node_str
*node
;
539 struct lttng_ht_iter iter
;
541 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
542 ret
= lttng_ht_del(events
, &iter
);
544 call_rcu(&node
->head
, destroy_event_rcu
);
547 lttng_ht_destroy(events
);
551 * Cleanup ust channel structure.
553 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
555 DBG2("Trace destroy UST channel %s", channel
->name
);
559 /* Destroying all events of the channel */
560 destroy_events(channel
->events
);
561 /* Destroying all context of the channel */
562 destroy_contexts(channel
->ctx
);
570 * URCU intermediate call to complete destroy channel.
572 static void destroy_channel_rcu(struct rcu_head
*head
)
574 struct lttng_ht_node_str
*node
=
575 caa_container_of(head
, struct lttng_ht_node_str
, head
);
576 struct ltt_ust_channel
*channel
=
577 caa_container_of(node
, struct ltt_ust_channel
, node
);
579 trace_ust_destroy_channel(channel
);
583 * Cleanup ust metadata structure.
585 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
587 if (!metadata
->handle
) {
590 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
595 * Iterate over a hash table containing channels and cleanup safely.
597 static void destroy_channels(struct lttng_ht
*channels
)
600 struct lttng_ht_node_str
*node
;
601 struct lttng_ht_iter iter
;
605 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
606 ret
= lttng_ht_del(channels
, &iter
);
608 call_rcu(&node
->head
, destroy_channel_rcu
);
611 lttng_ht_destroy(channels
);
617 * Cleanup UST pid domain.
619 static void destroy_domain_pid(struct lttng_ht
*ht
)
622 struct lttng_ht_iter iter
;
623 struct ltt_ust_domain_pid
*dpid
;
625 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dpid
, node
.node
) {
626 ret
= lttng_ht_del(ht
, &iter
);
628 destroy_channels(dpid
->channels
);
631 lttng_ht_destroy(ht
);
635 * Cleanup UST exec name domain.
637 static void destroy_domain_exec(struct lttng_ht
*ht
)
640 struct lttng_ht_iter iter
;
641 struct ltt_ust_domain_exec
*dexec
;
643 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dexec
, node
.node
) {
644 ret
= lttng_ht_del(ht
, &iter
);
646 destroy_channels(dexec
->channels
);
649 lttng_ht_destroy(ht
);
653 * Cleanup UST global domain.
655 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
657 destroy_channels(dom
->channels
);
661 * Cleanup ust session structure
663 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
665 /* Extra protection */
666 if (session
== NULL
) {
672 DBG2("Trace UST destroy session %u", session
->id
);
674 /* Cleaning up UST domain */
675 destroy_domain_global(&session
->domain_global
);
676 destroy_domain_pid(session
->domain_pid
);
677 destroy_domain_exec(session
->domain_exec
);
679 consumer_destroy_output(session
->consumer
);
680 consumer_destroy_output(session
->tmp_consumer
);