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"
30 * Match function for the events hash table lookup.
32 * Matches by name only. Used by the disable command.
34 int trace_ust_ht_match_event_by_name(struct cds_lfht_node
*node
,
37 struct ltt_ust_event
*event
;
43 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
47 if (strncmp(event
->attr
.name
, name
, sizeof(event
->attr
.name
)) != 0) {
59 * Match function for the hash table lookup.
61 * It matches an ust event based on three attributes which are the event name,
62 * the filter bytecode and the loglevel.
64 int trace_ust_ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
66 struct ltt_ust_event
*event
;
67 const struct ltt_ust_ht_key
*key
;
72 event
= caa_container_of(node
, struct ltt_ust_event
, node
.node
);
75 /* Match the 3 elements of the key: name, filter and loglevel. */
78 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
83 if (event
->attr
.loglevel
!= key
->loglevel
) {
84 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
85 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
87 * Match is accepted. This is because on event creation, the
88 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
89 * -1 are accepted for this loglevel type since 0 is the one set by
90 * the API when receiving an enable event.
97 /* Only one of the filters is NULL, fail. */
98 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
102 if (key
->filter
&& event
->filter
) {
103 /* Both filters exists, check length followed by the bytecode. */
104 if (event
->filter
->len
!= key
->filter
->len
||
105 memcmp(event
->filter
->data
, key
->filter
->data
,
106 event
->filter
->len
) != 0) {
119 * Find the channel in the hashtable and return channel pointer. RCU read side
120 * lock MUST be acquired before calling this.
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
;
128 lttng_ht_lookup(ht
, (void *)name
, &iter
);
129 node
= lttng_ht_iter_get_node_str(&iter
);
134 DBG2("Trace UST channel %s found by name", name
);
136 return caa_container_of(node
, struct ltt_ust_channel
, node
);
139 DBG2("Trace UST channel %s not found by name", name
);
144 * Find the event in the hashtable and return event pointer. RCU read side lock
145 * MUST be acquired before calling this.
147 struct ltt_ust_event
*trace_ust_find_event(struct lttng_ht
*ht
,
148 char *name
, struct lttng_filter_bytecode
*filter
, int loglevel
)
150 struct lttng_ht_node_str
*node
;
151 struct lttng_ht_iter iter
;
152 struct ltt_ust_ht_key key
;
159 key
.loglevel
= loglevel
;
161 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
162 trace_ust_ht_match_event
, &key
, &iter
.iter
);
163 node
= lttng_ht_iter_get_node_str(&iter
);
168 DBG2("Trace UST event %s found", key
.name
);
170 return caa_container_of(node
, struct ltt_ust_event
, node
);
173 DBG2("Trace UST event %s NOT found", key
.name
);
178 * Allocate and initialize a ust session data structure.
180 * Return pointer to structure or NULL.
182 struct ltt_ust_session
*trace_ust_create_session(char *path
,
183 unsigned int session_id
)
185 struct ltt_ust_session
*lus
;
187 /* Allocate a new ltt ust session */
188 lus
= zmalloc(sizeof(struct ltt_ust_session
));
190 PERROR("create ust session zmalloc");
194 /* Init data structure */
195 lus
->id
= session_id
;
196 lus
->start_trace
= 0;
198 /* Alloc UST domain hash tables */
199 lus
->domain_pid
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
200 lus
->domain_exec
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
202 /* Alloc UST global domain channels' HT */
203 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
205 lus
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
206 if (lus
->consumer
== NULL
) {
211 * The tmp_consumer stays NULL until a set_consumer_uri command is
212 * executed. At this point, the consumer should be nullify until an
213 * enable_consumer command. This assignment is symbolic since we've zmalloc
216 lus
->tmp_consumer
= NULL
;
218 /* Use the default consumer output which is the tracing session path. */
219 if (path
&& strlen(path
) > 0) {
222 ret
= snprintf(lus
->consumer
->dst
.trace_path
, PATH_MAX
,
223 "%s" DEFAULT_UST_TRACE_DIR
, path
);
225 PERROR("snprintf UST consumer trace path");
229 /* Set session path */
230 ret
= snprintf(lus
->pathname
, PATH_MAX
, "%s" DEFAULT_UST_TRACE_DIR
,
233 PERROR("snprintf kernel traces path");
238 DBG2("UST trace session create successful");
243 consumer_destroy_output(lus
->consumer
);
245 lttng_ht_destroy(lus
->domain_global
.channels
);
246 lttng_ht_destroy(lus
->domain_exec
);
247 lttng_ht_destroy(lus
->domain_pid
);
254 * Allocate and initialize a ust channel data structure.
256 * Return pointer to structure or NULL.
258 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
262 struct ltt_ust_channel
*luc
;
267 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
269 PERROR("ltt_ust_channel zmalloc");
273 /* Copy UST channel attributes */
274 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
275 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
276 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
277 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
278 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
279 luc
->attr
.output
= (enum lttng_ust_output
) chan
->attr
.output
;
281 /* Translate to UST output enum */
282 switch (luc
->attr
.output
) {
284 luc
->attr
.output
= LTTNG_UST_MMAP
;
288 /* Copy channel name */
289 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
290 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
293 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
294 /* Alloc hash tables */
295 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
296 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
298 /* Set trace output path */
299 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
301 PERROR("asprintf ust create channel");
302 goto error_free_channel
;
305 DBG2("Trace UST channel %s created", luc
->name
);
310 lttng_ht_destroy(luc
->ctx
);
311 lttng_ht_destroy(luc
->events
);
318 * Allocate and initialize a ust event. Set name and event type.
320 * Return pointer to structure or NULL.
322 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
,
323 struct lttng_filter_bytecode
*filter
)
325 struct ltt_ust_event
*lue
;
329 lue
= zmalloc(sizeof(struct ltt_ust_event
));
331 PERROR("ust event zmalloc");
336 case LTTNG_EVENT_PROBE
:
337 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
339 case LTTNG_EVENT_FUNCTION
:
340 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
342 case LTTNG_EVENT_FUNCTION_ENTRY
:
343 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
345 case LTTNG_EVENT_TRACEPOINT
:
346 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
349 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
350 goto error_free_event
;
353 /* Copy event name */
354 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
355 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
357 switch (ev
->loglevel_type
) {
358 case LTTNG_EVENT_LOGLEVEL_ALL
:
359 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
360 lue
->attr
.loglevel
= -1; /* Force to -1 */
362 case LTTNG_EVENT_LOGLEVEL_RANGE
:
363 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
364 lue
->attr
.loglevel
= ev
->loglevel
;
366 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
367 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
368 lue
->attr
.loglevel
= ev
->loglevel
;
371 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
372 goto error_free_event
;
376 lue
->filter
= (struct lttng_ust_filter_bytecode
*) filter
;
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
;
405 lum
= zmalloc(sizeof(struct ltt_ust_metadata
));
407 PERROR("ust metadata zmalloc");
411 /* Set default attributes */
412 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
413 lum
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
414 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
415 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
416 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
417 lum
->attr
.output
= LTTNG_UST_MMAP
;
420 /* Set metadata trace path */
421 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
423 PERROR("asprintf ust metadata");
424 goto error_free_metadata
;
436 * Allocate and initialize an UST context.
438 * Return pointer to structure or NULL.
440 struct ltt_ust_context
*trace_ust_create_context(
441 struct lttng_event_context
*ctx
)
443 struct ltt_ust_context
*uctx
;
444 enum lttng_ust_context_type utype
;
449 case LTTNG_EVENT_CONTEXT_VTID
:
450 utype
= LTTNG_UST_CONTEXT_VTID
;
452 case LTTNG_EVENT_CONTEXT_VPID
:
453 utype
= LTTNG_UST_CONTEXT_VPID
;
455 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
456 utype
= LTTNG_UST_CONTEXT_PTHREAD_ID
;
458 case LTTNG_EVENT_CONTEXT_PROCNAME
:
459 utype
= LTTNG_UST_CONTEXT_PROCNAME
;
462 ERR("Invalid UST context");
466 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
468 PERROR("zmalloc ltt_ust_context");
472 uctx
->ctx
.ctx
= utype
;
473 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
482 * RCU safe free context structure.
484 static void destroy_context_rcu(struct rcu_head
*head
)
486 struct lttng_ht_node_ulong
*node
=
487 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
488 struct ltt_ust_context
*ctx
=
489 caa_container_of(node
, struct ltt_ust_context
, node
);
495 * Cleanup UST context hash table.
497 static void destroy_contexts(struct lttng_ht
*ht
)
500 struct lttng_ht_node_ulong
*node
;
501 struct lttng_ht_iter iter
;
505 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
506 ret
= lttng_ht_del(ht
, &iter
);
508 call_rcu(&node
->head
, destroy_context_rcu
);
512 lttng_ht_destroy(ht
);
516 * Cleanup ust event structure.
518 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
522 DBG2("Trace destroy UST event %s", event
->attr
.name
);
528 * URCU intermediate call to complete destroy event.
530 static void destroy_event_rcu(struct rcu_head
*head
)
532 struct lttng_ht_node_str
*node
=
533 caa_container_of(head
, struct lttng_ht_node_str
, head
);
534 struct ltt_ust_event
*event
=
535 caa_container_of(node
, struct ltt_ust_event
, node
);
537 trace_ust_destroy_event(event
);
541 * Cleanup UST events hashtable.
543 static void destroy_events(struct lttng_ht
*events
)
546 struct lttng_ht_node_str
*node
;
547 struct lttng_ht_iter iter
;
551 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
552 ret
= lttng_ht_del(events
, &iter
);
554 call_rcu(&node
->head
, destroy_event_rcu
);
557 lttng_ht_destroy(events
);
561 * Cleanup ust channel structure.
563 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
567 DBG2("Trace destroy UST channel %s", channel
->name
);
571 /* Destroying all events of the channel */
572 destroy_events(channel
->events
);
573 /* Destroying all context of the channel */
574 destroy_contexts(channel
->ctx
);
582 * URCU intermediate call to complete destroy channel.
584 static void destroy_channel_rcu(struct rcu_head
*head
)
586 struct lttng_ht_node_str
*node
=
587 caa_container_of(head
, struct lttng_ht_node_str
, head
);
588 struct ltt_ust_channel
*channel
=
589 caa_container_of(node
, struct ltt_ust_channel
, node
);
591 trace_ust_destroy_channel(channel
);
595 * Cleanup ust metadata structure.
597 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
601 if (!metadata
->handle
) {
604 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
609 * Iterate over a hash table containing channels and cleanup safely.
611 static void destroy_channels(struct lttng_ht
*channels
)
614 struct lttng_ht_node_str
*node
;
615 struct lttng_ht_iter iter
;
621 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
622 ret
= lttng_ht_del(channels
, &iter
);
624 call_rcu(&node
->head
, destroy_channel_rcu
);
627 lttng_ht_destroy(channels
);
633 * Cleanup UST pid domain.
635 static void destroy_domain_pid(struct lttng_ht
*ht
)
638 struct lttng_ht_iter iter
;
639 struct ltt_ust_domain_pid
*dpid
;
643 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dpid
, node
.node
) {
644 ret
= lttng_ht_del(ht
, &iter
);
646 destroy_channels(dpid
->channels
);
649 lttng_ht_destroy(ht
);
653 * Cleanup UST exec name domain.
655 static void destroy_domain_exec(struct lttng_ht
*ht
)
658 struct lttng_ht_iter iter
;
659 struct ltt_ust_domain_exec
*dexec
;
663 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dexec
, node
.node
) {
664 ret
= lttng_ht_del(ht
, &iter
);
666 destroy_channels(dexec
->channels
);
669 lttng_ht_destroy(ht
);
673 * Cleanup UST global domain.
675 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
679 destroy_channels(dom
->channels
);
683 * Cleanup ust session structure
685 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
691 DBG2("Trace UST destroy session %u", session
->id
);
693 /* Cleaning up UST domain */
694 destroy_domain_global(&session
->domain_global
);
695 destroy_domain_pid(session
->domain_pid
);
696 destroy_domain_exec(session
->domain_exec
);
698 consumer_destroy_output(session
->consumer
);
699 consumer_destroy_output(session
->tmp_consumer
);