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