Update following loglevel support UST API change
[lttng-tools.git] / lttng-sessiond / trace-ust.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <lttngerr.h>
25 #include <lttng-share.h>
26
27 #include "hashtable.h"
28 #include "trace-ust.h"
29
30 /*
31 * Find the channel in the hashtable.
32 */
33 struct ltt_ust_channel *trace_ust_find_channel_by_name(struct cds_lfht *ht,
34 char *name)
35 {
36 struct cds_lfht_node *node;
37 struct cds_lfht_iter iter;
38
39 rcu_read_lock();
40 node = hashtable_lookup(ht, (void *) name, strlen(name), &iter);
41 if (node == NULL) {
42 rcu_read_unlock();
43 goto error;
44 }
45 rcu_read_unlock();
46
47 DBG2("Trace UST channel %s found by name", name);
48
49 return caa_container_of(node, struct ltt_ust_channel, node);
50
51 error:
52 DBG2("Trace UST channel %s not found by name", name);
53 return NULL;
54 }
55
56 /*
57 * Find the event in the hashtable.
58 */
59 struct ltt_ust_event *trace_ust_find_event_by_name(struct cds_lfht *ht,
60 char *name)
61 {
62 struct cds_lfht_node *node;
63 struct cds_lfht_iter iter;
64
65 rcu_read_lock();
66 node = hashtable_lookup(ht, (void *) name, strlen(name), &iter);
67 if (node == NULL) {
68 rcu_read_unlock();
69 goto error;
70 }
71 rcu_read_unlock();
72
73 DBG2("Trace UST event found by name %s", name);
74
75 return caa_container_of(node, struct ltt_ust_event, node);
76
77 error:
78 DBG2("Trace UST event NOT found by name %s", name);
79 return NULL;
80 }
81
82 /*
83 * Allocate and initialize a ust session data structure.
84 *
85 * Return pointer to structure or NULL.
86 */
87 struct ltt_ust_session *trace_ust_create_session(char *path, unsigned int uid,
88 struct lttng_domain *domain)
89 {
90 int ret;
91 struct ltt_ust_session *lus;
92
93 /* Allocate a new ltt ust session */
94 lus = zmalloc(sizeof(struct ltt_ust_session));
95 if (lus == NULL) {
96 PERROR("create ust session zmalloc");
97 goto error;
98 }
99
100 /* Init data structure */
101 lus->uid = uid;
102 lus->start_trace = 0;
103
104 /* Alloc UST domain hash tables */
105 lus->domain_pid = hashtable_new(0);
106 lus->domain_exec = hashtable_new_str(0);
107
108 /* Alloc UST global domain channels' HT */
109 lus->domain_global.channels = hashtable_new_str(0);
110
111 /* Set session path */
112 ret = snprintf(lus->pathname, PATH_MAX, "%s/ust", path);
113 if (ret < 0) {
114 PERROR("snprintf kernel traces path");
115 goto error_free_session;
116 }
117
118 DBG2("UST trace session create successful");
119
120 return lus;
121
122 error_free_session:
123 free(lus);
124 error:
125 return NULL;
126 }
127
128 /*
129 * Allocate and initialize a ust channel data structure.
130 *
131 * Return pointer to structure or NULL.
132 */
133 struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
134 char *path)
135 {
136 int ret;
137 struct ltt_ust_channel *luc;
138
139 luc = zmalloc(sizeof(struct ltt_ust_channel));
140 if (luc == NULL) {
141 perror("ltt_ust_channel zmalloc");
142 goto error;
143 }
144
145 /* Copy UST channel attributes */
146 luc->attr.overwrite = chan->attr.overwrite;
147 luc->attr.subbuf_size = chan->attr.subbuf_size;
148 luc->attr.num_subbuf = chan->attr.num_subbuf;
149 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
150 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
151 luc->attr.output = chan->attr.output;
152
153 /* Translate to UST output enum */
154 switch (luc->attr.output) {
155 default:
156 luc->attr.output = LTTNG_UST_MMAP;
157 break;
158 }
159
160 /* Copy channel name */
161 strncpy(luc->name, chan->name, sizeof(&luc->name));
162 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
163
164 /* Init node */
165 hashtable_node_init(&luc->node, (void *) luc->name, strlen(luc->name));
166 /* Alloc hash tables */
167 luc->events = hashtable_new_str(0);
168 luc->ctx = hashtable_new(0);
169
170 /* Set trace output path */
171 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
172 if (ret < 0) {
173 perror("asprintf ust create channel");
174 goto error_free_channel;
175 }
176
177 DBG2("Trace UST channel %s created", luc->name);
178
179 return luc;
180
181 error_free_channel:
182 free(luc);
183 error:
184 return NULL;
185 }
186
187 /*
188 * Allocate and initialize a ust event. Set name and event type.
189 *
190 * Return pointer to structure or NULL.
191 */
192 struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
193 {
194 struct ltt_ust_event *lue;
195
196 lue = zmalloc(sizeof(struct ltt_ust_event));
197 if (lue == NULL) {
198 PERROR("ust event zmalloc");
199 goto error;
200 }
201
202 switch (ev->type) {
203 case LTTNG_EVENT_PROBE:
204 lue->attr.instrumentation = LTTNG_UST_PROBE;
205 break;
206 case LTTNG_EVENT_FUNCTION:
207 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
208 break;
209 case LTTNG_EVENT_FUNCTION_ENTRY:
210 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
211 break;
212 case LTTNG_EVENT_TRACEPOINT:
213 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
214 break;
215 default:
216 ERR("Unknown ust instrumentation type (%d)", ev->type);
217 goto error_free_event;
218 }
219
220 /* Copy event name */
221 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
222 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
223
224 /* Init node */
225 hashtable_node_init(&lue->node, (void *) lue->attr.name,
226 strlen(lue->attr.name));
227 /* Alloc context hash tables */
228 lue->ctx = hashtable_new(0);
229
230 DBG2("Trace UST event %s created", lue->attr.name);
231
232 return lue;
233
234 error_free_event:
235 free(lue);
236 error:
237 return NULL;
238 }
239
240 /*
241 * Allocate and initialize a ust metadata.
242 *
243 * Return pointer to structure or NULL.
244 */
245 struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
246 {
247 int ret;
248 struct ltt_ust_metadata *lum;
249
250 lum = zmalloc(sizeof(struct ltt_ust_metadata));
251 if (lum == NULL) {
252 perror("ust metadata zmalloc");
253 goto error;
254 }
255
256 /* Set default attributes */
257 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
258 lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
259 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
260 lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
261 lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
262 lum->attr.output = LTTNG_UST_MMAP;
263
264 lum->handle = -1;
265 /* Set metadata trace path */
266 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
267 if (ret < 0) {
268 perror("asprintf ust metadata");
269 goto error_free_metadata;
270 }
271
272 return lum;
273
274 error_free_metadata:
275 free(lum);
276 error:
277 return NULL;
278 }
279
280 /*
281 * Allocate and initialize an UST context.
282 *
283 * Return pointer to structure or NULL.
284 */
285 struct ltt_ust_context *trace_ust_create_context(
286 struct lttng_event_context *ctx)
287 {
288 struct ltt_ust_context *uctx;
289
290 uctx = zmalloc(sizeof(struct ltt_ust_context));
291 if (uctx == NULL) {
292 PERROR("zmalloc ltt_ust_context");
293 goto error;
294 }
295
296 uctx->ctx.ctx = ctx->ctx;
297 hashtable_node_init(&uctx->node, (void *)((unsigned long) uctx->ctx.ctx),
298 sizeof(void *));
299
300 return uctx;
301
302 error:
303 return NULL;
304 }
305
306 /*
307 * RCU safe free context structure.
308 */
309 static void destroy_context_rcu(struct rcu_head *head)
310 {
311 struct cds_lfht_node *node =
312 caa_container_of(head, struct cds_lfht_node, head);
313 struct ltt_ust_context *ctx =
314 caa_container_of(node, struct ltt_ust_context, node);
315
316 free(ctx);
317 }
318
319 /*
320 * Cleanup UST context hash table.
321 */
322 static void destroy_context(struct cds_lfht *ht)
323 {
324 int ret;
325 struct cds_lfht_node *node;
326 struct cds_lfht_iter iter;
327
328 cds_lfht_for_each(ht, &iter, node) {
329 ret = hashtable_del(ht, &iter);
330 if (!ret) {
331 call_rcu(&node->head, destroy_context_rcu);
332 }
333 }
334
335 hashtable_destroy(ht);
336 }
337
338 /*
339 * Cleanup ust event structure.
340 */
341 void trace_ust_destroy_event(struct ltt_ust_event *event)
342 {
343 DBG2("Trace destroy UST event %s", event->attr.name);
344 destroy_context(event->ctx);
345
346 free(event);
347 }
348
349 /*
350 * URCU intermediate call to complete destroy event.
351 */
352 static void destroy_event_rcu(struct rcu_head *head)
353 {
354 struct cds_lfht_node *node =
355 caa_container_of(head, struct cds_lfht_node, head);
356 struct ltt_ust_event *event =
357 caa_container_of(node, struct ltt_ust_event, node);
358
359 trace_ust_destroy_event(event);
360 }
361
362 /*
363 * Cleanup UST events hashtable.
364 */
365 static void destroy_event(struct cds_lfht *events)
366 {
367 int ret;
368 struct cds_lfht_node *node;
369 struct cds_lfht_iter iter;
370
371 cds_lfht_for_each(events, &iter, node) {
372 ret = hashtable_del(events, &iter);
373 if (!ret) {
374 call_rcu(&node->head, destroy_event_rcu);
375 }
376 }
377
378 hashtable_destroy(events);
379 }
380
381 /*
382 * Cleanup ust channel structure.
383 */
384 void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
385 {
386 int ret;
387 struct cds_lfht_node *node;
388 struct cds_lfht_iter iter;
389
390 DBG2("Trace destroy UST channel %s", channel->name);
391
392 rcu_read_lock();
393
394 cds_lfht_for_each(channel->events, &iter, node) {
395 ret = hashtable_del(channel->events, &iter);
396 if (!ret) {
397 destroy_event(channel->events);
398 }
399 }
400
401 destroy_context(channel->ctx);
402 free(channel);
403
404 rcu_read_unlock();
405 }
406
407 /*
408 * URCU intermediate call to complete destroy channel.
409 */
410 static void destroy_channel_rcu(struct rcu_head *head)
411 {
412 struct cds_lfht_node *node =
413 caa_container_of(head, struct cds_lfht_node, head);
414 struct ltt_ust_channel *channel =
415 caa_container_of(node, struct ltt_ust_channel, node);
416
417 trace_ust_destroy_channel(channel);
418 }
419
420 /*
421 * Cleanup ust metadata structure.
422 */
423 void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
424 {
425 DBG2("Trace UST destroy metadata %d", metadata->handle);
426
427 free(metadata);
428 }
429
430 /*
431 * Iterate over a hash table containing channels and cleanup safely.
432 */
433 static void destroy_channels(struct cds_lfht *channels)
434 {
435 int ret;
436 struct cds_lfht_node *node;
437 struct cds_lfht_iter iter;
438
439 cds_lfht_for_each(channels, &iter, node) {
440 ret = hashtable_del(channels, &iter);
441 if (!ret) {
442 call_rcu(&node->head, destroy_channel_rcu);
443 }
444 }
445
446 hashtable_destroy(channels);
447 }
448
449 /*
450 * Cleanup UST pid domain.
451 */
452 static void destroy_domain_pid(struct cds_lfht *ht)
453 {
454 int ret;
455 struct cds_lfht_iter iter;
456 struct ltt_ust_domain_pid *dpid;
457
458 cds_lfht_for_each_entry(ht, &iter, dpid, node) {
459 ret = hashtable_del(ht , &iter);
460 if (!ret) {
461 destroy_channels(dpid->channels);
462 }
463 }
464
465 hashtable_destroy(ht);
466 }
467
468 /*
469 * Cleanup UST exec name domain.
470 */
471 static void destroy_domain_exec(struct cds_lfht *ht)
472 {
473 int ret;
474 struct cds_lfht_iter iter;
475 struct ltt_ust_domain_exec *dexec;
476
477 cds_lfht_for_each_entry(ht, &iter, dexec, node) {
478 ret = hashtable_del(ht , &iter);
479 if (!ret) {
480 destroy_channels(dexec->channels);
481 }
482 }
483
484 hashtable_destroy(ht);
485 }
486
487 /*
488 * Cleanup UST global domain.
489 */
490 static void destroy_domain_global(struct ltt_ust_domain_global *dom)
491 {
492 destroy_channels(dom->channels);
493 }
494
495 /*
496 * Cleanup ust session structure
497 */
498 void trace_ust_destroy_session(struct ltt_ust_session *session)
499 {
500 /* Extra protection */
501 if (session == NULL) {
502 return;
503 }
504
505 rcu_read_lock();
506
507 DBG2("Trace UST destroy session %d", session->uid);
508
509 /* Cleaning up UST domain */
510 destroy_domain_global(&session->domain_global);
511 destroy_domain_pid(session->domain_pid);
512 destroy_domain_exec(session->domain_exec);
513
514 free(session);
515
516 rcu_read_unlock();
517 }
This page took 0.041291 seconds and 4 git commands to generate.