2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, 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");
130 * Allocate and initialize a ust channel data structure.
132 * Return pointer to structure or NULL.
134 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
138 struct ltt_ust_channel
*luc
;
140 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
142 perror("ltt_ust_channel zmalloc");
146 /* Copy UST channel attributes */
147 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
148 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
149 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
150 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
151 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
152 luc
->attr
.output
= chan
->attr
.output
;
154 /* Translate to UST output enum */
155 switch (luc
->attr
.output
) {
157 luc
->attr
.output
= LTTNG_UST_MMAP
;
161 /* Copy channel name */
162 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
163 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
166 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
167 /* Alloc hash tables */
168 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
169 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
171 /* Set trace output path */
172 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
174 perror("asprintf ust create channel");
175 goto error_free_channel
;
178 DBG2("Trace UST channel %s created", luc
->name
);
189 * Allocate and initialize a ust event. Set name and event type.
191 * Return pointer to structure or NULL.
193 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
195 struct ltt_ust_event
*lue
;
197 lue
= zmalloc(sizeof(struct ltt_ust_event
));
199 PERROR("ust event zmalloc");
204 case LTTNG_EVENT_PROBE
:
205 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
207 case LTTNG_EVENT_FUNCTION
:
208 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
210 case LTTNG_EVENT_FUNCTION_ENTRY
:
211 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
213 case LTTNG_EVENT_TRACEPOINT
:
214 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
217 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
218 goto error_free_event
;
221 /* Copy event name */
222 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
223 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
225 switch (ev
->loglevel_type
) {
226 case LTTNG_EVENT_LOGLEVEL_ALL
:
227 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
229 case LTTNG_EVENT_LOGLEVEL_RANGE
:
230 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
232 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
233 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
236 ERR("Unknown ust loglevel type (%d)", ev
->type
);
237 goto error_free_event
;
241 lue
->attr
.loglevel
= ev
->loglevel
;
244 lttng_ht_node_init_str(&lue
->node
, lue
->attr
.name
);
245 /* Alloc context hash tables */
246 lue
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
248 DBG2("Trace UST event %s, loglevel (%d,%d) created",
249 lue
->attr
.name
, lue
->attr
.loglevel_type
,
261 * Allocate and initialize a ust metadata.
263 * Return pointer to structure or NULL.
265 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
268 struct ltt_ust_metadata
*lum
;
270 lum
= zmalloc(sizeof(struct ltt_ust_metadata
));
272 perror("ust metadata zmalloc");
276 /* Set default attributes */
277 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
278 lum
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
279 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
280 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
281 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
282 lum
->attr
.output
= LTTNG_UST_MMAP
;
285 /* Set metadata trace path */
286 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
288 perror("asprintf ust metadata");
289 goto error_free_metadata
;
301 * Allocate and initialize an UST context.
303 * Return pointer to structure or NULL.
305 struct ltt_ust_context
*trace_ust_create_context(
306 struct lttng_event_context
*ctx
)
308 struct ltt_ust_context
*uctx
;
309 enum lttng_ust_context_type utype
;
312 case LTTNG_EVENT_CONTEXT_VTID
:
313 utype
= LTTNG_UST_CONTEXT_VTID
;
315 case LTTNG_EVENT_CONTEXT_VPID
:
316 utype
= LTTNG_UST_CONTEXT_VPID
;
318 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
319 utype
= LTTNG_UST_CONTEXT_PTHREAD_ID
;
321 case LTTNG_EVENT_CONTEXT_PROCNAME
:
322 utype
= LTTNG_UST_CONTEXT_PROCNAME
;
325 ERR("Invalid UST context");
329 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
331 PERROR("zmalloc ltt_ust_context");
335 uctx
->ctx
.ctx
= utype
;
336 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
345 * RCU safe free context structure.
347 static void destroy_context_rcu(struct rcu_head
*head
)
349 struct lttng_ht_node_ulong
*node
=
350 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
351 struct ltt_ust_context
*ctx
=
352 caa_container_of(node
, struct ltt_ust_context
, node
);
358 * Cleanup UST context hash table.
360 static void destroy_contexts(struct lttng_ht
*ht
)
363 struct lttng_ht_node_ulong
*node
;
364 struct lttng_ht_iter iter
;
366 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
367 ret
= lttng_ht_del(ht
, &iter
);
369 call_rcu(&node
->head
, destroy_context_rcu
);
373 lttng_ht_destroy(ht
);
377 * Cleanup ust event structure.
379 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
381 DBG2("Trace destroy UST event %s", event
->attr
.name
);
382 destroy_contexts(event
->ctx
);
388 * URCU intermediate call to complete destroy event.
390 static void destroy_event_rcu(struct rcu_head
*head
)
392 struct lttng_ht_node_str
*node
=
393 caa_container_of(head
, struct lttng_ht_node_str
, head
);
394 struct ltt_ust_event
*event
=
395 caa_container_of(node
, struct ltt_ust_event
, node
);
397 trace_ust_destroy_event(event
);
401 * Cleanup UST events hashtable.
403 static void destroy_events(struct lttng_ht
*events
)
406 struct lttng_ht_node_str
*node
;
407 struct lttng_ht_iter iter
;
409 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
410 ret
= lttng_ht_del(events
, &iter
);
412 call_rcu(&node
->head
, destroy_event_rcu
);
415 lttng_ht_destroy(events
);
419 * Cleanup ust channel structure.
421 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
423 DBG2("Trace destroy UST channel %s", channel
->name
);
427 /* Destroying all events of the channel */
428 destroy_events(channel
->events
);
429 /* Destroying all context of the channel */
430 destroy_contexts(channel
->ctx
);
438 * URCU intermediate call to complete destroy channel.
440 static void destroy_channel_rcu(struct rcu_head
*head
)
442 struct lttng_ht_node_str
*node
=
443 caa_container_of(head
, struct lttng_ht_node_str
, head
);
444 struct ltt_ust_channel
*channel
=
445 caa_container_of(node
, struct ltt_ust_channel
, node
);
447 trace_ust_destroy_channel(channel
);
451 * Cleanup ust metadata structure.
453 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
455 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
461 * Iterate over a hash table containing channels and cleanup safely.
463 static void destroy_channels(struct lttng_ht
*channels
)
466 struct lttng_ht_node_str
*node
;
467 struct lttng_ht_iter iter
;
471 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
472 ret
= lttng_ht_del(channels
, &iter
);
474 call_rcu(&node
->head
, destroy_channel_rcu
);
477 lttng_ht_destroy(channels
);
483 * Cleanup UST pid domain.
485 static void destroy_domain_pid(struct lttng_ht
*ht
)
488 struct lttng_ht_iter iter
;
489 struct ltt_ust_domain_pid
*dpid
;
491 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dpid
, node
.node
) {
492 ret
= lttng_ht_del(ht
, &iter
);
494 destroy_channels(dpid
->channels
);
497 lttng_ht_destroy(ht
);
501 * Cleanup UST exec name domain.
503 static void destroy_domain_exec(struct lttng_ht
*ht
)
506 struct lttng_ht_iter iter
;
507 struct ltt_ust_domain_exec
*dexec
;
509 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dexec
, node
.node
) {
510 ret
= lttng_ht_del(ht
, &iter
);
512 destroy_channels(dexec
->channels
);
515 lttng_ht_destroy(ht
);
519 * Cleanup UST global domain.
521 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
523 destroy_channels(dom
->channels
);
527 * Cleanup ust session structure
529 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
531 /* Extra protection */
532 if (session
== NULL
) {
538 DBG2("Trace UST destroy session %d", session
->id
);
540 /* Cleaning up UST domain */
541 destroy_domain_global(&session
->domain_global
);
542 destroy_domain_pid(session
->domain_pid
);
543 destroy_domain_exec(session
->domain_exec
);