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 * Find the channel in the hashtable.
32 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct lttng_ht
*ht
,
35 struct lttng_ht_node_str
*node
;
36 struct lttng_ht_iter iter
;
39 lttng_ht_lookup(ht
, (void *)name
, &iter
);
40 node
= lttng_ht_iter_get_node_str(&iter
);
47 DBG2("Trace UST channel %s found by name", name
);
49 return caa_container_of(node
, struct ltt_ust_channel
, node
);
52 DBG2("Trace UST channel %s not found by name", name
);
57 * Find the event in the hashtable.
59 struct ltt_ust_event
*trace_ust_find_event_by_name(struct lttng_ht
*ht
,
62 struct lttng_ht_node_str
*node
;
63 struct lttng_ht_iter iter
;
66 lttng_ht_lookup(ht
, (void *) name
, &iter
);
67 node
= lttng_ht_iter_get_node_str(&iter
);
74 DBG2("Trace UST event found by name %s", name
);
76 return caa_container_of(node
, struct ltt_ust_event
, node
);
79 DBG2("Trace UST event NOT found by name %s", name
);
84 * Allocate and initialize a ust session data structure.
86 * Return pointer to structure or NULL.
88 struct ltt_ust_session
*trace_ust_create_session(char *path
, int session_id
,
89 struct lttng_domain
*domain
)
92 struct ltt_ust_session
*lus
;
94 /* Allocate a new ltt ust session */
95 lus
= zmalloc(sizeof(struct ltt_ust_session
));
97 PERROR("create ust session zmalloc");
101 /* Init data structure */
102 lus
->id
= session_id
;
103 lus
->start_trace
= 0;
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
);
109 /* Alloc UST global domain channels' HT */
110 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
112 /* Set session path */
113 ret
= snprintf(lus
->pathname
, PATH_MAX
, "%s/ust", path
);
115 PERROR("snprintf kernel traces path");
116 goto error_free_session
;
119 DBG2("UST trace session create successful");
124 lttng_ht_destroy(lus
->domain_global
.channels
);
125 lttng_ht_destroy(lus
->domain_exec
);
126 lttng_ht_destroy(lus
->domain_pid
);
133 * Allocate and initialize a ust channel data structure.
135 * Return pointer to structure or NULL.
137 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
141 struct ltt_ust_channel
*luc
;
143 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
145 PERROR("ltt_ust_channel zmalloc");
149 /* Copy UST channel attributes */
150 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
151 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
152 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
153 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
154 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
155 luc
->attr
.output
= chan
->attr
.output
;
157 /* Translate to UST output enum */
158 switch (luc
->attr
.output
) {
160 luc
->attr
.output
= LTTNG_UST_MMAP
;
164 /* Copy channel name */
165 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
166 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
169 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
170 /* Alloc hash tables */
171 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
172 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
174 /* Set trace output path */
175 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
177 PERROR("asprintf ust create channel");
178 goto error_free_channel
;
181 DBG2("Trace UST channel %s created", luc
->name
);
186 lttng_ht_destroy(luc
->ctx
);
187 lttng_ht_destroy(luc
->events
);
194 * Allocate and initialize a ust event. Set name and event type.
196 * Return pointer to structure or NULL.
198 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
200 struct ltt_ust_event
*lue
;
202 lue
= zmalloc(sizeof(struct ltt_ust_event
));
204 PERROR("ust event zmalloc");
209 case LTTNG_EVENT_PROBE
:
210 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
212 case LTTNG_EVENT_FUNCTION
:
213 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
215 case LTTNG_EVENT_FUNCTION_ENTRY
:
216 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
218 case LTTNG_EVENT_TRACEPOINT
:
219 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
222 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
223 goto error_free_event
;
226 /* Copy event name */
227 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
228 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
230 switch (ev
->loglevel_type
) {
231 case LTTNG_EVENT_LOGLEVEL_ALL
:
232 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
233 lue
->attr
.loglevel
= -1; /* Force to -1 */
235 case LTTNG_EVENT_LOGLEVEL_RANGE
:
236 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
237 lue
->attr
.loglevel
= ev
->loglevel
;
239 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
240 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
241 lue
->attr
.loglevel
= ev
->loglevel
;
244 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
245 goto error_free_event
;
250 lttng_ht_node_init_str(&lue
->node
, lue
->attr
.name
);
251 /* Alloc context hash tables */
252 lue
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
254 DBG2("Trace UST event %s, loglevel (%d,%d) created",
255 lue
->attr
.name
, lue
->attr
.loglevel_type
,
261 lttng_ht_destroy(lue
->ctx
);
268 * Allocate and initialize a ust metadata.
270 * Return pointer to structure or NULL.
272 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
275 struct ltt_ust_metadata
*lum
;
277 lum
= zmalloc(sizeof(struct ltt_ust_metadata
));
279 PERROR("ust metadata zmalloc");
283 /* Set default attributes */
284 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
285 lum
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
286 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
287 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
288 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
289 lum
->attr
.output
= LTTNG_UST_MMAP
;
292 /* Set metadata trace path */
293 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
295 PERROR("asprintf ust metadata");
296 goto error_free_metadata
;
308 * Allocate and initialize an UST context.
310 * Return pointer to structure or NULL.
312 struct ltt_ust_context
*trace_ust_create_context(
313 struct lttng_event_context
*ctx
)
315 struct ltt_ust_context
*uctx
;
316 enum lttng_ust_context_type utype
;
319 case LTTNG_EVENT_CONTEXT_VTID
:
320 utype
= LTTNG_UST_CONTEXT_VTID
;
322 case LTTNG_EVENT_CONTEXT_VPID
:
323 utype
= LTTNG_UST_CONTEXT_VPID
;
325 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
326 utype
= LTTNG_UST_CONTEXT_PTHREAD_ID
;
328 case LTTNG_EVENT_CONTEXT_PROCNAME
:
329 utype
= LTTNG_UST_CONTEXT_PROCNAME
;
332 ERR("Invalid UST context");
336 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
338 PERROR("zmalloc ltt_ust_context");
342 uctx
->ctx
.ctx
= utype
;
343 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
352 * RCU safe free context structure.
354 static void destroy_context_rcu(struct rcu_head
*head
)
356 struct lttng_ht_node_ulong
*node
=
357 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
358 struct ltt_ust_context
*ctx
=
359 caa_container_of(node
, struct ltt_ust_context
, node
);
365 * Cleanup UST context hash table.
367 static void destroy_contexts(struct lttng_ht
*ht
)
370 struct lttng_ht_node_ulong
*node
;
371 struct lttng_ht_iter iter
;
373 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
374 ret
= lttng_ht_del(ht
, &iter
);
376 call_rcu(&node
->head
, destroy_context_rcu
);
380 lttng_ht_destroy(ht
);
384 * Cleanup ust event structure.
386 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
388 DBG2("Trace destroy UST event %s", event
->attr
.name
);
389 destroy_contexts(event
->ctx
);
395 * URCU intermediate call to complete destroy event.
397 static void destroy_event_rcu(struct rcu_head
*head
)
399 struct lttng_ht_node_str
*node
=
400 caa_container_of(head
, struct lttng_ht_node_str
, head
);
401 struct ltt_ust_event
*event
=
402 caa_container_of(node
, struct ltt_ust_event
, node
);
404 trace_ust_destroy_event(event
);
408 * Cleanup UST events hashtable.
410 static void destroy_events(struct lttng_ht
*events
)
413 struct lttng_ht_node_str
*node
;
414 struct lttng_ht_iter iter
;
416 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
417 ret
= lttng_ht_del(events
, &iter
);
419 call_rcu(&node
->head
, destroy_event_rcu
);
422 lttng_ht_destroy(events
);
426 * Cleanup ust channel structure.
428 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
430 DBG2("Trace destroy UST channel %s", channel
->name
);
434 /* Destroying all events of the channel */
435 destroy_events(channel
->events
);
436 /* Destroying all context of the channel */
437 destroy_contexts(channel
->ctx
);
445 * URCU intermediate call to complete destroy channel.
447 static void destroy_channel_rcu(struct rcu_head
*head
)
449 struct lttng_ht_node_str
*node
=
450 caa_container_of(head
, struct lttng_ht_node_str
, head
);
451 struct ltt_ust_channel
*channel
=
452 caa_container_of(node
, struct ltt_ust_channel
, node
);
454 trace_ust_destroy_channel(channel
);
458 * Cleanup ust metadata structure.
460 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
462 if (!metadata
->handle
) {
465 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
470 * Iterate over a hash table containing channels and cleanup safely.
472 static void destroy_channels(struct lttng_ht
*channels
)
475 struct lttng_ht_node_str
*node
;
476 struct lttng_ht_iter iter
;
480 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
481 ret
= lttng_ht_del(channels
, &iter
);
483 call_rcu(&node
->head
, destroy_channel_rcu
);
486 lttng_ht_destroy(channels
);
492 * Cleanup UST pid domain.
494 static void destroy_domain_pid(struct lttng_ht
*ht
)
497 struct lttng_ht_iter iter
;
498 struct ltt_ust_domain_pid
*dpid
;
500 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dpid
, node
.node
) {
501 ret
= lttng_ht_del(ht
, &iter
);
503 destroy_channels(dpid
->channels
);
506 lttng_ht_destroy(ht
);
510 * Cleanup UST exec name domain.
512 static void destroy_domain_exec(struct lttng_ht
*ht
)
515 struct lttng_ht_iter iter
;
516 struct ltt_ust_domain_exec
*dexec
;
518 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dexec
, node
.node
) {
519 ret
= lttng_ht_del(ht
, &iter
);
521 destroy_channels(dexec
->channels
);
524 lttng_ht_destroy(ht
);
528 * Cleanup UST global domain.
530 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
532 destroy_channels(dom
->channels
);
536 * Cleanup ust session structure
538 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
540 /* Extra protection */
541 if (session
== NULL
) {
547 DBG2("Trace UST destroy session %d", session
->id
);
549 /* Cleaning up UST domain */
550 destroy_domain_global(&session
->domain_global
);
551 destroy_domain_pid(session
->domain_pid
);
552 destroy_domain_exec(session
->domain_exec
);